blob: 32920daefe2ec7fbdd933d2c1729ec23ed03b455 [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>
Harald Weltea2665542010-05-02 09:28:11 +020034
35#include <openbsc/gsm_data.h>
36#include <openbsc/debug.h>
Harald Welte807a5d82010-06-01 11:53:01 +020037#include <openbsc/gprs_sgsn.h>
38#include <openbsc/gprs_gmm.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080039#include <openbsc/gprs_llc.h>
40#include <openbsc/crc24.h>
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +020041#include <openbsc/sgsn.h>
Philipp4ac3aee2016-08-10 12:24:09 +020042#include <openbsc/gprs_llc_xid.h>
43#include <openbsc/gprs_sndcp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080044
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +020045static struct gprs_llc_llme *llme_alloc(uint32_t tlli);
Philipp4ac3aee2016-08-10 12:24:09 +020046static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
47 int command);
48static int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi,
49 int command, enum gprs_llc_u_cmd u_cmd, int pf_bit);
50
51/* BEGIN XID RELATED */
52
53/* Generate XID message */
54static int gprs_llc_generate_xid(uint8_t *bytes, int bytes_len,
55 struct gprs_llc_xid_field *l3_xid_field,
56 struct gprs_llc_llme *llme)
57{
58 /* Note: Called by gprs_ll_xid_req() */
59
60 LLIST_HEAD(xid_fields);
61
62 struct gprs_llc_xid_field xid_version;
63 struct gprs_llc_xid_field xid_n201u;
64 struct gprs_llc_xid_field xid_n201i;
65
66 xid_version.type = GPRS_LLC_XID_T_VERSION;
67 xid_version.data = (uint8_t *) "\x00";
68 xid_version.data_len = 1;
69
70 xid_n201u.type = GPRS_LLC_XID_T_N201_U;
71 xid_n201u.data = (uint8_t *) "\x05\xf0";
72 xid_n201u.data_len = 2;
73
74 xid_n201i.type = GPRS_LLC_XID_T_N201_I;
75 xid_n201i.data = (uint8_t *) "\x05\xf0";
76 xid_n201i.data_len = 2;
77
78 /* Add locally managed XID Fields */
79 llist_add(&xid_n201i.list, &xid_fields);
80 llist_add(&xid_n201u.list, &xid_fields);
81 llist_add(&xid_version.list, &xid_fields);
82
83 /* Append layer 3 XID field (if present) */
84 if (l3_xid_field) {
85 /* Enforce layer 3 XID type (just to be sure) */
86 l3_xid_field->type = GPRS_LLC_XID_T_L3_PAR;
87
88 /* Add Layer 3 XID field to the list */
89 llist_add(&l3_xid_field->list, &xid_fields);
90 }
91
92 /* Store generated XID for later reference */
93 talloc_free(llme->xid);
94 llme->xid = gprs_llc_copy_xid(llme, &xid_fields);
95
96 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
97}
98
99/* Generate XID message that will cause the GMM to reset */
100static int gprs_llc_generate_xid_for_gmm_reset(uint8_t *bytes,
101 int bytes_len, uint32_t iov_ui,
102 struct gprs_llc_llme *llme)
103{
104 /* Called by gprs_llgmm_reset() and
105 * gprs_llgmm_reset_oldmsg() */
106
107 LLIST_HEAD(xid_fields);
108
109 struct gprs_llc_xid_field xid_reset;
110 struct gprs_llc_xid_field xid_iovui;
111
112 /* First XID component must be RESET */
113 xid_reset.type = GPRS_LLC_XID_T_RESET;
114 xid_reset.data = NULL;
115 xid_reset.data_len = 0;
116
117 /* Add new IOV-UI */
118 xid_iovui.type = GPRS_LLC_XID_T_IOV_UI;
119 xid_iovui.data = (uint8_t *) & iov_ui;
120 xid_iovui.data_len = 4;
121
122 /* Add locally managed XID Fields */
123 llist_add(&xid_iovui.list, &xid_fields);
124 llist_add(&xid_reset.list, &xid_fields);
125
126 /* Store generated XID for later reference */
127 talloc_free(llme->xid);
128 llme->xid = gprs_llc_copy_xid(llme, &xid_fields);
129
130 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
131}
132
133/* Process an incoming XID confirmation */
134static int gprs_llc_process_xid_conf(uint8_t *bytes, int bytes_len,
135 struct gprs_llc_lle *lle)
136{
137 /* Note: This function handles the response of a network originated
138 * XID-Request. There XID messages reflected by the phone are analyzed
139 * and processed here. The caller is called by rx_llc_xid(). */
140
141 struct llist_head *xid_fields;
142 struct gprs_llc_xid_field *xid_field;
143
144 /* Parse and analyze XID-Response */
145 xid_fields = gprs_llc_parse_xid(NULL, bytes, bytes_len);
146
147 if (xid_fields) {
148
149 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
150 llist_for_each_entry(xid_field, xid_fields, list) {
151
152 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
153 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR) {
154 LOGP(DLLC, LOGL_NOTICE,
155 "Ignoring SNDCP-XID-Field: XID: type=%i, data_len=%i, data=%s\n",
156 xid_field->type, xid_field->data_len,
157 osmo_hexdump_nospc(xid_field->data,
158 xid_field->data_len));
159 }
160
161 /* Process LLC-XID fields: */
162 else {
163
164 /* FIXME: Do something more useful with the
165 * echoed XID-Information. Currently we
166 * just ignore the response completely and
167 * by doing so we blindly accept any changes
168 * the MS might have done to the our XID
169 * inquiry. There is a remainig risk of
170 * malfunction! */
171 LOGP(DLLC, LOGL_NOTICE,
172 "Ignoring XID-Field: XID: type=%d, data_len=%d, data=%s\n",
173 xid_field->type, xid_field->data_len,
174 osmo_hexdump_nospc(xid_field->data,
175 xid_field->data_len));
176 }
177 }
178 talloc_free(xid_fields);
179 }
180
181 /* Flush pending XID fields */
182 talloc_free(lle->llme->xid);
183 lle->llme->xid = NULL;
184
185 return 0;
186}
187
188/* Process an incoming XID indication and generate an appropiate response */
189static int gprs_llc_process_xid_ind(uint8_t *bytes_request,
190 int bytes_request_len,
191 uint8_t *bytes_response,
192 int bytes_response_maxlen,
193 struct gprs_llc_lle *lle)
194{
195 /* Note: This function computes the response that is sent back to the
196 * phone when a phone originated XID is received. The function is
197 * called by rx_llc_xid() */
198
199 int rc = -EINVAL;
200
201 struct llist_head *xid_fields;
202 struct llist_head *xid_fields_response;
203
204 struct gprs_llc_xid_field *xid_field;
205 struct gprs_llc_xid_field *xid_field_response;
206
207 /* Flush eventually pending XID fields */
208 talloc_free(lle->llme->xid);
209 lle->llme->xid = NULL;
210
211 /* Parse and analyze XID-Request */
212 xid_fields =
213 gprs_llc_parse_xid(lle->llme, bytes_request, bytes_request_len);
214 if (xid_fields) {
215 xid_fields_response = talloc_zero(lle->llme, struct llist_head);
216 INIT_LLIST_HEAD(xid_fields_response);
217 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
218
219 /* Process LLC-XID fields: */
220 llist_for_each_entry(xid_field, xid_fields, list) {
221
222 if (xid_field->type != GPRS_LLC_XID_T_L3_PAR) {
223 /* FIXME: Check the incoming XID parameters for
224 * for validity. Currently we just blindly
225 * accept all XID fields by just echoing them.
226 * There is a remaining risk of malfunction
227 * when a phone submits values which defer from
228 * the default! */
229 LOGP(DLLC, LOGL_NOTICE,
230 "Echoing XID-Field: XID: type=%d, data_len=%d, data=%s\n",
231 xid_field->type, xid_field->data_len,
232 osmo_hexdump_nospc(xid_field->data,
233 xid_field->data_len));
234 xid_field_response =
235 gprs_llc_dup_xid_field
236 (lle->llme, xid_field);
237 llist_add(&xid_field_response->list,
238 xid_fields_response);
239 }
240 }
241
242 rc = gprs_llc_compile_xid(bytes_response,
243 bytes_response_maxlen,
244 xid_fields_response);
245 talloc_free(xid_fields_response);
246 talloc_free(xid_fields);
247 }
248
249 return rc;
250}
251
252/* Dispatch XID indications and responses comming from the Phone */
253static void rx_llc_xid(struct gprs_llc_lle *lle,
254 struct gprs_llc_hdr_parsed *gph)
255{
256 uint8_t response[1024];
257 int response_len;
258
259 /* FIXME: 8.5.3.3: check if XID is invalid */
260 if (gph->is_cmd) {
261 LOGP(DLLC, LOGL_NOTICE,
262 "Received XID indication from phone.\n");
263
264 struct msgb *resp;
265 uint8_t *xid;
266 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
267
268 response_len =
269 gprs_llc_process_xid_ind(gph->data, gph->data_len,
270 response, sizeof(response),
271 lle);
272 xid = msgb_put(resp, response_len);
273 memcpy(xid, response, response_len);
274
275 gprs_llc_tx_xid(lle, resp, 0);
276 } else {
277 LOGP(DLLC, LOGL_NOTICE,
278 "Received XID confirmation from phone.\n");
279 gprs_llc_process_xid_conf(gph->data, gph->data_len, lle);
280 /* FIXME: if we had sent a XID reset, send
281 * LLGMM-RESET.conf to GMM */
282 }
283}
284
285
286/* Set of LL-XID negotiation (See also: TS 101 351, Section 7.2.2.4) */
287int gprs_ll_xid_req(struct gprs_llc_lle *lle,
288 struct gprs_llc_xid_field *l3_xid_field)
289{
290 /* Note: This functions is calle from gprs_sndcp.c */
291
292 uint8_t xid_bytes[1024];;
293 int xid_bytes_len;
294 uint8_t *xid;
295 struct msgb *msg;
296
297 /* Generate XID */
298 xid_bytes_len =
299 gprs_llc_generate_xid(xid_bytes, sizeof(xid_bytes),
300 l3_xid_field, lle->llme);
301
302 /* Only perform XID sending if the XID message contains something */
303 if (xid_bytes_len > 0) {
304 /* Transmit XID bytes */
305 msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
306 xid = msgb_put(msg, xid_bytes_len);
307 memcpy(xid, xid_bytes, xid_bytes_len);
308 LOGP(DLLC, LOGL_NOTICE, "Sending XID request to phone...\n");
309 gprs_llc_tx_xid(lle, msg, 1);
310 } else {
311 LOGP(DLLC, LOGL_ERROR,
312 "XID-Message generation failed, XID not sent!\n");
313 return -EINVAL;
314 }
315
316 return 0;
317}
318/* END XID RELATED */
319
320
321
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200322
Harald Weltefaa70ff2012-06-17 09:31:16 +0800323/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
324 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
325static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
326{
327 struct bssgp_dl_ud_par dup;
328 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
329
Harald Welte8c004962012-07-04 21:53:12 +0200330 memset(&dup, 0, sizeof(dup));
331 /* before we have received some identity from the MS, we might
332 * not yet have a MMC context (e.g. XID negotiation of primarly
Philipp4ac3aee2016-08-10 12:24:09 +0200333 * LLC connection from GMM sapi). */
Harald Welte8c004962012-07-04 21:53:12 +0200334 if (mmctx) {
335 dup.imsi = mmctx->imsi;
336 dup.drx_parms = mmctx->drx_parms;
337 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
338 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +0200339
340 /* make sure we only send it to the right llme */
Harald Weltef97ee042015-12-25 19:12:21 +0100341 OSMO_ASSERT(msgb_tlli(msg) == mmctx->gb.llme->tlli
342 || msgb_tlli(msg) == mmctx->gb.llme->old_tlli);
Harald Welte8c004962012-07-04 21:53:12 +0200343 }
Harald Weltefaa70ff2012-06-17 09:31:16 +0800344 memcpy(&dup.qos_profile, qos_profile_default,
345 sizeof(qos_profile_default));
346
Harald Weltece95b272012-06-17 13:04:02 +0800347 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +0800348}
349
350
Harald Welte1d9d9442010-06-03 07:11:04 +0200351/* Section 8.9.9 LLC layer parameter default values */
Daniel Willmann46d13262014-06-27 17:05:48 +0200352static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = {
Harald Welte1d9d9442010-06-03 07:11:04 +0200353 [1] = {
354 .t200_201 = 5,
355 .n200 = 3,
356 .n201_u = 400,
357 },
358 [2] = {
359 .t200_201 = 5,
360 .n200 = 3,
361 .n201_u = 270,
362 },
363 [3] = {
364 .iov_i_exp = 27,
365 .t200_201 = 5,
366 .n200 = 3,
367 .n201_u = 500,
368 .n201_i = 1503,
369 .mD = 1520,
370 .mU = 1520,
371 .kD = 16,
372 .kU = 16,
373 },
374 [5] = {
375 .iov_i_exp = 27,
376 .t200_201 = 10,
377 .n200 = 3,
378 .n201_u = 500,
379 .n201_i = 1503,
380 .mD = 760,
381 .mU = 760,
382 .kD = 8,
383 .kU = 8,
384 },
385 [7] = {
386 .t200_201 = 20,
387 .n200 = 3,
388 .n201_u = 270,
389 },
390 [8] = {
391 .t200_201 = 20,
392 .n200 = 3,
393 .n201_u = 270,
394 },
395 [9] = {
396 .iov_i_exp = 27,
397 .t200_201 = 20,
398 .n200 = 3,
399 .n201_u = 500,
400 .n201_i = 1503,
401 .mD = 380,
402 .mU = 380,
403 .kD = 4,
404 .kU = 4,
405 },
406 [11] = {
407 .iov_i_exp = 27,
408 .t200_201 = 40,
409 .n200 = 3,
410 .n201_u = 500,
411 .n201_i = 1503,
412 .mD = 190,
413 .mU = 190,
414 .kD = 2,
415 .kU = 2,
416 },
417};
418
Harald Welte807a5d82010-06-01 11:53:01 +0200419LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200420void *llc_tall_ctx;
421
422/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200423static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200424{
Harald Welte807a5d82010-06-01 11:53:01 +0200425 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200426
Harald Welte807a5d82010-06-01 11:53:01 +0200427 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
428 if (llme->tlli == tlli || llme->old_tlli == tlli)
429 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200430 }
431 return NULL;
432}
433
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200434struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi)
435{
436 struct gprs_llc_llme *llme;
437 struct gprs_llc_lle *lle;
438
439 lle = lle_by_tlli_sapi(tlli, sapi);
440 if (lle)
441 return lle;
442
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200443 LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, "
444 "creating LLME on the fly\n", tlli);
445 llme = llme_alloc(tlli);
446 lle = &llme->lle[sapi];
447 return lle;
448}
449
450struct llist_head *gprs_llme_list(void)
451{
452 return &gprs_llc_llmes;
453}
454
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200455/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
456static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
457 uint8_t sapi, enum gprs_llc_cmd cmd)
458{
459 struct gprs_llc_lle *lle;
460
461 /* We already know about this TLLI */
462 lle = lle_by_tlli_sapi(tlli, sapi);
463 if (lle)
464 return lle;
465
466 /* Maybe it is a routing area update but we already know this sapi? */
467 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
Jacob Erlbeck3fbf0a32016-01-04 18:43:32 +0100468 lle = lle_by_tlli_sapi(tlli, sapi);
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200469 if (lle) {
470 LOGP(DLLC, LOGL_NOTICE,
471 "LLC RX: Found a local entry for TLLI 0x%08x\n",
472 tlli);
473 return lle;
474 }
475 }
476
477 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
478 * except UID and XID frames with SAPI=1 */
479 if (sapi == GPRS_SAPI_GMM &&
480 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
481 struct gprs_llc_llme *llme;
482 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
483 llme = llme_alloc(tlli);
Daniel Willmann46553142014-09-03 17:46:44 +0200484 LOGP(DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, "
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200485 "creating LLME on the fly\n", tlli);
486 lle = &llme->lle[sapi];
487 return lle;
488 }
489
490 LOGP(DLLC, LOGL_NOTICE,
491 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
492 tlli, sapi);
493 return NULL;
494}
495
Harald Welte1d9d9442010-06-03 07:11:04 +0200496static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200497{
Harald Welte807a5d82010-06-01 11:53:01 +0200498 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200499
Harald Welte807a5d82010-06-01 11:53:01 +0200500 lle->llme = llme;
501 lle->sapi = sapi;
502 lle->state = GPRS_LLES_UNASSIGNED;
503
Harald Welte1d9d9442010-06-03 07:11:04 +0200504 /* Initialize according to parameters */
505 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200506}
507
508static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
509{
510 struct gprs_llc_llme *llme;
511 uint32_t i;
512
513 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
514 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200515 return NULL;
516
Harald Welte807a5d82010-06-01 11:53:01 +0200517 llme->tlli = tlli;
Harald Welte875840c2010-07-01 11:54:31 +0200518 llme->old_tlli = 0xffffffff;
Harald Welte807a5d82010-06-01 11:53:01 +0200519 llme->state = GPRS_LLMS_UNASSIGNED;
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100520 llme->age_timestamp = GPRS_LLME_RESET_AGE;
Max5aa51962016-07-06 11:33:04 +0200521 llme->cksn = GSM_KEY_SEQ_INVAL;
Harald Weltea2665542010-05-02 09:28:11 +0200522
Harald Welte807a5d82010-06-01 11:53:01 +0200523 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
524 lle_init(llme, i);
525
526 llist_add(&llme->list, &gprs_llc_llmes);
527
528 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200529}
530
Harald Weltef7fef482010-06-28 22:18:26 +0200531static void llme_free(struct gprs_llc_llme *llme)
532{
Philipp4ac3aee2016-08-10 12:24:09 +0200533 talloc_free(llme->xid);
Harald Weltef7fef482010-06-28 22:18:26 +0200534 llist_del(&llme->list);
535 talloc_free(llme);
536}
537
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200538#if 0
539/* FIXME: Unused code... */
Harald Welte9b455bf2010-03-14 15:45:01 +0800540static void t200_expired(void *data)
541{
542 struct gprs_llc_lle *lle = data;
543
544 /* 8.5.1.3: Expiry of T200 */
545
Harald Welte1d9d9442010-06-03 07:11:04 +0200546 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800547 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200548 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800549 }
550
551 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200552 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200553 /* FIXME: retransmit SABM */
554 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800555 lle->retrans_ctr++;
556 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200557 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200558 /* FIXME: retransmit DISC */
559 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800560 lle->retrans_ctr++;
561 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200562 default:
563 LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state);
564 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800565 }
566
567}
568
569static void t201_expired(void *data)
570{
571 struct gprs_llc_lle *lle = data;
572
Harald Welte1d9d9442010-06-03 07:11:04 +0200573 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200574 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
575 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800576 lle->retrans_ctr++;
577 }
578}
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200579#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800580
Harald Welte10997d02010-05-03 12:28:12 +0200581int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
582 enum gprs_llc_u_cmd u_cmd, int pf_bit)
583{
584 uint8_t *fcs, *llch;
585 uint8_t addr, ctrl;
586 uint32_t fcs_calc;
587
588 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
589
590 /* Address Field */
591 addr = sapi & 0xf;
592 if (command)
593 addr |= 0x40;
594
595 /* 6.3 Figure 8 */
596 ctrl = 0xe0 | u_cmd;
597 if (pf_bit)
598 ctrl |= 0x10;
599
600 /* prepend LLC UI header */
601 llch = msgb_push(msg, 2);
602 llch[0] = addr;
603 llch[1] = ctrl;
604
605 /* append FCS to end of frame */
606 fcs = msgb_put(msg, 3);
607 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
608 fcs[0] = fcs_calc & 0xff;
609 fcs[1] = (fcs_calc >> 8) & 0xff;
610 fcs[2] = (fcs_calc >> 16) & 0xff;
611
612 /* Identifiers passed down: (BVCI, NSEI) */
613
Alexander Couzens4e699a92016-07-05 11:04:27 +0200614 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_PACKETS]);
615 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_BYTES], msg->len);
616
Harald Welte1ae09c72010-05-13 19:22:55 +0200617 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800618 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200619}
620
621/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200622static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
623 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200624{
625 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200626 msgb_tlli(msg) = lle->llme->tlli;
627 msgb_bvci(msg) = lle->llme->bvci;
628 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200629
Harald Welte0c1a3032011-10-16 18:49:05 +0200630 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200631}
632
Max1de15912016-07-11 12:42:12 +0200633/* encrypt information field + FCS, if needed! */
634static int apply_gea(struct gprs_llc_lle *lle, uint16_t crypt_len, uint16_t nu,
635 uint32_t oc, uint8_t sapi, uint8_t *fcs, uint8_t *data)
636{
637 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
638
639 if (lle->llme->algo == GPRS_ALGO_GEA0)
640 return -EINVAL;
641
642 /* Compute the 'Input' Paraemeter */
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200643 uint32_t fcs_calc, iv = gprs_cipher_gen_input_ui(lle->llme->iov_ui, sapi,
Max1de15912016-07-11 12:42:12 +0200644 nu, oc);
645 /* Compute gamma that we need to XOR with the data */
646 int r = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
647 lle->llme->kc, iv,
648 fcs ? GPRS_CIPH_SGSN2MS : GPRS_CIPH_MS2SGSN);
649 if (r < 0) {
650 LOGP(DLLC, LOGL_ERROR, "Error producing %s gamma for UI "
651 "frame: %d\n", get_value_string(gprs_cipher_names,
652 lle->llme->algo), r);
653 return -ENOMSG;
654 }
655
656 if (fcs) {
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200657 /* Mark frame as encrypted and update FCS */
658 data[2] |= 0x02;
659 fcs_calc = gprs_llc_fcs(data, fcs - data);
660 fcs[0] = fcs_calc & 0xff;
661 fcs[1] = (fcs_calc >> 8) & 0xff;
662 fcs[2] = (fcs_calc >> 16) & 0xff;
Max1de15912016-07-11 12:42:12 +0200663 data += 3;
664 }
665
666 /* XOR the cipher output with the data */
667 for (r = 0; r < crypt_len; r++)
668 *(data + r) ^= cipher_out[r];
669
670 return 0;
671}
672
Max82040102016-07-06 11:59:18 +0200673/* Transmit a UI frame over the given SAPI:
674 'encryptable' indicates whether particular message can be encrypted according
675 to 3GPP TS 24.008 § 4.7.1.2
676 */
Harald Welte56a01452010-05-31 22:12:30 +0200677int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
Max82040102016-07-06 11:59:18 +0200678 struct sgsn_mm_ctx *mmctx, bool encryptable)
Harald Welte9b455bf2010-03-14 15:45:01 +0800679{
Harald Weltee6afd602010-05-02 11:19:37 +0200680 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200681 uint8_t *fcs, *llch;
682 uint8_t addr, ctrl[2];
683 uint32_t fcs_calc;
684 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200685 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800686
Harald Weltee6afd602010-05-02 11:19:37 +0200687 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
688
689 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200690 lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi);
Harald Welte1d9d9442010-06-03 07:11:04 +0200691
692 if (msg->len > lle->params.n201_u) {
693 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
694 msg->len, lle->params.n201_u);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200695 msgb_free(msg);
Harald Welte1d9d9442010-06-03 07:11:04 +0200696 return -EFBIG;
697 }
698
Max5aa51962016-07-06 11:33:04 +0200699 gprs_llme_copy_key(mmctx, lle->llme);
700
Harald Weltee6afd602010-05-02 11:19:37 +0200701 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200702 lle->llme->bvci = msgb_bvci(msg);
703 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200704
Harald Welted07b4f92010-06-30 23:07:59 +0200705 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200706 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200707 oc = lle->oc_ui_send;
708 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200709 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200710 /* Increment Overflow Counter, if needed */
711 if ((lle->vu_send + 1) / 512)
712 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200713
Harald Welte9b455bf2010-03-14 15:45:01 +0800714 /* Address Field */
715 addr = sapi & 0xf;
716 if (command)
717 addr |= 0x40;
718
719 /* Control Field */
720 ctrl[0] = 0xc0;
721 ctrl[0] |= nu >> 6;
722 ctrl[1] = (nu << 2) & 0xfc;
723 ctrl[1] |= 0x01; /* Protected Mode */
724
725 /* prepend LLC UI header */
726 llch = msgb_push(msg, 3);
727 llch[0] = addr;
728 llch[1] = ctrl[0];
729 llch[2] = ctrl[1];
730
731 /* append FCS to end of frame */
732 fcs = msgb_put(msg, 3);
733 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
734 fcs[0] = fcs_calc & 0xff;
735 fcs[1] = (fcs_calc >> 8) & 0xff;
736 fcs[2] = (fcs_calc >> 16) & 0xff;
737
Max82040102016-07-06 11:59:18 +0200738 if (lle->llme->algo != GPRS_ALGO_GEA0 && encryptable) {
Max1de15912016-07-11 12:42:12 +0200739 int rc = apply_gea(lle, fcs - llch, nu, oc, sapi, fcs, llch);
Harald Welted07b4f92010-06-30 23:07:59 +0200740 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200741 msgb_free(msg);
Harald Welted07b4f92010-06-30 23:07:59 +0200742 return rc;
743 }
Harald Welted07b4f92010-06-30 23:07:59 +0200744 }
745
Harald Weltee6afd602010-05-02 11:19:37 +0200746 /* Identifiers passed down: (BVCI, NSEI) */
747
Harald Welte1ae09c72010-05-13 19:22:55 +0200748 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800749 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800750}
751
Harald Welte9b455bf2010-03-14 15:45:01 +0800752static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
753 struct gprs_llc_lle *lle)
754{
755 switch (gph->cmd) {
756 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
757 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200758 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800759 /* start re-establishment (8.7.1) */
760 }
Harald Welte807a5d82010-06-01 11:53:01 +0200761 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800762 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200763 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800764 /* FIXME: process data */
765 break;
766 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
767 /* FIXME: Send UA */
768 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200769 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800770 break;
771 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200772 if (lle->state == GPRS_LLES_LOCAL_EST)
773 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800774 break;
775 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200776 if (lle->state == GPRS_LLES_LOCAL_EST)
777 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800778 break;
779 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
780 break;
781 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200782 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800783 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200784 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400785 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
786 LOGP(DLLC, LOGL_NOTICE,
787 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800788 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200789 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200790
791 /* HACK: non-standard recovery handling. If remote LLE
792 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800793 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200794 * and 'learn' the new sequence number */
795 if (gph->seq_tx != lle->vu_recv_last) {
796 lle->vu_recv_last = gph->seq_tx;
797 lle->vu_recv_duplicates = 0;
798 } else {
799 lle->vu_recv_duplicates++;
800 if (lle->vu_recv_duplicates < 3)
801 return -EIO;
802 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
803 "N(U=%d) after receiving %u duplicates\n",
804 lle->llme ? lle->llme->tlli : -1,
805 gph->seq_tx, lle->vu_recv_duplicates);
806 }
Harald Welteebabdea2010-06-01 18:28:10 +0200807 }
808 /* Increment the sequence number that we expect in the next frame */
809 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200810 /* Increment Overflow Counter */
811 if ((gph->seq_tx + 1) / 512)
812 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200813 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200814 default:
815 LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd);
816 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800817 }
818
819 return 0;
820}
821
Harald Weltea2665542010-05-02 09:28:11 +0200822/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800823int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
824{
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200825 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800826 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200827 struct gprs_llc_lle *lle;
Max82040102016-07-06 11:59:18 +0200828 bool drop_cipherable = false;
Harald Weltea2665542010-05-02 09:28:11 +0200829 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800830
Harald Welte11d7c102010-05-02 11:54:55 +0200831 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
832
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800833 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800834 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800835 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200836 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200837 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200838 return rc;
839 }
840
Harald Welte807a5d82010-06-01 11:53:01 +0200841 switch (gprs_tlli_type(msgb_tlli(msg))) {
842 case TLLI_LOCAL:
843 case TLLI_FOREIGN:
844 case TLLI_RANDOM:
845 case TLLI_AUXILIARY:
846 break;
847 default:
848 LOGP(DLLC, LOGL_ERROR,
849 "Discarding frame with strange TLLI type\n");
850 break;
851 }
852
Harald Weltea2665542010-05-02 09:28:11 +0200853 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200854 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200855 if (!lle) {
856 switch (llhp.sapi) {
857 case GPRS_SAPI_SNDCP3:
858 case GPRS_SAPI_SNDCP5:
859 case GPRS_SAPI_SNDCP9:
860 case GPRS_SAPI_SNDCP11:
861 /* Ask an upper layer for help. */
Maxb997f842016-07-06 15:57:01 +0200862 return gsm0408_gprs_force_reattach_oldmsg(msg,
863 lle->llme);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200864 default:
865 break;
866 }
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200867 return 0;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200868 }
Harald Weltea2665542010-05-02 09:28:11 +0200869
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100870 /* reset age computation */
871 lle->llme->age_timestamp = GPRS_LLME_RESET_AGE;
872
Harald Welted07b4f92010-06-30 23:07:59 +0200873 /* decrypt information field + FCS, if needed! */
874 if (llhp.is_encrypted) {
Max1de15912016-07-11 12:42:12 +0200875 if (lle->llme->algo != GPRS_ALGO_GEA0) {
876 rc = apply_gea(lle, llhp.data_len + 3, llhp.seq_tx,
877 lle->oc_ui_recv, lle->sapi, NULL,
878 llhp.data);
879 if (rc < 0)
880 return rc;
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200881 llhp.fcs = *(llhp.data + llhp.data_len);
882 llhp.fcs |= *(llhp.data + llhp.data_len + 1) << 8;
883 llhp.fcs |= *(llhp.data + llhp.data_len + 2) << 16;
Max1de15912016-07-11 12:42:12 +0200884 } else {
Harald Welted07b4f92010-06-30 23:07:59 +0200885 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
886 "has no KC/Algo! Dropping.\n");
887 return 0;
888 }
Harald Welted07b4f92010-06-30 23:07:59 +0200889 } else {
Max82040102016-07-06 11:59:18 +0200890 if (lle->llme->algo != GPRS_ALGO_GEA0 &&
891 lle->llme->cksn != GSM_KEY_SEQ_INVAL)
892 drop_cipherable = true;
Harald Welted07b4f92010-06-30 23:07:59 +0200893 }
894
895 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200896 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200897 if (llhp.fcs != llhp.fcs_calc) {
898 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
899 return -EIO;
900 }
901
Harald Welte10997d02010-05-03 12:28:12 +0200902 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200903 lle->llme->bvci = msgb_bvci(msg);
904 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200905
Harald Welte1ae09c72010-05-13 19:22:55 +0200906 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800907 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200908 if (rc < 0)
909 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800910
Alexander Couzens4e699a92016-07-05 11:04:27 +0200911 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_PACKETS]);
912 rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_BYTES], msg->len);
913
Harald Welte1ae09c72010-05-13 19:22:55 +0200914 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte22df4ac2015-08-16 15:23:32 +0200915 if (llhp.cmd == GPRS_LLC_UI && llhp.data && llhp.data_len) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200916 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800917 switch (llhp.sapi) {
918 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200919 /* send LL_UNITDATA_IND to GMM */
Max82040102016-07-06 11:59:18 +0200920 rc = gsm0408_gprs_rcvmsg_gb(msg, lle->llme,
921 drop_cipherable);
Harald Weltea2665542010-05-02 09:28:11 +0200922 break;
Harald Weltea2665542010-05-02 09:28:11 +0200923 case GPRS_SAPI_SNDCP3:
924 case GPRS_SAPI_SNDCP5:
925 case GPRS_SAPI_SNDCP9:
926 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +0200927 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
928 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
929 break;
Harald Weltea2665542010-05-02 09:28:11 +0200930 case GPRS_SAPI_SMS:
931 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +0200932 case GPRS_SAPI_TOM2:
933 case GPRS_SAPI_TOM8:
934 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200935 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200936 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200937 rc = -EINVAL;
938 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800939 }
940 }
941
Harald Weltea2665542010-05-02 09:28:11 +0200942 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800943}
Harald Welte807a5d82010-06-01 11:53:01 +0200944
Max5aa51962016-07-06 11:33:04 +0200945/* Propagate crypto parameters MM -> LLME */
946void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme)
947{
948 if (!mm)
949 return;
950 if (mm->ciph_algo != GPRS_ALGO_GEA0) {
951 llme->algo = mm->ciph_algo;
952 if (llme->cksn != mm->auth_triplet.key_seq &&
953 mm->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
954 memcpy(llme->kc, mm->auth_triplet.vec.kc,
955 gprs_cipher_key_length(mm->ciph_algo));
956 llme->cksn = mm->auth_triplet.key_seq;
957 }
958 } else
959 llme->cksn = GSM_KEY_SEQ_INVAL;
960}
961
Harald Welte807a5d82010-06-01 11:53:01 +0200962/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
963int gprs_llgmm_assign(struct gprs_llc_llme *llme,
Max5aa51962016-07-06 11:33:04 +0200964 uint32_t old_tlli, uint32_t new_tlli)
Harald Welte807a5d82010-06-01 11:53:01 +0200965{
966 unsigned int i;
967
968 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
969 /* TLLI Assignment 8.3.1 */
970 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
971 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
972 * old is unassigned. Only TLLI new shall be accepted when
973 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +0200974 if (llme->old_tlli != 0xffffffff) {
975 llme->old_tlli = 0xffffffff;
976 llme->tlli = new_tlli;
977 } else {
978 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
979 * TLLI assignmemt according to 8.3.1 */
980 llme->old_tlli = 0xffffffff;
981 llme->tlli = new_tlli;
982 llme->state = GPRS_LLMS_ASSIGNED;
983 /* 8.5.3.1 For all LLE's */
984 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
985 struct gprs_llc_lle *l = &llme->lle[i];
986 l->vu_send = l->vu_recv = 0;
987 l->retrans_ctr = 0;
988 l->state = GPRS_LLES_ASSIGNED_ADM;
989 /* FIXME Set parameters according to table 9 */
990 }
Harald Welte807a5d82010-06-01 11:53:01 +0200991 }
992 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
993 /* TLLI Change 8.3.2 */
994 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +0200995 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freytheraa93bac2013-07-31 11:20:37 +0200996 llme->old_tlli = old_tlli;
Harald Welte807a5d82010-06-01 11:53:01 +0200997 llme->tlli = new_tlli;
998 llme->state = GPRS_LLMS_ASSIGNED;
999 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
1000 /* TLLI Unassignment 8.3.3) */
1001 llme->tlli = llme->old_tlli = 0;
1002 llme->state = GPRS_LLMS_UNASSIGNED;
1003 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1004 struct gprs_llc_lle *l = &llme->lle[i];
1005 l->state = GPRS_LLES_UNASSIGNED;
1006 }
Harald Weltef7fef482010-06-28 22:18:26 +02001007 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +02001008 } else
1009 return -EINVAL;
1010
1011 return 0;
1012}
Harald Welte496aee42010-06-30 19:59:55 +02001013
Max39550252016-06-28 17:39:20 +02001014/* TLLI unassignment */
1015int gprs_llgmm_unassign(struct gprs_llc_llme *llme)
1016{
Max5aa51962016-07-06 11:33:04 +02001017 return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff);
Max39550252016-06-28 17:39:20 +02001018}
1019
Harald Welte0c1a3032011-10-16 18:49:05 +02001020/* Chapter 7.2.1.2 LLGMM-RESET.req */
1021int gprs_llgmm_reset(struct gprs_llc_llme *llme)
1022{
1023 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001024 struct gprs_llc_lle *lle = &llme->lle[1];
Philipp4ac3aee2016-08-10 12:24:09 +02001025 uint8_t xid_bytes[1024];
1026 int xid_bytes_len;
1027 uint8_t *xid;
Harald Welte0c1a3032011-10-16 18:49:05 +02001028
Philipp4ac3aee2016-08-10 12:24:09 +02001029 LOGP(DLLC, LOGL_NOTICE, "LLGM Reset\n");
Maxb997f842016-07-06 15:57:01 +02001030 if (RAND_bytes((uint8_t *) &llme->iov_ui, 4) != 1) {
1031 LOGP(DLLC, LOGL_NOTICE, "RAND_bytes failed for LLC XID reset, "
1032 "falling back to rand()\n");
1033 llme->iov_ui = rand();
1034 }
1035
Philipp4ac3aee2016-08-10 12:24:09 +02001036 /* Generate XID message */
1037 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes,
1038 sizeof(xid_bytes),llme->iov_ui,llme);
1039 if(xid_bytes_len < 0)
1040 return -EINVAL;
1041 xid = msgb_put(msg, xid_bytes_len);
1042 memcpy(xid, xid_bytes, xid_bytes_len);
Harald Welte0c1a3032011-10-16 18:49:05 +02001043
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001044 /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */
1045 lle->vu_recv = 0;
1046 lle->vu_send = 0;
1047 lle->oc_ui_send = 0;
1048 lle->oc_ui_recv = 0;
1049
Harald Welte0c1a3032011-10-16 18:49:05 +02001050 /* FIXME: Start T200, wait for XID response */
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001051 return gprs_llc_tx_xid(lle, msg, 1);
Harald Welte0c1a3032011-10-16 18:49:05 +02001052}
1053
Maxb997f842016-07-06 15:57:01 +02001054int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi,
1055 struct gprs_llc_llme *llme)
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001056{
1057 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Philipp4ac3aee2016-08-10 12:24:09 +02001058 uint8_t xid_bytes[1024];
1059 int xid_bytes_len;
1060 uint8_t *xid;
Maxb997f842016-07-06 15:57:01 +02001061
Philipp4ac3aee2016-08-10 12:24:09 +02001062 LOGP(DLLC, LOGL_NOTICE, "LLGM Reset\n");
Maxb997f842016-07-06 15:57:01 +02001063 if (RAND_bytes((uint8_t *) &llme->iov_ui, 4) != 1) {
1064 LOGP(DLLC, LOGL_NOTICE, "RAND_bytes failed for LLC XID reset, "
1065 "falling back to rand()\n");
1066 llme->iov_ui = rand();
1067 }
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001068
Philipp4ac3aee2016-08-10 12:24:09 +02001069 /* Generate XID message */
1070 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes,
1071 sizeof(xid_bytes),llme->iov_ui,llme);
1072 if(xid_bytes_len < 0)
1073 return -EINVAL;
1074 xid = msgb_put(msg, xid_bytes_len);
1075 memcpy(xid, xid_bytes, xid_bytes_len);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001076
1077 /* FIXME: Start T200, wait for XID response */
1078
1079 msgb_tlli(msg) = msgb_tlli(oldmsg);
1080 msgb_bvci(msg) = msgb_bvci(oldmsg);
1081 msgb_nsei(msg) = msgb_nsei(oldmsg);
1082
1083 return gprs_llc_tx_u(msg, sapi, 1, GPRS_LLC_U_XID, 1);
1084}
1085
Harald Welte496aee42010-06-30 19:59:55 +02001086int gprs_llc_init(const char *cipher_plugin_path)
1087{
1088 return gprs_cipher_load(cipher_plugin_path);
1089}