blob: 5a74a6343b40a4790f05735aa4e2c5978e36386d [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001/* GPRS LLC protocol implementation as per 3GPP TS 04.64 */
2
Harald Weltea2665542010-05-02 09:28:11 +02003/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte9b455bf2010-03-14 15:45:01 +08004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9b455bf2010-03-14 15:45:01 +080010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte9b455bf2010-03-14 15:45:01 +080016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9b455bf2010-03-14 15:45:01 +080019 *
20 */
21
22#include <errno.h>
Harald Welteeaa614c2010-05-02 11:26:34 +020023#include <stdint.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080024
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010025#include <osmocom/core/msgb.h>
26#include <osmocom/core/linuxlist.h>
27#include <osmocom/core/timer.h>
28#include <osmocom/core/talloc.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080029#include <osmocom/gprs/gprs_bssgp.h>
Harald Weltea2665542010-05-02 09:28:11 +020030
31#include <openbsc/gsm_data.h>
32#include <openbsc/debug.h>
Harald Welte807a5d82010-06-01 11:53:01 +020033#include <openbsc/gprs_sgsn.h>
34#include <openbsc/gprs_gmm.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080035#include <openbsc/gprs_llc.h>
36#include <openbsc/crc24.h>
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +020037#include <openbsc/sgsn.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080038
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +020039enum gprs_llc_cmd {
40 GPRS_LLC_NULL,
41 GPRS_LLC_RR,
42 GPRS_LLC_ACK,
43 GPRS_LLC_RNR,
44 GPRS_LLC_SACK,
45 GPRS_LLC_DM,
46 GPRS_LLC_DISC,
47 GPRS_LLC_UA,
48 GPRS_LLC_SABM,
49 GPRS_LLC_FRMR,
50 GPRS_LLC_XID,
51 GPRS_LLC_UI,
52};
53
54static const struct value_string llc_cmd_strs[] = {
55 { GPRS_LLC_NULL, "NULL" },
56 { GPRS_LLC_RR, "RR" },
57 { GPRS_LLC_ACK, "ACK" },
58 { GPRS_LLC_RNR, "RNR" },
59 { GPRS_LLC_SACK, "SACK" },
60 { GPRS_LLC_DM, "DM" },
61 { GPRS_LLC_DISC, "DISC" },
62 { GPRS_LLC_UA, "UA" },
63 { GPRS_LLC_SABM, "SABM" },
64 { GPRS_LLC_FRMR, "FRMR" },
65 { GPRS_LLC_XID, "XID" },
66 { GPRS_LLC_UI, "UI" },
67 { 0, NULL }
68};
69
70struct gprs_llc_hdr_parsed {
71 uint8_t sapi;
72 uint8_t is_cmd:1,
73 ack_req:1,
74 is_encrypted:1;
75 uint32_t seq_rx;
76 uint32_t seq_tx;
77 uint32_t fcs;
78 uint32_t fcs_calc;
79 uint8_t *data;
80 uint16_t data_len;
81 uint16_t crc_length;
82 enum gprs_llc_cmd cmd;
83};
84
85static struct gprs_llc_llme *llme_alloc(uint32_t tlli);
86
Harald Weltefaa70ff2012-06-17 09:31:16 +080087/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
88 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
89static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
90{
91 struct bssgp_dl_ud_par dup;
92 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
93
Harald Welte8c004962012-07-04 21:53:12 +020094 memset(&dup, 0, sizeof(dup));
95 /* before we have received some identity from the MS, we might
96 * not yet have a MMC context (e.g. XID negotiation of primarly
97 * LLC connection fro GMM sapi). */
98 if (mmctx) {
99 dup.imsi = mmctx->imsi;
100 dup.drx_parms = mmctx->drx_parms;
101 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
102 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
103 }
Harald Weltefaa70ff2012-06-17 09:31:16 +0800104 memcpy(&dup.qos_profile, qos_profile_default,
105 sizeof(qos_profile_default));
106
Harald Weltece95b272012-06-17 13:04:02 +0800107 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +0800108}
109
110
Harald Welte1d9d9442010-06-03 07:11:04 +0200111/* Section 8.9.9 LLC layer parameter default values */
112static const struct gprs_llc_params llc_default_params[] = {
113 [1] = {
114 .t200_201 = 5,
115 .n200 = 3,
116 .n201_u = 400,
117 },
118 [2] = {
119 .t200_201 = 5,
120 .n200 = 3,
121 .n201_u = 270,
122 },
123 [3] = {
124 .iov_i_exp = 27,
125 .t200_201 = 5,
126 .n200 = 3,
127 .n201_u = 500,
128 .n201_i = 1503,
129 .mD = 1520,
130 .mU = 1520,
131 .kD = 16,
132 .kU = 16,
133 },
134 [5] = {
135 .iov_i_exp = 27,
136 .t200_201 = 10,
137 .n200 = 3,
138 .n201_u = 500,
139 .n201_i = 1503,
140 .mD = 760,
141 .mU = 760,
142 .kD = 8,
143 .kU = 8,
144 },
145 [7] = {
146 .t200_201 = 20,
147 .n200 = 3,
148 .n201_u = 270,
149 },
150 [8] = {
151 .t200_201 = 20,
152 .n200 = 3,
153 .n201_u = 270,
154 },
155 [9] = {
156 .iov_i_exp = 27,
157 .t200_201 = 20,
158 .n200 = 3,
159 .n201_u = 500,
160 .n201_i = 1503,
161 .mD = 380,
162 .mU = 380,
163 .kD = 4,
164 .kU = 4,
165 },
166 [11] = {
167 .iov_i_exp = 27,
168 .t200_201 = 40,
169 .n200 = 3,
170 .n201_u = 500,
171 .n201_i = 1503,
172 .mD = 190,
173 .mU = 190,
174 .kD = 2,
175 .kU = 2,
176 },
177};
178
Harald Welte807a5d82010-06-01 11:53:01 +0200179LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200180void *llc_tall_ctx;
181
Harald Weltef0901f02010-12-26 10:39:26 +0100182/* If the TLLI is foreign, return its local version */
183static inline uint32_t tlli_foreign2local(uint32_t tlli)
184{
185 uint32_t new_tlli;
186
187 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
188 new_tlli = tlli | 0x40000000;
189 DEBUGP(DLLC, "TLLI 0x%08x is foreign, converting to "
190 "local TLLI 0x%08x\n", tlli, new_tlli);
191 } else
192 new_tlli = tlli;
193
194 return new_tlli;
195}
196
Harald Weltea2665542010-05-02 09:28:11 +0200197/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200198static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200199{
Harald Welte807a5d82010-06-01 11:53:01 +0200200 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200201
Harald Welte807a5d82010-06-01 11:53:01 +0200202 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
203 if (llme->tlli == tlli || llme->old_tlli == tlli)
204 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200205 }
206 return NULL;
207}
208
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200209/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
210static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
211 uint8_t sapi, enum gprs_llc_cmd cmd)
212{
213 struct gprs_llc_lle *lle;
214
215 /* We already know about this TLLI */
216 lle = lle_by_tlli_sapi(tlli, sapi);
217 if (lle)
218 return lle;
219
220 /* Maybe it is a routing area update but we already know this sapi? */
221 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
222 lle = lle_by_tlli_sapi(tlli_foreign2local(tlli), sapi);
223 if (lle) {
224 LOGP(DLLC, LOGL_NOTICE,
225 "LLC RX: Found a local entry for TLLI 0x%08x\n",
226 tlli);
227 return lle;
228 }
229 }
230
231 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
232 * except UID and XID frames with SAPI=1 */
233 if (sapi == GPRS_SAPI_GMM &&
234 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
235 struct gprs_llc_llme *llme;
236 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
237 llme = llme_alloc(tlli);
238 LOGP(DLLC, LOGL_DEBUG, "LLC RX: unknown TLLI 0x%08x, "
239 "creating LLME on the fly\n", tlli);
240 lle = &llme->lle[sapi];
241 return lle;
242 }
243
244 LOGP(DLLC, LOGL_NOTICE,
245 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
246 tlli, sapi);
247 return NULL;
248}
249
Harald Welte1d9d9442010-06-03 07:11:04 +0200250static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200251{
Harald Welte807a5d82010-06-01 11:53:01 +0200252 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200253
Harald Welte807a5d82010-06-01 11:53:01 +0200254 lle->llme = llme;
255 lle->sapi = sapi;
256 lle->state = GPRS_LLES_UNASSIGNED;
257
Harald Welte1d9d9442010-06-03 07:11:04 +0200258 /* Initialize according to parameters */
259 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200260}
261
262static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
263{
264 struct gprs_llc_llme *llme;
265 uint32_t i;
266
267 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
268 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200269 return NULL;
270
Harald Welte807a5d82010-06-01 11:53:01 +0200271 llme->tlli = tlli;
Harald Welte875840c2010-07-01 11:54:31 +0200272 llme->old_tlli = 0xffffffff;
Harald Welte807a5d82010-06-01 11:53:01 +0200273 llme->state = GPRS_LLMS_UNASSIGNED;
Harald Weltea2665542010-05-02 09:28:11 +0200274
Harald Welte807a5d82010-06-01 11:53:01 +0200275 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
276 lle_init(llme, i);
277
278 llist_add(&llme->list, &gprs_llc_llmes);
279
280 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200281}
282
Harald Weltef7fef482010-06-28 22:18:26 +0200283static void llme_free(struct gprs_llc_llme *llme)
284{
285 llist_del(&llme->list);
286 talloc_free(llme);
287}
288
Harald Welte9b455bf2010-03-14 15:45:01 +0800289#define LLC_ALLOC_SIZE 16384
290#define UI_HDR_LEN 3
291#define N202 4
292#define CRC24_LENGTH 3
293
Harald Welteeaa614c2010-05-02 11:26:34 +0200294static int gprs_llc_fcs(uint8_t *data, unsigned int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800295{
Harald Welteeaa614c2010-05-02 11:26:34 +0200296 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800297
298 fcs_calc = crc24_calc(INIT_CRC24, data, len);
299 fcs_calc = ~fcs_calc;
300 fcs_calc &= 0xffffff;
301
302 return fcs_calc;
303}
304
Harald Welte9b455bf2010-03-14 15:45:01 +0800305static void t200_expired(void *data)
306{
307 struct gprs_llc_lle *lle = data;
308
309 /* 8.5.1.3: Expiry of T200 */
310
Harald Welte1d9d9442010-06-03 07:11:04 +0200311 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800312 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200313 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800314 }
315
316 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200317 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200318 /* FIXME: retransmit SABM */
319 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800320 lle->retrans_ctr++;
321 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200322 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200323 /* FIXME: retransmit DISC */
324 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800325 lle->retrans_ctr++;
326 break;
327 }
328
329}
330
331static void t201_expired(void *data)
332{
333 struct gprs_llc_lle *lle = data;
334
Harald Welte1d9d9442010-06-03 07:11:04 +0200335 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200336 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
337 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800338 lle->retrans_ctr++;
339 }
340}
341
Harald Welte10997d02010-05-03 12:28:12 +0200342int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
343 enum gprs_llc_u_cmd u_cmd, int pf_bit)
344{
345 uint8_t *fcs, *llch;
346 uint8_t addr, ctrl;
347 uint32_t fcs_calc;
348
349 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
350
351 /* Address Field */
352 addr = sapi & 0xf;
353 if (command)
354 addr |= 0x40;
355
356 /* 6.3 Figure 8 */
357 ctrl = 0xe0 | u_cmd;
358 if (pf_bit)
359 ctrl |= 0x10;
360
361 /* prepend LLC UI header */
362 llch = msgb_push(msg, 2);
363 llch[0] = addr;
364 llch[1] = ctrl;
365
366 /* append FCS to end of frame */
367 fcs = msgb_put(msg, 3);
368 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
369 fcs[0] = fcs_calc & 0xff;
370 fcs[1] = (fcs_calc >> 8) & 0xff;
371 fcs[2] = (fcs_calc >> 16) & 0xff;
372
373 /* Identifiers passed down: (BVCI, NSEI) */
374
Harald Welte1ae09c72010-05-13 19:22:55 +0200375 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800376 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200377}
378
379/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200380static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
381 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200382{
383 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200384 msgb_tlli(msg) = lle->llme->tlli;
385 msgb_bvci(msg) = lle->llme->bvci;
386 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200387
Harald Welte0c1a3032011-10-16 18:49:05 +0200388 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200389}
390
Harald Welte9b455bf2010-03-14 15:45:01 +0800391/* Transmit a UI frame over the given SAPI */
Harald Welte56a01452010-05-31 22:12:30 +0200392int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
393 void *mmctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800394{
Harald Weltee6afd602010-05-02 11:19:37 +0200395 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200396 uint8_t *fcs, *llch;
397 uint8_t addr, ctrl[2];
398 uint32_t fcs_calc;
399 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200400 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800401
Harald Weltee6afd602010-05-02 11:19:37 +0200402 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
403
404 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
405 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200406 if (!lle)
407 lle = lle_by_tlli_sapi(tlli_foreign2local(msgb_tlli(msg)), sapi);
Harald Welte807a5d82010-06-01 11:53:01 +0200408 if (!lle) {
409 struct gprs_llc_llme *llme;
Harald Weltef0901f02010-12-26 10:39:26 +0100410 LOGP(DLLC, LOGL_ERROR, "LLC TX: unknown TLLI 0x%08x, "
411 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte807a5d82010-06-01 11:53:01 +0200412 llme = llme_alloc(msgb_tlli(msg));
413 lle = &llme->lle[sapi];
414 }
Harald Welte1d9d9442010-06-03 07:11:04 +0200415
416 if (msg->len > lle->params.n201_u) {
417 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
418 msg->len, lle->params.n201_u);
419 return -EFBIG;
420 }
421
Harald Weltee6afd602010-05-02 11:19:37 +0200422 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200423 lle->llme->bvci = msgb_bvci(msg);
424 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200425
Harald Welted07b4f92010-06-30 23:07:59 +0200426 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200427 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200428 oc = lle->oc_ui_send;
429 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200430 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200431 /* Increment Overflow Counter, if needed */
432 if ((lle->vu_send + 1) / 512)
433 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200434
Harald Welte9b455bf2010-03-14 15:45:01 +0800435 /* Address Field */
436 addr = sapi & 0xf;
437 if (command)
438 addr |= 0x40;
439
440 /* Control Field */
441 ctrl[0] = 0xc0;
442 ctrl[0] |= nu >> 6;
443 ctrl[1] = (nu << 2) & 0xfc;
444 ctrl[1] |= 0x01; /* Protected Mode */
445
446 /* prepend LLC UI header */
447 llch = msgb_push(msg, 3);
448 llch[0] = addr;
449 llch[1] = ctrl[0];
450 llch[2] = ctrl[1];
451
452 /* append FCS to end of frame */
453 fcs = msgb_put(msg, 3);
454 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
455 fcs[0] = fcs_calc & 0xff;
456 fcs[1] = (fcs_calc >> 8) & 0xff;
457 fcs[2] = (fcs_calc >> 16) & 0xff;
458
Harald Welted07b4f92010-06-30 23:07:59 +0200459 /* encrypt information field + FCS, if needed! */
460 if (lle->llme->algo != GPRS_ALGO_GEA0) {
461 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
462 uint16_t crypt_len = (fcs + 3) - (llch + 3);
463 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
464 uint32_t iv;
465 int rc, i;
466 uint64_t kc = *(uint64_t *)&lle->llme->kc;
467
468 /* Compute the 'Input' Paraemeter */
469 iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc);
470
471 /* Compute the keystream that we need to XOR with the data */
472 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
473 kc, iv, GPRS_CIPH_SGSN2MS);
474 if (rc < 0) {
475 LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc);
476 return rc;
477 }
478
479 /* XOR the cipher output with the information field + FCS */
480 for (i = 0; i < crypt_len; i++)
481 *(llch + 3 + i) ^= cipher_out[i];
482
483 /* Mark frame as encrypted */
484 ctrl[1] |= 0x02;
485 }
486
Harald Weltee6afd602010-05-02 11:19:37 +0200487 /* Identifiers passed down: (BVCI, NSEI) */
488
Harald Welte1ae09c72010-05-13 19:22:55 +0200489 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800490 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800491}
492
Harald Welte0c1a3032011-10-16 18:49:05 +0200493/* According to 6.4.1.6 / Figure 11 */
494static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data)
495{
496 uint8_t header_len = 1;
497 uint8_t *cur;
498
499 /* type is a 5-bit field... */
500 if (type > 0x1f)
501 return -EINVAL;
502
503 if (length > 3)
504 header_len = 2;
505
506 cur = msgb_put(msg, length + header_len);
507
508 /* build the header without or with XL bit */
509 if (length <= 3) {
510 *cur++ = (type << 2) | (length & 3);
511 } else {
512 *cur++ = 0x80 | (type << 2) | (length >> 6);
513 *cur++ = (length << 2);
514 }
515
516 /* copy over the payload of the parameter*/
517 memcpy(cur, data, length);
518
519 return length + header_len;
520}
521
522static void rx_llc_xid(struct gprs_llc_lle *lle,
523 struct gprs_llc_hdr_parsed *gph)
524{
525 /* FIXME: 8.5.3.3: check if XID is invalid */
526 if (gph->is_cmd) {
527 /* FIXME: implement XID negotiation using SNDCP */
528 struct msgb *resp;
529 uint8_t *xid;
530 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
531 xid = msgb_put(resp, gph->data_len);
532 memcpy(xid, gph->data, gph->data_len);
533 gprs_llc_tx_xid(lle, resp, 0);
534 } else {
535 /* FIXME: if we had sent a XID reset, send
536 * LLGMM-RESET.conf to GMM */
537 /* FIXME: implement XID negotiation using SNDCP */
538 }
539}
540
Holger Hans Peter Freyther3a6fdcd2010-05-23 21:35:25 +0800541static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
Harald Welte9b455bf2010-03-14 15:45:01 +0800542{
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200543 DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x",
Harald Welte9b455bf2010-03-14 15:45:01 +0800544 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200545 gph->fcs);
Harald Welte9b455bf2010-03-14 15:45:01 +0800546
547 if (gph->cmd)
Harald Welteb61f4032010-05-18 12:31:50 +0200548 DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd));
Harald Welte9b455bf2010-03-14 15:45:01 +0800549
550 if (gph->data)
Harald Weltec6ecafe2010-05-13 19:47:50 +0200551 DEBUGPC(DLLC, "DATA ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800552
Harald Weltec6ecafe2010-05-13 19:47:50 +0200553 DEBUGPC(DLLC, "\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800554}
555static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
556 struct gprs_llc_lle *lle)
557{
558 switch (gph->cmd) {
559 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
560 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200561 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800562 /* start re-establishment (8.7.1) */
563 }
Harald Welte807a5d82010-06-01 11:53:01 +0200564 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800565 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200566 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800567 /* FIXME: process data */
568 break;
569 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
570 /* FIXME: Send UA */
571 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200572 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800573 break;
574 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200575 if (lle->state == GPRS_LLES_LOCAL_EST)
576 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800577 break;
578 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200579 if (lle->state == GPRS_LLES_LOCAL_EST)
580 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800581 break;
582 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
583 break;
584 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200585 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800586 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200587 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400588 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
589 LOGP(DLLC, LOGL_NOTICE,
590 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800591 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200592 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200593
594 /* HACK: non-standard recovery handling. If remote LLE
595 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800596 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200597 * and 'learn' the new sequence number */
598 if (gph->seq_tx != lle->vu_recv_last) {
599 lle->vu_recv_last = gph->seq_tx;
600 lle->vu_recv_duplicates = 0;
601 } else {
602 lle->vu_recv_duplicates++;
603 if (lle->vu_recv_duplicates < 3)
604 return -EIO;
605 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
606 "N(U=%d) after receiving %u duplicates\n",
607 lle->llme ? lle->llme->tlli : -1,
608 gph->seq_tx, lle->vu_recv_duplicates);
609 }
Harald Welteebabdea2010-06-01 18:28:10 +0200610 }
611 /* Increment the sequence number that we expect in the next frame */
612 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200613 /* Increment Overflow Counter */
614 if ((gph->seq_tx + 1) / 512)
615 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200616 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800617 }
618
619 return 0;
620}
621
622/* parse a GPRS LLC header, also check for invalid frames */
623static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800624 uint8_t *llc_hdr, int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800625{
Harald Welteeaa614c2010-05-02 11:26:34 +0200626 uint8_t *ctrl = llc_hdr+1;
Harald Welte9b455bf2010-03-14 15:45:01 +0800627
628 if (len <= CRC24_LENGTH)
629 return -EIO;
630
Harald Welte1b8827a2010-06-30 23:15:57 +0200631 ghp->crc_length = len - CRC24_LENGTH;
Harald Welte9b455bf2010-03-14 15:45:01 +0800632
633 ghp->ack_req = 0;
634
635 /* Section 5.5: FCS */
636 ghp->fcs = *(llc_hdr + len - 3);
637 ghp->fcs |= *(llc_hdr + len - 2) << 8;
638 ghp->fcs |= *(llc_hdr + len - 1) << 16;
639
640 /* Section 6.2.1: invalid PD field */
641 if (llc_hdr[0] & 0x80)
642 return -EIO;
643
644 /* This only works for the MS->SGSN direction */
645 if (llc_hdr[0] & 0x40)
646 ghp->is_cmd = 0;
647 else
648 ghp->is_cmd = 1;
649
650 ghp->sapi = llc_hdr[0] & 0xf;
651
652 /* Section 6.2.3: check for reserved SAPI */
653 switch (ghp->sapi) {
654 case 0:
655 case 4:
656 case 6:
657 case 0xa:
658 case 0xc:
659 case 0xd:
660 case 0xf:
661 return -EINVAL;
662 }
663
664 if ((ctrl[0] & 0x80) == 0) {
665 /* I (Information transfer + Supervisory) format */
Harald Welteeaa614c2010-05-02 11:26:34 +0200666 uint8_t k;
Harald Welte9b455bf2010-03-14 15:45:01 +0800667
668 ghp->data = ctrl + 3;
669
670 if (ctrl[0] & 0x40)
671 ghp->ack_req = 1;
672
673 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
674 ghp->seq_tx |= (ctrl[1] >> 4);
675
676 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
677 ghp->seq_rx |= (ctrl[2] >> 2);
678
679 switch (ctrl[2] & 0x03) {
680 case 0:
681 ghp->cmd = GPRS_LLC_RR;
682 break;
683 case 1:
684 ghp->cmd = GPRS_LLC_ACK;
685 break;
686 case 2:
687 ghp->cmd = GPRS_LLC_RNR;
688 break;
689 case 3:
690 ghp->cmd = GPRS_LLC_SACK;
691 k = ctrl[3] & 0x1f;
692 ghp->data += 1 + k;
693 break;
694 }
Harald Welte5658a1a2010-05-03 13:25:07 +0200695 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800696 } else if ((ctrl[0] & 0xc0) == 0x80) {
697 /* S (Supervisory) format */
698 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200699 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800700
701 if (ctrl[0] & 0x20)
702 ghp->ack_req = 1;
703 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
704 ghp->seq_rx |= (ctrl[1] >> 2);
705
706 switch (ctrl[1] & 0x03) {
707 case 0:
708 ghp->cmd = GPRS_LLC_RR;
709 break;
710 case 1:
711 ghp->cmd = GPRS_LLC_ACK;
712 break;
713 case 2:
714 ghp->cmd = GPRS_LLC_RNR;
715 break;
716 case 3:
717 ghp->cmd = GPRS_LLC_SACK;
718 break;
719 }
720 } else if ((ctrl[0] & 0xe0) == 0xc0) {
721 /* UI (Unconfirmed Inforamtion) format */
Harald Welte1ae09c72010-05-13 19:22:55 +0200722 ghp->cmd = GPRS_LLC_UI;
Harald Welte9b455bf2010-03-14 15:45:01 +0800723 ghp->data = ctrl + 2;
Harald Welte5658a1a2010-05-03 13:25:07 +0200724 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800725
726 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
727 ghp->seq_tx |= (ctrl[1] >> 2);
728 if (ctrl[1] & 0x02) {
729 ghp->is_encrypted = 1;
730 /* FIXME: encryption */
731 }
732 if (ctrl[1] & 0x01) {
733 /* FCS over hdr + all inf fields */
734 } else {
735 /* FCS over hdr + N202 octets (4) */
Harald Welte1b8827a2010-06-30 23:15:57 +0200736 if (ghp->crc_length > UI_HDR_LEN + N202)
737 ghp->crc_length = UI_HDR_LEN + N202;
Harald Welte9b455bf2010-03-14 15:45:01 +0800738 }
739 } else {
740 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
741 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200742 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800743
744 switch (ctrl[0] & 0xf) {
Harald Welte5658a1a2010-05-03 13:25:07 +0200745 case GPRS_LLC_U_NULL_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800746 ghp->cmd = GPRS_LLC_NULL;
747 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200748 case GPRS_LLC_U_DM_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800749 ghp->cmd = GPRS_LLC_DM;
750 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200751 case GPRS_LLC_U_DISC_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800752 ghp->cmd = GPRS_LLC_DISC;
753 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200754 case GPRS_LLC_U_UA_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800755 ghp->cmd = GPRS_LLC_UA;
756 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200757 case GPRS_LLC_U_SABM_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800758 ghp->cmd = GPRS_LLC_SABM;
759 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200760 case GPRS_LLC_U_FRMR_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800761 ghp->cmd = GPRS_LLC_FRMR;
762 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200763 case GPRS_LLC_U_XID:
Harald Welte9b455bf2010-03-14 15:45:01 +0800764 ghp->cmd = GPRS_LLC_XID;
Harald Welte5658a1a2010-05-03 13:25:07 +0200765 ghp->data = ctrl + 1;
766 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800767 break;
768 default:
769 return -EIO;
770 }
771 }
772
Harald Welte9b455bf2010-03-14 15:45:01 +0800773 /* FIXME: parse sack frame */
Harald Welte1ae09c72010-05-13 19:22:55 +0200774 if (ghp->cmd == GPRS_LLC_SACK) {
Harald Welte1b170d12010-05-13 19:49:06 +0200775 LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200776 return -EIO;
777 }
778
779 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800780}
781
Harald Weltea2665542010-05-02 09:28:11 +0200782/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800783int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
784{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200785 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200786 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800787 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200788 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200789 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800790
Harald Welte11d7c102010-05-02 11:54:55 +0200791 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
792
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800793 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800794 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800795 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200796 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200797 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200798 return rc;
799 }
800
Harald Welte807a5d82010-06-01 11:53:01 +0200801 switch (gprs_tlli_type(msgb_tlli(msg))) {
802 case TLLI_LOCAL:
803 case TLLI_FOREIGN:
804 case TLLI_RANDOM:
805 case TLLI_AUXILIARY:
806 break;
807 default:
808 LOGP(DLLC, LOGL_ERROR,
809 "Discarding frame with strange TLLI type\n");
810 break;
811 }
812
Harald Weltea2665542010-05-02 09:28:11 +0200813 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200814 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
815 if (!lle)
816 return 0;
Harald Weltea2665542010-05-02 09:28:11 +0200817
Harald Welted07b4f92010-06-30 23:07:59 +0200818 /* decrypt information field + FCS, if needed! */
819 if (llhp.is_encrypted) {
820 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
821 uint16_t crypt_len = llhp.data_len + 3;
822 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
823 uint32_t iv;
824 uint64_t kc = *(uint64_t *)&lle->llme->kc;
825 int rc, i;
826
827 if (lle->llme->algo == GPRS_ALGO_GEA0) {
828 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
829 "has no KC/Algo! Dropping.\n");
830 return 0;
831 }
832
833 iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx,
834 lle->oc_ui_recv);
835 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
836 kc, iv, GPRS_CIPH_MS2SGSN);
837 if (rc < 0) {
838 LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n",
839 rc);
840 return rc;
841 }
842
843 /* XOR the cipher output with the information field + FCS */
844 for (i = 0; i < crypt_len; i++)
845 *(llhp.data + i) ^= cipher_out[i];
846 } else {
847 if (lle->llme->algo != GPRS_ALGO_GEA0) {
848 LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC "
849 "that is supposed to be encrypted. Dropping.\n");
850 return 0;
851 }
852 }
853
854 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200855 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200856 if (llhp.fcs != llhp.fcs_calc) {
857 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
858 return -EIO;
859 }
860
Harald Welte10997d02010-05-03 12:28:12 +0200861 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200862 lle->llme->bvci = msgb_bvci(msg);
863 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200864
Harald Welte1ae09c72010-05-13 19:22:55 +0200865 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800866 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200867 if (rc < 0)
868 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800869
Harald Welte1ae09c72010-05-13 19:22:55 +0200870 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte9b455bf2010-03-14 15:45:01 +0800871 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200872 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800873 switch (llhp.sapi) {
874 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200875 /* send LL_UNITDATA_IND to GMM */
Harald Welte807a5d82010-06-01 11:53:01 +0200876 rc = gsm0408_gprs_rcvmsg(msg, lle->llme);
Harald Weltea2665542010-05-02 09:28:11 +0200877 break;
Harald Weltea2665542010-05-02 09:28:11 +0200878 case GPRS_SAPI_SNDCP3:
879 case GPRS_SAPI_SNDCP5:
880 case GPRS_SAPI_SNDCP9:
881 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +0200882 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
883 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
884 break;
Harald Weltea2665542010-05-02 09:28:11 +0200885 case GPRS_SAPI_SMS:
886 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +0200887 case GPRS_SAPI_TOM2:
888 case GPRS_SAPI_TOM8:
889 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200890 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200891 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200892 rc = -EINVAL;
893 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800894 }
895 }
896
Harald Weltea2665542010-05-02 09:28:11 +0200897 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800898}
Harald Welte807a5d82010-06-01 11:53:01 +0200899
900/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
901int gprs_llgmm_assign(struct gprs_llc_llme *llme,
902 uint32_t old_tlli, uint32_t new_tlli,
903 enum gprs_ciph_algo alg, const uint8_t *kc)
904{
905 unsigned int i;
906
Harald Welted07b4f92010-06-30 23:07:59 +0200907 /* Update the crypto parameters */
Harald Welted07b4f92010-06-30 23:07:59 +0200908 llme->algo = alg;
Harald Welte3e2e1592010-06-30 23:19:23 +0200909 if (alg != GPRS_ALGO_GEA0)
910 memcpy(llme->kc, kc, sizeof(llme->kc));
Harald Welted07b4f92010-06-30 23:07:59 +0200911
Harald Welte807a5d82010-06-01 11:53:01 +0200912 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
913 /* TLLI Assignment 8.3.1 */
914 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
915 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
916 * old is unassigned. Only TLLI new shall be accepted when
917 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +0200918 if (llme->old_tlli != 0xffffffff) {
919 llme->old_tlli = 0xffffffff;
920 llme->tlli = new_tlli;
921 } else {
922 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
923 * TLLI assignmemt according to 8.3.1 */
924 llme->old_tlli = 0xffffffff;
925 llme->tlli = new_tlli;
926 llme->state = GPRS_LLMS_ASSIGNED;
927 /* 8.5.3.1 For all LLE's */
928 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
929 struct gprs_llc_lle *l = &llme->lle[i];
930 l->vu_send = l->vu_recv = 0;
931 l->retrans_ctr = 0;
932 l->state = GPRS_LLES_ASSIGNED_ADM;
933 /* FIXME Set parameters according to table 9 */
934 }
Harald Welte807a5d82010-06-01 11:53:01 +0200935 }
936 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
937 /* TLLI Change 8.3.2 */
938 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +0200939 * (re)transmitting. Accept both Old and New on Rx */
Harald Welte807a5d82010-06-01 11:53:01 +0200940 llme->old_tlli = llme->tlli;
941 llme->tlli = new_tlli;
942 llme->state = GPRS_LLMS_ASSIGNED;
943 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
944 /* TLLI Unassignment 8.3.3) */
945 llme->tlli = llme->old_tlli = 0;
946 llme->state = GPRS_LLMS_UNASSIGNED;
947 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
948 struct gprs_llc_lle *l = &llme->lle[i];
949 l->state = GPRS_LLES_UNASSIGNED;
950 }
Harald Weltef7fef482010-06-28 22:18:26 +0200951 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +0200952 } else
953 return -EINVAL;
954
955 return 0;
956}
Harald Welte496aee42010-06-30 19:59:55 +0200957
Harald Welte0c1a3032011-10-16 18:49:05 +0200958/* Chapter 7.2.1.2 LLGMM-RESET.req */
959int gprs_llgmm_reset(struct gprs_llc_llme *llme)
960{
961 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
962 int random = rand();
963
964 /* First XID component must be RESET */
965 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
966 /* randomly select new IOV-UI */
Harald Welte066a0f52011-10-16 18:59:20 +0200967 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random);
Harald Welte0c1a3032011-10-16 18:49:05 +0200968
969 /* FIXME: Start T200, wait for XID response */
970 return gprs_llc_tx_xid(&llme->lle[1], msg, 1);
971}
972
Harald Welte496aee42010-06-30 19:59:55 +0200973int gprs_llc_init(const char *cipher_plugin_path)
974{
975 return gprs_cipher_load(cipher_plugin_path);
976}