blob: 4cf51631b85c02b5f4ed67fe06b2a2b507354496 [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
Harald Weltefaa70ff2012-06-17 09:31:16 +080041/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
42 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
43static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
44{
45 struct bssgp_dl_ud_par dup;
46 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
47
Harald Welte8c004962012-07-04 21:53:12 +020048 memset(&dup, 0, sizeof(dup));
49 /* before we have received some identity from the MS, we might
50 * not yet have a MMC context (e.g. XID negotiation of primarly
51 * LLC connection fro GMM sapi). */
52 if (mmctx) {
53 dup.imsi = mmctx->imsi;
54 dup.drx_parms = mmctx->drx_parms;
55 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
56 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +020057
58 /* make sure we only send it to the right llme */
59 OSMO_ASSERT(msgb_tlli(msg) == mmctx->llme->tlli
Jacob Erlbeck3fbf0a32016-01-04 18:43:32 +010060 || msgb_tlli(msg) == mmctx->llme->old_tlli);
Harald Welte8c004962012-07-04 21:53:12 +020061 }
Harald Weltefaa70ff2012-06-17 09:31:16 +080062 memcpy(&dup.qos_profile, qos_profile_default,
63 sizeof(qos_profile_default));
64
Harald Weltece95b272012-06-17 13:04:02 +080065 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +080066}
67
68
Harald Welte1d9d9442010-06-03 07:11:04 +020069/* Section 8.9.9 LLC layer parameter default values */
Daniel Willmann46d13262014-06-27 17:05:48 +020070static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = {
Harald Welte1d9d9442010-06-03 07:11:04 +020071 [1] = {
72 .t200_201 = 5,
73 .n200 = 3,
74 .n201_u = 400,
75 },
76 [2] = {
77 .t200_201 = 5,
78 .n200 = 3,
79 .n201_u = 270,
80 },
81 [3] = {
82 .iov_i_exp = 27,
83 .t200_201 = 5,
84 .n200 = 3,
85 .n201_u = 500,
86 .n201_i = 1503,
87 .mD = 1520,
88 .mU = 1520,
89 .kD = 16,
90 .kU = 16,
91 },
92 [5] = {
93 .iov_i_exp = 27,
94 .t200_201 = 10,
95 .n200 = 3,
96 .n201_u = 500,
97 .n201_i = 1503,
98 .mD = 760,
99 .mU = 760,
100 .kD = 8,
101 .kU = 8,
102 },
103 [7] = {
104 .t200_201 = 20,
105 .n200 = 3,
106 .n201_u = 270,
107 },
108 [8] = {
109 .t200_201 = 20,
110 .n200 = 3,
111 .n201_u = 270,
112 },
113 [9] = {
114 .iov_i_exp = 27,
115 .t200_201 = 20,
116 .n200 = 3,
117 .n201_u = 500,
118 .n201_i = 1503,
119 .mD = 380,
120 .mU = 380,
121 .kD = 4,
122 .kU = 4,
123 },
124 [11] = {
125 .iov_i_exp = 27,
126 .t200_201 = 40,
127 .n200 = 3,
128 .n201_u = 500,
129 .n201_i = 1503,
130 .mD = 190,
131 .mU = 190,
132 .kD = 2,
133 .kU = 2,
134 },
135};
136
Harald Welte807a5d82010-06-01 11:53:01 +0200137LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200138void *llc_tall_ctx;
139
140/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200141static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200142{
Harald Welte807a5d82010-06-01 11:53:01 +0200143 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200144
Harald Welte807a5d82010-06-01 11:53:01 +0200145 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
146 if (llme->tlli == tlli || llme->old_tlli == tlli)
147 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200148 }
149 return NULL;
150}
151
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200152struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi)
153{
154 struct gprs_llc_llme *llme;
155 struct gprs_llc_lle *lle;
156
157 lle = lle_by_tlli_sapi(tlli, sapi);
158 if (lle)
159 return lle;
160
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200161 LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, "
162 "creating LLME on the fly\n", tlli);
163 llme = llme_alloc(tlli);
164 lle = &llme->lle[sapi];
165 return lle;
166}
167
168struct llist_head *gprs_llme_list(void)
169{
170 return &gprs_llc_llmes;
171}
172
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200173/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
174static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
175 uint8_t sapi, enum gprs_llc_cmd cmd)
176{
177 struct gprs_llc_lle *lle;
178
179 /* We already know about this TLLI */
180 lle = lle_by_tlli_sapi(tlli, sapi);
181 if (lle)
182 return lle;
183
184 /* Maybe it is a routing area update but we already know this sapi? */
185 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
Jacob Erlbeck3fbf0a32016-01-04 18:43:32 +0100186 lle = lle_by_tlli_sapi(tlli, sapi);
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200187 if (lle) {
188 LOGP(DLLC, LOGL_NOTICE,
189 "LLC RX: Found a local entry for TLLI 0x%08x\n",
190 tlli);
191 return lle;
192 }
193 }
194
195 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
196 * except UID and XID frames with SAPI=1 */
197 if (sapi == GPRS_SAPI_GMM &&
198 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
199 struct gprs_llc_llme *llme;
200 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
201 llme = llme_alloc(tlli);
Daniel Willmann46553142014-09-03 17:46:44 +0200202 LOGP(DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, "
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200203 "creating LLME on the fly\n", tlli);
204 lle = &llme->lle[sapi];
205 return lle;
206 }
207
208 LOGP(DLLC, LOGL_NOTICE,
209 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
210 tlli, sapi);
211 return NULL;
212}
213
Harald Welte1d9d9442010-06-03 07:11:04 +0200214static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200215{
Harald Welte807a5d82010-06-01 11:53:01 +0200216 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200217
Harald Welte807a5d82010-06-01 11:53:01 +0200218 lle->llme = llme;
219 lle->sapi = sapi;
220 lle->state = GPRS_LLES_UNASSIGNED;
221
Harald Welte1d9d9442010-06-03 07:11:04 +0200222 /* Initialize according to parameters */
223 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200224}
225
226static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
227{
228 struct gprs_llc_llme *llme;
229 uint32_t i;
230
231 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
232 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200233 return NULL;
234
Harald Welte807a5d82010-06-01 11:53:01 +0200235 llme->tlli = tlli;
Harald Welte875840c2010-07-01 11:54:31 +0200236 llme->old_tlli = 0xffffffff;
Harald Welte807a5d82010-06-01 11:53:01 +0200237 llme->state = GPRS_LLMS_UNASSIGNED;
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100238 llme->age_timestamp = GPRS_LLME_RESET_AGE;
Harald Weltea2665542010-05-02 09:28:11 +0200239
Harald Welte807a5d82010-06-01 11:53:01 +0200240 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
241 lle_init(llme, i);
242
243 llist_add(&llme->list, &gprs_llc_llmes);
244
245 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200246}
247
Harald Weltef7fef482010-06-28 22:18:26 +0200248static void llme_free(struct gprs_llc_llme *llme)
249{
250 llist_del(&llme->list);
251 talloc_free(llme);
252}
253
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200254#if 0
255/* FIXME: Unused code... */
Harald Welte9b455bf2010-03-14 15:45:01 +0800256static void t200_expired(void *data)
257{
258 struct gprs_llc_lle *lle = data;
259
260 /* 8.5.1.3: Expiry of T200 */
261
Harald Welte1d9d9442010-06-03 07:11:04 +0200262 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800263 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200264 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800265 }
266
267 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200268 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200269 /* FIXME: retransmit SABM */
270 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800271 lle->retrans_ctr++;
272 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200273 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200274 /* FIXME: retransmit DISC */
275 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800276 lle->retrans_ctr++;
277 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200278 default:
279 LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state);
280 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800281 }
282
283}
284
285static void t201_expired(void *data)
286{
287 struct gprs_llc_lle *lle = data;
288
Harald Welte1d9d9442010-06-03 07:11:04 +0200289 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200290 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
291 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800292 lle->retrans_ctr++;
293 }
294}
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200295#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800296
Harald Welte10997d02010-05-03 12:28:12 +0200297int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
298 enum gprs_llc_u_cmd u_cmd, int pf_bit)
299{
300 uint8_t *fcs, *llch;
301 uint8_t addr, ctrl;
302 uint32_t fcs_calc;
303
304 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
305
306 /* Address Field */
307 addr = sapi & 0xf;
308 if (command)
309 addr |= 0x40;
310
311 /* 6.3 Figure 8 */
312 ctrl = 0xe0 | u_cmd;
313 if (pf_bit)
314 ctrl |= 0x10;
315
316 /* prepend LLC UI header */
317 llch = msgb_push(msg, 2);
318 llch[0] = addr;
319 llch[1] = ctrl;
320
321 /* append FCS to end of frame */
322 fcs = msgb_put(msg, 3);
323 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
324 fcs[0] = fcs_calc & 0xff;
325 fcs[1] = (fcs_calc >> 8) & 0xff;
326 fcs[2] = (fcs_calc >> 16) & 0xff;
327
328 /* Identifiers passed down: (BVCI, NSEI) */
329
Harald Welte1ae09c72010-05-13 19:22:55 +0200330 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800331 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200332}
333
334/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200335static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
336 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200337{
338 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200339 msgb_tlli(msg) = lle->llme->tlli;
340 msgb_bvci(msg) = lle->llme->bvci;
341 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200342
Harald Welte0c1a3032011-10-16 18:49:05 +0200343 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200344}
345
Harald Welte9b455bf2010-03-14 15:45:01 +0800346/* Transmit a UI frame over the given SAPI */
Harald Welte56a01452010-05-31 22:12:30 +0200347int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
348 void *mmctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800349{
Harald Weltee6afd602010-05-02 11:19:37 +0200350 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200351 uint8_t *fcs, *llch;
352 uint8_t addr, ctrl[2];
353 uint32_t fcs_calc;
354 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200355 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800356
Harald Weltee6afd602010-05-02 11:19:37 +0200357 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
358
359 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200360 lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi);
Harald Welte1d9d9442010-06-03 07:11:04 +0200361
362 if (msg->len > lle->params.n201_u) {
363 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
364 msg->len, lle->params.n201_u);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200365 msgb_free(msg);
Harald Welte1d9d9442010-06-03 07:11:04 +0200366 return -EFBIG;
367 }
368
Harald Weltee6afd602010-05-02 11:19:37 +0200369 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200370 lle->llme->bvci = msgb_bvci(msg);
371 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200372
Harald Welted07b4f92010-06-30 23:07:59 +0200373 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200374 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200375 oc = lle->oc_ui_send;
376 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200377 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200378 /* Increment Overflow Counter, if needed */
379 if ((lle->vu_send + 1) / 512)
380 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200381
Harald Welte9b455bf2010-03-14 15:45:01 +0800382 /* Address Field */
383 addr = sapi & 0xf;
384 if (command)
385 addr |= 0x40;
386
387 /* Control Field */
388 ctrl[0] = 0xc0;
389 ctrl[0] |= nu >> 6;
390 ctrl[1] = (nu << 2) & 0xfc;
391 ctrl[1] |= 0x01; /* Protected Mode */
392
393 /* prepend LLC UI header */
394 llch = msgb_push(msg, 3);
395 llch[0] = addr;
396 llch[1] = ctrl[0];
397 llch[2] = ctrl[1];
398
399 /* append FCS to end of frame */
400 fcs = msgb_put(msg, 3);
401 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
402 fcs[0] = fcs_calc & 0xff;
403 fcs[1] = (fcs_calc >> 8) & 0xff;
404 fcs[2] = (fcs_calc >> 16) & 0xff;
405
Harald Welted07b4f92010-06-30 23:07:59 +0200406 /* encrypt information field + FCS, if needed! */
407 if (lle->llme->algo != GPRS_ALGO_GEA0) {
408 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
409 uint16_t crypt_len = (fcs + 3) - (llch + 3);
410 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
411 uint32_t iv;
412 int rc, i;
413 uint64_t kc = *(uint64_t *)&lle->llme->kc;
414
415 /* Compute the 'Input' Paraemeter */
416 iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc);
417
418 /* Compute the keystream that we need to XOR with the data */
419 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
420 kc, iv, GPRS_CIPH_SGSN2MS);
421 if (rc < 0) {
422 LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200423 msgb_free(msg);
Harald Welted07b4f92010-06-30 23:07:59 +0200424 return rc;
425 }
426
427 /* XOR the cipher output with the information field + FCS */
428 for (i = 0; i < crypt_len; i++)
429 *(llch + 3 + i) ^= cipher_out[i];
430
431 /* Mark frame as encrypted */
432 ctrl[1] |= 0x02;
433 }
434
Harald Weltee6afd602010-05-02 11:19:37 +0200435 /* Identifiers passed down: (BVCI, NSEI) */
436
Harald Welte1ae09c72010-05-13 19:22:55 +0200437 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800438 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800439}
440
Harald Welte0c1a3032011-10-16 18:49:05 +0200441/* According to 6.4.1.6 / Figure 11 */
442static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data)
443{
444 uint8_t header_len = 1;
445 uint8_t *cur;
446
447 /* type is a 5-bit field... */
448 if (type > 0x1f)
449 return -EINVAL;
450
451 if (length > 3)
452 header_len = 2;
453
454 cur = msgb_put(msg, length + header_len);
455
456 /* build the header without or with XL bit */
457 if (length <= 3) {
458 *cur++ = (type << 2) | (length & 3);
459 } else {
460 *cur++ = 0x80 | (type << 2) | (length >> 6);
461 *cur++ = (length << 2);
462 }
463
464 /* copy over the payload of the parameter*/
465 memcpy(cur, data, length);
466
467 return length + header_len;
468}
469
470static void rx_llc_xid(struct gprs_llc_lle *lle,
471 struct gprs_llc_hdr_parsed *gph)
472{
473 /* FIXME: 8.5.3.3: check if XID is invalid */
474 if (gph->is_cmd) {
475 /* FIXME: implement XID negotiation using SNDCP */
476 struct msgb *resp;
477 uint8_t *xid;
478 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
479 xid = msgb_put(resp, gph->data_len);
480 memcpy(xid, gph->data, gph->data_len);
481 gprs_llc_tx_xid(lle, resp, 0);
482 } else {
483 /* FIXME: if we had sent a XID reset, send
484 * LLGMM-RESET.conf to GMM */
485 /* FIXME: implement XID negotiation using SNDCP */
486 }
487}
488
Harald Welte9b455bf2010-03-14 15:45:01 +0800489static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
490 struct gprs_llc_lle *lle)
491{
492 switch (gph->cmd) {
493 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
494 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200495 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800496 /* start re-establishment (8.7.1) */
497 }
Harald Welte807a5d82010-06-01 11:53:01 +0200498 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800499 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200500 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800501 /* FIXME: process data */
502 break;
503 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
504 /* FIXME: Send UA */
505 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200506 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800507 break;
508 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200509 if (lle->state == GPRS_LLES_LOCAL_EST)
510 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800511 break;
512 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200513 if (lle->state == GPRS_LLES_LOCAL_EST)
514 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800515 break;
516 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
517 break;
518 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200519 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800520 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200521 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400522 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
523 LOGP(DLLC, LOGL_NOTICE,
524 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800525 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200526 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200527
528 /* HACK: non-standard recovery handling. If remote LLE
529 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800530 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200531 * and 'learn' the new sequence number */
532 if (gph->seq_tx != lle->vu_recv_last) {
533 lle->vu_recv_last = gph->seq_tx;
534 lle->vu_recv_duplicates = 0;
535 } else {
536 lle->vu_recv_duplicates++;
537 if (lle->vu_recv_duplicates < 3)
538 return -EIO;
539 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
540 "N(U=%d) after receiving %u duplicates\n",
541 lle->llme ? lle->llme->tlli : -1,
542 gph->seq_tx, lle->vu_recv_duplicates);
543 }
Harald Welteebabdea2010-06-01 18:28:10 +0200544 }
545 /* Increment the sequence number that we expect in the next frame */
546 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200547 /* Increment Overflow Counter */
548 if ((gph->seq_tx + 1) / 512)
549 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200550 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200551 default:
552 LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd);
553 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800554 }
555
556 return 0;
557}
558
Harald Weltea2665542010-05-02 09:28:11 +0200559/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800560int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
561{
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200562 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800563 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200564 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200565 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800566
Harald Welte11d7c102010-05-02 11:54:55 +0200567 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
568
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800569 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800570 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800571 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200572 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200573 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200574 return rc;
575 }
576
Harald Welte807a5d82010-06-01 11:53:01 +0200577 switch (gprs_tlli_type(msgb_tlli(msg))) {
578 case TLLI_LOCAL:
579 case TLLI_FOREIGN:
580 case TLLI_RANDOM:
581 case TLLI_AUXILIARY:
582 break;
583 default:
584 LOGP(DLLC, LOGL_ERROR,
585 "Discarding frame with strange TLLI type\n");
586 break;
587 }
588
Harald Weltea2665542010-05-02 09:28:11 +0200589 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200590 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200591 if (!lle) {
592 switch (llhp.sapi) {
593 case GPRS_SAPI_SNDCP3:
594 case GPRS_SAPI_SNDCP5:
595 case GPRS_SAPI_SNDCP9:
596 case GPRS_SAPI_SNDCP11:
597 /* Ask an upper layer for help. */
598 return sgsn_force_reattach_oldmsg(msg);
599 default:
600 break;
601 }
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200602 return 0;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200603 }
Harald Weltea2665542010-05-02 09:28:11 +0200604
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100605 /* reset age computation */
606 lle->llme->age_timestamp = GPRS_LLME_RESET_AGE;
607
Harald Welted07b4f92010-06-30 23:07:59 +0200608 /* decrypt information field + FCS, if needed! */
609 if (llhp.is_encrypted) {
610 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
611 uint16_t crypt_len = llhp.data_len + 3;
612 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
613 uint32_t iv;
614 uint64_t kc = *(uint64_t *)&lle->llme->kc;
615 int rc, i;
616
617 if (lle->llme->algo == GPRS_ALGO_GEA0) {
618 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
619 "has no KC/Algo! Dropping.\n");
620 return 0;
621 }
622
623 iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx,
624 lle->oc_ui_recv);
625 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
626 kc, iv, GPRS_CIPH_MS2SGSN);
627 if (rc < 0) {
628 LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n",
629 rc);
630 return rc;
631 }
632
633 /* XOR the cipher output with the information field + FCS */
634 for (i = 0; i < crypt_len; i++)
635 *(llhp.data + i) ^= cipher_out[i];
636 } else {
637 if (lle->llme->algo != GPRS_ALGO_GEA0) {
638 LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC "
639 "that is supposed to be encrypted. Dropping.\n");
640 return 0;
641 }
642 }
643
644 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200645 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200646 if (llhp.fcs != llhp.fcs_calc) {
647 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
648 return -EIO;
649 }
650
Harald Welte10997d02010-05-03 12:28:12 +0200651 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200652 lle->llme->bvci = msgb_bvci(msg);
653 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200654
Harald Welte1ae09c72010-05-13 19:22:55 +0200655 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800656 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200657 if (rc < 0)
658 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800659
Harald Welte1ae09c72010-05-13 19:22:55 +0200660 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte22df4ac2015-08-16 15:23:32 +0200661 if (llhp.cmd == GPRS_LLC_UI && llhp.data && llhp.data_len) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200662 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800663 switch (llhp.sapi) {
664 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200665 /* send LL_UNITDATA_IND to GMM */
Harald Welte807a5d82010-06-01 11:53:01 +0200666 rc = gsm0408_gprs_rcvmsg(msg, lle->llme);
Harald Weltea2665542010-05-02 09:28:11 +0200667 break;
Harald Weltea2665542010-05-02 09:28:11 +0200668 case GPRS_SAPI_SNDCP3:
669 case GPRS_SAPI_SNDCP5:
670 case GPRS_SAPI_SNDCP9:
671 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +0200672 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
673 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
674 break;
Harald Weltea2665542010-05-02 09:28:11 +0200675 case GPRS_SAPI_SMS:
676 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +0200677 case GPRS_SAPI_TOM2:
678 case GPRS_SAPI_TOM8:
679 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200680 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200681 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200682 rc = -EINVAL;
683 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800684 }
685 }
686
Harald Weltea2665542010-05-02 09:28:11 +0200687 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800688}
Harald Welte807a5d82010-06-01 11:53:01 +0200689
690/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
691int gprs_llgmm_assign(struct gprs_llc_llme *llme,
692 uint32_t old_tlli, uint32_t new_tlli,
693 enum gprs_ciph_algo alg, const uint8_t *kc)
694{
695 unsigned int i;
696
Harald Welted07b4f92010-06-30 23:07:59 +0200697 /* Update the crypto parameters */
Harald Welted07b4f92010-06-30 23:07:59 +0200698 llme->algo = alg;
Harald Welte3e2e1592010-06-30 23:19:23 +0200699 if (alg != GPRS_ALGO_GEA0)
700 memcpy(llme->kc, kc, sizeof(llme->kc));
Harald Welted07b4f92010-06-30 23:07:59 +0200701
Harald Welte807a5d82010-06-01 11:53:01 +0200702 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
703 /* TLLI Assignment 8.3.1 */
704 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
705 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
706 * old is unassigned. Only TLLI new shall be accepted when
707 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +0200708 if (llme->old_tlli != 0xffffffff) {
709 llme->old_tlli = 0xffffffff;
710 llme->tlli = new_tlli;
711 } else {
712 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
713 * TLLI assignmemt according to 8.3.1 */
714 llme->old_tlli = 0xffffffff;
715 llme->tlli = new_tlli;
716 llme->state = GPRS_LLMS_ASSIGNED;
717 /* 8.5.3.1 For all LLE's */
718 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
719 struct gprs_llc_lle *l = &llme->lle[i];
720 l->vu_send = l->vu_recv = 0;
721 l->retrans_ctr = 0;
722 l->state = GPRS_LLES_ASSIGNED_ADM;
723 /* FIXME Set parameters according to table 9 */
724 }
Harald Welte807a5d82010-06-01 11:53:01 +0200725 }
726 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
727 /* TLLI Change 8.3.2 */
728 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +0200729 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freytheraa93bac2013-07-31 11:20:37 +0200730 llme->old_tlli = old_tlli;
Harald Welte807a5d82010-06-01 11:53:01 +0200731 llme->tlli = new_tlli;
732 llme->state = GPRS_LLMS_ASSIGNED;
733 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
734 /* TLLI Unassignment 8.3.3) */
735 llme->tlli = llme->old_tlli = 0;
736 llme->state = GPRS_LLMS_UNASSIGNED;
737 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
738 struct gprs_llc_lle *l = &llme->lle[i];
739 l->state = GPRS_LLES_UNASSIGNED;
740 }
Harald Weltef7fef482010-06-28 22:18:26 +0200741 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +0200742 } else
743 return -EINVAL;
744
745 return 0;
746}
Harald Welte496aee42010-06-30 19:59:55 +0200747
Harald Welte0c1a3032011-10-16 18:49:05 +0200748/* Chapter 7.2.1.2 LLGMM-RESET.req */
749int gprs_llgmm_reset(struct gprs_llc_llme *llme)
750{
751 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
752 int random = rand();
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +0200753 struct gprs_llc_lle *lle = &llme->lle[1];
Harald Welte0c1a3032011-10-16 18:49:05 +0200754
755 /* First XID component must be RESET */
756 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
757 /* randomly select new IOV-UI */
Harald Welte066a0f52011-10-16 18:59:20 +0200758 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random);
Harald Welte0c1a3032011-10-16 18:49:05 +0200759
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +0200760 /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */
761 lle->vu_recv = 0;
762 lle->vu_send = 0;
763 lle->oc_ui_send = 0;
764 lle->oc_ui_recv = 0;
765
Harald Welte0c1a3032011-10-16 18:49:05 +0200766 /* FIXME: Start T200, wait for XID response */
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +0200767 return gprs_llc_tx_xid(lle, msg, 1);
Harald Welte0c1a3032011-10-16 18:49:05 +0200768}
769
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200770int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi)
771{
772 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
773 int random = rand();
774
775 /* First XID component must be RESET */
776 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
777 /* randomly select new IOV-UI */
778 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random);
779
780 /* FIXME: Start T200, wait for XID response */
781
782 msgb_tlli(msg) = msgb_tlli(oldmsg);
783 msgb_bvci(msg) = msgb_bvci(oldmsg);
784 msgb_nsei(msg) = msgb_nsei(oldmsg);
785
786 return gprs_llc_tx_u(msg, sapi, 1, GPRS_LLC_U_XID, 1);
787}
788
Harald Welte496aee42010-06-30 19:59:55 +0200789int gprs_llc_init(const char *cipher_plugin_path)
790{
791 return gprs_cipher_load(cipher_plugin_path);
792}