blob: 26730d4c21f22315fee1c07772d49edea2a1819b [file] [log] [blame]
Harald Welte75bb8202010-03-14 15:45:01 +08001/* GPRS LLC protocol implementation as per 3GPP TS 04.64 */
2
Harald Weltec14e5822010-05-02 09:28:11 +02003/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte75bb8202010-03-14 15:45:01 +08004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <errno.h>
Harald Welted85d9a92010-05-02 11:26:34 +020024#include <stdint.h>
Harald Welte75bb8202010-03-14 15:45:01 +080025
Harald Welte75bb8202010-03-14 15:45:01 +080026#include <osmocore/msgb.h>
Harald Welte75bb8202010-03-14 15:45:01 +080027#include <osmocore/linuxlist.h>
28#include <osmocore/timer.h>
Harald Weltec14e5822010-05-02 09:28:11 +020029#include <osmocore/talloc.h>
30
31#include <openbsc/gsm_data.h>
32#include <openbsc/debug.h>
Harald Welte75871c72010-06-01 11:53:01 +020033#include <openbsc/gprs_sgsn.h>
34#include <openbsc/gprs_gmm.h>
Harald Welte75bb8202010-03-14 15:45:01 +080035#include <openbsc/gprs_bssgp.h>
36#include <openbsc/gprs_llc.h>
37#include <openbsc/crc24.h>
38
Harald Welte75871c72010-06-01 11:53:01 +020039LLIST_HEAD(gprs_llc_llmes);
Harald Weltec14e5822010-05-02 09:28:11 +020040void *llc_tall_ctx;
41
42/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
43static struct gprs_llc_lle *lle_by_tlli_sapi(uint32_t tlli, uint32_t sapi)
44{
Harald Welte75871c72010-06-01 11:53:01 +020045 struct gprs_llc_llme *llme;
Harald Weltec14e5822010-05-02 09:28:11 +020046
Harald Welte75871c72010-06-01 11:53:01 +020047 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
48 if (llme->tlli == tlli || llme->old_tlli == tlli)
49 return &llme->lle[sapi];
Harald Weltec14e5822010-05-02 09:28:11 +020050 }
51 return NULL;
52}
53
Harald Welte75871c72010-06-01 11:53:01 +020054static void lle_init(struct gprs_llc_llme *llme, uint32_t sapi)
Harald Weltec14e5822010-05-02 09:28:11 +020055{
Harald Welte75871c72010-06-01 11:53:01 +020056 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltec14e5822010-05-02 09:28:11 +020057
Harald Welte75871c72010-06-01 11:53:01 +020058 lle->llme = llme;
59 lle->sapi = sapi;
60 lle->state = GPRS_LLES_UNASSIGNED;
61
62 /* FIXME: Initialize according to parameters from SAPI9 */
63
64}
65
66static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
67{
68 struct gprs_llc_llme *llme;
69 uint32_t i;
70
71 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
72 if (!llme)
Harald Weltec14e5822010-05-02 09:28:11 +020073 return NULL;
74
Harald Welte75871c72010-06-01 11:53:01 +020075 llme->tlli = tlli;
76 llme->state = GPRS_LLMS_UNASSIGNED;
Harald Weltec14e5822010-05-02 09:28:11 +020077
Harald Welte75871c72010-06-01 11:53:01 +020078 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
79 lle_init(llme, i);
80
81 llist_add(&llme->list, &gprs_llc_llmes);
82
83 return llme;
Harald Weltec14e5822010-05-02 09:28:11 +020084}
85
Harald Welte75bb8202010-03-14 15:45:01 +080086enum gprs_llc_cmd {
87 GPRS_LLC_NULL,
88 GPRS_LLC_RR,
89 GPRS_LLC_ACK,
90 GPRS_LLC_RNR,
91 GPRS_LLC_SACK,
92 GPRS_LLC_DM,
93 GPRS_LLC_DISC,
94 GPRS_LLC_UA,
95 GPRS_LLC_SABM,
96 GPRS_LLC_FRMR,
97 GPRS_LLC_XID,
Harald Weltefebc4262010-05-13 19:22:55 +020098 GPRS_LLC_UI,
Harald Welte75bb8202010-03-14 15:45:01 +080099};
100
Harald Welte366c2042010-05-18 12:31:50 +0200101static const struct value_string llc_cmd_strs[] = {
102 { GPRS_LLC_NULL, "NULL" },
103 { GPRS_LLC_RR, "RR" },
104 { GPRS_LLC_ACK, "ACK" },
105 { GPRS_LLC_RNR, "RNR" },
106 { GPRS_LLC_SACK, "SACK" },
107 { GPRS_LLC_DM, "DM" },
108 { GPRS_LLC_DISC, "DISC" },
109 { GPRS_LLC_UA, "UA" },
110 { GPRS_LLC_SABM, "SABM" },
111 { GPRS_LLC_FRMR, "FRMR" },
112 { GPRS_LLC_XID, "XID" },
113 { GPRS_LLC_UI, "UI" },
114 { 0, NULL }
115};
116
Harald Welte75bb8202010-03-14 15:45:01 +0800117struct gprs_llc_hdr_parsed {
Harald Welted85d9a92010-05-02 11:26:34 +0200118 uint8_t sapi;
119 uint8_t is_cmd:1,
Harald Welte75bb8202010-03-14 15:45:01 +0800120 ack_req:1,
121 is_encrypted:1;
Harald Welted85d9a92010-05-02 11:26:34 +0200122 uint32_t seq_rx;
123 uint32_t seq_tx;
124 uint32_t fcs;
125 uint32_t fcs_calc;
126 uint8_t *data;
Harald Welte9f14f982010-05-03 13:25:07 +0200127 uint16_t data_len;
Harald Welte75bb8202010-03-14 15:45:01 +0800128 enum gprs_llc_cmd cmd;
129};
130
131#define LLC_ALLOC_SIZE 16384
132#define UI_HDR_LEN 3
133#define N202 4
134#define CRC24_LENGTH 3
135
Harald Welted85d9a92010-05-02 11:26:34 +0200136static int gprs_llc_fcs(uint8_t *data, unsigned int len)
Harald Welte75bb8202010-03-14 15:45:01 +0800137{
Harald Welted85d9a92010-05-02 11:26:34 +0200138 uint32_t fcs_calc;
Harald Welte75bb8202010-03-14 15:45:01 +0800139
140 fcs_calc = crc24_calc(INIT_CRC24, data, len);
141 fcs_calc = ~fcs_calc;
142 fcs_calc &= 0xffffff;
143
144 return fcs_calc;
145}
146
Harald Welte75bb8202010-03-14 15:45:01 +0800147static void t200_expired(void *data)
148{
149 struct gprs_llc_lle *lle = data;
150
151 /* 8.5.1.3: Expiry of T200 */
152
153 if (lle->retrans_ctr >= lle->n200) {
154 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte75871c72010-06-01 11:53:01 +0200155 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte75bb8202010-03-14 15:45:01 +0800156 }
157
158 switch (lle->state) {
Harald Welte75871c72010-06-01 11:53:01 +0200159 case GPRS_LLES_LOCAL_EST:
Harald Weltefebc4262010-05-13 19:22:55 +0200160 /* FIXME: retransmit SABM */
161 /* FIXME: re-start T200 */
Harald Welte75bb8202010-03-14 15:45:01 +0800162 lle->retrans_ctr++;
163 break;
Harald Welte75871c72010-06-01 11:53:01 +0200164 case GPRS_LLES_LOCAL_REL:
Harald Weltefebc4262010-05-13 19:22:55 +0200165 /* FIXME: retransmit DISC */
166 /* FIXME: re-start T200 */
Harald Welte75bb8202010-03-14 15:45:01 +0800167 lle->retrans_ctr++;
168 break;
169 }
170
171}
172
173static void t201_expired(void *data)
174{
175 struct gprs_llc_lle *lle = data;
176
177 if (lle->retrans_ctr < lle->n200) {
Harald Weltefebc4262010-05-13 19:22:55 +0200178 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
179 /* FIXME: set timer T201 */
Harald Welte75bb8202010-03-14 15:45:01 +0800180 lle->retrans_ctr++;
181 }
182}
183
Harald Welte439fa722010-05-03 12:28:12 +0200184int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
185 enum gprs_llc_u_cmd u_cmd, int pf_bit)
186{
187 uint8_t *fcs, *llch;
188 uint8_t addr, ctrl;
189 uint32_t fcs_calc;
190
191 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
192
193 /* Address Field */
194 addr = sapi & 0xf;
195 if (command)
196 addr |= 0x40;
197
198 /* 6.3 Figure 8 */
199 ctrl = 0xe0 | u_cmd;
200 if (pf_bit)
201 ctrl |= 0x10;
202
203 /* prepend LLC UI header */
204 llch = msgb_push(msg, 2);
205 llch[0] = addr;
206 llch[1] = ctrl;
207
208 /* append FCS to end of frame */
209 fcs = msgb_put(msg, 3);
210 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
211 fcs[0] = fcs_calc & 0xff;
212 fcs[1] = (fcs_calc >> 8) & 0xff;
213 fcs[2] = (fcs_calc >> 16) & 0xff;
214
215 /* Identifiers passed down: (BVCI, NSEI) */
216
Harald Weltefebc4262010-05-13 19:22:55 +0200217 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltecca80c92010-05-31 22:12:30 +0200218 return gprs_bssgp_tx_dl_ud(msg, NULL);
Harald Welte439fa722010-05-03 12:28:12 +0200219}
220
221/* Send XID response to LLE */
222static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg)
223{
224 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte75871c72010-06-01 11:53:01 +0200225 msgb_tlli(msg) = lle->llme->tlli;
226 msgb_bvci(msg) = lle->llme->bvci;
227 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte439fa722010-05-03 12:28:12 +0200228
229 return gprs_llc_tx_u(msg, lle->sapi, 0, GPRS_LLC_U_XID, 1);
230}
231
Harald Welte75bb8202010-03-14 15:45:01 +0800232/* Transmit a UI frame over the given SAPI */
Harald Weltecca80c92010-05-31 22:12:30 +0200233int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
234 void *mmctx)
Harald Welte75bb8202010-03-14 15:45:01 +0800235{
Harald Weltea6e32702010-05-02 11:19:37 +0200236 struct gprs_llc_lle *lle;
Harald Welted85d9a92010-05-02 11:26:34 +0200237 uint8_t *fcs, *llch;
238 uint8_t addr, ctrl[2];
239 uint32_t fcs_calc;
240 uint16_t nu = 0;
Harald Welte75bb8202010-03-14 15:45:01 +0800241
Harald Weltea6e32702010-05-02 11:19:37 +0200242 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
243
244 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
245 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
Harald Welte75871c72010-06-01 11:53:01 +0200246 if (!lle) {
247 struct gprs_llc_llme *llme;
248 llme = llme_alloc(msgb_tlli(msg));
249 lle = &llme->lle[sapi];
250 }
Harald Weltea6e32702010-05-02 11:19:37 +0200251 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte75871c72010-06-01 11:53:01 +0200252 lle->llme->bvci = msgb_bvci(msg);
253 lle->llme->nsei = msgb_nsei(msg);
Harald Weltea6e32702010-05-02 11:19:37 +0200254
Harald Welte02343f92010-05-30 21:51:58 +0200255 /* Increment V(U) */
256 nu = lle->vu_send;
257 lle->vu_send = (lle->vu_send + 1) % 512;
258
Harald Welte75bb8202010-03-14 15:45:01 +0800259 /* Address Field */
260 addr = sapi & 0xf;
261 if (command)
262 addr |= 0x40;
263
264 /* Control Field */
265 ctrl[0] = 0xc0;
266 ctrl[0] |= nu >> 6;
267 ctrl[1] = (nu << 2) & 0xfc;
268 ctrl[1] |= 0x01; /* Protected Mode */
269
270 /* prepend LLC UI header */
271 llch = msgb_push(msg, 3);
272 llch[0] = addr;
273 llch[1] = ctrl[0];
274 llch[2] = ctrl[1];
275
276 /* append FCS to end of frame */
277 fcs = msgb_put(msg, 3);
278 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
279 fcs[0] = fcs_calc & 0xff;
280 fcs[1] = (fcs_calc >> 8) & 0xff;
281 fcs[2] = (fcs_calc >> 16) & 0xff;
282
Harald Weltea6e32702010-05-02 11:19:37 +0200283 /* Identifiers passed down: (BVCI, NSEI) */
284
Harald Weltefebc4262010-05-13 19:22:55 +0200285 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltecca80c92010-05-31 22:12:30 +0200286 return gprs_bssgp_tx_dl_ud(msg, mmctx);
Harald Welte75bb8202010-03-14 15:45:01 +0800287}
288
Holger Hans Peter Freyther6f8867b2010-05-23 21:35:25 +0800289static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
Harald Welte75bb8202010-03-14 15:45:01 +0800290{
Harald Welte24b78872010-05-13 19:47:50 +0200291 DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x(%s) ",
Harald Welte75bb8202010-03-14 15:45:01 +0800292 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
293 gph->fcs, gph->fcs_calc == gph->fcs ? "correct" : "WRONG");
294
295 if (gph->cmd)
Harald Welte366c2042010-05-18 12:31:50 +0200296 DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd));
Harald Welte75bb8202010-03-14 15:45:01 +0800297
298 if (gph->data)
Harald Welte24b78872010-05-13 19:47:50 +0200299 DEBUGPC(DLLC, "DATA ");
Harald Welte75bb8202010-03-14 15:45:01 +0800300
Harald Welte24b78872010-05-13 19:47:50 +0200301 DEBUGPC(DLLC, "\n");
Harald Welte75bb8202010-03-14 15:45:01 +0800302}
303static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
304 struct gprs_llc_lle *lle)
305{
306 switch (gph->cmd) {
307 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
308 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte75871c72010-06-01 11:53:01 +0200309 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte75bb8202010-03-14 15:45:01 +0800310 /* start re-establishment (8.7.1) */
311 }
Harald Welte75871c72010-06-01 11:53:01 +0200312 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte75bb8202010-03-14 15:45:01 +0800313 /* FIXME: Send UA */
Harald Welte75871c72010-06-01 11:53:01 +0200314 lle->state = GPRS_LLES_ABM;
Harald Welte75bb8202010-03-14 15:45:01 +0800315 /* FIXME: process data */
316 break;
317 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
318 /* FIXME: Send UA */
319 /* terminate ABM */
Harald Welte75871c72010-06-01 11:53:01 +0200320 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte75bb8202010-03-14 15:45:01 +0800321 break;
322 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte75871c72010-06-01 11:53:01 +0200323 if (lle->state == GPRS_LLES_LOCAL_EST)
324 lle->state = GPRS_LLES_ABM;
Harald Welte75bb8202010-03-14 15:45:01 +0800325 break;
326 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte75871c72010-06-01 11:53:01 +0200327 if (lle->state == GPRS_LLES_LOCAL_EST)
328 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte75bb8202010-03-14 15:45:01 +0800329 break;
330 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
331 break;
332 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte9f14f982010-05-03 13:25:07 +0200333 /* FIXME: implement XID negotiation using SNDCP */
334 {
335 struct msgb *resp;
336 uint8_t *xid;
337 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
338 xid = msgb_put(resp, gph->data_len);
339 memcpy(xid, gph->data, gph->data_len);
340 gprs_llc_tx_xid(lle, resp);
341 }
Harald Welte75bb8202010-03-14 15:45:01 +0800342 break;
343 }
344
345 return 0;
346}
347
348/* parse a GPRS LLC header, also check for invalid frames */
349static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Holger Hans Peter Freyther6f261512010-05-23 21:43:57 +0800350 uint8_t *llc_hdr, int len)
Harald Welte75bb8202010-03-14 15:45:01 +0800351{
Harald Welted85d9a92010-05-02 11:26:34 +0200352 uint8_t *ctrl = llc_hdr+1;
Harald Welte75bb8202010-03-14 15:45:01 +0800353 int is_sack = 0;
354 unsigned int crc_length;
Harald Welted85d9a92010-05-02 11:26:34 +0200355 uint32_t fcs_calc;
Harald Welte75bb8202010-03-14 15:45:01 +0800356
357 if (len <= CRC24_LENGTH)
358 return -EIO;
359
360 crc_length = len - CRC24_LENGTH;
361
362 ghp->ack_req = 0;
363
364 /* Section 5.5: FCS */
365 ghp->fcs = *(llc_hdr + len - 3);
366 ghp->fcs |= *(llc_hdr + len - 2) << 8;
367 ghp->fcs |= *(llc_hdr + len - 1) << 16;
368
369 /* Section 6.2.1: invalid PD field */
370 if (llc_hdr[0] & 0x80)
371 return -EIO;
372
373 /* This only works for the MS->SGSN direction */
374 if (llc_hdr[0] & 0x40)
375 ghp->is_cmd = 0;
376 else
377 ghp->is_cmd = 1;
378
379 ghp->sapi = llc_hdr[0] & 0xf;
380
381 /* Section 6.2.3: check for reserved SAPI */
382 switch (ghp->sapi) {
383 case 0:
384 case 4:
385 case 6:
386 case 0xa:
387 case 0xc:
388 case 0xd:
389 case 0xf:
390 return -EINVAL;
391 }
392
393 if ((ctrl[0] & 0x80) == 0) {
394 /* I (Information transfer + Supervisory) format */
Harald Welted85d9a92010-05-02 11:26:34 +0200395 uint8_t k;
Harald Welte75bb8202010-03-14 15:45:01 +0800396
397 ghp->data = ctrl + 3;
398
399 if (ctrl[0] & 0x40)
400 ghp->ack_req = 1;
401
402 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
403 ghp->seq_tx |= (ctrl[1] >> 4);
404
405 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
406 ghp->seq_rx |= (ctrl[2] >> 2);
407
408 switch (ctrl[2] & 0x03) {
409 case 0:
410 ghp->cmd = GPRS_LLC_RR;
411 break;
412 case 1:
413 ghp->cmd = GPRS_LLC_ACK;
414 break;
415 case 2:
416 ghp->cmd = GPRS_LLC_RNR;
417 break;
418 case 3:
419 ghp->cmd = GPRS_LLC_SACK;
420 k = ctrl[3] & 0x1f;
421 ghp->data += 1 + k;
422 break;
423 }
Harald Welte9f14f982010-05-03 13:25:07 +0200424 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte75bb8202010-03-14 15:45:01 +0800425 } else if ((ctrl[0] & 0xc0) == 0x80) {
426 /* S (Supervisory) format */
427 ghp->data = NULL;
Harald Welte9f14f982010-05-03 13:25:07 +0200428 ghp->data_len = 0;
Harald Welte75bb8202010-03-14 15:45:01 +0800429
430 if (ctrl[0] & 0x20)
431 ghp->ack_req = 1;
432 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
433 ghp->seq_rx |= (ctrl[1] >> 2);
434
435 switch (ctrl[1] & 0x03) {
436 case 0:
437 ghp->cmd = GPRS_LLC_RR;
438 break;
439 case 1:
440 ghp->cmd = GPRS_LLC_ACK;
441 break;
442 case 2:
443 ghp->cmd = GPRS_LLC_RNR;
444 break;
445 case 3:
446 ghp->cmd = GPRS_LLC_SACK;
447 break;
448 }
449 } else if ((ctrl[0] & 0xe0) == 0xc0) {
450 /* UI (Unconfirmed Inforamtion) format */
Harald Weltefebc4262010-05-13 19:22:55 +0200451 ghp->cmd = GPRS_LLC_UI;
Harald Welte75bb8202010-03-14 15:45:01 +0800452 ghp->data = ctrl + 2;
Harald Welte9f14f982010-05-03 13:25:07 +0200453 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte75bb8202010-03-14 15:45:01 +0800454
455 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
456 ghp->seq_tx |= (ctrl[1] >> 2);
457 if (ctrl[1] & 0x02) {
458 ghp->is_encrypted = 1;
459 /* FIXME: encryption */
460 }
461 if (ctrl[1] & 0x01) {
462 /* FCS over hdr + all inf fields */
463 } else {
464 /* FCS over hdr + N202 octets (4) */
465 if (crc_length > UI_HDR_LEN + N202)
466 crc_length = UI_HDR_LEN + N202;
467 }
468 } else {
469 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
470 ghp->data = NULL;
Harald Welte9f14f982010-05-03 13:25:07 +0200471 ghp->data_len = 0;
Harald Welte75bb8202010-03-14 15:45:01 +0800472
473 switch (ctrl[0] & 0xf) {
Harald Welte9f14f982010-05-03 13:25:07 +0200474 case GPRS_LLC_U_NULL_CMD:
Harald Welte75bb8202010-03-14 15:45:01 +0800475 ghp->cmd = GPRS_LLC_NULL;
476 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200477 case GPRS_LLC_U_DM_RESP:
Harald Welte75bb8202010-03-14 15:45:01 +0800478 ghp->cmd = GPRS_LLC_DM;
479 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200480 case GPRS_LLC_U_DISC_CMD:
Harald Welte75bb8202010-03-14 15:45:01 +0800481 ghp->cmd = GPRS_LLC_DISC;
482 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200483 case GPRS_LLC_U_UA_RESP:
Harald Welte75bb8202010-03-14 15:45:01 +0800484 ghp->cmd = GPRS_LLC_UA;
485 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200486 case GPRS_LLC_U_SABM_CMD:
Harald Welte75bb8202010-03-14 15:45:01 +0800487 ghp->cmd = GPRS_LLC_SABM;
488 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200489 case GPRS_LLC_U_FRMR_RESP:
Harald Welte75bb8202010-03-14 15:45:01 +0800490 ghp->cmd = GPRS_LLC_FRMR;
491 break;
Harald Welte9f14f982010-05-03 13:25:07 +0200492 case GPRS_LLC_U_XID:
Harald Welte75bb8202010-03-14 15:45:01 +0800493 ghp->cmd = GPRS_LLC_XID;
Harald Welte9f14f982010-05-03 13:25:07 +0200494 ghp->data = ctrl + 1;
495 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte75bb8202010-03-14 15:45:01 +0800496 break;
497 default:
498 return -EIO;
499 }
500 }
501
502 /* calculate what FCS we expect */
503 ghp->fcs_calc = gprs_llc_fcs(llc_hdr, crc_length);
504
505 /* FIXME: parse sack frame */
Harald Weltefebc4262010-05-13 19:22:55 +0200506 if (ghp->cmd == GPRS_LLC_SACK) {
Harald Weltee3497362010-05-13 19:49:06 +0200507 LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n");
Harald Weltefebc4262010-05-13 19:22:55 +0200508 return -EIO;
509 }
510
511 return 0;
Harald Welte75bb8202010-03-14 15:45:01 +0800512}
513
Harald Weltec14e5822010-05-02 09:28:11 +0200514/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte75bb8202010-03-14 15:45:01 +0800515int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
516{
Harald Welte73164402010-05-02 09:50:42 +0200517 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Weltecf3c4142010-04-30 16:33:12 +0200518 struct gprs_llc_hdr *lh = msgb_llch(msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800519 struct gprs_llc_hdr_parsed llhp;
Harald Welte439fa722010-05-03 12:28:12 +0200520 struct gprs_llc_lle *lle;
Harald Weltec14e5822010-05-02 09:28:11 +0200521 int rc = 0;
Harald Welte75bb8202010-03-14 15:45:01 +0800522
Harald Welte2351f1f2010-05-02 11:54:55 +0200523 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
524
Holger Hans Peter Freyther510ae722010-05-23 21:33:57 +0800525 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freyther6f261512010-05-23 21:43:57 +0800526 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte75bb8202010-03-14 15:45:01 +0800527 gprs_llc_hdr_dump(&llhp);
Harald Weltefebc4262010-05-13 19:22:55 +0200528 if (rc < 0) {
Harald Weltee3497362010-05-13 19:49:06 +0200529 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Weltefebc4262010-05-13 19:22:55 +0200530 return rc;
531 }
532
533 if (llhp.fcs != llhp.fcs_calc) {
Harald Weltee3497362010-05-13 19:49:06 +0200534 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
Harald Weltefebc4262010-05-13 19:22:55 +0200535 return -EIO;
536 }
Harald Weltec14e5822010-05-02 09:28:11 +0200537
Harald Welte75871c72010-06-01 11:53:01 +0200538 switch (gprs_tlli_type(msgb_tlli(msg))) {
539 case TLLI_LOCAL:
540 case TLLI_FOREIGN:
541 case TLLI_RANDOM:
542 case TLLI_AUXILIARY:
543 break;
544 default:
545 LOGP(DLLC, LOGL_ERROR,
546 "Discarding frame with strange TLLI type\n");
547 break;
548 }
549
Harald Weltec14e5822010-05-02 09:28:11 +0200550 /* find the LLC Entity for this TLLI+SAPI tuple */
551 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
Harald Weltefebc4262010-05-13 19:22:55 +0200552
553 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
554 * except UID and XID frames with SAPI=1 */
Harald Welte4b7d1de2010-05-18 12:45:08 +0200555 if (!lle) {
556 if (llhp.sapi == GPRS_SAPI_GMM &&
557 (llhp.cmd == GPRS_LLC_XID || llhp.cmd == GPRS_LLC_UI)) {
Harald Welte75871c72010-06-01 11:53:01 +0200558 struct gprs_llc_llme *llme;
Harald Welte4b7d1de2010-05-18 12:45:08 +0200559 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
Harald Welte75871c72010-06-01 11:53:01 +0200560 llme = llme_alloc(msgb_tlli(msg));
561 lle = &llme->lle[llhp.sapi];
Harald Welte4b7d1de2010-05-18 12:45:08 +0200562 } else {
563 LOGP(DLLC, LOGL_NOTICE,
564 "unknown TLLI/SAPI: Silently dropping\n");
565 return 0;
566 }
Harald Weltefebc4262010-05-13 19:22:55 +0200567 }
Harald Weltec14e5822010-05-02 09:28:11 +0200568
Harald Welte439fa722010-05-03 12:28:12 +0200569 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte75871c72010-06-01 11:53:01 +0200570 lle->llme->bvci = msgb_bvci(msg);
571 lle->llme->nsei = msgb_nsei(msg);
Harald Welte439fa722010-05-03 12:28:12 +0200572
Harald Weltefebc4262010-05-13 19:22:55 +0200573 /* Receive and Process the actual LLC frame */
Harald Welte75bb8202010-03-14 15:45:01 +0800574 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Weltefebc4262010-05-13 19:22:55 +0200575 if (rc < 0)
576 return rc;
Harald Welte75bb8202010-03-14 15:45:01 +0800577
Harald Weltefebc4262010-05-13 19:22:55 +0200578 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte75bb8202010-03-14 15:45:01 +0800579 if (llhp.data) {
Harald Weltecf3c4142010-04-30 16:33:12 +0200580 msgb_gmmh(msg) = llhp.data;
Harald Welte75bb8202010-03-14 15:45:01 +0800581 switch (llhp.sapi) {
582 case GPRS_SAPI_GMM:
Harald Weltefebc4262010-05-13 19:22:55 +0200583 /* send LL_UNITDATA_IND to GMM */
Harald Welte75871c72010-06-01 11:53:01 +0200584 rc = gsm0408_gprs_rcvmsg(msg, lle->llme);
Harald Weltec14e5822010-05-02 09:28:11 +0200585 break;
586 case GPRS_SAPI_TOM2:
587 case GPRS_SAPI_TOM8:
Harald Weltefebc4262010-05-13 19:22:55 +0200588 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltec14e5822010-05-02 09:28:11 +0200589 case GPRS_SAPI_SNDCP3:
590 case GPRS_SAPI_SNDCP5:
591 case GPRS_SAPI_SNDCP9:
592 case GPRS_SAPI_SNDCP11:
Harald Weltefebc4262010-05-13 19:22:55 +0200593 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
Harald Weltec14e5822010-05-02 09:28:11 +0200594 case GPRS_SAPI_SMS:
595 /* FIXME */
596 default:
Harald Welte24b78872010-05-13 19:47:50 +0200597 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltec14e5822010-05-02 09:28:11 +0200598 rc = -EINVAL;
599 break;
Harald Welte75bb8202010-03-14 15:45:01 +0800600 }
601 }
602
Harald Weltec14e5822010-05-02 09:28:11 +0200603 return rc;
Harald Welte75bb8202010-03-14 15:45:01 +0800604}
Harald Welte75871c72010-06-01 11:53:01 +0200605
606/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
607int gprs_llgmm_assign(struct gprs_llc_llme *llme,
608 uint32_t old_tlli, uint32_t new_tlli,
609 enum gprs_ciph_algo alg, const uint8_t *kc)
610{
611 unsigned int i;
612
613 if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) {
614 /* TLLI Assignment 8.3.1 */
615 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
616 /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI
617 * old is unassigned. Only TLLI new shall be accepted when
618 * received from peer. */
619
620 /* If TLLI old == 0xffffffff was assigned to LLME, then this is
621 * TLLI assignmemt according to 8.3.1 */
622 llme->old_tlli = 0;
623 llme->tlli = new_tlli;
624 llme->state = GPRS_LLMS_ASSIGNED;
625 /* 8.5.3.1 For all LLE's */
626 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
627 struct gprs_llc_lle *l = &llme->lle[i];
628 l->vu_send = l->vu_recv = 0;
629 l->retrans_ctr = 0;
630 l->state = GPRS_LLES_ASSIGNED_ADM;
631 /* FIXME Set parameters according to table 9 */
632 }
633 } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) {
634 /* TLLI Change 8.3.2 */
635 /* Both TLLI Old and TLLI New are assigned; use New when
636 * (re)transmitting. Accept toth Old and New on Rx */
637 llme->old_tlli = llme->tlli;
638 llme->tlli = new_tlli;
639 llme->state = GPRS_LLMS_ASSIGNED;
640 } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) {
641 /* TLLI Unassignment 8.3.3) */
642 llme->tlli = llme->old_tlli = 0;
643 llme->state = GPRS_LLMS_UNASSIGNED;
644 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
645 struct gprs_llc_lle *l = &llme->lle[i];
646 l->state = GPRS_LLES_UNASSIGNED;
647 }
648 } else
649 return -EINVAL;
650
651 return 0;
652}