blob: fdaa7b3f7049485e5f2a18bba6dceff56b26f766 [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
145/* transmit a simple U frame */
146static int gprs_llc_tx_u()
147{
148 struct msgb *msg = msgb_alloc(LLC_ALLOC_SIZE, "GPRS/LLC");
149
150 if (!msg)
151 return -ENOMEM;
152
153
154
155 /* transmit the frame via BSSGP->NS->... */
156}
157
158static void t200_expired(void *data)
159{
160 struct gprs_llc_lle *lle = data;
161
162 /* 8.5.1.3: Expiry of T200 */
163
164 if (lle->retrans_ctr >= lle->n200) {
165 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
166 lle->state = GPRS_LLS_ASSIGNED_ADM;
167 }
168
169 switch (lle->state) {
170 case GPRS_LLS_LOCAL_EST:
171 /* retransmit SABM */
172 /* re-start T200 */
173 lle->retrans_ctr++;
174 break;
175 case GPRS_LLS_LOCAL_REL:
176 /* retransmit DISC */
177 /* re-start T200 */
178 lle->retrans_ctr++;
179 break;
180 }
181
182}
183
184static void t201_expired(void *data)
185{
186 struct gprs_llc_lle *lle = data;
187
188 if (lle->retrans_ctr < lle->n200) {
189 /* transmit apropriate supervisory frame (8.6.4.1) */
190 /* set timer T201 */
191 lle->retrans_ctr++;
192 }
193}
194
195/* Transmit a UI frame over the given SAPI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200196int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command)
Harald Welte9b455bf2010-03-14 15:45:01 +0800197{
Harald Weltee6afd602010-05-02 11:19:37 +0200198 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200199 uint8_t *fcs, *llch;
200 uint8_t addr, ctrl[2];
201 uint32_t fcs_calc;
202 uint16_t nu = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800203
Harald Weltee6afd602010-05-02 11:19:37 +0200204 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
205
206 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
207 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
208 if (!lle)
209 lle = lle_alloc(msgb_tlli(msg), sapi);
210 /* Update LLE's (BVCI, NSEI) tuple */
211 lle->bvci = msgb_bvci(msg);
212 lle->nsei = msgb_nsei(msg);
213
Harald Welte9b455bf2010-03-14 15:45:01 +0800214 /* Address Field */
215 addr = sapi & 0xf;
216 if (command)
217 addr |= 0x40;
218
219 /* Control Field */
220 ctrl[0] = 0xc0;
221 ctrl[0] |= nu >> 6;
222 ctrl[1] = (nu << 2) & 0xfc;
223 ctrl[1] |= 0x01; /* Protected Mode */
224
225 /* prepend LLC UI header */
226 llch = msgb_push(msg, 3);
227 llch[0] = addr;
228 llch[1] = ctrl[0];
229 llch[2] = ctrl[1];
230
231 /* append FCS to end of frame */
232 fcs = msgb_put(msg, 3);
233 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
234 fcs[0] = fcs_calc & 0xff;
235 fcs[1] = (fcs_calc >> 8) & 0xff;
236 fcs[2] = (fcs_calc >> 16) & 0xff;
237
Harald Weltee6afd602010-05-02 11:19:37 +0200238 /* Identifiers passed down: (BVCI, NSEI) */
239
Harald Welte9b455bf2010-03-14 15:45:01 +0800240 return gprs_bssgp_tx_dl_ud(msg);
241}
242
243static int gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
244{
245 DEBUGP(DGPRS, "LLC SAPI=%u %c %c FCS=0x%06x(%s) ",
246 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
247 gph->fcs, gph->fcs_calc == gph->fcs ? "correct" : "WRONG");
248
249 if (gph->cmd)
250 DEBUGPC(DGPRS, "CMD=%u ", gph->cmd);
251
252 if (gph->data)
253 DEBUGPC(DGPRS, "DATA ");
254
255 DEBUGPC(DGPRS, "\n");
256}
257static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
258 struct gprs_llc_lle *lle)
259{
260 switch (gph->cmd) {
261 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
262 lle->v_sent = lle->v_ack = lle->v_recv = 0;
263 if (lle->state == GPRS_LLS_ASSIGNED_ADM) {
264 /* start re-establishment (8.7.1) */
265 }
266 lle->state = GPRS_LLS_REMOTE_EST;
267 /* FIXME: Send UA */
268 lle->state = GPRS_LLS_ABM;
269 /* FIXME: process data */
270 break;
271 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
272 /* FIXME: Send UA */
273 /* terminate ABM */
274 lle->state = GPRS_LLS_ASSIGNED_ADM;
275 break;
276 case GPRS_LLC_UA: /* Section 6.4.1.3 */
277 if (lle->state == GPRS_LLS_LOCAL_EST)
278 lle->state = GPRS_LLS_ABM;
279 break;
280 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
281 if (lle->state == GPRS_LLS_LOCAL_EST)
282 lle->state = GPRS_LLS_ASSIGNED_ADM;
283 break;
284 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
285 break;
286 case GPRS_LLC_XID: /* Section 6.4.1.6 */
287 break;
288 }
289
290 return 0;
291}
292
293/* parse a GPRS LLC header, also check for invalid frames */
294static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Harald Welteeaa614c2010-05-02 11:26:34 +0200295 const uint8_t *llc_hdr, int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800296{
Harald Welteeaa614c2010-05-02 11:26:34 +0200297 uint8_t *ctrl = llc_hdr+1;
Harald Welte9b455bf2010-03-14 15:45:01 +0800298 int is_sack = 0;
299 unsigned int crc_length;
Harald Welteeaa614c2010-05-02 11:26:34 +0200300 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800301
302 if (len <= CRC24_LENGTH)
303 return -EIO;
304
305 crc_length = len - CRC24_LENGTH;
306
307 ghp->ack_req = 0;
308
309 /* Section 5.5: FCS */
310 ghp->fcs = *(llc_hdr + len - 3);
311 ghp->fcs |= *(llc_hdr + len - 2) << 8;
312 ghp->fcs |= *(llc_hdr + len - 1) << 16;
313
314 /* Section 6.2.1: invalid PD field */
315 if (llc_hdr[0] & 0x80)
316 return -EIO;
317
318 /* This only works for the MS->SGSN direction */
319 if (llc_hdr[0] & 0x40)
320 ghp->is_cmd = 0;
321 else
322 ghp->is_cmd = 1;
323
324 ghp->sapi = llc_hdr[0] & 0xf;
325
326 /* Section 6.2.3: check for reserved SAPI */
327 switch (ghp->sapi) {
328 case 0:
329 case 4:
330 case 6:
331 case 0xa:
332 case 0xc:
333 case 0xd:
334 case 0xf:
335 return -EINVAL;
336 }
337
338 if ((ctrl[0] & 0x80) == 0) {
339 /* I (Information transfer + Supervisory) format */
Harald Welteeaa614c2010-05-02 11:26:34 +0200340 uint8_t k;
Harald Welte9b455bf2010-03-14 15:45:01 +0800341
342 ghp->data = ctrl + 3;
343
344 if (ctrl[0] & 0x40)
345 ghp->ack_req = 1;
346
347 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
348 ghp->seq_tx |= (ctrl[1] >> 4);
349
350 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
351 ghp->seq_rx |= (ctrl[2] >> 2);
352
353 switch (ctrl[2] & 0x03) {
354 case 0:
355 ghp->cmd = GPRS_LLC_RR;
356 break;
357 case 1:
358 ghp->cmd = GPRS_LLC_ACK;
359 break;
360 case 2:
361 ghp->cmd = GPRS_LLC_RNR;
362 break;
363 case 3:
364 ghp->cmd = GPRS_LLC_SACK;
365 k = ctrl[3] & 0x1f;
366 ghp->data += 1 + k;
367 break;
368 }
369 } else if ((ctrl[0] & 0xc0) == 0x80) {
370 /* S (Supervisory) format */
371 ghp->data = NULL;
372
373 if (ctrl[0] & 0x20)
374 ghp->ack_req = 1;
375 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
376 ghp->seq_rx |= (ctrl[1] >> 2);
377
378 switch (ctrl[1] & 0x03) {
379 case 0:
380 ghp->cmd = GPRS_LLC_RR;
381 break;
382 case 1:
383 ghp->cmd = GPRS_LLC_ACK;
384 break;
385 case 2:
386 ghp->cmd = GPRS_LLC_RNR;
387 break;
388 case 3:
389 ghp->cmd = GPRS_LLC_SACK;
390 break;
391 }
392 } else if ((ctrl[0] & 0xe0) == 0xc0) {
393 /* UI (Unconfirmed Inforamtion) format */
394 ghp->data = ctrl + 2;
395
396 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
397 ghp->seq_tx |= (ctrl[1] >> 2);
398 if (ctrl[1] & 0x02) {
399 ghp->is_encrypted = 1;
400 /* FIXME: encryption */
401 }
402 if (ctrl[1] & 0x01) {
403 /* FCS over hdr + all inf fields */
404 } else {
405 /* FCS over hdr + N202 octets (4) */
406 if (crc_length > UI_HDR_LEN + N202)
407 crc_length = UI_HDR_LEN + N202;
408 }
409 } else {
410 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
411 ghp->data = NULL;
412
413 switch (ctrl[0] & 0xf) {
414 case 0:
415 ghp->cmd = GPRS_LLC_NULL;
416 break;
417 case 0x1:
418 ghp->cmd = GPRS_LLC_DM;
419 break;
420 case 0x4:
421 ghp->cmd = GPRS_LLC_DISC;
422 break;
423 case 0x6:
424 ghp->cmd = GPRS_LLC_UA;
425 break;
426 case 0x7:
427 ghp->cmd = GPRS_LLC_SABM;
428 break;
429 case 0x8:
430 ghp->cmd = GPRS_LLC_FRMR;
431 break;
432 case 0xb:
433 ghp->cmd = GPRS_LLC_XID;
434 break;
435 default:
436 return -EIO;
437 }
438 }
439
440 /* calculate what FCS we expect */
441 ghp->fcs_calc = gprs_llc_fcs(llc_hdr, crc_length);
442
443 /* FIXME: parse sack frame */
444}
445
Harald Weltea2665542010-05-02 09:28:11 +0200446/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800447int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
448{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200449 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte943c5bc2010-04-30 16:33:12 +0200450 struct gprs_llc_hdr *lh = msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800451 struct gprs_llc_hdr_parsed llhp;
452 struct gprs_llc_entity *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200453 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800454
455 rc = gprs_llc_hdr_parse(&llhp, lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Weltea2665542010-05-02 09:28:11 +0200456 /* FIXME */
Harald Welte9b455bf2010-03-14 15:45:01 +0800457
458 gprs_llc_hdr_dump(&llhp);
Harald Weltea2665542010-05-02 09:28:11 +0200459
460 /* find the LLC Entity for this TLLI+SAPI tuple */
461 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
462 /* allocate a new LLE if needed */
463 if (!lle)
464 lle = lle_alloc(msgb_tlli(msg), llhp.sapi);
465
Harald Welte9b455bf2010-03-14 15:45:01 +0800466 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Weltea2665542010-05-02 09:28:11 +0200467 /* FIXME */
Harald Welte9b455bf2010-03-14 15:45:01 +0800468
469 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200470 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800471 switch (llhp.sapi) {
472 case GPRS_SAPI_GMM:
473 rc = gsm0408_gprs_rcvmsg(msg);
Harald Weltea2665542010-05-02 09:28:11 +0200474 break;
475 case GPRS_SAPI_TOM2:
476 case GPRS_SAPI_TOM8:
477 /* FIXME */
478 case GPRS_SAPI_SNDCP3:
479 case GPRS_SAPI_SNDCP5:
480 case GPRS_SAPI_SNDCP9:
481 case GPRS_SAPI_SNDCP11:
482 /* FIXME */
483 case GPRS_SAPI_SMS:
484 /* FIXME */
485 default:
486 LOGP(DGPRS, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
487 rc = -EINVAL;
488 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800489 }
490 }
491
Harald Weltea2665542010-05-02 09:28:11 +0200492 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800493}