blob: c705c87b5e465fcaa2c1b768d3a18044c6e8d473 [file] [log] [blame]
Harald Welte75bb8202010-03-14 15:45:01 +08001/* GPRS LLC protocol implementation as per 3GPP TS 04.64 */
2
Harald Weltec14e5822010-05-02 09:28:11 +02003/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte75bb8202010-03-14 15:45:01 +08004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte75bb8202010-03-14 15:45:01 +080010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte0e3e88e2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte75bb8202010-03-14 15:45:01 +080016 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte75bb8202010-03-14 15:45:01 +080019 *
20 */
21
22#include <errno.h>
Harald Welted85d9a92010-05-02 11:26:34 +020023#include <stdint.h>
Harald Welte75bb8202010-03-14 15:45:01 +080024
Pablo Neira Ayusodd5fff42011-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 Weltecfb6b282012-06-16 14:59:56 +080029#include <osmocom/gprs/gprs_bssgp.h>
Harald Weltec14e5822010-05-02 09:28:11 +020030
31#include <openbsc/gsm_data.h>
32#include <openbsc/debug.h>
Harald Welte75871c72010-06-01 11:53:01 +020033#include <openbsc/gprs_sgsn.h>
34#include <openbsc/gprs_gmm.h>
Harald Welte75bb8202010-03-14 15:45:01 +080035#include <openbsc/gprs_llc.h>
36#include <openbsc/crc24.h>
Holger Hans Peter Freytherf9839f62011-10-14 23:42:13 +020037#include <openbsc/sgsn.h>
Harald Welte75bb8202010-03-14 15:45:01 +080038
Harald Welte1e8b9462012-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;
Harald Welte1e8b9462012-06-17 09:31:16 +080051 memcpy(&dup.qos_profile, qos_profile_default,
52 sizeof(qos_profile_default));
53
Harald Weltea084f122012-06-17 13:04:02 +080054 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Welte1e8b9462012-06-17 09:31:16 +080055}
56
57
Harald Weltedf270ad2010-06-03 07:11:04 +020058/* Section 8.9.9 LLC layer parameter default values */
59static const struct gprs_llc_params llc_default_params[] = {
60 [1] = {
61 .t200_201 = 5,
62 .n200 = 3,
63 .n201_u = 400,
64 },
65 [2] = {
66 .t200_201 = 5,
67 .n200 = 3,
68 .n201_u = 270,
69 },
70 [3] = {
71 .iov_i_exp = 27,
72 .t200_201 = 5,
73 .n200 = 3,
74 .n201_u = 500,
75 .n201_i = 1503,
76 .mD = 1520,
77 .mU = 1520,
78 .kD = 16,
79 .kU = 16,
80 },
81 [5] = {
82 .iov_i_exp = 27,
83 .t200_201 = 10,
84 .n200 = 3,
85 .n201_u = 500,
86 .n201_i = 1503,
87 .mD = 760,
88 .mU = 760,
89 .kD = 8,
90 .kU = 8,
91 },
92 [7] = {
93 .t200_201 = 20,
94 .n200 = 3,
95 .n201_u = 270,
96 },
97 [8] = {
98 .t200_201 = 20,
99 .n200 = 3,
100 .n201_u = 270,
101 },
102 [9] = {
103 .iov_i_exp = 27,
104 .t200_201 = 20,
105 .n200 = 3,
106 .n201_u = 500,
107 .n201_i = 1503,
108 .mD = 380,
109 .mU = 380,
110 .kD = 4,
111 .kU = 4,
112 },
113 [11] = {
114 .iov_i_exp = 27,
115 .t200_201 = 40,
116 .n200 = 3,
117 .n201_u = 500,
118 .n201_i = 1503,
119 .mD = 190,
120 .mU = 190,
121 .kD = 2,
122 .kU = 2,
123 },
124};
125
Harald Welte75871c72010-06-01 11:53:01 +0200126LLIST_HEAD(gprs_llc_llmes);
Harald Weltec14e5822010-05-02 09:28:11 +0200127void *llc_tall_ctx;
128
Harald Welte66d062a2010-12-26 10:39:26 +0100129/* If the TLLI is foreign, return its local version */
130static inline uint32_t tlli_foreign2local(uint32_t tlli)
131{
132 uint32_t new_tlli;
133
134 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
135 new_tlli = tlli | 0x40000000;
136 DEBUGP(DLLC, "TLLI 0x%08x is foreign, converting to "
137 "local TLLI 0x%08x\n", tlli, new_tlli);
138 } else
139 new_tlli = tlli;
140
141 return new_tlli;
142}
143
Harald Weltec14e5822010-05-02 09:28:11 +0200144/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Harald Weltedf270ad2010-06-03 07:11:04 +0200145static struct gprs_llc_lle *lle_by_tlli_sapi(uint32_t tlli, uint8_t sapi)
Harald Weltec14e5822010-05-02 09:28:11 +0200146{
Harald Welte75871c72010-06-01 11:53:01 +0200147 struct gprs_llc_llme *llme;
Harald Weltec14e5822010-05-02 09:28:11 +0200148
Harald Welte66d062a2010-12-26 10:39:26 +0100149 tlli = tlli_foreign2local(tlli);
150
Harald Welte75871c72010-06-01 11:53:01 +0200151 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
152 if (llme->tlli == tlli || llme->old_tlli == tlli)
153 return &llme->lle[sapi];
Harald Weltec14e5822010-05-02 09:28:11 +0200154 }
155 return NULL;
156}
157
Harald Weltedf270ad2010-06-03 07:11:04 +0200158static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltec14e5822010-05-02 09:28:11 +0200159{
Harald Welte75871c72010-06-01 11:53:01 +0200160 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltec14e5822010-05-02 09:28:11 +0200161
Harald Welte75871c72010-06-01 11:53:01 +0200162 lle->llme = llme;
163 lle->sapi = sapi;
164 lle->state = GPRS_LLES_UNASSIGNED;
165
Harald Weltedf270ad2010-06-03 07:11:04 +0200166 /* Initialize according to parameters */
167 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte75871c72010-06-01 11:53:01 +0200168}
169
170static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
171{
172 struct gprs_llc_llme *llme;
173 uint32_t i;
174
175 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
176 if (!llme)
Harald Weltec14e5822010-05-02 09:28:11 +0200177 return NULL;
178
Harald Welte75871c72010-06-01 11:53:01 +0200179 llme->tlli = tlli;
Harald Welteb3d84a42010-07-01 11:54:31 +0200180 llme->old_tlli = 0xffffffff;
Harald Welte75871c72010-06-01 11:53:01 +0200181 llme->state = GPRS_LLMS_UNASSIGNED;
Harald Weltec14e5822010-05-02 09:28:11 +0200182
Harald Welte75871c72010-06-01 11:53:01 +0200183 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
184 lle_init(llme, i);
185
186 llist_add(&llme->list, &gprs_llc_llmes);
187
188 return llme;
Harald Weltec14e5822010-05-02 09:28:11 +0200189}
190
Harald Welte7a62a3a2010-06-28 22:18:26 +0200191static void llme_free(struct gprs_llc_llme *llme)
192{
193 llist_del(&llme->list);
194 talloc_free(llme);
195}
196
Harald Welte75bb8202010-03-14 15:45:01 +0800197enum gprs_llc_cmd {
198 GPRS_LLC_NULL,
199 GPRS_LLC_RR,
200 GPRS_LLC_ACK,
201 GPRS_LLC_RNR,
202 GPRS_LLC_SACK,
203 GPRS_LLC_DM,
204 GPRS_LLC_DISC,
205 GPRS_LLC_UA,
206 GPRS_LLC_SABM,
207 GPRS_LLC_FRMR,
208 GPRS_LLC_XID,
Harald Weltefebc4262010-05-13 19:22:55 +0200209 GPRS_LLC_UI,
Harald Welte75bb8202010-03-14 15:45:01 +0800210};
211
Harald Welte366c2042010-05-18 12:31:50 +0200212static const struct value_string llc_cmd_strs[] = {
213 { GPRS_LLC_NULL, "NULL" },
214 { GPRS_LLC_RR, "RR" },
215 { GPRS_LLC_ACK, "ACK" },
216 { GPRS_LLC_RNR, "RNR" },
217 { GPRS_LLC_SACK, "SACK" },
218 { GPRS_LLC_DM, "DM" },
219 { GPRS_LLC_DISC, "DISC" },
220 { GPRS_LLC_UA, "UA" },
221 { GPRS_LLC_SABM, "SABM" },
222 { GPRS_LLC_FRMR, "FRMR" },
223 { GPRS_LLC_XID, "XID" },
224 { GPRS_LLC_UI, "UI" },
225 { 0, NULL }
226};
227
Harald Welte75bb8202010-03-14 15:45:01 +0800228struct gprs_llc_hdr_parsed {
Harald Welted85d9a92010-05-02 11:26:34 +0200229 uint8_t sapi;
230 uint8_t is_cmd:1,
Harald Welte75bb8202010-03-14 15:45:01 +0800231 ack_req:1,
232 is_encrypted:1;
Harald Welted85d9a92010-05-02 11:26:34 +0200233 uint32_t seq_rx;
234 uint32_t seq_tx;
235 uint32_t fcs;
236 uint32_t fcs_calc;
237 uint8_t *data;
Harald Welte9f14f982010-05-03 13:25:07 +0200238 uint16_t data_len;
Harald Weltefa325072010-06-30 23:15:57 +0200239 uint16_t crc_length;
Harald Welte75bb8202010-03-14 15:45:01 +0800240 enum gprs_llc_cmd cmd;
241};
242
243#define LLC_ALLOC_SIZE 16384
244#define UI_HDR_LEN 3
245#define N202 4
246#define CRC24_LENGTH 3
247
Harald Welted85d9a92010-05-02 11:26:34 +0200248static int gprs_llc_fcs(uint8_t *data, unsigned int len)
Harald Welte75bb8202010-03-14 15:45:01 +0800249{
Harald Welted85d9a92010-05-02 11:26:34 +0200250 uint32_t fcs_calc;
Harald Welte75bb8202010-03-14 15:45:01 +0800251
252 fcs_calc = crc24_calc(INIT_CRC24, data, len);
253 fcs_calc = ~fcs_calc;
254 fcs_calc &= 0xffffff;
255
256 return fcs_calc;
257}
258
Harald Welte75bb8202010-03-14 15:45:01 +0800259static void t200_expired(void *data)
260{
261 struct gprs_llc_lle *lle = data;
262
263 /* 8.5.1.3: Expiry of T200 */
264
Harald Weltedf270ad2010-06-03 07:11:04 +0200265 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte75bb8202010-03-14 15:45:01 +0800266 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte75871c72010-06-01 11:53:01 +0200267 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte75bb8202010-03-14 15:45:01 +0800268 }
269
270 switch (lle->state) {
Harald Welte75871c72010-06-01 11:53:01 +0200271 case GPRS_LLES_LOCAL_EST:
Harald Weltefebc4262010-05-13 19:22:55 +0200272 /* FIXME: retransmit SABM */
273 /* FIXME: re-start T200 */
Harald Welte75bb8202010-03-14 15:45:01 +0800274 lle->retrans_ctr++;
275 break;
Harald Welte75871c72010-06-01 11:53:01 +0200276 case GPRS_LLES_LOCAL_REL:
Harald Weltefebc4262010-05-13 19:22:55 +0200277 /* FIXME: retransmit DISC */
278 /* FIXME: re-start T200 */
Harald Welte75bb8202010-03-14 15:45:01 +0800279 lle->retrans_ctr++;
280 break;
281 }
282
283}
284
285static void t201_expired(void *data)
286{
287 struct gprs_llc_lle *lle = data;
288
Harald Weltedf270ad2010-06-03 07:11:04 +0200289 if (lle->retrans_ctr < lle->params.n200) {
Harald Weltefebc4262010-05-13 19:22:55 +0200290 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
291 /* FIXME: set timer T201 */
Harald Welte75bb8202010-03-14 15:45:01 +0800292 lle->retrans_ctr++;
293 }
294}
295
Harald Welte439fa722010-05-03 12:28:12 +0200296int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
297 enum gprs_llc_u_cmd u_cmd, int pf_bit)
298{
299 uint8_t *fcs, *llch;
300 uint8_t addr, ctrl;
301 uint32_t fcs_calc;
302
303 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
304
305 /* Address Field */
306 addr = sapi & 0xf;
307 if (command)
308 addr |= 0x40;
309
310 /* 6.3 Figure 8 */
311 ctrl = 0xe0 | u_cmd;
312 if (pf_bit)
313 ctrl |= 0x10;
314
315 /* prepend LLC UI header */
316 llch = msgb_push(msg, 2);
317 llch[0] = addr;
318 llch[1] = ctrl;
319
320 /* append FCS to end of frame */
321 fcs = msgb_put(msg, 3);
322 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
323 fcs[0] = fcs_calc & 0xff;
324 fcs[1] = (fcs_calc >> 8) & 0xff;
325 fcs[2] = (fcs_calc >> 16) & 0xff;
326
327 /* Identifiers passed down: (BVCI, NSEI) */
328
Harald Weltefebc4262010-05-13 19:22:55 +0200329 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltea62e9fe2012-06-17 12:16:31 +0800330 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte439fa722010-05-03 12:28:12 +0200331}
332
333/* Send XID response to LLE */
Harald Welteac687be2011-10-16 18:49:05 +0200334static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
335 int command)
Harald Welte439fa722010-05-03 12:28:12 +0200336{
337 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte75871c72010-06-01 11:53:01 +0200338 msgb_tlli(msg) = lle->llme->tlli;
339 msgb_bvci(msg) = lle->llme->bvci;
340 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte439fa722010-05-03 12:28:12 +0200341
Harald Welteac687be2011-10-16 18:49:05 +0200342 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte439fa722010-05-03 12:28:12 +0200343}
344
Harald Welte75bb8202010-03-14 15:45:01 +0800345/* Transmit a UI frame over the given SAPI */
Harald Weltecca80c92010-05-31 22:12:30 +0200346int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
347 void *mmctx)
Harald Welte75bb8202010-03-14 15:45:01 +0800348{
Harald Weltea6e32702010-05-02 11:19:37 +0200349 struct gprs_llc_lle *lle;
Harald Welted85d9a92010-05-02 11:26:34 +0200350 uint8_t *fcs, *llch;
351 uint8_t addr, ctrl[2];
352 uint32_t fcs_calc;
353 uint16_t nu = 0;
Harald Welte222b0682010-06-30 23:07:59 +0200354 uint32_t oc;
Harald Welte75bb8202010-03-14 15:45:01 +0800355
Harald Weltea6e32702010-05-02 11:19:37 +0200356 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
357
358 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
359 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
Harald Welte75871c72010-06-01 11:53:01 +0200360 if (!lle) {
361 struct gprs_llc_llme *llme;
Harald Welte66d062a2010-12-26 10:39:26 +0100362 LOGP(DLLC, LOGL_ERROR, "LLC TX: unknown TLLI 0x%08x, "
363 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte75871c72010-06-01 11:53:01 +0200364 llme = llme_alloc(msgb_tlli(msg));
365 lle = &llme->lle[sapi];
366 }
Harald Weltedf270ad2010-06-03 07:11:04 +0200367
368 if (msg->len > lle->params.n201_u) {
369 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
370 msg->len, lle->params.n201_u);
371 return -EFBIG;
372 }
373
Harald Weltea6e32702010-05-02 11:19:37 +0200374 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte75871c72010-06-01 11:53:01 +0200375 lle->llme->bvci = msgb_bvci(msg);
376 lle->llme->nsei = msgb_nsei(msg);
Harald Weltea6e32702010-05-02 11:19:37 +0200377
Harald Welte222b0682010-06-30 23:07:59 +0200378 /* Obtain current values for N(u) and OC */
Harald Welte02343f92010-05-30 21:51:58 +0200379 nu = lle->vu_send;
Harald Welte222b0682010-06-30 23:07:59 +0200380 oc = lle->oc_ui_send;
381 /* Increment V(U) */
Harald Welte02343f92010-05-30 21:51:58 +0200382 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welte222b0682010-06-30 23:07:59 +0200383 /* Increment Overflow Counter, if needed */
384 if ((lle->vu_send + 1) / 512)
385 lle->oc_ui_send += 512;
Harald Welte02343f92010-05-30 21:51:58 +0200386
Harald Welte75bb8202010-03-14 15:45:01 +0800387 /* Address Field */
388 addr = sapi & 0xf;
389 if (command)
390 addr |= 0x40;
391
392 /* Control Field */
393 ctrl[0] = 0xc0;
394 ctrl[0] |= nu >> 6;
395 ctrl[1] = (nu << 2) & 0xfc;
396 ctrl[1] |= 0x01; /* Protected Mode */
397
398 /* prepend LLC UI header */
399 llch = msgb_push(msg, 3);
400 llch[0] = addr;
401 llch[1] = ctrl[0];
402 llch[2] = ctrl[1];
403
404 /* append FCS to end of frame */
405 fcs = msgb_put(msg, 3);
406 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
407 fcs[0] = fcs_calc & 0xff;
408 fcs[1] = (fcs_calc >> 8) & 0xff;
409 fcs[2] = (fcs_calc >> 16) & 0xff;
410
Harald Welte222b0682010-06-30 23:07:59 +0200411 /* encrypt information field + FCS, if needed! */
412 if (lle->llme->algo != GPRS_ALGO_GEA0) {
413 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
414 uint16_t crypt_len = (fcs + 3) - (llch + 3);
415 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
416 uint32_t iv;
417 int rc, i;
418 uint64_t kc = *(uint64_t *)&lle->llme->kc;
419
420 /* Compute the 'Input' Paraemeter */
421 iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc);
422
423 /* Compute the keystream that we need to XOR with the data */
424 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
425 kc, iv, GPRS_CIPH_SGSN2MS);
426 if (rc < 0) {
427 LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc);
428 return rc;
429 }
430
431 /* XOR the cipher output with the information field + FCS */
432 for (i = 0; i < crypt_len; i++)
433 *(llch + 3 + i) ^= cipher_out[i];
434
435 /* Mark frame as encrypted */
436 ctrl[1] |= 0x02;
437 }
438
Harald Weltea6e32702010-05-02 11:19:37 +0200439 /* Identifiers passed down: (BVCI, NSEI) */
440
Harald Weltefebc4262010-05-13 19:22:55 +0200441 /* Send BSSGP-DL-UNITDATA.req */
Harald Welte1e8b9462012-06-17 09:31:16 +0800442 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte75bb8202010-03-14 15:45:01 +0800443}
444
Harald Welteac687be2011-10-16 18:49:05 +0200445/* According to 6.4.1.6 / Figure 11 */
446static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data)
447{
448 uint8_t header_len = 1;
449 uint8_t *cur;
450
451 /* type is a 5-bit field... */
452 if (type > 0x1f)
453 return -EINVAL;
454
455 if (length > 3)
456 header_len = 2;
457
458 cur = msgb_put(msg, length + header_len);
459
460 /* build the header without or with XL bit */
461 if (length <= 3) {
462 *cur++ = (type << 2) | (length & 3);
463 } else {
464 *cur++ = 0x80 | (type << 2) | (length >> 6);
465 *cur++ = (length << 2);
466 }
467
468 /* copy over the payload of the parameter*/
469 memcpy(cur, data, length);
470
471 return length + header_len;
472}
473
474static void rx_llc_xid(struct gprs_llc_lle *lle,
475 struct gprs_llc_hdr_parsed *gph)
476{
477 /* FIXME: 8.5.3.3: check if XID is invalid */
478 if (gph->is_cmd) {
479 /* FIXME: implement XID negotiation using SNDCP */
480 struct msgb *resp;
481 uint8_t *xid;
482 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
483 xid = msgb_put(resp, gph->data_len);
484 memcpy(xid, gph->data, gph->data_len);
485 gprs_llc_tx_xid(lle, resp, 0);
486 } else {
487 /* FIXME: if we had sent a XID reset, send
488 * LLGMM-RESET.conf to GMM */
489 /* FIXME: implement XID negotiation using SNDCP */
490 }
491}
492
Holger Hans Peter Freyther6f8867b2010-05-23 21:35:25 +0800493static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
Harald Welte75bb8202010-03-14 15:45:01 +0800494{
Sylvain Munaut1a3b9c92010-07-03 22:03:02 +0200495 DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x",
Harald Welte75bb8202010-03-14 15:45:01 +0800496 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
Sylvain Munaut1a3b9c92010-07-03 22:03:02 +0200497 gph->fcs);
Harald Welte75bb8202010-03-14 15:45:01 +0800498
499 if (gph->cmd)
Harald Welte366c2042010-05-18 12:31:50 +0200500 DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd));
Harald Welte75bb8202010-03-14 15:45:01 +0800501
502 if (gph->data)
Harald Welte24b78872010-05-13 19:47:50 +0200503 DEBUGPC(DLLC, "DATA ");
Harald Welte75bb8202010-03-14 15:45:01 +0800504
Harald Welte24b78872010-05-13 19:47:50 +0200505 DEBUGPC(DLLC, "\n");
Harald Welte75bb8202010-03-14 15:45:01 +0800506}
507static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
508 struct gprs_llc_lle *lle)
509{
510 switch (gph->cmd) {
511 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
512 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte75871c72010-06-01 11:53:01 +0200513 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte75bb8202010-03-14 15:45:01 +0800514 /* start re-establishment (8.7.1) */
515 }
Harald Welte75871c72010-06-01 11:53:01 +0200516 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte75bb8202010-03-14 15:45:01 +0800517 /* FIXME: Send UA */
Harald Welte75871c72010-06-01 11:53:01 +0200518 lle->state = GPRS_LLES_ABM;
Harald Welte75bb8202010-03-14 15:45:01 +0800519 /* FIXME: process data */
520 break;
521 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
522 /* FIXME: Send UA */
523 /* terminate ABM */
Harald Welte75871c72010-06-01 11:53:01 +0200524 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte75bb8202010-03-14 15:45:01 +0800525 break;
526 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte75871c72010-06-01 11:53:01 +0200527 if (lle->state == GPRS_LLES_LOCAL_EST)
528 lle->state = GPRS_LLES_ABM;
Harald Welte75bb8202010-03-14 15:45:01 +0800529 break;
530 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte75871c72010-06-01 11:53:01 +0200531 if (lle->state == GPRS_LLES_LOCAL_EST)
532 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte75bb8202010-03-14 15:45:01 +0800533 break;
534 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
535 break;
536 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welteac687be2011-10-16 18:49:05 +0200537 rx_llc_xid(lle, gph);
Harald Welte75bb8202010-03-14 15:45:01 +0800538 break;
Harald Welted0e36dc2010-06-01 18:28:10 +0200539 case GPRS_LLC_UI:
Holger Hans Peter Freyther6721a082011-06-23 17:53:27 -0400540 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
541 LOGP(DLLC, LOGL_NOTICE,
542 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther1b5b66c2010-06-23 09:48:25 +0800543 lle->llme ? lle->llme->tlli : -1,
Harald Welted0e36dc2010-06-01 18:28:10 +0200544 gph->seq_tx, lle->vu_recv);
545 return -EIO;
546 }
547 /* Increment the sequence number that we expect in the next frame */
548 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welte222b0682010-06-30 23:07:59 +0200549 /* Increment Overflow Counter */
550 if ((gph->seq_tx + 1) / 512)
551 lle->oc_ui_recv += 512;
Harald Welted0e36dc2010-06-01 18:28:10 +0200552 break;
Harald Welte75bb8202010-03-14 15:45:01 +0800553 }
554
555 return 0;
556}
557
558/* parse a GPRS LLC header, also check for invalid frames */
559static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Holger Hans Peter Freyther6f261512010-05-23 21:43:57 +0800560 uint8_t *llc_hdr, int len)
Harald Welte75bb8202010-03-14 15:45:01 +0800561{
Harald Welted85d9a92010-05-02 11:26:34 +0200562 uint8_t *ctrl = llc_hdr+1;
Harald Welte75bb8202010-03-14 15:45:01 +0800563
564 if (len <= CRC24_LENGTH)
565 return -EIO;
566
Harald Weltefa325072010-06-30 23:15:57 +0200567 ghp->crc_length = len - CRC24_LENGTH;
Harald Welte75bb8202010-03-14 15:45:01 +0800568
569 ghp->ack_req = 0;
570
571 /* Section 5.5: FCS */
572 ghp->fcs = *(llc_hdr + len - 3);
573 ghp->fcs |= *(llc_hdr + len - 2) << 8;
574 ghp->fcs |= *(llc_hdr + len - 1) << 16;
575
576 /* Section 6.2.1: invalid PD field */
577 if (llc_hdr[0] & 0x80)
578 return -EIO;
579
580 /* This only works for the MS->SGSN direction */
581 if (llc_hdr[0] & 0x40)
582 ghp->is_cmd = 0;
583 else
584 ghp->is_cmd = 1;
585
586 ghp->sapi = llc_hdr[0] & 0xf;
587
588 /* Section 6.2.3: check for reserved SAPI */
589 switch (ghp->sapi) {
590 case 0:
591 case 4:
592 case 6:
593 case 0xa:
594 case 0xc:
595 case 0xd:
596 case 0xf:
597 return -EINVAL;
598 }
599
600 if ((ctrl[0] & 0x80) == 0) {
601 /* I (Information transfer + Supervisory) format */
Harald Welted85d9a92010-05-02 11:26:34 +0200602 uint8_t k;
Harald Welte75bb8202010-03-14 15:45:01 +0800603
604 ghp->data = ctrl + 3;
605
606 if (ctrl[0] & 0x40)
607 ghp->ack_req = 1;
608
609 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
610 ghp->seq_tx |= (ctrl[1] >> 4);
611
612 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
613 ghp->seq_rx |= (ctrl[2] >> 2);
614
615 switch (ctrl[2] & 0x03) {
616 case 0:
617 ghp->cmd = GPRS_LLC_RR;
618 break;
619 case 1:
620 ghp->cmd = GPRS_LLC_ACK;
621 break;
622 case 2:
623 ghp->cmd = GPRS_LLC_RNR;
624 break;
625 case 3:
626 ghp->cmd = GPRS_LLC_SACK;
627 k = ctrl[3] & 0x1f;
628 ghp->data += 1 + k;
629 break;
630 }
Harald Welte9f14f982010-05-03 13:25:07 +0200631 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte75bb8202010-03-14 15:45:01 +0800632 } else if ((ctrl[0] & 0xc0) == 0x80) {
633 /* S (Supervisory) format */
634 ghp->data = NULL;
Harald Welte9f14f982010-05-03 13:25:07 +0200635 ghp->data_len = 0;
Harald Welte75bb8202010-03-14 15:45:01 +0800636
637 if (ctrl[0] & 0x20)
638 ghp->ack_req = 1;
639 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
640 ghp->seq_rx |= (ctrl[1] >> 2);
641
642 switch (ctrl[1] & 0x03) {
643 case 0:
644 ghp->cmd = GPRS_LLC_RR;
645 break;
646 case 1:
647 ghp->cmd = GPRS_LLC_ACK;
648 break;
649 case 2:
650 ghp->cmd = GPRS_LLC_RNR;
651 break;
652 case 3:
653 ghp->cmd = GPRS_LLC_SACK;
654 break;
655 }
656 } else if ((ctrl[0] & 0xe0) == 0xc0) {
657 /* UI (Unconfirmed Inforamtion) format */
Harald Weltefebc4262010-05-13 19:22:55 +0200658 ghp->cmd = GPRS_LLC_UI;
Harald Welte75bb8202010-03-14 15:45:01 +0800659 ghp->data = ctrl + 2;
Harald Welte9f14f982010-05-03 13:25:07 +0200660 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte75bb8202010-03-14 15:45:01 +0800661
662 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
663 ghp->seq_tx |= (ctrl[1] >> 2);
664 if (ctrl[1] & 0x02) {
665 ghp->is_encrypted = 1;
666 /* FIXME: encryption */
667 }
668 if (ctrl[1] & 0x01) {
669 /* FCS over hdr + all inf fields */
670 } else {
671 /* FCS over hdr + N202 octets (4) */
Harald Weltefa325072010-06-30 23:15:57 +0200672 if (ghp->crc_length > UI_HDR_LEN + N202)
673 ghp->crc_length = UI_HDR_LEN + N202;
Harald Welte75bb8202010-03-14 15:45:01 +0800674 }
675 } else {
676 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
677 ghp->data = NULL;
Harald Welte9f14f982010-05-03 13:25:07 +0200678 ghp->data_len = 0;
Harald Welte75bb8202010-03-14 15:45:01 +0800679
680 switch (ctrl[0] & 0xf) {
Harald Welte9f14f982010-05-03 13:25:07 +0200681 case GPRS_LLC_U_NULL_CMD:
Harald Welte75bb8202010-03-14 15:45:01 +0800682 ghp->cmd = GPRS_LLC_NULL;
683 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200684 case GPRS_LLC_U_DM_RESP:
Harald Welte75bb8202010-03-14 15:45:01 +0800685 ghp->cmd = GPRS_LLC_DM;
686 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200687 case GPRS_LLC_U_DISC_CMD:
Harald Welte75bb8202010-03-14 15:45:01 +0800688 ghp->cmd = GPRS_LLC_DISC;
689 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200690 case GPRS_LLC_U_UA_RESP:
Harald Welte75bb8202010-03-14 15:45:01 +0800691 ghp->cmd = GPRS_LLC_UA;
692 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200693 case GPRS_LLC_U_SABM_CMD:
Harald Welte75bb8202010-03-14 15:45:01 +0800694 ghp->cmd = GPRS_LLC_SABM;
695 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200696 case GPRS_LLC_U_FRMR_RESP:
Harald Welte75bb8202010-03-14 15:45:01 +0800697 ghp->cmd = GPRS_LLC_FRMR;
698 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200699 case GPRS_LLC_U_XID:
Harald Welte75bb8202010-03-14 15:45:01 +0800700 ghp->cmd = GPRS_LLC_XID;
Harald Welte9f14f982010-05-03 13:25:07 +0200701 ghp->data = ctrl + 1;
702 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte75bb8202010-03-14 15:45:01 +0800703 break;
704 default:
705 return -EIO;
706 }
707 }
708
Harald Welte75bb8202010-03-14 15:45:01 +0800709 /* FIXME: parse sack frame */
Harald Weltefebc4262010-05-13 19:22:55 +0200710 if (ghp->cmd == GPRS_LLC_SACK) {
Harald Weltee3497362010-05-13 19:49:06 +0200711 LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n");
Harald Weltefebc4262010-05-13 19:22:55 +0200712 return -EIO;
713 }
714
715 return 0;
Harald Welte75bb8202010-03-14 15:45:01 +0800716}
717
Harald Weltec14e5822010-05-02 09:28:11 +0200718/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte75bb8202010-03-14 15:45:01 +0800719int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
720{
Harald Welte73164402010-05-02 09:50:42 +0200721 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Holger Hans Peter Freytherf9839f62011-10-14 23:42:13 +0200722 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800723 struct gprs_llc_hdr_parsed llhp;
Harald Welte439fa722010-05-03 12:28:12 +0200724 struct gprs_llc_lle *lle;
Harald Weltec14e5822010-05-02 09:28:11 +0200725 int rc = 0;
Harald Welte75bb8202010-03-14 15:45:01 +0800726
Harald Welte2351f1f2010-05-02 11:54:55 +0200727 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
728
Holger Hans Peter Freyther510ae722010-05-23 21:33:57 +0800729 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freyther6f261512010-05-23 21:43:57 +0800730 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte75bb8202010-03-14 15:45:01 +0800731 gprs_llc_hdr_dump(&llhp);
Harald Weltefebc4262010-05-13 19:22:55 +0200732 if (rc < 0) {
Harald Weltee3497362010-05-13 19:49:06 +0200733 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Weltefebc4262010-05-13 19:22:55 +0200734 return rc;
735 }
736
Harald Welte75871c72010-06-01 11:53:01 +0200737 switch (gprs_tlli_type(msgb_tlli(msg))) {
738 case TLLI_LOCAL:
739 case TLLI_FOREIGN:
740 case TLLI_RANDOM:
741 case TLLI_AUXILIARY:
742 break;
743 default:
744 LOGP(DLLC, LOGL_ERROR,
745 "Discarding frame with strange TLLI type\n");
746 break;
747 }
748
Harald Weltec14e5822010-05-02 09:28:11 +0200749 /* find the LLC Entity for this TLLI+SAPI tuple */
750 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
Harald Weltefebc4262010-05-13 19:22:55 +0200751
752 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
753 * except UID and XID frames with SAPI=1 */
Harald Welte4b7d1de2010-05-18 12:45:08 +0200754 if (!lle) {
755 if (llhp.sapi == GPRS_SAPI_GMM &&
756 (llhp.cmd == GPRS_LLC_XID || llhp.cmd == GPRS_LLC_UI)) {
Harald Welte75871c72010-06-01 11:53:01 +0200757 struct gprs_llc_llme *llme;
Harald Welte4b7d1de2010-05-18 12:45:08 +0200758 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
Harald Welte75871c72010-06-01 11:53:01 +0200759 llme = llme_alloc(msgb_tlli(msg));
Dieter Spaar9770bd42011-07-27 23:38:46 +0200760 LOGP(DLLC, LOGL_DEBUG, "LLC RX: unknown TLLI 0x%08x, "
Harald Welte66d062a2010-12-26 10:39:26 +0100761 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte75871c72010-06-01 11:53:01 +0200762 lle = &llme->lle[llhp.sapi];
Harald Welte4b7d1de2010-05-18 12:45:08 +0200763 } else {
764 LOGP(DLLC, LOGL_NOTICE,
765 "unknown TLLI/SAPI: Silently dropping\n");
766 return 0;
767 }
Harald Weltefebc4262010-05-13 19:22:55 +0200768 }
Harald Weltec14e5822010-05-02 09:28:11 +0200769
Harald Welte222b0682010-06-30 23:07:59 +0200770 /* decrypt information field + FCS, if needed! */
771 if (llhp.is_encrypted) {
772 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
773 uint16_t crypt_len = llhp.data_len + 3;
774 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
775 uint32_t iv;
776 uint64_t kc = *(uint64_t *)&lle->llme->kc;
777 int rc, i;
778
779 if (lle->llme->algo == GPRS_ALGO_GEA0) {
780 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
781 "has no KC/Algo! Dropping.\n");
782 return 0;
783 }
784
785 iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx,
786 lle->oc_ui_recv);
787 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
788 kc, iv, GPRS_CIPH_MS2SGSN);
789 if (rc < 0) {
790 LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n",
791 rc);
792 return rc;
793 }
794
795 /* XOR the cipher output with the information field + FCS */
796 for (i = 0; i < crypt_len; i++)
797 *(llhp.data + i) ^= cipher_out[i];
798 } else {
799 if (lle->llme->algo != GPRS_ALGO_GEA0) {
800 LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC "
801 "that is supposed to be encrypted. Dropping.\n");
802 return 0;
803 }
804 }
805
806 /* We have to do the FCS check _after_ decryption */
Harald Weltefa325072010-06-30 23:15:57 +0200807 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welte222b0682010-06-30 23:07:59 +0200808 if (llhp.fcs != llhp.fcs_calc) {
809 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
810 return -EIO;
811 }
812
Harald Welte439fa722010-05-03 12:28:12 +0200813 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte75871c72010-06-01 11:53:01 +0200814 lle->llme->bvci = msgb_bvci(msg);
815 lle->llme->nsei = msgb_nsei(msg);
Harald Welte439fa722010-05-03 12:28:12 +0200816
Harald Weltefebc4262010-05-13 19:22:55 +0200817 /* Receive and Process the actual LLC frame */
Harald Welte75bb8202010-03-14 15:45:01 +0800818 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Weltefebc4262010-05-13 19:22:55 +0200819 if (rc < 0)
820 return rc;
Harald Welte75bb8202010-03-14 15:45:01 +0800821
Harald Weltefebc4262010-05-13 19:22:55 +0200822 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte75bb8202010-03-14 15:45:01 +0800823 if (llhp.data) {
Harald Weltecf3c4142010-04-30 16:33:12 +0200824 msgb_gmmh(msg) = llhp.data;
Harald Welte75bb8202010-03-14 15:45:01 +0800825 switch (llhp.sapi) {
826 case GPRS_SAPI_GMM:
Harald Weltefebc4262010-05-13 19:22:55 +0200827 /* send LL_UNITDATA_IND to GMM */
Harald Welte75871c72010-06-01 11:53:01 +0200828 rc = gsm0408_gprs_rcvmsg(msg, lle->llme);
Harald Weltec14e5822010-05-02 09:28:11 +0200829 break;
Harald Weltec14e5822010-05-02 09:28:11 +0200830 case GPRS_SAPI_SNDCP3:
831 case GPRS_SAPI_SNDCP5:
832 case GPRS_SAPI_SNDCP9:
833 case GPRS_SAPI_SNDCP11:
Harald Welted0e36dc2010-06-01 18:28:10 +0200834 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
835 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
836 break;
Harald Weltec14e5822010-05-02 09:28:11 +0200837 case GPRS_SAPI_SMS:
838 /* FIXME */
Harald Welted0e36dc2010-06-01 18:28:10 +0200839 case GPRS_SAPI_TOM2:
840 case GPRS_SAPI_TOM8:
841 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltec14e5822010-05-02 09:28:11 +0200842 default:
Harald Welte24b78872010-05-13 19:47:50 +0200843 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltec14e5822010-05-02 09:28:11 +0200844 rc = -EINVAL;
845 break;
Harald Welte75bb8202010-03-14 15:45:01 +0800846 }
847 }
848
Harald Weltec14e5822010-05-02 09:28:11 +0200849 return rc;
Harald Welte75bb8202010-03-14 15:45:01 +0800850}
Harald Welte75871c72010-06-01 11:53:01 +0200851
852/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
853int gprs_llgmm_assign(struct gprs_llc_llme *llme,
854 uint32_t old_tlli, uint32_t new_tlli,
855 enum gprs_ciph_algo alg, const uint8_t *kc)
856{
857 unsigned int i;
858
Harald Welte222b0682010-06-30 23:07:59 +0200859 /* Update the crypto parameters */
Harald Welte222b0682010-06-30 23:07:59 +0200860 llme->algo = alg;
Harald Welte82e26e22010-06-30 23:19:23 +0200861 if (alg != GPRS_ALGO_GEA0)
862 memcpy(llme->kc, kc, sizeof(llme->kc));
Harald Welte222b0682010-06-30 23:07:59 +0200863
Harald Welte75871c72010-06-01 11:53:01 +0200864 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
865 /* TLLI Assignment 8.3.1 */
866 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
867 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
868 * old is unassigned. Only TLLI new shall be accepted when
869 * received from peer. */
Harald Welteb3d84a42010-07-01 11:54:31 +0200870 if (llme->old_tlli != 0xffffffff) {
871 llme->old_tlli = 0xffffffff;
872 llme->tlli = new_tlli;
873 } else {
874 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
875 * TLLI assignmemt according to 8.3.1 */
876 llme->old_tlli = 0xffffffff;
877 llme->tlli = new_tlli;
878 llme->state = GPRS_LLMS_ASSIGNED;
879 /* 8.5.3.1 For all LLE's */
880 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
881 struct gprs_llc_lle *l = &llme->lle[i];
882 l->vu_send = l->vu_recv = 0;
883 l->retrans_ctr = 0;
884 l->state = GPRS_LLES_ASSIGNED_ADM;
885 /* FIXME Set parameters according to table 9 */
886 }
Harald Welte75871c72010-06-01 11:53:01 +0200887 }
888 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
889 /* TLLI Change 8.3.2 */
890 /* Both TLLI Old and TLLI New are assigned; use New when
891 * (re)transmitting. Accept toth Old and New on Rx */
892 llme->old_tlli = llme->tlli;
893 llme->tlli = new_tlli;
894 llme->state = GPRS_LLMS_ASSIGNED;
895 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
896 /* TLLI Unassignment 8.3.3) */
897 llme->tlli = llme->old_tlli = 0;
898 llme->state = GPRS_LLMS_UNASSIGNED;
899 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
900 struct gprs_llc_lle *l = &llme->lle[i];
901 l->state = GPRS_LLES_UNASSIGNED;
902 }
Harald Welte7a62a3a2010-06-28 22:18:26 +0200903 llme_free(llme);
Harald Welte75871c72010-06-01 11:53:01 +0200904 } else
905 return -EINVAL;
906
907 return 0;
908}
Harald Welte81fb3582010-06-30 19:59:55 +0200909
Harald Welteac687be2011-10-16 18:49:05 +0200910/* Chapter 7.2.1.2 LLGMM-RESET.req */
911int gprs_llgmm_reset(struct gprs_llc_llme *llme)
912{
913 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
914 int random = rand();
915
916 /* First XID component must be RESET */
917 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
918 /* randomly select new IOV-UI */
Harald Welteffc19992011-10-16 18:59:20 +0200919 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random);
Harald Welteac687be2011-10-16 18:49:05 +0200920
921 /* FIXME: Start T200, wait for XID response */
922 return gprs_llc_tx_xid(&llme->lle[1], msg, 1);
923}
924
Harald Welte81fb3582010-06-30 19:59:55 +0200925int gprs_llc_init(const char *cipher_plugin_path)
926{
927 return gprs_cipher_load(cipher_plugin_path);
928}