blob: c1327b6cdc0bf21ca4cbc2a9b7380a5081b6ab91 [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
46 dup.tlli = NULL;
47 dup.imsi = mmctx->imsi;
48 dup.drx_parms = mmctx->drx_parms;
49 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
50 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
51 dup.pdu_lifetime = 1000; /* centi-seconds */
52 memcpy(&dup.qos_profile, qos_profile_default,
53 sizeof(qos_profile_default));
54
55 return gprs_bssgp_tx_dl_ud(msg, 1000, &dup);
56}
57
58
Harald Welte1d9d9442010-06-03 07:11:04 +020059/* Section 8.9.9 LLC layer parameter default values */
60static const struct gprs_llc_params llc_default_params[] = {
61 [1] = {
62 .t200_201 = 5,
63 .n200 = 3,
64 .n201_u = 400,
65 },
66 [2] = {
67 .t200_201 = 5,
68 .n200 = 3,
69 .n201_u = 270,
70 },
71 [3] = {
72 .iov_i_exp = 27,
73 .t200_201 = 5,
74 .n200 = 3,
75 .n201_u = 500,
76 .n201_i = 1503,
77 .mD = 1520,
78 .mU = 1520,
79 .kD = 16,
80 .kU = 16,
81 },
82 [5] = {
83 .iov_i_exp = 27,
84 .t200_201 = 10,
85 .n200 = 3,
86 .n201_u = 500,
87 .n201_i = 1503,
88 .mD = 760,
89 .mU = 760,
90 .kD = 8,
91 .kU = 8,
92 },
93 [7] = {
94 .t200_201 = 20,
95 .n200 = 3,
96 .n201_u = 270,
97 },
98 [8] = {
99 .t200_201 = 20,
100 .n200 = 3,
101 .n201_u = 270,
102 },
103 [9] = {
104 .iov_i_exp = 27,
105 .t200_201 = 20,
106 .n200 = 3,
107 .n201_u = 500,
108 .n201_i = 1503,
109 .mD = 380,
110 .mU = 380,
111 .kD = 4,
112 .kU = 4,
113 },
114 [11] = {
115 .iov_i_exp = 27,
116 .t200_201 = 40,
117 .n200 = 3,
118 .n201_u = 500,
119 .n201_i = 1503,
120 .mD = 190,
121 .mU = 190,
122 .kD = 2,
123 .kU = 2,
124 },
125};
126
Harald Welte807a5d82010-06-01 11:53:01 +0200127LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200128void *llc_tall_ctx;
129
Harald Weltef0901f02010-12-26 10:39:26 +0100130/* If the TLLI is foreign, return its local version */
131static inline uint32_t tlli_foreign2local(uint32_t tlli)
132{
133 uint32_t new_tlli;
134
135 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
136 new_tlli = tlli | 0x40000000;
137 DEBUGP(DLLC, "TLLI 0x%08x is foreign, converting to "
138 "local TLLI 0x%08x\n", tlli, new_tlli);
139 } else
140 new_tlli = tlli;
141
142 return new_tlli;
143}
144
Harald Weltea2665542010-05-02 09:28:11 +0200145/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Harald Welte1d9d9442010-06-03 07:11:04 +0200146static struct gprs_llc_lle *lle_by_tlli_sapi(uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200147{
Harald Welte807a5d82010-06-01 11:53:01 +0200148 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200149
Harald Weltef0901f02010-12-26 10:39:26 +0100150 tlli = tlli_foreign2local(tlli);
151
Harald Welte807a5d82010-06-01 11:53:01 +0200152 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
153 if (llme->tlli == tlli || llme->old_tlli == tlli)
154 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200155 }
156 return NULL;
157}
158
Harald Welte1d9d9442010-06-03 07:11:04 +0200159static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200160{
Harald Welte807a5d82010-06-01 11:53:01 +0200161 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200162
Harald Welte807a5d82010-06-01 11:53:01 +0200163 lle->llme = llme;
164 lle->sapi = sapi;
165 lle->state = GPRS_LLES_UNASSIGNED;
166
Harald Welte1d9d9442010-06-03 07:11:04 +0200167 /* Initialize according to parameters */
168 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200169}
170
171static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
172{
173 struct gprs_llc_llme *llme;
174 uint32_t i;
175
176 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
177 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200178 return NULL;
179
Harald Welte807a5d82010-06-01 11:53:01 +0200180 llme->tlli = tlli;
Harald Welte875840c2010-07-01 11:54:31 +0200181 llme->old_tlli = 0xffffffff;
Harald Welte807a5d82010-06-01 11:53:01 +0200182 llme->state = GPRS_LLMS_UNASSIGNED;
Harald Weltea2665542010-05-02 09:28:11 +0200183
Harald Welte807a5d82010-06-01 11:53:01 +0200184 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
185 lle_init(llme, i);
186
187 llist_add(&llme->list, &gprs_llc_llmes);
188
189 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200190}
191
Harald Weltef7fef482010-06-28 22:18:26 +0200192static void llme_free(struct gprs_llc_llme *llme)
193{
194 llist_del(&llme->list);
195 talloc_free(llme);
196}
197
Harald Welte9b455bf2010-03-14 15:45:01 +0800198enum gprs_llc_cmd {
199 GPRS_LLC_NULL,
200 GPRS_LLC_RR,
201 GPRS_LLC_ACK,
202 GPRS_LLC_RNR,
203 GPRS_LLC_SACK,
204 GPRS_LLC_DM,
205 GPRS_LLC_DISC,
206 GPRS_LLC_UA,
207 GPRS_LLC_SABM,
208 GPRS_LLC_FRMR,
209 GPRS_LLC_XID,
Harald Welte1ae09c72010-05-13 19:22:55 +0200210 GPRS_LLC_UI,
Harald Welte9b455bf2010-03-14 15:45:01 +0800211};
212
Harald Welteb61f4032010-05-18 12:31:50 +0200213static const struct value_string llc_cmd_strs[] = {
214 { GPRS_LLC_NULL, "NULL" },
215 { GPRS_LLC_RR, "RR" },
216 { GPRS_LLC_ACK, "ACK" },
217 { GPRS_LLC_RNR, "RNR" },
218 { GPRS_LLC_SACK, "SACK" },
219 { GPRS_LLC_DM, "DM" },
220 { GPRS_LLC_DISC, "DISC" },
221 { GPRS_LLC_UA, "UA" },
222 { GPRS_LLC_SABM, "SABM" },
223 { GPRS_LLC_FRMR, "FRMR" },
224 { GPRS_LLC_XID, "XID" },
225 { GPRS_LLC_UI, "UI" },
226 { 0, NULL }
227};
228
Harald Welte9b455bf2010-03-14 15:45:01 +0800229struct gprs_llc_hdr_parsed {
Harald Welteeaa614c2010-05-02 11:26:34 +0200230 uint8_t sapi;
231 uint8_t is_cmd:1,
Harald Welte9b455bf2010-03-14 15:45:01 +0800232 ack_req:1,
233 is_encrypted:1;
Harald Welteeaa614c2010-05-02 11:26:34 +0200234 uint32_t seq_rx;
235 uint32_t seq_tx;
236 uint32_t fcs;
237 uint32_t fcs_calc;
238 uint8_t *data;
Harald Welte5658a1a2010-05-03 13:25:07 +0200239 uint16_t data_len;
Harald Welte1b8827a2010-06-30 23:15:57 +0200240 uint16_t crc_length;
Harald Welte9b455bf2010-03-14 15:45:01 +0800241 enum gprs_llc_cmd cmd;
242};
243
244#define LLC_ALLOC_SIZE 16384
245#define UI_HDR_LEN 3
246#define N202 4
247#define CRC24_LENGTH 3
248
Harald Welteeaa614c2010-05-02 11:26:34 +0200249static int gprs_llc_fcs(uint8_t *data, unsigned int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800250{
Harald Welteeaa614c2010-05-02 11:26:34 +0200251 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800252
253 fcs_calc = crc24_calc(INIT_CRC24, data, len);
254 fcs_calc = ~fcs_calc;
255 fcs_calc &= 0xffffff;
256
257 return fcs_calc;
258}
259
Harald Welte9b455bf2010-03-14 15:45:01 +0800260static void t200_expired(void *data)
261{
262 struct gprs_llc_lle *lle = data;
263
264 /* 8.5.1.3: Expiry of T200 */
265
Harald Welte1d9d9442010-06-03 07:11:04 +0200266 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800267 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200268 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800269 }
270
271 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200272 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200273 /* FIXME: retransmit SABM */
274 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800275 lle->retrans_ctr++;
276 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200277 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200278 /* FIXME: retransmit DISC */
279 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800280 lle->retrans_ctr++;
281 break;
282 }
283
284}
285
286static void t201_expired(void *data)
287{
288 struct gprs_llc_lle *lle = data;
289
Harald Welte1d9d9442010-06-03 07:11:04 +0200290 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200291 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
292 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800293 lle->retrans_ctr++;
294 }
295}
296
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 Weltefaa70ff2012-06-17 09:31:16 +0800331 return _bssgp_tx_dl_ud(msg, 1000, 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 */
360 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
Harald Welte807a5d82010-06-01 11:53:01 +0200361 if (!lle) {
362 struct gprs_llc_llme *llme;
Harald Weltef0901f02010-12-26 10:39:26 +0100363 LOGP(DLLC, LOGL_ERROR, "LLC TX: unknown TLLI 0x%08x, "
364 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte807a5d82010-06-01 11:53:01 +0200365 llme = llme_alloc(msgb_tlli(msg));
366 lle = &llme->lle[sapi];
367 }
Harald Welte1d9d9442010-06-03 07:11:04 +0200368
369 if (msg->len > lle->params.n201_u) {
370 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
371 msg->len, lle->params.n201_u);
372 return -EFBIG;
373 }
374
Harald Weltee6afd602010-05-02 11:19:37 +0200375 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200376 lle->llme->bvci = msgb_bvci(msg);
377 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200378
Harald Welted07b4f92010-06-30 23:07:59 +0200379 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200380 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200381 oc = lle->oc_ui_send;
382 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200383 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200384 /* Increment Overflow Counter, if needed */
385 if ((lle->vu_send + 1) / 512)
386 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200387
Harald Welte9b455bf2010-03-14 15:45:01 +0800388 /* Address Field */
389 addr = sapi & 0xf;
390 if (command)
391 addr |= 0x40;
392
393 /* Control Field */
394 ctrl[0] = 0xc0;
395 ctrl[0] |= nu >> 6;
396 ctrl[1] = (nu << 2) & 0xfc;
397 ctrl[1] |= 0x01; /* Protected Mode */
398
399 /* prepend LLC UI header */
400 llch = msgb_push(msg, 3);
401 llch[0] = addr;
402 llch[1] = ctrl[0];
403 llch[2] = ctrl[1];
404
405 /* append FCS to end of frame */
406 fcs = msgb_put(msg, 3);
407 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
408 fcs[0] = fcs_calc & 0xff;
409 fcs[1] = (fcs_calc >> 8) & 0xff;
410 fcs[2] = (fcs_calc >> 16) & 0xff;
411
Harald Welted07b4f92010-06-30 23:07:59 +0200412 /* encrypt information field + FCS, if needed! */
413 if (lle->llme->algo != GPRS_ALGO_GEA0) {
414 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
415 uint16_t crypt_len = (fcs + 3) - (llch + 3);
416 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
417 uint32_t iv;
418 int rc, i;
419 uint64_t kc = *(uint64_t *)&lle->llme->kc;
420
421 /* Compute the 'Input' Paraemeter */
422 iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc);
423
424 /* Compute the keystream that we need to XOR with the data */
425 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
426 kc, iv, GPRS_CIPH_SGSN2MS);
427 if (rc < 0) {
428 LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc);
429 return rc;
430 }
431
432 /* XOR the cipher output with the information field + FCS */
433 for (i = 0; i < crypt_len; i++)
434 *(llch + 3 + i) ^= cipher_out[i];
435
436 /* Mark frame as encrypted */
437 ctrl[1] |= 0x02;
438 }
439
Harald Weltee6afd602010-05-02 11:19:37 +0200440 /* Identifiers passed down: (BVCI, NSEI) */
441
Harald Welte1ae09c72010-05-13 19:22:55 +0200442 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800443 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800444}
445
Harald Welte0c1a3032011-10-16 18:49:05 +0200446/* According to 6.4.1.6 / Figure 11 */
447static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data)
448{
449 uint8_t header_len = 1;
450 uint8_t *cur;
451
452 /* type is a 5-bit field... */
453 if (type > 0x1f)
454 return -EINVAL;
455
456 if (length > 3)
457 header_len = 2;
458
459 cur = msgb_put(msg, length + header_len);
460
461 /* build the header without or with XL bit */
462 if (length <= 3) {
463 *cur++ = (type << 2) | (length & 3);
464 } else {
465 *cur++ = 0x80 | (type << 2) | (length >> 6);
466 *cur++ = (length << 2);
467 }
468
469 /* copy over the payload of the parameter*/
470 memcpy(cur, data, length);
471
472 return length + header_len;
473}
474
475static void rx_llc_xid(struct gprs_llc_lle *lle,
476 struct gprs_llc_hdr_parsed *gph)
477{
478 /* FIXME: 8.5.3.3: check if XID is invalid */
479 if (gph->is_cmd) {
480 /* FIXME: implement XID negotiation using SNDCP */
481 struct msgb *resp;
482 uint8_t *xid;
483 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
484 xid = msgb_put(resp, gph->data_len);
485 memcpy(xid, gph->data, gph->data_len);
486 gprs_llc_tx_xid(lle, resp, 0);
487 } else {
488 /* FIXME: if we had sent a XID reset, send
489 * LLGMM-RESET.conf to GMM */
490 /* FIXME: implement XID negotiation using SNDCP */
491 }
492}
493
Holger Hans Peter Freyther3a6fdcd2010-05-23 21:35:25 +0800494static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
Harald Welte9b455bf2010-03-14 15:45:01 +0800495{
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200496 DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x",
Harald Welte9b455bf2010-03-14 15:45:01 +0800497 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200498 gph->fcs);
Harald Welte9b455bf2010-03-14 15:45:01 +0800499
500 if (gph->cmd)
Harald Welteb61f4032010-05-18 12:31:50 +0200501 DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd));
Harald Welte9b455bf2010-03-14 15:45:01 +0800502
503 if (gph->data)
Harald Weltec6ecafe2010-05-13 19:47:50 +0200504 DEBUGPC(DLLC, "DATA ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800505
Harald Weltec6ecafe2010-05-13 19:47:50 +0200506 DEBUGPC(DLLC, "\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800507}
508static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
509 struct gprs_llc_lle *lle)
510{
511 switch (gph->cmd) {
512 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
513 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200514 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800515 /* start re-establishment (8.7.1) */
516 }
Harald Welte807a5d82010-06-01 11:53:01 +0200517 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800518 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200519 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800520 /* FIXME: process data */
521 break;
522 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
523 /* FIXME: Send UA */
524 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200525 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800526 break;
527 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200528 if (lle->state == GPRS_LLES_LOCAL_EST)
529 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800530 break;
531 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200532 if (lle->state == GPRS_LLES_LOCAL_EST)
533 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800534 break;
535 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
536 break;
537 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200538 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800539 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200540 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400541 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
542 LOGP(DLLC, LOGL_NOTICE,
543 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800544 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200545 gph->seq_tx, lle->vu_recv);
546 return -EIO;
547 }
548 /* Increment the sequence number that we expect in the next frame */
549 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200550 /* Increment Overflow Counter */
551 if ((gph->seq_tx + 1) / 512)
552 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200553 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800554 }
555
556 return 0;
557}
558
559/* parse a GPRS LLC header, also check for invalid frames */
560static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800561 uint8_t *llc_hdr, int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800562{
Harald Welteeaa614c2010-05-02 11:26:34 +0200563 uint8_t *ctrl = llc_hdr+1;
Harald Welte9b455bf2010-03-14 15:45:01 +0800564
565 if (len <= CRC24_LENGTH)
566 return -EIO;
567
Harald Welte1b8827a2010-06-30 23:15:57 +0200568 ghp->crc_length = len - CRC24_LENGTH;
Harald Welte9b455bf2010-03-14 15:45:01 +0800569
570 ghp->ack_req = 0;
571
572 /* Section 5.5: FCS */
573 ghp->fcs = *(llc_hdr + len - 3);
574 ghp->fcs |= *(llc_hdr + len - 2) << 8;
575 ghp->fcs |= *(llc_hdr + len - 1) << 16;
576
577 /* Section 6.2.1: invalid PD field */
578 if (llc_hdr[0] & 0x80)
579 return -EIO;
580
581 /* This only works for the MS->SGSN direction */
582 if (llc_hdr[0] & 0x40)
583 ghp->is_cmd = 0;
584 else
585 ghp->is_cmd = 1;
586
587 ghp->sapi = llc_hdr[0] & 0xf;
588
589 /* Section 6.2.3: check for reserved SAPI */
590 switch (ghp->sapi) {
591 case 0:
592 case 4:
593 case 6:
594 case 0xa:
595 case 0xc:
596 case 0xd:
597 case 0xf:
598 return -EINVAL;
599 }
600
601 if ((ctrl[0] & 0x80) == 0) {
602 /* I (Information transfer + Supervisory) format */
Harald Welteeaa614c2010-05-02 11:26:34 +0200603 uint8_t k;
Harald Welte9b455bf2010-03-14 15:45:01 +0800604
605 ghp->data = ctrl + 3;
606
607 if (ctrl[0] & 0x40)
608 ghp->ack_req = 1;
609
610 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
611 ghp->seq_tx |= (ctrl[1] >> 4);
612
613 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
614 ghp->seq_rx |= (ctrl[2] >> 2);
615
616 switch (ctrl[2] & 0x03) {
617 case 0:
618 ghp->cmd = GPRS_LLC_RR;
619 break;
620 case 1:
621 ghp->cmd = GPRS_LLC_ACK;
622 break;
623 case 2:
624 ghp->cmd = GPRS_LLC_RNR;
625 break;
626 case 3:
627 ghp->cmd = GPRS_LLC_SACK;
628 k = ctrl[3] & 0x1f;
629 ghp->data += 1 + k;
630 break;
631 }
Harald Welte5658a1a2010-05-03 13:25:07 +0200632 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800633 } else if ((ctrl[0] & 0xc0) == 0x80) {
634 /* S (Supervisory) format */
635 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200636 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800637
638 if (ctrl[0] & 0x20)
639 ghp->ack_req = 1;
640 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
641 ghp->seq_rx |= (ctrl[1] >> 2);
642
643 switch (ctrl[1] & 0x03) {
644 case 0:
645 ghp->cmd = GPRS_LLC_RR;
646 break;
647 case 1:
648 ghp->cmd = GPRS_LLC_ACK;
649 break;
650 case 2:
651 ghp->cmd = GPRS_LLC_RNR;
652 break;
653 case 3:
654 ghp->cmd = GPRS_LLC_SACK;
655 break;
656 }
657 } else if ((ctrl[0] & 0xe0) == 0xc0) {
658 /* UI (Unconfirmed Inforamtion) format */
Harald Welte1ae09c72010-05-13 19:22:55 +0200659 ghp->cmd = GPRS_LLC_UI;
Harald Welte9b455bf2010-03-14 15:45:01 +0800660 ghp->data = ctrl + 2;
Harald Welte5658a1a2010-05-03 13:25:07 +0200661 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800662
663 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
664 ghp->seq_tx |= (ctrl[1] >> 2);
665 if (ctrl[1] & 0x02) {
666 ghp->is_encrypted = 1;
667 /* FIXME: encryption */
668 }
669 if (ctrl[1] & 0x01) {
670 /* FCS over hdr + all inf fields */
671 } else {
672 /* FCS over hdr + N202 octets (4) */
Harald Welte1b8827a2010-06-30 23:15:57 +0200673 if (ghp->crc_length > UI_HDR_LEN + N202)
674 ghp->crc_length = UI_HDR_LEN + N202;
Harald Welte9b455bf2010-03-14 15:45:01 +0800675 }
676 } else {
677 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
678 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200679 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800680
681 switch (ctrl[0] & 0xf) {
Harald Welte5658a1a2010-05-03 13:25:07 +0200682 case GPRS_LLC_U_NULL_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800683 ghp->cmd = GPRS_LLC_NULL;
684 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200685 case GPRS_LLC_U_DM_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800686 ghp->cmd = GPRS_LLC_DM;
687 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200688 case GPRS_LLC_U_DISC_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800689 ghp->cmd = GPRS_LLC_DISC;
690 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200691 case GPRS_LLC_U_UA_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800692 ghp->cmd = GPRS_LLC_UA;
693 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200694 case GPRS_LLC_U_SABM_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800695 ghp->cmd = GPRS_LLC_SABM;
696 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200697 case GPRS_LLC_U_FRMR_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800698 ghp->cmd = GPRS_LLC_FRMR;
699 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200700 case GPRS_LLC_U_XID:
Harald Welte9b455bf2010-03-14 15:45:01 +0800701 ghp->cmd = GPRS_LLC_XID;
Harald Welte5658a1a2010-05-03 13:25:07 +0200702 ghp->data = ctrl + 1;
703 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800704 break;
705 default:
706 return -EIO;
707 }
708 }
709
Harald Welte9b455bf2010-03-14 15:45:01 +0800710 /* FIXME: parse sack frame */
Harald Welte1ae09c72010-05-13 19:22:55 +0200711 if (ghp->cmd == GPRS_LLC_SACK) {
Harald Welte1b170d12010-05-13 19:49:06 +0200712 LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200713 return -EIO;
714 }
715
716 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800717}
718
Harald Weltea2665542010-05-02 09:28:11 +0200719/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800720int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
721{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200722 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200723 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800724 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200725 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200726 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800727
Harald Welte11d7c102010-05-02 11:54:55 +0200728 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
729
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800730 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800731 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800732 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200733 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200734 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200735 return rc;
736 }
737
Harald Welte807a5d82010-06-01 11:53:01 +0200738 switch (gprs_tlli_type(msgb_tlli(msg))) {
739 case TLLI_LOCAL:
740 case TLLI_FOREIGN:
741 case TLLI_RANDOM:
742 case TLLI_AUXILIARY:
743 break;
744 default:
745 LOGP(DLLC, LOGL_ERROR,
746 "Discarding frame with strange TLLI type\n");
747 break;
748 }
749
Harald Weltea2665542010-05-02 09:28:11 +0200750 /* find the LLC Entity for this TLLI+SAPI tuple */
751 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
Harald Welte1ae09c72010-05-13 19:22:55 +0200752
753 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
754 * except UID and XID frames with SAPI=1 */
Harald Welted764c062010-05-18 12:45:08 +0200755 if (!lle) {
756 if (llhp.sapi == GPRS_SAPI_GMM &&
757 (llhp.cmd == GPRS_LLC_XID || llhp.cmd == GPRS_LLC_UI)) {
Harald Welte807a5d82010-06-01 11:53:01 +0200758 struct gprs_llc_llme *llme;
Harald Welted764c062010-05-18 12:45:08 +0200759 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
Harald Welte807a5d82010-06-01 11:53:01 +0200760 llme = llme_alloc(msgb_tlli(msg));
Dieter Spaar1f447fb2011-07-27 23:38:46 +0200761 LOGP(DLLC, LOGL_DEBUG, "LLC RX: unknown TLLI 0x%08x, "
Harald Weltef0901f02010-12-26 10:39:26 +0100762 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte807a5d82010-06-01 11:53:01 +0200763 lle = &llme->lle[llhp.sapi];
Harald Welted764c062010-05-18 12:45:08 +0200764 } else {
765 LOGP(DLLC, LOGL_NOTICE,
766 "unknown TLLI/SAPI: Silently dropping\n");
767 return 0;
768 }
Harald Welte1ae09c72010-05-13 19:22:55 +0200769 }
Harald Weltea2665542010-05-02 09:28:11 +0200770
Harald Welted07b4f92010-06-30 23:07:59 +0200771 /* decrypt information field + FCS, if needed! */
772 if (llhp.is_encrypted) {
773 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
774 uint16_t crypt_len = llhp.data_len + 3;
775 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
776 uint32_t iv;
777 uint64_t kc = *(uint64_t *)&lle->llme->kc;
778 int rc, i;
779
780 if (lle->llme->algo == GPRS_ALGO_GEA0) {
781 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
782 "has no KC/Algo! Dropping.\n");
783 return 0;
784 }
785
786 iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx,
787 lle->oc_ui_recv);
788 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
789 kc, iv, GPRS_CIPH_MS2SGSN);
790 if (rc < 0) {
791 LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n",
792 rc);
793 return rc;
794 }
795
796 /* XOR the cipher output with the information field + FCS */
797 for (i = 0; i < crypt_len; i++)
798 *(llhp.data + i) ^= cipher_out[i];
799 } else {
800 if (lle->llme->algo != GPRS_ALGO_GEA0) {
801 LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC "
802 "that is supposed to be encrypted. Dropping.\n");
803 return 0;
804 }
805 }
806
807 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200808 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200809 if (llhp.fcs != llhp.fcs_calc) {
810 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
811 return -EIO;
812 }
813
Harald Welte10997d02010-05-03 12:28:12 +0200814 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200815 lle->llme->bvci = msgb_bvci(msg);
816 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200817
Harald Welte1ae09c72010-05-13 19:22:55 +0200818 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800819 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200820 if (rc < 0)
821 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800822
Harald Welte1ae09c72010-05-13 19:22:55 +0200823 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte9b455bf2010-03-14 15:45:01 +0800824 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200825 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800826 switch (llhp.sapi) {
827 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200828 /* send LL_UNITDATA_IND to GMM */
Harald Welte807a5d82010-06-01 11:53:01 +0200829 rc = gsm0408_gprs_rcvmsg(msg, lle->llme);
Harald Weltea2665542010-05-02 09:28:11 +0200830 break;
Harald Weltea2665542010-05-02 09:28:11 +0200831 case GPRS_SAPI_SNDCP3:
832 case GPRS_SAPI_SNDCP5:
833 case GPRS_SAPI_SNDCP9:
834 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +0200835 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
836 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
837 break;
Harald Weltea2665542010-05-02 09:28:11 +0200838 case GPRS_SAPI_SMS:
839 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +0200840 case GPRS_SAPI_TOM2:
841 case GPRS_SAPI_TOM8:
842 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200843 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200844 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200845 rc = -EINVAL;
846 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800847 }
848 }
849
Harald Weltea2665542010-05-02 09:28:11 +0200850 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800851}
Harald Welte807a5d82010-06-01 11:53:01 +0200852
853/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
854int gprs_llgmm_assign(struct gprs_llc_llme *llme,
855 uint32_t old_tlli, uint32_t new_tlli,
856 enum gprs_ciph_algo alg, const uint8_t *kc)
857{
858 unsigned int i;
859
Harald Welted07b4f92010-06-30 23:07:59 +0200860 /* Update the crypto parameters */
Harald Welted07b4f92010-06-30 23:07:59 +0200861 llme->algo = alg;
Harald Welte3e2e1592010-06-30 23:19:23 +0200862 if (alg != GPRS_ALGO_GEA0)
863 memcpy(llme->kc, kc, sizeof(llme->kc));
Harald Welted07b4f92010-06-30 23:07:59 +0200864
Harald Welte807a5d82010-06-01 11:53:01 +0200865 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
866 /* TLLI Assignment 8.3.1 */
867 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
868 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
869 * old is unassigned. Only TLLI new shall be accepted when
870 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +0200871 if (llme->old_tlli != 0xffffffff) {
872 llme->old_tlli = 0xffffffff;
873 llme->tlli = new_tlli;
874 } else {
875 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
876 * TLLI assignmemt according to 8.3.1 */
877 llme->old_tlli = 0xffffffff;
878 llme->tlli = new_tlli;
879 llme->state = GPRS_LLMS_ASSIGNED;
880 /* 8.5.3.1 For all LLE's */
881 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
882 struct gprs_llc_lle *l = &llme->lle[i];
883 l->vu_send = l->vu_recv = 0;
884 l->retrans_ctr = 0;
885 l->state = GPRS_LLES_ASSIGNED_ADM;
886 /* FIXME Set parameters according to table 9 */
887 }
Harald Welte807a5d82010-06-01 11:53:01 +0200888 }
889 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
890 /* TLLI Change 8.3.2 */
891 /* Both TLLI Old and TLLI New are assigned; use New when
892 * (re)transmitting. Accept toth Old and New on Rx */
893 llme->old_tlli = llme->tlli;
894 llme->tlli = new_tlli;
895 llme->state = GPRS_LLMS_ASSIGNED;
896 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
897 /* TLLI Unassignment 8.3.3) */
898 llme->tlli = llme->old_tlli = 0;
899 llme->state = GPRS_LLMS_UNASSIGNED;
900 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
901 struct gprs_llc_lle *l = &llme->lle[i];
902 l->state = GPRS_LLES_UNASSIGNED;
903 }
Harald Weltef7fef482010-06-28 22:18:26 +0200904 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +0200905 } else
906 return -EINVAL;
907
908 return 0;
909}
Harald Welte496aee42010-06-30 19:59:55 +0200910
Harald Welte0c1a3032011-10-16 18:49:05 +0200911/* Chapter 7.2.1.2 LLGMM-RESET.req */
912int gprs_llgmm_reset(struct gprs_llc_llme *llme)
913{
914 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
915 int random = rand();
916
917 /* First XID component must be RESET */
918 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
919 /* randomly select new IOV-UI */
Harald Welte066a0f52011-10-16 18:59:20 +0200920 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random);
Harald Welte0c1a3032011-10-16 18:49:05 +0200921
922 /* FIXME: Start T200, wait for XID response */
923 return gprs_llc_tx_xid(&llme->lle[1], msg, 1);
924}
925
Harald Welte496aee42010-06-30 19:59:55 +0200926int gprs_llc_init(const char *cipher_plugin_path)
927{
928 return gprs_cipher_load(cipher_plugin_path);
929}