blob: 22743fe91ec97a679cbad9d6536a37b2d31dfeb0 [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
Maxb997f842016-07-06 15:57:01 +020026#include <openssl/rand.h>
27
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010028#include <osmocom/core/msgb.h>
29#include <osmocom/core/linuxlist.h>
30#include <osmocom/core/timer.h>
31#include <osmocom/core/talloc.h>
Alexander Couzens4e699a92016-07-05 11:04:27 +020032#include <osmocom/core/rate_ctr.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080033#include <osmocom/gprs/gprs_bssgp.h>
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020034#include <osmocom/gsm/gsm_utils.h>
Harald Weltea2665542010-05-02 09:28:11 +020035
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020036#include <osmocom/sgsn/debug.h>
37#include <osmocom/sgsn/gprs_sgsn.h>
38#include <osmocom/sgsn/gprs_gmm.h>
39#include <osmocom/sgsn/gprs_llc.h>
40#include <osmocom/sgsn/crc24.h>
41#include <osmocom/sgsn/sgsn.h>
42#include <osmocom/sgsn/gprs_llc_xid.h>
43#include <osmocom/sgsn/gprs_sndcp_comp.h>
44#include <osmocom/sgsn/gprs_sndcp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080045
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +020046static struct gprs_llc_llme *llme_alloc(uint32_t tlli);
Philipp4ac3aee2016-08-10 12:24:09 +020047static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
48 int command);
49static int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi,
50 int command, enum gprs_llc_u_cmd u_cmd, int pf_bit);
51
52/* BEGIN XID RELATED */
53
54/* Generate XID message */
55static int gprs_llc_generate_xid(uint8_t *bytes, int bytes_len,
56 struct gprs_llc_xid_field *l3_xid_field,
57 struct gprs_llc_llme *llme)
58{
59 /* Note: Called by gprs_ll_xid_req() */
60
61 LLIST_HEAD(xid_fields);
62
63 struct gprs_llc_xid_field xid_version;
64 struct gprs_llc_xid_field xid_n201u;
65 struct gprs_llc_xid_field xid_n201i;
66
67 xid_version.type = GPRS_LLC_XID_T_VERSION;
68 xid_version.data = (uint8_t *) "\x00";
69 xid_version.data_len = 1;
70
71 xid_n201u.type = GPRS_LLC_XID_T_N201_U;
72 xid_n201u.data = (uint8_t *) "\x05\xf0";
73 xid_n201u.data_len = 2;
74
75 xid_n201i.type = GPRS_LLC_XID_T_N201_I;
76 xid_n201i.data = (uint8_t *) "\x05\xf0";
77 xid_n201i.data_len = 2;
78
79 /* Add locally managed XID Fields */
Philipp4ac3aee2016-08-10 12:24:09 +020080 llist_add(&xid_version.list, &xid_fields);
Philippf788d932016-12-05 12:44:19 +010081 llist_add(&xid_n201u.list, &xid_fields);
82 llist_add(&xid_n201i.list, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +020083
84 /* Append layer 3 XID field (if present) */
85 if (l3_xid_field) {
86 /* Enforce layer 3 XID type (just to be sure) */
87 l3_xid_field->type = GPRS_LLC_XID_T_L3_PAR;
88
89 /* Add Layer 3 XID field to the list */
90 llist_add(&l3_xid_field->list, &xid_fields);
91 }
92
93 /* Store generated XID for later reference */
94 talloc_free(llme->xid);
95 llme->xid = gprs_llc_copy_xid(llme, &xid_fields);
96
97 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
98}
99
100/* Generate XID message that will cause the GMM to reset */
101static int gprs_llc_generate_xid_for_gmm_reset(uint8_t *bytes,
102 int bytes_len, uint32_t iov_ui,
103 struct gprs_llc_llme *llme)
104{
105 /* Called by gprs_llgmm_reset() and
106 * gprs_llgmm_reset_oldmsg() */
107
108 LLIST_HEAD(xid_fields);
109
110 struct gprs_llc_xid_field xid_reset;
111 struct gprs_llc_xid_field xid_iovui;
112
113 /* First XID component must be RESET */
114 xid_reset.type = GPRS_LLC_XID_T_RESET;
115 xid_reset.data = NULL;
116 xid_reset.data_len = 0;
117
118 /* Add new IOV-UI */
119 xid_iovui.type = GPRS_LLC_XID_T_IOV_UI;
120 xid_iovui.data = (uint8_t *) & iov_ui;
121 xid_iovui.data_len = 4;
122
123 /* Add locally managed XID Fields */
124 llist_add(&xid_iovui.list, &xid_fields);
125 llist_add(&xid_reset.list, &xid_fields);
126
127 /* Store generated XID for later reference */
128 talloc_free(llme->xid);
129 llme->xid = gprs_llc_copy_xid(llme, &xid_fields);
130
131 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
132}
133
134/* Process an incoming XID confirmation */
135static int gprs_llc_process_xid_conf(uint8_t *bytes, int bytes_len,
136 struct gprs_llc_lle *lle)
137{
138 /* Note: This function handles the response of a network originated
Philipp532480a2016-12-23 11:05:11 +0100139 * XID-Request. There XID messages reflected by the MS are analyzed
Philipp4ac3aee2016-08-10 12:24:09 +0200140 * and processed here. The caller is called by rx_llc_xid(). */
141
142 struct llist_head *xid_fields;
143 struct gprs_llc_xid_field *xid_field;
Philippf1f34362016-08-26 17:00:21 +0200144 struct gprs_llc_xid_field *xid_field_request;
145 struct gprs_llc_xid_field *xid_field_request_l3 = NULL;
146
147 /* Pick layer3 XID from the XID request we have sent last */
148 if (lle->llme->xid) {
149 llist_for_each_entry(xid_field_request, lle->llme->xid, list) {
150 if (xid_field_request->type == GPRS_LLC_XID_T_L3_PAR)
151 xid_field_request_l3 = xid_field_request;
152 }
153 }
Philipp4ac3aee2016-08-10 12:24:09 +0200154
155 /* Parse and analyze XID-Response */
156 xid_fields = gprs_llc_parse_xid(NULL, bytes, bytes_len);
157
158 if (xid_fields) {
159
160 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
161 llist_for_each_entry(xid_field, xid_fields, list) {
162
163 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
Philippf1f34362016-08-26 17:00:21 +0200164 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR &&
165 xid_field_request_l3) {
166 sndcp_sn_xid_conf(xid_field,
167 xid_field_request_l3, lle);
Philipp4ac3aee2016-08-10 12:24:09 +0200168 }
169
170 /* Process LLC-XID fields: */
171 else {
172
173 /* FIXME: Do something more useful with the
174 * echoed XID-Information. Currently we
175 * just ignore the response completely and
176 * by doing so we blindly accept any changes
177 * the MS might have done to the our XID
178 * inquiry. There is a remainig risk of
179 * malfunction! */
180 LOGP(DLLC, LOGL_NOTICE,
Max549ebc72016-11-18 14:07:04 +0100181 "Ignoring XID-Field: XID: type %s, data_len=%d, data=%s\n",
182 get_value_string(gprs_llc_xid_type_names,
183 xid_field->type),
184 xid_field->data_len,
Philipp4ac3aee2016-08-10 12:24:09 +0200185 osmo_hexdump_nospc(xid_field->data,
186 xid_field->data_len));
187 }
188 }
189 talloc_free(xid_fields);
190 }
191
192 /* Flush pending XID fields */
193 talloc_free(lle->llme->xid);
194 lle->llme->xid = NULL;
195
196 return 0;
197}
198
199/* Process an incoming XID indication and generate an appropiate response */
200static int gprs_llc_process_xid_ind(uint8_t *bytes_request,
201 int bytes_request_len,
202 uint8_t *bytes_response,
203 int bytes_response_maxlen,
204 struct gprs_llc_lle *lle)
205{
206 /* Note: This function computes the response that is sent back to the
Philipp532480a2016-12-23 11:05:11 +0100207 * MS when a mobile originated XID is received. The function is
Philipp4ac3aee2016-08-10 12:24:09 +0200208 * called by rx_llc_xid() */
209
210 int rc = -EINVAL;
211
212 struct llist_head *xid_fields;
213 struct llist_head *xid_fields_response;
214
215 struct gprs_llc_xid_field *xid_field;
216 struct gprs_llc_xid_field *xid_field_response;
217
Philipp4ac3aee2016-08-10 12:24:09 +0200218 /* Parse and analyze XID-Request */
219 xid_fields =
220 gprs_llc_parse_xid(lle->llme, bytes_request, bytes_request_len);
221 if (xid_fields) {
222 xid_fields_response = talloc_zero(lle->llme, struct llist_head);
223 INIT_LLIST_HEAD(xid_fields_response);
224 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
225
226 /* Process LLC-XID fields: */
227 llist_for_each_entry(xid_field, xid_fields, list) {
228
229 if (xid_field->type != GPRS_LLC_XID_T_L3_PAR) {
230 /* FIXME: Check the incoming XID parameters for
231 * for validity. Currently we just blindly
232 * accept all XID fields by just echoing them.
233 * There is a remaining risk of malfunction
Philipp532480a2016-12-23 11:05:11 +0100234 * when a MS submits values which defer from
Philipp4ac3aee2016-08-10 12:24:09 +0200235 * the default! */
236 LOGP(DLLC, LOGL_NOTICE,
Max549ebc72016-11-18 14:07:04 +0100237 "Echoing XID-Field: XID: type %s, data_len=%d, data=%s\n",
238 get_value_string(gprs_llc_xid_type_names,
239 xid_field->type),
240 xid_field->data_len,
Philipp4ac3aee2016-08-10 12:24:09 +0200241 osmo_hexdump_nospc(xid_field->data,
242 xid_field->data_len));
243 xid_field_response =
244 gprs_llc_dup_xid_field
245 (lle->llme, xid_field);
246 llist_add(&xid_field_response->list,
247 xid_fields_response);
248 }
249 }
250
Philippf1f34362016-08-26 17:00:21 +0200251 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
252 llist_for_each_entry(xid_field, xid_fields, list) {
253 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR) {
254
255 xid_field_response =
256 talloc_zero(lle->llme,
257 struct gprs_llc_xid_field);
258 rc = sndcp_sn_xid_ind(xid_field,
259 xid_field_response, lle);
260 if (rc == 0)
261 llist_add(&xid_field_response->list,
262 xid_fields_response);
263 else
264 talloc_free(xid_field_response);
265 }
266 }
267
Philipp4ac3aee2016-08-10 12:24:09 +0200268 rc = gprs_llc_compile_xid(bytes_response,
269 bytes_response_maxlen,
270 xid_fields_response);
271 talloc_free(xid_fields_response);
272 talloc_free(xid_fields);
273 }
274
275 return rc;
276}
277
Philipp532480a2016-12-23 11:05:11 +0100278/* Dispatch XID indications and responses comming from the MS */
Philipp4ac3aee2016-08-10 12:24:09 +0200279static void rx_llc_xid(struct gprs_llc_lle *lle,
280 struct gprs_llc_hdr_parsed *gph)
281{
282 uint8_t response[1024];
283 int response_len;
284
285 /* FIXME: 8.5.3.3: check if XID is invalid */
286 if (gph->is_cmd) {
287 LOGP(DLLC, LOGL_NOTICE,
Philipp532480a2016-12-23 11:05:11 +0100288 "Received XID indication from MS.\n");
Philipp4ac3aee2016-08-10 12:24:09 +0200289
290 struct msgb *resp;
291 uint8_t *xid;
292 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
293
294 response_len =
295 gprs_llc_process_xid_ind(gph->data, gph->data_len,
296 response, sizeof(response),
297 lle);
Philippf1f34362016-08-26 17:00:21 +0200298 if (response_len < 0) {
299 LOGP(DLLC, LOGL_ERROR,
300 "invalid XID indication received!\n");
301 } else {
302 xid = msgb_put(resp, response_len);
303 memcpy(xid, response, response_len);
304 }
Philipp4ac3aee2016-08-10 12:24:09 +0200305 gprs_llc_tx_xid(lle, resp, 0);
306 } else {
307 LOGP(DLLC, LOGL_NOTICE,
Philipp532480a2016-12-23 11:05:11 +0100308 "Received XID confirmation from MS.\n");
Philipp4ac3aee2016-08-10 12:24:09 +0200309 gprs_llc_process_xid_conf(gph->data, gph->data_len, lle);
310 /* FIXME: if we had sent a XID reset, send
311 * LLGMM-RESET.conf to GMM */
312 }
313}
314
Philipp4ac3aee2016-08-10 12:24:09 +0200315/* Set of LL-XID negotiation (See also: TS 101 351, Section 7.2.2.4) */
316int gprs_ll_xid_req(struct gprs_llc_lle *lle,
317 struct gprs_llc_xid_field *l3_xid_field)
318{
319 /* Note: This functions is calle from gprs_sndcp.c */
320
321 uint8_t xid_bytes[1024];;
322 int xid_bytes_len;
323 uint8_t *xid;
324 struct msgb *msg;
Max549ebc72016-11-18 14:07:04 +0100325 const char *ftype;
Philipp4ac3aee2016-08-10 12:24:09 +0200326
327 /* Generate XID */
328 xid_bytes_len =
329 gprs_llc_generate_xid(xid_bytes, sizeof(xid_bytes),
330 l3_xid_field, lle->llme);
331
332 /* Only perform XID sending if the XID message contains something */
333 if (xid_bytes_len > 0) {
334 /* Transmit XID bytes */
335 msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
336 xid = msgb_put(msg, xid_bytes_len);
337 memcpy(xid, xid_bytes, xid_bytes_len);
Max549ebc72016-11-18 14:07:04 +0100338 if (l3_xid_field)
339 ftype = get_value_string(gprs_llc_xid_type_names,
340 l3_xid_field->type);
341 else
342 ftype = "NULL";
343 LOGP(DLLC, LOGL_NOTICE, "Sending XID type %s (%d bytes) request"
Philipp532480a2016-12-23 11:05:11 +0100344 " to MS...\n", ftype, xid_bytes_len);
Philipp4ac3aee2016-08-10 12:24:09 +0200345 gprs_llc_tx_xid(lle, msg, 1);
346 } else {
347 LOGP(DLLC, LOGL_ERROR,
348 "XID-Message generation failed, XID not sent!\n");
349 return -EINVAL;
350 }
351
352 return 0;
353}
354/* END XID RELATED */
355
356
357
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200358
Harald Weltefaa70ff2012-06-17 09:31:16 +0800359/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
360 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
361static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
362{
363 struct bssgp_dl_ud_par dup;
364 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
365
Harald Welte8c004962012-07-04 21:53:12 +0200366 memset(&dup, 0, sizeof(dup));
367 /* before we have received some identity from the MS, we might
368 * not yet have a MMC context (e.g. XID negotiation of primarly
Philipp4ac3aee2016-08-10 12:24:09 +0200369 * LLC connection from GMM sapi). */
Harald Welte8c004962012-07-04 21:53:12 +0200370 if (mmctx) {
371 dup.imsi = mmctx->imsi;
372 dup.drx_parms = mmctx->drx_parms;
373 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
374 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +0200375
376 /* make sure we only send it to the right llme */
Harald Weltef97ee042015-12-25 19:12:21 +0100377 OSMO_ASSERT(msgb_tlli(msg) == mmctx->gb.llme->tlli
378 || msgb_tlli(msg) == mmctx->gb.llme->old_tlli);
Harald Welte8c004962012-07-04 21:53:12 +0200379 }
Harald Weltefaa70ff2012-06-17 09:31:16 +0800380 memcpy(&dup.qos_profile, qos_profile_default,
381 sizeof(qos_profile_default));
382
Harald Weltece95b272012-06-17 13:04:02 +0800383 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +0800384}
385
386
Harald Welte1d9d9442010-06-03 07:11:04 +0200387/* Section 8.9.9 LLC layer parameter default values */
Daniel Willmann46d13262014-06-27 17:05:48 +0200388static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = {
Harald Welte1d9d9442010-06-03 07:11:04 +0200389 [1] = {
390 .t200_201 = 5,
391 .n200 = 3,
392 .n201_u = 400,
393 },
394 [2] = {
395 .t200_201 = 5,
396 .n200 = 3,
397 .n201_u = 270,
398 },
399 [3] = {
400 .iov_i_exp = 27,
401 .t200_201 = 5,
402 .n200 = 3,
403 .n201_u = 500,
404 .n201_i = 1503,
405 .mD = 1520,
406 .mU = 1520,
407 .kD = 16,
408 .kU = 16,
409 },
410 [5] = {
411 .iov_i_exp = 27,
412 .t200_201 = 10,
413 .n200 = 3,
414 .n201_u = 500,
415 .n201_i = 1503,
416 .mD = 760,
417 .mU = 760,
418 .kD = 8,
419 .kU = 8,
420 },
421 [7] = {
422 .t200_201 = 20,
423 .n200 = 3,
424 .n201_u = 270,
425 },
426 [8] = {
427 .t200_201 = 20,
428 .n200 = 3,
429 .n201_u = 270,
430 },
431 [9] = {
432 .iov_i_exp = 27,
433 .t200_201 = 20,
434 .n200 = 3,
435 .n201_u = 500,
436 .n201_i = 1503,
437 .mD = 380,
438 .mU = 380,
439 .kD = 4,
440 .kU = 4,
441 },
442 [11] = {
443 .iov_i_exp = 27,
444 .t200_201 = 40,
445 .n200 = 3,
446 .n201_u = 500,
447 .n201_i = 1503,
448 .mD = 190,
449 .mU = 190,
450 .kD = 2,
451 .kU = 2,
452 },
453};
454
Harald Welte807a5d82010-06-01 11:53:01 +0200455LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200456void *llc_tall_ctx;
457
458/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200459static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200460{
Harald Welte807a5d82010-06-01 11:53:01 +0200461 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200462
Harald Welte807a5d82010-06-01 11:53:01 +0200463 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
464 if (llme->tlli == tlli || llme->old_tlli == tlli)
465 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200466 }
467 return NULL;
468}
469
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200470struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi)
471{
472 struct gprs_llc_llme *llme;
473 struct gprs_llc_lle *lle;
474
475 lle = lle_by_tlli_sapi(tlli, sapi);
476 if (lle)
477 return lle;
478
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200479 LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, "
480 "creating LLME on the fly\n", tlli);
481 llme = llme_alloc(tlli);
482 lle = &llme->lle[sapi];
483 return lle;
484}
485
486struct llist_head *gprs_llme_list(void)
487{
488 return &gprs_llc_llmes;
489}
490
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200491/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
492static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
493 uint8_t sapi, enum gprs_llc_cmd cmd)
494{
495 struct gprs_llc_lle *lle;
496
497 /* We already know about this TLLI */
498 lle = lle_by_tlli_sapi(tlli, sapi);
499 if (lle)
500 return lle;
501
502 /* Maybe it is a routing area update but we already know this sapi? */
503 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
Jacob Erlbeck3fbf0a32016-01-04 18:43:32 +0100504 lle = lle_by_tlli_sapi(tlli, sapi);
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200505 if (lle) {
506 LOGP(DLLC, LOGL_NOTICE,
507 "LLC RX: Found a local entry for TLLI 0x%08x\n",
508 tlli);
509 return lle;
510 }
511 }
512
513 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
514 * except UID and XID frames with SAPI=1 */
515 if (sapi == GPRS_SAPI_GMM &&
516 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
517 struct gprs_llc_llme *llme;
518 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
519 llme = llme_alloc(tlli);
Daniel Willmann46553142014-09-03 17:46:44 +0200520 LOGP(DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, "
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200521 "creating LLME on the fly\n", tlli);
522 lle = &llme->lle[sapi];
523 return lle;
524 }
525
526 LOGP(DLLC, LOGL_NOTICE,
527 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
528 tlli, sapi);
529 return NULL;
530}
531
Harald Welte1d9d9442010-06-03 07:11:04 +0200532static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200533{
Harald Welte807a5d82010-06-01 11:53:01 +0200534 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200535
Harald Welte807a5d82010-06-01 11:53:01 +0200536 lle->llme = llme;
537 lle->sapi = sapi;
538 lle->state = GPRS_LLES_UNASSIGNED;
539
Harald Welte1d9d9442010-06-03 07:11:04 +0200540 /* Initialize according to parameters */
541 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200542}
543
544static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
545{
546 struct gprs_llc_llme *llme;
547 uint32_t i;
548
549 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
550 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200551 return NULL;
552
Harald Welte807a5d82010-06-01 11:53:01 +0200553 llme->tlli = tlli;
Harald Welte875840c2010-07-01 11:54:31 +0200554 llme->old_tlli = 0xffffffff;
Harald Welte807a5d82010-06-01 11:53:01 +0200555 llme->state = GPRS_LLMS_UNASSIGNED;
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100556 llme->age_timestamp = GPRS_LLME_RESET_AGE;
Max5aa51962016-07-06 11:33:04 +0200557 llme->cksn = GSM_KEY_SEQ_INVAL;
Harald Weltea2665542010-05-02 09:28:11 +0200558
Harald Welte807a5d82010-06-01 11:53:01 +0200559 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
560 lle_init(llme, i);
561
562 llist_add(&llme->list, &gprs_llc_llmes);
563
Philippf1f34362016-08-26 17:00:21 +0200564 llme->comp.proto = gprs_sndcp_comp_alloc(llme);
565 llme->comp.data = gprs_sndcp_comp_alloc(llme);
566
Harald Welte807a5d82010-06-01 11:53:01 +0200567 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200568}
569
Harald Weltef7fef482010-06-28 22:18:26 +0200570static void llme_free(struct gprs_llc_llme *llme)
571{
Philippf1f34362016-08-26 17:00:21 +0200572 gprs_sndcp_comp_free(llme->comp.proto);
573 gprs_sndcp_comp_free(llme->comp.data);
Philipp4ac3aee2016-08-10 12:24:09 +0200574 talloc_free(llme->xid);
Harald Weltef7fef482010-06-28 22:18:26 +0200575 llist_del(&llme->list);
576 talloc_free(llme);
577}
578
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200579#if 0
580/* FIXME: Unused code... */
Harald Welte9b455bf2010-03-14 15:45:01 +0800581static void t200_expired(void *data)
582{
583 struct gprs_llc_lle *lle = data;
584
585 /* 8.5.1.3: Expiry of T200 */
586
Harald Welte1d9d9442010-06-03 07:11:04 +0200587 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800588 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200589 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800590 }
591
592 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200593 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200594 /* FIXME: retransmit SABM */
595 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800596 lle->retrans_ctr++;
597 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200598 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200599 /* FIXME: retransmit DISC */
600 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800601 lle->retrans_ctr++;
602 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200603 default:
604 LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state);
605 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800606 }
607
608}
609
610static void t201_expired(void *data)
611{
612 struct gprs_llc_lle *lle = data;
613
Harald Welte1d9d9442010-06-03 07:11:04 +0200614 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200615 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
616 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800617 lle->retrans_ctr++;
618 }
619}
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200620#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800621
Harald Welte10997d02010-05-03 12:28:12 +0200622int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
623 enum gprs_llc_u_cmd u_cmd, int pf_bit)
624{
625 uint8_t *fcs, *llch;
626 uint8_t addr, ctrl;
627 uint32_t fcs_calc;
628
629 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
630
631 /* Address Field */
632 addr = sapi & 0xf;
633 if (command)
634 addr |= 0x40;
635
636 /* 6.3 Figure 8 */
637 ctrl = 0xe0 | u_cmd;
638 if (pf_bit)
639 ctrl |= 0x10;
640
641 /* prepend LLC UI header */
642 llch = msgb_push(msg, 2);
643 llch[0] = addr;
644 llch[1] = ctrl;
645
646 /* append FCS to end of frame */
647 fcs = msgb_put(msg, 3);
648 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
649 fcs[0] = fcs_calc & 0xff;
650 fcs[1] = (fcs_calc >> 8) & 0xff;
651 fcs[2] = (fcs_calc >> 16) & 0xff;
652
653 /* Identifiers passed down: (BVCI, NSEI) */
654
Alexander Couzens4e699a92016-07-05 11:04:27 +0200655 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_PACKETS]);
656 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_BYTES], msg->len);
657
Harald Welte1ae09c72010-05-13 19:22:55 +0200658 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800659 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200660}
661
662/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200663static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
664 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200665{
666 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200667 msgb_tlli(msg) = lle->llme->tlli;
668 msgb_bvci(msg) = lle->llme->bvci;
669 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200670
Harald Welte0c1a3032011-10-16 18:49:05 +0200671 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200672}
673
Max1de15912016-07-11 12:42:12 +0200674/* encrypt information field + FCS, if needed! */
675static int apply_gea(struct gprs_llc_lle *lle, uint16_t crypt_len, uint16_t nu,
676 uint32_t oc, uint8_t sapi, uint8_t *fcs, uint8_t *data)
677{
678 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
679
680 if (lle->llme->algo == GPRS_ALGO_GEA0)
681 return -EINVAL;
682
683 /* Compute the 'Input' Paraemeter */
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200684 uint32_t fcs_calc, iv = gprs_cipher_gen_input_ui(lle->llme->iov_ui, sapi,
Max1de15912016-07-11 12:42:12 +0200685 nu, oc);
686 /* Compute gamma that we need to XOR with the data */
687 int r = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
688 lle->llme->kc, iv,
689 fcs ? GPRS_CIPH_SGSN2MS : GPRS_CIPH_MS2SGSN);
690 if (r < 0) {
691 LOGP(DLLC, LOGL_ERROR, "Error producing %s gamma for UI "
692 "frame: %d\n", get_value_string(gprs_cipher_names,
693 lle->llme->algo), r);
694 return -ENOMSG;
695 }
696
697 if (fcs) {
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200698 /* Mark frame as encrypted and update FCS */
699 data[2] |= 0x02;
700 fcs_calc = gprs_llc_fcs(data, fcs - data);
701 fcs[0] = fcs_calc & 0xff;
702 fcs[1] = (fcs_calc >> 8) & 0xff;
703 fcs[2] = (fcs_calc >> 16) & 0xff;
Max1de15912016-07-11 12:42:12 +0200704 data += 3;
705 }
706
707 /* XOR the cipher output with the data */
708 for (r = 0; r < crypt_len; r++)
709 *(data + r) ^= cipher_out[r];
710
711 return 0;
712}
713
Max82040102016-07-06 11:59:18 +0200714/* Transmit a UI frame over the given SAPI:
715 'encryptable' indicates whether particular message can be encrypted according
716 to 3GPP TS 24.008 § 4.7.1.2
717 */
Harald Welte56a01452010-05-31 22:12:30 +0200718int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
Max82040102016-07-06 11:59:18 +0200719 struct sgsn_mm_ctx *mmctx, bool encryptable)
Harald Welte9b455bf2010-03-14 15:45:01 +0800720{
Harald Weltee6afd602010-05-02 11:19:37 +0200721 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200722 uint8_t *fcs, *llch;
723 uint8_t addr, ctrl[2];
724 uint32_t fcs_calc;
725 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200726 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800727
Harald Weltee6afd602010-05-02 11:19:37 +0200728 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
729
730 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200731 lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi);
Harald Welte1d9d9442010-06-03 07:11:04 +0200732
733 if (msg->len > lle->params.n201_u) {
734 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
735 msg->len, lle->params.n201_u);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200736 msgb_free(msg);
Harald Welte1d9d9442010-06-03 07:11:04 +0200737 return -EFBIG;
738 }
739
Max5aa51962016-07-06 11:33:04 +0200740 gprs_llme_copy_key(mmctx, lle->llme);
741
Harald Weltee6afd602010-05-02 11:19:37 +0200742 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200743 lle->llme->bvci = msgb_bvci(msg);
744 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200745
Harald Welted07b4f92010-06-30 23:07:59 +0200746 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200747 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200748 oc = lle->oc_ui_send;
749 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200750 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200751 /* Increment Overflow Counter, if needed */
752 if ((lle->vu_send + 1) / 512)
753 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200754
Harald Welte9b455bf2010-03-14 15:45:01 +0800755 /* Address Field */
756 addr = sapi & 0xf;
757 if (command)
758 addr |= 0x40;
759
760 /* Control Field */
761 ctrl[0] = 0xc0;
762 ctrl[0] |= nu >> 6;
763 ctrl[1] = (nu << 2) & 0xfc;
764 ctrl[1] |= 0x01; /* Protected Mode */
765
766 /* prepend LLC UI header */
767 llch = msgb_push(msg, 3);
768 llch[0] = addr;
769 llch[1] = ctrl[0];
770 llch[2] = ctrl[1];
771
772 /* append FCS to end of frame */
773 fcs = msgb_put(msg, 3);
774 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
775 fcs[0] = fcs_calc & 0xff;
776 fcs[1] = (fcs_calc >> 8) & 0xff;
777 fcs[2] = (fcs_calc >> 16) & 0xff;
778
Max82040102016-07-06 11:59:18 +0200779 if (lle->llme->algo != GPRS_ALGO_GEA0 && encryptable) {
Max1de15912016-07-11 12:42:12 +0200780 int rc = apply_gea(lle, fcs - llch, nu, oc, sapi, fcs, llch);
Harald Welted07b4f92010-06-30 23:07:59 +0200781 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200782 msgb_free(msg);
Harald Welted07b4f92010-06-30 23:07:59 +0200783 return rc;
784 }
Harald Welted07b4f92010-06-30 23:07:59 +0200785 }
786
Alexander Couzens33163972016-10-04 17:53:21 +0200787 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_PACKETS]);
788 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_BYTES], msg->len);
789
Harald Weltee6afd602010-05-02 11:19:37 +0200790 /* Identifiers passed down: (BVCI, NSEI) */
791
Harald Welte1ae09c72010-05-13 19:22:55 +0200792 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800793 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800794}
795
Harald Welte9b455bf2010-03-14 15:45:01 +0800796static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
797 struct gprs_llc_lle *lle)
798{
799 switch (gph->cmd) {
800 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
801 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200802 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800803 /* start re-establishment (8.7.1) */
804 }
Harald Welte807a5d82010-06-01 11:53:01 +0200805 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800806 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200807 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800808 /* FIXME: process data */
809 break;
810 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
811 /* FIXME: Send UA */
812 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200813 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800814 break;
815 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200816 if (lle->state == GPRS_LLES_LOCAL_EST)
817 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800818 break;
819 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200820 if (lle->state == GPRS_LLES_LOCAL_EST)
821 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800822 break;
823 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
824 break;
825 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200826 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800827 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200828 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400829 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
830 LOGP(DLLC, LOGL_NOTICE,
831 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800832 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200833 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200834
835 /* HACK: non-standard recovery handling. If remote LLE
836 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800837 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200838 * and 'learn' the new sequence number */
839 if (gph->seq_tx != lle->vu_recv_last) {
840 lle->vu_recv_last = gph->seq_tx;
841 lle->vu_recv_duplicates = 0;
842 } else {
843 lle->vu_recv_duplicates++;
844 if (lle->vu_recv_duplicates < 3)
845 return -EIO;
846 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
847 "N(U=%d) after receiving %u duplicates\n",
848 lle->llme ? lle->llme->tlli : -1,
849 gph->seq_tx, lle->vu_recv_duplicates);
850 }
Harald Welteebabdea2010-06-01 18:28:10 +0200851 }
852 /* Increment the sequence number that we expect in the next frame */
853 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200854 /* Increment Overflow Counter */
855 if ((gph->seq_tx + 1) / 512)
856 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200857 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200858 default:
859 LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd);
860 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800861 }
862
863 return 0;
864}
865
Harald Weltea2665542010-05-02 09:28:11 +0200866/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800867int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
868{
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200869 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800870 struct gprs_llc_hdr_parsed llhp;
Max549ebc72016-11-18 14:07:04 +0100871 struct gprs_llc_lle *lle = NULL;
Max82040102016-07-06 11:59:18 +0200872 bool drop_cipherable = false;
Harald Weltea2665542010-05-02 09:28:11 +0200873 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800874
Harald Welte11d7c102010-05-02 11:54:55 +0200875 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
876
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800877 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800878 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte1ae09c72010-05-13 19:22:55 +0200879 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200880 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200881 return rc;
882 }
883
Harald Welte807a5d82010-06-01 11:53:01 +0200884 switch (gprs_tlli_type(msgb_tlli(msg))) {
885 case TLLI_LOCAL:
886 case TLLI_FOREIGN:
887 case TLLI_RANDOM:
888 case TLLI_AUXILIARY:
889 break;
890 default:
891 LOGP(DLLC, LOGL_ERROR,
892 "Discarding frame with strange TLLI type\n");
893 break;
894 }
895
Harald Weltea2665542010-05-02 09:28:11 +0200896 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200897 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200898 if (!lle) {
899 switch (llhp.sapi) {
900 case GPRS_SAPI_SNDCP3:
901 case GPRS_SAPI_SNDCP5:
902 case GPRS_SAPI_SNDCP9:
903 case GPRS_SAPI_SNDCP11:
904 /* Ask an upper layer for help. */
Alexander Couzens58f446c2016-08-30 18:51:50 +0200905 return gsm0408_gprs_force_reattach_oldmsg(msg, NULL);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200906 default:
907 break;
908 }
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200909 return 0;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200910 }
Max549ebc72016-11-18 14:07:04 +0100911 gprs_llc_hdr_dump(&llhp, lle);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100912 /* reset age computation */
913 lle->llme->age_timestamp = GPRS_LLME_RESET_AGE;
914
Harald Welted07b4f92010-06-30 23:07:59 +0200915 /* decrypt information field + FCS, if needed! */
916 if (llhp.is_encrypted) {
Max1de15912016-07-11 12:42:12 +0200917 if (lle->llme->algo != GPRS_ALGO_GEA0) {
918 rc = apply_gea(lle, llhp.data_len + 3, llhp.seq_tx,
919 lle->oc_ui_recv, lle->sapi, NULL,
920 llhp.data);
921 if (rc < 0)
922 return rc;
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200923 llhp.fcs = *(llhp.data + llhp.data_len);
924 llhp.fcs |= *(llhp.data + llhp.data_len + 1) << 8;
925 llhp.fcs |= *(llhp.data + llhp.data_len + 2) << 16;
Max1de15912016-07-11 12:42:12 +0200926 } else {
Harald Welted07b4f92010-06-30 23:07:59 +0200927 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
928 "has no KC/Algo! Dropping.\n");
929 return 0;
930 }
Harald Welted07b4f92010-06-30 23:07:59 +0200931 } else {
Max82040102016-07-06 11:59:18 +0200932 if (lle->llme->algo != GPRS_ALGO_GEA0 &&
933 lle->llme->cksn != GSM_KEY_SEQ_INVAL)
934 drop_cipherable = true;
Harald Welted07b4f92010-06-30 23:07:59 +0200935 }
936
937 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200938 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200939 if (llhp.fcs != llhp.fcs_calc) {
940 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
941 return -EIO;
942 }
943
Harald Welte10997d02010-05-03 12:28:12 +0200944 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200945 lle->llme->bvci = msgb_bvci(msg);
946 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200947
Harald Welte1ae09c72010-05-13 19:22:55 +0200948 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800949 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200950 if (rc < 0)
951 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800952
Alexander Couzens4e699a92016-07-05 11:04:27 +0200953 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_PACKETS]);
954 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_BYTES], msg->len);
955
Harald Welte1ae09c72010-05-13 19:22:55 +0200956 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte22df4ac2015-08-16 15:23:32 +0200957 if (llhp.cmd == GPRS_LLC_UI && llhp.data && llhp.data_len) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200958 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800959 switch (llhp.sapi) {
960 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200961 /* send LL_UNITDATA_IND to GMM */
Max82040102016-07-06 11:59:18 +0200962 rc = gsm0408_gprs_rcvmsg_gb(msg, lle->llme,
963 drop_cipherable);
Harald Weltea2665542010-05-02 09:28:11 +0200964 break;
Harald Weltea2665542010-05-02 09:28:11 +0200965 case GPRS_SAPI_SNDCP3:
966 case GPRS_SAPI_SNDCP5:
967 case GPRS_SAPI_SNDCP9:
968 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +0200969 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
970 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
971 break;
Harald Weltea2665542010-05-02 09:28:11 +0200972 case GPRS_SAPI_SMS:
973 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +0200974 case GPRS_SAPI_TOM2:
975 case GPRS_SAPI_TOM8:
976 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200977 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200978 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200979 rc = -EINVAL;
980 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800981 }
982 }
983
Harald Weltea2665542010-05-02 09:28:11 +0200984 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800985}
Harald Welte807a5d82010-06-01 11:53:01 +0200986
Max5aa51962016-07-06 11:33:04 +0200987/* Propagate crypto parameters MM -> LLME */
988void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme)
989{
990 if (!mm)
991 return;
992 if (mm->ciph_algo != GPRS_ALGO_GEA0) {
993 llme->algo = mm->ciph_algo;
994 if (llme->cksn != mm->auth_triplet.key_seq &&
995 mm->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
996 memcpy(llme->kc, mm->auth_triplet.vec.kc,
997 gprs_cipher_key_length(mm->ciph_algo));
998 llme->cksn = mm->auth_triplet.key_seq;
999 }
1000 } else
1001 llme->cksn = GSM_KEY_SEQ_INVAL;
1002}
1003
Harald Welte807a5d82010-06-01 11:53:01 +02001004/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
1005int gprs_llgmm_assign(struct gprs_llc_llme *llme,
Max5aa51962016-07-06 11:33:04 +02001006 uint32_t old_tlli, uint32_t new_tlli)
Harald Welte807a5d82010-06-01 11:53:01 +02001007{
1008 unsigned int i;
1009
1010 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
1011 /* TLLI Assignment 8.3.1 */
1012 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
1013 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
1014 * old is unassigned. Only TLLI new shall be accepted when
1015 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +02001016 if (llme->old_tlli != 0xffffffff) {
1017 llme->old_tlli = 0xffffffff;
1018 llme->tlli = new_tlli;
1019 } else {
1020 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
1021 * TLLI assignmemt according to 8.3.1 */
1022 llme->old_tlli = 0xffffffff;
1023 llme->tlli = new_tlli;
1024 llme->state = GPRS_LLMS_ASSIGNED;
1025 /* 8.5.3.1 For all LLE's */
1026 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1027 struct gprs_llc_lle *l = &llme->lle[i];
1028 l->vu_send = l->vu_recv = 0;
1029 l->retrans_ctr = 0;
1030 l->state = GPRS_LLES_ASSIGNED_ADM;
1031 /* FIXME Set parameters according to table 9 */
1032 }
Harald Welte807a5d82010-06-01 11:53:01 +02001033 }
1034 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
1035 /* TLLI Change 8.3.2 */
1036 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +02001037 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freytheraa93bac2013-07-31 11:20:37 +02001038 llme->old_tlli = old_tlli;
Harald Welte807a5d82010-06-01 11:53:01 +02001039 llme->tlli = new_tlli;
1040 llme->state = GPRS_LLMS_ASSIGNED;
1041 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
1042 /* TLLI Unassignment 8.3.3) */
1043 llme->tlli = llme->old_tlli = 0;
1044 llme->state = GPRS_LLMS_UNASSIGNED;
1045 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1046 struct gprs_llc_lle *l = &llme->lle[i];
1047 l->state = GPRS_LLES_UNASSIGNED;
1048 }
Harald Weltef7fef482010-06-28 22:18:26 +02001049 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +02001050 } else
1051 return -EINVAL;
1052
1053 return 0;
1054}
Harald Welte496aee42010-06-30 19:59:55 +02001055
Max39550252016-06-28 17:39:20 +02001056/* TLLI unassignment */
1057int gprs_llgmm_unassign(struct gprs_llc_llme *llme)
1058{
Max5aa51962016-07-06 11:33:04 +02001059 return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff);
Max39550252016-06-28 17:39:20 +02001060}
1061
Harald Welte0c1a3032011-10-16 18:49:05 +02001062/* Chapter 7.2.1.2 LLGMM-RESET.req */
1063int gprs_llgmm_reset(struct gprs_llc_llme *llme)
1064{
1065 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001066 struct gprs_llc_lle *lle = &llme->lle[1];
Philipp4ac3aee2016-08-10 12:24:09 +02001067 uint8_t xid_bytes[1024];
1068 int xid_bytes_len;
1069 uint8_t *xid;
Harald Welte0c1a3032011-10-16 18:49:05 +02001070
Philipp4ac3aee2016-08-10 12:24:09 +02001071 LOGP(DLLC, LOGL_NOTICE, "LLGM Reset\n");
Maxb997f842016-07-06 15:57:01 +02001072 if (RAND_bytes((uint8_t *) &llme->iov_ui, 4) != 1) {
1073 LOGP(DLLC, LOGL_NOTICE, "RAND_bytes failed for LLC XID reset, "
1074 "falling back to rand()\n");
1075 llme->iov_ui = rand();
1076 }
1077
Philipp4ac3aee2016-08-10 12:24:09 +02001078 /* Generate XID message */
1079 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes,
1080 sizeof(xid_bytes),llme->iov_ui,llme);
Harald Welte7e5bb622016-09-28 08:20:58 +08001081 if (xid_bytes_len < 0)
Philipp4ac3aee2016-08-10 12:24:09 +02001082 return -EINVAL;
1083 xid = msgb_put(msg, xid_bytes_len);
1084 memcpy(xid, xid_bytes, xid_bytes_len);
Harald Welte0c1a3032011-10-16 18:49:05 +02001085
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001086 /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */
1087 lle->vu_recv = 0;
1088 lle->vu_send = 0;
1089 lle->oc_ui_send = 0;
1090 lle->oc_ui_recv = 0;
1091
Harald Welte0c1a3032011-10-16 18:49:05 +02001092 /* FIXME: Start T200, wait for XID response */
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001093 return gprs_llc_tx_xid(lle, msg, 1);
Harald Welte0c1a3032011-10-16 18:49:05 +02001094}
1095
Maxb997f842016-07-06 15:57:01 +02001096int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi,
1097 struct gprs_llc_llme *llme)
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001098{
1099 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Philipp4ac3aee2016-08-10 12:24:09 +02001100 uint8_t xid_bytes[1024];
1101 int xid_bytes_len;
1102 uint8_t *xid;
Maxb997f842016-07-06 15:57:01 +02001103
Philipp4ac3aee2016-08-10 12:24:09 +02001104 LOGP(DLLC, LOGL_NOTICE, "LLGM Reset\n");
Maxb997f842016-07-06 15:57:01 +02001105 if (RAND_bytes((uint8_t *) &llme->iov_ui, 4) != 1) {
1106 LOGP(DLLC, LOGL_NOTICE, "RAND_bytes failed for LLC XID reset, "
1107 "falling back to rand()\n");
1108 llme->iov_ui = rand();
1109 }
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}