blob: bc856e98e4d078be2d5128dc8c3a483ac3ea42a3 [file] [log] [blame]
Harald Welte75bb8202010-03-14 15:45:01 +08001/* GPRS LLC protocol implementation as per 3GPP TS 04.64 */
2
Harald Weltec14e5822010-05-02 09:28:11 +02003/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte75bb8202010-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 Welte0e3e88e2011-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 Welte75bb8202010-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 Welte0e3e88e2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte75bb8202010-03-14 15:45:01 +080016 *
Harald Welte0e3e88e2011-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 Welte75bb8202010-03-14 15:45:01 +080019 *
20 */
21
22#include <errno.h>
Harald Welted85d9a92010-05-02 11:26:34 +020023#include <stdint.h>
Harald Welte75bb8202010-03-14 15:45:01 +080024
Max6d999582016-07-06 15:57:01 +020025#include <openssl/rand.h>
26
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010027#include <osmocom/core/msgb.h>
28#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/timer.h>
30#include <osmocom/core/talloc.h>
Harald Weltecfb6b282012-06-16 14:59:56 +080031#include <osmocom/gprs/gprs_bssgp.h>
Harald Weltec14e5822010-05-02 09:28:11 +020032
33#include <openbsc/gsm_data.h>
34#include <openbsc/debug.h>
Harald Welte75871c72010-06-01 11:53:01 +020035#include <openbsc/gprs_sgsn.h>
36#include <openbsc/gprs_gmm.h>
Harald Welte75bb8202010-03-14 15:45:01 +080037#include <openbsc/gprs_llc.h>
38#include <openbsc/crc24.h>
Holger Hans Peter Freytherf9839f62011-10-14 23:42:13 +020039#include <openbsc/sgsn.h>
Harald Welte75bb8202010-03-14 15:45:01 +080040
Holger Hans Peter Freyther1a3fff22013-07-30 09:29:27 +020041static struct gprs_llc_llme *llme_alloc(uint32_t tlli);
42
Harald Welte1e8b9462012-06-17 09:31:16 +080043/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
44 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
45static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
46{
47 struct bssgp_dl_ud_par dup;
48 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
49
Harald Welte26096012012-07-04 21:53:12 +020050 memset(&dup, 0, sizeof(dup));
51 /* before we have received some identity from the MS, we might
52 * not yet have a MMC context (e.g. XID negotiation of primarly
53 * LLC connection fro GMM sapi). */
54 if (mmctx) {
55 dup.imsi = mmctx->imsi;
56 dup.drx_parms = mmctx->drx_parms;
57 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
58 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
Holger Hans Peter Freyther374c7f92013-07-29 10:09:12 +020059
60 /* make sure we only send it to the right llme */
Harald Weltec28dc2f2015-12-25 19:12:21 +010061 OSMO_ASSERT(msgb_tlli(msg) == mmctx->gb.llme->tlli
62 || msgb_tlli(msg) == mmctx->gb.llme->old_tlli);
Harald Welte26096012012-07-04 21:53:12 +020063 }
Harald Welte1e8b9462012-06-17 09:31:16 +080064 memcpy(&dup.qos_profile, qos_profile_default,
65 sizeof(qos_profile_default));
66
Harald Weltea084f122012-06-17 13:04:02 +080067 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Welte1e8b9462012-06-17 09:31:16 +080068}
69
70
Harald Weltedf270ad2010-06-03 07:11:04 +020071/* Section 8.9.9 LLC layer parameter default values */
Daniel Willmann922303c2014-06-27 17:05:48 +020072static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = {
Harald Weltedf270ad2010-06-03 07:11:04 +020073 [1] = {
74 .t200_201 = 5,
75 .n200 = 3,
76 .n201_u = 400,
77 },
78 [2] = {
79 .t200_201 = 5,
80 .n200 = 3,
81 .n201_u = 270,
82 },
83 [3] = {
84 .iov_i_exp = 27,
85 .t200_201 = 5,
86 .n200 = 3,
87 .n201_u = 500,
88 .n201_i = 1503,
89 .mD = 1520,
90 .mU = 1520,
91 .kD = 16,
92 .kU = 16,
93 },
94 [5] = {
95 .iov_i_exp = 27,
96 .t200_201 = 10,
97 .n200 = 3,
98 .n201_u = 500,
99 .n201_i = 1503,
100 .mD = 760,
101 .mU = 760,
102 .kD = 8,
103 .kU = 8,
104 },
105 [7] = {
106 .t200_201 = 20,
107 .n200 = 3,
108 .n201_u = 270,
109 },
110 [8] = {
111 .t200_201 = 20,
112 .n200 = 3,
113 .n201_u = 270,
114 },
115 [9] = {
116 .iov_i_exp = 27,
117 .t200_201 = 20,
118 .n200 = 3,
119 .n201_u = 500,
120 .n201_i = 1503,
121 .mD = 380,
122 .mU = 380,
123 .kD = 4,
124 .kU = 4,
125 },
126 [11] = {
127 .iov_i_exp = 27,
128 .t200_201 = 40,
129 .n200 = 3,
130 .n201_u = 500,
131 .n201_i = 1503,
132 .mD = 190,
133 .mU = 190,
134 .kD = 2,
135 .kU = 2,
136 },
137};
138
Harald Welte75871c72010-06-01 11:53:01 +0200139LLIST_HEAD(gprs_llc_llmes);
Harald Weltec14e5822010-05-02 09:28:11 +0200140void *llc_tall_ctx;
141
142/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freytherfe5a6e82013-07-29 09:06:46 +0200143static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltec14e5822010-05-02 09:28:11 +0200144{
Harald Welte75871c72010-06-01 11:53:01 +0200145 struct gprs_llc_llme *llme;
Harald Weltec14e5822010-05-02 09:28:11 +0200146
Harald Welte75871c72010-06-01 11:53:01 +0200147 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
148 if (llme->tlli == tlli || llme->old_tlli == tlli)
149 return &llme->lle[sapi];
Harald Weltec14e5822010-05-02 09:28:11 +0200150 }
151 return NULL;
152}
153
Holger Hans Peter Freyther49dbcd92014-10-02 21:27:24 +0200154struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi)
155{
156 struct gprs_llc_llme *llme;
157 struct gprs_llc_lle *lle;
158
159 lle = lle_by_tlli_sapi(tlli, sapi);
160 if (lle)
161 return lle;
162
Holger Hans Peter Freyther49dbcd92014-10-02 21:27:24 +0200163 LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, "
164 "creating LLME on the fly\n", tlli);
165 llme = llme_alloc(tlli);
166 lle = &llme->lle[sapi];
167 return lle;
168}
169
170struct llist_head *gprs_llme_list(void)
171{
172 return &gprs_llc_llmes;
173}
174
Holger Hans Peter Freyther1a3fff22013-07-30 09:29:27 +0200175/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
176static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
177 uint8_t sapi, enum gprs_llc_cmd cmd)
178{
179 struct gprs_llc_lle *lle;
180
181 /* We already know about this TLLI */
182 lle = lle_by_tlli_sapi(tlli, sapi);
183 if (lle)
184 return lle;
185
186 /* Maybe it is a routing area update but we already know this sapi? */
187 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
Jacob Erlbeckb0fbb842016-01-04 18:43:32 +0100188 lle = lle_by_tlli_sapi(tlli, sapi);
Holger Hans Peter Freyther1a3fff22013-07-30 09:29:27 +0200189 if (lle) {
190 LOGP(DLLC, LOGL_NOTICE,
191 "LLC RX: Found a local entry for TLLI 0x%08x\n",
192 tlli);
193 return lle;
194 }
195 }
196
197 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
198 * except UID and XID frames with SAPI=1 */
199 if (sapi == GPRS_SAPI_GMM &&
200 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
201 struct gprs_llc_llme *llme;
202 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
203 llme = llme_alloc(tlli);
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200204 LOGP(DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, "
Holger Hans Peter Freyther1a3fff22013-07-30 09:29:27 +0200205 "creating LLME on the fly\n", tlli);
206 lle = &llme->lle[sapi];
207 return lle;
208 }
209
210 LOGP(DLLC, LOGL_NOTICE,
211 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
212 tlli, sapi);
213 return NULL;
214}
215
Harald Weltedf270ad2010-06-03 07:11:04 +0200216static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltec14e5822010-05-02 09:28:11 +0200217{
Harald Welte75871c72010-06-01 11:53:01 +0200218 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltec14e5822010-05-02 09:28:11 +0200219
Harald Welte75871c72010-06-01 11:53:01 +0200220 lle->llme = llme;
221 lle->sapi = sapi;
222 lle->state = GPRS_LLES_UNASSIGNED;
223
Harald Weltedf270ad2010-06-03 07:11:04 +0200224 /* Initialize according to parameters */
225 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte75871c72010-06-01 11:53:01 +0200226}
227
228static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
229{
230 struct gprs_llc_llme *llme;
231 uint32_t i;
232
233 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
234 if (!llme)
Harald Weltec14e5822010-05-02 09:28:11 +0200235 return NULL;
236
Harald Welte75871c72010-06-01 11:53:01 +0200237 llme->tlli = tlli;
Harald Welteb3d84a42010-07-01 11:54:31 +0200238 llme->old_tlli = 0xffffffff;
Harald Welte75871c72010-06-01 11:53:01 +0200239 llme->state = GPRS_LLMS_UNASSIGNED;
Jacob Erlbecke6140d02015-01-23 11:33:51 +0100240 llme->age_timestamp = GPRS_LLME_RESET_AGE;
Maxeb64e3f2016-07-06 11:33:04 +0200241 llme->cksn = GSM_KEY_SEQ_INVAL;
Harald Weltec14e5822010-05-02 09:28:11 +0200242
Harald Welte75871c72010-06-01 11:53:01 +0200243 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
244 lle_init(llme, i);
245
246 llist_add(&llme->list, &gprs_llc_llmes);
247
248 return llme;
Harald Weltec14e5822010-05-02 09:28:11 +0200249}
250
Harald Welte7a62a3a2010-06-28 22:18:26 +0200251static void llme_free(struct gprs_llc_llme *llme)
252{
253 llist_del(&llme->list);
254 talloc_free(llme);
255}
256
Holger Hans Peter Freyther19bb4742014-04-04 12:47:32 +0200257#if 0
258/* FIXME: Unused code... */
Harald Welte75bb8202010-03-14 15:45:01 +0800259static void t200_expired(void *data)
260{
261 struct gprs_llc_lle *lle = data;
262
263 /* 8.5.1.3: Expiry of T200 */
264
Harald Weltedf270ad2010-06-03 07:11:04 +0200265 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte75bb8202010-03-14 15:45:01 +0800266 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte75871c72010-06-01 11:53:01 +0200267 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte75bb8202010-03-14 15:45:01 +0800268 }
269
270 switch (lle->state) {
Harald Welte75871c72010-06-01 11:53:01 +0200271 case GPRS_LLES_LOCAL_EST:
Harald Weltefebc4262010-05-13 19:22:55 +0200272 /* FIXME: retransmit SABM */
273 /* FIXME: re-start T200 */
Harald Welte75bb8202010-03-14 15:45:01 +0800274 lle->retrans_ctr++;
275 break;
Harald Welte75871c72010-06-01 11:53:01 +0200276 case GPRS_LLES_LOCAL_REL:
Harald Weltefebc4262010-05-13 19:22:55 +0200277 /* FIXME: retransmit DISC */
278 /* FIXME: re-start T200 */
Harald Welte75bb8202010-03-14 15:45:01 +0800279 lle->retrans_ctr++;
280 break;
Holger Hans Peter Freyther19bb4742014-04-04 12:47:32 +0200281 default:
282 LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state);
283 break;
Harald Welte75bb8202010-03-14 15:45:01 +0800284 }
285
286}
287
288static void t201_expired(void *data)
289{
290 struct gprs_llc_lle *lle = data;
291
Harald Weltedf270ad2010-06-03 07:11:04 +0200292 if (lle->retrans_ctr < lle->params.n200) {
Harald Weltefebc4262010-05-13 19:22:55 +0200293 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
294 /* FIXME: set timer T201 */
Harald Welte75bb8202010-03-14 15:45:01 +0800295 lle->retrans_ctr++;
296 }
297}
Holger Hans Peter Freyther19bb4742014-04-04 12:47:32 +0200298#endif
Harald Welte75bb8202010-03-14 15:45:01 +0800299
Harald Welte439fa722010-05-03 12:28:12 +0200300int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
301 enum gprs_llc_u_cmd u_cmd, int pf_bit)
302{
303 uint8_t *fcs, *llch;
304 uint8_t addr, ctrl;
305 uint32_t fcs_calc;
306
307 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
308
309 /* Address Field */
310 addr = sapi & 0xf;
311 if (command)
312 addr |= 0x40;
313
314 /* 6.3 Figure 8 */
315 ctrl = 0xe0 | u_cmd;
316 if (pf_bit)
317 ctrl |= 0x10;
318
319 /* prepend LLC UI header */
320 llch = msgb_push(msg, 2);
321 llch[0] = addr;
322 llch[1] = ctrl;
323
324 /* append FCS to end of frame */
325 fcs = msgb_put(msg, 3);
326 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
327 fcs[0] = fcs_calc & 0xff;
328 fcs[1] = (fcs_calc >> 8) & 0xff;
329 fcs[2] = (fcs_calc >> 16) & 0xff;
330
331 /* Identifiers passed down: (BVCI, NSEI) */
332
Harald Weltefebc4262010-05-13 19:22:55 +0200333 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltea62e9fe2012-06-17 12:16:31 +0800334 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte439fa722010-05-03 12:28:12 +0200335}
336
337/* Send XID response to LLE */
Harald Welteac687be2011-10-16 18:49:05 +0200338static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
339 int command)
Harald Welte439fa722010-05-03 12:28:12 +0200340{
341 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte75871c72010-06-01 11:53:01 +0200342 msgb_tlli(msg) = lle->llme->tlli;
343 msgb_bvci(msg) = lle->llme->bvci;
344 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte439fa722010-05-03 12:28:12 +0200345
Harald Welteac687be2011-10-16 18:49:05 +0200346 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte439fa722010-05-03 12:28:12 +0200347}
348
Harald Welte75bb8202010-03-14 15:45:01 +0800349/* Transmit a UI frame over the given SAPI */
Harald Weltecca80c92010-05-31 22:12:30 +0200350int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
351 void *mmctx)
Harald Welte75bb8202010-03-14 15:45:01 +0800352{
Harald Weltea6e32702010-05-02 11:19:37 +0200353 struct gprs_llc_lle *lle;
Harald Welted85d9a92010-05-02 11:26:34 +0200354 uint8_t *fcs, *llch;
355 uint8_t addr, ctrl[2];
356 uint32_t fcs_calc;
357 uint16_t nu = 0;
Harald Welte222b0682010-06-30 23:07:59 +0200358 uint32_t oc;
Harald Welte75bb8202010-03-14 15:45:01 +0800359
Harald Weltea6e32702010-05-02 11:19:37 +0200360 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
361
362 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
Holger Hans Peter Freyther49dbcd92014-10-02 21:27:24 +0200363 lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi);
Harald Weltedf270ad2010-06-03 07:11:04 +0200364
365 if (msg->len > lle->params.n201_u) {
366 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
367 msg->len, lle->params.n201_u);
Holger Hans Peter Freyther13293f32014-10-10 17:35:54 +0200368 msgb_free(msg);
Harald Weltedf270ad2010-06-03 07:11:04 +0200369 return -EFBIG;
370 }
371
Maxeb64e3f2016-07-06 11:33:04 +0200372 gprs_llme_copy_key(mmctx, lle->llme);
373
Harald Weltea6e32702010-05-02 11:19:37 +0200374 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte75871c72010-06-01 11:53:01 +0200375 lle->llme->bvci = msgb_bvci(msg);
376 lle->llme->nsei = msgb_nsei(msg);
Harald Weltea6e32702010-05-02 11:19:37 +0200377
Harald Welte222b0682010-06-30 23:07:59 +0200378 /* Obtain current values for N(u) and OC */
Harald Welte02343f92010-05-30 21:51:58 +0200379 nu = lle->vu_send;
Harald Welte222b0682010-06-30 23:07:59 +0200380 oc = lle->oc_ui_send;
381 /* Increment V(U) */
Harald Welte02343f92010-05-30 21:51:58 +0200382 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welte222b0682010-06-30 23:07:59 +0200383 /* Increment Overflow Counter, if needed */
384 if ((lle->vu_send + 1) / 512)
385 lle->oc_ui_send += 512;
Harald Welte02343f92010-05-30 21:51:58 +0200386
Harald Welte75bb8202010-03-14 15:45:01 +0800387 /* Address Field */
388 addr = sapi & 0xf;
389 if (command)
390 addr |= 0x40;
391
392 /* Control Field */
393 ctrl[0] = 0xc0;
394 ctrl[0] |= nu >> 6;
395 ctrl[1] = (nu << 2) & 0xfc;
396 ctrl[1] |= 0x01; /* Protected Mode */
397
398 /* prepend LLC UI header */
399 llch = msgb_push(msg, 3);
400 llch[0] = addr;
401 llch[1] = ctrl[0];
402 llch[2] = ctrl[1];
403
404 /* append FCS to end of frame */
405 fcs = msgb_put(msg, 3);
406 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
407 fcs[0] = fcs_calc & 0xff;
408 fcs[1] = (fcs_calc >> 8) & 0xff;
409 fcs[2] = (fcs_calc >> 16) & 0xff;
410
Harald Welte222b0682010-06-30 23:07:59 +0200411 /* encrypt information field + FCS, if needed! */
412 if (lle->llme->algo != GPRS_ALGO_GEA0) {
413 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
414 uint16_t crypt_len = (fcs + 3) - (llch + 3);
415 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
416 uint32_t iv;
417 int rc, i;
Alexander Couzens23134422016-05-31 17:37:54 +0200418 uint8_t *kc = lle->llme->kc;
Harald Welte222b0682010-06-30 23:07:59 +0200419
420 /* Compute the 'Input' Paraemeter */
421 iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc);
422
423 /* Compute the keystream that we need to XOR with the data */
424 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
425 kc, iv, GPRS_CIPH_SGSN2MS);
426 if (rc < 0) {
427 LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc);
Holger Hans Peter Freyther13293f32014-10-10 17:35:54 +0200428 msgb_free(msg);
Harald Welte222b0682010-06-30 23:07:59 +0200429 return rc;
430 }
431
432 /* XOR the cipher output with the information field + FCS */
433 for (i = 0; i < crypt_len; i++)
434 *(llch + 3 + i) ^= cipher_out[i];
435
436 /* Mark frame as encrypted */
437 ctrl[1] |= 0x02;
438 }
439
Harald Weltea6e32702010-05-02 11:19:37 +0200440 /* Identifiers passed down: (BVCI, NSEI) */
441
Harald Weltefebc4262010-05-13 19:22:55 +0200442 /* Send BSSGP-DL-UNITDATA.req */
Harald Welte1e8b9462012-06-17 09:31:16 +0800443 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte75bb8202010-03-14 15:45:01 +0800444}
445
Harald Welteac687be2011-10-16 18:49:05 +0200446/* According to 6.4.1.6 / Figure 11 */
447static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data)
448{
449 uint8_t header_len = 1;
450 uint8_t *cur;
451
452 /* type is a 5-bit field... */
453 if (type > 0x1f)
454 return -EINVAL;
455
456 if (length > 3)
457 header_len = 2;
458
459 cur = msgb_put(msg, length + header_len);
460
461 /* build the header without or with XL bit */
462 if (length <= 3) {
463 *cur++ = (type << 2) | (length & 3);
464 } else {
465 *cur++ = 0x80 | (type << 2) | (length >> 6);
466 *cur++ = (length << 2);
467 }
468
469 /* copy over the payload of the parameter*/
470 memcpy(cur, data, length);
471
472 return length + header_len;
473}
474
475static void rx_llc_xid(struct gprs_llc_lle *lle,
476 struct gprs_llc_hdr_parsed *gph)
477{
478 /* FIXME: 8.5.3.3: check if XID is invalid */
479 if (gph->is_cmd) {
480 /* FIXME: implement XID negotiation using SNDCP */
481 struct msgb *resp;
482 uint8_t *xid;
483 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
484 xid = msgb_put(resp, gph->data_len);
485 memcpy(xid, gph->data, gph->data_len);
486 gprs_llc_tx_xid(lle, resp, 0);
487 } else {
488 /* FIXME: if we had sent a XID reset, send
489 * LLGMM-RESET.conf to GMM */
490 /* FIXME: implement XID negotiation using SNDCP */
491 }
492}
493
Harald Welte75bb8202010-03-14 15:45:01 +0800494static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
495 struct gprs_llc_lle *lle)
496{
497 switch (gph->cmd) {
498 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
499 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte75871c72010-06-01 11:53:01 +0200500 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte75bb8202010-03-14 15:45:01 +0800501 /* start re-establishment (8.7.1) */
502 }
Harald Welte75871c72010-06-01 11:53:01 +0200503 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte75bb8202010-03-14 15:45:01 +0800504 /* FIXME: Send UA */
Harald Welte75871c72010-06-01 11:53:01 +0200505 lle->state = GPRS_LLES_ABM;
Harald Welte75bb8202010-03-14 15:45:01 +0800506 /* FIXME: process data */
507 break;
508 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
509 /* FIXME: Send UA */
510 /* terminate ABM */
Harald Welte75871c72010-06-01 11:53:01 +0200511 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte75bb8202010-03-14 15:45:01 +0800512 break;
513 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte75871c72010-06-01 11:53:01 +0200514 if (lle->state == GPRS_LLES_LOCAL_EST)
515 lle->state = GPRS_LLES_ABM;
Harald Welte75bb8202010-03-14 15:45:01 +0800516 break;
517 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte75871c72010-06-01 11:53:01 +0200518 if (lle->state == GPRS_LLES_LOCAL_EST)
519 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte75bb8202010-03-14 15:45:01 +0800520 break;
521 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
522 break;
523 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welteac687be2011-10-16 18:49:05 +0200524 rx_llc_xid(lle, gph);
Harald Welte75bb8202010-03-14 15:45:01 +0800525 break;
Harald Welted0e36dc2010-06-01 18:28:10 +0200526 case GPRS_LLC_UI:
Holger Hans Peter Freyther6721a082011-06-23 17:53:27 -0400527 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
528 LOGP(DLLC, LOGL_NOTICE,
529 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther1b5b66c2010-06-23 09:48:25 +0800530 lle->llme ? lle->llme->tlli : -1,
Harald Welted0e36dc2010-06-01 18:28:10 +0200531 gph->seq_tx, lle->vu_recv);
Harald Welte0273fab2013-06-21 14:06:18 +0200532
533 /* HACK: non-standard recovery handling. If remote LLE
534 * is re-transmitting the same sequence number for
Harald Welted4b9b022013-07-21 17:41:46 +0800535 * three times, don't discard the frame but pass it on
Harald Welte0273fab2013-06-21 14:06:18 +0200536 * and 'learn' the new sequence number */
537 if (gph->seq_tx != lle->vu_recv_last) {
538 lle->vu_recv_last = gph->seq_tx;
539 lle->vu_recv_duplicates = 0;
540 } else {
541 lle->vu_recv_duplicates++;
542 if (lle->vu_recv_duplicates < 3)
543 return -EIO;
544 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
545 "N(U=%d) after receiving %u duplicates\n",
546 lle->llme ? lle->llme->tlli : -1,
547 gph->seq_tx, lle->vu_recv_duplicates);
548 }
Harald Welted0e36dc2010-06-01 18:28:10 +0200549 }
550 /* Increment the sequence number that we expect in the next frame */
551 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welte222b0682010-06-30 23:07:59 +0200552 /* Increment Overflow Counter */
553 if ((gph->seq_tx + 1) / 512)
554 lle->oc_ui_recv += 512;
Harald Welted0e36dc2010-06-01 18:28:10 +0200555 break;
Holger Hans Peter Freyther19bb4742014-04-04 12:47:32 +0200556 default:
557 LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd);
558 break;
Harald Welte75bb8202010-03-14 15:45:01 +0800559 }
560
561 return 0;
562}
563
Harald Weltec14e5822010-05-02 09:28:11 +0200564/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte75bb8202010-03-14 15:45:01 +0800565int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
566{
Holger Hans Peter Freytherf9839f62011-10-14 23:42:13 +0200567 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800568 struct gprs_llc_hdr_parsed llhp;
Harald Welte439fa722010-05-03 12:28:12 +0200569 struct gprs_llc_lle *lle;
Harald Weltec14e5822010-05-02 09:28:11 +0200570 int rc = 0;
Harald Welte75bb8202010-03-14 15:45:01 +0800571
Harald Welte2351f1f2010-05-02 11:54:55 +0200572 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
573
Holger Hans Peter Freyther510ae722010-05-23 21:33:57 +0800574 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freyther6f261512010-05-23 21:43:57 +0800575 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte75bb8202010-03-14 15:45:01 +0800576 gprs_llc_hdr_dump(&llhp);
Harald Weltefebc4262010-05-13 19:22:55 +0200577 if (rc < 0) {
Harald Weltee3497362010-05-13 19:49:06 +0200578 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Weltefebc4262010-05-13 19:22:55 +0200579 return rc;
580 }
581
Harald Welte75871c72010-06-01 11:53:01 +0200582 switch (gprs_tlli_type(msgb_tlli(msg))) {
583 case TLLI_LOCAL:
584 case TLLI_FOREIGN:
585 case TLLI_RANDOM:
586 case TLLI_AUXILIARY:
587 break;
588 default:
589 LOGP(DLLC, LOGL_ERROR,
590 "Discarding frame with strange TLLI type\n");
591 break;
592 }
593
Harald Weltec14e5822010-05-02 09:28:11 +0200594 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther1a3fff22013-07-30 09:29:27 +0200595 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200596 if (!lle) {
597 switch (llhp.sapi) {
598 case GPRS_SAPI_SNDCP3:
599 case GPRS_SAPI_SNDCP5:
600 case GPRS_SAPI_SNDCP9:
601 case GPRS_SAPI_SNDCP11:
602 /* Ask an upper layer for help. */
Max6d999582016-07-06 15:57:01 +0200603 return gsm0408_gprs_force_reattach_oldmsg(msg,
604 lle->llme);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200605 default:
606 break;
607 }
Holger Hans Peter Freyther1a3fff22013-07-30 09:29:27 +0200608 return 0;
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200609 }
Harald Weltec14e5822010-05-02 09:28:11 +0200610
Jacob Erlbecke6140d02015-01-23 11:33:51 +0100611 /* reset age computation */
612 lle->llme->age_timestamp = GPRS_LLME_RESET_AGE;
613
Harald Welte222b0682010-06-30 23:07:59 +0200614 /* decrypt information field + FCS, if needed! */
615 if (llhp.is_encrypted) {
616 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
617 uint16_t crypt_len = llhp.data_len + 3;
618 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
619 uint32_t iv;
Alexander Couzens23134422016-05-31 17:37:54 +0200620 uint8_t *kc = lle->llme->kc;
Harald Welte222b0682010-06-30 23:07:59 +0200621 int rc, i;
622
623 if (lle->llme->algo == GPRS_ALGO_GEA0) {
624 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
625 "has no KC/Algo! Dropping.\n");
626 return 0;
627 }
628
629 iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx,
630 lle->oc_ui_recv);
631 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
632 kc, iv, GPRS_CIPH_MS2SGSN);
633 if (rc < 0) {
634 LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n",
635 rc);
636 return rc;
637 }
638
639 /* XOR the cipher output with the information field + FCS */
640 for (i = 0; i < crypt_len; i++)
641 *(llhp.data + i) ^= cipher_out[i];
642 } else {
643 if (lle->llme->algo != GPRS_ALGO_GEA0) {
644 LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC "
645 "that is supposed to be encrypted. Dropping.\n");
646 return 0;
647 }
648 }
649
650 /* We have to do the FCS check _after_ decryption */
Harald Weltefa325072010-06-30 23:15:57 +0200651 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welte222b0682010-06-30 23:07:59 +0200652 if (llhp.fcs != llhp.fcs_calc) {
653 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
654 return -EIO;
655 }
656
Harald Welte439fa722010-05-03 12:28:12 +0200657 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte75871c72010-06-01 11:53:01 +0200658 lle->llme->bvci = msgb_bvci(msg);
659 lle->llme->nsei = msgb_nsei(msg);
Harald Welte439fa722010-05-03 12:28:12 +0200660
Harald Weltefebc4262010-05-13 19:22:55 +0200661 /* Receive and Process the actual LLC frame */
Harald Welte75bb8202010-03-14 15:45:01 +0800662 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Weltefebc4262010-05-13 19:22:55 +0200663 if (rc < 0)
664 return rc;
Harald Welte75bb8202010-03-14 15:45:01 +0800665
Harald Weltefebc4262010-05-13 19:22:55 +0200666 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Weltee4235962015-08-16 15:23:32 +0200667 if (llhp.cmd == GPRS_LLC_UI && llhp.data && llhp.data_len) {
Harald Weltecf3c4142010-04-30 16:33:12 +0200668 msgb_gmmh(msg) = llhp.data;
Harald Welte75bb8202010-03-14 15:45:01 +0800669 switch (llhp.sapi) {
670 case GPRS_SAPI_GMM:
Harald Weltefebc4262010-05-13 19:22:55 +0200671 /* send LL_UNITDATA_IND to GMM */
Harald Weltebeaf11d2015-12-25 19:14:57 +0100672 rc = gsm0408_gprs_rcvmsg_gb(msg, lle->llme);
Harald Weltec14e5822010-05-02 09:28:11 +0200673 break;
Harald Weltec14e5822010-05-02 09:28:11 +0200674 case GPRS_SAPI_SNDCP3:
675 case GPRS_SAPI_SNDCP5:
676 case GPRS_SAPI_SNDCP9:
677 case GPRS_SAPI_SNDCP11:
Harald Welted0e36dc2010-06-01 18:28:10 +0200678 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
679 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
680 break;
Harald Weltec14e5822010-05-02 09:28:11 +0200681 case GPRS_SAPI_SMS:
682 /* FIXME */
Harald Welted0e36dc2010-06-01 18:28:10 +0200683 case GPRS_SAPI_TOM2:
684 case GPRS_SAPI_TOM8:
685 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltec14e5822010-05-02 09:28:11 +0200686 default:
Harald Welte24b78872010-05-13 19:47:50 +0200687 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltec14e5822010-05-02 09:28:11 +0200688 rc = -EINVAL;
689 break;
Harald Welte75bb8202010-03-14 15:45:01 +0800690 }
691 }
692
Harald Weltec14e5822010-05-02 09:28:11 +0200693 return rc;
Harald Welte75bb8202010-03-14 15:45:01 +0800694}
Harald Welte75871c72010-06-01 11:53:01 +0200695
Maxeb64e3f2016-07-06 11:33:04 +0200696/* Propagate crypto parameters MM -> LLME */
697void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme)
698{
699 if (!mm)
700 return;
701 if (mm->ciph_algo != GPRS_ALGO_GEA0) {
702 llme->algo = mm->ciph_algo;
703 if (llme->cksn != mm->auth_triplet.key_seq &&
704 mm->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
705 memcpy(llme->kc, mm->auth_triplet.vec.kc,
706 gprs_cipher_key_length(mm->ciph_algo));
707 llme->cksn = mm->auth_triplet.key_seq;
708 }
709 } else
710 llme->cksn = GSM_KEY_SEQ_INVAL;
711}
712
Harald Welte75871c72010-06-01 11:53:01 +0200713/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
714int gprs_llgmm_assign(struct gprs_llc_llme *llme,
Maxeb64e3f2016-07-06 11:33:04 +0200715 uint32_t old_tlli, uint32_t new_tlli)
Harald Welte75871c72010-06-01 11:53:01 +0200716{
717 unsigned int i;
718
719 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
720 /* TLLI Assignment 8.3.1 */
721 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
722 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
723 * old is unassigned. Only TLLI new shall be accepted when
724 * received from peer. */
Harald Welteb3d84a42010-07-01 11:54:31 +0200725 if (llme->old_tlli != 0xffffffff) {
726 llme->old_tlli = 0xffffffff;
727 llme->tlli = new_tlli;
728 } else {
729 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
730 * TLLI assignmemt according to 8.3.1 */
731 llme->old_tlli = 0xffffffff;
732 llme->tlli = new_tlli;
733 llme->state = GPRS_LLMS_ASSIGNED;
734 /* 8.5.3.1 For all LLE's */
735 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
736 struct gprs_llc_lle *l = &llme->lle[i];
737 l->vu_send = l->vu_recv = 0;
738 l->retrans_ctr = 0;
739 l->state = GPRS_LLES_ASSIGNED_ADM;
740 /* FIXME Set parameters according to table 9 */
741 }
Harald Welte75871c72010-06-01 11:53:01 +0200742 }
743 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
744 /* TLLI Change 8.3.2 */
745 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther28f928d2013-07-28 20:13:01 +0200746 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freyther1cb1ce22013-07-31 11:20:37 +0200747 llme->old_tlli = old_tlli;
Harald Welte75871c72010-06-01 11:53:01 +0200748 llme->tlli = new_tlli;
749 llme->state = GPRS_LLMS_ASSIGNED;
750 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
751 /* TLLI Unassignment 8.3.3) */
752 llme->tlli = llme->old_tlli = 0;
753 llme->state = GPRS_LLMS_UNASSIGNED;
754 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
755 struct gprs_llc_lle *l = &llme->lle[i];
756 l->state = GPRS_LLES_UNASSIGNED;
757 }
Harald Welte7a62a3a2010-06-28 22:18:26 +0200758 llme_free(llme);
Harald Welte75871c72010-06-01 11:53:01 +0200759 } else
760 return -EINVAL;
761
762 return 0;
763}
Harald Welte81fb3582010-06-30 19:59:55 +0200764
Maxa86f5c02016-06-28 17:39:20 +0200765/* TLLI unassignment */
766int gprs_llgmm_unassign(struct gprs_llc_llme *llme)
767{
Maxeb64e3f2016-07-06 11:33:04 +0200768 return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff);
Maxa86f5c02016-06-28 17:39:20 +0200769}
770
Harald Welteac687be2011-10-16 18:49:05 +0200771/* Chapter 7.2.1.2 LLGMM-RESET.req */
772int gprs_llgmm_reset(struct gprs_llc_llme *llme)
773{
774 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Jacob Erlbeck956f5c72014-09-11 14:20:53 +0200775 struct gprs_llc_lle *lle = &llme->lle[1];
Harald Welteac687be2011-10-16 18:49:05 +0200776
Max6d999582016-07-06 15:57:01 +0200777 if (RAND_bytes((uint8_t *) &llme->iov_ui, 4) != 1) {
778 LOGP(DLLC, LOGL_NOTICE, "RAND_bytes failed for LLC XID reset, "
779 "falling back to rand()\n");
780 llme->iov_ui = rand();
781 }
782
Harald Welteac687be2011-10-16 18:49:05 +0200783 /* First XID component must be RESET */
784 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
785 /* randomly select new IOV-UI */
Max6d999582016-07-06 15:57:01 +0200786 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &llme->iov_ui);
Harald Welteac687be2011-10-16 18:49:05 +0200787
Jacob Erlbeck956f5c72014-09-11 14:20:53 +0200788 /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */
789 lle->vu_recv = 0;
790 lle->vu_send = 0;
791 lle->oc_ui_send = 0;
792 lle->oc_ui_recv = 0;
793
Harald Welteac687be2011-10-16 18:49:05 +0200794 /* FIXME: Start T200, wait for XID response */
Jacob Erlbeck956f5c72014-09-11 14:20:53 +0200795 return gprs_llc_tx_xid(lle, msg, 1);
Harald Welteac687be2011-10-16 18:49:05 +0200796}
797
Max6d999582016-07-06 15:57:01 +0200798int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi,
799 struct gprs_llc_llme *llme)
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200800{
801 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Max6d999582016-07-06 15:57:01 +0200802
803 if (RAND_bytes((uint8_t *) &llme->iov_ui, 4) != 1) {
804 LOGP(DLLC, LOGL_NOTICE, "RAND_bytes failed for LLC XID reset, "
805 "falling back to rand()\n");
806 llme->iov_ui = rand();
807 }
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200808
809 /* First XID component must be RESET */
810 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
811 /* randomly select new IOV-UI */
Max6d999582016-07-06 15:57:01 +0200812 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &llme->iov_ui);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200813
814 /* FIXME: Start T200, wait for XID response */
815
816 msgb_tlli(msg) = msgb_tlli(oldmsg);
817 msgb_bvci(msg) = msgb_bvci(oldmsg);
818 msgb_nsei(msg) = msgb_nsei(oldmsg);
819
820 return gprs_llc_tx_u(msg, sapi, 1, GPRS_LLC_U_XID, 1);
821}
822
Harald Welte81fb3582010-06-30 19:59:55 +0200823int gprs_llc_init(const char *cipher_plugin_path)
824{
825 return gprs_cipher_load(cipher_plugin_path);
826}