blob: c88d1626935fca4240f7445f38f5daf252bd3cc8 [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>
Harald Welte9b455bf2010-03-14 15:45:01 +080024
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010025#include <osmocom/core/msgb.h>
26#include <osmocom/core/linuxlist.h>
27#include <osmocom/core/timer.h>
28#include <osmocom/core/talloc.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080029#include <osmocom/gprs/gprs_bssgp.h>
Harald Weltea2665542010-05-02 09:28:11 +020030
31#include <openbsc/gsm_data.h>
32#include <openbsc/debug.h>
Harald Welte807a5d82010-06-01 11:53:01 +020033#include <openbsc/gprs_sgsn.h>
34#include <openbsc/gprs_gmm.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080035#include <openbsc/gprs_llc.h>
36#include <openbsc/crc24.h>
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +020037#include <openbsc/sgsn.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080038
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +020039static struct gprs_llc_llme *llme_alloc(uint32_t tlli);
40
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +020041/* If the TLLI is foreign, return its local version */
42static inline uint32_t tlli_foreign2local(uint32_t tlli)
43{
44 uint32_t new_tlli;
45
46 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
47 new_tlli = tlli | 0x40000000;
Daniel Willmann46553142014-09-03 17:46:44 +020048 LOGP(DLLC, LOGL_NOTICE, "TLLI 0x%08x is foreign, converting to "
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +020049 "local TLLI 0x%08x\n", tlli, new_tlli);
50 } else
51 new_tlli = tlli;
52
53 return new_tlli;
54}
55
Harald Weltefaa70ff2012-06-17 09:31:16 +080056/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
57 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
58static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
59{
60 struct bssgp_dl_ud_par dup;
61 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
62
Harald Welte8c004962012-07-04 21:53:12 +020063 memset(&dup, 0, sizeof(dup));
64 /* before we have received some identity from the MS, we might
65 * not yet have a MMC context (e.g. XID negotiation of primarly
66 * LLC connection fro GMM sapi). */
67 if (mmctx) {
68 dup.imsi = mmctx->imsi;
69 dup.drx_parms = mmctx->drx_parms;
70 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
71 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +020072
73 /* make sure we only send it to the right llme */
74 OSMO_ASSERT(msgb_tlli(msg) == mmctx->llme->tlli
75 || msgb_tlli(msg) == mmctx->llme->old_tlli
76 || tlli_foreign2local(msgb_tlli(msg)) == mmctx->llme->tlli
77 || tlli_foreign2local(msgb_tlli(msg)) == mmctx->llme->old_tlli);
Harald Welte8c004962012-07-04 21:53:12 +020078 }
Harald Weltefaa70ff2012-06-17 09:31:16 +080079 memcpy(&dup.qos_profile, qos_profile_default,
80 sizeof(qos_profile_default));
81
Harald Weltece95b272012-06-17 13:04:02 +080082 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +080083}
84
85
Harald Welte1d9d9442010-06-03 07:11:04 +020086/* Section 8.9.9 LLC layer parameter default values */
Daniel Willmann46d13262014-06-27 17:05:48 +020087static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = {
Harald Welte1d9d9442010-06-03 07:11:04 +020088 [1] = {
89 .t200_201 = 5,
90 .n200 = 3,
91 .n201_u = 400,
92 },
93 [2] = {
94 .t200_201 = 5,
95 .n200 = 3,
96 .n201_u = 270,
97 },
98 [3] = {
99 .iov_i_exp = 27,
100 .t200_201 = 5,
101 .n200 = 3,
102 .n201_u = 500,
103 .n201_i = 1503,
104 .mD = 1520,
105 .mU = 1520,
106 .kD = 16,
107 .kU = 16,
108 },
109 [5] = {
110 .iov_i_exp = 27,
111 .t200_201 = 10,
112 .n200 = 3,
113 .n201_u = 500,
114 .n201_i = 1503,
115 .mD = 760,
116 .mU = 760,
117 .kD = 8,
118 .kU = 8,
119 },
120 [7] = {
121 .t200_201 = 20,
122 .n200 = 3,
123 .n201_u = 270,
124 },
125 [8] = {
126 .t200_201 = 20,
127 .n200 = 3,
128 .n201_u = 270,
129 },
130 [9] = {
131 .iov_i_exp = 27,
132 .t200_201 = 20,
133 .n200 = 3,
134 .n201_u = 500,
135 .n201_i = 1503,
136 .mD = 380,
137 .mU = 380,
138 .kD = 4,
139 .kU = 4,
140 },
141 [11] = {
142 .iov_i_exp = 27,
143 .t200_201 = 40,
144 .n200 = 3,
145 .n201_u = 500,
146 .n201_i = 1503,
147 .mD = 190,
148 .mU = 190,
149 .kD = 2,
150 .kU = 2,
151 },
152};
153
Harald Welte807a5d82010-06-01 11:53:01 +0200154LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200155void *llc_tall_ctx;
156
157/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200158static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200159{
Harald Welte807a5d82010-06-01 11:53:01 +0200160 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200161
Harald Welte807a5d82010-06-01 11:53:01 +0200162 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
163 if (llme->tlli == tlli || llme->old_tlli == tlli)
164 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200165 }
166 return NULL;
167}
168
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200169struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi)
170{
171 struct gprs_llc_llme *llme;
172 struct gprs_llc_lle *lle;
173
174 lle = lle_by_tlli_sapi(tlli, sapi);
175 if (lle)
176 return lle;
177
178 lle = lle_by_tlli_sapi(tlli_foreign2local(tlli), sapi);
179 if (lle)
180 return lle;
181
182 LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, "
183 "creating LLME on the fly\n", tlli);
184 llme = llme_alloc(tlli);
185 lle = &llme->lle[sapi];
186 return lle;
187}
188
189struct llist_head *gprs_llme_list(void)
190{
191 return &gprs_llc_llmes;
192}
193
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200194/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
195static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
196 uint8_t sapi, enum gprs_llc_cmd cmd)
197{
198 struct gprs_llc_lle *lle;
199
200 /* We already know about this TLLI */
201 lle = lle_by_tlli_sapi(tlli, sapi);
202 if (lle)
203 return lle;
204
205 /* Maybe it is a routing area update but we already know this sapi? */
206 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
207 lle = lle_by_tlli_sapi(tlli_foreign2local(tlli), sapi);
208 if (lle) {
209 LOGP(DLLC, LOGL_NOTICE,
210 "LLC RX: Found a local entry for TLLI 0x%08x\n",
211 tlli);
212 return lle;
213 }
214 }
215
216 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
217 * except UID and XID frames with SAPI=1 */
218 if (sapi == GPRS_SAPI_GMM &&
219 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
220 struct gprs_llc_llme *llme;
221 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
222 llme = llme_alloc(tlli);
Daniel Willmann46553142014-09-03 17:46:44 +0200223 LOGP(DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, "
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200224 "creating LLME on the fly\n", tlli);
225 lle = &llme->lle[sapi];
226 return lle;
227 }
228
229 LOGP(DLLC, LOGL_NOTICE,
230 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
231 tlli, sapi);
232 return NULL;
233}
234
Harald Welte1d9d9442010-06-03 07:11:04 +0200235static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200236{
Harald Welte807a5d82010-06-01 11:53:01 +0200237 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200238
Harald Welte807a5d82010-06-01 11:53:01 +0200239 lle->llme = llme;
240 lle->sapi = sapi;
241 lle->state = GPRS_LLES_UNASSIGNED;
242
Harald Welte1d9d9442010-06-03 07:11:04 +0200243 /* Initialize according to parameters */
244 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200245}
246
247static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
248{
249 struct gprs_llc_llme *llme;
250 uint32_t i;
251
252 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
253 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200254 return NULL;
255
Harald Welte807a5d82010-06-01 11:53:01 +0200256 llme->tlli = tlli;
Harald Welte875840c2010-07-01 11:54:31 +0200257 llme->old_tlli = 0xffffffff;
Harald Welte807a5d82010-06-01 11:53:01 +0200258 llme->state = GPRS_LLMS_UNASSIGNED;
Harald Weltea2665542010-05-02 09:28:11 +0200259
Harald Welte807a5d82010-06-01 11:53:01 +0200260 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
261 lle_init(llme, i);
262
263 llist_add(&llme->list, &gprs_llc_llmes);
264
265 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200266}
267
Harald Weltef7fef482010-06-28 22:18:26 +0200268static void llme_free(struct gprs_llc_llme *llme)
269{
270 llist_del(&llme->list);
271 talloc_free(llme);
272}
273
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200274#if 0
275/* FIXME: Unused code... */
Harald Welte9b455bf2010-03-14 15:45:01 +0800276static void t200_expired(void *data)
277{
278 struct gprs_llc_lle *lle = data;
279
280 /* 8.5.1.3: Expiry of T200 */
281
Harald Welte1d9d9442010-06-03 07:11:04 +0200282 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800283 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200284 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800285 }
286
287 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200288 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200289 /* FIXME: retransmit SABM */
290 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800291 lle->retrans_ctr++;
292 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200293 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200294 /* FIXME: retransmit DISC */
295 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800296 lle->retrans_ctr++;
297 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200298 default:
299 LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state);
300 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800301 }
302
303}
304
305static void t201_expired(void *data)
306{
307 struct gprs_llc_lle *lle = data;
308
Harald Welte1d9d9442010-06-03 07:11:04 +0200309 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200310 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
311 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800312 lle->retrans_ctr++;
313 }
314}
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200315#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800316
Harald Welte10997d02010-05-03 12:28:12 +0200317int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
318 enum gprs_llc_u_cmd u_cmd, int pf_bit)
319{
320 uint8_t *fcs, *llch;
321 uint8_t addr, ctrl;
322 uint32_t fcs_calc;
323
324 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
325
326 /* Address Field */
327 addr = sapi & 0xf;
328 if (command)
329 addr |= 0x40;
330
331 /* 6.3 Figure 8 */
332 ctrl = 0xe0 | u_cmd;
333 if (pf_bit)
334 ctrl |= 0x10;
335
336 /* prepend LLC UI header */
337 llch = msgb_push(msg, 2);
338 llch[0] = addr;
339 llch[1] = ctrl;
340
341 /* append FCS to end of frame */
342 fcs = msgb_put(msg, 3);
343 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
344 fcs[0] = fcs_calc & 0xff;
345 fcs[1] = (fcs_calc >> 8) & 0xff;
346 fcs[2] = (fcs_calc >> 16) & 0xff;
347
348 /* Identifiers passed down: (BVCI, NSEI) */
349
Harald Welte1ae09c72010-05-13 19:22:55 +0200350 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800351 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200352}
353
354/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200355static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
356 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200357{
358 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200359 msgb_tlli(msg) = lle->llme->tlli;
360 msgb_bvci(msg) = lle->llme->bvci;
361 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200362
Harald Welte0c1a3032011-10-16 18:49:05 +0200363 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200364}
365
Harald Welte9b455bf2010-03-14 15:45:01 +0800366/* Transmit a UI frame over the given SAPI */
Harald Welte56a01452010-05-31 22:12:30 +0200367int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
368 void *mmctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800369{
Harald Weltee6afd602010-05-02 11:19:37 +0200370 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200371 uint8_t *fcs, *llch;
372 uint8_t addr, ctrl[2];
373 uint32_t fcs_calc;
374 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200375 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800376
Harald Weltee6afd602010-05-02 11:19:37 +0200377 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
378
379 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200380 lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi);
Harald Welte1d9d9442010-06-03 07:11:04 +0200381
382 if (msg->len > lle->params.n201_u) {
383 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
384 msg->len, lle->params.n201_u);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200385 msgb_free(msg);
Harald Welte1d9d9442010-06-03 07:11:04 +0200386 return -EFBIG;
387 }
388
Harald Weltee6afd602010-05-02 11:19:37 +0200389 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200390 lle->llme->bvci = msgb_bvci(msg);
391 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200392
Harald Welted07b4f92010-06-30 23:07:59 +0200393 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200394 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200395 oc = lle->oc_ui_send;
396 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200397 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200398 /* Increment Overflow Counter, if needed */
399 if ((lle->vu_send + 1) / 512)
400 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200401
Harald Welte9b455bf2010-03-14 15:45:01 +0800402 /* Address Field */
403 addr = sapi & 0xf;
404 if (command)
405 addr |= 0x40;
406
407 /* Control Field */
408 ctrl[0] = 0xc0;
409 ctrl[0] |= nu >> 6;
410 ctrl[1] = (nu << 2) & 0xfc;
411 ctrl[1] |= 0x01; /* Protected Mode */
412
413 /* prepend LLC UI header */
414 llch = msgb_push(msg, 3);
415 llch[0] = addr;
416 llch[1] = ctrl[0];
417 llch[2] = ctrl[1];
418
419 /* append FCS to end of frame */
420 fcs = msgb_put(msg, 3);
421 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
422 fcs[0] = fcs_calc & 0xff;
423 fcs[1] = (fcs_calc >> 8) & 0xff;
424 fcs[2] = (fcs_calc >> 16) & 0xff;
425
Harald Welted07b4f92010-06-30 23:07:59 +0200426 /* encrypt information field + FCS, if needed! */
427 if (lle->llme->algo != GPRS_ALGO_GEA0) {
428 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
429 uint16_t crypt_len = (fcs + 3) - (llch + 3);
430 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
431 uint32_t iv;
432 int rc, i;
433 uint64_t kc = *(uint64_t *)&lle->llme->kc;
434
435 /* Compute the 'Input' Paraemeter */
436 iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc);
437
438 /* Compute the keystream that we need to XOR with the data */
439 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
440 kc, iv, GPRS_CIPH_SGSN2MS);
441 if (rc < 0) {
442 LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200443 msgb_free(msg);
Harald Welted07b4f92010-06-30 23:07:59 +0200444 return rc;
445 }
446
447 /* XOR the cipher output with the information field + FCS */
448 for (i = 0; i < crypt_len; i++)
449 *(llch + 3 + i) ^= cipher_out[i];
450
451 /* Mark frame as encrypted */
452 ctrl[1] |= 0x02;
453 }
454
Harald Weltee6afd602010-05-02 11:19:37 +0200455 /* Identifiers passed down: (BVCI, NSEI) */
456
Harald Welte1ae09c72010-05-13 19:22:55 +0200457 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800458 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800459}
460
Harald Welte0c1a3032011-10-16 18:49:05 +0200461/* According to 6.4.1.6 / Figure 11 */
462static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data)
463{
464 uint8_t header_len = 1;
465 uint8_t *cur;
466
467 /* type is a 5-bit field... */
468 if (type > 0x1f)
469 return -EINVAL;
470
471 if (length > 3)
472 header_len = 2;
473
474 cur = msgb_put(msg, length + header_len);
475
476 /* build the header without or with XL bit */
477 if (length <= 3) {
478 *cur++ = (type << 2) | (length & 3);
479 } else {
480 *cur++ = 0x80 | (type << 2) | (length >> 6);
481 *cur++ = (length << 2);
482 }
483
484 /* copy over the payload of the parameter*/
485 memcpy(cur, data, length);
486
487 return length + header_len;
488}
489
490static void rx_llc_xid(struct gprs_llc_lle *lle,
491 struct gprs_llc_hdr_parsed *gph)
492{
493 /* FIXME: 8.5.3.3: check if XID is invalid */
494 if (gph->is_cmd) {
495 /* FIXME: implement XID negotiation using SNDCP */
496 struct msgb *resp;
497 uint8_t *xid;
498 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
499 xid = msgb_put(resp, gph->data_len);
500 memcpy(xid, gph->data, gph->data_len);
501 gprs_llc_tx_xid(lle, resp, 0);
502 } else {
503 /* FIXME: if we had sent a XID reset, send
504 * LLGMM-RESET.conf to GMM */
505 /* FIXME: implement XID negotiation using SNDCP */
506 }
507}
508
Harald Welte9b455bf2010-03-14 15:45:01 +0800509static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
510 struct gprs_llc_lle *lle)
511{
512 switch (gph->cmd) {
513 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
514 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200515 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800516 /* start re-establishment (8.7.1) */
517 }
Harald Welte807a5d82010-06-01 11:53:01 +0200518 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800519 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200520 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800521 /* FIXME: process data */
522 break;
523 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
524 /* FIXME: Send UA */
525 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200526 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800527 break;
528 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200529 if (lle->state == GPRS_LLES_LOCAL_EST)
530 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800531 break;
532 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200533 if (lle->state == GPRS_LLES_LOCAL_EST)
534 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800535 break;
536 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
537 break;
538 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200539 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800540 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200541 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400542 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
543 LOGP(DLLC, LOGL_NOTICE,
544 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800545 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200546 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200547
548 /* HACK: non-standard recovery handling. If remote LLE
549 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800550 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200551 * and 'learn' the new sequence number */
552 if (gph->seq_tx != lle->vu_recv_last) {
553 lle->vu_recv_last = gph->seq_tx;
554 lle->vu_recv_duplicates = 0;
555 } else {
556 lle->vu_recv_duplicates++;
557 if (lle->vu_recv_duplicates < 3)
558 return -EIO;
559 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
560 "N(U=%d) after receiving %u duplicates\n",
561 lle->llme ? lle->llme->tlli : -1,
562 gph->seq_tx, lle->vu_recv_duplicates);
563 }
Harald Welteebabdea2010-06-01 18:28:10 +0200564 }
565 /* Increment the sequence number that we expect in the next frame */
566 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200567 /* Increment Overflow Counter */
568 if ((gph->seq_tx + 1) / 512)
569 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200570 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200571 default:
572 LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd);
573 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800574 }
575
576 return 0;
577}
578
Harald Weltea2665542010-05-02 09:28:11 +0200579/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800580int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
581{
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200582 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800583 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200584 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200585 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800586
Harald Welte11d7c102010-05-02 11:54:55 +0200587 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
588
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800589 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800590 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800591 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200592 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200593 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200594 return rc;
595 }
596
Harald Welte807a5d82010-06-01 11:53:01 +0200597 switch (gprs_tlli_type(msgb_tlli(msg))) {
598 case TLLI_LOCAL:
599 case TLLI_FOREIGN:
600 case TLLI_RANDOM:
601 case TLLI_AUXILIARY:
602 break;
603 default:
604 LOGP(DLLC, LOGL_ERROR,
605 "Discarding frame with strange TLLI type\n");
606 break;
607 }
608
Harald Weltea2665542010-05-02 09:28:11 +0200609 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200610 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
611 if (!lle)
612 return 0;
Harald Weltea2665542010-05-02 09:28:11 +0200613
Harald Welted07b4f92010-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;
620 uint64_t kc = *(uint64_t *)&lle->llme->kc;
621 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 Welte1b8827a2010-06-30 23:15:57 +0200651 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-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 Welte10997d02010-05-03 12:28:12 +0200657 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200658 lle->llme->bvci = msgb_bvci(msg);
659 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200660
Harald Welte1ae09c72010-05-13 19:22:55 +0200661 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800662 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200663 if (rc < 0)
664 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800665
Harald Welte1ae09c72010-05-13 19:22:55 +0200666 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte9b455bf2010-03-14 15:45:01 +0800667 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200668 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800669 switch (llhp.sapi) {
670 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200671 /* send LL_UNITDATA_IND to GMM */
Harald Welte807a5d82010-06-01 11:53:01 +0200672 rc = gsm0408_gprs_rcvmsg(msg, lle->llme);
Harald Weltea2665542010-05-02 09:28:11 +0200673 break;
Harald Weltea2665542010-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 Welteebabdea2010-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 Weltea2665542010-05-02 09:28:11 +0200681 case GPRS_SAPI_SMS:
682 /* FIXME */
Harald Welteebabdea2010-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 Weltea2665542010-05-02 09:28:11 +0200686 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200687 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200688 rc = -EINVAL;
689 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800690 }
691 }
692
Harald Weltea2665542010-05-02 09:28:11 +0200693 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800694}
Harald Welte807a5d82010-06-01 11:53:01 +0200695
696/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
697int gprs_llgmm_assign(struct gprs_llc_llme *llme,
698 uint32_t old_tlli, uint32_t new_tlli,
699 enum gprs_ciph_algo alg, const uint8_t *kc)
700{
701 unsigned int i;
702
Harald Welted07b4f92010-06-30 23:07:59 +0200703 /* Update the crypto parameters */
Harald Welted07b4f92010-06-30 23:07:59 +0200704 llme->algo = alg;
Harald Welte3e2e1592010-06-30 23:19:23 +0200705 if (alg != GPRS_ALGO_GEA0)
706 memcpy(llme->kc, kc, sizeof(llme->kc));
Harald Welted07b4f92010-06-30 23:07:59 +0200707
Harald Welte807a5d82010-06-01 11:53:01 +0200708 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
709 /* TLLI Assignment 8.3.1 */
710 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
711 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
712 * old is unassigned. Only TLLI new shall be accepted when
713 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +0200714 if (llme->old_tlli != 0xffffffff) {
715 llme->old_tlli = 0xffffffff;
716 llme->tlli = new_tlli;
717 } else {
718 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
719 * TLLI assignmemt according to 8.3.1 */
720 llme->old_tlli = 0xffffffff;
721 llme->tlli = new_tlli;
722 llme->state = GPRS_LLMS_ASSIGNED;
723 /* 8.5.3.1 For all LLE's */
724 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
725 struct gprs_llc_lle *l = &llme->lle[i];
726 l->vu_send = l->vu_recv = 0;
727 l->retrans_ctr = 0;
728 l->state = GPRS_LLES_ASSIGNED_ADM;
729 /* FIXME Set parameters according to table 9 */
730 }
Harald Welte807a5d82010-06-01 11:53:01 +0200731 }
732 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
733 /* TLLI Change 8.3.2 */
734 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +0200735 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freytheraa93bac2013-07-31 11:20:37 +0200736 llme->old_tlli = old_tlli;
Harald Welte807a5d82010-06-01 11:53:01 +0200737 llme->tlli = new_tlli;
738 llme->state = GPRS_LLMS_ASSIGNED;
739 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
740 /* TLLI Unassignment 8.3.3) */
741 llme->tlli = llme->old_tlli = 0;
742 llme->state = GPRS_LLMS_UNASSIGNED;
743 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
744 struct gprs_llc_lle *l = &llme->lle[i];
745 l->state = GPRS_LLES_UNASSIGNED;
746 }
Harald Weltef7fef482010-06-28 22:18:26 +0200747 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +0200748 } else
749 return -EINVAL;
750
751 return 0;
752}
Harald Welte496aee42010-06-30 19:59:55 +0200753
Harald Welte0c1a3032011-10-16 18:49:05 +0200754/* Chapter 7.2.1.2 LLGMM-RESET.req */
755int gprs_llgmm_reset(struct gprs_llc_llme *llme)
756{
757 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
758 int random = rand();
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +0200759 struct gprs_llc_lle *lle = &llme->lle[1];
Harald Welte0c1a3032011-10-16 18:49:05 +0200760
761 /* First XID component must be RESET */
762 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
763 /* randomly select new IOV-UI */
Harald Welte066a0f52011-10-16 18:59:20 +0200764 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random);
Harald Welte0c1a3032011-10-16 18:49:05 +0200765
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +0200766 /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */
767 lle->vu_recv = 0;
768 lle->vu_send = 0;
769 lle->oc_ui_send = 0;
770 lle->oc_ui_recv = 0;
771
Harald Welte0c1a3032011-10-16 18:49:05 +0200772 /* FIXME: Start T200, wait for XID response */
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +0200773 return gprs_llc_tx_xid(lle, msg, 1);
Harald Welte0c1a3032011-10-16 18:49:05 +0200774}
775
Harald Welte496aee42010-06-30 19:59:55 +0200776int gprs_llc_init(const char *cipher_plugin_path)
777{
778 return gprs_cipher_load(cipher_plugin_path);
779}