blob: 6244d186f7d46fa8ae03d68271001c44b2ed4cf1 [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) */
Harald Welte1d9d9442010-06-03 07:11:04 +0200150static struct gprs_llc_lle *lle_by_tlli_sapi(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 Weltef0901f02010-12-26 10:39:26 +0100154 tlli = tlli_foreign2local(tlli);
155
Harald Welte807a5d82010-06-01 11:53:01 +0200156 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
157 if (llme->tlli == tlli || llme->old_tlli == tlli)
158 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200159 }
160 return NULL;
161}
162
Harald Welte1d9d9442010-06-03 07:11:04 +0200163static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200164{
Harald Welte807a5d82010-06-01 11:53:01 +0200165 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200166
Harald Welte807a5d82010-06-01 11:53:01 +0200167 lle->llme = llme;
168 lle->sapi = sapi;
169 lle->state = GPRS_LLES_UNASSIGNED;
170
Harald Welte1d9d9442010-06-03 07:11:04 +0200171 /* Initialize according to parameters */
172 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200173}
174
175static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
176{
177 struct gprs_llc_llme *llme;
178 uint32_t i;
179
180 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
181 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200182 return NULL;
183
Harald Welte807a5d82010-06-01 11:53:01 +0200184 llme->tlli = tlli;
Harald Welte875840c2010-07-01 11:54:31 +0200185 llme->old_tlli = 0xffffffff;
Harald Welte807a5d82010-06-01 11:53:01 +0200186 llme->state = GPRS_LLMS_UNASSIGNED;
Harald Weltea2665542010-05-02 09:28:11 +0200187
Harald Welte807a5d82010-06-01 11:53:01 +0200188 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
189 lle_init(llme, i);
190
191 llist_add(&llme->list, &gprs_llc_llmes);
192
193 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200194}
195
Harald Weltef7fef482010-06-28 22:18:26 +0200196static void llme_free(struct gprs_llc_llme *llme)
197{
198 llist_del(&llme->list);
199 talloc_free(llme);
200}
201
Harald Welte9b455bf2010-03-14 15:45:01 +0800202enum gprs_llc_cmd {
203 GPRS_LLC_NULL,
204 GPRS_LLC_RR,
205 GPRS_LLC_ACK,
206 GPRS_LLC_RNR,
207 GPRS_LLC_SACK,
208 GPRS_LLC_DM,
209 GPRS_LLC_DISC,
210 GPRS_LLC_UA,
211 GPRS_LLC_SABM,
212 GPRS_LLC_FRMR,
213 GPRS_LLC_XID,
Harald Welte1ae09c72010-05-13 19:22:55 +0200214 GPRS_LLC_UI,
Harald Welte9b455bf2010-03-14 15:45:01 +0800215};
216
Harald Welteb61f4032010-05-18 12:31:50 +0200217static const struct value_string llc_cmd_strs[] = {
218 { GPRS_LLC_NULL, "NULL" },
219 { GPRS_LLC_RR, "RR" },
220 { GPRS_LLC_ACK, "ACK" },
221 { GPRS_LLC_RNR, "RNR" },
222 { GPRS_LLC_SACK, "SACK" },
223 { GPRS_LLC_DM, "DM" },
224 { GPRS_LLC_DISC, "DISC" },
225 { GPRS_LLC_UA, "UA" },
226 { GPRS_LLC_SABM, "SABM" },
227 { GPRS_LLC_FRMR, "FRMR" },
228 { GPRS_LLC_XID, "XID" },
229 { GPRS_LLC_UI, "UI" },
230 { 0, NULL }
231};
232
Harald Welte9b455bf2010-03-14 15:45:01 +0800233struct gprs_llc_hdr_parsed {
Harald Welteeaa614c2010-05-02 11:26:34 +0200234 uint8_t sapi;
235 uint8_t is_cmd:1,
Harald Welte9b455bf2010-03-14 15:45:01 +0800236 ack_req:1,
237 is_encrypted:1;
Harald Welteeaa614c2010-05-02 11:26:34 +0200238 uint32_t seq_rx;
239 uint32_t seq_tx;
240 uint32_t fcs;
241 uint32_t fcs_calc;
242 uint8_t *data;
Harald Welte5658a1a2010-05-03 13:25:07 +0200243 uint16_t data_len;
Harald Welte1b8827a2010-06-30 23:15:57 +0200244 uint16_t crc_length;
Harald Welte9b455bf2010-03-14 15:45:01 +0800245 enum gprs_llc_cmd cmd;
246};
247
248#define LLC_ALLOC_SIZE 16384
249#define UI_HDR_LEN 3
250#define N202 4
251#define CRC24_LENGTH 3
252
Harald Welteeaa614c2010-05-02 11:26:34 +0200253static int gprs_llc_fcs(uint8_t *data, unsigned int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800254{
Harald Welteeaa614c2010-05-02 11:26:34 +0200255 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800256
257 fcs_calc = crc24_calc(INIT_CRC24, data, len);
258 fcs_calc = ~fcs_calc;
259 fcs_calc &= 0xffffff;
260
261 return fcs_calc;
262}
263
Harald Welte9b455bf2010-03-14 15:45:01 +0800264static void t200_expired(void *data)
265{
266 struct gprs_llc_lle *lle = data;
267
268 /* 8.5.1.3: Expiry of T200 */
269
Harald Welte1d9d9442010-06-03 07:11:04 +0200270 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800271 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200272 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800273 }
274
275 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200276 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200277 /* FIXME: retransmit SABM */
278 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800279 lle->retrans_ctr++;
280 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200281 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200282 /* FIXME: retransmit DISC */
283 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800284 lle->retrans_ctr++;
285 break;
286 }
287
288}
289
290static void t201_expired(void *data)
291{
292 struct gprs_llc_lle *lle = data;
293
Harald Welte1d9d9442010-06-03 07:11:04 +0200294 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200295 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
296 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800297 lle->retrans_ctr++;
298 }
299}
300
Harald Welte10997d02010-05-03 12:28:12 +0200301int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
302 enum gprs_llc_u_cmd u_cmd, int pf_bit)
303{
304 uint8_t *fcs, *llch;
305 uint8_t addr, ctrl;
306 uint32_t fcs_calc;
307
308 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
309
310 /* Address Field */
311 addr = sapi & 0xf;
312 if (command)
313 addr |= 0x40;
314
315 /* 6.3 Figure 8 */
316 ctrl = 0xe0 | u_cmd;
317 if (pf_bit)
318 ctrl |= 0x10;
319
320 /* prepend LLC UI header */
321 llch = msgb_push(msg, 2);
322 llch[0] = addr;
323 llch[1] = ctrl;
324
325 /* append FCS to end of frame */
326 fcs = msgb_put(msg, 3);
327 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
328 fcs[0] = fcs_calc & 0xff;
329 fcs[1] = (fcs_calc >> 8) & 0xff;
330 fcs[2] = (fcs_calc >> 16) & 0xff;
331
332 /* Identifiers passed down: (BVCI, NSEI) */
333
Harald Welte1ae09c72010-05-13 19:22:55 +0200334 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800335 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200336}
337
338/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200339static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
340 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200341{
342 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200343 msgb_tlli(msg) = lle->llme->tlli;
344 msgb_bvci(msg) = lle->llme->bvci;
345 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200346
Harald Welte0c1a3032011-10-16 18:49:05 +0200347 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200348}
349
Harald Welte9b455bf2010-03-14 15:45:01 +0800350/* Transmit a UI frame over the given SAPI */
Harald Welte56a01452010-05-31 22:12:30 +0200351int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
352 void *mmctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800353{
Harald Weltee6afd602010-05-02 11:19:37 +0200354 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200355 uint8_t *fcs, *llch;
356 uint8_t addr, ctrl[2];
357 uint32_t fcs_calc;
358 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200359 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800360
Harald Weltee6afd602010-05-02 11:19:37 +0200361 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
362
363 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
364 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
Harald Welte807a5d82010-06-01 11:53:01 +0200365 if (!lle) {
366 struct gprs_llc_llme *llme;
Harald Weltef0901f02010-12-26 10:39:26 +0100367 LOGP(DLLC, LOGL_ERROR, "LLC TX: unknown TLLI 0x%08x, "
368 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte807a5d82010-06-01 11:53:01 +0200369 llme = llme_alloc(msgb_tlli(msg));
370 lle = &llme->lle[sapi];
371 }
Harald Welte1d9d9442010-06-03 07:11:04 +0200372
373 if (msg->len > lle->params.n201_u) {
374 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
375 msg->len, lle->params.n201_u);
376 return -EFBIG;
377 }
378
Harald Weltee6afd602010-05-02 11:19:37 +0200379 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200380 lle->llme->bvci = msgb_bvci(msg);
381 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200382
Harald Welted07b4f92010-06-30 23:07:59 +0200383 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200384 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200385 oc = lle->oc_ui_send;
386 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200387 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200388 /* Increment Overflow Counter, if needed */
389 if ((lle->vu_send + 1) / 512)
390 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200391
Harald Welte9b455bf2010-03-14 15:45:01 +0800392 /* Address Field */
393 addr = sapi & 0xf;
394 if (command)
395 addr |= 0x40;
396
397 /* Control Field */
398 ctrl[0] = 0xc0;
399 ctrl[0] |= nu >> 6;
400 ctrl[1] = (nu << 2) & 0xfc;
401 ctrl[1] |= 0x01; /* Protected Mode */
402
403 /* prepend LLC UI header */
404 llch = msgb_push(msg, 3);
405 llch[0] = addr;
406 llch[1] = ctrl[0];
407 llch[2] = ctrl[1];
408
409 /* append FCS to end of frame */
410 fcs = msgb_put(msg, 3);
411 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
412 fcs[0] = fcs_calc & 0xff;
413 fcs[1] = (fcs_calc >> 8) & 0xff;
414 fcs[2] = (fcs_calc >> 16) & 0xff;
415
Harald Welted07b4f92010-06-30 23:07:59 +0200416 /* encrypt information field + FCS, if needed! */
417 if (lle->llme->algo != GPRS_ALGO_GEA0) {
418 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
419 uint16_t crypt_len = (fcs + 3) - (llch + 3);
420 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
421 uint32_t iv;
422 int rc, i;
423 uint64_t kc = *(uint64_t *)&lle->llme->kc;
424
425 /* Compute the 'Input' Paraemeter */
426 iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc);
427
428 /* Compute the keystream that we need to XOR with the data */
429 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
430 kc, iv, GPRS_CIPH_SGSN2MS);
431 if (rc < 0) {
432 LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc);
433 return rc;
434 }
435
436 /* XOR the cipher output with the information field + FCS */
437 for (i = 0; i < crypt_len; i++)
438 *(llch + 3 + i) ^= cipher_out[i];
439
440 /* Mark frame as encrypted */
441 ctrl[1] |= 0x02;
442 }
443
Harald Weltee6afd602010-05-02 11:19:37 +0200444 /* Identifiers passed down: (BVCI, NSEI) */
445
Harald Welte1ae09c72010-05-13 19:22:55 +0200446 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800447 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800448}
449
Harald Welte0c1a3032011-10-16 18:49:05 +0200450/* According to 6.4.1.6 / Figure 11 */
451static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data)
452{
453 uint8_t header_len = 1;
454 uint8_t *cur;
455
456 /* type is a 5-bit field... */
457 if (type > 0x1f)
458 return -EINVAL;
459
460 if (length > 3)
461 header_len = 2;
462
463 cur = msgb_put(msg, length + header_len);
464
465 /* build the header without or with XL bit */
466 if (length <= 3) {
467 *cur++ = (type << 2) | (length & 3);
468 } else {
469 *cur++ = 0x80 | (type << 2) | (length >> 6);
470 *cur++ = (length << 2);
471 }
472
473 /* copy over the payload of the parameter*/
474 memcpy(cur, data, length);
475
476 return length + header_len;
477}
478
479static void rx_llc_xid(struct gprs_llc_lle *lle,
480 struct gprs_llc_hdr_parsed *gph)
481{
482 /* FIXME: 8.5.3.3: check if XID is invalid */
483 if (gph->is_cmd) {
484 /* FIXME: implement XID negotiation using SNDCP */
485 struct msgb *resp;
486 uint8_t *xid;
487 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
488 xid = msgb_put(resp, gph->data_len);
489 memcpy(xid, gph->data, gph->data_len);
490 gprs_llc_tx_xid(lle, resp, 0);
491 } else {
492 /* FIXME: if we had sent a XID reset, send
493 * LLGMM-RESET.conf to GMM */
494 /* FIXME: implement XID negotiation using SNDCP */
495 }
496}
497
Holger Hans Peter Freyther3a6fdcd2010-05-23 21:35:25 +0800498static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
Harald Welte9b455bf2010-03-14 15:45:01 +0800499{
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200500 DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x",
Harald Welte9b455bf2010-03-14 15:45:01 +0800501 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200502 gph->fcs);
Harald Welte9b455bf2010-03-14 15:45:01 +0800503
504 if (gph->cmd)
Harald Welteb61f4032010-05-18 12:31:50 +0200505 DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd));
Harald Welte9b455bf2010-03-14 15:45:01 +0800506
507 if (gph->data)
Harald Weltec6ecafe2010-05-13 19:47:50 +0200508 DEBUGPC(DLLC, "DATA ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800509
Harald Weltec6ecafe2010-05-13 19:47:50 +0200510 DEBUGPC(DLLC, "\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800511}
512static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
513 struct gprs_llc_lle *lle)
514{
515 switch (gph->cmd) {
516 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
517 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200518 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800519 /* start re-establishment (8.7.1) */
520 }
Harald Welte807a5d82010-06-01 11:53:01 +0200521 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800522 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200523 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800524 /* FIXME: process data */
525 break;
526 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
527 /* FIXME: Send UA */
528 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200529 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800530 break;
531 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200532 if (lle->state == GPRS_LLES_LOCAL_EST)
533 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800534 break;
535 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200536 if (lle->state == GPRS_LLES_LOCAL_EST)
537 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800538 break;
539 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
540 break;
541 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200542 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800543 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200544 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400545 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
546 LOGP(DLLC, LOGL_NOTICE,
547 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800548 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200549 gph->seq_tx, lle->vu_recv);
550 return -EIO;
551 }
552 /* Increment the sequence number that we expect in the next frame */
553 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200554 /* Increment Overflow Counter */
555 if ((gph->seq_tx + 1) / 512)
556 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200557 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800558 }
559
560 return 0;
561}
562
563/* parse a GPRS LLC header, also check for invalid frames */
564static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800565 uint8_t *llc_hdr, int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800566{
Harald Welteeaa614c2010-05-02 11:26:34 +0200567 uint8_t *ctrl = llc_hdr+1;
Harald Welte9b455bf2010-03-14 15:45:01 +0800568
569 if (len <= CRC24_LENGTH)
570 return -EIO;
571
Harald Welte1b8827a2010-06-30 23:15:57 +0200572 ghp->crc_length = len - CRC24_LENGTH;
Harald Welte9b455bf2010-03-14 15:45:01 +0800573
574 ghp->ack_req = 0;
575
576 /* Section 5.5: FCS */
577 ghp->fcs = *(llc_hdr + len - 3);
578 ghp->fcs |= *(llc_hdr + len - 2) << 8;
579 ghp->fcs |= *(llc_hdr + len - 1) << 16;
580
581 /* Section 6.2.1: invalid PD field */
582 if (llc_hdr[0] & 0x80)
583 return -EIO;
584
585 /* This only works for the MS->SGSN direction */
586 if (llc_hdr[0] & 0x40)
587 ghp->is_cmd = 0;
588 else
589 ghp->is_cmd = 1;
590
591 ghp->sapi = llc_hdr[0] & 0xf;
592
593 /* Section 6.2.3: check for reserved SAPI */
594 switch (ghp->sapi) {
595 case 0:
596 case 4:
597 case 6:
598 case 0xa:
599 case 0xc:
600 case 0xd:
601 case 0xf:
602 return -EINVAL;
603 }
604
605 if ((ctrl[0] & 0x80) == 0) {
606 /* I (Information transfer + Supervisory) format */
Harald Welteeaa614c2010-05-02 11:26:34 +0200607 uint8_t k;
Harald Welte9b455bf2010-03-14 15:45:01 +0800608
609 ghp->data = ctrl + 3;
610
611 if (ctrl[0] & 0x40)
612 ghp->ack_req = 1;
613
614 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
615 ghp->seq_tx |= (ctrl[1] >> 4);
616
617 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
618 ghp->seq_rx |= (ctrl[2] >> 2);
619
620 switch (ctrl[2] & 0x03) {
621 case 0:
622 ghp->cmd = GPRS_LLC_RR;
623 break;
624 case 1:
625 ghp->cmd = GPRS_LLC_ACK;
626 break;
627 case 2:
628 ghp->cmd = GPRS_LLC_RNR;
629 break;
630 case 3:
631 ghp->cmd = GPRS_LLC_SACK;
632 k = ctrl[3] & 0x1f;
633 ghp->data += 1 + k;
634 break;
635 }
Harald Welte5658a1a2010-05-03 13:25:07 +0200636 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800637 } else if ((ctrl[0] & 0xc0) == 0x80) {
638 /* S (Supervisory) format */
639 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200640 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800641
642 if (ctrl[0] & 0x20)
643 ghp->ack_req = 1;
644 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
645 ghp->seq_rx |= (ctrl[1] >> 2);
646
647 switch (ctrl[1] & 0x03) {
648 case 0:
649 ghp->cmd = GPRS_LLC_RR;
650 break;
651 case 1:
652 ghp->cmd = GPRS_LLC_ACK;
653 break;
654 case 2:
655 ghp->cmd = GPRS_LLC_RNR;
656 break;
657 case 3:
658 ghp->cmd = GPRS_LLC_SACK;
659 break;
660 }
661 } else if ((ctrl[0] & 0xe0) == 0xc0) {
662 /* UI (Unconfirmed Inforamtion) format */
Harald Welte1ae09c72010-05-13 19:22:55 +0200663 ghp->cmd = GPRS_LLC_UI;
Harald Welte9b455bf2010-03-14 15:45:01 +0800664 ghp->data = ctrl + 2;
Harald Welte5658a1a2010-05-03 13:25:07 +0200665 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800666
667 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
668 ghp->seq_tx |= (ctrl[1] >> 2);
669 if (ctrl[1] & 0x02) {
670 ghp->is_encrypted = 1;
671 /* FIXME: encryption */
672 }
673 if (ctrl[1] & 0x01) {
674 /* FCS over hdr + all inf fields */
675 } else {
676 /* FCS over hdr + N202 octets (4) */
Harald Welte1b8827a2010-06-30 23:15:57 +0200677 if (ghp->crc_length > UI_HDR_LEN + N202)
678 ghp->crc_length = UI_HDR_LEN + N202;
Harald Welte9b455bf2010-03-14 15:45:01 +0800679 }
680 } else {
681 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
682 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200683 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800684
685 switch (ctrl[0] & 0xf) {
Harald Welte5658a1a2010-05-03 13:25:07 +0200686 case GPRS_LLC_U_NULL_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800687 ghp->cmd = GPRS_LLC_NULL;
688 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200689 case GPRS_LLC_U_DM_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800690 ghp->cmd = GPRS_LLC_DM;
691 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200692 case GPRS_LLC_U_DISC_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800693 ghp->cmd = GPRS_LLC_DISC;
694 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200695 case GPRS_LLC_U_UA_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800696 ghp->cmd = GPRS_LLC_UA;
697 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200698 case GPRS_LLC_U_SABM_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800699 ghp->cmd = GPRS_LLC_SABM;
700 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200701 case GPRS_LLC_U_FRMR_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800702 ghp->cmd = GPRS_LLC_FRMR;
703 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200704 case GPRS_LLC_U_XID:
Harald Welte9b455bf2010-03-14 15:45:01 +0800705 ghp->cmd = GPRS_LLC_XID;
Harald Welte5658a1a2010-05-03 13:25:07 +0200706 ghp->data = ctrl + 1;
707 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800708 break;
709 default:
710 return -EIO;
711 }
712 }
713
Harald Welte9b455bf2010-03-14 15:45:01 +0800714 /* FIXME: parse sack frame */
Harald Welte1ae09c72010-05-13 19:22:55 +0200715 if (ghp->cmd == GPRS_LLC_SACK) {
Harald Welte1b170d12010-05-13 19:49:06 +0200716 LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200717 return -EIO;
718 }
719
720 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800721}
722
Harald Weltea2665542010-05-02 09:28:11 +0200723/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800724int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
725{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200726 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200727 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800728 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200729 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200730 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800731
Harald Welte11d7c102010-05-02 11:54:55 +0200732 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
733
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800734 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800735 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800736 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200737 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200738 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200739 return rc;
740 }
741
Harald Welte807a5d82010-06-01 11:53:01 +0200742 switch (gprs_tlli_type(msgb_tlli(msg))) {
743 case TLLI_LOCAL:
744 case TLLI_FOREIGN:
745 case TLLI_RANDOM:
746 case TLLI_AUXILIARY:
747 break;
748 default:
749 LOGP(DLLC, LOGL_ERROR,
750 "Discarding frame with strange TLLI type\n");
751 break;
752 }
753
Harald Weltea2665542010-05-02 09:28:11 +0200754 /* find the LLC Entity for this TLLI+SAPI tuple */
755 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
Harald Welte1ae09c72010-05-13 19:22:55 +0200756
757 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
758 * except UID and XID frames with SAPI=1 */
Harald Welted764c062010-05-18 12:45:08 +0200759 if (!lle) {
760 if (llhp.sapi == GPRS_SAPI_GMM &&
761 (llhp.cmd == GPRS_LLC_XID || llhp.cmd == GPRS_LLC_UI)) {
Harald Welte807a5d82010-06-01 11:53:01 +0200762 struct gprs_llc_llme *llme;
Harald Welted764c062010-05-18 12:45:08 +0200763 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
Harald Welte807a5d82010-06-01 11:53:01 +0200764 llme = llme_alloc(msgb_tlli(msg));
Dieter Spaar1f447fb2011-07-27 23:38:46 +0200765 LOGP(DLLC, LOGL_DEBUG, "LLC RX: unknown TLLI 0x%08x, "
Harald Weltef0901f02010-12-26 10:39:26 +0100766 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte807a5d82010-06-01 11:53:01 +0200767 lle = &llme->lle[llhp.sapi];
Harald Welted764c062010-05-18 12:45:08 +0200768 } else {
769 LOGP(DLLC, LOGL_NOTICE,
770 "unknown TLLI/SAPI: Silently dropping\n");
771 return 0;
772 }
Harald Welte1ae09c72010-05-13 19:22:55 +0200773 }
Harald Weltea2665542010-05-02 09:28:11 +0200774
Harald Welted07b4f92010-06-30 23:07:59 +0200775 /* decrypt information field + FCS, if needed! */
776 if (llhp.is_encrypted) {
777 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
778 uint16_t crypt_len = llhp.data_len + 3;
779 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
780 uint32_t iv;
781 uint64_t kc = *(uint64_t *)&lle->llme->kc;
782 int rc, i;
783
784 if (lle->llme->algo == GPRS_ALGO_GEA0) {
785 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
786 "has no KC/Algo! Dropping.\n");
787 return 0;
788 }
789
790 iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx,
791 lle->oc_ui_recv);
792 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
793 kc, iv, GPRS_CIPH_MS2SGSN);
794 if (rc < 0) {
795 LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n",
796 rc);
797 return rc;
798 }
799
800 /* XOR the cipher output with the information field + FCS */
801 for (i = 0; i < crypt_len; i++)
802 *(llhp.data + i) ^= cipher_out[i];
803 } else {
804 if (lle->llme->algo != GPRS_ALGO_GEA0) {
805 LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC "
806 "that is supposed to be encrypted. Dropping.\n");
807 return 0;
808 }
809 }
810
811 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200812 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200813 if (llhp.fcs != llhp.fcs_calc) {
814 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
815 return -EIO;
816 }
817
Harald Welte10997d02010-05-03 12:28:12 +0200818 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200819 lle->llme->bvci = msgb_bvci(msg);
820 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200821
Harald Welte1ae09c72010-05-13 19:22:55 +0200822 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800823 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200824 if (rc < 0)
825 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800826
Harald Welte1ae09c72010-05-13 19:22:55 +0200827 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte9b455bf2010-03-14 15:45:01 +0800828 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200829 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800830 switch (llhp.sapi) {
831 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200832 /* send LL_UNITDATA_IND to GMM */
Harald Welte807a5d82010-06-01 11:53:01 +0200833 rc = gsm0408_gprs_rcvmsg(msg, lle->llme);
Harald Weltea2665542010-05-02 09:28:11 +0200834 break;
Harald Weltea2665542010-05-02 09:28:11 +0200835 case GPRS_SAPI_SNDCP3:
836 case GPRS_SAPI_SNDCP5:
837 case GPRS_SAPI_SNDCP9:
838 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +0200839 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
840 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
841 break;
Harald Weltea2665542010-05-02 09:28:11 +0200842 case GPRS_SAPI_SMS:
843 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +0200844 case GPRS_SAPI_TOM2:
845 case GPRS_SAPI_TOM8:
846 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200847 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200848 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200849 rc = -EINVAL;
850 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800851 }
852 }
853
Harald Weltea2665542010-05-02 09:28:11 +0200854 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800855}
Harald Welte807a5d82010-06-01 11:53:01 +0200856
857/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
858int gprs_llgmm_assign(struct gprs_llc_llme *llme,
859 uint32_t old_tlli, uint32_t new_tlli,
860 enum gprs_ciph_algo alg, const uint8_t *kc)
861{
862 unsigned int i;
863
Harald Welted07b4f92010-06-30 23:07:59 +0200864 /* Update the crypto parameters */
Harald Welted07b4f92010-06-30 23:07:59 +0200865 llme->algo = alg;
Harald Welte3e2e1592010-06-30 23:19:23 +0200866 if (alg != GPRS_ALGO_GEA0)
867 memcpy(llme->kc, kc, sizeof(llme->kc));
Harald Welted07b4f92010-06-30 23:07:59 +0200868
Harald Welte807a5d82010-06-01 11:53:01 +0200869 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
870 /* TLLI Assignment 8.3.1 */
871 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
872 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
873 * old is unassigned. Only TLLI new shall be accepted when
874 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +0200875 if (llme->old_tlli != 0xffffffff) {
876 llme->old_tlli = 0xffffffff;
877 llme->tlli = new_tlli;
878 } else {
879 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
880 * TLLI assignmemt according to 8.3.1 */
881 llme->old_tlli = 0xffffffff;
882 llme->tlli = new_tlli;
883 llme->state = GPRS_LLMS_ASSIGNED;
884 /* 8.5.3.1 For all LLE's */
885 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
886 struct gprs_llc_lle *l = &llme->lle[i];
887 l->vu_send = l->vu_recv = 0;
888 l->retrans_ctr = 0;
889 l->state = GPRS_LLES_ASSIGNED_ADM;
890 /* FIXME Set parameters according to table 9 */
891 }
Harald Welte807a5d82010-06-01 11:53:01 +0200892 }
893 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
894 /* TLLI Change 8.3.2 */
895 /* Both TLLI Old and TLLI New are assigned; use New when
896 * (re)transmitting. Accept toth Old and New on Rx */
897 llme->old_tlli = llme->tlli;
898 llme->tlli = new_tlli;
899 llme->state = GPRS_LLMS_ASSIGNED;
900 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
901 /* TLLI Unassignment 8.3.3) */
902 llme->tlli = llme->old_tlli = 0;
903 llme->state = GPRS_LLMS_UNASSIGNED;
904 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
905 struct gprs_llc_lle *l = &llme->lle[i];
906 l->state = GPRS_LLES_UNASSIGNED;
907 }
Harald Weltef7fef482010-06-28 22:18:26 +0200908 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +0200909 } else
910 return -EINVAL;
911
912 return 0;
913}
Harald Welte496aee42010-06-30 19:59:55 +0200914
Harald Welte0c1a3032011-10-16 18:49:05 +0200915/* Chapter 7.2.1.2 LLGMM-RESET.req */
916int gprs_llgmm_reset(struct gprs_llc_llme *llme)
917{
918 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
919 int random = rand();
920
921 /* First XID component must be RESET */
922 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
923 /* randomly select new IOV-UI */
Harald Welte066a0f52011-10-16 18:59:20 +0200924 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random);
Harald Welte0c1a3032011-10-16 18:49:05 +0200925
926 /* FIXME: Start T200, wait for XID response */
927 return gprs_llc_tx_xid(&llme->lle[1], msg, 1);
928}
929
Harald Welte496aee42010-06-30 19:59:55 +0200930int gprs_llc_init(const char *cipher_plugin_path)
931{
932 return gprs_cipher_load(cipher_plugin_path);
933}