blob: 1a7cf3db8c3ade695d5aa64db783d49861737627 [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
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +020044static struct gprs_llc_llme *llme_alloc(uint32_t tlli);
Philipp4ac3aee2016-08-10 12:24:09 +020045static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
46 int command);
47static int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi,
48 int command, enum gprs_llc_u_cmd u_cmd, int pf_bit);
49
50/* BEGIN XID RELATED */
51
52/* Generate XID message */
53static int gprs_llc_generate_xid(uint8_t *bytes, int bytes_len,
54 struct gprs_llc_xid_field *l3_xid_field,
55 struct gprs_llc_llme *llme)
56{
57 /* Note: Called by gprs_ll_xid_req() */
58
59 LLIST_HEAD(xid_fields);
60
61 struct gprs_llc_xid_field xid_version;
62 struct gprs_llc_xid_field xid_n201u;
63 struct gprs_llc_xid_field xid_n201i;
64
65 xid_version.type = GPRS_LLC_XID_T_VERSION;
66 xid_version.data = (uint8_t *) "\x00";
67 xid_version.data_len = 1;
68
69 xid_n201u.type = GPRS_LLC_XID_T_N201_U;
70 xid_n201u.data = (uint8_t *) "\x05\xf0";
71 xid_n201u.data_len = 2;
72
73 xid_n201i.type = GPRS_LLC_XID_T_N201_I;
74 xid_n201i.data = (uint8_t *) "\x05\xf0";
75 xid_n201i.data_len = 2;
76
77 /* Add locally managed XID Fields */
Philipp4ac3aee2016-08-10 12:24:09 +020078 llist_add(&xid_version.list, &xid_fields);
Philippf788d932016-12-05 12:44:19 +010079 llist_add(&xid_n201u.list, &xid_fields);
80 llist_add(&xid_n201i.list, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +020081
82 /* Append layer 3 XID field (if present) */
83 if (l3_xid_field) {
84 /* Enforce layer 3 XID type (just to be sure) */
85 l3_xid_field->type = GPRS_LLC_XID_T_L3_PAR;
86
87 /* Add Layer 3 XID field to the list */
88 llist_add(&l3_xid_field->list, &xid_fields);
89 }
90
91 /* Store generated XID for later reference */
92 talloc_free(llme->xid);
93 llme->xid = gprs_llc_copy_xid(llme, &xid_fields);
94
95 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
96}
97
98/* Generate XID message that will cause the GMM to reset */
99static int gprs_llc_generate_xid_for_gmm_reset(uint8_t *bytes,
100 int bytes_len, uint32_t iov_ui,
101 struct gprs_llc_llme *llme)
102{
103 /* Called by gprs_llgmm_reset() and
104 * gprs_llgmm_reset_oldmsg() */
105
106 LLIST_HEAD(xid_fields);
107
108 struct gprs_llc_xid_field xid_reset;
109 struct gprs_llc_xid_field xid_iovui;
110
111 /* First XID component must be RESET */
112 xid_reset.type = GPRS_LLC_XID_T_RESET;
113 xid_reset.data = NULL;
114 xid_reset.data_len = 0;
115
116 /* Add new IOV-UI */
117 xid_iovui.type = GPRS_LLC_XID_T_IOV_UI;
118 xid_iovui.data = (uint8_t *) & iov_ui;
119 xid_iovui.data_len = 4;
120
121 /* Add locally managed XID Fields */
122 llist_add(&xid_iovui.list, &xid_fields);
123 llist_add(&xid_reset.list, &xid_fields);
124
125 /* Store generated XID for later reference */
126 talloc_free(llme->xid);
127 llme->xid = gprs_llc_copy_xid(llme, &xid_fields);
128
129 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
130}
131
132/* Process an incoming XID confirmation */
133static int gprs_llc_process_xid_conf(uint8_t *bytes, int bytes_len,
134 struct gprs_llc_lle *lle)
135{
136 /* Note: This function handles the response of a network originated
Philipp532480a2016-12-23 11:05:11 +0100137 * XID-Request. There XID messages reflected by the MS are analyzed
Philipp4ac3aee2016-08-10 12:24:09 +0200138 * and processed here. The caller is called by rx_llc_xid(). */
139
140 struct llist_head *xid_fields;
141 struct gprs_llc_xid_field *xid_field;
Philippf1f34362016-08-26 17:00:21 +0200142 struct gprs_llc_xid_field *xid_field_request;
143 struct gprs_llc_xid_field *xid_field_request_l3 = NULL;
144
145 /* Pick layer3 XID from the XID request we have sent last */
146 if (lle->llme->xid) {
147 llist_for_each_entry(xid_field_request, lle->llme->xid, list) {
148 if (xid_field_request->type == GPRS_LLC_XID_T_L3_PAR)
149 xid_field_request_l3 = xid_field_request;
150 }
151 }
Philipp4ac3aee2016-08-10 12:24:09 +0200152
153 /* Parse and analyze XID-Response */
154 xid_fields = gprs_llc_parse_xid(NULL, bytes, bytes_len);
155
156 if (xid_fields) {
157
158 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
159 llist_for_each_entry(xid_field, xid_fields, list) {
160
161 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
Philippf1f34362016-08-26 17:00:21 +0200162 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR &&
163 xid_field_request_l3) {
164 sndcp_sn_xid_conf(xid_field,
165 xid_field_request_l3, lle);
Philipp4ac3aee2016-08-10 12:24:09 +0200166 }
167
168 /* Process LLC-XID fields: */
169 else {
170
171 /* FIXME: Do something more useful with the
172 * echoed XID-Information. Currently we
173 * just ignore the response completely and
174 * by doing so we blindly accept any changes
175 * the MS might have done to the our XID
176 * inquiry. There is a remainig risk of
177 * malfunction! */
178 LOGP(DLLC, LOGL_NOTICE,
Max549ebc72016-11-18 14:07:04 +0100179 "Ignoring XID-Field: XID: type %s, data_len=%d, data=%s\n",
180 get_value_string(gprs_llc_xid_type_names,
181 xid_field->type),
182 xid_field->data_len,
Philipp4ac3aee2016-08-10 12:24:09 +0200183 osmo_hexdump_nospc(xid_field->data,
184 xid_field->data_len));
185 }
186 }
187 talloc_free(xid_fields);
188 }
189
190 /* Flush pending XID fields */
191 talloc_free(lle->llme->xid);
192 lle->llme->xid = NULL;
193
194 return 0;
195}
196
197/* Process an incoming XID indication and generate an appropiate response */
198static int gprs_llc_process_xid_ind(uint8_t *bytes_request,
199 int bytes_request_len,
200 uint8_t *bytes_response,
201 int bytes_response_maxlen,
202 struct gprs_llc_lle *lle)
203{
204 /* Note: This function computes the response that is sent back to the
Philipp532480a2016-12-23 11:05:11 +0100205 * MS when a mobile originated XID is received. The function is
Philipp4ac3aee2016-08-10 12:24:09 +0200206 * called by rx_llc_xid() */
207
208 int rc = -EINVAL;
209
210 struct llist_head *xid_fields;
211 struct llist_head *xid_fields_response;
212
213 struct gprs_llc_xid_field *xid_field;
214 struct gprs_llc_xid_field *xid_field_response;
215
Philipp4ac3aee2016-08-10 12:24:09 +0200216 /* Parse and analyze XID-Request */
217 xid_fields =
218 gprs_llc_parse_xid(lle->llme, bytes_request, bytes_request_len);
219 if (xid_fields) {
220 xid_fields_response = talloc_zero(lle->llme, struct llist_head);
221 INIT_LLIST_HEAD(xid_fields_response);
222 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
223
224 /* Process LLC-XID fields: */
225 llist_for_each_entry(xid_field, xid_fields, list) {
226
227 if (xid_field->type != GPRS_LLC_XID_T_L3_PAR) {
228 /* FIXME: Check the incoming XID parameters for
229 * for validity. Currently we just blindly
230 * accept all XID fields by just echoing them.
231 * There is a remaining risk of malfunction
Philipp532480a2016-12-23 11:05:11 +0100232 * when a MS submits values which defer from
Philipp4ac3aee2016-08-10 12:24:09 +0200233 * the default! */
234 LOGP(DLLC, LOGL_NOTICE,
Max549ebc72016-11-18 14:07:04 +0100235 "Echoing XID-Field: XID: type %s, data_len=%d, data=%s\n",
236 get_value_string(gprs_llc_xid_type_names,
237 xid_field->type),
238 xid_field->data_len,
Philipp4ac3aee2016-08-10 12:24:09 +0200239 osmo_hexdump_nospc(xid_field->data,
240 xid_field->data_len));
241 xid_field_response =
242 gprs_llc_dup_xid_field
243 (lle->llme, xid_field);
244 llist_add(&xid_field_response->list,
245 xid_fields_response);
246 }
247 }
248
Philippf1f34362016-08-26 17:00:21 +0200249 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
250 llist_for_each_entry(xid_field, xid_fields, list) {
251 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR) {
252
253 xid_field_response =
254 talloc_zero(lle->llme,
255 struct gprs_llc_xid_field);
256 rc = sndcp_sn_xid_ind(xid_field,
257 xid_field_response, lle);
258 if (rc == 0)
259 llist_add(&xid_field_response->list,
260 xid_fields_response);
261 else
262 talloc_free(xid_field_response);
263 }
264 }
265
Philipp4ac3aee2016-08-10 12:24:09 +0200266 rc = gprs_llc_compile_xid(bytes_response,
267 bytes_response_maxlen,
268 xid_fields_response);
269 talloc_free(xid_fields_response);
270 talloc_free(xid_fields);
271 }
272
273 return rc;
274}
275
Philipp532480a2016-12-23 11:05:11 +0100276/* Dispatch XID indications and responses comming from the MS */
Philipp4ac3aee2016-08-10 12:24:09 +0200277static void rx_llc_xid(struct gprs_llc_lle *lle,
278 struct gprs_llc_hdr_parsed *gph)
279{
280 uint8_t response[1024];
281 int response_len;
282
283 /* FIXME: 8.5.3.3: check if XID is invalid */
284 if (gph->is_cmd) {
285 LOGP(DLLC, LOGL_NOTICE,
Philipp532480a2016-12-23 11:05:11 +0100286 "Received XID indication from MS.\n");
Philipp4ac3aee2016-08-10 12:24:09 +0200287
288 struct msgb *resp;
289 uint8_t *xid;
290 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
291
292 response_len =
293 gprs_llc_process_xid_ind(gph->data, gph->data_len,
294 response, sizeof(response),
295 lle);
Philippf1f34362016-08-26 17:00:21 +0200296 if (response_len < 0) {
297 LOGP(DLLC, LOGL_ERROR,
298 "invalid XID indication received!\n");
299 } else {
300 xid = msgb_put(resp, response_len);
301 memcpy(xid, response, response_len);
302 }
Philipp4ac3aee2016-08-10 12:24:09 +0200303 gprs_llc_tx_xid(lle, resp, 0);
304 } else {
305 LOGP(DLLC, LOGL_NOTICE,
Philipp532480a2016-12-23 11:05:11 +0100306 "Received XID confirmation from MS.\n");
Philipp4ac3aee2016-08-10 12:24:09 +0200307 gprs_llc_process_xid_conf(gph->data, gph->data_len, lle);
308 /* FIXME: if we had sent a XID reset, send
309 * LLGMM-RESET.conf to GMM */
310 }
311}
312
Philipp4ac3aee2016-08-10 12:24:09 +0200313/* Set of LL-XID negotiation (See also: TS 101 351, Section 7.2.2.4) */
314int gprs_ll_xid_req(struct gprs_llc_lle *lle,
315 struct gprs_llc_xid_field *l3_xid_field)
316{
317 /* Note: This functions is calle from gprs_sndcp.c */
318
319 uint8_t xid_bytes[1024];;
320 int xid_bytes_len;
321 uint8_t *xid;
322 struct msgb *msg;
Max549ebc72016-11-18 14:07:04 +0100323 const char *ftype;
Philipp4ac3aee2016-08-10 12:24:09 +0200324
325 /* Generate XID */
326 xid_bytes_len =
327 gprs_llc_generate_xid(xid_bytes, sizeof(xid_bytes),
328 l3_xid_field, lle->llme);
329
330 /* Only perform XID sending if the XID message contains something */
331 if (xid_bytes_len > 0) {
332 /* Transmit XID bytes */
333 msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
334 xid = msgb_put(msg, xid_bytes_len);
335 memcpy(xid, xid_bytes, xid_bytes_len);
Max549ebc72016-11-18 14:07:04 +0100336 if (l3_xid_field)
337 ftype = get_value_string(gprs_llc_xid_type_names,
338 l3_xid_field->type);
339 else
340 ftype = "NULL";
341 LOGP(DLLC, LOGL_NOTICE, "Sending XID type %s (%d bytes) request"
Philipp532480a2016-12-23 11:05:11 +0100342 " to MS...\n", ftype, xid_bytes_len);
Philipp4ac3aee2016-08-10 12:24:09 +0200343 gprs_llc_tx_xid(lle, msg, 1);
344 } else {
345 LOGP(DLLC, LOGL_ERROR,
346 "XID-Message generation failed, XID not sent!\n");
347 return -EINVAL;
348 }
349
350 return 0;
351}
352/* END XID RELATED */
353
354
355
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200356
Harald Weltefaa70ff2012-06-17 09:31:16 +0800357/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
358 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
359static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
360{
361 struct bssgp_dl_ud_par dup;
362 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
363
Harald Welte8c004962012-07-04 21:53:12 +0200364 memset(&dup, 0, sizeof(dup));
365 /* before we have received some identity from the MS, we might
366 * not yet have a MMC context (e.g. XID negotiation of primarly
Philipp4ac3aee2016-08-10 12:24:09 +0200367 * LLC connection from GMM sapi). */
Harald Welte8c004962012-07-04 21:53:12 +0200368 if (mmctx) {
369 dup.imsi = mmctx->imsi;
370 dup.drx_parms = mmctx->drx_parms;
371 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
372 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +0200373
374 /* make sure we only send it to the right llme */
Harald Weltef97ee042015-12-25 19:12:21 +0100375 OSMO_ASSERT(msgb_tlli(msg) == mmctx->gb.llme->tlli
376 || msgb_tlli(msg) == mmctx->gb.llme->old_tlli);
Harald Welte8c004962012-07-04 21:53:12 +0200377 }
Harald Weltefaa70ff2012-06-17 09:31:16 +0800378 memcpy(&dup.qos_profile, qos_profile_default,
379 sizeof(qos_profile_default));
380
Harald Weltece95b272012-06-17 13:04:02 +0800381 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +0800382}
383
384
Harald Welte1d9d9442010-06-03 07:11:04 +0200385/* Section 8.9.9 LLC layer parameter default values */
Daniel Willmann46d13262014-06-27 17:05:48 +0200386static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = {
Harald Welte1d9d9442010-06-03 07:11:04 +0200387 [1] = {
388 .t200_201 = 5,
389 .n200 = 3,
390 .n201_u = 400,
391 },
392 [2] = {
393 .t200_201 = 5,
394 .n200 = 3,
395 .n201_u = 270,
396 },
397 [3] = {
398 .iov_i_exp = 27,
399 .t200_201 = 5,
400 .n200 = 3,
401 .n201_u = 500,
402 .n201_i = 1503,
403 .mD = 1520,
404 .mU = 1520,
405 .kD = 16,
406 .kU = 16,
407 },
408 [5] = {
409 .iov_i_exp = 27,
410 .t200_201 = 10,
411 .n200 = 3,
412 .n201_u = 500,
413 .n201_i = 1503,
414 .mD = 760,
415 .mU = 760,
416 .kD = 8,
417 .kU = 8,
418 },
419 [7] = {
420 .t200_201 = 20,
421 .n200 = 3,
422 .n201_u = 270,
423 },
424 [8] = {
425 .t200_201 = 20,
426 .n200 = 3,
427 .n201_u = 270,
428 },
429 [9] = {
430 .iov_i_exp = 27,
431 .t200_201 = 20,
432 .n200 = 3,
433 .n201_u = 500,
434 .n201_i = 1503,
435 .mD = 380,
436 .mU = 380,
437 .kD = 4,
438 .kU = 4,
439 },
440 [11] = {
441 .iov_i_exp = 27,
442 .t200_201 = 40,
443 .n200 = 3,
444 .n201_u = 500,
445 .n201_i = 1503,
446 .mD = 190,
447 .mU = 190,
448 .kD = 2,
449 .kU = 2,
450 },
451};
452
Harald Welte807a5d82010-06-01 11:53:01 +0200453LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200454void *llc_tall_ctx;
455
456/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200457static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200458{
Harald Welte807a5d82010-06-01 11:53:01 +0200459 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200460
Harald Welte807a5d82010-06-01 11:53:01 +0200461 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
462 if (llme->tlli == tlli || llme->old_tlli == tlli)
463 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200464 }
465 return NULL;
466}
467
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200468struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi)
469{
470 struct gprs_llc_llme *llme;
471 struct gprs_llc_lle *lle;
472
473 lle = lle_by_tlli_sapi(tlli, sapi);
474 if (lle)
475 return lle;
476
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200477 LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, "
478 "creating LLME on the fly\n", tlli);
479 llme = llme_alloc(tlli);
480 lle = &llme->lle[sapi];
481 return lle;
482}
483
484struct llist_head *gprs_llme_list(void)
485{
486 return &gprs_llc_llmes;
487}
488
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200489/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
490static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
491 uint8_t sapi, enum gprs_llc_cmd cmd)
492{
493 struct gprs_llc_lle *lle;
494
495 /* We already know about this TLLI */
496 lle = lle_by_tlli_sapi(tlli, sapi);
497 if (lle)
498 return lle;
499
500 /* Maybe it is a routing area update but we already know this sapi? */
501 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
Jacob Erlbeck3fbf0a32016-01-04 18:43:32 +0100502 lle = lle_by_tlli_sapi(tlli, sapi);
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200503 if (lle) {
504 LOGP(DLLC, LOGL_NOTICE,
505 "LLC RX: Found a local entry for TLLI 0x%08x\n",
506 tlli);
507 return lle;
508 }
509 }
510
511 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
512 * except UID and XID frames with SAPI=1 */
513 if (sapi == GPRS_SAPI_GMM &&
514 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
515 struct gprs_llc_llme *llme;
516 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
517 llme = llme_alloc(tlli);
Daniel Willmann46553142014-09-03 17:46:44 +0200518 LOGP(DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, "
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200519 "creating LLME on the fly\n", tlli);
520 lle = &llme->lle[sapi];
521 return lle;
522 }
523
524 LOGP(DLLC, LOGL_NOTICE,
525 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
526 tlli, sapi);
527 return NULL;
528}
529
Harald Welte1d9d9442010-06-03 07:11:04 +0200530static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200531{
Harald Welte807a5d82010-06-01 11:53:01 +0200532 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200533
Harald Welte807a5d82010-06-01 11:53:01 +0200534 lle->llme = llme;
535 lle->sapi = sapi;
536 lle->state = GPRS_LLES_UNASSIGNED;
537
Harald Welte1d9d9442010-06-03 07:11:04 +0200538 /* Initialize according to parameters */
539 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200540}
541
542static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
543{
544 struct gprs_llc_llme *llme;
545 uint32_t i;
546
547 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
548 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200549 return NULL;
550
Harald Welte807a5d82010-06-01 11:53:01 +0200551 llme->tlli = tlli;
Harald Welte875840c2010-07-01 11:54:31 +0200552 llme->old_tlli = 0xffffffff;
Harald Welte807a5d82010-06-01 11:53:01 +0200553 llme->state = GPRS_LLMS_UNASSIGNED;
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100554 llme->age_timestamp = GPRS_LLME_RESET_AGE;
Max5aa51962016-07-06 11:33:04 +0200555 llme->cksn = GSM_KEY_SEQ_INVAL;
Harald Weltea2665542010-05-02 09:28:11 +0200556
Harald Welte807a5d82010-06-01 11:53:01 +0200557 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
558 lle_init(llme, i);
559
560 llist_add(&llme->list, &gprs_llc_llmes);
561
Philippf1f34362016-08-26 17:00:21 +0200562 llme->comp.proto = gprs_sndcp_comp_alloc(llme);
563 llme->comp.data = gprs_sndcp_comp_alloc(llme);
564
Harald Welte807a5d82010-06-01 11:53:01 +0200565 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200566}
567
Harald Weltef7fef482010-06-28 22:18:26 +0200568static void llme_free(struct gprs_llc_llme *llme)
569{
Philippf1f34362016-08-26 17:00:21 +0200570 gprs_sndcp_comp_free(llme->comp.proto);
571 gprs_sndcp_comp_free(llme->comp.data);
Philipp4ac3aee2016-08-10 12:24:09 +0200572 talloc_free(llme->xid);
Harald Weltef7fef482010-06-28 22:18:26 +0200573 llist_del(&llme->list);
574 talloc_free(llme);
575}
576
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200577#if 0
578/* FIXME: Unused code... */
Harald Welte9b455bf2010-03-14 15:45:01 +0800579static void t200_expired(void *data)
580{
581 struct gprs_llc_lle *lle = data;
582
583 /* 8.5.1.3: Expiry of T200 */
584
Harald Welte1d9d9442010-06-03 07:11:04 +0200585 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800586 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200587 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800588 }
589
590 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200591 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200592 /* FIXME: retransmit SABM */
593 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800594 lle->retrans_ctr++;
595 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200596 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200597 /* FIXME: retransmit DISC */
598 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800599 lle->retrans_ctr++;
600 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200601 default:
602 LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state);
603 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800604 }
605
606}
607
608static void t201_expired(void *data)
609{
610 struct gprs_llc_lle *lle = data;
611
Harald Welte1d9d9442010-06-03 07:11:04 +0200612 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200613 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
614 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800615 lle->retrans_ctr++;
616 }
617}
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200618#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800619
Harald Welte10997d02010-05-03 12:28:12 +0200620int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
621 enum gprs_llc_u_cmd u_cmd, int pf_bit)
622{
623 uint8_t *fcs, *llch;
624 uint8_t addr, ctrl;
625 uint32_t fcs_calc;
626
627 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
628
629 /* Address Field */
630 addr = sapi & 0xf;
631 if (command)
632 addr |= 0x40;
633
634 /* 6.3 Figure 8 */
635 ctrl = 0xe0 | u_cmd;
636 if (pf_bit)
637 ctrl |= 0x10;
638
639 /* prepend LLC UI header */
640 llch = msgb_push(msg, 2);
641 llch[0] = addr;
642 llch[1] = ctrl;
643
644 /* append FCS to end of frame */
645 fcs = msgb_put(msg, 3);
646 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
647 fcs[0] = fcs_calc & 0xff;
648 fcs[1] = (fcs_calc >> 8) & 0xff;
649 fcs[2] = (fcs_calc >> 16) & 0xff;
650
651 /* Identifiers passed down: (BVCI, NSEI) */
652
Alexander Couzens4e699a92016-07-05 11:04:27 +0200653 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_PACKETS]);
654 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_BYTES], msg->len);
655
Harald Welte1ae09c72010-05-13 19:22:55 +0200656 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800657 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200658}
659
660/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200661static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
662 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200663{
664 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200665 msgb_tlli(msg) = lle->llme->tlli;
666 msgb_bvci(msg) = lle->llme->bvci;
667 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200668
Harald Welte0c1a3032011-10-16 18:49:05 +0200669 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200670}
671
Max1de15912016-07-11 12:42:12 +0200672/* encrypt information field + FCS, if needed! */
673static int apply_gea(struct gprs_llc_lle *lle, uint16_t crypt_len, uint16_t nu,
674 uint32_t oc, uint8_t sapi, uint8_t *fcs, uint8_t *data)
675{
676 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
677
678 if (lle->llme->algo == GPRS_ALGO_GEA0)
679 return -EINVAL;
680
681 /* Compute the 'Input' Paraemeter */
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200682 uint32_t fcs_calc, iv = gprs_cipher_gen_input_ui(lle->llme->iov_ui, sapi,
Max1de15912016-07-11 12:42:12 +0200683 nu, oc);
684 /* Compute gamma that we need to XOR with the data */
685 int r = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
686 lle->llme->kc, iv,
687 fcs ? GPRS_CIPH_SGSN2MS : GPRS_CIPH_MS2SGSN);
688 if (r < 0) {
689 LOGP(DLLC, LOGL_ERROR, "Error producing %s gamma for UI "
690 "frame: %d\n", get_value_string(gprs_cipher_names,
691 lle->llme->algo), r);
692 return -ENOMSG;
693 }
694
695 if (fcs) {
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200696 /* Mark frame as encrypted and update FCS */
697 data[2] |= 0x02;
698 fcs_calc = gprs_llc_fcs(data, fcs - data);
699 fcs[0] = fcs_calc & 0xff;
700 fcs[1] = (fcs_calc >> 8) & 0xff;
701 fcs[2] = (fcs_calc >> 16) & 0xff;
Max1de15912016-07-11 12:42:12 +0200702 data += 3;
703 }
704
705 /* XOR the cipher output with the data */
706 for (r = 0; r < crypt_len; r++)
707 *(data + r) ^= cipher_out[r];
708
709 return 0;
710}
711
Max82040102016-07-06 11:59:18 +0200712/* Transmit a UI frame over the given SAPI:
713 'encryptable' indicates whether particular message can be encrypted according
714 to 3GPP TS 24.008 § 4.7.1.2
715 */
Harald Welte56a01452010-05-31 22:12:30 +0200716int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
Max82040102016-07-06 11:59:18 +0200717 struct sgsn_mm_ctx *mmctx, bool encryptable)
Harald Welte9b455bf2010-03-14 15:45:01 +0800718{
Harald Weltee6afd602010-05-02 11:19:37 +0200719 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200720 uint8_t *fcs, *llch;
721 uint8_t addr, ctrl[2];
722 uint32_t fcs_calc;
723 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200724 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800725
Harald Weltee6afd602010-05-02 11:19:37 +0200726 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
727
728 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200729 lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi);
Harald Welte1d9d9442010-06-03 07:11:04 +0200730
731 if (msg->len > lle->params.n201_u) {
732 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
733 msg->len, lle->params.n201_u);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200734 msgb_free(msg);
Harald Welte1d9d9442010-06-03 07:11:04 +0200735 return -EFBIG;
736 }
737
Max5aa51962016-07-06 11:33:04 +0200738 gprs_llme_copy_key(mmctx, lle->llme);
739
Harald Weltee6afd602010-05-02 11:19:37 +0200740 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200741 lle->llme->bvci = msgb_bvci(msg);
742 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200743
Harald Welted07b4f92010-06-30 23:07:59 +0200744 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200745 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200746 oc = lle->oc_ui_send;
747 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200748 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200749 /* Increment Overflow Counter, if needed */
750 if ((lle->vu_send + 1) / 512)
751 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200752
Harald Welte9b455bf2010-03-14 15:45:01 +0800753 /* Address Field */
754 addr = sapi & 0xf;
755 if (command)
756 addr |= 0x40;
757
758 /* Control Field */
759 ctrl[0] = 0xc0;
760 ctrl[0] |= nu >> 6;
761 ctrl[1] = (nu << 2) & 0xfc;
762 ctrl[1] |= 0x01; /* Protected Mode */
763
764 /* prepend LLC UI header */
765 llch = msgb_push(msg, 3);
766 llch[0] = addr;
767 llch[1] = ctrl[0];
768 llch[2] = ctrl[1];
769
770 /* append FCS to end of frame */
771 fcs = msgb_put(msg, 3);
772 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
773 fcs[0] = fcs_calc & 0xff;
774 fcs[1] = (fcs_calc >> 8) & 0xff;
775 fcs[2] = (fcs_calc >> 16) & 0xff;
776
Max82040102016-07-06 11:59:18 +0200777 if (lle->llme->algo != GPRS_ALGO_GEA0 && encryptable) {
Max1de15912016-07-11 12:42:12 +0200778 int rc = apply_gea(lle, fcs - llch, nu, oc, sapi, fcs, llch);
Harald Welted07b4f92010-06-30 23:07:59 +0200779 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200780 msgb_free(msg);
Harald Welted07b4f92010-06-30 23:07:59 +0200781 return rc;
782 }
Harald Welted07b4f92010-06-30 23:07:59 +0200783 }
784
Alexander Couzens33163972016-10-04 17:53:21 +0200785 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_PACKETS]);
786 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_BYTES], msg->len);
787
Harald Weltee6afd602010-05-02 11:19:37 +0200788 /* Identifiers passed down: (BVCI, NSEI) */
789
Harald Welte1ae09c72010-05-13 19:22:55 +0200790 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800791 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800792}
793
Harald Welte9b455bf2010-03-14 15:45:01 +0800794static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
795 struct gprs_llc_lle *lle)
796{
797 switch (gph->cmd) {
798 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
799 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200800 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800801 /* start re-establishment (8.7.1) */
802 }
Harald Welte807a5d82010-06-01 11:53:01 +0200803 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800804 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200805 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800806 /* FIXME: process data */
807 break;
808 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
809 /* FIXME: Send UA */
810 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200811 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800812 break;
813 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200814 if (lle->state == GPRS_LLES_LOCAL_EST)
815 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800816 break;
817 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200818 if (lle->state == GPRS_LLES_LOCAL_EST)
819 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800820 break;
821 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
822 break;
823 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200824 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800825 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200826 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400827 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
828 LOGP(DLLC, LOGL_NOTICE,
829 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800830 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200831 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200832
833 /* HACK: non-standard recovery handling. If remote LLE
834 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800835 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200836 * and 'learn' the new sequence number */
837 if (gph->seq_tx != lle->vu_recv_last) {
838 lle->vu_recv_last = gph->seq_tx;
839 lle->vu_recv_duplicates = 0;
840 } else {
841 lle->vu_recv_duplicates++;
842 if (lle->vu_recv_duplicates < 3)
843 return -EIO;
844 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
845 "N(U=%d) after receiving %u duplicates\n",
846 lle->llme ? lle->llme->tlli : -1,
847 gph->seq_tx, lle->vu_recv_duplicates);
848 }
Harald Welteebabdea2010-06-01 18:28:10 +0200849 }
850 /* Increment the sequence number that we expect in the next frame */
851 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200852 /* Increment Overflow Counter */
853 if ((gph->seq_tx + 1) / 512)
854 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200855 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200856 default:
857 LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd);
858 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800859 }
860
861 return 0;
862}
863
Harald Weltea2665542010-05-02 09:28:11 +0200864/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800865int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
866{
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200867 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800868 struct gprs_llc_hdr_parsed llhp;
Max549ebc72016-11-18 14:07:04 +0100869 struct gprs_llc_lle *lle = NULL;
Max82040102016-07-06 11:59:18 +0200870 bool drop_cipherable = false;
Harald Weltea2665542010-05-02 09:28:11 +0200871 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800872
Harald Welte11d7c102010-05-02 11:54:55 +0200873 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
874
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800875 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800876 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte1ae09c72010-05-13 19:22:55 +0200877 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200878 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200879 return rc;
880 }
881
Harald Welte807a5d82010-06-01 11:53:01 +0200882 switch (gprs_tlli_type(msgb_tlli(msg))) {
883 case TLLI_LOCAL:
884 case TLLI_FOREIGN:
885 case TLLI_RANDOM:
886 case TLLI_AUXILIARY:
887 break;
888 default:
889 LOGP(DLLC, LOGL_ERROR,
890 "Discarding frame with strange TLLI type\n");
891 break;
892 }
893
Harald Weltea2665542010-05-02 09:28:11 +0200894 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200895 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200896 if (!lle) {
897 switch (llhp.sapi) {
898 case GPRS_SAPI_SNDCP3:
899 case GPRS_SAPI_SNDCP5:
900 case GPRS_SAPI_SNDCP9:
901 case GPRS_SAPI_SNDCP11:
902 /* Ask an upper layer for help. */
Alexander Couzens58f446c2016-08-30 18:51:50 +0200903 return gsm0408_gprs_force_reattach_oldmsg(msg, NULL);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200904 default:
905 break;
906 }
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200907 return 0;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200908 }
Max549ebc72016-11-18 14:07:04 +0100909 gprs_llc_hdr_dump(&llhp, lle);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100910 /* reset age computation */
911 lle->llme->age_timestamp = GPRS_LLME_RESET_AGE;
912
Harald Welted07b4f92010-06-30 23:07:59 +0200913 /* decrypt information field + FCS, if needed! */
914 if (llhp.is_encrypted) {
Max1de15912016-07-11 12:42:12 +0200915 if (lle->llme->algo != GPRS_ALGO_GEA0) {
916 rc = apply_gea(lle, llhp.data_len + 3, llhp.seq_tx,
917 lle->oc_ui_recv, lle->sapi, NULL,
918 llhp.data);
919 if (rc < 0)
920 return rc;
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200921 llhp.fcs = *(llhp.data + llhp.data_len);
922 llhp.fcs |= *(llhp.data + llhp.data_len + 1) << 8;
923 llhp.fcs |= *(llhp.data + llhp.data_len + 2) << 16;
Max1de15912016-07-11 12:42:12 +0200924 } else {
Harald Welted07b4f92010-06-30 23:07:59 +0200925 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
926 "has no KC/Algo! Dropping.\n");
927 return 0;
928 }
Harald Welted07b4f92010-06-30 23:07:59 +0200929 } else {
Max82040102016-07-06 11:59:18 +0200930 if (lle->llme->algo != GPRS_ALGO_GEA0 &&
931 lle->llme->cksn != GSM_KEY_SEQ_INVAL)
932 drop_cipherable = true;
Harald Welted07b4f92010-06-30 23:07:59 +0200933 }
934
935 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200936 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200937 if (llhp.fcs != llhp.fcs_calc) {
938 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
939 return -EIO;
940 }
941
Harald Welte10997d02010-05-03 12:28:12 +0200942 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200943 lle->llme->bvci = msgb_bvci(msg);
944 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200945
Harald Welte1ae09c72010-05-13 19:22:55 +0200946 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800947 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200948 if (rc < 0)
949 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800950
Alexander Couzens4e699a92016-07-05 11:04:27 +0200951 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_PACKETS]);
952 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_BYTES], msg->len);
953
Harald Welte1ae09c72010-05-13 19:22:55 +0200954 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte22df4ac2015-08-16 15:23:32 +0200955 if (llhp.cmd == GPRS_LLC_UI && llhp.data && llhp.data_len) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200956 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800957 switch (llhp.sapi) {
958 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200959 /* send LL_UNITDATA_IND to GMM */
Max82040102016-07-06 11:59:18 +0200960 rc = gsm0408_gprs_rcvmsg_gb(msg, lle->llme,
961 drop_cipherable);
Harald Weltea2665542010-05-02 09:28:11 +0200962 break;
Harald Weltea2665542010-05-02 09:28:11 +0200963 case GPRS_SAPI_SNDCP3:
964 case GPRS_SAPI_SNDCP5:
965 case GPRS_SAPI_SNDCP9:
966 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +0200967 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
968 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
969 break;
Harald Weltea2665542010-05-02 09:28:11 +0200970 case GPRS_SAPI_SMS:
971 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +0200972 case GPRS_SAPI_TOM2:
973 case GPRS_SAPI_TOM8:
974 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200975 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200976 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200977 rc = -EINVAL;
978 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800979 }
980 }
981
Harald Weltea2665542010-05-02 09:28:11 +0200982 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800983}
Harald Welte807a5d82010-06-01 11:53:01 +0200984
Max5aa51962016-07-06 11:33:04 +0200985/* Propagate crypto parameters MM -> LLME */
986void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme)
987{
988 if (!mm)
989 return;
990 if (mm->ciph_algo != GPRS_ALGO_GEA0) {
991 llme->algo = mm->ciph_algo;
992 if (llme->cksn != mm->auth_triplet.key_seq &&
993 mm->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
994 memcpy(llme->kc, mm->auth_triplet.vec.kc,
995 gprs_cipher_key_length(mm->ciph_algo));
996 llme->cksn = mm->auth_triplet.key_seq;
997 }
998 } else
999 llme->cksn = GSM_KEY_SEQ_INVAL;
1000}
1001
Harald Welte807a5d82010-06-01 11:53:01 +02001002/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
1003int gprs_llgmm_assign(struct gprs_llc_llme *llme,
Max5aa51962016-07-06 11:33:04 +02001004 uint32_t old_tlli, uint32_t new_tlli)
Harald Welte807a5d82010-06-01 11:53:01 +02001005{
1006 unsigned int i;
1007
1008 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
1009 /* TLLI Assignment 8.3.1 */
1010 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
1011 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
1012 * old is unassigned. Only TLLI new shall be accepted when
1013 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +02001014 if (llme->old_tlli != 0xffffffff) {
1015 llme->old_tlli = 0xffffffff;
1016 llme->tlli = new_tlli;
1017 } else {
1018 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
1019 * TLLI assignmemt according to 8.3.1 */
1020 llme->old_tlli = 0xffffffff;
1021 llme->tlli = new_tlli;
1022 llme->state = GPRS_LLMS_ASSIGNED;
1023 /* 8.5.3.1 For all LLE's */
1024 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1025 struct gprs_llc_lle *l = &llme->lle[i];
1026 l->vu_send = l->vu_recv = 0;
1027 l->retrans_ctr = 0;
1028 l->state = GPRS_LLES_ASSIGNED_ADM;
1029 /* FIXME Set parameters according to table 9 */
1030 }
Harald Welte807a5d82010-06-01 11:53:01 +02001031 }
1032 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
1033 /* TLLI Change 8.3.2 */
1034 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +02001035 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freytheraa93bac2013-07-31 11:20:37 +02001036 llme->old_tlli = old_tlli;
Harald Welte807a5d82010-06-01 11:53:01 +02001037 llme->tlli = new_tlli;
1038 llme->state = GPRS_LLMS_ASSIGNED;
1039 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
1040 /* TLLI Unassignment 8.3.3) */
1041 llme->tlli = llme->old_tlli = 0;
1042 llme->state = GPRS_LLMS_UNASSIGNED;
1043 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1044 struct gprs_llc_lle *l = &llme->lle[i];
1045 l->state = GPRS_LLES_UNASSIGNED;
1046 }
Harald Weltef7fef482010-06-28 22:18:26 +02001047 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +02001048 } else
1049 return -EINVAL;
1050
1051 return 0;
1052}
Harald Welte496aee42010-06-30 19:59:55 +02001053
Max39550252016-06-28 17:39:20 +02001054/* TLLI unassignment */
1055int gprs_llgmm_unassign(struct gprs_llc_llme *llme)
1056{
Max5aa51962016-07-06 11:33:04 +02001057 return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff);
Max39550252016-06-28 17:39:20 +02001058}
1059
Harald Welte0c1a3032011-10-16 18:49:05 +02001060/* Chapter 7.2.1.2 LLGMM-RESET.req */
1061int gprs_llgmm_reset(struct gprs_llc_llme *llme)
1062{
1063 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001064 struct gprs_llc_lle *lle = &llme->lle[1];
Philipp4ac3aee2016-08-10 12:24:09 +02001065 uint8_t xid_bytes[1024];
Max3b6332f2017-11-01 13:28:38 +01001066 int xid_bytes_len, rc;
Philipp4ac3aee2016-08-10 12:24:09 +02001067 uint8_t *xid;
Harald Welte0c1a3032011-10-16 18:49:05 +02001068
Philipp4ac3aee2016-08-10 12:24:09 +02001069 LOGP(DLLC, LOGL_NOTICE, "LLGM Reset\n");
Max3b6332f2017-11-01 13:28:38 +01001070
1071 rc = osmo_get_rand_id((uint8_t *) &llme->iov_ui, 4);
1072 if (rc < 0) {
1073 LOGP(DLLC, LOGL_ERROR, "osmo_get_rand_id() failed for LLC XID reset: %s\n", strerror(-rc));
1074 return rc;
Maxb997f842016-07-06 15:57:01 +02001075 }
1076
Philipp4ac3aee2016-08-10 12:24:09 +02001077 /* Generate XID message */
1078 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes,
1079 sizeof(xid_bytes),llme->iov_ui,llme);
Harald Welte7e5bb622016-09-28 08:20:58 +08001080 if (xid_bytes_len < 0)
Philipp4ac3aee2016-08-10 12:24:09 +02001081 return -EINVAL;
1082 xid = msgb_put(msg, xid_bytes_len);
1083 memcpy(xid, xid_bytes, xid_bytes_len);
Harald Welte0c1a3032011-10-16 18:49:05 +02001084
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001085 /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */
1086 lle->vu_recv = 0;
1087 lle->vu_send = 0;
1088 lle->oc_ui_send = 0;
1089 lle->oc_ui_recv = 0;
1090
Harald Welte0c1a3032011-10-16 18:49:05 +02001091 /* FIXME: Start T200, wait for XID response */
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001092 return gprs_llc_tx_xid(lle, msg, 1);
Harald Welte0c1a3032011-10-16 18:49:05 +02001093}
1094
Maxb997f842016-07-06 15:57:01 +02001095int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi,
1096 struct gprs_llc_llme *llme)
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001097{
1098 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Philipp4ac3aee2016-08-10 12:24:09 +02001099 uint8_t xid_bytes[1024];
Max3b6332f2017-11-01 13:28:38 +01001100 int xid_bytes_len, rc;
Philipp4ac3aee2016-08-10 12:24:09 +02001101 uint8_t *xid;
Maxb997f842016-07-06 15:57:01 +02001102
Philipp4ac3aee2016-08-10 12:24:09 +02001103 LOGP(DLLC, LOGL_NOTICE, "LLGM Reset\n");
Max3b6332f2017-11-01 13:28:38 +01001104
1105 rc = osmo_get_rand_id((uint8_t *) &llme->iov_ui, 4);
1106 if (rc < 0) {
1107 LOGP(DLLC, LOGL_ERROR, "osmo_get_rand_id() failed for LLC XID reset: %s\n", strerror(-rc));
1108 return rc;
Maxb997f842016-07-06 15:57:01 +02001109 }
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001110
Philipp4ac3aee2016-08-10 12:24:09 +02001111 /* Generate XID message */
1112 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes,
1113 sizeof(xid_bytes),llme->iov_ui,llme);
Harald Welte7e5bb622016-09-28 08:20:58 +08001114 if (xid_bytes_len < 0)
Philipp4ac3aee2016-08-10 12:24:09 +02001115 return -EINVAL;
1116 xid = msgb_put(msg, xid_bytes_len);
1117 memcpy(xid, xid_bytes, xid_bytes_len);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001118
1119 /* FIXME: Start T200, wait for XID response */
1120
1121 msgb_tlli(msg) = msgb_tlli(oldmsg);
1122 msgb_bvci(msg) = msgb_bvci(oldmsg);
1123 msgb_nsei(msg) = msgb_nsei(oldmsg);
1124
1125 return gprs_llc_tx_u(msg, sapi, 1, GPRS_LLC_U_XID, 1);
1126}
1127
Harald Welte496aee42010-06-30 19:59:55 +02001128int gprs_llc_init(const char *cipher_plugin_path)
1129{
1130 return gprs_cipher_load(cipher_plugin_path);
1131}