blob: aea40d360b6b87179aa6c210704fd69eca539547 [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;
64};
65
Harald Weltea2665542010-05-02 09:28:11 +020066static LLIST_HEAD(gprs_llc_lles);
67void *llc_tall_ctx;
68
69/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
70static struct gprs_llc_lle *lle_by_tlli_sapi(uint32_t tlli, uint32_t sapi)
71{
72 struct gprs_llc_lle *lle;
73
74 llist_for_each_entry(lle, &gprs_llc_lles, list) {
75 if (lle->tlli == tlli && lle->sapi == sapi)
76 return lle;
77 }
78 return NULL;
79}
80
81static struct gprs_llc_lle *lle_alloc(uint32_t tlli, uint32_t sapi)
82{
83 struct gprs_llc_lle *lle;
84
85 lle = talloc_zero(llc_tall_ctx, struct gprs_llc_lle);
86 if (!lle)
87 return NULL;
88
89 lle->tlli = tlli;
90 lle->sapi = sapi;
91 lle->state = GPRS_LLS_UNASSIGNED;
92 llist_add(&lle->list, &gprs_llc_lles);
93
94 return lle;
95}
96
Harald Welte9b455bf2010-03-14 15:45:01 +080097enum gprs_llc_cmd {
98 GPRS_LLC_NULL,
99 GPRS_LLC_RR,
100 GPRS_LLC_ACK,
101 GPRS_LLC_RNR,
102 GPRS_LLC_SACK,
103 GPRS_LLC_DM,
104 GPRS_LLC_DISC,
105 GPRS_LLC_UA,
106 GPRS_LLC_SABM,
107 GPRS_LLC_FRMR,
108 GPRS_LLC_XID,
109};
110
111struct gprs_llc_hdr_parsed {
112 u_int8_t sapi;
113 u_int8_t is_cmd:1,
114 ack_req:1,
115 is_encrypted:1;
116 u_int32_t seq_rx;
117 u_int32_t seq_tx;
118 u_int32_t fcs;
119 u_int32_t fcs_calc;
120 u_int8_t *data;
121 enum gprs_llc_cmd cmd;
122};
123
124#define LLC_ALLOC_SIZE 16384
125#define UI_HDR_LEN 3
126#define N202 4
127#define CRC24_LENGTH 3
128
129static int gprs_llc_fcs(u_int8_t *data, unsigned int len)
130{
131 u_int32_t fcs_calc;
132
133 fcs_calc = crc24_calc(INIT_CRC24, data, len);
134 fcs_calc = ~fcs_calc;
135 fcs_calc &= 0xffffff;
136
137 return fcs_calc;
138}
139
140/* transmit a simple U frame */
141static int gprs_llc_tx_u()
142{
143 struct msgb *msg = msgb_alloc(LLC_ALLOC_SIZE, "GPRS/LLC");
144
145 if (!msg)
146 return -ENOMEM;
147
148
149
150 /* transmit the frame via BSSGP->NS->... */
151}
152
153static void t200_expired(void *data)
154{
155 struct gprs_llc_lle *lle = data;
156
157 /* 8.5.1.3: Expiry of T200 */
158
159 if (lle->retrans_ctr >= lle->n200) {
160 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
161 lle->state = GPRS_LLS_ASSIGNED_ADM;
162 }
163
164 switch (lle->state) {
165 case GPRS_LLS_LOCAL_EST:
166 /* retransmit SABM */
167 /* re-start T200 */
168 lle->retrans_ctr++;
169 break;
170 case GPRS_LLS_LOCAL_REL:
171 /* retransmit DISC */
172 /* re-start T200 */
173 lle->retrans_ctr++;
174 break;
175 }
176
177}
178
179static void t201_expired(void *data)
180{
181 struct gprs_llc_lle *lle = data;
182
183 if (lle->retrans_ctr < lle->n200) {
184 /* transmit apropriate supervisory frame (8.6.4.1) */
185 /* set timer T201 */
186 lle->retrans_ctr++;
187 }
188}
189
190/* Transmit a UI frame over the given SAPI */
191int gprs_llc_tx_ui(struct msgb *msg, u_int8_t sapi, int command)
192{
193 u_int8_t *fcs, *llch;
194 u_int8_t addr, ctrl[2];
195 u_int32_t fcs_calc;
196 u_int16_t nu = 0;
197
198 /* Address Field */
199 addr = sapi & 0xf;
200 if (command)
201 addr |= 0x40;
202
203 /* Control Field */
204 ctrl[0] = 0xc0;
205 ctrl[0] |= nu >> 6;
206 ctrl[1] = (nu << 2) & 0xfc;
207 ctrl[1] |= 0x01; /* Protected Mode */
208
209 /* prepend LLC UI header */
210 llch = msgb_push(msg, 3);
211 llch[0] = addr;
212 llch[1] = ctrl[0];
213 llch[2] = ctrl[1];
214
215 /* append FCS to end of frame */
216 fcs = msgb_put(msg, 3);
217 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
218 fcs[0] = fcs_calc & 0xff;
219 fcs[1] = (fcs_calc >> 8) & 0xff;
220 fcs[2] = (fcs_calc >> 16) & 0xff;
221
222 return gprs_bssgp_tx_dl_ud(msg);
223}
224
225static int gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
226{
227 DEBUGP(DGPRS, "LLC SAPI=%u %c %c FCS=0x%06x(%s) ",
228 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
229 gph->fcs, gph->fcs_calc == gph->fcs ? "correct" : "WRONG");
230
231 if (gph->cmd)
232 DEBUGPC(DGPRS, "CMD=%u ", gph->cmd);
233
234 if (gph->data)
235 DEBUGPC(DGPRS, "DATA ");
236
237 DEBUGPC(DGPRS, "\n");
238}
239static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
240 struct gprs_llc_lle *lle)
241{
242 switch (gph->cmd) {
243 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
244 lle->v_sent = lle->v_ack = lle->v_recv = 0;
245 if (lle->state == GPRS_LLS_ASSIGNED_ADM) {
246 /* start re-establishment (8.7.1) */
247 }
248 lle->state = GPRS_LLS_REMOTE_EST;
249 /* FIXME: Send UA */
250 lle->state = GPRS_LLS_ABM;
251 /* FIXME: process data */
252 break;
253 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
254 /* FIXME: Send UA */
255 /* terminate ABM */
256 lle->state = GPRS_LLS_ASSIGNED_ADM;
257 break;
258 case GPRS_LLC_UA: /* Section 6.4.1.3 */
259 if (lle->state == GPRS_LLS_LOCAL_EST)
260 lle->state = GPRS_LLS_ABM;
261 break;
262 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
263 if (lle->state == GPRS_LLS_LOCAL_EST)
264 lle->state = GPRS_LLS_ASSIGNED_ADM;
265 break;
266 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
267 break;
268 case GPRS_LLC_XID: /* Section 6.4.1.6 */
269 break;
270 }
271
272 return 0;
273}
274
275/* parse a GPRS LLC header, also check for invalid frames */
276static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
277 const u_int8_t *llc_hdr, int len)
278{
279 u_int8_t *ctrl = llc_hdr+1;
280 int is_sack = 0;
281 unsigned int crc_length;
282 u_int32_t fcs_calc;
283
284 if (len <= CRC24_LENGTH)
285 return -EIO;
286
287 crc_length = len - CRC24_LENGTH;
288
289 ghp->ack_req = 0;
290
291 /* Section 5.5: FCS */
292 ghp->fcs = *(llc_hdr + len - 3);
293 ghp->fcs |= *(llc_hdr + len - 2) << 8;
294 ghp->fcs |= *(llc_hdr + len - 1) << 16;
295
296 /* Section 6.2.1: invalid PD field */
297 if (llc_hdr[0] & 0x80)
298 return -EIO;
299
300 /* This only works for the MS->SGSN direction */
301 if (llc_hdr[0] & 0x40)
302 ghp->is_cmd = 0;
303 else
304 ghp->is_cmd = 1;
305
306 ghp->sapi = llc_hdr[0] & 0xf;
307
308 /* Section 6.2.3: check for reserved SAPI */
309 switch (ghp->sapi) {
310 case 0:
311 case 4:
312 case 6:
313 case 0xa:
314 case 0xc:
315 case 0xd:
316 case 0xf:
317 return -EINVAL;
318 }
319
320 if ((ctrl[0] & 0x80) == 0) {
321 /* I (Information transfer + Supervisory) format */
322 u_int8_t k;
323
324 ghp->data = ctrl + 3;
325
326 if (ctrl[0] & 0x40)
327 ghp->ack_req = 1;
328
329 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
330 ghp->seq_tx |= (ctrl[1] >> 4);
331
332 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
333 ghp->seq_rx |= (ctrl[2] >> 2);
334
335 switch (ctrl[2] & 0x03) {
336 case 0:
337 ghp->cmd = GPRS_LLC_RR;
338 break;
339 case 1:
340 ghp->cmd = GPRS_LLC_ACK;
341 break;
342 case 2:
343 ghp->cmd = GPRS_LLC_RNR;
344 break;
345 case 3:
346 ghp->cmd = GPRS_LLC_SACK;
347 k = ctrl[3] & 0x1f;
348 ghp->data += 1 + k;
349 break;
350 }
351 } else if ((ctrl[0] & 0xc0) == 0x80) {
352 /* S (Supervisory) format */
353 ghp->data = NULL;
354
355 if (ctrl[0] & 0x20)
356 ghp->ack_req = 1;
357 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
358 ghp->seq_rx |= (ctrl[1] >> 2);
359
360 switch (ctrl[1] & 0x03) {
361 case 0:
362 ghp->cmd = GPRS_LLC_RR;
363 break;
364 case 1:
365 ghp->cmd = GPRS_LLC_ACK;
366 break;
367 case 2:
368 ghp->cmd = GPRS_LLC_RNR;
369 break;
370 case 3:
371 ghp->cmd = GPRS_LLC_SACK;
372 break;
373 }
374 } else if ((ctrl[0] & 0xe0) == 0xc0) {
375 /* UI (Unconfirmed Inforamtion) format */
376 ghp->data = ctrl + 2;
377
378 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
379 ghp->seq_tx |= (ctrl[1] >> 2);
380 if (ctrl[1] & 0x02) {
381 ghp->is_encrypted = 1;
382 /* FIXME: encryption */
383 }
384 if (ctrl[1] & 0x01) {
385 /* FCS over hdr + all inf fields */
386 } else {
387 /* FCS over hdr + N202 octets (4) */
388 if (crc_length > UI_HDR_LEN + N202)
389 crc_length = UI_HDR_LEN + N202;
390 }
391 } else {
392 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
393 ghp->data = NULL;
394
395 switch (ctrl[0] & 0xf) {
396 case 0:
397 ghp->cmd = GPRS_LLC_NULL;
398 break;
399 case 0x1:
400 ghp->cmd = GPRS_LLC_DM;
401 break;
402 case 0x4:
403 ghp->cmd = GPRS_LLC_DISC;
404 break;
405 case 0x6:
406 ghp->cmd = GPRS_LLC_UA;
407 break;
408 case 0x7:
409 ghp->cmd = GPRS_LLC_SABM;
410 break;
411 case 0x8:
412 ghp->cmd = GPRS_LLC_FRMR;
413 break;
414 case 0xb:
415 ghp->cmd = GPRS_LLC_XID;
416 break;
417 default:
418 return -EIO;
419 }
420 }
421
422 /* calculate what FCS we expect */
423 ghp->fcs_calc = gprs_llc_fcs(llc_hdr, crc_length);
424
425 /* FIXME: parse sack frame */
426}
427
Harald Weltea2665542010-05-02 09:28:11 +0200428/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800429int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
430{
431 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msg->l3h;
Harald Welte943c5bc2010-04-30 16:33:12 +0200432 struct gprs_llc_hdr *lh = msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800433 struct gprs_llc_hdr_parsed llhp;
434 struct gprs_llc_entity *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200435 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800436
437 rc = gprs_llc_hdr_parse(&llhp, lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Weltea2665542010-05-02 09:28:11 +0200438 /* FIXME */
Harald Welte9b455bf2010-03-14 15:45:01 +0800439
440 gprs_llc_hdr_dump(&llhp);
Harald Weltea2665542010-05-02 09:28:11 +0200441
442 /* find the LLC Entity for this TLLI+SAPI tuple */
443 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
444 /* allocate a new LLE if needed */
445 if (!lle)
446 lle = lle_alloc(msgb_tlli(msg), llhp.sapi);
447
Harald Welte9b455bf2010-03-14 15:45:01 +0800448 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Weltea2665542010-05-02 09:28:11 +0200449 /* FIXME */
Harald Welte9b455bf2010-03-14 15:45:01 +0800450
451 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200452 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800453 switch (llhp.sapi) {
454 case GPRS_SAPI_GMM:
455 rc = gsm0408_gprs_rcvmsg(msg);
Harald Weltea2665542010-05-02 09:28:11 +0200456 break;
457 case GPRS_SAPI_TOM2:
458 case GPRS_SAPI_TOM8:
459 /* FIXME */
460 case GPRS_SAPI_SNDCP3:
461 case GPRS_SAPI_SNDCP5:
462 case GPRS_SAPI_SNDCP9:
463 case GPRS_SAPI_SNDCP11:
464 /* FIXME */
465 case GPRS_SAPI_SMS:
466 /* FIXME */
467 default:
468 LOGP(DGPRS, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
469 rc = -EINVAL;
470 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800471 }
472 }
473
Harald Weltea2665542010-05-02 09:28:11 +0200474 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800475}