blob: 57e557ae3bc9d8142d3afce3a4b00747f0cee30f [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
Harald Weltefaa70ff2012-06-17 09:31:16 +080039/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
40 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
41static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
42{
43 struct bssgp_dl_ud_par dup;
44 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
45
Harald Welte8c004962012-07-04 21:53:12 +020046 memset(&dup, 0, sizeof(dup));
47 /* before we have received some identity from the MS, we might
48 * not yet have a MMC context (e.g. XID negotiation of primarly
49 * LLC connection fro GMM sapi). */
50 if (mmctx) {
51 dup.imsi = mmctx->imsi;
52 dup.drx_parms = mmctx->drx_parms;
53 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
54 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
55 }
Harald Weltefaa70ff2012-06-17 09:31:16 +080056 memcpy(&dup.qos_profile, qos_profile_default,
57 sizeof(qos_profile_default));
58
Harald Weltece95b272012-06-17 13:04:02 +080059 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +080060}
61
62
Harald Welte1d9d9442010-06-03 07:11:04 +020063/* Section 8.9.9 LLC layer parameter default values */
64static const struct gprs_llc_params llc_default_params[] = {
65 [1] = {
66 .t200_201 = 5,
67 .n200 = 3,
68 .n201_u = 400,
69 },
70 [2] = {
71 .t200_201 = 5,
72 .n200 = 3,
73 .n201_u = 270,
74 },
75 [3] = {
76 .iov_i_exp = 27,
77 .t200_201 = 5,
78 .n200 = 3,
79 .n201_u = 500,
80 .n201_i = 1503,
81 .mD = 1520,
82 .mU = 1520,
83 .kD = 16,
84 .kU = 16,
85 },
86 [5] = {
87 .iov_i_exp = 27,
88 .t200_201 = 10,
89 .n200 = 3,
90 .n201_u = 500,
91 .n201_i = 1503,
92 .mD = 760,
93 .mU = 760,
94 .kD = 8,
95 .kU = 8,
96 },
97 [7] = {
98 .t200_201 = 20,
99 .n200 = 3,
100 .n201_u = 270,
101 },
102 [8] = {
103 .t200_201 = 20,
104 .n200 = 3,
105 .n201_u = 270,
106 },
107 [9] = {
108 .iov_i_exp = 27,
109 .t200_201 = 20,
110 .n200 = 3,
111 .n201_u = 500,
112 .n201_i = 1503,
113 .mD = 380,
114 .mU = 380,
115 .kD = 4,
116 .kU = 4,
117 },
118 [11] = {
119 .iov_i_exp = 27,
120 .t200_201 = 40,
121 .n200 = 3,
122 .n201_u = 500,
123 .n201_i = 1503,
124 .mD = 190,
125 .mU = 190,
126 .kD = 2,
127 .kU = 2,
128 },
129};
130
Harald Welte807a5d82010-06-01 11:53:01 +0200131LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200132void *llc_tall_ctx;
133
Harald Weltef0901f02010-12-26 10:39:26 +0100134/* If the TLLI is foreign, return its local version */
135static inline uint32_t tlli_foreign2local(uint32_t tlli)
136{
137 uint32_t new_tlli;
138
139 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
140 new_tlli = tlli | 0x40000000;
141 DEBUGP(DLLC, "TLLI 0x%08x is foreign, converting to "
142 "local TLLI 0x%08x\n", tlli, new_tlli);
143 } else
144 new_tlli = tlli;
145
146 return new_tlli;
147}
148
Harald Weltea2665542010-05-02 09:28:11 +0200149/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200150static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200151{
Harald Welte807a5d82010-06-01 11:53:01 +0200152 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200153
Harald Welte807a5d82010-06-01 11:53:01 +0200154 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
155 if (llme->tlli == tlli || llme->old_tlli == tlli)
156 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200157 }
158 return NULL;
159}
160
Harald Welte1d9d9442010-06-03 07:11:04 +0200161static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200162{
Harald Welte807a5d82010-06-01 11:53:01 +0200163 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200164
Harald Welte807a5d82010-06-01 11:53:01 +0200165 lle->llme = llme;
166 lle->sapi = sapi;
167 lle->state = GPRS_LLES_UNASSIGNED;
168
Harald Welte1d9d9442010-06-03 07:11:04 +0200169 /* Initialize according to parameters */
170 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200171}
172
173static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
174{
175 struct gprs_llc_llme *llme;
176 uint32_t i;
177
178 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
179 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200180 return NULL;
181
Harald Welte807a5d82010-06-01 11:53:01 +0200182 llme->tlli = tlli;
Harald Welte875840c2010-07-01 11:54:31 +0200183 llme->old_tlli = 0xffffffff;
Harald Welte807a5d82010-06-01 11:53:01 +0200184 llme->state = GPRS_LLMS_UNASSIGNED;
Harald Weltea2665542010-05-02 09:28:11 +0200185
Harald Welte807a5d82010-06-01 11:53:01 +0200186 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
187 lle_init(llme, i);
188
189 llist_add(&llme->list, &gprs_llc_llmes);
190
191 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200192}
193
Harald Weltef7fef482010-06-28 22:18:26 +0200194static void llme_free(struct gprs_llc_llme *llme)
195{
196 llist_del(&llme->list);
197 talloc_free(llme);
198}
199
Harald Welte9b455bf2010-03-14 15:45:01 +0800200enum gprs_llc_cmd {
201 GPRS_LLC_NULL,
202 GPRS_LLC_RR,
203 GPRS_LLC_ACK,
204 GPRS_LLC_RNR,
205 GPRS_LLC_SACK,
206 GPRS_LLC_DM,
207 GPRS_LLC_DISC,
208 GPRS_LLC_UA,
209 GPRS_LLC_SABM,
210 GPRS_LLC_FRMR,
211 GPRS_LLC_XID,
Harald Welte1ae09c72010-05-13 19:22:55 +0200212 GPRS_LLC_UI,
Harald Welte9b455bf2010-03-14 15:45:01 +0800213};
214
Harald Welteb61f4032010-05-18 12:31:50 +0200215static const struct value_string llc_cmd_strs[] = {
216 { GPRS_LLC_NULL, "NULL" },
217 { GPRS_LLC_RR, "RR" },
218 { GPRS_LLC_ACK, "ACK" },
219 { GPRS_LLC_RNR, "RNR" },
220 { GPRS_LLC_SACK, "SACK" },
221 { GPRS_LLC_DM, "DM" },
222 { GPRS_LLC_DISC, "DISC" },
223 { GPRS_LLC_UA, "UA" },
224 { GPRS_LLC_SABM, "SABM" },
225 { GPRS_LLC_FRMR, "FRMR" },
226 { GPRS_LLC_XID, "XID" },
227 { GPRS_LLC_UI, "UI" },
228 { 0, NULL }
229};
230
Harald Welte9b455bf2010-03-14 15:45:01 +0800231struct gprs_llc_hdr_parsed {
Harald Welteeaa614c2010-05-02 11:26:34 +0200232 uint8_t sapi;
233 uint8_t is_cmd:1,
Harald Welte9b455bf2010-03-14 15:45:01 +0800234 ack_req:1,
235 is_encrypted:1;
Harald Welteeaa614c2010-05-02 11:26:34 +0200236 uint32_t seq_rx;
237 uint32_t seq_tx;
238 uint32_t fcs;
239 uint32_t fcs_calc;
240 uint8_t *data;
Harald Welte5658a1a2010-05-03 13:25:07 +0200241 uint16_t data_len;
Harald Welte1b8827a2010-06-30 23:15:57 +0200242 uint16_t crc_length;
Harald Welte9b455bf2010-03-14 15:45:01 +0800243 enum gprs_llc_cmd cmd;
244};
245
246#define LLC_ALLOC_SIZE 16384
247#define UI_HDR_LEN 3
248#define N202 4
249#define CRC24_LENGTH 3
250
Harald Welteeaa614c2010-05-02 11:26:34 +0200251static int gprs_llc_fcs(uint8_t *data, unsigned int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800252{
Harald Welteeaa614c2010-05-02 11:26:34 +0200253 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800254
255 fcs_calc = crc24_calc(INIT_CRC24, data, len);
256 fcs_calc = ~fcs_calc;
257 fcs_calc &= 0xffffff;
258
259 return fcs_calc;
260}
261
Harald Welte9b455bf2010-03-14 15:45:01 +0800262static void t200_expired(void *data)
263{
264 struct gprs_llc_lle *lle = data;
265
266 /* 8.5.1.3: Expiry of T200 */
267
Harald Welte1d9d9442010-06-03 07:11:04 +0200268 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800269 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200270 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800271 }
272
273 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200274 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200275 /* FIXME: retransmit SABM */
276 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800277 lle->retrans_ctr++;
278 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200279 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200280 /* FIXME: retransmit DISC */
281 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800282 lle->retrans_ctr++;
283 break;
284 }
285
286}
287
288static void t201_expired(void *data)
289{
290 struct gprs_llc_lle *lle = data;
291
Harald Welte1d9d9442010-06-03 07:11:04 +0200292 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200293 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
294 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800295 lle->retrans_ctr++;
296 }
297}
298
Harald Welte10997d02010-05-03 12:28:12 +0200299int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
300 enum gprs_llc_u_cmd u_cmd, int pf_bit)
301{
302 uint8_t *fcs, *llch;
303 uint8_t addr, ctrl;
304 uint32_t fcs_calc;
305
306 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
307
308 /* Address Field */
309 addr = sapi & 0xf;
310 if (command)
311 addr |= 0x40;
312
313 /* 6.3 Figure 8 */
314 ctrl = 0xe0 | u_cmd;
315 if (pf_bit)
316 ctrl |= 0x10;
317
318 /* prepend LLC UI header */
319 llch = msgb_push(msg, 2);
320 llch[0] = addr;
321 llch[1] = ctrl;
322
323 /* append FCS to end of frame */
324 fcs = msgb_put(msg, 3);
325 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
326 fcs[0] = fcs_calc & 0xff;
327 fcs[1] = (fcs_calc >> 8) & 0xff;
328 fcs[2] = (fcs_calc >> 16) & 0xff;
329
330 /* Identifiers passed down: (BVCI, NSEI) */
331
Harald Welte1ae09c72010-05-13 19:22:55 +0200332 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800333 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200334}
335
336/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200337static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
338 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200339{
340 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200341 msgb_tlli(msg) = lle->llme->tlli;
342 msgb_bvci(msg) = lle->llme->bvci;
343 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200344
Harald Welte0c1a3032011-10-16 18:49:05 +0200345 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200346}
347
Harald Welte9b455bf2010-03-14 15:45:01 +0800348/* Transmit a UI frame over the given SAPI */
Harald Welte56a01452010-05-31 22:12:30 +0200349int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
350 void *mmctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800351{
Harald Weltee6afd602010-05-02 11:19:37 +0200352 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200353 uint8_t *fcs, *llch;
354 uint8_t addr, ctrl[2];
355 uint32_t fcs_calc;
356 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200357 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800358
Harald Weltee6afd602010-05-02 11:19:37 +0200359 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
360
361 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
362 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
Harald Welte807a5d82010-06-01 11:53:01 +0200363 if (!lle) {
364 struct gprs_llc_llme *llme;
Harald Weltef0901f02010-12-26 10:39:26 +0100365 LOGP(DLLC, LOGL_ERROR, "LLC TX: unknown TLLI 0x%08x, "
366 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte807a5d82010-06-01 11:53:01 +0200367 llme = llme_alloc(msgb_tlli(msg));
368 lle = &llme->lle[sapi];
369 }
Harald Welte1d9d9442010-06-03 07:11:04 +0200370
371 if (msg->len > lle->params.n201_u) {
372 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
373 msg->len, lle->params.n201_u);
374 return -EFBIG;
375 }
376
Harald Weltee6afd602010-05-02 11:19:37 +0200377 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200378 lle->llme->bvci = msgb_bvci(msg);
379 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200380
Harald Welted07b4f92010-06-30 23:07:59 +0200381 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200382 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200383 oc = lle->oc_ui_send;
384 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200385 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200386 /* Increment Overflow Counter, if needed */
387 if ((lle->vu_send + 1) / 512)
388 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200389
Harald Welte9b455bf2010-03-14 15:45:01 +0800390 /* Address Field */
391 addr = sapi & 0xf;
392 if (command)
393 addr |= 0x40;
394
395 /* Control Field */
396 ctrl[0] = 0xc0;
397 ctrl[0] |= nu >> 6;
398 ctrl[1] = (nu << 2) & 0xfc;
399 ctrl[1] |= 0x01; /* Protected Mode */
400
401 /* prepend LLC UI header */
402 llch = msgb_push(msg, 3);
403 llch[0] = addr;
404 llch[1] = ctrl[0];
405 llch[2] = ctrl[1];
406
407 /* append FCS to end of frame */
408 fcs = msgb_put(msg, 3);
409 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
410 fcs[0] = fcs_calc & 0xff;
411 fcs[1] = (fcs_calc >> 8) & 0xff;
412 fcs[2] = (fcs_calc >> 16) & 0xff;
413
Harald Welted07b4f92010-06-30 23:07:59 +0200414 /* encrypt information field + FCS, if needed! */
415 if (lle->llme->algo != GPRS_ALGO_GEA0) {
416 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
417 uint16_t crypt_len = (fcs + 3) - (llch + 3);
418 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
419 uint32_t iv;
420 int rc, i;
421 uint64_t kc = *(uint64_t *)&lle->llme->kc;
422
423 /* Compute the 'Input' Paraemeter */
424 iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc);
425
426 /* Compute the keystream that we need to XOR with the data */
427 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
428 kc, iv, GPRS_CIPH_SGSN2MS);
429 if (rc < 0) {
430 LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc);
431 return rc;
432 }
433
434 /* XOR the cipher output with the information field + FCS */
435 for (i = 0; i < crypt_len; i++)
436 *(llch + 3 + i) ^= cipher_out[i];
437
438 /* Mark frame as encrypted */
439 ctrl[1] |= 0x02;
440 }
441
Harald Weltee6afd602010-05-02 11:19:37 +0200442 /* Identifiers passed down: (BVCI, NSEI) */
443
Harald Welte1ae09c72010-05-13 19:22:55 +0200444 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800445 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800446}
447
Harald Welte0c1a3032011-10-16 18:49:05 +0200448/* According to 6.4.1.6 / Figure 11 */
449static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data)
450{
451 uint8_t header_len = 1;
452 uint8_t *cur;
453
454 /* type is a 5-bit field... */
455 if (type > 0x1f)
456 return -EINVAL;
457
458 if (length > 3)
459 header_len = 2;
460
461 cur = msgb_put(msg, length + header_len);
462
463 /* build the header without or with XL bit */
464 if (length <= 3) {
465 *cur++ = (type << 2) | (length & 3);
466 } else {
467 *cur++ = 0x80 | (type << 2) | (length >> 6);
468 *cur++ = (length << 2);
469 }
470
471 /* copy over the payload of the parameter*/
472 memcpy(cur, data, length);
473
474 return length + header_len;
475}
476
477static void rx_llc_xid(struct gprs_llc_lle *lle,
478 struct gprs_llc_hdr_parsed *gph)
479{
480 /* FIXME: 8.5.3.3: check if XID is invalid */
481 if (gph->is_cmd) {
482 /* FIXME: implement XID negotiation using SNDCP */
483 struct msgb *resp;
484 uint8_t *xid;
485 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
486 xid = msgb_put(resp, gph->data_len);
487 memcpy(xid, gph->data, gph->data_len);
488 gprs_llc_tx_xid(lle, resp, 0);
489 } else {
490 /* FIXME: if we had sent a XID reset, send
491 * LLGMM-RESET.conf to GMM */
492 /* FIXME: implement XID negotiation using SNDCP */
493 }
494}
495
Holger Hans Peter Freyther3a6fdcd2010-05-23 21:35:25 +0800496static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
Harald Welte9b455bf2010-03-14 15:45:01 +0800497{
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200498 DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x",
Harald Welte9b455bf2010-03-14 15:45:01 +0800499 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200500 gph->fcs);
Harald Welte9b455bf2010-03-14 15:45:01 +0800501
502 if (gph->cmd)
Harald Welteb61f4032010-05-18 12:31:50 +0200503 DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd));
Harald Welte9b455bf2010-03-14 15:45:01 +0800504
505 if (gph->data)
Harald Weltec6ecafe2010-05-13 19:47:50 +0200506 DEBUGPC(DLLC, "DATA ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800507
Harald Weltec6ecafe2010-05-13 19:47:50 +0200508 DEBUGPC(DLLC, "\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800509}
510static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
511 struct gprs_llc_lle *lle)
512{
513 switch (gph->cmd) {
514 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
515 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200516 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800517 /* start re-establishment (8.7.1) */
518 }
Harald Welte807a5d82010-06-01 11:53:01 +0200519 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800520 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200521 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800522 /* FIXME: process data */
523 break;
524 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
525 /* FIXME: Send UA */
526 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200527 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800528 break;
529 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200530 if (lle->state == GPRS_LLES_LOCAL_EST)
531 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800532 break;
533 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200534 if (lle->state == GPRS_LLES_LOCAL_EST)
535 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800536 break;
537 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
538 break;
539 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200540 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800541 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200542 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400543 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
544 LOGP(DLLC, LOGL_NOTICE,
545 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800546 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200547 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200548
549 /* HACK: non-standard recovery handling. If remote LLE
550 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800551 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200552 * and 'learn' the new sequence number */
553 if (gph->seq_tx != lle->vu_recv_last) {
554 lle->vu_recv_last = gph->seq_tx;
555 lle->vu_recv_duplicates = 0;
556 } else {
557 lle->vu_recv_duplicates++;
558 if (lle->vu_recv_duplicates < 3)
559 return -EIO;
560 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
561 "N(U=%d) after receiving %u duplicates\n",
562 lle->llme ? lle->llme->tlli : -1,
563 gph->seq_tx, lle->vu_recv_duplicates);
564 }
Harald Welteebabdea2010-06-01 18:28:10 +0200565 }
566 /* Increment the sequence number that we expect in the next frame */
567 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200568 /* Increment Overflow Counter */
569 if ((gph->seq_tx + 1) / 512)
570 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200571 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800572 }
573
574 return 0;
575}
576
577/* parse a GPRS LLC header, also check for invalid frames */
578static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800579 uint8_t *llc_hdr, int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800580{
Harald Welteeaa614c2010-05-02 11:26:34 +0200581 uint8_t *ctrl = llc_hdr+1;
Harald Welte9b455bf2010-03-14 15:45:01 +0800582
583 if (len <= CRC24_LENGTH)
584 return -EIO;
585
Harald Welte1b8827a2010-06-30 23:15:57 +0200586 ghp->crc_length = len - CRC24_LENGTH;
Harald Welte9b455bf2010-03-14 15:45:01 +0800587
588 ghp->ack_req = 0;
589
590 /* Section 5.5: FCS */
591 ghp->fcs = *(llc_hdr + len - 3);
592 ghp->fcs |= *(llc_hdr + len - 2) << 8;
593 ghp->fcs |= *(llc_hdr + len - 1) << 16;
594
595 /* Section 6.2.1: invalid PD field */
596 if (llc_hdr[0] & 0x80)
597 return -EIO;
598
599 /* This only works for the MS->SGSN direction */
600 if (llc_hdr[0] & 0x40)
601 ghp->is_cmd = 0;
602 else
603 ghp->is_cmd = 1;
604
605 ghp->sapi = llc_hdr[0] & 0xf;
606
607 /* Section 6.2.3: check for reserved SAPI */
608 switch (ghp->sapi) {
609 case 0:
610 case 4:
611 case 6:
612 case 0xa:
613 case 0xc:
614 case 0xd:
615 case 0xf:
616 return -EINVAL;
617 }
618
619 if ((ctrl[0] & 0x80) == 0) {
620 /* I (Information transfer + Supervisory) format */
Harald Welteeaa614c2010-05-02 11:26:34 +0200621 uint8_t k;
Harald Welte9b455bf2010-03-14 15:45:01 +0800622
623 ghp->data = ctrl + 3;
624
625 if (ctrl[0] & 0x40)
626 ghp->ack_req = 1;
627
628 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
629 ghp->seq_tx |= (ctrl[1] >> 4);
630
631 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
632 ghp->seq_rx |= (ctrl[2] >> 2);
633
634 switch (ctrl[2] & 0x03) {
635 case 0:
636 ghp->cmd = GPRS_LLC_RR;
637 break;
638 case 1:
639 ghp->cmd = GPRS_LLC_ACK;
640 break;
641 case 2:
642 ghp->cmd = GPRS_LLC_RNR;
643 break;
644 case 3:
645 ghp->cmd = GPRS_LLC_SACK;
646 k = ctrl[3] & 0x1f;
647 ghp->data += 1 + k;
648 break;
649 }
Harald Welte5658a1a2010-05-03 13:25:07 +0200650 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800651 } else if ((ctrl[0] & 0xc0) == 0x80) {
652 /* S (Supervisory) format */
653 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200654 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800655
656 if (ctrl[0] & 0x20)
657 ghp->ack_req = 1;
658 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
659 ghp->seq_rx |= (ctrl[1] >> 2);
660
661 switch (ctrl[1] & 0x03) {
662 case 0:
663 ghp->cmd = GPRS_LLC_RR;
664 break;
665 case 1:
666 ghp->cmd = GPRS_LLC_ACK;
667 break;
668 case 2:
669 ghp->cmd = GPRS_LLC_RNR;
670 break;
671 case 3:
672 ghp->cmd = GPRS_LLC_SACK;
673 break;
674 }
675 } else if ((ctrl[0] & 0xe0) == 0xc0) {
676 /* UI (Unconfirmed Inforamtion) format */
Harald Welte1ae09c72010-05-13 19:22:55 +0200677 ghp->cmd = GPRS_LLC_UI;
Harald Welte9b455bf2010-03-14 15:45:01 +0800678 ghp->data = ctrl + 2;
Harald Welte5658a1a2010-05-03 13:25:07 +0200679 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800680
681 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
682 ghp->seq_tx |= (ctrl[1] >> 2);
683 if (ctrl[1] & 0x02) {
684 ghp->is_encrypted = 1;
685 /* FIXME: encryption */
686 }
687 if (ctrl[1] & 0x01) {
688 /* FCS over hdr + all inf fields */
689 } else {
690 /* FCS over hdr + N202 octets (4) */
Harald Welte1b8827a2010-06-30 23:15:57 +0200691 if (ghp->crc_length > UI_HDR_LEN + N202)
692 ghp->crc_length = UI_HDR_LEN + N202;
Harald Welte9b455bf2010-03-14 15:45:01 +0800693 }
694 } else {
695 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
696 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200697 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800698
699 switch (ctrl[0] & 0xf) {
Harald Welte5658a1a2010-05-03 13:25:07 +0200700 case GPRS_LLC_U_NULL_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800701 ghp->cmd = GPRS_LLC_NULL;
702 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200703 case GPRS_LLC_U_DM_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800704 ghp->cmd = GPRS_LLC_DM;
705 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200706 case GPRS_LLC_U_DISC_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800707 ghp->cmd = GPRS_LLC_DISC;
708 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200709 case GPRS_LLC_U_UA_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800710 ghp->cmd = GPRS_LLC_UA;
711 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200712 case GPRS_LLC_U_SABM_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800713 ghp->cmd = GPRS_LLC_SABM;
714 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200715 case GPRS_LLC_U_FRMR_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800716 ghp->cmd = GPRS_LLC_FRMR;
717 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200718 case GPRS_LLC_U_XID:
Harald Welte9b455bf2010-03-14 15:45:01 +0800719 ghp->cmd = GPRS_LLC_XID;
Harald Welte5658a1a2010-05-03 13:25:07 +0200720 ghp->data = ctrl + 1;
721 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800722 break;
723 default:
724 return -EIO;
725 }
726 }
727
Harald Welte9b455bf2010-03-14 15:45:01 +0800728 /* FIXME: parse sack frame */
Harald Welte1ae09c72010-05-13 19:22:55 +0200729 if (ghp->cmd == GPRS_LLC_SACK) {
Harald Welte1b170d12010-05-13 19:49:06 +0200730 LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200731 return -EIO;
732 }
733
734 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800735}
736
Harald Weltea2665542010-05-02 09:28:11 +0200737/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800738int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
739{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200740 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200741 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800742 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200743 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200744 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800745
Harald Welte11d7c102010-05-02 11:54:55 +0200746 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
747
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800748 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800749 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800750 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200751 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200752 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200753 return rc;
754 }
755
Harald Welte807a5d82010-06-01 11:53:01 +0200756 switch (gprs_tlli_type(msgb_tlli(msg))) {
757 case TLLI_LOCAL:
758 case TLLI_FOREIGN:
759 case TLLI_RANDOM:
760 case TLLI_AUXILIARY:
761 break;
762 default:
763 LOGP(DLLC, LOGL_ERROR,
764 "Discarding frame with strange TLLI type\n");
765 break;
766 }
767
Harald Weltea2665542010-05-02 09:28:11 +0200768 /* find the LLC Entity for this TLLI+SAPI tuple */
769 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
Harald Welte1ae09c72010-05-13 19:22:55 +0200770
771 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
772 * except UID and XID frames with SAPI=1 */
Harald Welted764c062010-05-18 12:45:08 +0200773 if (!lle) {
774 if (llhp.sapi == GPRS_SAPI_GMM &&
775 (llhp.cmd == GPRS_LLC_XID || llhp.cmd == GPRS_LLC_UI)) {
Harald Welte807a5d82010-06-01 11:53:01 +0200776 struct gprs_llc_llme *llme;
Harald Welted764c062010-05-18 12:45:08 +0200777 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
Harald Welte807a5d82010-06-01 11:53:01 +0200778 llme = llme_alloc(msgb_tlli(msg));
Dieter Spaar1f447fb2011-07-27 23:38:46 +0200779 LOGP(DLLC, LOGL_DEBUG, "LLC RX: unknown TLLI 0x%08x, "
Harald Weltef0901f02010-12-26 10:39:26 +0100780 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte807a5d82010-06-01 11:53:01 +0200781 lle = &llme->lle[llhp.sapi];
Harald Welted764c062010-05-18 12:45:08 +0200782 } else {
783 LOGP(DLLC, LOGL_NOTICE,
784 "unknown TLLI/SAPI: Silently dropping\n");
785 return 0;
786 }
Harald Welte1ae09c72010-05-13 19:22:55 +0200787 }
Harald Weltea2665542010-05-02 09:28:11 +0200788
Harald Welted07b4f92010-06-30 23:07:59 +0200789 /* decrypt information field + FCS, if needed! */
790 if (llhp.is_encrypted) {
791 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
792 uint16_t crypt_len = llhp.data_len + 3;
793 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
794 uint32_t iv;
795 uint64_t kc = *(uint64_t *)&lle->llme->kc;
796 int rc, i;
797
798 if (lle->llme->algo == GPRS_ALGO_GEA0) {
799 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
800 "has no KC/Algo! Dropping.\n");
801 return 0;
802 }
803
804 iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx,
805 lle->oc_ui_recv);
806 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
807 kc, iv, GPRS_CIPH_MS2SGSN);
808 if (rc < 0) {
809 LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n",
810 rc);
811 return rc;
812 }
813
814 /* XOR the cipher output with the information field + FCS */
815 for (i = 0; i < crypt_len; i++)
816 *(llhp.data + i) ^= cipher_out[i];
817 } else {
818 if (lle->llme->algo != GPRS_ALGO_GEA0) {
819 LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC "
820 "that is supposed to be encrypted. Dropping.\n");
821 return 0;
822 }
823 }
824
825 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200826 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200827 if (llhp.fcs != llhp.fcs_calc) {
828 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
829 return -EIO;
830 }
831
Harald Welte10997d02010-05-03 12:28:12 +0200832 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200833 lle->llme->bvci = msgb_bvci(msg);
834 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200835
Harald Welte1ae09c72010-05-13 19:22:55 +0200836 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800837 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200838 if (rc < 0)
839 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800840
Harald Welte1ae09c72010-05-13 19:22:55 +0200841 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte9b455bf2010-03-14 15:45:01 +0800842 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200843 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800844 switch (llhp.sapi) {
845 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200846 /* send LL_UNITDATA_IND to GMM */
Harald Welte807a5d82010-06-01 11:53:01 +0200847 rc = gsm0408_gprs_rcvmsg(msg, lle->llme);
Harald Weltea2665542010-05-02 09:28:11 +0200848 break;
Harald Weltea2665542010-05-02 09:28:11 +0200849 case GPRS_SAPI_SNDCP3:
850 case GPRS_SAPI_SNDCP5:
851 case GPRS_SAPI_SNDCP9:
852 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +0200853 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
854 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
855 break;
Harald Weltea2665542010-05-02 09:28:11 +0200856 case GPRS_SAPI_SMS:
857 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +0200858 case GPRS_SAPI_TOM2:
859 case GPRS_SAPI_TOM8:
860 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200861 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200862 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200863 rc = -EINVAL;
864 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800865 }
866 }
867
Harald Weltea2665542010-05-02 09:28:11 +0200868 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800869}
Harald Welte807a5d82010-06-01 11:53:01 +0200870
871/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
872int gprs_llgmm_assign(struct gprs_llc_llme *llme,
873 uint32_t old_tlli, uint32_t new_tlli,
874 enum gprs_ciph_algo alg, const uint8_t *kc)
875{
876 unsigned int i;
877
Harald Welted07b4f92010-06-30 23:07:59 +0200878 /* Update the crypto parameters */
Harald Welted07b4f92010-06-30 23:07:59 +0200879 llme->algo = alg;
Harald Welte3e2e1592010-06-30 23:19:23 +0200880 if (alg != GPRS_ALGO_GEA0)
881 memcpy(llme->kc, kc, sizeof(llme->kc));
Harald Welted07b4f92010-06-30 23:07:59 +0200882
Harald Welte807a5d82010-06-01 11:53:01 +0200883 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
884 /* TLLI Assignment 8.3.1 */
885 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
886 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
887 * old is unassigned. Only TLLI new shall be accepted when
888 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +0200889 if (llme->old_tlli != 0xffffffff) {
890 llme->old_tlli = 0xffffffff;
891 llme->tlli = new_tlli;
892 } else {
893 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
894 * TLLI assignmemt according to 8.3.1 */
895 llme->old_tlli = 0xffffffff;
896 llme->tlli = new_tlli;
897 llme->state = GPRS_LLMS_ASSIGNED;
898 /* 8.5.3.1 For all LLE's */
899 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
900 struct gprs_llc_lle *l = &llme->lle[i];
901 l->vu_send = l->vu_recv = 0;
902 l->retrans_ctr = 0;
903 l->state = GPRS_LLES_ASSIGNED_ADM;
904 /* FIXME Set parameters according to table 9 */
905 }
Harald Welte807a5d82010-06-01 11:53:01 +0200906 }
907 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
908 /* TLLI Change 8.3.2 */
909 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +0200910 * (re)transmitting. Accept both Old and New on Rx */
Harald Welte807a5d82010-06-01 11:53:01 +0200911 llme->old_tlli = llme->tlli;
912 llme->tlli = new_tlli;
913 llme->state = GPRS_LLMS_ASSIGNED;
914 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
915 /* TLLI Unassignment 8.3.3) */
916 llme->tlli = llme->old_tlli = 0;
917 llme->state = GPRS_LLMS_UNASSIGNED;
918 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
919 struct gprs_llc_lle *l = &llme->lle[i];
920 l->state = GPRS_LLES_UNASSIGNED;
921 }
Harald Weltef7fef482010-06-28 22:18:26 +0200922 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +0200923 } else
924 return -EINVAL;
925
926 return 0;
927}
Harald Welte496aee42010-06-30 19:59:55 +0200928
Harald Welte0c1a3032011-10-16 18:49:05 +0200929/* Chapter 7.2.1.2 LLGMM-RESET.req */
930int gprs_llgmm_reset(struct gprs_llc_llme *llme)
931{
932 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
933 int random = rand();
934
935 /* First XID component must be RESET */
936 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
937 /* randomly select new IOV-UI */
Harald Welte066a0f52011-10-16 18:59:20 +0200938 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random);
Harald Welte0c1a3032011-10-16 18:49:05 +0200939
940 /* FIXME: Start T200, wait for XID response */
941 return gprs_llc_tx_xid(&llme->lle[1], msg, 1);
942}
943
Harald Welte496aee42010-06-30 19:59:55 +0200944int gprs_llc_init(const char *cipher_plugin_path)
945{
946 return gprs_cipher_load(cipher_plugin_path);
947}