blob: dffca5e21e87c19f1f12c666eb371b5b1eb95632 [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
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 Welteeaa614c2010-05-02 11:26:34 +020024#include <stdint.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080025
Harald Welte9b455bf2010-03-14 15:45:01 +080026#include <osmocore/msgb.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080027#include <osmocore/linuxlist.h>
28#include <osmocore/timer.h>
Harald Weltea2665542010-05-02 09:28:11 +020029#include <osmocore/talloc.h>
30
31#include <openbsc/gsm_data.h>
32#include <openbsc/debug.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080033#include <openbsc/gprs_bssgp.h>
34#include <openbsc/gprs_llc.h>
35#include <openbsc/crc24.h>
36
37/* Section 4.5.2 Logical Link States + Annex C.2 */
38enum gprs_llc_ll_state {
39 GPRS_LLS_UNASSIGNED = 1, /* No TLLI yet */
40 GPRS_LLS_ASSIGNED_ADM = 2, /* TLLI assigned */
41 GPRS_LLS_LOCAL_EST = 3, /* Local Establishment */
42 GPRS_LLS_REMOTE_EST = 4, /* Remote Establishment */
43 GPRS_LLS_ABM = 5,
44 GPRS_LLS_LOCAL_REL = 6, /* Local Release */
45 GPRS_LLS_TIMER_REC = 7, /* Timer Recovery */
46};
47
48/* Section 4.7.1: Logical Link Entity: One per DLCI (TLLI + SAPI) */
49struct gprs_llc_lle {
50 struct llist_head list;
51 struct timer_list t200;
52 struct timer_list t201; /* wait for acknowledgement */
53
54 enum gprs_llc_ll_state state;
55
Harald Welteeaa614c2010-05-02 11:26:34 +020056 uint32_t tlli;
57 uint32_t sapi;
Harald Welte9b455bf2010-03-14 15:45:01 +080058
Harald Welteeaa614c2010-05-02 11:26:34 +020059 uint8_t v_sent;
60 uint8_t v_ack;
61 uint8_t v_recv;
Harald Welte9b455bf2010-03-14 15:45:01 +080062
63 unsigned int n200;
64 unsigned int retrans_ctr;
Harald Weltee6afd602010-05-02 11:19:37 +020065
66 /* over which BSSGP BTS ctx do we need to transmit */
67 uint16_t bvci;
68 uint16_t nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +080069};
70
Harald Weltea2665542010-05-02 09:28:11 +020071static LLIST_HEAD(gprs_llc_lles);
72void *llc_tall_ctx;
73
74/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
75static struct gprs_llc_lle *lle_by_tlli_sapi(uint32_t tlli, uint32_t sapi)
76{
77 struct gprs_llc_lle *lle;
78
79 llist_for_each_entry(lle, &gprs_llc_lles, list) {
80 if (lle->tlli == tlli && lle->sapi == sapi)
81 return lle;
82 }
83 return NULL;
84}
85
86static struct gprs_llc_lle *lle_alloc(uint32_t tlli, uint32_t sapi)
87{
88 struct gprs_llc_lle *lle;
89
90 lle = talloc_zero(llc_tall_ctx, struct gprs_llc_lle);
91 if (!lle)
92 return NULL;
93
94 lle->tlli = tlli;
95 lle->sapi = sapi;
96 lle->state = GPRS_LLS_UNASSIGNED;
97 llist_add(&lle->list, &gprs_llc_lles);
98
99 return lle;
100}
101
Harald Welte9b455bf2010-03-14 15:45:01 +0800102enum gprs_llc_cmd {
103 GPRS_LLC_NULL,
104 GPRS_LLC_RR,
105 GPRS_LLC_ACK,
106 GPRS_LLC_RNR,
107 GPRS_LLC_SACK,
108 GPRS_LLC_DM,
109 GPRS_LLC_DISC,
110 GPRS_LLC_UA,
111 GPRS_LLC_SABM,
112 GPRS_LLC_FRMR,
113 GPRS_LLC_XID,
114};
115
116struct gprs_llc_hdr_parsed {
Harald Welteeaa614c2010-05-02 11:26:34 +0200117 uint8_t sapi;
118 uint8_t is_cmd:1,
Harald Welte9b455bf2010-03-14 15:45:01 +0800119 ack_req:1,
120 is_encrypted:1;
Harald Welteeaa614c2010-05-02 11:26:34 +0200121 uint32_t seq_rx;
122 uint32_t seq_tx;
123 uint32_t fcs;
124 uint32_t fcs_calc;
125 uint8_t *data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800126 enum gprs_llc_cmd cmd;
127};
128
129#define LLC_ALLOC_SIZE 16384
130#define UI_HDR_LEN 3
131#define N202 4
132#define CRC24_LENGTH 3
133
Harald Welteeaa614c2010-05-02 11:26:34 +0200134static int gprs_llc_fcs(uint8_t *data, unsigned int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800135{
Harald Welteeaa614c2010-05-02 11:26:34 +0200136 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800137
138 fcs_calc = crc24_calc(INIT_CRC24, data, len);
139 fcs_calc = ~fcs_calc;
140 fcs_calc &= 0xffffff;
141
142 return fcs_calc;
143}
144
Harald Welte9b455bf2010-03-14 15:45:01 +0800145static void t200_expired(void *data)
146{
147 struct gprs_llc_lle *lle = data;
148
149 /* 8.5.1.3: Expiry of T200 */
150
151 if (lle->retrans_ctr >= lle->n200) {
152 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
153 lle->state = GPRS_LLS_ASSIGNED_ADM;
154 }
155
156 switch (lle->state) {
157 case GPRS_LLS_LOCAL_EST:
158 /* retransmit SABM */
159 /* re-start T200 */
160 lle->retrans_ctr++;
161 break;
162 case GPRS_LLS_LOCAL_REL:
163 /* retransmit DISC */
164 /* re-start T200 */
165 lle->retrans_ctr++;
166 break;
167 }
168
169}
170
171static void t201_expired(void *data)
172{
173 struct gprs_llc_lle *lle = data;
174
175 if (lle->retrans_ctr < lle->n200) {
176 /* transmit apropriate supervisory frame (8.6.4.1) */
177 /* set timer T201 */
178 lle->retrans_ctr++;
179 }
180}
181
Harald Welte10997d02010-05-03 12:28:12 +0200182int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
183 enum gprs_llc_u_cmd u_cmd, int pf_bit)
184{
185 uint8_t *fcs, *llch;
186 uint8_t addr, ctrl;
187 uint32_t fcs_calc;
188
189 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
190
191 /* Address Field */
192 addr = sapi & 0xf;
193 if (command)
194 addr |= 0x40;
195
196 /* 6.3 Figure 8 */
197 ctrl = 0xe0 | u_cmd;
198 if (pf_bit)
199 ctrl |= 0x10;
200
201 /* prepend LLC UI header */
202 llch = msgb_push(msg, 2);
203 llch[0] = addr;
204 llch[1] = ctrl;
205
206 /* append FCS to end of frame */
207 fcs = msgb_put(msg, 3);
208 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
209 fcs[0] = fcs_calc & 0xff;
210 fcs[1] = (fcs_calc >> 8) & 0xff;
211 fcs[2] = (fcs_calc >> 16) & 0xff;
212
213 /* Identifiers passed down: (BVCI, NSEI) */
214
215 return gprs_bssgp_tx_dl_ud(msg);
216}
217
218/* Send XID response to LLE */
219static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg)
220{
221 /* copy identifiers from LLE to ensure lower layers can route */
222 msgb_tlli(msg) = lle->tlli;
223 msgb_bvci(msg) = lle->bvci;
224 msgb_nsei(msg) = lle->nsei;
225
226 return gprs_llc_tx_u(msg, lle->sapi, 0, GPRS_LLC_U_XID, 1);
227}
228
Harald Welte9b455bf2010-03-14 15:45:01 +0800229/* Transmit a UI frame over the given SAPI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200230int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command)
Harald Welte9b455bf2010-03-14 15:45:01 +0800231{
Harald Weltee6afd602010-05-02 11:19:37 +0200232 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200233 uint8_t *fcs, *llch;
234 uint8_t addr, ctrl[2];
235 uint32_t fcs_calc;
236 uint16_t nu = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800237
Harald Weltee6afd602010-05-02 11:19:37 +0200238 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
239
240 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
241 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
242 if (!lle)
243 lle = lle_alloc(msgb_tlli(msg), sapi);
244 /* Update LLE's (BVCI, NSEI) tuple */
245 lle->bvci = msgb_bvci(msg);
246 lle->nsei = msgb_nsei(msg);
247
Harald Welte9b455bf2010-03-14 15:45:01 +0800248 /* Address Field */
249 addr = sapi & 0xf;
250 if (command)
251 addr |= 0x40;
252
253 /* Control Field */
254 ctrl[0] = 0xc0;
255 ctrl[0] |= nu >> 6;
256 ctrl[1] = (nu << 2) & 0xfc;
257 ctrl[1] |= 0x01; /* Protected Mode */
258
259 /* prepend LLC UI header */
260 llch = msgb_push(msg, 3);
261 llch[0] = addr;
262 llch[1] = ctrl[0];
263 llch[2] = ctrl[1];
264
265 /* append FCS to end of frame */
266 fcs = msgb_put(msg, 3);
267 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
268 fcs[0] = fcs_calc & 0xff;
269 fcs[1] = (fcs_calc >> 8) & 0xff;
270 fcs[2] = (fcs_calc >> 16) & 0xff;
271
Harald Weltee6afd602010-05-02 11:19:37 +0200272 /* Identifiers passed down: (BVCI, NSEI) */
273
Harald Welte9b455bf2010-03-14 15:45:01 +0800274 return gprs_bssgp_tx_dl_ud(msg);
275}
276
277static int gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
278{
279 DEBUGP(DGPRS, "LLC SAPI=%u %c %c FCS=0x%06x(%s) ",
280 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
281 gph->fcs, gph->fcs_calc == gph->fcs ? "correct" : "WRONG");
282
283 if (gph->cmd)
284 DEBUGPC(DGPRS, "CMD=%u ", gph->cmd);
285
286 if (gph->data)
287 DEBUGPC(DGPRS, "DATA ");
288
289 DEBUGPC(DGPRS, "\n");
290}
291static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
292 struct gprs_llc_lle *lle)
293{
Harald Welte10997d02010-05-03 12:28:12 +0200294 struct msgb *resp;
295
Harald Welte9b455bf2010-03-14 15:45:01 +0800296 switch (gph->cmd) {
297 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
298 lle->v_sent = lle->v_ack = lle->v_recv = 0;
299 if (lle->state == GPRS_LLS_ASSIGNED_ADM) {
300 /* start re-establishment (8.7.1) */
301 }
302 lle->state = GPRS_LLS_REMOTE_EST;
303 /* FIXME: Send UA */
304 lle->state = GPRS_LLS_ABM;
305 /* FIXME: process data */
306 break;
307 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
308 /* FIXME: Send UA */
309 /* terminate ABM */
310 lle->state = GPRS_LLS_ASSIGNED_ADM;
311 break;
312 case GPRS_LLC_UA: /* Section 6.4.1.3 */
313 if (lle->state == GPRS_LLS_LOCAL_EST)
314 lle->state = GPRS_LLS_ABM;
315 break;
316 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
317 if (lle->state == GPRS_LLS_LOCAL_EST)
318 lle->state = GPRS_LLS_ASSIGNED_ADM;
319 break;
320 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
321 break;
322 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte10997d02010-05-03 12:28:12 +0200323 /* FIXME: implement XID negotiation */
324 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
325 gprs_llc_tx_xid(lle, resp);
Harald Welte9b455bf2010-03-14 15:45:01 +0800326 break;
327 }
328
329 return 0;
330}
331
332/* parse a GPRS LLC header, also check for invalid frames */
333static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Harald Welteeaa614c2010-05-02 11:26:34 +0200334 const uint8_t *llc_hdr, int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800335{
Harald Welteeaa614c2010-05-02 11:26:34 +0200336 uint8_t *ctrl = llc_hdr+1;
Harald Welte9b455bf2010-03-14 15:45:01 +0800337 int is_sack = 0;
338 unsigned int crc_length;
Harald Welteeaa614c2010-05-02 11:26:34 +0200339 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800340
341 if (len <= CRC24_LENGTH)
342 return -EIO;
343
344 crc_length = len - CRC24_LENGTH;
345
346 ghp->ack_req = 0;
347
348 /* Section 5.5: FCS */
349 ghp->fcs = *(llc_hdr + len - 3);
350 ghp->fcs |= *(llc_hdr + len - 2) << 8;
351 ghp->fcs |= *(llc_hdr + len - 1) << 16;
352
353 /* Section 6.2.1: invalid PD field */
354 if (llc_hdr[0] & 0x80)
355 return -EIO;
356
357 /* This only works for the MS->SGSN direction */
358 if (llc_hdr[0] & 0x40)
359 ghp->is_cmd = 0;
360 else
361 ghp->is_cmd = 1;
362
363 ghp->sapi = llc_hdr[0] & 0xf;
364
365 /* Section 6.2.3: check for reserved SAPI */
366 switch (ghp->sapi) {
367 case 0:
368 case 4:
369 case 6:
370 case 0xa:
371 case 0xc:
372 case 0xd:
373 case 0xf:
374 return -EINVAL;
375 }
376
377 if ((ctrl[0] & 0x80) == 0) {
378 /* I (Information transfer + Supervisory) format */
Harald Welteeaa614c2010-05-02 11:26:34 +0200379 uint8_t k;
Harald Welte9b455bf2010-03-14 15:45:01 +0800380
381 ghp->data = ctrl + 3;
382
383 if (ctrl[0] & 0x40)
384 ghp->ack_req = 1;
385
386 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
387 ghp->seq_tx |= (ctrl[1] >> 4);
388
389 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
390 ghp->seq_rx |= (ctrl[2] >> 2);
391
392 switch (ctrl[2] & 0x03) {
393 case 0:
394 ghp->cmd = GPRS_LLC_RR;
395 break;
396 case 1:
397 ghp->cmd = GPRS_LLC_ACK;
398 break;
399 case 2:
400 ghp->cmd = GPRS_LLC_RNR;
401 break;
402 case 3:
403 ghp->cmd = GPRS_LLC_SACK;
404 k = ctrl[3] & 0x1f;
405 ghp->data += 1 + k;
406 break;
407 }
408 } else if ((ctrl[0] & 0xc0) == 0x80) {
409 /* S (Supervisory) format */
410 ghp->data = NULL;
411
412 if (ctrl[0] & 0x20)
413 ghp->ack_req = 1;
414 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
415 ghp->seq_rx |= (ctrl[1] >> 2);
416
417 switch (ctrl[1] & 0x03) {
418 case 0:
419 ghp->cmd = GPRS_LLC_RR;
420 break;
421 case 1:
422 ghp->cmd = GPRS_LLC_ACK;
423 break;
424 case 2:
425 ghp->cmd = GPRS_LLC_RNR;
426 break;
427 case 3:
428 ghp->cmd = GPRS_LLC_SACK;
429 break;
430 }
431 } else if ((ctrl[0] & 0xe0) == 0xc0) {
432 /* UI (Unconfirmed Inforamtion) format */
433 ghp->data = ctrl + 2;
434
435 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
436 ghp->seq_tx |= (ctrl[1] >> 2);
437 if (ctrl[1] & 0x02) {
438 ghp->is_encrypted = 1;
439 /* FIXME: encryption */
440 }
441 if (ctrl[1] & 0x01) {
442 /* FCS over hdr + all inf fields */
443 } else {
444 /* FCS over hdr + N202 octets (4) */
445 if (crc_length > UI_HDR_LEN + N202)
446 crc_length = UI_HDR_LEN + N202;
447 }
448 } else {
449 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
450 ghp->data = NULL;
451
452 switch (ctrl[0] & 0xf) {
453 case 0:
454 ghp->cmd = GPRS_LLC_NULL;
455 break;
456 case 0x1:
457 ghp->cmd = GPRS_LLC_DM;
458 break;
459 case 0x4:
460 ghp->cmd = GPRS_LLC_DISC;
461 break;
462 case 0x6:
463 ghp->cmd = GPRS_LLC_UA;
464 break;
465 case 0x7:
466 ghp->cmd = GPRS_LLC_SABM;
467 break;
468 case 0x8:
469 ghp->cmd = GPRS_LLC_FRMR;
470 break;
471 case 0xb:
472 ghp->cmd = GPRS_LLC_XID;
473 break;
474 default:
475 return -EIO;
476 }
477 }
478
479 /* calculate what FCS we expect */
480 ghp->fcs_calc = gprs_llc_fcs(llc_hdr, crc_length);
481
482 /* FIXME: parse sack frame */
483}
484
Harald Weltea2665542010-05-02 09:28:11 +0200485/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800486int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
487{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200488 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte943c5bc2010-04-30 16:33:12 +0200489 struct gprs_llc_hdr *lh = msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800490 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200491 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200492 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800493
Harald Welte11d7c102010-05-02 11:54:55 +0200494 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
495
Harald Welte9b455bf2010-03-14 15:45:01 +0800496 rc = gprs_llc_hdr_parse(&llhp, lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Weltea2665542010-05-02 09:28:11 +0200497 /* FIXME */
Harald Welte9b455bf2010-03-14 15:45:01 +0800498
499 gprs_llc_hdr_dump(&llhp);
Harald Weltea2665542010-05-02 09:28:11 +0200500
501 /* find the LLC Entity for this TLLI+SAPI tuple */
502 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
503 /* allocate a new LLE if needed */
504 if (!lle)
505 lle = lle_alloc(msgb_tlli(msg), llhp.sapi);
506
Harald Welte10997d02010-05-03 12:28:12 +0200507 /* Update LLE's (BVCI, NSEI) tuple */
508 lle->bvci = msgb_bvci(msg);
509 lle->nsei = msgb_nsei(msg);
510
Harald Welte9b455bf2010-03-14 15:45:01 +0800511 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Weltea2665542010-05-02 09:28:11 +0200512 /* FIXME */
Harald Welte9b455bf2010-03-14 15:45:01 +0800513
514 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200515 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800516 switch (llhp.sapi) {
517 case GPRS_SAPI_GMM:
518 rc = gsm0408_gprs_rcvmsg(msg);
Harald Weltea2665542010-05-02 09:28:11 +0200519 break;
520 case GPRS_SAPI_TOM2:
521 case GPRS_SAPI_TOM8:
522 /* FIXME */
523 case GPRS_SAPI_SNDCP3:
524 case GPRS_SAPI_SNDCP5:
525 case GPRS_SAPI_SNDCP9:
526 case GPRS_SAPI_SNDCP11:
527 /* FIXME */
528 case GPRS_SAPI_SMS:
529 /* FIXME */
530 default:
531 LOGP(DGPRS, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
532 rc = -EINVAL;
533 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800534 }
535 }
536
Harald Weltea2665542010-05-02 09:28:11 +0200537 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800538}