blob: e357d16f2dfe3e7ef80ddcbe278e717c6ae002f4 [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001/* GPRS LLC protocol implementation as per 3GPP TS 04.64 */
2
Harald Weltea2665542010-05-02 09:28:11 +02003/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte9b455bf2010-03-14 15:45:01 +08004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9b455bf2010-03-14 15:45:01 +080010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte9b455bf2010-03-14 15:45:01 +080016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9b455bf2010-03-14 15:45:01 +080019 *
20 */
21
22#include <errno.h>
Harald Welteeaa614c2010-05-02 11:26:34 +020023#include <stdint.h>
Max82040102016-07-06 11:59:18 +020024#include <stdbool.h>
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +010025#include <inttypes.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080026
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010027#include <osmocom/core/msgb.h>
28#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/timer.h>
30#include <osmocom/core/talloc.h>
Alexander Couzens4e699a92016-07-05 11:04:27 +020031#include <osmocom/core/rate_ctr.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080032#include <osmocom/gprs/gprs_bssgp.h>
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020033#include <osmocom/gsm/gsm_utils.h>
Harald Weltea2665542010-05-02 09:28:11 +020034
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020035#include <osmocom/sgsn/debug.h>
36#include <osmocom/sgsn/gprs_sgsn.h>
37#include <osmocom/sgsn/gprs_gmm.h>
38#include <osmocom/sgsn/gprs_llc.h>
39#include <osmocom/sgsn/crc24.h>
40#include <osmocom/sgsn/sgsn.h>
41#include <osmocom/sgsn/gprs_llc_xid.h>
42#include <osmocom/sgsn/gprs_sndcp_comp.h>
43#include <osmocom/sgsn/gprs_sndcp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080044
Pau Espin Pedrol5b6c4b82019-08-14 16:08:15 +020045const struct value_string gprs_llc_llme_state_names[] = {
46 { GPRS_LLMS_UNASSIGNED, "UNASSIGNED" },
47 { GPRS_LLMS_ASSIGNED, "ASSIGNED" },
48 { 0, NULL }
49};
50
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +020051static struct gprs_llc_llme *llme_alloc(uint32_t tlli);
Philipp4ac3aee2016-08-10 12:24:09 +020052static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
53 int command);
Harald Welte1f60bbe2019-04-23 23:21:14 +020054static int gprs_llc_tx_dm(struct gprs_llc_lle *lle);
Philipp4ac3aee2016-08-10 12:24:09 +020055static int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi,
56 int command, enum gprs_llc_u_cmd u_cmd, int pf_bit);
57
58/* BEGIN XID RELATED */
59
60/* Generate XID message */
61static int gprs_llc_generate_xid(uint8_t *bytes, int bytes_len,
62 struct gprs_llc_xid_field *l3_xid_field,
Harald Welteaf779d22019-04-12 16:56:04 +020063 struct gprs_llc_lle *lle)
Philipp4ac3aee2016-08-10 12:24:09 +020064{
65 /* Note: Called by gprs_ll_xid_req() */
66
67 LLIST_HEAD(xid_fields);
68
69 struct gprs_llc_xid_field xid_version;
70 struct gprs_llc_xid_field xid_n201u;
71 struct gprs_llc_xid_field xid_n201i;
Harald Welte41461212019-04-12 17:01:32 +020072 uint16_t n201_u, n201_i;
Philipp4ac3aee2016-08-10 12:24:09 +020073
74 xid_version.type = GPRS_LLC_XID_T_VERSION;
75 xid_version.data = (uint8_t *) "\x00";
76 xid_version.data_len = 1;
77
Harald Welte41461212019-04-12 17:01:32 +020078 n201_u = htons(lle->params.n201_u);
Philipp4ac3aee2016-08-10 12:24:09 +020079 xid_n201u.type = GPRS_LLC_XID_T_N201_U;
Harald Welte41461212019-04-12 17:01:32 +020080 xid_n201u.data = (uint8_t *) &n201_u;
Philipp4ac3aee2016-08-10 12:24:09 +020081 xid_n201u.data_len = 2;
82
Harald Welte41461212019-04-12 17:01:32 +020083 n201_i = htons(lle->params.n201_i);
Philipp4ac3aee2016-08-10 12:24:09 +020084 xid_n201i.type = GPRS_LLC_XID_T_N201_I;
Harald Welte41461212019-04-12 17:01:32 +020085 xid_n201i.data = (uint8_t *) &n201_i;
Philipp4ac3aee2016-08-10 12:24:09 +020086 xid_n201i.data_len = 2;
87
88 /* Add locally managed XID Fields */
Philipp4ac3aee2016-08-10 12:24:09 +020089 llist_add(&xid_version.list, &xid_fields);
Philippf788d932016-12-05 12:44:19 +010090 llist_add(&xid_n201u.list, &xid_fields);
91 llist_add(&xid_n201i.list, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +020092
93 /* Append layer 3 XID field (if present) */
94 if (l3_xid_field) {
95 /* Enforce layer 3 XID type (just to be sure) */
96 l3_xid_field->type = GPRS_LLC_XID_T_L3_PAR;
97
98 /* Add Layer 3 XID field to the list */
99 llist_add(&l3_xid_field->list, &xid_fields);
100 }
101
102 /* Store generated XID for later reference */
Harald Welteaf779d22019-04-12 16:56:04 +0200103 talloc_free(lle->xid);
104 lle->xid = gprs_llc_copy_xid(lle->llme, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +0200105
106 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
107}
108
109/* Generate XID message that will cause the GMM to reset */
110static int gprs_llc_generate_xid_for_gmm_reset(uint8_t *bytes,
111 int bytes_len, uint32_t iov_ui,
Harald Welteaf779d22019-04-12 16:56:04 +0200112 struct gprs_llc_lle *lle)
Philipp4ac3aee2016-08-10 12:24:09 +0200113{
114 /* Called by gprs_llgmm_reset() and
115 * gprs_llgmm_reset_oldmsg() */
116
117 LLIST_HEAD(xid_fields);
118
119 struct gprs_llc_xid_field xid_reset;
120 struct gprs_llc_xid_field xid_iovui;
121
122 /* First XID component must be RESET */
123 xid_reset.type = GPRS_LLC_XID_T_RESET;
124 xid_reset.data = NULL;
125 xid_reset.data_len = 0;
126
127 /* Add new IOV-UI */
128 xid_iovui.type = GPRS_LLC_XID_T_IOV_UI;
129 xid_iovui.data = (uint8_t *) & iov_ui;
130 xid_iovui.data_len = 4;
131
132 /* Add locally managed XID Fields */
133 llist_add(&xid_iovui.list, &xid_fields);
134 llist_add(&xid_reset.list, &xid_fields);
135
136 /* Store generated XID for later reference */
Harald Welteaf779d22019-04-12 16:56:04 +0200137 talloc_free(lle->xid);
138 lle->xid = gprs_llc_copy_xid(lle->llme, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +0200139
140 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
141}
142
143/* Process an incoming XID confirmation */
144static int gprs_llc_process_xid_conf(uint8_t *bytes, int bytes_len,
145 struct gprs_llc_lle *lle)
146{
147 /* Note: This function handles the response of a network originated
Philipp532480a2016-12-23 11:05:11 +0100148 * XID-Request. There XID messages reflected by the MS are analyzed
Philipp4ac3aee2016-08-10 12:24:09 +0200149 * and processed here. The caller is called by rx_llc_xid(). */
150
151 struct llist_head *xid_fields;
152 struct gprs_llc_xid_field *xid_field;
Philippf1f34362016-08-26 17:00:21 +0200153 struct gprs_llc_xid_field *xid_field_request;
154 struct gprs_llc_xid_field *xid_field_request_l3 = NULL;
155
156 /* Pick layer3 XID from the XID request we have sent last */
Harald Welteaf779d22019-04-12 16:56:04 +0200157 if (lle->xid) {
158 llist_for_each_entry(xid_field_request, lle->xid, list) {
Philippf1f34362016-08-26 17:00:21 +0200159 if (xid_field_request->type == GPRS_LLC_XID_T_L3_PAR)
160 xid_field_request_l3 = xid_field_request;
161 }
162 }
Philipp4ac3aee2016-08-10 12:24:09 +0200163
164 /* Parse and analyze XID-Response */
165 xid_fields = gprs_llc_parse_xid(NULL, bytes, bytes_len);
166
167 if (xid_fields) {
168
169 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
170 llist_for_each_entry(xid_field, xid_fields, list) {
171
172 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
Philippf1f34362016-08-26 17:00:21 +0200173 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR &&
174 xid_field_request_l3) {
175 sndcp_sn_xid_conf(xid_field,
176 xid_field_request_l3, lle);
Philipp4ac3aee2016-08-10 12:24:09 +0200177 }
178
179 /* Process LLC-XID fields: */
180 else {
181
182 /* FIXME: Do something more useful with the
183 * echoed XID-Information. Currently we
184 * just ignore the response completely and
185 * by doing so we blindly accept any changes
186 * the MS might have done to the our XID
187 * inquiry. There is a remainig risk of
188 * malfunction! */
189 LOGP(DLLC, LOGL_NOTICE,
Max549ebc72016-11-18 14:07:04 +0100190 "Ignoring XID-Field: XID: type %s, data_len=%d, data=%s\n",
191 get_value_string(gprs_llc_xid_type_names,
192 xid_field->type),
193 xid_field->data_len,
Philipp4ac3aee2016-08-10 12:24:09 +0200194 osmo_hexdump_nospc(xid_field->data,
195 xid_field->data_len));
196 }
197 }
198 talloc_free(xid_fields);
199 }
200
201 /* Flush pending XID fields */
Harald Welteaf779d22019-04-12 16:56:04 +0200202 talloc_free(lle->xid);
203 lle->xid = NULL;
Philipp4ac3aee2016-08-10 12:24:09 +0200204
205 return 0;
206}
207
208/* Process an incoming XID indication and generate an appropiate response */
209static int gprs_llc_process_xid_ind(uint8_t *bytes_request,
210 int bytes_request_len,
211 uint8_t *bytes_response,
212 int bytes_response_maxlen,
213 struct gprs_llc_lle *lle)
214{
215 /* Note: This function computes the response that is sent back to the
Philipp532480a2016-12-23 11:05:11 +0100216 * MS when a mobile originated XID is received. The function is
Philipp4ac3aee2016-08-10 12:24:09 +0200217 * called by rx_llc_xid() */
218
219 int rc = -EINVAL;
220
221 struct llist_head *xid_fields;
222 struct llist_head *xid_fields_response;
223
224 struct gprs_llc_xid_field *xid_field;
225 struct gprs_llc_xid_field *xid_field_response;
226
Philipp4ac3aee2016-08-10 12:24:09 +0200227 /* Parse and analyze XID-Request */
228 xid_fields =
229 gprs_llc_parse_xid(lle->llme, bytes_request, bytes_request_len);
230 if (xid_fields) {
231 xid_fields_response = talloc_zero(lle->llme, struct llist_head);
232 INIT_LLIST_HEAD(xid_fields_response);
233 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
234
235 /* Process LLC-XID fields: */
236 llist_for_each_entry(xid_field, xid_fields, list) {
237
238 if (xid_field->type != GPRS_LLC_XID_T_L3_PAR) {
239 /* FIXME: Check the incoming XID parameters for
240 * for validity. Currently we just blindly
241 * accept all XID fields by just echoing them.
242 * There is a remaining risk of malfunction
Philipp532480a2016-12-23 11:05:11 +0100243 * when a MS submits values which defer from
Philipp4ac3aee2016-08-10 12:24:09 +0200244 * the default! */
245 LOGP(DLLC, LOGL_NOTICE,
Max549ebc72016-11-18 14:07:04 +0100246 "Echoing XID-Field: XID: type %s, data_len=%d, data=%s\n",
247 get_value_string(gprs_llc_xid_type_names,
248 xid_field->type),
249 xid_field->data_len,
Philipp4ac3aee2016-08-10 12:24:09 +0200250 osmo_hexdump_nospc(xid_field->data,
251 xid_field->data_len));
252 xid_field_response =
253 gprs_llc_dup_xid_field
254 (lle->llme, xid_field);
255 llist_add(&xid_field_response->list,
256 xid_fields_response);
257 }
258 }
259
Philippf1f34362016-08-26 17:00:21 +0200260 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
261 llist_for_each_entry(xid_field, xid_fields, list) {
262 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR) {
263
264 xid_field_response =
265 talloc_zero(lle->llme,
266 struct gprs_llc_xid_field);
267 rc = sndcp_sn_xid_ind(xid_field,
268 xid_field_response, lle);
269 if (rc == 0)
270 llist_add(&xid_field_response->list,
271 xid_fields_response);
272 else
273 talloc_free(xid_field_response);
274 }
275 }
276
Philipp4ac3aee2016-08-10 12:24:09 +0200277 rc = gprs_llc_compile_xid(bytes_response,
278 bytes_response_maxlen,
279 xid_fields_response);
280 talloc_free(xid_fields_response);
281 talloc_free(xid_fields);
282 }
283
284 return rc;
285}
286
Philipp532480a2016-12-23 11:05:11 +0100287/* Dispatch XID indications and responses comming from the MS */
Philipp4ac3aee2016-08-10 12:24:09 +0200288static void rx_llc_xid(struct gprs_llc_lle *lle,
289 struct gprs_llc_hdr_parsed *gph)
290{
291 uint8_t response[1024];
292 int response_len;
293
294 /* FIXME: 8.5.3.3: check if XID is invalid */
295 if (gph->is_cmd) {
296 LOGP(DLLC, LOGL_NOTICE,
Philipp532480a2016-12-23 11:05:11 +0100297 "Received XID indication from MS.\n");
Philipp4ac3aee2016-08-10 12:24:09 +0200298
299 struct msgb *resp;
300 uint8_t *xid;
301 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
302
303 response_len =
304 gprs_llc_process_xid_ind(gph->data, gph->data_len,
305 response, sizeof(response),
306 lle);
Philippf1f34362016-08-26 17:00:21 +0200307 if (response_len < 0) {
308 LOGP(DLLC, LOGL_ERROR,
309 "invalid XID indication received!\n");
310 } else {
311 xid = msgb_put(resp, response_len);
312 memcpy(xid, response, response_len);
313 }
Philipp4ac3aee2016-08-10 12:24:09 +0200314 gprs_llc_tx_xid(lle, resp, 0);
315 } else {
316 LOGP(DLLC, LOGL_NOTICE,
Philipp532480a2016-12-23 11:05:11 +0100317 "Received XID confirmation from MS.\n");
Philipp4ac3aee2016-08-10 12:24:09 +0200318 gprs_llc_process_xid_conf(gph->data, gph->data_len, lle);
319 /* FIXME: if we had sent a XID reset, send
320 * LLGMM-RESET.conf to GMM */
321 }
322}
323
Philipp4ac3aee2016-08-10 12:24:09 +0200324/* Set of LL-XID negotiation (See also: TS 101 351, Section 7.2.2.4) */
325int gprs_ll_xid_req(struct gprs_llc_lle *lle,
326 struct gprs_llc_xid_field *l3_xid_field)
327{
328 /* Note: This functions is calle from gprs_sndcp.c */
329
330 uint8_t xid_bytes[1024];;
331 int xid_bytes_len;
332 uint8_t *xid;
333 struct msgb *msg;
Max549ebc72016-11-18 14:07:04 +0100334 const char *ftype;
Philipp4ac3aee2016-08-10 12:24:09 +0200335
336 /* Generate XID */
337 xid_bytes_len =
Harald Welteaf779d22019-04-12 16:56:04 +0200338 gprs_llc_generate_xid(xid_bytes, sizeof(xid_bytes), l3_xid_field, lle);
Philipp4ac3aee2016-08-10 12:24:09 +0200339
340 /* Only perform XID sending if the XID message contains something */
341 if (xid_bytes_len > 0) {
342 /* Transmit XID bytes */
343 msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
344 xid = msgb_put(msg, xid_bytes_len);
345 memcpy(xid, xid_bytes, xid_bytes_len);
Max549ebc72016-11-18 14:07:04 +0100346 if (l3_xid_field)
347 ftype = get_value_string(gprs_llc_xid_type_names,
348 l3_xid_field->type);
349 else
350 ftype = "NULL";
351 LOGP(DLLC, LOGL_NOTICE, "Sending XID type %s (%d bytes) request"
Philipp532480a2016-12-23 11:05:11 +0100352 " to MS...\n", ftype, xid_bytes_len);
Philipp4ac3aee2016-08-10 12:24:09 +0200353 gprs_llc_tx_xid(lle, msg, 1);
354 } else {
355 LOGP(DLLC, LOGL_ERROR,
356 "XID-Message generation failed, XID not sent!\n");
357 return -EINVAL;
358 }
359
360 return 0;
361}
362/* END XID RELATED */
363
364
365
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200366
Harald Weltefaa70ff2012-06-17 09:31:16 +0800367/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
368 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
369static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
370{
371 struct bssgp_dl_ud_par dup;
372 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
373
Harald Welte8c004962012-07-04 21:53:12 +0200374 memset(&dup, 0, sizeof(dup));
375 /* before we have received some identity from the MS, we might
376 * not yet have a MMC context (e.g. XID negotiation of primarly
Philipp4ac3aee2016-08-10 12:24:09 +0200377 * LLC connection from GMM sapi). */
Harald Welte8c004962012-07-04 21:53:12 +0200378 if (mmctx) {
Alexander Couzensd3c3dde2020-09-18 18:28:33 +0200379 /* In rare cases the LLME is NULL in those cases don't
380 * use the mm radio capabilities */
Harald Welte8c004962012-07-04 21:53:12 +0200381 dup.imsi = mmctx->imsi;
Alexander Couzensd3c3dde2020-09-18 18:28:33 +0200382 if (mmctx->gb.llme) {
383 dup.drx_parms = mmctx->drx_parms;
384 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
385 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +0200386
Alexander Couzensd3c3dde2020-09-18 18:28:33 +0200387 /* make sure we only send it to the right llme */
388 if (!(msgb_tlli(msg) == mmctx->gb.llme->tlli
389 || msgb_tlli(msg) == mmctx->gb.llme->old_tlli)) {
390 LOGP(DLLC, LOGL_ERROR,
391 "_bssgp_tx_dl_ud(): Attempt to send Downlink Unitdata to wrong LLME:"
392 " msgb_tlli=0x%x mmctx->gb.llme->tlli=0x%x ->old_tlli=0x%x\n",
393 msgb_tlli(msg), mmctx->gb.llme->tlli, mmctx->gb.llme->old_tlli);
394 msgb_free(msg);
395 return -EINVAL;
396 }
Neels Hofmeyr188a91c2017-12-27 17:29:04 +0100397 }
Harald Welte8c004962012-07-04 21:53:12 +0200398 }
Harald Weltefaa70ff2012-06-17 09:31:16 +0800399 memcpy(&dup.qos_profile, qos_profile_default,
400 sizeof(qos_profile_default));
401
Harald Weltece95b272012-06-17 13:04:02 +0800402 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +0800403}
404
405
Harald Welte1d9d9442010-06-03 07:11:04 +0200406/* Section 8.9.9 LLC layer parameter default values */
Daniel Willmann46d13262014-06-27 17:05:48 +0200407static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = {
Harald Welte1d9d9442010-06-03 07:11:04 +0200408 [1] = {
409 .t200_201 = 5,
410 .n200 = 3,
411 .n201_u = 400,
412 },
413 [2] = {
414 .t200_201 = 5,
415 .n200 = 3,
416 .n201_u = 270,
417 },
418 [3] = {
419 .iov_i_exp = 27,
420 .t200_201 = 5,
421 .n200 = 3,
422 .n201_u = 500,
423 .n201_i = 1503,
424 .mD = 1520,
425 .mU = 1520,
426 .kD = 16,
427 .kU = 16,
428 },
429 [5] = {
430 .iov_i_exp = 27,
431 .t200_201 = 10,
432 .n200 = 3,
433 .n201_u = 500,
434 .n201_i = 1503,
435 .mD = 760,
436 .mU = 760,
437 .kD = 8,
438 .kU = 8,
439 },
440 [7] = {
441 .t200_201 = 20,
442 .n200 = 3,
443 .n201_u = 270,
444 },
445 [8] = {
446 .t200_201 = 20,
447 .n200 = 3,
448 .n201_u = 270,
449 },
450 [9] = {
451 .iov_i_exp = 27,
452 .t200_201 = 20,
453 .n200 = 3,
454 .n201_u = 500,
455 .n201_i = 1503,
456 .mD = 380,
457 .mU = 380,
458 .kD = 4,
459 .kU = 4,
460 },
461 [11] = {
462 .iov_i_exp = 27,
463 .t200_201 = 40,
464 .n200 = 3,
465 .n201_u = 500,
466 .n201_i = 1503,
467 .mD = 190,
468 .mU = 190,
469 .kD = 2,
470 .kU = 2,
471 },
472};
473
Harald Welte807a5d82010-06-01 11:53:01 +0200474LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200475void *llc_tall_ctx;
476
477/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200478static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200479{
Harald Welte807a5d82010-06-01 11:53:01 +0200480 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200481
Harald Welte807a5d82010-06-01 11:53:01 +0200482 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
483 if (llme->tlli == tlli || llme->old_tlli == tlli)
484 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200485 }
486 return NULL;
487}
488
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200489struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi)
490{
491 struct gprs_llc_llme *llme;
492 struct gprs_llc_lle *lle;
493
494 lle = lle_by_tlli_sapi(tlli, sapi);
495 if (lle)
496 return lle;
497
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200498 LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, "
499 "creating LLME on the fly\n", tlli);
500 llme = llme_alloc(tlli);
501 lle = &llme->lle[sapi];
502 return lle;
503}
504
505struct llist_head *gprs_llme_list(void)
506{
507 return &gprs_llc_llmes;
508}
509
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200510/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
511static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
512 uint8_t sapi, enum gprs_llc_cmd cmd)
513{
514 struct gprs_llc_lle *lle;
515
516 /* We already know about this TLLI */
517 lle = lle_by_tlli_sapi(tlli, sapi);
518 if (lle)
519 return lle;
520
521 /* Maybe it is a routing area update but we already know this sapi? */
522 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
Jacob Erlbeck3fbf0a32016-01-04 18:43:32 +0100523 lle = lle_by_tlli_sapi(tlli, sapi);
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200524 if (lle) {
525 LOGP(DLLC, LOGL_NOTICE,
526 "LLC RX: Found a local entry for TLLI 0x%08x\n",
527 tlli);
528 return lle;
529 }
530 }
531
532 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
533 * except UID and XID frames with SAPI=1 */
534 if (sapi == GPRS_SAPI_GMM &&
535 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
536 struct gprs_llc_llme *llme;
537 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
538 llme = llme_alloc(tlli);
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +0100539 LOGGBP(llme, DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, "
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200540 "creating LLME on the fly\n", tlli);
541 lle = &llme->lle[sapi];
542 return lle;
543 }
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +0200544
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200545 LOGP(DLLC, LOGL_NOTICE,
546 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
547 tlli, sapi);
548 return NULL;
549}
550
Harald Welte1d9d9442010-06-03 07:11:04 +0200551static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200552{
Harald Welte807a5d82010-06-01 11:53:01 +0200553 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200554
Harald Welte807a5d82010-06-01 11:53:01 +0200555 lle->llme = llme;
556 lle->sapi = sapi;
557 lle->state = GPRS_LLES_UNASSIGNED;
558
Harald Welte1d9d9442010-06-03 07:11:04 +0200559 /* Initialize according to parameters */
560 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200561}
562
563static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
564{
565 struct gprs_llc_llme *llme;
566 uint32_t i;
567
568 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
569 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200570 return NULL;
571
Harald Welte807a5d82010-06-01 11:53:01 +0200572 llme->tlli = tlli;
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +0200573 llme->old_tlli = TLLI_UNASSIGNED;
Harald Welte807a5d82010-06-01 11:53:01 +0200574 llme->state = GPRS_LLMS_UNASSIGNED;
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100575 llme->age_timestamp = GPRS_LLME_RESET_AGE;
Max5aa51962016-07-06 11:33:04 +0200576 llme->cksn = GSM_KEY_SEQ_INVAL;
Harald Weltea2665542010-05-02 09:28:11 +0200577
Harald Welte807a5d82010-06-01 11:53:01 +0200578 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
579 lle_init(llme, i);
580
581 llist_add(&llme->list, &gprs_llc_llmes);
582
Philippf1f34362016-08-26 17:00:21 +0200583 llme->comp.proto = gprs_sndcp_comp_alloc(llme);
584 llme->comp.data = gprs_sndcp_comp_alloc(llme);
585
Harald Welte807a5d82010-06-01 11:53:01 +0200586 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200587}
588
Harald Weltef7fef482010-06-28 22:18:26 +0200589static void llme_free(struct gprs_llc_llme *llme)
590{
Philippf1f34362016-08-26 17:00:21 +0200591 gprs_sndcp_comp_free(llme->comp.proto);
592 gprs_sndcp_comp_free(llme->comp.data);
Harald Weltef7fef482010-06-28 22:18:26 +0200593 llist_del(&llme->list);
594 talloc_free(llme);
595}
596
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200597#if 0
598/* FIXME: Unused code... */
Harald Welte9b455bf2010-03-14 15:45:01 +0800599static void t200_expired(void *data)
600{
601 struct gprs_llc_lle *lle = data;
602
603 /* 8.5.1.3: Expiry of T200 */
604
Harald Welte1d9d9442010-06-03 07:11:04 +0200605 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800606 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200607 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800608 }
609
610 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200611 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200612 /* FIXME: retransmit SABM */
613 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800614 lle->retrans_ctr++;
615 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200616 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200617 /* FIXME: retransmit DISC */
618 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800619 lle->retrans_ctr++;
620 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200621 default:
622 LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state);
623 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800624 }
625
626}
627
628static void t201_expired(void *data)
629{
630 struct gprs_llc_lle *lle = data;
631
Harald Welte1d9d9442010-06-03 07:11:04 +0200632 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200633 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
634 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800635 lle->retrans_ctr++;
636 }
637}
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200638#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800639
Harald Welte10997d02010-05-03 12:28:12 +0200640int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
641 enum gprs_llc_u_cmd u_cmd, int pf_bit)
642{
643 uint8_t *fcs, *llch;
644 uint8_t addr, ctrl;
645 uint32_t fcs_calc;
646
647 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
648
649 /* Address Field */
650 addr = sapi & 0xf;
651 if (command)
652 addr |= 0x40;
653
654 /* 6.3 Figure 8 */
655 ctrl = 0xe0 | u_cmd;
656 if (pf_bit)
657 ctrl |= 0x10;
658
659 /* prepend LLC UI header */
660 llch = msgb_push(msg, 2);
661 llch[0] = addr;
662 llch[1] = ctrl;
663
664 /* append FCS to end of frame */
665 fcs = msgb_put(msg, 3);
666 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
667 fcs[0] = fcs_calc & 0xff;
668 fcs[1] = (fcs_calc >> 8) & 0xff;
669 fcs[2] = (fcs_calc >> 16) & 0xff;
670
671 /* Identifiers passed down: (BVCI, NSEI) */
672
Alexander Couzens4e699a92016-07-05 11:04:27 +0200673 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_PACKETS]);
674 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_BYTES], msg->len);
675
Harald Welte1ae09c72010-05-13 19:22:55 +0200676 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800677 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200678}
679
680/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200681static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
682 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200683{
684 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200685 msgb_tlli(msg) = lle->llme->tlli;
686 msgb_bvci(msg) = lle->llme->bvci;
687 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200688
Harald Welte0c1a3032011-10-16 18:49:05 +0200689 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200690}
691
Harald Welte1f60bbe2019-04-23 23:21:14 +0200692static int gprs_llc_tx_dm(struct gprs_llc_lle *lle)
693{
694 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_DM");
695
696 /* copy identifiers from LLE to ensure lower layers can route */
697 msgb_tlli(msg) = lle->llme->tlli;
698 msgb_bvci(msg) = lle->llme->bvci;
699 msgb_nsei(msg) = lle->llme->nsei;
700
701 return gprs_llc_tx_u(msg, lle->sapi, 0, GPRS_LLC_U_DM_RESP, 1);
702}
703
Max1de15912016-07-11 12:42:12 +0200704/* encrypt information field + FCS, if needed! */
705static int apply_gea(struct gprs_llc_lle *lle, uint16_t crypt_len, uint16_t nu,
706 uint32_t oc, uint8_t sapi, uint8_t *fcs, uint8_t *data)
707{
708 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
709
710 if (lle->llme->algo == GPRS_ALGO_GEA0)
711 return -EINVAL;
712
713 /* Compute the 'Input' Paraemeter */
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200714 uint32_t fcs_calc, iv = gprs_cipher_gen_input_ui(lle->llme->iov_ui, sapi,
Max1de15912016-07-11 12:42:12 +0200715 nu, oc);
716 /* Compute gamma that we need to XOR with the data */
717 int r = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
718 lle->llme->kc, iv,
719 fcs ? GPRS_CIPH_SGSN2MS : GPRS_CIPH_MS2SGSN);
720 if (r < 0) {
721 LOGP(DLLC, LOGL_ERROR, "Error producing %s gamma for UI "
722 "frame: %d\n", get_value_string(gprs_cipher_names,
723 lle->llme->algo), r);
724 return -ENOMSG;
725 }
726
727 if (fcs) {
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200728 /* Mark frame as encrypted and update FCS */
729 data[2] |= 0x02;
730 fcs_calc = gprs_llc_fcs(data, fcs - data);
731 fcs[0] = fcs_calc & 0xff;
732 fcs[1] = (fcs_calc >> 8) & 0xff;
733 fcs[2] = (fcs_calc >> 16) & 0xff;
Max1de15912016-07-11 12:42:12 +0200734 data += 3;
735 }
736
737 /* XOR the cipher output with the data */
738 for (r = 0; r < crypt_len; r++)
739 *(data + r) ^= cipher_out[r];
740
741 return 0;
742}
743
Max82040102016-07-06 11:59:18 +0200744/* Transmit a UI frame over the given SAPI:
745 'encryptable' indicates whether particular message can be encrypted according
746 to 3GPP TS 24.008 § 4.7.1.2
747 */
Harald Welte56a01452010-05-31 22:12:30 +0200748int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
Max82040102016-07-06 11:59:18 +0200749 struct sgsn_mm_ctx *mmctx, bool encryptable)
Harald Welte9b455bf2010-03-14 15:45:01 +0800750{
Harald Weltee6afd602010-05-02 11:19:37 +0200751 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200752 uint8_t *fcs, *llch;
753 uint8_t addr, ctrl[2];
754 uint32_t fcs_calc;
755 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200756 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800757
Harald Weltee6afd602010-05-02 11:19:37 +0200758 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
759
760 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200761 lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi);
Harald Welte1d9d9442010-06-03 07:11:04 +0200762
763 if (msg->len > lle->params.n201_u) {
764 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
765 msg->len, lle->params.n201_u);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200766 msgb_free(msg);
Harald Welte1d9d9442010-06-03 07:11:04 +0200767 return -EFBIG;
768 }
769
Max5aa51962016-07-06 11:33:04 +0200770 gprs_llme_copy_key(mmctx, lle->llme);
771
Harald Weltee6afd602010-05-02 11:19:37 +0200772 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200773 lle->llme->bvci = msgb_bvci(msg);
774 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200775
Harald Welted07b4f92010-06-30 23:07:59 +0200776 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200777 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200778 oc = lle->oc_ui_send;
779 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200780 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200781 /* Increment Overflow Counter, if needed */
782 if ((lle->vu_send + 1) / 512)
783 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200784
Harald Welte9b455bf2010-03-14 15:45:01 +0800785 /* Address Field */
786 addr = sapi & 0xf;
787 if (command)
788 addr |= 0x40;
789
790 /* Control Field */
791 ctrl[0] = 0xc0;
792 ctrl[0] |= nu >> 6;
793 ctrl[1] = (nu << 2) & 0xfc;
794 ctrl[1] |= 0x01; /* Protected Mode */
795
796 /* prepend LLC UI header */
797 llch = msgb_push(msg, 3);
798 llch[0] = addr;
799 llch[1] = ctrl[0];
800 llch[2] = ctrl[1];
801
802 /* append FCS to end of frame */
803 fcs = msgb_put(msg, 3);
804 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
805 fcs[0] = fcs_calc & 0xff;
806 fcs[1] = (fcs_calc >> 8) & 0xff;
807 fcs[2] = (fcs_calc >> 16) & 0xff;
808
Max82040102016-07-06 11:59:18 +0200809 if (lle->llme->algo != GPRS_ALGO_GEA0 && encryptable) {
Max1de15912016-07-11 12:42:12 +0200810 int rc = apply_gea(lle, fcs - llch, nu, oc, sapi, fcs, llch);
Harald Welted07b4f92010-06-30 23:07:59 +0200811 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200812 msgb_free(msg);
Harald Welted07b4f92010-06-30 23:07:59 +0200813 return rc;
814 }
Harald Welted07b4f92010-06-30 23:07:59 +0200815 }
816
Alexander Couzens33163972016-10-04 17:53:21 +0200817 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_PACKETS]);
818 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_BYTES], msg->len);
819
Harald Weltee6afd602010-05-02 11:19:37 +0200820 /* Identifiers passed down: (BVCI, NSEI) */
821
Harald Welte1ae09c72010-05-13 19:22:55 +0200822 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800823 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800824}
825
Harald Welte9b455bf2010-03-14 15:45:01 +0800826static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
827 struct gprs_llc_lle *lle)
828{
829 switch (gph->cmd) {
Harald Welte1f60bbe2019-04-23 23:21:14 +0200830#if 0
831 /* we don't fully imoplement ABM, so refuse it properly (OS#3953) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800832 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
833 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200834 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800835 /* start re-establishment (8.7.1) */
836 }
Harald Welte807a5d82010-06-01 11:53:01 +0200837 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800838 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200839 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800840 /* FIXME: process data */
841 break;
842 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
843 /* FIXME: Send UA */
844 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200845 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800846 break;
847 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200848 if (lle->state == GPRS_LLES_LOCAL_EST)
849 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800850 break;
851 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200852 if (lle->state == GPRS_LLES_LOCAL_EST)
853 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800854 break;
855 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
856 break;
Harald Welte1f60bbe2019-04-23 23:21:14 +0200857#else
858 case GPRS_LLC_SABM:
859 case GPRS_LLC_DISC:
860 /* send DM to properly signal we don't do ABM */
861 gprs_llc_tx_dm(lle);
862 break;
863#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800864 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200865 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800866 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200867 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400868 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
869 LOGP(DLLC, LOGL_NOTICE,
870 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800871 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200872 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200873
874 /* HACK: non-standard recovery handling. If remote LLE
875 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800876 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200877 * and 'learn' the new sequence number */
878 if (gph->seq_tx != lle->vu_recv_last) {
879 lle->vu_recv_last = gph->seq_tx;
880 lle->vu_recv_duplicates = 0;
881 } else {
882 lle->vu_recv_duplicates++;
883 if (lle->vu_recv_duplicates < 3)
884 return -EIO;
885 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
886 "N(U=%d) after receiving %u duplicates\n",
887 lle->llme ? lle->llme->tlli : -1,
888 gph->seq_tx, lle->vu_recv_duplicates);
889 }
Harald Welteebabdea2010-06-01 18:28:10 +0200890 }
891 /* Increment the sequence number that we expect in the next frame */
892 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200893 /* Increment Overflow Counter */
894 if ((gph->seq_tx + 1) / 512)
895 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200896 break;
Harald Welte3a0b7722019-04-23 22:48:26 +0200897 case GPRS_LLC_NULL:
898 LOGP(DLLC, LOGL_DEBUG, "TLLI=%08x sends us LLC NULL\n", lle->llme ? lle->llme->tlli : -1);
899 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200900 default:
901 LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd);
902 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800903 }
904
905 return 0;
906}
907
Harald Weltea2665542010-05-02 09:28:11 +0200908/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800909int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
910{
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200911 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800912 struct gprs_llc_hdr_parsed llhp;
Max549ebc72016-11-18 14:07:04 +0100913 struct gprs_llc_lle *lle = NULL;
Max82040102016-07-06 11:59:18 +0200914 bool drop_cipherable = false;
Harald Weltea2665542010-05-02 09:28:11 +0200915 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800916
Harald Welte11d7c102010-05-02 11:54:55 +0200917 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
918
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800919 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800920 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte1ae09c72010-05-13 19:22:55 +0200921 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200922 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200923 return rc;
924 }
925
Harald Welte807a5d82010-06-01 11:53:01 +0200926 switch (gprs_tlli_type(msgb_tlli(msg))) {
927 case TLLI_LOCAL:
928 case TLLI_FOREIGN:
929 case TLLI_RANDOM:
930 case TLLI_AUXILIARY:
931 break;
932 default:
933 LOGP(DLLC, LOGL_ERROR,
934 "Discarding frame with strange TLLI type\n");
935 break;
936 }
937
Harald Weltea2665542010-05-02 09:28:11 +0200938 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200939 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200940 if (!lle) {
941 switch (llhp.sapi) {
942 case GPRS_SAPI_SNDCP3:
943 case GPRS_SAPI_SNDCP5:
944 case GPRS_SAPI_SNDCP9:
945 case GPRS_SAPI_SNDCP11:
946 /* Ask an upper layer for help. */
Alexander Couzens58f446c2016-08-30 18:51:50 +0200947 return gsm0408_gprs_force_reattach_oldmsg(msg, NULL);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200948 default:
949 break;
950 }
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200951 return 0;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200952 }
Max549ebc72016-11-18 14:07:04 +0100953 gprs_llc_hdr_dump(&llhp, lle);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100954 /* reset age computation */
955 lle->llme->age_timestamp = GPRS_LLME_RESET_AGE;
956
Harald Welted07b4f92010-06-30 23:07:59 +0200957 /* decrypt information field + FCS, if needed! */
958 if (llhp.is_encrypted) {
Max1de15912016-07-11 12:42:12 +0200959 if (lle->llme->algo != GPRS_ALGO_GEA0) {
960 rc = apply_gea(lle, llhp.data_len + 3, llhp.seq_tx,
961 lle->oc_ui_recv, lle->sapi, NULL,
962 llhp.data);
963 if (rc < 0)
964 return rc;
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200965 llhp.fcs = *(llhp.data + llhp.data_len);
966 llhp.fcs |= *(llhp.data + llhp.data_len + 1) << 8;
967 llhp.fcs |= *(llhp.data + llhp.data_len + 2) << 16;
Max1de15912016-07-11 12:42:12 +0200968 } else {
Harald Welted07b4f92010-06-30 23:07:59 +0200969 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
970 "has no KC/Algo! Dropping.\n");
971 return 0;
972 }
Harald Welted07b4f92010-06-30 23:07:59 +0200973 } else {
Max82040102016-07-06 11:59:18 +0200974 if (lle->llme->algo != GPRS_ALGO_GEA0 &&
975 lle->llme->cksn != GSM_KEY_SEQ_INVAL)
976 drop_cipherable = true;
Harald Welted07b4f92010-06-30 23:07:59 +0200977 }
978
979 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200980 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200981 if (llhp.fcs != llhp.fcs_calc) {
982 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
983 return -EIO;
984 }
985
Harald Welte10997d02010-05-03 12:28:12 +0200986 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200987 lle->llme->bvci = msgb_bvci(msg);
988 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200989
Harald Welte1ae09c72010-05-13 19:22:55 +0200990 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800991 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200992 if (rc < 0)
993 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800994
Harald Welte37bd0212019-04-23 22:46:43 +0200995 /* there are many frame types that don't carry user information
996 * and which hence have llhp.data = NULL */
997 if (llhp.data) {
998 /* set l3 layer & remove the fcs */
999 msg->l3h = llhp.data;
1000 msgb_l3trim(msg, llhp.data_len);
1001 }
1002
Alexander Couzens4e699a92016-07-05 11:04:27 +02001003 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_PACKETS]);
1004 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_BYTES], msg->len);
1005
Harald Welte1ae09c72010-05-13 19:22:55 +02001006 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte22df4ac2015-08-16 15:23:32 +02001007 if (llhp.cmd == GPRS_LLC_UI && llhp.data && llhp.data_len) {
Harald Welte9b455bf2010-03-14 15:45:01 +08001008 switch (llhp.sapi) {
1009 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +02001010 /* send LL_UNITDATA_IND to GMM */
Max82040102016-07-06 11:59:18 +02001011 rc = gsm0408_gprs_rcvmsg_gb(msg, lle->llme,
1012 drop_cipherable);
Harald Weltea2665542010-05-02 09:28:11 +02001013 break;
Harald Weltea2665542010-05-02 09:28:11 +02001014 case GPRS_SAPI_SNDCP3:
1015 case GPRS_SAPI_SNDCP5:
1016 case GPRS_SAPI_SNDCP9:
1017 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +02001018 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
1019 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
1020 break;
Harald Weltea2665542010-05-02 09:28:11 +02001021 case GPRS_SAPI_SMS:
1022 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +02001023 case GPRS_SAPI_TOM2:
1024 case GPRS_SAPI_TOM8:
1025 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +02001026 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +02001027 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +02001028 rc = -EINVAL;
1029 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001030 }
1031 }
1032
Harald Weltea2665542010-05-02 09:28:11 +02001033 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +08001034}
Harald Welte807a5d82010-06-01 11:53:01 +02001035
Max5aa51962016-07-06 11:33:04 +02001036/* Propagate crypto parameters MM -> LLME */
1037void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme)
1038{
1039 if (!mm)
1040 return;
1041 if (mm->ciph_algo != GPRS_ALGO_GEA0) {
1042 llme->algo = mm->ciph_algo;
1043 if (llme->cksn != mm->auth_triplet.key_seq &&
1044 mm->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
1045 memcpy(llme->kc, mm->auth_triplet.vec.kc,
1046 gprs_cipher_key_length(mm->ciph_algo));
1047 llme->cksn = mm->auth_triplet.key_seq;
1048 }
1049 } else
1050 llme->cksn = GSM_KEY_SEQ_INVAL;
1051}
1052
Harald Welte807a5d82010-06-01 11:53:01 +02001053/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
1054int gprs_llgmm_assign(struct gprs_llc_llme *llme,
Max5aa51962016-07-06 11:33:04 +02001055 uint32_t old_tlli, uint32_t new_tlli)
Harald Welte807a5d82010-06-01 11:53:01 +02001056{
1057 unsigned int i;
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001058 bool free = false;
1059
1060 LOGGBP(llme, DLLC, LOGL_NOTICE, "LLGM Assign pre (%08x => %08x)\n",
1061 old_tlli, new_tlli);
Harald Welte807a5d82010-06-01 11:53:01 +02001062
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001063 if (old_tlli == TLLI_UNASSIGNED && new_tlli != TLLI_UNASSIGNED) {
Harald Welte807a5d82010-06-01 11:53:01 +02001064 /* TLLI Assignment 8.3.1 */
1065 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001066 /* If old TLLI != TLLI_UNASSIGNED was assigned to LLME, then TLLI
Harald Welte807a5d82010-06-01 11:53:01 +02001067 * old is unassigned. Only TLLI new shall be accepted when
1068 * received from peer. */
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001069 if (llme->old_tlli != TLLI_UNASSIGNED) {
1070 llme->old_tlli = TLLI_UNASSIGNED;
Harald Welte875840c2010-07-01 11:54:31 +02001071 llme->tlli = new_tlli;
1072 } else {
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001073 /* If TLLI old == TLLI_UNASSIGNED was assigned to LLME, then this is
Harald Welte875840c2010-07-01 11:54:31 +02001074 * TLLI assignmemt according to 8.3.1 */
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001075 llme->old_tlli = TLLI_UNASSIGNED;
Harald Welte875840c2010-07-01 11:54:31 +02001076 llme->tlli = new_tlli;
1077 llme->state = GPRS_LLMS_ASSIGNED;
1078 /* 8.5.3.1 For all LLE's */
1079 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1080 struct gprs_llc_lle *l = &llme->lle[i];
1081 l->vu_send = l->vu_recv = 0;
1082 l->retrans_ctr = 0;
1083 l->state = GPRS_LLES_ASSIGNED_ADM;
1084 /* FIXME Set parameters according to table 9 */
1085 }
Harald Welte807a5d82010-06-01 11:53:01 +02001086 }
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001087 } else if (old_tlli != TLLI_UNASSIGNED && new_tlli != TLLI_UNASSIGNED) {
Harald Welte807a5d82010-06-01 11:53:01 +02001088 /* TLLI Change 8.3.2 */
1089 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +02001090 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freytheraa93bac2013-07-31 11:20:37 +02001091 llme->old_tlli = old_tlli;
Harald Welte807a5d82010-06-01 11:53:01 +02001092 llme->tlli = new_tlli;
1093 llme->state = GPRS_LLMS_ASSIGNED;
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001094 } else if (old_tlli != TLLI_UNASSIGNED && new_tlli == TLLI_UNASSIGNED) {
Harald Welte807a5d82010-06-01 11:53:01 +02001095 /* TLLI Unassignment 8.3.3) */
1096 llme->tlli = llme->old_tlli = 0;
1097 llme->state = GPRS_LLMS_UNASSIGNED;
1098 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1099 struct gprs_llc_lle *l = &llme->lle[i];
1100 l->state = GPRS_LLES_UNASSIGNED;
1101 }
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001102 free = true;
Harald Welte807a5d82010-06-01 11:53:01 +02001103 } else
1104 return -EINVAL;
1105
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001106 LOGGBP(llme, DLLC, LOGL_NOTICE, "LLGM Assign post (%08x => %08x)\n",
1107 old_tlli, new_tlli);
1108
1109 if (free)
1110 llme_free(llme);
1111
Harald Welte807a5d82010-06-01 11:53:01 +02001112 return 0;
1113}
Harald Welte496aee42010-06-30 19:59:55 +02001114
Max39550252016-06-28 17:39:20 +02001115/* TLLI unassignment */
1116int gprs_llgmm_unassign(struct gprs_llc_llme *llme)
1117{
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001118 return gprs_llgmm_assign(llme, llme->tlli, TLLI_UNASSIGNED);
Max39550252016-06-28 17:39:20 +02001119}
1120
Harald Welte0c1a3032011-10-16 18:49:05 +02001121/* Chapter 7.2.1.2 LLGMM-RESET.req */
1122int gprs_llgmm_reset(struct gprs_llc_llme *llme)
1123{
1124 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Pau Espin Pedrolb71d2c52019-11-21 16:15:52 +01001125 struct gprs_llc_lle *lle = &llme->lle[GPRS_SAPI_GMM];
Philipp4ac3aee2016-08-10 12:24:09 +02001126 uint8_t xid_bytes[1024];
Max3b6332f2017-11-01 13:28:38 +01001127 int xid_bytes_len, rc;
Philipp4ac3aee2016-08-10 12:24:09 +02001128 uint8_t *xid;
Harald Welte0c1a3032011-10-16 18:49:05 +02001129
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001130 LOGGBP(llme, DLLC, LOGL_NOTICE, "LLGM Reset\n");
Max3b6332f2017-11-01 13:28:38 +01001131
1132 rc = osmo_get_rand_id((uint8_t *) &llme->iov_ui, 4);
1133 if (rc < 0) {
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001134 LOGGBP(llme, DLLC, LOGL_ERROR,
1135 "osmo_get_rand_id() failed for LLC XID reset: %s\n",
1136 strerror(-rc));
Max3b6332f2017-11-01 13:28:38 +01001137 return rc;
Maxb997f842016-07-06 15:57:01 +02001138 }
1139
Philipp4ac3aee2016-08-10 12:24:09 +02001140 /* Generate XID message */
Harald Welteaf779d22019-04-12 16:56:04 +02001141 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes, sizeof(xid_bytes),
1142 llme->iov_ui, lle);
Harald Welte7e5bb622016-09-28 08:20:58 +08001143 if (xid_bytes_len < 0)
Philipp4ac3aee2016-08-10 12:24:09 +02001144 return -EINVAL;
1145 xid = msgb_put(msg, xid_bytes_len);
1146 memcpy(xid, xid_bytes, xid_bytes_len);
Harald Welte0c1a3032011-10-16 18:49:05 +02001147
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001148 /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */
1149 lle->vu_recv = 0;
1150 lle->vu_send = 0;
1151 lle->oc_ui_send = 0;
1152 lle->oc_ui_recv = 0;
1153
Harald Welte0c1a3032011-10-16 18:49:05 +02001154 /* FIXME: Start T200, wait for XID response */
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001155 return gprs_llc_tx_xid(lle, msg, 1);
Harald Welte0c1a3032011-10-16 18:49:05 +02001156}
1157
Maxb997f842016-07-06 15:57:01 +02001158int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi,
1159 struct gprs_llc_llme *llme)
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001160{
1161 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Harald Welteaf779d22019-04-12 16:56:04 +02001162 struct gprs_llc_lle *lle = &llme->lle[sapi];
Philipp4ac3aee2016-08-10 12:24:09 +02001163 uint8_t xid_bytes[1024];
Max3b6332f2017-11-01 13:28:38 +01001164 int xid_bytes_len, rc;
Philipp4ac3aee2016-08-10 12:24:09 +02001165 uint8_t *xid;
Maxb997f842016-07-06 15:57:01 +02001166
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001167 LOGGBP(llme, DLLC, LOGL_NOTICE, "LLGM Reset (SAPI=%" PRIu8 ")\n", sapi);
Max3b6332f2017-11-01 13:28:38 +01001168
1169 rc = osmo_get_rand_id((uint8_t *) &llme->iov_ui, 4);
1170 if (rc < 0) {
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001171 LOGGBP(llme, DLLC, LOGL_ERROR,
1172 "osmo_get_rand_id() failed for LLC XID reset: %s\n",
1173 strerror(-rc));
Max3b6332f2017-11-01 13:28:38 +01001174 return rc;
Maxb997f842016-07-06 15:57:01 +02001175 }
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001176
Philipp4ac3aee2016-08-10 12:24:09 +02001177 /* Generate XID message */
Harald Welteaf779d22019-04-12 16:56:04 +02001178 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes, sizeof(xid_bytes),
1179 llme->iov_ui, lle);
Harald Welte7e5bb622016-09-28 08:20:58 +08001180 if (xid_bytes_len < 0)
Philipp4ac3aee2016-08-10 12:24:09 +02001181 return -EINVAL;
1182 xid = msgb_put(msg, xid_bytes_len);
1183 memcpy(xid, xid_bytes, xid_bytes_len);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001184
1185 /* FIXME: Start T200, wait for XID response */
1186
1187 msgb_tlli(msg) = msgb_tlli(oldmsg);
1188 msgb_bvci(msg) = msgb_bvci(oldmsg);
1189 msgb_nsei(msg) = msgb_nsei(oldmsg);
1190
1191 return gprs_llc_tx_u(msg, sapi, 1, GPRS_LLC_U_XID, 1);
1192}
1193
Harald Welte496aee42010-06-30 19:59:55 +02001194int gprs_llc_init(const char *cipher_plugin_path)
1195{
1196 return gprs_cipher_load(cipher_plugin_path);
1197}