blob: c3df013525352b13d6c238a82b9457cace4fdcf2 [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,
Harald Welte1ae09c72010-05-13 19:22:55 +0200114 GPRS_LLC_UI,
Harald Welte9b455bf2010-03-14 15:45:01 +0800115};
116
117struct gprs_llc_hdr_parsed {
Harald Welteeaa614c2010-05-02 11:26:34 +0200118 uint8_t sapi;
119 uint8_t is_cmd:1,
Harald Welte9b455bf2010-03-14 15:45:01 +0800120 ack_req:1,
121 is_encrypted:1;
Harald Welteeaa614c2010-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 Welte5658a1a2010-05-03 13:25:07 +0200127 uint16_t data_len;
Harald Welte9b455bf2010-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 Welteeaa614c2010-05-02 11:26:34 +0200136static int gprs_llc_fcs(uint8_t *data, unsigned int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800137{
Harald Welteeaa614c2010-05-02 11:26:34 +0200138 uint32_t fcs_calc;
Harald Welte9b455bf2010-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 Welte9b455bf2010-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 */
155 lle->state = GPRS_LLS_ASSIGNED_ADM;
156 }
157
158 switch (lle->state) {
159 case GPRS_LLS_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200160 /* FIXME: retransmit SABM */
161 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800162 lle->retrans_ctr++;
163 break;
164 case GPRS_LLS_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200165 /* FIXME: retransmit DISC */
166 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-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 Welte1ae09c72010-05-13 19:22:55 +0200178 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
179 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800180 lle->retrans_ctr++;
181 }
182}
183
Harald Welte10997d02010-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 Welte1ae09c72010-05-13 19:22:55 +0200217 /* Send BSSGP-DL-UNITDATA.req */
Harald Welte10997d02010-05-03 12:28:12 +0200218 return gprs_bssgp_tx_dl_ud(msg);
219}
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 */
225 msgb_tlli(msg) = lle->tlli;
226 msgb_bvci(msg) = lle->bvci;
227 msgb_nsei(msg) = lle->nsei;
228
229 return gprs_llc_tx_u(msg, lle->sapi, 0, GPRS_LLC_U_XID, 1);
230}
231
Harald Welte9b455bf2010-03-14 15:45:01 +0800232/* Transmit a UI frame over the given SAPI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200233int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command)
Harald Welte9b455bf2010-03-14 15:45:01 +0800234{
Harald Weltee6afd602010-05-02 11:19:37 +0200235 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200236 uint8_t *fcs, *llch;
237 uint8_t addr, ctrl[2];
238 uint32_t fcs_calc;
239 uint16_t nu = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800240
Harald Weltee6afd602010-05-02 11:19:37 +0200241 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
242
243 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
244 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
245 if (!lle)
246 lle = lle_alloc(msgb_tlli(msg), sapi);
247 /* Update LLE's (BVCI, NSEI) tuple */
248 lle->bvci = msgb_bvci(msg);
249 lle->nsei = msgb_nsei(msg);
250
Harald Welte9b455bf2010-03-14 15:45:01 +0800251 /* Address Field */
252 addr = sapi & 0xf;
253 if (command)
254 addr |= 0x40;
255
256 /* Control Field */
257 ctrl[0] = 0xc0;
258 ctrl[0] |= nu >> 6;
259 ctrl[1] = (nu << 2) & 0xfc;
260 ctrl[1] |= 0x01; /* Protected Mode */
261
262 /* prepend LLC UI header */
263 llch = msgb_push(msg, 3);
264 llch[0] = addr;
265 llch[1] = ctrl[0];
266 llch[2] = ctrl[1];
267
268 /* append FCS to end of frame */
269 fcs = msgb_put(msg, 3);
270 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
271 fcs[0] = fcs_calc & 0xff;
272 fcs[1] = (fcs_calc >> 8) & 0xff;
273 fcs[2] = (fcs_calc >> 16) & 0xff;
274
Harald Weltee6afd602010-05-02 11:19:37 +0200275 /* Identifiers passed down: (BVCI, NSEI) */
276
Harald Welte1ae09c72010-05-13 19:22:55 +0200277 /* Send BSSGP-DL-UNITDATA.req */
Harald Welte9b455bf2010-03-14 15:45:01 +0800278 return gprs_bssgp_tx_dl_ud(msg);
279}
280
281static int gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
282{
283 DEBUGP(DGPRS, "LLC SAPI=%u %c %c FCS=0x%06x(%s) ",
284 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
285 gph->fcs, gph->fcs_calc == gph->fcs ? "correct" : "WRONG");
286
287 if (gph->cmd)
288 DEBUGPC(DGPRS, "CMD=%u ", gph->cmd);
289
290 if (gph->data)
291 DEBUGPC(DGPRS, "DATA ");
292
293 DEBUGPC(DGPRS, "\n");
294}
295static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
296 struct gprs_llc_lle *lle)
297{
298 switch (gph->cmd) {
299 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
300 lle->v_sent = lle->v_ack = lle->v_recv = 0;
301 if (lle->state == GPRS_LLS_ASSIGNED_ADM) {
302 /* start re-establishment (8.7.1) */
303 }
304 lle->state = GPRS_LLS_REMOTE_EST;
305 /* FIXME: Send UA */
306 lle->state = GPRS_LLS_ABM;
307 /* FIXME: process data */
308 break;
309 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
310 /* FIXME: Send UA */
311 /* terminate ABM */
312 lle->state = GPRS_LLS_ASSIGNED_ADM;
313 break;
314 case GPRS_LLC_UA: /* Section 6.4.1.3 */
315 if (lle->state == GPRS_LLS_LOCAL_EST)
316 lle->state = GPRS_LLS_ABM;
317 break;
318 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
319 if (lle->state == GPRS_LLS_LOCAL_EST)
320 lle->state = GPRS_LLS_ASSIGNED_ADM;
321 break;
322 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
323 break;
324 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte5658a1a2010-05-03 13:25:07 +0200325 /* FIXME: implement XID negotiation using SNDCP */
326 {
327 struct msgb *resp;
328 uint8_t *xid;
329 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
330 xid = msgb_put(resp, gph->data_len);
331 memcpy(xid, gph->data, gph->data_len);
332 gprs_llc_tx_xid(lle, resp);
333 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800334 break;
335 }
336
337 return 0;
338}
339
340/* parse a GPRS LLC header, also check for invalid frames */
341static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Harald Welteeaa614c2010-05-02 11:26:34 +0200342 const uint8_t *llc_hdr, int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800343{
Harald Welteeaa614c2010-05-02 11:26:34 +0200344 uint8_t *ctrl = llc_hdr+1;
Harald Welte9b455bf2010-03-14 15:45:01 +0800345 int is_sack = 0;
346 unsigned int crc_length;
Harald Welteeaa614c2010-05-02 11:26:34 +0200347 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800348
349 if (len <= CRC24_LENGTH)
350 return -EIO;
351
352 crc_length = len - CRC24_LENGTH;
353
354 ghp->ack_req = 0;
355
356 /* Section 5.5: FCS */
357 ghp->fcs = *(llc_hdr + len - 3);
358 ghp->fcs |= *(llc_hdr + len - 2) << 8;
359 ghp->fcs |= *(llc_hdr + len - 1) << 16;
360
361 /* Section 6.2.1: invalid PD field */
362 if (llc_hdr[0] & 0x80)
363 return -EIO;
364
365 /* This only works for the MS->SGSN direction */
366 if (llc_hdr[0] & 0x40)
367 ghp->is_cmd = 0;
368 else
369 ghp->is_cmd = 1;
370
371 ghp->sapi = llc_hdr[0] & 0xf;
372
373 /* Section 6.2.3: check for reserved SAPI */
374 switch (ghp->sapi) {
375 case 0:
376 case 4:
377 case 6:
378 case 0xa:
379 case 0xc:
380 case 0xd:
381 case 0xf:
382 return -EINVAL;
383 }
384
385 if ((ctrl[0] & 0x80) == 0) {
386 /* I (Information transfer + Supervisory) format */
Harald Welteeaa614c2010-05-02 11:26:34 +0200387 uint8_t k;
Harald Welte9b455bf2010-03-14 15:45:01 +0800388
389 ghp->data = ctrl + 3;
390
391 if (ctrl[0] & 0x40)
392 ghp->ack_req = 1;
393
394 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
395 ghp->seq_tx |= (ctrl[1] >> 4);
396
397 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
398 ghp->seq_rx |= (ctrl[2] >> 2);
399
400 switch (ctrl[2] & 0x03) {
401 case 0:
402 ghp->cmd = GPRS_LLC_RR;
403 break;
404 case 1:
405 ghp->cmd = GPRS_LLC_ACK;
406 break;
407 case 2:
408 ghp->cmd = GPRS_LLC_RNR;
409 break;
410 case 3:
411 ghp->cmd = GPRS_LLC_SACK;
412 k = ctrl[3] & 0x1f;
413 ghp->data += 1 + k;
414 break;
415 }
Harald Welte5658a1a2010-05-03 13:25:07 +0200416 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800417 } else if ((ctrl[0] & 0xc0) == 0x80) {
418 /* S (Supervisory) format */
419 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200420 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800421
422 if (ctrl[0] & 0x20)
423 ghp->ack_req = 1;
424 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
425 ghp->seq_rx |= (ctrl[1] >> 2);
426
427 switch (ctrl[1] & 0x03) {
428 case 0:
429 ghp->cmd = GPRS_LLC_RR;
430 break;
431 case 1:
432 ghp->cmd = GPRS_LLC_ACK;
433 break;
434 case 2:
435 ghp->cmd = GPRS_LLC_RNR;
436 break;
437 case 3:
438 ghp->cmd = GPRS_LLC_SACK;
439 break;
440 }
441 } else if ((ctrl[0] & 0xe0) == 0xc0) {
442 /* UI (Unconfirmed Inforamtion) format */
Harald Welte1ae09c72010-05-13 19:22:55 +0200443 ghp->cmd = GPRS_LLC_UI;
Harald Welte9b455bf2010-03-14 15:45:01 +0800444 ghp->data = ctrl + 2;
Harald Welte5658a1a2010-05-03 13:25:07 +0200445 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800446
447 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
448 ghp->seq_tx |= (ctrl[1] >> 2);
449 if (ctrl[1] & 0x02) {
450 ghp->is_encrypted = 1;
451 /* FIXME: encryption */
452 }
453 if (ctrl[1] & 0x01) {
454 /* FCS over hdr + all inf fields */
455 } else {
456 /* FCS over hdr + N202 octets (4) */
457 if (crc_length > UI_HDR_LEN + N202)
458 crc_length = UI_HDR_LEN + N202;
459 }
460 } else {
461 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
462 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200463 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800464
465 switch (ctrl[0] & 0xf) {
Harald Welte5658a1a2010-05-03 13:25:07 +0200466 case GPRS_LLC_U_NULL_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800467 ghp->cmd = GPRS_LLC_NULL;
468 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200469 case GPRS_LLC_U_DM_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800470 ghp->cmd = GPRS_LLC_DM;
471 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200472 case GPRS_LLC_U_DISC_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800473 ghp->cmd = GPRS_LLC_DISC;
474 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200475 case GPRS_LLC_U_UA_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800476 ghp->cmd = GPRS_LLC_UA;
477 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200478 case GPRS_LLC_U_SABM_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800479 ghp->cmd = GPRS_LLC_SABM;
480 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200481 case GPRS_LLC_U_FRMR_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800482 ghp->cmd = GPRS_LLC_FRMR;
483 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200484 case GPRS_LLC_U_XID:
Harald Welte9b455bf2010-03-14 15:45:01 +0800485 ghp->cmd = GPRS_LLC_XID;
Harald Welte5658a1a2010-05-03 13:25:07 +0200486 ghp->data = ctrl + 1;
487 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800488 break;
489 default:
490 return -EIO;
491 }
492 }
493
494 /* calculate what FCS we expect */
495 ghp->fcs_calc = gprs_llc_fcs(llc_hdr, crc_length);
496
497 /* FIXME: parse sack frame */
Harald Welte1ae09c72010-05-13 19:22:55 +0200498 if (ghp->cmd == GPRS_LLC_SACK) {
499 DEBUGP(DGPRS, "Unsupported SACK frame\n");
500 return -EIO;
501 }
502
503 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800504}
505
Harald Weltea2665542010-05-02 09:28:11 +0200506/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800507int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
508{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200509 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte943c5bc2010-04-30 16:33:12 +0200510 struct gprs_llc_hdr *lh = msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800511 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200512 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200513 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800514
Harald Welte11d7c102010-05-02 11:54:55 +0200515 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
516
Harald Welte9b455bf2010-03-14 15:45:01 +0800517 rc = gprs_llc_hdr_parse(&llhp, lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800518 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200519 if (rc < 0) {
520 DEBUGP(DGPRS, "Error during LLC header parsing\n");
521 return rc;
522 }
523
524 if (llhp.fcs != llhp.fcs_calc) {
525 DEBUGP(DGPRS, "Dropping frame with invalid FCS\n");
526 return -EIO;
527 }
Harald Weltea2665542010-05-02 09:28:11 +0200528
529 /* find the LLC Entity for this TLLI+SAPI tuple */
530 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
Harald Welte1ae09c72010-05-13 19:22:55 +0200531
532 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
533 * except UID and XID frames with SAPI=1 */
534 if (!lle && llhp.sapi == GPRS_SAPI_GMM &&
535 (llhp.cmd == GPRS_LLC_XID || llhp.cmd == GPRS_LLC_UI)) {
536 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
Harald Weltea2665542010-05-02 09:28:11 +0200537 lle = lle_alloc(msgb_tlli(msg), llhp.sapi);
Harald Welte1ae09c72010-05-13 19:22:55 +0200538 } else {
539 DEBUGP(DGPRS, "unknown TLLI/SAPI: Silently dropping\n");
540 return 0;
541 }
Harald Weltea2665542010-05-02 09:28:11 +0200542
Harald Welte10997d02010-05-03 12:28:12 +0200543 /* Update LLE's (BVCI, NSEI) tuple */
544 lle->bvci = msgb_bvci(msg);
545 lle->nsei = msgb_nsei(msg);
546
Harald Welte1ae09c72010-05-13 19:22:55 +0200547 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800548 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200549 if (rc < 0)
550 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800551
Harald Welte1ae09c72010-05-13 19:22:55 +0200552 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte9b455bf2010-03-14 15:45:01 +0800553 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200554 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800555 switch (llhp.sapi) {
556 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200557 /* send LL_UNITDATA_IND to GMM */
Harald Welte9b455bf2010-03-14 15:45:01 +0800558 rc = gsm0408_gprs_rcvmsg(msg);
Harald Weltea2665542010-05-02 09:28:11 +0200559 break;
560 case GPRS_SAPI_TOM2:
561 case GPRS_SAPI_TOM8:
Harald Welte1ae09c72010-05-13 19:22:55 +0200562 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200563 case GPRS_SAPI_SNDCP3:
564 case GPRS_SAPI_SNDCP5:
565 case GPRS_SAPI_SNDCP9:
566 case GPRS_SAPI_SNDCP11:
Harald Welte1ae09c72010-05-13 19:22:55 +0200567 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
Harald Weltea2665542010-05-02 09:28:11 +0200568 case GPRS_SAPI_SMS:
569 /* FIXME */
570 default:
571 LOGP(DGPRS, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
572 rc = -EINVAL;
573 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800574 }
575 }
576
Harald Weltea2665542010-05-02 09:28:11 +0200577 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800578}