blob: 1e40dcba6676e379fd73d92d394d806ea652f4d1 [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
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200311#if 0
312/* FIXME: Unused code... */
Harald Welte9b455bf2010-03-14 15:45:01 +0800313static void t200_expired(void *data)
314{
315 struct gprs_llc_lle *lle = data;
316
317 /* 8.5.1.3: Expiry of T200 */
318
Harald Welte1d9d9442010-06-03 07:11:04 +0200319 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800320 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200321 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800322 }
323
324 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200325 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200326 /* FIXME: retransmit SABM */
327 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800328 lle->retrans_ctr++;
329 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200330 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200331 /* FIXME: retransmit DISC */
332 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800333 lle->retrans_ctr++;
334 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200335 default:
336 LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state);
337 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800338 }
339
340}
341
342static void t201_expired(void *data)
343{
344 struct gprs_llc_lle *lle = data;
345
Harald Welte1d9d9442010-06-03 07:11:04 +0200346 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200347 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
348 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800349 lle->retrans_ctr++;
350 }
351}
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200352#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800353
Harald Welte10997d02010-05-03 12:28:12 +0200354int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
355 enum gprs_llc_u_cmd u_cmd, int pf_bit)
356{
357 uint8_t *fcs, *llch;
358 uint8_t addr, ctrl;
359 uint32_t fcs_calc;
360
361 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
362
363 /* Address Field */
364 addr = sapi & 0xf;
365 if (command)
366 addr |= 0x40;
367
368 /* 6.3 Figure 8 */
369 ctrl = 0xe0 | u_cmd;
370 if (pf_bit)
371 ctrl |= 0x10;
372
373 /* prepend LLC UI header */
374 llch = msgb_push(msg, 2);
375 llch[0] = addr;
376 llch[1] = ctrl;
377
378 /* append FCS to end of frame */
379 fcs = msgb_put(msg, 3);
380 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
381 fcs[0] = fcs_calc & 0xff;
382 fcs[1] = (fcs_calc >> 8) & 0xff;
383 fcs[2] = (fcs_calc >> 16) & 0xff;
384
385 /* Identifiers passed down: (BVCI, NSEI) */
386
Harald Welte1ae09c72010-05-13 19:22:55 +0200387 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800388 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200389}
390
391/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200392static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
393 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200394{
395 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200396 msgb_tlli(msg) = lle->llme->tlli;
397 msgb_bvci(msg) = lle->llme->bvci;
398 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200399
Harald Welte0c1a3032011-10-16 18:49:05 +0200400 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200401}
402
Harald Welte9b455bf2010-03-14 15:45:01 +0800403/* Transmit a UI frame over the given SAPI */
Harald Welte56a01452010-05-31 22:12:30 +0200404int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
405 void *mmctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800406{
Harald Weltee6afd602010-05-02 11:19:37 +0200407 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200408 uint8_t *fcs, *llch;
409 uint8_t addr, ctrl[2];
410 uint32_t fcs_calc;
411 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200412 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800413
Harald Weltee6afd602010-05-02 11:19:37 +0200414 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
415
416 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
417 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200418 if (!lle)
419 lle = lle_by_tlli_sapi(tlli_foreign2local(msgb_tlli(msg)), sapi);
Harald Welte807a5d82010-06-01 11:53:01 +0200420 if (!lle) {
421 struct gprs_llc_llme *llme;
Harald Weltef0901f02010-12-26 10:39:26 +0100422 LOGP(DLLC, LOGL_ERROR, "LLC TX: unknown TLLI 0x%08x, "
423 "creating LLME on the fly\n", msgb_tlli(msg));
Harald Welte807a5d82010-06-01 11:53:01 +0200424 llme = llme_alloc(msgb_tlli(msg));
425 lle = &llme->lle[sapi];
426 }
Harald Welte1d9d9442010-06-03 07:11:04 +0200427
428 if (msg->len > lle->params.n201_u) {
429 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
430 msg->len, lle->params.n201_u);
431 return -EFBIG;
432 }
433
Harald Weltee6afd602010-05-02 11:19:37 +0200434 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200435 lle->llme->bvci = msgb_bvci(msg);
436 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200437
Harald Welted07b4f92010-06-30 23:07:59 +0200438 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200439 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200440 oc = lle->oc_ui_send;
441 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200442 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200443 /* Increment Overflow Counter, if needed */
444 if ((lle->vu_send + 1) / 512)
445 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200446
Harald Welte9b455bf2010-03-14 15:45:01 +0800447 /* Address Field */
448 addr = sapi & 0xf;
449 if (command)
450 addr |= 0x40;
451
452 /* Control Field */
453 ctrl[0] = 0xc0;
454 ctrl[0] |= nu >> 6;
455 ctrl[1] = (nu << 2) & 0xfc;
456 ctrl[1] |= 0x01; /* Protected Mode */
457
458 /* prepend LLC UI header */
459 llch = msgb_push(msg, 3);
460 llch[0] = addr;
461 llch[1] = ctrl[0];
462 llch[2] = ctrl[1];
463
464 /* append FCS to end of frame */
465 fcs = msgb_put(msg, 3);
466 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
467 fcs[0] = fcs_calc & 0xff;
468 fcs[1] = (fcs_calc >> 8) & 0xff;
469 fcs[2] = (fcs_calc >> 16) & 0xff;
470
Harald Welted07b4f92010-06-30 23:07:59 +0200471 /* encrypt information field + FCS, if needed! */
472 if (lle->llme->algo != GPRS_ALGO_GEA0) {
473 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
474 uint16_t crypt_len = (fcs + 3) - (llch + 3);
475 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
476 uint32_t iv;
477 int rc, i;
478 uint64_t kc = *(uint64_t *)&lle->llme->kc;
479
480 /* Compute the 'Input' Paraemeter */
481 iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc);
482
483 /* Compute the keystream that we need to XOR with the data */
484 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
485 kc, iv, GPRS_CIPH_SGSN2MS);
486 if (rc < 0) {
487 LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc);
488 return rc;
489 }
490
491 /* XOR the cipher output with the information field + FCS */
492 for (i = 0; i < crypt_len; i++)
493 *(llch + 3 + i) ^= cipher_out[i];
494
495 /* Mark frame as encrypted */
496 ctrl[1] |= 0x02;
497 }
498
Harald Weltee6afd602010-05-02 11:19:37 +0200499 /* Identifiers passed down: (BVCI, NSEI) */
500
Harald Welte1ae09c72010-05-13 19:22:55 +0200501 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800502 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800503}
504
Harald Welte0c1a3032011-10-16 18:49:05 +0200505/* According to 6.4.1.6 / Figure 11 */
506static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data)
507{
508 uint8_t header_len = 1;
509 uint8_t *cur;
510
511 /* type is a 5-bit field... */
512 if (type > 0x1f)
513 return -EINVAL;
514
515 if (length > 3)
516 header_len = 2;
517
518 cur = msgb_put(msg, length + header_len);
519
520 /* build the header without or with XL bit */
521 if (length <= 3) {
522 *cur++ = (type << 2) | (length & 3);
523 } else {
524 *cur++ = 0x80 | (type << 2) | (length >> 6);
525 *cur++ = (length << 2);
526 }
527
528 /* copy over the payload of the parameter*/
529 memcpy(cur, data, length);
530
531 return length + header_len;
532}
533
534static void rx_llc_xid(struct gprs_llc_lle *lle,
535 struct gprs_llc_hdr_parsed *gph)
536{
537 /* FIXME: 8.5.3.3: check if XID is invalid */
538 if (gph->is_cmd) {
539 /* FIXME: implement XID negotiation using SNDCP */
540 struct msgb *resp;
541 uint8_t *xid;
542 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
543 xid = msgb_put(resp, gph->data_len);
544 memcpy(xid, gph->data, gph->data_len);
545 gprs_llc_tx_xid(lle, resp, 0);
546 } else {
547 /* FIXME: if we had sent a XID reset, send
548 * LLGMM-RESET.conf to GMM */
549 /* FIXME: implement XID negotiation using SNDCP */
550 }
551}
552
Holger Hans Peter Freyther3a6fdcd2010-05-23 21:35:25 +0800553static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
Harald Welte9b455bf2010-03-14 15:45:01 +0800554{
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200555 DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x",
Harald Welte9b455bf2010-03-14 15:45:01 +0800556 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
Sylvain Munaut6f3850f2010-07-03 22:03:02 +0200557 gph->fcs);
Harald Welte9b455bf2010-03-14 15:45:01 +0800558
559 if (gph->cmd)
Harald Welteb61f4032010-05-18 12:31:50 +0200560 DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd));
Harald Welte9b455bf2010-03-14 15:45:01 +0800561
562 if (gph->data)
Harald Weltec6ecafe2010-05-13 19:47:50 +0200563 DEBUGPC(DLLC, "DATA ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800564
Harald Weltec6ecafe2010-05-13 19:47:50 +0200565 DEBUGPC(DLLC, "\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800566}
567static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
568 struct gprs_llc_lle *lle)
569{
570 switch (gph->cmd) {
571 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
572 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200573 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800574 /* start re-establishment (8.7.1) */
575 }
Harald Welte807a5d82010-06-01 11:53:01 +0200576 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800577 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200578 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800579 /* FIXME: process data */
580 break;
581 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
582 /* FIXME: Send UA */
583 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200584 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800585 break;
586 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200587 if (lle->state == GPRS_LLES_LOCAL_EST)
588 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800589 break;
590 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200591 if (lle->state == GPRS_LLES_LOCAL_EST)
592 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800593 break;
594 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
595 break;
596 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200597 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800598 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200599 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400600 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
601 LOGP(DLLC, LOGL_NOTICE,
602 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800603 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200604 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200605
606 /* HACK: non-standard recovery handling. If remote LLE
607 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800608 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200609 * and 'learn' the new sequence number */
610 if (gph->seq_tx != lle->vu_recv_last) {
611 lle->vu_recv_last = gph->seq_tx;
612 lle->vu_recv_duplicates = 0;
613 } else {
614 lle->vu_recv_duplicates++;
615 if (lle->vu_recv_duplicates < 3)
616 return -EIO;
617 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
618 "N(U=%d) after receiving %u duplicates\n",
619 lle->llme ? lle->llme->tlli : -1,
620 gph->seq_tx, lle->vu_recv_duplicates);
621 }
Harald Welteebabdea2010-06-01 18:28:10 +0200622 }
623 /* Increment the sequence number that we expect in the next frame */
624 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200625 /* Increment Overflow Counter */
626 if ((gph->seq_tx + 1) / 512)
627 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200628 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200629 default:
630 LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd);
631 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800632 }
633
634 return 0;
635}
636
637/* parse a GPRS LLC header, also check for invalid frames */
638static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800639 uint8_t *llc_hdr, int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800640{
Harald Welteeaa614c2010-05-02 11:26:34 +0200641 uint8_t *ctrl = llc_hdr+1;
Harald Welte9b455bf2010-03-14 15:45:01 +0800642
643 if (len <= CRC24_LENGTH)
644 return -EIO;
645
Harald Welte1b8827a2010-06-30 23:15:57 +0200646 ghp->crc_length = len - CRC24_LENGTH;
Harald Welte9b455bf2010-03-14 15:45:01 +0800647
648 ghp->ack_req = 0;
649
650 /* Section 5.5: FCS */
651 ghp->fcs = *(llc_hdr + len - 3);
652 ghp->fcs |= *(llc_hdr + len - 2) << 8;
653 ghp->fcs |= *(llc_hdr + len - 1) << 16;
654
655 /* Section 6.2.1: invalid PD field */
656 if (llc_hdr[0] & 0x80)
657 return -EIO;
658
659 /* This only works for the MS->SGSN direction */
660 if (llc_hdr[0] & 0x40)
661 ghp->is_cmd = 0;
662 else
663 ghp->is_cmd = 1;
664
665 ghp->sapi = llc_hdr[0] & 0xf;
666
667 /* Section 6.2.3: check for reserved SAPI */
668 switch (ghp->sapi) {
669 case 0:
670 case 4:
671 case 6:
672 case 0xa:
673 case 0xc:
674 case 0xd:
675 case 0xf:
676 return -EINVAL;
677 }
678
679 if ((ctrl[0] & 0x80) == 0) {
680 /* I (Information transfer + Supervisory) format */
Harald Welteeaa614c2010-05-02 11:26:34 +0200681 uint8_t k;
Harald Welte9b455bf2010-03-14 15:45:01 +0800682
683 ghp->data = ctrl + 3;
684
685 if (ctrl[0] & 0x40)
686 ghp->ack_req = 1;
687
688 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
689 ghp->seq_tx |= (ctrl[1] >> 4);
690
691 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
692 ghp->seq_rx |= (ctrl[2] >> 2);
693
694 switch (ctrl[2] & 0x03) {
695 case 0:
696 ghp->cmd = GPRS_LLC_RR;
697 break;
698 case 1:
699 ghp->cmd = GPRS_LLC_ACK;
700 break;
701 case 2:
702 ghp->cmd = GPRS_LLC_RNR;
703 break;
704 case 3:
705 ghp->cmd = GPRS_LLC_SACK;
706 k = ctrl[3] & 0x1f;
707 ghp->data += 1 + k;
708 break;
709 }
Harald Welte5658a1a2010-05-03 13:25:07 +0200710 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800711 } else if ((ctrl[0] & 0xc0) == 0x80) {
712 /* S (Supervisory) format */
713 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200714 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800715
716 if (ctrl[0] & 0x20)
717 ghp->ack_req = 1;
718 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
719 ghp->seq_rx |= (ctrl[1] >> 2);
720
721 switch (ctrl[1] & 0x03) {
722 case 0:
723 ghp->cmd = GPRS_LLC_RR;
724 break;
725 case 1:
726 ghp->cmd = GPRS_LLC_ACK;
727 break;
728 case 2:
729 ghp->cmd = GPRS_LLC_RNR;
730 break;
731 case 3:
732 ghp->cmd = GPRS_LLC_SACK;
733 break;
734 }
735 } else if ((ctrl[0] & 0xe0) == 0xc0) {
736 /* UI (Unconfirmed Inforamtion) format */
Harald Welte1ae09c72010-05-13 19:22:55 +0200737 ghp->cmd = GPRS_LLC_UI;
Harald Welte9b455bf2010-03-14 15:45:01 +0800738 ghp->data = ctrl + 2;
Harald Welte5658a1a2010-05-03 13:25:07 +0200739 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800740
741 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
742 ghp->seq_tx |= (ctrl[1] >> 2);
743 if (ctrl[1] & 0x02) {
744 ghp->is_encrypted = 1;
745 /* FIXME: encryption */
746 }
747 if (ctrl[1] & 0x01) {
748 /* FCS over hdr + all inf fields */
749 } else {
750 /* FCS over hdr + N202 octets (4) */
Harald Welte1b8827a2010-06-30 23:15:57 +0200751 if (ghp->crc_length > UI_HDR_LEN + N202)
752 ghp->crc_length = UI_HDR_LEN + N202;
Harald Welte9b455bf2010-03-14 15:45:01 +0800753 }
754 } else {
755 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
756 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200757 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800758
759 switch (ctrl[0] & 0xf) {
Harald Welte5658a1a2010-05-03 13:25:07 +0200760 case GPRS_LLC_U_NULL_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800761 ghp->cmd = GPRS_LLC_NULL;
762 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200763 case GPRS_LLC_U_DM_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800764 ghp->cmd = GPRS_LLC_DM;
765 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200766 case GPRS_LLC_U_DISC_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800767 ghp->cmd = GPRS_LLC_DISC;
768 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200769 case GPRS_LLC_U_UA_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800770 ghp->cmd = GPRS_LLC_UA;
771 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200772 case GPRS_LLC_U_SABM_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800773 ghp->cmd = GPRS_LLC_SABM;
774 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200775 case GPRS_LLC_U_FRMR_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800776 ghp->cmd = GPRS_LLC_FRMR;
777 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200778 case GPRS_LLC_U_XID:
Harald Welte9b455bf2010-03-14 15:45:01 +0800779 ghp->cmd = GPRS_LLC_XID;
Harald Welte5658a1a2010-05-03 13:25:07 +0200780 ghp->data = ctrl + 1;
781 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800782 break;
783 default:
784 return -EIO;
785 }
786 }
787
Harald Welte9b455bf2010-03-14 15:45:01 +0800788 /* FIXME: parse sack frame */
Harald Welte1ae09c72010-05-13 19:22:55 +0200789 if (ghp->cmd == GPRS_LLC_SACK) {
Harald Welte1b170d12010-05-13 19:49:06 +0200790 LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200791 return -EIO;
792 }
793
794 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800795}
796
Harald Weltea2665542010-05-02 09:28:11 +0200797/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800798int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
799{
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200800 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800801 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200802 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200803 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800804
Harald Welte11d7c102010-05-02 11:54:55 +0200805 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
806
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800807 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800808 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800809 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200810 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200811 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200812 return rc;
813 }
814
Harald Welte807a5d82010-06-01 11:53:01 +0200815 switch (gprs_tlli_type(msgb_tlli(msg))) {
816 case TLLI_LOCAL:
817 case TLLI_FOREIGN:
818 case TLLI_RANDOM:
819 case TLLI_AUXILIARY:
820 break;
821 default:
822 LOGP(DLLC, LOGL_ERROR,
823 "Discarding frame with strange TLLI type\n");
824 break;
825 }
826
Harald Weltea2665542010-05-02 09:28:11 +0200827 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200828 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
829 if (!lle)
830 return 0;
Harald Weltea2665542010-05-02 09:28:11 +0200831
Harald Welted07b4f92010-06-30 23:07:59 +0200832 /* decrypt information field + FCS, if needed! */
833 if (llhp.is_encrypted) {
834 uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */
835 uint16_t crypt_len = llhp.data_len + 3;
836 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
837 uint32_t iv;
838 uint64_t kc = *(uint64_t *)&lle->llme->kc;
839 int rc, i;
840
841 if (lle->llme->algo == GPRS_ALGO_GEA0) {
842 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
843 "has no KC/Algo! Dropping.\n");
844 return 0;
845 }
846
847 iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx,
848 lle->oc_ui_recv);
849 rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
850 kc, iv, GPRS_CIPH_MS2SGSN);
851 if (rc < 0) {
852 LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n",
853 rc);
854 return rc;
855 }
856
857 /* XOR the cipher output with the information field + FCS */
858 for (i = 0; i < crypt_len; i++)
859 *(llhp.data + i) ^= cipher_out[i];
860 } else {
861 if (lle->llme->algo != GPRS_ALGO_GEA0) {
862 LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC "
863 "that is supposed to be encrypted. Dropping.\n");
864 return 0;
865 }
866 }
867
868 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200869 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200870 if (llhp.fcs != llhp.fcs_calc) {
871 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
872 return -EIO;
873 }
874
Harald Welte10997d02010-05-03 12:28:12 +0200875 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200876 lle->llme->bvci = msgb_bvci(msg);
877 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200878
Harald Welte1ae09c72010-05-13 19:22:55 +0200879 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800880 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200881 if (rc < 0)
882 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800883
Harald Welte1ae09c72010-05-13 19:22:55 +0200884 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte9b455bf2010-03-14 15:45:01 +0800885 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200886 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800887 switch (llhp.sapi) {
888 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200889 /* send LL_UNITDATA_IND to GMM */
Harald Welte807a5d82010-06-01 11:53:01 +0200890 rc = gsm0408_gprs_rcvmsg(msg, lle->llme);
Harald Weltea2665542010-05-02 09:28:11 +0200891 break;
Harald Weltea2665542010-05-02 09:28:11 +0200892 case GPRS_SAPI_SNDCP3:
893 case GPRS_SAPI_SNDCP5:
894 case GPRS_SAPI_SNDCP9:
895 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +0200896 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
897 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
898 break;
Harald Weltea2665542010-05-02 09:28:11 +0200899 case GPRS_SAPI_SMS:
900 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +0200901 case GPRS_SAPI_TOM2:
902 case GPRS_SAPI_TOM8:
903 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200904 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200905 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200906 rc = -EINVAL;
907 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800908 }
909 }
910
Harald Weltea2665542010-05-02 09:28:11 +0200911 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800912}
Harald Welte807a5d82010-06-01 11:53:01 +0200913
914/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
915int gprs_llgmm_assign(struct gprs_llc_llme *llme,
916 uint32_t old_tlli, uint32_t new_tlli,
917 enum gprs_ciph_algo alg, const uint8_t *kc)
918{
919 unsigned int i;
920
Harald Welted07b4f92010-06-30 23:07:59 +0200921 /* Update the crypto parameters */
Harald Welted07b4f92010-06-30 23:07:59 +0200922 llme->algo = alg;
Harald Welte3e2e1592010-06-30 23:19:23 +0200923 if (alg != GPRS_ALGO_GEA0)
924 memcpy(llme->kc, kc, sizeof(llme->kc));
Harald Welted07b4f92010-06-30 23:07:59 +0200925
Harald Welte807a5d82010-06-01 11:53:01 +0200926 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
927 /* TLLI Assignment 8.3.1 */
928 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
929 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
930 * old is unassigned. Only TLLI new shall be accepted when
931 * received from peer. */
Harald Welte875840c2010-07-01 11:54:31 +0200932 if (llme->old_tlli != 0xffffffff) {
933 llme->old_tlli = 0xffffffff;
934 llme->tlli = new_tlli;
935 } else {
936 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
937 * TLLI assignmemt according to 8.3.1 */
938 llme->old_tlli = 0xffffffff;
939 llme->tlli = new_tlli;
940 llme->state = GPRS_LLMS_ASSIGNED;
941 /* 8.5.3.1 For all LLE's */
942 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
943 struct gprs_llc_lle *l = &llme->lle[i];
944 l->vu_send = l->vu_recv = 0;
945 l->retrans_ctr = 0;
946 l->state = GPRS_LLES_ASSIGNED_ADM;
947 /* FIXME Set parameters according to table 9 */
948 }
Harald Welte807a5d82010-06-01 11:53:01 +0200949 }
950 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
951 /* TLLI Change 8.3.2 */
952 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +0200953 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freytheraa93bac2013-07-31 11:20:37 +0200954 llme->old_tlli = old_tlli;
Harald Welte807a5d82010-06-01 11:53:01 +0200955 llme->tlli = new_tlli;
956 llme->state = GPRS_LLMS_ASSIGNED;
957 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
958 /* TLLI Unassignment 8.3.3) */
959 llme->tlli = llme->old_tlli = 0;
960 llme->state = GPRS_LLMS_UNASSIGNED;
961 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
962 struct gprs_llc_lle *l = &llme->lle[i];
963 l->state = GPRS_LLES_UNASSIGNED;
964 }
Harald Weltef7fef482010-06-28 22:18:26 +0200965 llme_free(llme);
Harald Welte807a5d82010-06-01 11:53:01 +0200966 } else
967 return -EINVAL;
968
969 return 0;
970}
Harald Welte496aee42010-06-30 19:59:55 +0200971
Harald Welte0c1a3032011-10-16 18:49:05 +0200972/* Chapter 7.2.1.2 LLGMM-RESET.req */
973int gprs_llgmm_reset(struct gprs_llc_llme *llme)
974{
975 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
976 int random = rand();
977
978 /* First XID component must be RESET */
979 msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL);
980 /* randomly select new IOV-UI */
Harald Welte066a0f52011-10-16 18:59:20 +0200981 msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random);
Harald Welte0c1a3032011-10-16 18:49:05 +0200982
983 /* FIXME: Start T200, wait for XID response */
984 return gprs_llc_tx_xid(&llme->lle[1], msg, 1);
985}
986
Harald Welte496aee42010-06-30 19:59:55 +0200987int gprs_llc_init(const char *cipher_plugin_path)
988{
989 return gprs_cipher_load(cipher_plugin_path);
990}