blob: 2a27da8446d98aff783410d5e6e36f7a915aac25 [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>
Harald Welte9b455bf2010-03-14 15:45:01 +080025
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010026#include <osmocom/core/msgb.h>
27#include <osmocom/core/linuxlist.h>
28#include <osmocom/core/timer.h>
29#include <osmocom/core/talloc.h>
Alexander Couzens4e699a92016-07-05 11:04:27 +020030#include <osmocom/core/rate_ctr.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080031#include <osmocom/gprs/gprs_bssgp.h>
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020032#include <osmocom/gsm/gsm_utils.h>
Harald Weltea2665542010-05-02 09:28:11 +020033
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020034#include <osmocom/sgsn/debug.h>
35#include <osmocom/sgsn/gprs_sgsn.h>
36#include <osmocom/sgsn/gprs_gmm.h>
37#include <osmocom/sgsn/gprs_llc.h>
38#include <osmocom/sgsn/crc24.h>
39#include <osmocom/sgsn/sgsn.h>
40#include <osmocom/sgsn/gprs_llc_xid.h>
41#include <osmocom/sgsn/gprs_sndcp_comp.h>
42#include <osmocom/sgsn/gprs_sndcp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080043
Pau Espin Pedrol5b6c4b82019-08-14 16:08:15 +020044const struct value_string gprs_llc_llme_state_names[] = {
45 { GPRS_LLMS_UNASSIGNED, "UNASSIGNED" },
46 { GPRS_LLMS_ASSIGNED, "ASSIGNED" },
47 { 0, NULL }
48};
49
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +020050static struct gprs_llc_llme *llme_alloc(uint32_t tlli);
Philipp4ac3aee2016-08-10 12:24:09 +020051static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
52 int command);
Harald Welte1f60bbe2019-04-23 23:21:14 +020053static int gprs_llc_tx_dm(struct gprs_llc_lle *lle);
Philipp4ac3aee2016-08-10 12:24:09 +020054static int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi,
55 int command, enum gprs_llc_u_cmd u_cmd, int pf_bit);
56
57/* BEGIN XID RELATED */
58
59/* Generate XID message */
60static int gprs_llc_generate_xid(uint8_t *bytes, int bytes_len,
61 struct gprs_llc_xid_field *l3_xid_field,
Harald Welteaf779d22019-04-12 16:56:04 +020062 struct gprs_llc_lle *lle)
Philipp4ac3aee2016-08-10 12:24:09 +020063{
64 /* Note: Called by gprs_ll_xid_req() */
65
66 LLIST_HEAD(xid_fields);
67
68 struct gprs_llc_xid_field xid_version;
69 struct gprs_llc_xid_field xid_n201u;
70 struct gprs_llc_xid_field xid_n201i;
71
72 xid_version.type = GPRS_LLC_XID_T_VERSION;
73 xid_version.data = (uint8_t *) "\x00";
74 xid_version.data_len = 1;
75
76 xid_n201u.type = GPRS_LLC_XID_T_N201_U;
77 xid_n201u.data = (uint8_t *) "\x05\xf0";
78 xid_n201u.data_len = 2;
79
80 xid_n201i.type = GPRS_LLC_XID_T_N201_I;
81 xid_n201i.data = (uint8_t *) "\x05\xf0";
82 xid_n201i.data_len = 2;
83
84 /* Add locally managed XID Fields */
Philipp4ac3aee2016-08-10 12:24:09 +020085 llist_add(&xid_version.list, &xid_fields);
Philippf788d932016-12-05 12:44:19 +010086 llist_add(&xid_n201u.list, &xid_fields);
87 llist_add(&xid_n201i.list, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +020088
89 /* Append layer 3 XID field (if present) */
90 if (l3_xid_field) {
91 /* Enforce layer 3 XID type (just to be sure) */
92 l3_xid_field->type = GPRS_LLC_XID_T_L3_PAR;
93
94 /* Add Layer 3 XID field to the list */
95 llist_add(&l3_xid_field->list, &xid_fields);
96 }
97
98 /* Store generated XID for later reference */
Harald Welteaf779d22019-04-12 16:56:04 +020099 talloc_free(lle->xid);
100 lle->xid = gprs_llc_copy_xid(lle->llme, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +0200101
102 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
103}
104
105/* Generate XID message that will cause the GMM to reset */
106static int gprs_llc_generate_xid_for_gmm_reset(uint8_t *bytes,
107 int bytes_len, uint32_t iov_ui,
Harald Welteaf779d22019-04-12 16:56:04 +0200108 struct gprs_llc_lle *lle)
Philipp4ac3aee2016-08-10 12:24:09 +0200109{
110 /* Called by gprs_llgmm_reset() and
111 * gprs_llgmm_reset_oldmsg() */
112
113 LLIST_HEAD(xid_fields);
114
115 struct gprs_llc_xid_field xid_reset;
116 struct gprs_llc_xid_field xid_iovui;
117
118 /* First XID component must be RESET */
119 xid_reset.type = GPRS_LLC_XID_T_RESET;
120 xid_reset.data = NULL;
121 xid_reset.data_len = 0;
122
123 /* Add new IOV-UI */
124 xid_iovui.type = GPRS_LLC_XID_T_IOV_UI;
125 xid_iovui.data = (uint8_t *) & iov_ui;
126 xid_iovui.data_len = 4;
127
128 /* Add locally managed XID Fields */
129 llist_add(&xid_iovui.list, &xid_fields);
130 llist_add(&xid_reset.list, &xid_fields);
131
132 /* Store generated XID for later reference */
Harald Welteaf779d22019-04-12 16:56:04 +0200133 talloc_free(lle->xid);
134 lle->xid = gprs_llc_copy_xid(lle->llme, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +0200135
136 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
137}
138
139/* Process an incoming XID confirmation */
140static int gprs_llc_process_xid_conf(uint8_t *bytes, int bytes_len,
141 struct gprs_llc_lle *lle)
142{
143 /* Note: This function handles the response of a network originated
Philipp532480a2016-12-23 11:05:11 +0100144 * XID-Request. There XID messages reflected by the MS are analyzed
Philipp4ac3aee2016-08-10 12:24:09 +0200145 * and processed here. The caller is called by rx_llc_xid(). */
146
147 struct llist_head *xid_fields;
148 struct gprs_llc_xid_field *xid_field;
Philippf1f34362016-08-26 17:00:21 +0200149 struct gprs_llc_xid_field *xid_field_request;
150 struct gprs_llc_xid_field *xid_field_request_l3 = NULL;
151
152 /* Pick layer3 XID from the XID request we have sent last */
Harald Welteaf779d22019-04-12 16:56:04 +0200153 if (lle->xid) {
154 llist_for_each_entry(xid_field_request, lle->xid, list) {
Philippf1f34362016-08-26 17:00:21 +0200155 if (xid_field_request->type == GPRS_LLC_XID_T_L3_PAR)
156 xid_field_request_l3 = xid_field_request;
157 }
158 }
Philipp4ac3aee2016-08-10 12:24:09 +0200159
160 /* Parse and analyze XID-Response */
161 xid_fields = gprs_llc_parse_xid(NULL, bytes, bytes_len);
162
163 if (xid_fields) {
164
165 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
166 llist_for_each_entry(xid_field, xid_fields, list) {
167
168 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
Philippf1f34362016-08-26 17:00:21 +0200169 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR &&
170 xid_field_request_l3) {
171 sndcp_sn_xid_conf(xid_field,
172 xid_field_request_l3, lle);
Philipp4ac3aee2016-08-10 12:24:09 +0200173 }
174
175 /* Process LLC-XID fields: */
176 else {
177
178 /* FIXME: Do something more useful with the
179 * echoed XID-Information. Currently we
180 * just ignore the response completely and
181 * by doing so we blindly accept any changes
182 * the MS might have done to the our XID
183 * inquiry. There is a remainig risk of
184 * malfunction! */
185 LOGP(DLLC, LOGL_NOTICE,
Max549ebc72016-11-18 14:07:04 +0100186 "Ignoring XID-Field: XID: type %s, data_len=%d, data=%s\n",
187 get_value_string(gprs_llc_xid_type_names,
188 xid_field->type),
189 xid_field->data_len,
Philipp4ac3aee2016-08-10 12:24:09 +0200190 osmo_hexdump_nospc(xid_field->data,
191 xid_field->data_len));
192 }
193 }
194 talloc_free(xid_fields);
195 }
196
197 /* Flush pending XID fields */
Harald Welteaf779d22019-04-12 16:56:04 +0200198 talloc_free(lle->xid);
199 lle->xid = NULL;
Philipp4ac3aee2016-08-10 12:24:09 +0200200
201 return 0;
202}
203
204/* Process an incoming XID indication and generate an appropiate response */
205static int gprs_llc_process_xid_ind(uint8_t *bytes_request,
206 int bytes_request_len,
207 uint8_t *bytes_response,
208 int bytes_response_maxlen,
209 struct gprs_llc_lle *lle)
210{
211 /* Note: This function computes the response that is sent back to the
Philipp532480a2016-12-23 11:05:11 +0100212 * MS when a mobile originated XID is received. The function is
Philipp4ac3aee2016-08-10 12:24:09 +0200213 * called by rx_llc_xid() */
214
215 int rc = -EINVAL;
216
217 struct llist_head *xid_fields;
218 struct llist_head *xid_fields_response;
219
220 struct gprs_llc_xid_field *xid_field;
221 struct gprs_llc_xid_field *xid_field_response;
222
Philipp4ac3aee2016-08-10 12:24:09 +0200223 /* Parse and analyze XID-Request */
224 xid_fields =
225 gprs_llc_parse_xid(lle->llme, bytes_request, bytes_request_len);
226 if (xid_fields) {
227 xid_fields_response = talloc_zero(lle->llme, struct llist_head);
228 INIT_LLIST_HEAD(xid_fields_response);
229 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
230
231 /* Process LLC-XID fields: */
232 llist_for_each_entry(xid_field, xid_fields, list) {
233
234 if (xid_field->type != GPRS_LLC_XID_T_L3_PAR) {
235 /* FIXME: Check the incoming XID parameters for
236 * for validity. Currently we just blindly
237 * accept all XID fields by just echoing them.
238 * There is a remaining risk of malfunction
Philipp532480a2016-12-23 11:05:11 +0100239 * when a MS submits values which defer from
Philipp4ac3aee2016-08-10 12:24:09 +0200240 * the default! */
241 LOGP(DLLC, LOGL_NOTICE,
Max549ebc72016-11-18 14:07:04 +0100242 "Echoing XID-Field: XID: type %s, data_len=%d, data=%s\n",
243 get_value_string(gprs_llc_xid_type_names,
244 xid_field->type),
245 xid_field->data_len,
Philipp4ac3aee2016-08-10 12:24:09 +0200246 osmo_hexdump_nospc(xid_field->data,
247 xid_field->data_len));
248 xid_field_response =
249 gprs_llc_dup_xid_field
250 (lle->llme, xid_field);
251 llist_add(&xid_field_response->list,
252 xid_fields_response);
253 }
254 }
255
Philippf1f34362016-08-26 17:00:21 +0200256 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
257 llist_for_each_entry(xid_field, xid_fields, list) {
258 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR) {
259
260 xid_field_response =
261 talloc_zero(lle->llme,
262 struct gprs_llc_xid_field);
263 rc = sndcp_sn_xid_ind(xid_field,
264 xid_field_response, lle);
265 if (rc == 0)
266 llist_add(&xid_field_response->list,
267 xid_fields_response);
268 else
269 talloc_free(xid_field_response);
270 }
271 }
272
Philipp4ac3aee2016-08-10 12:24:09 +0200273 rc = gprs_llc_compile_xid(bytes_response,
274 bytes_response_maxlen,
275 xid_fields_response);
276 talloc_free(xid_fields_response);
277 talloc_free(xid_fields);
278 }
279
280 return rc;
281}
282
Philipp532480a2016-12-23 11:05:11 +0100283/* Dispatch XID indications and responses comming from the MS */
Philipp4ac3aee2016-08-10 12:24:09 +0200284static void rx_llc_xid(struct gprs_llc_lle *lle,
285 struct gprs_llc_hdr_parsed *gph)
286{
287 uint8_t response[1024];
288 int response_len;
289
290 /* FIXME: 8.5.3.3: check if XID is invalid */
291 if (gph->is_cmd) {
292 LOGP(DLLC, LOGL_NOTICE,
Philipp532480a2016-12-23 11:05:11 +0100293 "Received XID indication from MS.\n");
Philipp4ac3aee2016-08-10 12:24:09 +0200294
295 struct msgb *resp;
296 uint8_t *xid;
297 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
298
299 response_len =
300 gprs_llc_process_xid_ind(gph->data, gph->data_len,
301 response, sizeof(response),
302 lle);
Philippf1f34362016-08-26 17:00:21 +0200303 if (response_len < 0) {
304 LOGP(DLLC, LOGL_ERROR,
305 "invalid XID indication received!\n");
306 } else {
307 xid = msgb_put(resp, response_len);
308 memcpy(xid, response, response_len);
309 }
Philipp4ac3aee2016-08-10 12:24:09 +0200310 gprs_llc_tx_xid(lle, resp, 0);
311 } else {
312 LOGP(DLLC, LOGL_NOTICE,
Philipp532480a2016-12-23 11:05:11 +0100313 "Received XID confirmation from MS.\n");
Philipp4ac3aee2016-08-10 12:24:09 +0200314 gprs_llc_process_xid_conf(gph->data, gph->data_len, lle);
315 /* FIXME: if we had sent a XID reset, send
316 * LLGMM-RESET.conf to GMM */
317 }
318}
319
Philipp4ac3aee2016-08-10 12:24:09 +0200320/* Set of LL-XID negotiation (See also: TS 101 351, Section 7.2.2.4) */
321int gprs_ll_xid_req(struct gprs_llc_lle *lle,
322 struct gprs_llc_xid_field *l3_xid_field)
323{
324 /* Note: This functions is calle from gprs_sndcp.c */
325
326 uint8_t xid_bytes[1024];;
327 int xid_bytes_len;
328 uint8_t *xid;
329 struct msgb *msg;
Max549ebc72016-11-18 14:07:04 +0100330 const char *ftype;
Philipp4ac3aee2016-08-10 12:24:09 +0200331
332 /* Generate XID */
333 xid_bytes_len =
Harald Welteaf779d22019-04-12 16:56:04 +0200334 gprs_llc_generate_xid(xid_bytes, sizeof(xid_bytes), l3_xid_field, lle);
Philipp4ac3aee2016-08-10 12:24:09 +0200335
336 /* Only perform XID sending if the XID message contains something */
337 if (xid_bytes_len > 0) {
338 /* Transmit XID bytes */
339 msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
340 xid = msgb_put(msg, xid_bytes_len);
341 memcpy(xid, xid_bytes, xid_bytes_len);
Max549ebc72016-11-18 14:07:04 +0100342 if (l3_xid_field)
343 ftype = get_value_string(gprs_llc_xid_type_names,
344 l3_xid_field->type);
345 else
346 ftype = "NULL";
347 LOGP(DLLC, LOGL_NOTICE, "Sending XID type %s (%d bytes) request"
Philipp532480a2016-12-23 11:05:11 +0100348 " to MS...\n", ftype, xid_bytes_len);
Philipp4ac3aee2016-08-10 12:24:09 +0200349 gprs_llc_tx_xid(lle, msg, 1);
350 } else {
351 LOGP(DLLC, LOGL_ERROR,
352 "XID-Message generation failed, XID not sent!\n");
353 return -EINVAL;
354 }
355
356 return 0;
357}
358/* END XID RELATED */
359
360
361
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200362
Harald Weltefaa70ff2012-06-17 09:31:16 +0800363/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
364 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
365static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
366{
367 struct bssgp_dl_ud_par dup;
368 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
369
Harald Welte8c004962012-07-04 21:53:12 +0200370 memset(&dup, 0, sizeof(dup));
371 /* before we have received some identity from the MS, we might
372 * not yet have a MMC context (e.g. XID negotiation of primarly
Philipp4ac3aee2016-08-10 12:24:09 +0200373 * LLC connection from GMM sapi). */
Harald Welte8c004962012-07-04 21:53:12 +0200374 if (mmctx) {
375 dup.imsi = mmctx->imsi;
376 dup.drx_parms = mmctx->drx_parms;
377 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
378 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +0200379
380 /* make sure we only send it to the right llme */
Neels Hofmeyr188a91c2017-12-27 17:29:04 +0100381 if (!(msgb_tlli(msg) == mmctx->gb.llme->tlli
382 || msgb_tlli(msg) == mmctx->gb.llme->old_tlli)) {
383 LOGP(DLLC, LOGL_ERROR,
384 "_bssgp_tx_dl_ud(): Attempt to send Downlink Unitdata to wrong LLME:"
385 " msgb_tlli=0x%x mmctx->gb.llme->tlli=0x%x ->old_tlli=0x%x\n",
386 msgb_tlli(msg), mmctx->gb.llme->tlli, mmctx->gb.llme->old_tlli);
387 msgb_free(msg);
388 return -EINVAL;
389 }
Harald Welte8c004962012-07-04 21:53:12 +0200390 }
Harald Weltefaa70ff2012-06-17 09:31:16 +0800391 memcpy(&dup.qos_profile, qos_profile_default,
392 sizeof(qos_profile_default));
393
Harald Weltece95b272012-06-17 13:04:02 +0800394 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +0800395}
396
397
Harald Welte1d9d9442010-06-03 07:11:04 +0200398/* Section 8.9.9 LLC layer parameter default values */
Daniel Willmann46d13262014-06-27 17:05:48 +0200399static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = {
Harald Welte1d9d9442010-06-03 07:11:04 +0200400 [1] = {
401 .t200_201 = 5,
402 .n200 = 3,
403 .n201_u = 400,
404 },
405 [2] = {
406 .t200_201 = 5,
407 .n200 = 3,
408 .n201_u = 270,
409 },
410 [3] = {
411 .iov_i_exp = 27,
412 .t200_201 = 5,
413 .n200 = 3,
414 .n201_u = 500,
415 .n201_i = 1503,
416 .mD = 1520,
417 .mU = 1520,
418 .kD = 16,
419 .kU = 16,
420 },
421 [5] = {
422 .iov_i_exp = 27,
423 .t200_201 = 10,
424 .n200 = 3,
425 .n201_u = 500,
426 .n201_i = 1503,
427 .mD = 760,
428 .mU = 760,
429 .kD = 8,
430 .kU = 8,
431 },
432 [7] = {
433 .t200_201 = 20,
434 .n200 = 3,
435 .n201_u = 270,
436 },
437 [8] = {
438 .t200_201 = 20,
439 .n200 = 3,
440 .n201_u = 270,
441 },
442 [9] = {
443 .iov_i_exp = 27,
444 .t200_201 = 20,
445 .n200 = 3,
446 .n201_u = 500,
447 .n201_i = 1503,
448 .mD = 380,
449 .mU = 380,
450 .kD = 4,
451 .kU = 4,
452 },
453 [11] = {
454 .iov_i_exp = 27,
455 .t200_201 = 40,
456 .n200 = 3,
457 .n201_u = 500,
458 .n201_i = 1503,
459 .mD = 190,
460 .mU = 190,
461 .kD = 2,
462 .kU = 2,
463 },
464};
465
Harald Welte807a5d82010-06-01 11:53:01 +0200466LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200467void *llc_tall_ctx;
468
469/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200470static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200471{
Harald Welte807a5d82010-06-01 11:53:01 +0200472 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200473
Harald Welte807a5d82010-06-01 11:53:01 +0200474 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
475 if (llme->tlli == tlli || llme->old_tlli == tlli)
476 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200477 }
478 return NULL;
479}
480
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200481struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi)
482{
483 struct gprs_llc_llme *llme;
484 struct gprs_llc_lle *lle;
485
486 lle = lle_by_tlli_sapi(tlli, sapi);
487 if (lle)
488 return lle;
489
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200490 LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, "
491 "creating LLME on the fly\n", tlli);
492 llme = llme_alloc(tlli);
493 lle = &llme->lle[sapi];
494 return lle;
495}
496
497struct llist_head *gprs_llme_list(void)
498{
499 return &gprs_llc_llmes;
500}
501
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200502/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
503static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
504 uint8_t sapi, enum gprs_llc_cmd cmd)
505{
506 struct gprs_llc_lle *lle;
507
508 /* We already know about this TLLI */
509 lle = lle_by_tlli_sapi(tlli, sapi);
510 if (lle)
511 return lle;
512
513 /* Maybe it is a routing area update but we already know this sapi? */
514 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
Jacob Erlbeck3fbf0a32016-01-04 18:43:32 +0100515 lle = lle_by_tlli_sapi(tlli, sapi);
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200516 if (lle) {
517 LOGP(DLLC, LOGL_NOTICE,
518 "LLC RX: Found a local entry for TLLI 0x%08x\n",
519 tlli);
520 return lle;
521 }
522 }
523
524 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
525 * except UID and XID frames with SAPI=1 */
526 if (sapi == GPRS_SAPI_GMM &&
527 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
528 struct gprs_llc_llme *llme;
529 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
530 llme = llme_alloc(tlli);
Daniel Willmann46553142014-09-03 17:46:44 +0200531 LOGP(DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, "
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200532 "creating LLME on the fly\n", tlli);
533 lle = &llme->lle[sapi];
534 return lle;
535 }
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +0200536
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200537 LOGP(DLLC, LOGL_NOTICE,
538 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
539 tlli, sapi);
540 return NULL;
541}
542
Harald Welte1d9d9442010-06-03 07:11:04 +0200543static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200544{
Harald Welte807a5d82010-06-01 11:53:01 +0200545 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200546
Harald Welte807a5d82010-06-01 11:53:01 +0200547 lle->llme = llme;
548 lle->sapi = sapi;
549 lle->state = GPRS_LLES_UNASSIGNED;
550
Harald Welte1d9d9442010-06-03 07:11:04 +0200551 /* Initialize according to parameters */
552 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200553}
554
555static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
556{
557 struct gprs_llc_llme *llme;
558 uint32_t i;
559
560 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
561 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200562 return NULL;
563
Harald Welte807a5d82010-06-01 11:53:01 +0200564 llme->tlli = tlli;
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +0200565 llme->old_tlli = TLLI_UNASSIGNED;
Harald Welte807a5d82010-06-01 11:53:01 +0200566 llme->state = GPRS_LLMS_UNASSIGNED;
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100567 llme->age_timestamp = GPRS_LLME_RESET_AGE;
Max5aa51962016-07-06 11:33:04 +0200568 llme->cksn = GSM_KEY_SEQ_INVAL;
Harald Weltea2665542010-05-02 09:28:11 +0200569
Harald Welte807a5d82010-06-01 11:53:01 +0200570 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
571 lle_init(llme, i);
572
573 llist_add(&llme->list, &gprs_llc_llmes);
574
Philippf1f34362016-08-26 17:00:21 +0200575 llme->comp.proto = gprs_sndcp_comp_alloc(llme);
576 llme->comp.data = gprs_sndcp_comp_alloc(llme);
577
Harald Welte807a5d82010-06-01 11:53:01 +0200578 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200579}
580
Harald Weltef7fef482010-06-28 22:18:26 +0200581static void llme_free(struct gprs_llc_llme *llme)
582{
Philippf1f34362016-08-26 17:00:21 +0200583 gprs_sndcp_comp_free(llme->comp.proto);
584 gprs_sndcp_comp_free(llme->comp.data);
Harald Weltef7fef482010-06-28 22:18:26 +0200585 llist_del(&llme->list);
586 talloc_free(llme);
587}
588
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200589#if 0
590/* FIXME: Unused code... */
Harald Welte9b455bf2010-03-14 15:45:01 +0800591static void t200_expired(void *data)
592{
593 struct gprs_llc_lle *lle = data;
594
595 /* 8.5.1.3: Expiry of T200 */
596
Harald Welte1d9d9442010-06-03 07:11:04 +0200597 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800598 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200599 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800600 }
601
602 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200603 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200604 /* FIXME: retransmit SABM */
605 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800606 lle->retrans_ctr++;
607 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200608 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200609 /* FIXME: retransmit DISC */
610 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800611 lle->retrans_ctr++;
612 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200613 default:
614 LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state);
615 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800616 }
617
618}
619
620static void t201_expired(void *data)
621{
622 struct gprs_llc_lle *lle = data;
623
Harald Welte1d9d9442010-06-03 07:11:04 +0200624 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200625 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
626 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800627 lle->retrans_ctr++;
628 }
629}
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200630#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800631
Harald Welte10997d02010-05-03 12:28:12 +0200632int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
633 enum gprs_llc_u_cmd u_cmd, int pf_bit)
634{
635 uint8_t *fcs, *llch;
636 uint8_t addr, ctrl;
637 uint32_t fcs_calc;
638
639 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
640
641 /* Address Field */
642 addr = sapi & 0xf;
643 if (command)
644 addr |= 0x40;
645
646 /* 6.3 Figure 8 */
647 ctrl = 0xe0 | u_cmd;
648 if (pf_bit)
649 ctrl |= 0x10;
650
651 /* prepend LLC UI header */
652 llch = msgb_push(msg, 2);
653 llch[0] = addr;
654 llch[1] = ctrl;
655
656 /* append FCS to end of frame */
657 fcs = msgb_put(msg, 3);
658 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
659 fcs[0] = fcs_calc & 0xff;
660 fcs[1] = (fcs_calc >> 8) & 0xff;
661 fcs[2] = (fcs_calc >> 16) & 0xff;
662
663 /* Identifiers passed down: (BVCI, NSEI) */
664
Alexander Couzens4e699a92016-07-05 11:04:27 +0200665 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_PACKETS]);
666 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_BYTES], msg->len);
667
Harald Welte1ae09c72010-05-13 19:22:55 +0200668 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800669 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200670}
671
672/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200673static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
674 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200675{
676 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200677 msgb_tlli(msg) = lle->llme->tlli;
678 msgb_bvci(msg) = lle->llme->bvci;
679 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200680
Harald Welte0c1a3032011-10-16 18:49:05 +0200681 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200682}
683
Harald Welte1f60bbe2019-04-23 23:21:14 +0200684static int gprs_llc_tx_dm(struct gprs_llc_lle *lle)
685{
686 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_DM");
687
688 /* copy identifiers from LLE to ensure lower layers can route */
689 msgb_tlli(msg) = lle->llme->tlli;
690 msgb_bvci(msg) = lle->llme->bvci;
691 msgb_nsei(msg) = lle->llme->nsei;
692
693 return gprs_llc_tx_u(msg, lle->sapi, 0, GPRS_LLC_U_DM_RESP, 1);
694}
695
Max1de15912016-07-11 12:42:12 +0200696/* encrypt information field + FCS, if needed! */
697static int apply_gea(struct gprs_llc_lle *lle, uint16_t crypt_len, uint16_t nu,
698 uint32_t oc, uint8_t sapi, uint8_t *fcs, uint8_t *data)
699{
700 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
701
702 if (lle->llme->algo == GPRS_ALGO_GEA0)
703 return -EINVAL;
704
705 /* Compute the 'Input' Paraemeter */
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200706 uint32_t fcs_calc, iv = gprs_cipher_gen_input_ui(lle->llme->iov_ui, sapi,
Max1de15912016-07-11 12:42:12 +0200707 nu, oc);
708 /* Compute gamma that we need to XOR with the data */
709 int r = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
710 lle->llme->kc, iv,
711 fcs ? GPRS_CIPH_SGSN2MS : GPRS_CIPH_MS2SGSN);
712 if (r < 0) {
713 LOGP(DLLC, LOGL_ERROR, "Error producing %s gamma for UI "
714 "frame: %d\n", get_value_string(gprs_cipher_names,
715 lle->llme->algo), r);
716 return -ENOMSG;
717 }
718
719 if (fcs) {
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200720 /* Mark frame as encrypted and update FCS */
721 data[2] |= 0x02;
722 fcs_calc = gprs_llc_fcs(data, fcs - data);
723 fcs[0] = fcs_calc & 0xff;
724 fcs[1] = (fcs_calc >> 8) & 0xff;
725 fcs[2] = (fcs_calc >> 16) & 0xff;
Max1de15912016-07-11 12:42:12 +0200726 data += 3;
727 }
728
729 /* XOR the cipher output with the data */
730 for (r = 0; r < crypt_len; r++)
731 *(data + r) ^= cipher_out[r];
732
733 return 0;
734}
735
Max82040102016-07-06 11:59:18 +0200736/* Transmit a UI frame over the given SAPI:
737 'encryptable' indicates whether particular message can be encrypted according
738 to 3GPP TS 24.008 § 4.7.1.2
739 */
Harald Welte56a01452010-05-31 22:12:30 +0200740int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
Max82040102016-07-06 11:59:18 +0200741 struct sgsn_mm_ctx *mmctx, bool encryptable)
Harald Welte9b455bf2010-03-14 15:45:01 +0800742{
Harald Weltee6afd602010-05-02 11:19:37 +0200743 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200744 uint8_t *fcs, *llch;
745 uint8_t addr, ctrl[2];
746 uint32_t fcs_calc;
747 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200748 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800749
Harald Weltee6afd602010-05-02 11:19:37 +0200750 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
751
752 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200753 lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi);
Harald Welte1d9d9442010-06-03 07:11:04 +0200754
755 if (msg->len > lle->params.n201_u) {
756 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
757 msg->len, lle->params.n201_u);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200758 msgb_free(msg);
Harald Welte1d9d9442010-06-03 07:11:04 +0200759 return -EFBIG;
760 }
761
Max5aa51962016-07-06 11:33:04 +0200762 gprs_llme_copy_key(mmctx, lle->llme);
763
Harald Weltee6afd602010-05-02 11:19:37 +0200764 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200765 lle->llme->bvci = msgb_bvci(msg);
766 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200767
Harald Welted07b4f92010-06-30 23:07:59 +0200768 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200769 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200770 oc = lle->oc_ui_send;
771 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200772 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200773 /* Increment Overflow Counter, if needed */
774 if ((lle->vu_send + 1) / 512)
775 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200776
Harald Welte9b455bf2010-03-14 15:45:01 +0800777 /* Address Field */
778 addr = sapi & 0xf;
779 if (command)
780 addr |= 0x40;
781
782 /* Control Field */
783 ctrl[0] = 0xc0;
784 ctrl[0] |= nu >> 6;
785 ctrl[1] = (nu << 2) & 0xfc;
786 ctrl[1] |= 0x01; /* Protected Mode */
787
788 /* prepend LLC UI header */
789 llch = msgb_push(msg, 3);
790 llch[0] = addr;
791 llch[1] = ctrl[0];
792 llch[2] = ctrl[1];
793
794 /* append FCS to end of frame */
795 fcs = msgb_put(msg, 3);
796 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
797 fcs[0] = fcs_calc & 0xff;
798 fcs[1] = (fcs_calc >> 8) & 0xff;
799 fcs[2] = (fcs_calc >> 16) & 0xff;
800
Max82040102016-07-06 11:59:18 +0200801 if (lle->llme->algo != GPRS_ALGO_GEA0 && encryptable) {
Max1de15912016-07-11 12:42:12 +0200802 int rc = apply_gea(lle, fcs - llch, nu, oc, sapi, fcs, llch);
Harald Welted07b4f92010-06-30 23:07:59 +0200803 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200804 msgb_free(msg);
Harald Welted07b4f92010-06-30 23:07:59 +0200805 return rc;
806 }
Harald Welted07b4f92010-06-30 23:07:59 +0200807 }
808
Alexander Couzens33163972016-10-04 17:53:21 +0200809 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_PACKETS]);
810 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_BYTES], msg->len);
811
Harald Weltee6afd602010-05-02 11:19:37 +0200812 /* Identifiers passed down: (BVCI, NSEI) */
813
Harald Welte1ae09c72010-05-13 19:22:55 +0200814 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800815 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800816}
817
Harald Welte9b455bf2010-03-14 15:45:01 +0800818static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
819 struct gprs_llc_lle *lle)
820{
821 switch (gph->cmd) {
Harald Welte1f60bbe2019-04-23 23:21:14 +0200822#if 0
823 /* we don't fully imoplement ABM, so refuse it properly (OS#3953) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800824 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
825 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200826 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800827 /* start re-establishment (8.7.1) */
828 }
Harald Welte807a5d82010-06-01 11:53:01 +0200829 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800830 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200831 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800832 /* FIXME: process data */
833 break;
834 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
835 /* FIXME: Send UA */
836 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200837 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800838 break;
839 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200840 if (lle->state == GPRS_LLES_LOCAL_EST)
841 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800842 break;
843 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200844 if (lle->state == GPRS_LLES_LOCAL_EST)
845 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800846 break;
847 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
848 break;
Harald Welte1f60bbe2019-04-23 23:21:14 +0200849#else
850 case GPRS_LLC_SABM:
851 case GPRS_LLC_DISC:
852 /* send DM to properly signal we don't do ABM */
853 gprs_llc_tx_dm(lle);
854 break;
855#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800856 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200857 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800858 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200859 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400860 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
861 LOGP(DLLC, LOGL_NOTICE,
862 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800863 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200864 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200865
866 /* HACK: non-standard recovery handling. If remote LLE
867 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800868 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200869 * and 'learn' the new sequence number */
870 if (gph->seq_tx != lle->vu_recv_last) {
871 lle->vu_recv_last = gph->seq_tx;
872 lle->vu_recv_duplicates = 0;
873 } else {
874 lle->vu_recv_duplicates++;
875 if (lle->vu_recv_duplicates < 3)
876 return -EIO;
877 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
878 "N(U=%d) after receiving %u duplicates\n",
879 lle->llme ? lle->llme->tlli : -1,
880 gph->seq_tx, lle->vu_recv_duplicates);
881 }
Harald Welteebabdea2010-06-01 18:28:10 +0200882 }
883 /* Increment the sequence number that we expect in the next frame */
884 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200885 /* Increment Overflow Counter */
886 if ((gph->seq_tx + 1) / 512)
887 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200888 break;
Harald Welte3a0b7722019-04-23 22:48:26 +0200889 case GPRS_LLC_NULL:
890 LOGP(DLLC, LOGL_DEBUG, "TLLI=%08x sends us LLC NULL\n", lle->llme ? lle->llme->tlli : -1);
891 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200892 default:
893 LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd);
894 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800895 }
896
897 return 0;
898}
899
Harald Weltea2665542010-05-02 09:28:11 +0200900/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800901int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
902{
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200903 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800904 struct gprs_llc_hdr_parsed llhp;
Max549ebc72016-11-18 14:07:04 +0100905 struct gprs_llc_lle *lle = NULL;
Max82040102016-07-06 11:59:18 +0200906 bool drop_cipherable = false;
Harald Weltea2665542010-05-02 09:28:11 +0200907 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800908
Harald Welte11d7c102010-05-02 11:54:55 +0200909 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
910
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800911 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800912 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte1ae09c72010-05-13 19:22:55 +0200913 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200914 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200915 return rc;
916 }
917
Harald Welte807a5d82010-06-01 11:53:01 +0200918 switch (gprs_tlli_type(msgb_tlli(msg))) {
919 case TLLI_LOCAL:
920 case TLLI_FOREIGN:
921 case TLLI_RANDOM:
922 case TLLI_AUXILIARY:
923 break;
924 default:
925 LOGP(DLLC, LOGL_ERROR,
926 "Discarding frame with strange TLLI type\n");
927 break;
928 }
929
Harald Weltea2665542010-05-02 09:28:11 +0200930 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200931 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200932 if (!lle) {
933 switch (llhp.sapi) {
934 case GPRS_SAPI_SNDCP3:
935 case GPRS_SAPI_SNDCP5:
936 case GPRS_SAPI_SNDCP9:
937 case GPRS_SAPI_SNDCP11:
938 /* Ask an upper layer for help. */
Alexander Couzens58f446c2016-08-30 18:51:50 +0200939 return gsm0408_gprs_force_reattach_oldmsg(msg, NULL);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200940 default:
941 break;
942 }
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200943 return 0;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200944 }
Max549ebc72016-11-18 14:07:04 +0100945 gprs_llc_hdr_dump(&llhp, lle);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100946 /* reset age computation */
947 lle->llme->age_timestamp = GPRS_LLME_RESET_AGE;
948
Harald Welted07b4f92010-06-30 23:07:59 +0200949 /* decrypt information field + FCS, if needed! */
950 if (llhp.is_encrypted) {
Max1de15912016-07-11 12:42:12 +0200951 if (lle->llme->algo != GPRS_ALGO_GEA0) {
952 rc = apply_gea(lle, llhp.data_len + 3, llhp.seq_tx,
953 lle->oc_ui_recv, lle->sapi, NULL,
954 llhp.data);
955 if (rc < 0)
956 return rc;
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200957 llhp.fcs = *(llhp.data + llhp.data_len);
958 llhp.fcs |= *(llhp.data + llhp.data_len + 1) << 8;
959 llhp.fcs |= *(llhp.data + llhp.data_len + 2) << 16;
Max1de15912016-07-11 12:42:12 +0200960 } else {
Harald Welted07b4f92010-06-30 23:07:59 +0200961 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
962 "has no KC/Algo! Dropping.\n");
963 return 0;
964 }
Harald Welted07b4f92010-06-30 23:07:59 +0200965 } else {
Max82040102016-07-06 11:59:18 +0200966 if (lle->llme->algo != GPRS_ALGO_GEA0 &&
967 lle->llme->cksn != GSM_KEY_SEQ_INVAL)
968 drop_cipherable = true;
Harald Welted07b4f92010-06-30 23:07:59 +0200969 }
970
971 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200972 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200973 if (llhp.fcs != llhp.fcs_calc) {
974 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
975 return -EIO;
976 }
977
Harald Welte10997d02010-05-03 12:28:12 +0200978 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200979 lle->llme->bvci = msgb_bvci(msg);
980 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200981
Harald Welte1ae09c72010-05-13 19:22:55 +0200982 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800983 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200984 if (rc < 0)
985 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800986
Harald Welte37bd0212019-04-23 22:46:43 +0200987 /* there are many frame types that don't carry user information
988 * and which hence have llhp.data = NULL */
989 if (llhp.data) {
990 /* set l3 layer & remove the fcs */
991 msg->l3h = llhp.data;
992 msgb_l3trim(msg, llhp.data_len);
993 }
994
Alexander Couzens4e699a92016-07-05 11:04:27 +0200995 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_PACKETS]);
996 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_BYTES], msg->len);
997
Harald Welte1ae09c72010-05-13 19:22:55 +0200998 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte22df4ac2015-08-16 15:23:32 +0200999 if (llhp.cmd == GPRS_LLC_UI && llhp.data && llhp.data_len) {
Harald Welte9b455bf2010-03-14 15:45:01 +08001000 switch (llhp.sapi) {
1001 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +02001002 /* send LL_UNITDATA_IND to GMM */
Max82040102016-07-06 11:59:18 +02001003 rc = gsm0408_gprs_rcvmsg_gb(msg, lle->llme,
1004 drop_cipherable);
Harald Weltea2665542010-05-02 09:28:11 +02001005 break;
Harald Weltea2665542010-05-02 09:28:11 +02001006 case GPRS_SAPI_SNDCP3:
1007 case GPRS_SAPI_SNDCP5:
1008 case GPRS_SAPI_SNDCP9:
1009 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +02001010 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
1011 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
1012 break;
Harald Weltea2665542010-05-02 09:28:11 +02001013 case GPRS_SAPI_SMS:
1014 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +02001015 case GPRS_SAPI_TOM2:
1016 case GPRS_SAPI_TOM8:
1017 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +02001018 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +02001019 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +02001020 rc = -EINVAL;
1021 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001022 }
1023 }
1024
Harald Weltea2665542010-05-02 09:28:11 +02001025 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +08001026}
Harald Welte807a5d82010-06-01 11:53:01 +02001027
Max5aa51962016-07-06 11:33:04 +02001028/* Propagate crypto parameters MM -> LLME */
1029void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme)
1030{
1031 if (!mm)
1032 return;
1033 if (mm->ciph_algo != GPRS_ALGO_GEA0) {
1034 llme->algo = mm->ciph_algo;
1035 if (llme->cksn != mm->auth_triplet.key_seq &&
1036 mm->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
1037 memcpy(llme->kc, mm->auth_triplet.vec.kc,
1038 gprs_cipher_key_length(mm->ciph_algo));
1039 llme->cksn = mm->auth_triplet.key_seq;
1040 }
1041 } else
1042 llme->cksn = GSM_KEY_SEQ_INVAL;
1043}
1044
Harald Welte807a5d82010-06-01 11:53:01 +02001045/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
1046int gprs_llgmm_assign(struct gprs_llc_llme *llme,
Max5aa51962016-07-06 11:33:04 +02001047 uint32_t old_tlli, uint32_t new_tlli)
Harald Welte807a5d82010-06-01 11:53:01 +02001048{
1049 unsigned int i;
1050
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001051 if (old_tlli == TLLI_UNASSIGNED && new_tlli != TLLI_UNASSIGNED) {
Harald Welte807a5d82010-06-01 11:53:01 +02001052 /* TLLI Assignment 8.3.1 */
1053 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001054 /* If old TLLI != TLLI_UNASSIGNED was assigned to LLME, then TLLI
Harald Welte807a5d82010-06-01 11:53:01 +02001055 * old is unassigned. Only TLLI new shall be accepted when
1056 * received from peer. */
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001057 if (llme->old_tlli != TLLI_UNASSIGNED) {
1058 llme->old_tlli = TLLI_UNASSIGNED;
Harald Welte875840c2010-07-01 11:54:31 +02001059 llme->tlli = new_tlli;
1060 } else {
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001061 /* If TLLI old == TLLI_UNASSIGNED was assigned to LLME, then this is
Harald Welte875840c2010-07-01 11:54:31 +02001062 * TLLI assignmemt according to 8.3.1 */
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001063 llme->old_tlli = TLLI_UNASSIGNED;
Harald Welte875840c2010-07-01 11:54:31 +02001064 llme->tlli = new_tlli;
1065 llme->state = GPRS_LLMS_ASSIGNED;
1066 /* 8.5.3.1 For all LLE's */
1067 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1068 struct gprs_llc_lle *l = &llme->lle[i];
1069 l->vu_send = l->vu_recv = 0;
1070 l->retrans_ctr = 0;
1071 l->state = GPRS_LLES_ASSIGNED_ADM;
1072 /* FIXME Set parameters according to table 9 */
1073 }
Harald Welte807a5d82010-06-01 11:53:01 +02001074 }
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001075 } else if (old_tlli != TLLI_UNASSIGNED && new_tlli != TLLI_UNASSIGNED) {
Harald Welte807a5d82010-06-01 11:53:01 +02001076 /* TLLI Change 8.3.2 */
1077 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +02001078 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freytheraa93bac2013-07-31 11:20:37 +02001079 llme->old_tlli = old_tlli;
Harald Welte807a5d82010-06-01 11:53:01 +02001080 llme->tlli = new_tlli;
1081 llme->state = GPRS_LLMS_ASSIGNED;
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001082 } else if (old_tlli != TLLI_UNASSIGNED && new_tlli == TLLI_UNASSIGNED) {
Harald Welte807a5d82010-06-01 11:53:01 +02001083 /* TLLI Unassignment 8.3.3) */
1084 llme->tlli = llme->old_tlli = 0;
1085 llme->state = GPRS_LLMS_UNASSIGNED;
1086 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1087 struct gprs_llc_lle *l = &llme->lle[i];
1088 l->state = GPRS_LLES_UNASSIGNED;
1089 }
Harald Weltef7fef482010-06-28 22:18:26 +02001090 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +02001091 } else
1092 return -EINVAL;
1093
1094 return 0;
1095}
Harald Welte496aee42010-06-30 19:59:55 +02001096
Max39550252016-06-28 17:39:20 +02001097/* TLLI unassignment */
1098int gprs_llgmm_unassign(struct gprs_llc_llme *llme)
1099{
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001100 return gprs_llgmm_assign(llme, llme->tlli, TLLI_UNASSIGNED);
Max39550252016-06-28 17:39:20 +02001101}
1102
Harald Welte0c1a3032011-10-16 18:49:05 +02001103/* Chapter 7.2.1.2 LLGMM-RESET.req */
1104int gprs_llgmm_reset(struct gprs_llc_llme *llme)
1105{
1106 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001107 struct gprs_llc_lle *lle = &llme->lle[1];
Philipp4ac3aee2016-08-10 12:24:09 +02001108 uint8_t xid_bytes[1024];
Max3b6332f2017-11-01 13:28:38 +01001109 int xid_bytes_len, rc;
Philipp4ac3aee2016-08-10 12:24:09 +02001110 uint8_t *xid;
Harald Welte0c1a3032011-10-16 18:49:05 +02001111
Philipp4ac3aee2016-08-10 12:24:09 +02001112 LOGP(DLLC, LOGL_NOTICE, "LLGM Reset\n");
Max3b6332f2017-11-01 13:28:38 +01001113
1114 rc = osmo_get_rand_id((uint8_t *) &llme->iov_ui, 4);
1115 if (rc < 0) {
1116 LOGP(DLLC, LOGL_ERROR, "osmo_get_rand_id() failed for LLC XID reset: %s\n", strerror(-rc));
1117 return rc;
Maxb997f842016-07-06 15:57:01 +02001118 }
1119
Philipp4ac3aee2016-08-10 12:24:09 +02001120 /* Generate XID message */
Harald Welteaf779d22019-04-12 16:56:04 +02001121 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes, sizeof(xid_bytes),
1122 llme->iov_ui, lle);
Harald Welte7e5bb622016-09-28 08:20:58 +08001123 if (xid_bytes_len < 0)
Philipp4ac3aee2016-08-10 12:24:09 +02001124 return -EINVAL;
1125 xid = msgb_put(msg, xid_bytes_len);
1126 memcpy(xid, xid_bytes, xid_bytes_len);
Harald Welte0c1a3032011-10-16 18:49:05 +02001127
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001128 /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */
1129 lle->vu_recv = 0;
1130 lle->vu_send = 0;
1131 lle->oc_ui_send = 0;
1132 lle->oc_ui_recv = 0;
1133
Harald Welte0c1a3032011-10-16 18:49:05 +02001134 /* FIXME: Start T200, wait for XID response */
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001135 return gprs_llc_tx_xid(lle, msg, 1);
Harald Welte0c1a3032011-10-16 18:49:05 +02001136}
1137
Maxb997f842016-07-06 15:57:01 +02001138int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi,
1139 struct gprs_llc_llme *llme)
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001140{
1141 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Harald Welteaf779d22019-04-12 16:56:04 +02001142 struct gprs_llc_lle *lle = &llme->lle[sapi];
Philipp4ac3aee2016-08-10 12:24:09 +02001143 uint8_t xid_bytes[1024];
Max3b6332f2017-11-01 13:28:38 +01001144 int xid_bytes_len, rc;
Philipp4ac3aee2016-08-10 12:24:09 +02001145 uint8_t *xid;
Maxb997f842016-07-06 15:57:01 +02001146
Philipp4ac3aee2016-08-10 12:24:09 +02001147 LOGP(DLLC, LOGL_NOTICE, "LLGM Reset\n");
Max3b6332f2017-11-01 13:28:38 +01001148
1149 rc = osmo_get_rand_id((uint8_t *) &llme->iov_ui, 4);
1150 if (rc < 0) {
1151 LOGP(DLLC, LOGL_ERROR, "osmo_get_rand_id() failed for LLC XID reset: %s\n", strerror(-rc));
1152 return rc;
Maxb997f842016-07-06 15:57:01 +02001153 }
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001154
Philipp4ac3aee2016-08-10 12:24:09 +02001155 /* Generate XID message */
Harald Welteaf779d22019-04-12 16:56:04 +02001156 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes, sizeof(xid_bytes),
1157 llme->iov_ui, lle);
Harald Welte7e5bb622016-09-28 08:20:58 +08001158 if (xid_bytes_len < 0)
Philipp4ac3aee2016-08-10 12:24:09 +02001159 return -EINVAL;
1160 xid = msgb_put(msg, xid_bytes_len);
1161 memcpy(xid, xid_bytes, xid_bytes_len);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001162
1163 /* FIXME: Start T200, wait for XID response */
1164
1165 msgb_tlli(msg) = msgb_tlli(oldmsg);
1166 msgb_bvci(msg) = msgb_bvci(oldmsg);
1167 msgb_nsei(msg) = msgb_nsei(oldmsg);
1168
1169 return gprs_llc_tx_u(msg, sapi, 1, GPRS_LLC_U_XID, 1);
1170}
1171
Harald Welte496aee42010-06-30 19:59:55 +02001172int gprs_llc_init(const char *cipher_plugin_path)
1173{
1174 return gprs_cipher_load(cipher_plugin_path);
1175}