blob: e2d3160d0bfbe746204d16037ae27c90ff14785e [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
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +020087/* If the TLLI is foreign, return its local version */
88static inline uint32_t tlli_foreign2local(uint32_t tlli)
89{
90 uint32_t new_tlli;
91
92 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
93 new_tlli = tlli | 0x40000000;
94 DEBUGP(DLLC, "TLLI 0x%08x is foreign, converting to "
95 "local TLLI 0x%08x\n", tlli, new_tlli);
96 } else
97 new_tlli = tlli;
98
99 return new_tlli;
100}
101
Harald Weltefaa70ff2012-06-17 09:31:16 +0800102/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
103 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
104static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
105{
106 struct bssgp_dl_ud_par dup;
107 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
108
Harald Welte8c004962012-07-04 21:53:12 +0200109 memset(&dup, 0, sizeof(dup));
110 /* before we have received some identity from the MS, we might
111 * not yet have a MMC context (e.g. XID negotiation of primarly
112 * LLC connection fro GMM sapi). */
113 if (mmctx) {
114 dup.imsi = mmctx->imsi;
115 dup.drx_parms = mmctx->drx_parms;
116 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
117 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +0200118
119 /* make sure we only send it to the right llme */
120 OSMO_ASSERT(msgb_tlli(msg) == mmctx->llme->tlli
121 || msgb_tlli(msg) == mmctx->llme->old_tlli
122 || tlli_foreign2local(msgb_tlli(msg)) == mmctx->llme->tlli
123 || tlli_foreign2local(msgb_tlli(msg)) == mmctx->llme->old_tlli);
Harald Welte8c004962012-07-04 21:53:12 +0200124 }
Harald Weltefaa70ff2012-06-17 09:31:16 +0800125 memcpy(&dup.qos_profile, qos_profile_default,
126 sizeof(qos_profile_default));
127
Harald Weltece95b272012-06-17 13:04:02 +0800128 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +0800129}
130
131
Harald Welte1d9d9442010-06-03 07:11:04 +0200132/* Section 8.9.9 LLC layer parameter default values */
133static const struct gprs_llc_params llc_default_params[] = {
134 [1] = {
135 .t200_201 = 5,
136 .n200 = 3,
137 .n201_u = 400,
138 },
139 [2] = {
140 .t200_201 = 5,
141 .n200 = 3,
142 .n201_u = 270,
143 },
144 [3] = {
145 .iov_i_exp = 27,
146 .t200_201 = 5,
147 .n200 = 3,
148 .n201_u = 500,
149 .n201_i = 1503,
150 .mD = 1520,
151 .mU = 1520,
152 .kD = 16,
153 .kU = 16,
154 },
155 [5] = {
156 .iov_i_exp = 27,
157 .t200_201 = 10,
158 .n200 = 3,
159 .n201_u = 500,
160 .n201_i = 1503,
161 .mD = 760,
162 .mU = 760,
163 .kD = 8,
164 .kU = 8,
165 },
166 [7] = {
167 .t200_201 = 20,
168 .n200 = 3,
169 .n201_u = 270,
170 },
171 [8] = {
172 .t200_201 = 20,
173 .n200 = 3,
174 .n201_u = 270,
175 },
176 [9] = {
177 .iov_i_exp = 27,
178 .t200_201 = 20,
179 .n200 = 3,
180 .n201_u = 500,
181 .n201_i = 1503,
182 .mD = 380,
183 .mU = 380,
184 .kD = 4,
185 .kU = 4,
186 },
187 [11] = {
188 .iov_i_exp = 27,
189 .t200_201 = 40,
190 .n200 = 3,
191 .n201_u = 500,
192 .n201_i = 1503,
193 .mD = 190,
194 .mU = 190,
195 .kD = 2,
196 .kU = 2,
197 },
198};
199
Harald Welte807a5d82010-06-01 11:53:01 +0200200LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200201void *llc_tall_ctx;
202
203/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200204static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200205{
Harald Welte807a5d82010-06-01 11:53:01 +0200206 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200207
Harald Welte807a5d82010-06-01 11:53:01 +0200208 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
209 if (llme->tlli == tlli || llme->old_tlli == tlli)
210 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200211 }
212 return NULL;
213}
214
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200215/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
216static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
217 uint8_t sapi, enum gprs_llc_cmd cmd)
218{
219 struct gprs_llc_lle *lle;
220
221 /* We already know about this TLLI */
222 lle = lle_by_tlli_sapi(tlli, sapi);
223 if (lle)
224 return lle;
225
226 /* Maybe it is a routing area update but we already know this sapi? */
227 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
228 lle = lle_by_tlli_sapi(tlli_foreign2local(tlli), sapi);
229 if (lle) {
230 LOGP(DLLC, LOGL_NOTICE,
231 "LLC RX: Found a local entry for TLLI 0x%08x\n",
232 tlli);
233 return lle;
234 }
235 }
236
237 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
238 * except UID and XID frames with SAPI=1 */
239 if (sapi == GPRS_SAPI_GMM &&
240 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
241 struct gprs_llc_llme *llme;
242 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
243 llme = llme_alloc(tlli);
244 LOGP(DLLC, LOGL_DEBUG, "LLC RX: unknown TLLI 0x%08x, "
245 "creating LLME on the fly\n", tlli);
246 lle = &llme->lle[sapi];
247 return lle;
248 }
249
250 LOGP(DLLC, LOGL_NOTICE,
251 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
252 tlli, sapi);
253 return NULL;
254}
255
Harald Welte1d9d9442010-06-03 07:11:04 +0200256static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200257{
Harald Welte807a5d82010-06-01 11:53:01 +0200258 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200259
Harald Welte807a5d82010-06-01 11:53:01 +0200260 lle->llme = llme;
261 lle->sapi = sapi;
262 lle->state = GPRS_LLES_UNASSIGNED;
263
Harald Welte1d9d9442010-06-03 07:11:04 +0200264 /* Initialize according to parameters */
265 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200266}
267
268static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
269{
270 struct gprs_llc_llme *llme;
271 uint32_t i;
272
273 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
274 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200275 return NULL;
276
Harald Welte807a5d82010-06-01 11:53:01 +0200277 llme->tlli = tlli;
Harald Welte875840c2010-07-01 11:54:31 +0200278 llme->old_tlli = 0xffffffff;
Harald Welte807a5d82010-06-01 11:53:01 +0200279 llme->state = GPRS_LLMS_UNASSIGNED;
Harald Weltea2665542010-05-02 09:28:11 +0200280
Harald Welte807a5d82010-06-01 11:53:01 +0200281 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
282 lle_init(llme, i);
283
284 llist_add(&llme->list, &gprs_llc_llmes);
285
286 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200287}
288
Harald Weltef7fef482010-06-28 22:18:26 +0200289static void llme_free(struct gprs_llc_llme *llme)
290{
291 llist_del(&llme->list);
292 talloc_free(llme);
293}
294
Harald Welte9b455bf2010-03-14 15:45:01 +0800295#define LLC_ALLOC_SIZE 16384
296#define UI_HDR_LEN 3
297#define N202 4
298#define CRC24_LENGTH 3
299
Harald Welteeaa614c2010-05-02 11:26:34 +0200300static int gprs_llc_fcs(uint8_t *data, unsigned int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800301{
Harald Welteeaa614c2010-05-02 11:26:34 +0200302 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800303
304 fcs_calc = crc24_calc(INIT_CRC24, data, len);
305 fcs_calc = ~fcs_calc;
306 fcs_calc &= 0xffffff;
307
308 return fcs_calc;
309}
310
Harald Welte9b455bf2010-03-14 15:45:01 +0800311static void t200_expired(void *data)
312{
313 struct gprs_llc_lle *lle = data;
314
315 /* 8.5.1.3: Expiry of T200 */
316
Harald Welte1d9d9442010-06-03 07:11:04 +0200317 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800318 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200319 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800320 }
321
322 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200323 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200324 /* FIXME: retransmit SABM */
325 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800326 lle->retrans_ctr++;
327 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200328 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200329 /* FIXME: retransmit DISC */
330 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800331 lle->retrans_ctr++;
332 break;
333 }
334
335}
336
337static void t201_expired(void *data)
338{
339 struct gprs_llc_lle *lle = data;
340
Harald Welte1d9d9442010-06-03 07:11:04 +0200341 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200342 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
343 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800344 lle->retrans_ctr++;
345 }
346}
347
Harald Welte10997d02010-05-03 12:28:12 +0200348int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
349 enum gprs_llc_u_cmd u_cmd, int pf_bit)
350{
351 uint8_t *fcs, *llch;
352 uint8_t addr, ctrl;
353 uint32_t fcs_calc;
354
355 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
356
357 /* Address Field */
358 addr = sapi & 0xf;
359 if (command)
360 addr |= 0x40;
361
362 /* 6.3 Figure 8 */
363 ctrl = 0xe0 | u_cmd;
364 if (pf_bit)
365 ctrl |= 0x10;
366
367 /* prepend LLC UI header */
368 llch = msgb_push(msg, 2);
369 llch[0] = addr;
370 llch[1] = ctrl;
371
372 /* append FCS to end of frame */
373 fcs = msgb_put(msg, 3);
374 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
375 fcs[0] = fcs_calc & 0xff;
376 fcs[1] = (fcs_calc >> 8) & 0xff;
377 fcs[2] = (fcs_calc >> 16) & 0xff;
378
379 /* Identifiers passed down: (BVCI, NSEI) */
380
Harald Welte1ae09c72010-05-13 19:22:55 +0200381 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800382 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200383}
384
385/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200386static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
387 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200388{
389 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200390 msgb_tlli(msg) = lle->llme->tlli;
391 msgb_bvci(msg) = lle->llme->bvci;
392 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200393
Harald Welte0c1a3032011-10-16 18:49:05 +0200394 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200395}
396
Harald Welte9b455bf2010-03-14 15:45:01 +0800397/* Transmit a UI frame over the given SAPI */
Harald Welte56a01452010-05-31 22:12:30 +0200398int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
399 void *mmctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800400{
Harald Weltee6afd602010-05-02 11:19:37 +0200401 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200402 uint8_t *fcs, *llch;
403 uint8_t addr, ctrl[2];
404 uint32_t fcs_calc;
405 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200406 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800407
Harald Weltee6afd602010-05-02 11:19:37 +0200408 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
409
410 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
411 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200412 if (!lle)
413 lle = lle_by_tlli_sapi(tlli_foreign2local(msgb_tlli(msg)), sapi);
Harald Welte807a5d82010-06-01 11:53:01 +0200414 if (!lle) {
415 struct gprs_llc_llme *llme;
Harald Weltef0901f02010-12-26 10:39:26 +0100416 LOGP(DLLC, LOGL_ERROR, "LLC TX: unknown TLLI 0x%08x, "
417 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte807a5d82010-06-01 11:53:01 +0200418 llme = llme_alloc(msgb_tlli(msg));
419 lle = &llme->lle[sapi];
420 }
Harald Welte1d9d9442010-06-03 07:11:04 +0200421
422 if (msg->len > lle->params.n201_u) {
423 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
424 msg->len, lle->params.n201_u);
425 return -EFBIG;
426 }
427
Harald Weltee6afd602010-05-02 11:19:37 +0200428 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200429 lle->llme->bvci = msgb_bvci(msg);
430 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200431
Harald Welted07b4f92010-06-30 23:07:59 +0200432 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200433 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200434 oc = lle->oc_ui_send;
435 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200436 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200437 /* Increment Overflow Counter, if needed */
438 if ((lle->vu_send + 1) / 512)
439 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200440
Harald Welte9b455bf2010-03-14 15:45:01 +0800441 /* Address Field */
442 addr = sapi & 0xf;
443 if (command)
444 addr |= 0x40;
445
446 /* Control Field */
447 ctrl[0] = 0xc0;
448 ctrl[0] |= nu >> 6;
449 ctrl[1] = (nu << 2) & 0xfc;
450 ctrl[1] |= 0x01; /* Protected Mode */
451
452 /* prepend LLC UI header */
453 llch = msgb_push(msg, 3);
454 llch[0] = addr;
455 llch[1] = ctrl[0];
456 llch[2] = ctrl[1];
457
458 /* append FCS to end of frame */
459 fcs = msgb_put(msg, 3);
460 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
461 fcs[0] = fcs_calc & 0xff;
462 fcs[1] = (fcs_calc >> 8) & 0xff;
463 fcs[2] = (fcs_calc >> 16) & 0xff;
464
Harald Welted07b4f92010-06-30 23:07:59 +0200465 /* encrypt information field + FCS, if needed! */
466 if (lle->llme->algo != GPRS_ALGO_GEA0) {
467 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
468 uint16_t crypt_len = (fcs + 3) - (llch + 3);
469 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
470 uint32_t iv;
471 int rc, i;
472 uint64_t kc = *(uint64_t *)&lle->llme->kc;
473
474 /* Compute the 'Input' Paraemeter */
475 iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc);
476
477 /* Compute the keystream that we need to XOR with the data */
478 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
479 kc, iv, GPRS_CIPH_SGSN2MS);
480 if (rc < 0) {
481 LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc);
482 return rc;
483 }
484
485 /* XOR the cipher output with the information field + FCS */
486 for (i = 0; i < crypt_len; i++)
487 *(llch + 3 + i) ^= cipher_out[i];
488
489 /* Mark frame as encrypted */
490 ctrl[1] |= 0x02;
491 }
492
Harald Weltee6afd602010-05-02 11:19:37 +0200493 /* Identifiers passed down: (BVCI, NSEI) */
494
Harald Welte1ae09c72010-05-13 19:22:55 +0200495 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800496 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800497}
498
Harald Welte0c1a3032011-10-16 18:49:05 +0200499/* According to 6.4.1.6 / Figure 11 */
500static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data)
501{
502 uint8_t header_len = 1;
503 uint8_t *cur;
504
505 /* type is a 5-bit field... */
506 if (type > 0x1f)
507 return -EINVAL;
508
509 if (length > 3)
510 header_len = 2;
511
512 cur = msgb_put(msg, length + header_len);
513
514 /* build the header without or with XL bit */
515 if (length <= 3) {
516 *cur++ = (type << 2) | (length & 3);
517 } else {
518 *cur++ = 0x80 | (type << 2) | (length >> 6);
519 *cur++ = (length << 2);
520 }
521
522 /* copy over the payload of the parameter*/
523 memcpy(cur, data, length);
524
525 return length + header_len;
526}
527
528static void rx_llc_xid(struct gprs_llc_lle *lle,
529 struct gprs_llc_hdr_parsed *gph)
530{
531 /* FIXME: 8.5.3.3: check if XID is invalid */
532 if (gph->is_cmd) {
533 /* FIXME: implement XID negotiation using SNDCP */
534 struct msgb *resp;
535 uint8_t *xid;
536 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
537 xid = msgb_put(resp, gph->data_len);
538 memcpy(xid, gph->data, gph->data_len);
539 gprs_llc_tx_xid(lle, resp, 0);
540 } else {
541 /* FIXME: if we had sent a XID reset, send
542 * LLGMM-RESET.conf to GMM */
543 /* FIXME: implement XID negotiation using SNDCP */
544 }
545}
546
Holger Hans Peter Freyther3a6fdcd2010-05-23 21:35:25 +0800547static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
Harald Welte9b455bf2010-03-14 15:45:01 +0800548{
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200549 DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x",
Harald Welte9b455bf2010-03-14 15:45:01 +0800550 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200551 gph->fcs);
Harald Welte9b455bf2010-03-14 15:45:01 +0800552
553 if (gph->cmd)
Harald Welteb61f4032010-05-18 12:31:50 +0200554 DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd));
Harald Welte9b455bf2010-03-14 15:45:01 +0800555
556 if (gph->data)
Harald Weltec6ecafe2010-05-13 19:47:50 +0200557 DEBUGPC(DLLC, "DATA ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800558
Harald Weltec6ecafe2010-05-13 19:47:50 +0200559 DEBUGPC(DLLC, "\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800560}
561static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
562 struct gprs_llc_lle *lle)
563{
564 switch (gph->cmd) {
565 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
566 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200567 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800568 /* start re-establishment (8.7.1) */
569 }
Harald Welte807a5d82010-06-01 11:53:01 +0200570 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800571 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200572 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800573 /* FIXME: process data */
574 break;
575 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
576 /* FIXME: Send UA */
577 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200578 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800579 break;
580 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200581 if (lle->state == GPRS_LLES_LOCAL_EST)
582 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800583 break;
584 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200585 if (lle->state == GPRS_LLES_LOCAL_EST)
586 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800587 break;
588 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
589 break;
590 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200591 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800592 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200593 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400594 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
595 LOGP(DLLC, LOGL_NOTICE,
596 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800597 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200598 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200599
600 /* HACK: non-standard recovery handling. If remote LLE
601 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800602 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200603 * and 'learn' the new sequence number */
604 if (gph->seq_tx != lle->vu_recv_last) {
605 lle->vu_recv_last = gph->seq_tx;
606 lle->vu_recv_duplicates = 0;
607 } else {
608 lle->vu_recv_duplicates++;
609 if (lle->vu_recv_duplicates < 3)
610 return -EIO;
611 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
612 "N(U=%d) after receiving %u duplicates\n",
613 lle->llme ? lle->llme->tlli : -1,
614 gph->seq_tx, lle->vu_recv_duplicates);
615 }
Harald Welteebabdea2010-06-01 18:28:10 +0200616 }
617 /* Increment the sequence number that we expect in the next frame */
618 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200619 /* Increment Overflow Counter */
620 if ((gph->seq_tx + 1) / 512)
621 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200622 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800623 }
624
625 return 0;
626}
627
628/* parse a GPRS LLC header, also check for invalid frames */
629static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800630 uint8_t *llc_hdr, int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800631{
Harald Welteeaa614c2010-05-02 11:26:34 +0200632 uint8_t *ctrl = llc_hdr+1;
Harald Welte9b455bf2010-03-14 15:45:01 +0800633
634 if (len <= CRC24_LENGTH)
635 return -EIO;
636
Harald Welte1b8827a2010-06-30 23:15:57 +0200637 ghp->crc_length = len - CRC24_LENGTH;
Harald Welte9b455bf2010-03-14 15:45:01 +0800638
639 ghp->ack_req = 0;
640
641 /* Section 5.5: FCS */
642 ghp->fcs = *(llc_hdr + len - 3);
643 ghp->fcs |= *(llc_hdr + len - 2) << 8;
644 ghp->fcs |= *(llc_hdr + len - 1) << 16;
645
646 /* Section 6.2.1: invalid PD field */
647 if (llc_hdr[0] & 0x80)
648 return -EIO;
649
650 /* This only works for the MS->SGSN direction */
651 if (llc_hdr[0] & 0x40)
652 ghp->is_cmd = 0;
653 else
654 ghp->is_cmd = 1;
655
656 ghp->sapi = llc_hdr[0] & 0xf;
657
658 /* Section 6.2.3: check for reserved SAPI */
659 switch (ghp->sapi) {
660 case 0:
661 case 4:
662 case 6:
663 case 0xa:
664 case 0xc:
665 case 0xd:
666 case 0xf:
667 return -EINVAL;
668 }
669
670 if ((ctrl[0] & 0x80) == 0) {
671 /* I (Information transfer + Supervisory) format */
Harald Welteeaa614c2010-05-02 11:26:34 +0200672 uint8_t k;
Harald Welte9b455bf2010-03-14 15:45:01 +0800673
674 ghp->data = ctrl + 3;
675
676 if (ctrl[0] & 0x40)
677 ghp->ack_req = 1;
678
679 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
680 ghp->seq_tx |= (ctrl[1] >> 4);
681
682 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
683 ghp->seq_rx |= (ctrl[2] >> 2);
684
685 switch (ctrl[2] & 0x03) {
686 case 0:
687 ghp->cmd = GPRS_LLC_RR;
688 break;
689 case 1:
690 ghp->cmd = GPRS_LLC_ACK;
691 break;
692 case 2:
693 ghp->cmd = GPRS_LLC_RNR;
694 break;
695 case 3:
696 ghp->cmd = GPRS_LLC_SACK;
697 k = ctrl[3] & 0x1f;
698 ghp->data += 1 + k;
699 break;
700 }
Harald Welte5658a1a2010-05-03 13:25:07 +0200701 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800702 } else if ((ctrl[0] & 0xc0) == 0x80) {
703 /* S (Supervisory) format */
704 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200705 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800706
707 if (ctrl[0] & 0x20)
708 ghp->ack_req = 1;
709 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
710 ghp->seq_rx |= (ctrl[1] >> 2);
711
712 switch (ctrl[1] & 0x03) {
713 case 0:
714 ghp->cmd = GPRS_LLC_RR;
715 break;
716 case 1:
717 ghp->cmd = GPRS_LLC_ACK;
718 break;
719 case 2:
720 ghp->cmd = GPRS_LLC_RNR;
721 break;
722 case 3:
723 ghp->cmd = GPRS_LLC_SACK;
724 break;
725 }
726 } else if ((ctrl[0] & 0xe0) == 0xc0) {
727 /* UI (Unconfirmed Inforamtion) format */
Harald Welte1ae09c72010-05-13 19:22:55 +0200728 ghp->cmd = GPRS_LLC_UI;
Harald Welte9b455bf2010-03-14 15:45:01 +0800729 ghp->data = ctrl + 2;
Harald Welte5658a1a2010-05-03 13:25:07 +0200730 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800731
732 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
733 ghp->seq_tx |= (ctrl[1] >> 2);
734 if (ctrl[1] & 0x02) {
735 ghp->is_encrypted = 1;
736 /* FIXME: encryption */
737 }
738 if (ctrl[1] & 0x01) {
739 /* FCS over hdr + all inf fields */
740 } else {
741 /* FCS over hdr + N202 octets (4) */
Harald Welte1b8827a2010-06-30 23:15:57 +0200742 if (ghp->crc_length > UI_HDR_LEN + N202)
743 ghp->crc_length = UI_HDR_LEN + N202;
Harald Welte9b455bf2010-03-14 15:45:01 +0800744 }
745 } else {
746 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
747 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200748 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800749
750 switch (ctrl[0] & 0xf) {
Harald Welte5658a1a2010-05-03 13:25:07 +0200751 case GPRS_LLC_U_NULL_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800752 ghp->cmd = GPRS_LLC_NULL;
753 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200754 case GPRS_LLC_U_DM_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800755 ghp->cmd = GPRS_LLC_DM;
756 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200757 case GPRS_LLC_U_DISC_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800758 ghp->cmd = GPRS_LLC_DISC;
759 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200760 case GPRS_LLC_U_UA_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800761 ghp->cmd = GPRS_LLC_UA;
762 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200763 case GPRS_LLC_U_SABM_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800764 ghp->cmd = GPRS_LLC_SABM;
765 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200766 case GPRS_LLC_U_FRMR_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800767 ghp->cmd = GPRS_LLC_FRMR;
768 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200769 case GPRS_LLC_U_XID:
Harald Welte9b455bf2010-03-14 15:45:01 +0800770 ghp->cmd = GPRS_LLC_XID;
Harald Welte5658a1a2010-05-03 13:25:07 +0200771 ghp->data = ctrl + 1;
772 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800773 break;
774 default:
775 return -EIO;
776 }
777 }
778
Harald Welte9b455bf2010-03-14 15:45:01 +0800779 /* FIXME: parse sack frame */
Harald Welte1ae09c72010-05-13 19:22:55 +0200780 if (ghp->cmd == GPRS_LLC_SACK) {
Harald Welte1b170d12010-05-13 19:49:06 +0200781 LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200782 return -EIO;
783 }
784
785 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800786}
787
Harald Weltea2665542010-05-02 09:28:11 +0200788/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800789int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
790{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200791 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200792 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800793 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200794 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200795 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800796
Harald Welte11d7c102010-05-02 11:54:55 +0200797 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
798
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800799 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800800 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800801 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200802 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200803 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200804 return rc;
805 }
806
Harald Welte807a5d82010-06-01 11:53:01 +0200807 switch (gprs_tlli_type(msgb_tlli(msg))) {
808 case TLLI_LOCAL:
809 case TLLI_FOREIGN:
810 case TLLI_RANDOM:
811 case TLLI_AUXILIARY:
812 break;
813 default:
814 LOGP(DLLC, LOGL_ERROR,
815 "Discarding frame with strange TLLI type\n");
816 break;
817 }
818
Harald Weltea2665542010-05-02 09:28:11 +0200819 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200820 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
821 if (!lle)
822 return 0;
Harald Weltea2665542010-05-02 09:28:11 +0200823
Harald Welted07b4f92010-06-30 23:07:59 +0200824 /* decrypt information field + FCS, if needed! */
825 if (llhp.is_encrypted) {
826 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
827 uint16_t crypt_len = llhp.data_len + 3;
828 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
829 uint32_t iv;
830 uint64_t kc = *(uint64_t *)&lle->llme->kc;
831 int rc, i;
832
833 if (lle->llme->algo == GPRS_ALGO_GEA0) {
834 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
835 "has no KC/Algo! Dropping.\n");
836 return 0;
837 }
838
839 iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx,
840 lle->oc_ui_recv);
841 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
842 kc, iv, GPRS_CIPH_MS2SGSN);
843 if (rc < 0) {
844 LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n",
845 rc);
846 return rc;
847 }
848
849 /* XOR the cipher output with the information field + FCS */
850 for (i = 0; i < crypt_len; i++)
851 *(llhp.data + i) ^= cipher_out[i];
852 } else {
853 if (lle->llme->algo != GPRS_ALGO_GEA0) {
854 LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC "
855 "that is supposed to be encrypted. Dropping.\n");
856 return 0;
857 }
858 }
859
860 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200861 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200862 if (llhp.fcs != llhp.fcs_calc) {
863 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
864 return -EIO;
865 }
866
Harald Welte10997d02010-05-03 12:28:12 +0200867 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200868 lle->llme->bvci = msgb_bvci(msg);
869 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200870
Harald Welte1ae09c72010-05-13 19:22:55 +0200871 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800872 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200873 if (rc < 0)
874 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800875
Harald Welte1ae09c72010-05-13 19:22:55 +0200876 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte9b455bf2010-03-14 15:45:01 +0800877 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200878 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800879 switch (llhp.sapi) {
880 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200881 /* send LL_UNITDATA_IND to GMM */
Harald Welte807a5d82010-06-01 11:53:01 +0200882 rc = gsm0408_gprs_rcvmsg(msg, lle->llme);
Harald Weltea2665542010-05-02 09:28:11 +0200883 break;
Harald Weltea2665542010-05-02 09:28:11 +0200884 case GPRS_SAPI_SNDCP3:
885 case GPRS_SAPI_SNDCP5:
886 case GPRS_SAPI_SNDCP9:
887 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +0200888 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
889 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
890 break;
Harald Weltea2665542010-05-02 09:28:11 +0200891 case GPRS_SAPI_SMS:
892 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +0200893 case GPRS_SAPI_TOM2:
894 case GPRS_SAPI_TOM8:
895 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200896 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200897 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200898 rc = -EINVAL;
899 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800900 }
901 }
902
Harald Weltea2665542010-05-02 09:28:11 +0200903 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800904}
Harald Welte807a5d82010-06-01 11:53:01 +0200905
906/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
907int gprs_llgmm_assign(struct gprs_llc_llme *llme,
908 uint32_t old_tlli, uint32_t new_tlli,
909 enum gprs_ciph_algo alg, const uint8_t *kc)
910{
911 unsigned int i;
912
Harald Welted07b4f92010-06-30 23:07:59 +0200913 /* Update the crypto parameters */
Harald Welted07b4f92010-06-30 23:07:59 +0200914 llme->algo = alg;
Harald Welte3e2e1592010-06-30 23:19:23 +0200915 if (alg != GPRS_ALGO_GEA0)
916 memcpy(llme->kc, kc, sizeof(llme->kc));
Harald Welted07b4f92010-06-30 23:07:59 +0200917
Harald Welte807a5d82010-06-01 11:53:01 +0200918 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
919 /* TLLI Assignment 8.3.1 */
920 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
921 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
922 * old is unassigned. Only TLLI new shall be accepted when
923 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +0200924 if (llme->old_tlli != 0xffffffff) {
925 llme->old_tlli = 0xffffffff;
926 llme->tlli = new_tlli;
927 } else {
928 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
929 * TLLI assignmemt according to 8.3.1 */
930 llme->old_tlli = 0xffffffff;
931 llme->tlli = new_tlli;
932 llme->state = GPRS_LLMS_ASSIGNED;
933 /* 8.5.3.1 For all LLE's */
934 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
935 struct gprs_llc_lle *l = &llme->lle[i];
936 l->vu_send = l->vu_recv = 0;
937 l->retrans_ctr = 0;
938 l->state = GPRS_LLES_ASSIGNED_ADM;
939 /* FIXME Set parameters according to table 9 */
940 }
Harald Welte807a5d82010-06-01 11:53:01 +0200941 }
942 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
943 /* TLLI Change 8.3.2 */
944 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +0200945 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freytheraa93bac2013-07-31 11:20:37 +0200946 llme->old_tlli = old_tlli;
Harald Welte807a5d82010-06-01 11:53:01 +0200947 llme->tlli = new_tlli;
948 llme->state = GPRS_LLMS_ASSIGNED;
949 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
950 /* TLLI Unassignment 8.3.3) */
951 llme->tlli = llme->old_tlli = 0;
952 llme->state = GPRS_LLMS_UNASSIGNED;
953 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
954 struct gprs_llc_lle *l = &llme->lle[i];
955 l->state = GPRS_LLES_UNASSIGNED;
956 }
Harald Weltef7fef482010-06-28 22:18:26 +0200957 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +0200958 } else
959 return -EINVAL;
960
961 return 0;
962}
Harald Welte496aee42010-06-30 19:59:55 +0200963
Harald Welte0c1a3032011-10-16 18:49:05 +0200964/* Chapter 7.2.1.2 LLGMM-RESET.req */
965int gprs_llgmm_reset(struct gprs_llc_llme *llme)
966{
967 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
968 int random = rand();
969
970 /* First XID component must be RESET */
971 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
972 /* randomly select new IOV-UI */
Harald Welte066a0f52011-10-16 18:59:20 +0200973 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random);
Harald Welte0c1a3032011-10-16 18:49:05 +0200974
975 /* FIXME: Start T200, wait for XID response */
976 return gprs_llc_tx_xid(&llme->lle[1], msg, 1);
977}
978
Harald Welte496aee42010-06-30 19:59:55 +0200979int gprs_llc_init(const char *cipher_plugin_path)
980{
981 return gprs_cipher_load(cipher_plugin_path);
982}