blob: ba031a2ec971851a2d0446e6120c5a665d104e75 [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>
24
Harald Welte9b455bf2010-03-14 15:45:01 +080025#include <osmocore/msgb.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080026#include <osmocore/linuxlist.h>
27#include <osmocore/timer.h>
Harald Weltea2665542010-05-02 09:28:11 +020028#include <osmocore/talloc.h>
29
30#include <openbsc/gsm_data.h>
31#include <openbsc/debug.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080032#include <openbsc/gprs_bssgp.h>
33#include <openbsc/gprs_llc.h>
34#include <openbsc/crc24.h>
35
36/* Section 4.5.2 Logical Link States + Annex C.2 */
37enum gprs_llc_ll_state {
38 GPRS_LLS_UNASSIGNED = 1, /* No TLLI yet */
39 GPRS_LLS_ASSIGNED_ADM = 2, /* TLLI assigned */
40 GPRS_LLS_LOCAL_EST = 3, /* Local Establishment */
41 GPRS_LLS_REMOTE_EST = 4, /* Remote Establishment */
42 GPRS_LLS_ABM = 5,
43 GPRS_LLS_LOCAL_REL = 6, /* Local Release */
44 GPRS_LLS_TIMER_REC = 7, /* Timer Recovery */
45};
46
47/* Section 4.7.1: Logical Link Entity: One per DLCI (TLLI + SAPI) */
48struct gprs_llc_lle {
49 struct llist_head list;
50 struct timer_list t200;
51 struct timer_list t201; /* wait for acknowledgement */
52
53 enum gprs_llc_ll_state state;
54
55 u_int32_t tlli;
56 u_int32_t sapi;
57
58 u_int8_t v_sent;
59 u_int8_t v_ack;
60 u_int8_t v_recv;
61
62 unsigned int n200;
63 unsigned int retrans_ctr;
Harald Weltee6afd602010-05-02 11:19:37 +020064
65 /* over which BSSGP BTS ctx do we need to transmit */
66 uint16_t bvci;
67 uint16_t nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +080068};
69
Harald Weltea2665542010-05-02 09:28:11 +020070static LLIST_HEAD(gprs_llc_lles);
71void *llc_tall_ctx;
72
73/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
74static struct gprs_llc_lle *lle_by_tlli_sapi(uint32_t tlli, uint32_t sapi)
75{
76 struct gprs_llc_lle *lle;
77
78 llist_for_each_entry(lle, &gprs_llc_lles, list) {
79 if (lle->tlli == tlli && lle->sapi == sapi)
80 return lle;
81 }
82 return NULL;
83}
84
85static struct gprs_llc_lle *lle_alloc(uint32_t tlli, uint32_t sapi)
86{
87 struct gprs_llc_lle *lle;
88
89 lle = talloc_zero(llc_tall_ctx, struct gprs_llc_lle);
90 if (!lle)
91 return NULL;
92
93 lle->tlli = tlli;
94 lle->sapi = sapi;
95 lle->state = GPRS_LLS_UNASSIGNED;
96 llist_add(&lle->list, &gprs_llc_lles);
97
98 return lle;
99}
100
Harald Welte9b455bf2010-03-14 15:45:01 +0800101enum gprs_llc_cmd {
102 GPRS_LLC_NULL,
103 GPRS_LLC_RR,
104 GPRS_LLC_ACK,
105 GPRS_LLC_RNR,
106 GPRS_LLC_SACK,
107 GPRS_LLC_DM,
108 GPRS_LLC_DISC,
109 GPRS_LLC_UA,
110 GPRS_LLC_SABM,
111 GPRS_LLC_FRMR,
112 GPRS_LLC_XID,
113};
114
115struct gprs_llc_hdr_parsed {
116 u_int8_t sapi;
117 u_int8_t is_cmd:1,
118 ack_req:1,
119 is_encrypted:1;
120 u_int32_t seq_rx;
121 u_int32_t seq_tx;
122 u_int32_t fcs;
123 u_int32_t fcs_calc;
124 u_int8_t *data;
125 enum gprs_llc_cmd cmd;
126};
127
128#define LLC_ALLOC_SIZE 16384
129#define UI_HDR_LEN 3
130#define N202 4
131#define CRC24_LENGTH 3
132
133static int gprs_llc_fcs(u_int8_t *data, unsigned int len)
134{
135 u_int32_t fcs_calc;
136
137 fcs_calc = crc24_calc(INIT_CRC24, data, len);
138 fcs_calc = ~fcs_calc;
139 fcs_calc &= 0xffffff;
140
141 return fcs_calc;
142}
143
144/* transmit a simple U frame */
145static int gprs_llc_tx_u()
146{
147 struct msgb *msg = msgb_alloc(LLC_ALLOC_SIZE, "GPRS/LLC");
148
149 if (!msg)
150 return -ENOMEM;
151
152
153
154 /* transmit the frame via BSSGP->NS->... */
155}
156
157static void t200_expired(void *data)
158{
159 struct gprs_llc_lle *lle = data;
160
161 /* 8.5.1.3: Expiry of T200 */
162
163 if (lle->retrans_ctr >= lle->n200) {
164 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
165 lle->state = GPRS_LLS_ASSIGNED_ADM;
166 }
167
168 switch (lle->state) {
169 case GPRS_LLS_LOCAL_EST:
170 /* retransmit SABM */
171 /* re-start T200 */
172 lle->retrans_ctr++;
173 break;
174 case GPRS_LLS_LOCAL_REL:
175 /* retransmit DISC */
176 /* re-start T200 */
177 lle->retrans_ctr++;
178 break;
179 }
180
181}
182
183static void t201_expired(void *data)
184{
185 struct gprs_llc_lle *lle = data;
186
187 if (lle->retrans_ctr < lle->n200) {
188 /* transmit apropriate supervisory frame (8.6.4.1) */
189 /* set timer T201 */
190 lle->retrans_ctr++;
191 }
192}
193
194/* Transmit a UI frame over the given SAPI */
195int gprs_llc_tx_ui(struct msgb *msg, u_int8_t sapi, int command)
196{
Harald Weltee6afd602010-05-02 11:19:37 +0200197 struct gprs_llc_lle *lle;
Harald Welte9b455bf2010-03-14 15:45:01 +0800198 u_int8_t *fcs, *llch;
199 u_int8_t addr, ctrl[2];
200 u_int32_t fcs_calc;
201 u_int16_t nu = 0;
202
Harald Weltee6afd602010-05-02 11:19:37 +0200203 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
204
205 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
206 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
207 if (!lle)
208 lle = lle_alloc(msgb_tlli(msg), sapi);
209 /* Update LLE's (BVCI, NSEI) tuple */
210 lle->bvci = msgb_bvci(msg);
211 lle->nsei = msgb_nsei(msg);
212
Harald Welte9b455bf2010-03-14 15:45:01 +0800213 /* Address Field */
214 addr = sapi & 0xf;
215 if (command)
216 addr |= 0x40;
217
218 /* Control Field */
219 ctrl[0] = 0xc0;
220 ctrl[0] |= nu >> 6;
221 ctrl[1] = (nu << 2) & 0xfc;
222 ctrl[1] |= 0x01; /* Protected Mode */
223
224 /* prepend LLC UI header */
225 llch = msgb_push(msg, 3);
226 llch[0] = addr;
227 llch[1] = ctrl[0];
228 llch[2] = ctrl[1];
229
230 /* append FCS to end of frame */
231 fcs = msgb_put(msg, 3);
232 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
233 fcs[0] = fcs_calc & 0xff;
234 fcs[1] = (fcs_calc >> 8) & 0xff;
235 fcs[2] = (fcs_calc >> 16) & 0xff;
236
Harald Weltee6afd602010-05-02 11:19:37 +0200237 /* Identifiers passed down: (BVCI, NSEI) */
238
Harald Welte9b455bf2010-03-14 15:45:01 +0800239 return gprs_bssgp_tx_dl_ud(msg);
240}
241
242static int gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
243{
244 DEBUGP(DGPRS, "LLC SAPI=%u %c %c FCS=0x%06x(%s) ",
245 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
246 gph->fcs, gph->fcs_calc == gph->fcs ? "correct" : "WRONG");
247
248 if (gph->cmd)
249 DEBUGPC(DGPRS, "CMD=%u ", gph->cmd);
250
251 if (gph->data)
252 DEBUGPC(DGPRS, "DATA ");
253
254 DEBUGPC(DGPRS, "\n");
255}
256static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
257 struct gprs_llc_lle *lle)
258{
259 switch (gph->cmd) {
260 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
261 lle->v_sent = lle->v_ack = lle->v_recv = 0;
262 if (lle->state == GPRS_LLS_ASSIGNED_ADM) {
263 /* start re-establishment (8.7.1) */
264 }
265 lle->state = GPRS_LLS_REMOTE_EST;
266 /* FIXME: Send UA */
267 lle->state = GPRS_LLS_ABM;
268 /* FIXME: process data */
269 break;
270 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
271 /* FIXME: Send UA */
272 /* terminate ABM */
273 lle->state = GPRS_LLS_ASSIGNED_ADM;
274 break;
275 case GPRS_LLC_UA: /* Section 6.4.1.3 */
276 if (lle->state == GPRS_LLS_LOCAL_EST)
277 lle->state = GPRS_LLS_ABM;
278 break;
279 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
280 if (lle->state == GPRS_LLS_LOCAL_EST)
281 lle->state = GPRS_LLS_ASSIGNED_ADM;
282 break;
283 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
284 break;
285 case GPRS_LLC_XID: /* Section 6.4.1.6 */
286 break;
287 }
288
289 return 0;
290}
291
292/* parse a GPRS LLC header, also check for invalid frames */
293static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
294 const u_int8_t *llc_hdr, int len)
295{
296 u_int8_t *ctrl = llc_hdr+1;
297 int is_sack = 0;
298 unsigned int crc_length;
299 u_int32_t fcs_calc;
300
301 if (len <= CRC24_LENGTH)
302 return -EIO;
303
304 crc_length = len - CRC24_LENGTH;
305
306 ghp->ack_req = 0;
307
308 /* Section 5.5: FCS */
309 ghp->fcs = *(llc_hdr + len - 3);
310 ghp->fcs |= *(llc_hdr + len - 2) << 8;
311 ghp->fcs |= *(llc_hdr + len - 1) << 16;
312
313 /* Section 6.2.1: invalid PD field */
314 if (llc_hdr[0] & 0x80)
315 return -EIO;
316
317 /* This only works for the MS->SGSN direction */
318 if (llc_hdr[0] & 0x40)
319 ghp->is_cmd = 0;
320 else
321 ghp->is_cmd = 1;
322
323 ghp->sapi = llc_hdr[0] & 0xf;
324
325 /* Section 6.2.3: check for reserved SAPI */
326 switch (ghp->sapi) {
327 case 0:
328 case 4:
329 case 6:
330 case 0xa:
331 case 0xc:
332 case 0xd:
333 case 0xf:
334 return -EINVAL;
335 }
336
337 if ((ctrl[0] & 0x80) == 0) {
338 /* I (Information transfer + Supervisory) format */
339 u_int8_t k;
340
341 ghp->data = ctrl + 3;
342
343 if (ctrl[0] & 0x40)
344 ghp->ack_req = 1;
345
346 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
347 ghp->seq_tx |= (ctrl[1] >> 4);
348
349 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
350 ghp->seq_rx |= (ctrl[2] >> 2);
351
352 switch (ctrl[2] & 0x03) {
353 case 0:
354 ghp->cmd = GPRS_LLC_RR;
355 break;
356 case 1:
357 ghp->cmd = GPRS_LLC_ACK;
358 break;
359 case 2:
360 ghp->cmd = GPRS_LLC_RNR;
361 break;
362 case 3:
363 ghp->cmd = GPRS_LLC_SACK;
364 k = ctrl[3] & 0x1f;
365 ghp->data += 1 + k;
366 break;
367 }
368 } else if ((ctrl[0] & 0xc0) == 0x80) {
369 /* S (Supervisory) format */
370 ghp->data = NULL;
371
372 if (ctrl[0] & 0x20)
373 ghp->ack_req = 1;
374 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
375 ghp->seq_rx |= (ctrl[1] >> 2);
376
377 switch (ctrl[1] & 0x03) {
378 case 0:
379 ghp->cmd = GPRS_LLC_RR;
380 break;
381 case 1:
382 ghp->cmd = GPRS_LLC_ACK;
383 break;
384 case 2:
385 ghp->cmd = GPRS_LLC_RNR;
386 break;
387 case 3:
388 ghp->cmd = GPRS_LLC_SACK;
389 break;
390 }
391 } else if ((ctrl[0] & 0xe0) == 0xc0) {
392 /* UI (Unconfirmed Inforamtion) format */
393 ghp->data = ctrl + 2;
394
395 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
396 ghp->seq_tx |= (ctrl[1] >> 2);
397 if (ctrl[1] & 0x02) {
398 ghp->is_encrypted = 1;
399 /* FIXME: encryption */
400 }
401 if (ctrl[1] & 0x01) {
402 /* FCS over hdr + all inf fields */
403 } else {
404 /* FCS over hdr + N202 octets (4) */
405 if (crc_length > UI_HDR_LEN + N202)
406 crc_length = UI_HDR_LEN + N202;
407 }
408 } else {
409 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
410 ghp->data = NULL;
411
412 switch (ctrl[0] & 0xf) {
413 case 0:
414 ghp->cmd = GPRS_LLC_NULL;
415 break;
416 case 0x1:
417 ghp->cmd = GPRS_LLC_DM;
418 break;
419 case 0x4:
420 ghp->cmd = GPRS_LLC_DISC;
421 break;
422 case 0x6:
423 ghp->cmd = GPRS_LLC_UA;
424 break;
425 case 0x7:
426 ghp->cmd = GPRS_LLC_SABM;
427 break;
428 case 0x8:
429 ghp->cmd = GPRS_LLC_FRMR;
430 break;
431 case 0xb:
432 ghp->cmd = GPRS_LLC_XID;
433 break;
434 default:
435 return -EIO;
436 }
437 }
438
439 /* calculate what FCS we expect */
440 ghp->fcs_calc = gprs_llc_fcs(llc_hdr, crc_length);
441
442 /* FIXME: parse sack frame */
443}
444
Harald Weltea2665542010-05-02 09:28:11 +0200445/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800446int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
447{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200448 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte943c5bc2010-04-30 16:33:12 +0200449 struct gprs_llc_hdr *lh = msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800450 struct gprs_llc_hdr_parsed llhp;
451 struct gprs_llc_entity *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200452 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800453
454 rc = gprs_llc_hdr_parse(&llhp, lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Weltea2665542010-05-02 09:28:11 +0200455 /* FIXME */
Harald Welte9b455bf2010-03-14 15:45:01 +0800456
457 gprs_llc_hdr_dump(&llhp);
Harald Weltea2665542010-05-02 09:28:11 +0200458
459 /* find the LLC Entity for this TLLI+SAPI tuple */
460 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
461 /* allocate a new LLE if needed */
462 if (!lle)
463 lle = lle_alloc(msgb_tlli(msg), llhp.sapi);
464
Harald Welte9b455bf2010-03-14 15:45:01 +0800465 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Weltea2665542010-05-02 09:28:11 +0200466 /* FIXME */
Harald Welte9b455bf2010-03-14 15:45:01 +0800467
468 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200469 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800470 switch (llhp.sapi) {
471 case GPRS_SAPI_GMM:
472 rc = gsm0408_gprs_rcvmsg(msg);
Harald Weltea2665542010-05-02 09:28:11 +0200473 break;
474 case GPRS_SAPI_TOM2:
475 case GPRS_SAPI_TOM8:
476 /* FIXME */
477 case GPRS_SAPI_SNDCP3:
478 case GPRS_SAPI_SNDCP5:
479 case GPRS_SAPI_SNDCP9:
480 case GPRS_SAPI_SNDCP11:
481 /* FIXME */
482 case GPRS_SAPI_SMS:
483 /* FIXME */
484 default:
485 LOGP(DGPRS, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
486 rc = -EINVAL;
487 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800488 }
489 }
490
Harald Weltea2665542010-05-02 09:28:11 +0200491 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800492}