blob: 3eea82d4834509e844495d86bce9e15192a7ed2c [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
Harald Welte2e918a82010-05-18 12:20:12 +020037LLIST_HEAD(gprs_llc_lles);
Harald Weltea2665542010-05-02 09:28:11 +020038void *llc_tall_ctx;
39
40/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
41static struct gprs_llc_lle *lle_by_tlli_sapi(uint32_t tlli, uint32_t sapi)
42{
43 struct gprs_llc_lle *lle;
44
45 llist_for_each_entry(lle, &gprs_llc_lles, list) {
46 if (lle->tlli == tlli && lle->sapi == sapi)
47 return lle;
48 }
49 return NULL;
50}
51
52static struct gprs_llc_lle *lle_alloc(uint32_t tlli, uint32_t sapi)
53{
54 struct gprs_llc_lle *lle;
55
56 lle = talloc_zero(llc_tall_ctx, struct gprs_llc_lle);
57 if (!lle)
58 return NULL;
59
60 lle->tlli = tlli;
61 lle->sapi = sapi;
62 lle->state = GPRS_LLS_UNASSIGNED;
63 llist_add(&lle->list, &gprs_llc_lles);
64
65 return lle;
66}
67
Harald Welte9b455bf2010-03-14 15:45:01 +080068enum gprs_llc_cmd {
69 GPRS_LLC_NULL,
70 GPRS_LLC_RR,
71 GPRS_LLC_ACK,
72 GPRS_LLC_RNR,
73 GPRS_LLC_SACK,
74 GPRS_LLC_DM,
75 GPRS_LLC_DISC,
76 GPRS_LLC_UA,
77 GPRS_LLC_SABM,
78 GPRS_LLC_FRMR,
79 GPRS_LLC_XID,
Harald Welte1ae09c72010-05-13 19:22:55 +020080 GPRS_LLC_UI,
Harald Welte9b455bf2010-03-14 15:45:01 +080081};
82
83struct gprs_llc_hdr_parsed {
Harald Welteeaa614c2010-05-02 11:26:34 +020084 uint8_t sapi;
85 uint8_t is_cmd:1,
Harald Welte9b455bf2010-03-14 15:45:01 +080086 ack_req:1,
87 is_encrypted:1;
Harald Welteeaa614c2010-05-02 11:26:34 +020088 uint32_t seq_rx;
89 uint32_t seq_tx;
90 uint32_t fcs;
91 uint32_t fcs_calc;
92 uint8_t *data;
Harald Welte5658a1a2010-05-03 13:25:07 +020093 uint16_t data_len;
Harald Welte9b455bf2010-03-14 15:45:01 +080094 enum gprs_llc_cmd cmd;
95};
96
97#define LLC_ALLOC_SIZE 16384
98#define UI_HDR_LEN 3
99#define N202 4
100#define CRC24_LENGTH 3
101
Harald Welteeaa614c2010-05-02 11:26:34 +0200102static int gprs_llc_fcs(uint8_t *data, unsigned int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800103{
Harald Welteeaa614c2010-05-02 11:26:34 +0200104 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800105
106 fcs_calc = crc24_calc(INIT_CRC24, data, len);
107 fcs_calc = ~fcs_calc;
108 fcs_calc &= 0xffffff;
109
110 return fcs_calc;
111}
112
Harald Welte9b455bf2010-03-14 15:45:01 +0800113static void t200_expired(void *data)
114{
115 struct gprs_llc_lle *lle = data;
116
117 /* 8.5.1.3: Expiry of T200 */
118
119 if (lle->retrans_ctr >= lle->n200) {
120 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
121 lle->state = GPRS_LLS_ASSIGNED_ADM;
122 }
123
124 switch (lle->state) {
125 case GPRS_LLS_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200126 /* FIXME: retransmit SABM */
127 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800128 lle->retrans_ctr++;
129 break;
130 case GPRS_LLS_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200131 /* FIXME: retransmit DISC */
132 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800133 lle->retrans_ctr++;
134 break;
135 }
136
137}
138
139static void t201_expired(void *data)
140{
141 struct gprs_llc_lle *lle = data;
142
143 if (lle->retrans_ctr < lle->n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200144 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
145 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800146 lle->retrans_ctr++;
147 }
148}
149
Harald Welte10997d02010-05-03 12:28:12 +0200150int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
151 enum gprs_llc_u_cmd u_cmd, int pf_bit)
152{
153 uint8_t *fcs, *llch;
154 uint8_t addr, ctrl;
155 uint32_t fcs_calc;
156
157 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
158
159 /* Address Field */
160 addr = sapi & 0xf;
161 if (command)
162 addr |= 0x40;
163
164 /* 6.3 Figure 8 */
165 ctrl = 0xe0 | u_cmd;
166 if (pf_bit)
167 ctrl |= 0x10;
168
169 /* prepend LLC UI header */
170 llch = msgb_push(msg, 2);
171 llch[0] = addr;
172 llch[1] = ctrl;
173
174 /* append FCS to end of frame */
175 fcs = msgb_put(msg, 3);
176 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
177 fcs[0] = fcs_calc & 0xff;
178 fcs[1] = (fcs_calc >> 8) & 0xff;
179 fcs[2] = (fcs_calc >> 16) & 0xff;
180
181 /* Identifiers passed down: (BVCI, NSEI) */
182
Harald Welte1ae09c72010-05-13 19:22:55 +0200183 /* Send BSSGP-DL-UNITDATA.req */
Harald Welte10997d02010-05-03 12:28:12 +0200184 return gprs_bssgp_tx_dl_ud(msg);
185}
186
187/* Send XID response to LLE */
188static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg)
189{
190 /* copy identifiers from LLE to ensure lower layers can route */
191 msgb_tlli(msg) = lle->tlli;
192 msgb_bvci(msg) = lle->bvci;
193 msgb_nsei(msg) = lle->nsei;
194
195 return gprs_llc_tx_u(msg, lle->sapi, 0, GPRS_LLC_U_XID, 1);
196}
197
Harald Welte9b455bf2010-03-14 15:45:01 +0800198/* Transmit a UI frame over the given SAPI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200199int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command)
Harald Welte9b455bf2010-03-14 15:45:01 +0800200{
Harald Weltee6afd602010-05-02 11:19:37 +0200201 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200202 uint8_t *fcs, *llch;
203 uint8_t addr, ctrl[2];
204 uint32_t fcs_calc;
205 uint16_t nu = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800206
Harald Weltee6afd602010-05-02 11:19:37 +0200207 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
208
209 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
210 lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi);
211 if (!lle)
212 lle = lle_alloc(msgb_tlli(msg), sapi);
213 /* Update LLE's (BVCI, NSEI) tuple */
214 lle->bvci = msgb_bvci(msg);
215 lle->nsei = msgb_nsei(msg);
216
Harald Welte9b455bf2010-03-14 15:45:01 +0800217 /* Address Field */
218 addr = sapi & 0xf;
219 if (command)
220 addr |= 0x40;
221
222 /* Control Field */
223 ctrl[0] = 0xc0;
224 ctrl[0] |= nu >> 6;
225 ctrl[1] = (nu << 2) & 0xfc;
226 ctrl[1] |= 0x01; /* Protected Mode */
227
228 /* prepend LLC UI header */
229 llch = msgb_push(msg, 3);
230 llch[0] = addr;
231 llch[1] = ctrl[0];
232 llch[2] = ctrl[1];
233
234 /* append FCS to end of frame */
235 fcs = msgb_put(msg, 3);
236 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
237 fcs[0] = fcs_calc & 0xff;
238 fcs[1] = (fcs_calc >> 8) & 0xff;
239 fcs[2] = (fcs_calc >> 16) & 0xff;
240
Harald Weltee6afd602010-05-02 11:19:37 +0200241 /* Identifiers passed down: (BVCI, NSEI) */
242
Harald Welte1ae09c72010-05-13 19:22:55 +0200243 /* Send BSSGP-DL-UNITDATA.req */
Harald Welte9b455bf2010-03-14 15:45:01 +0800244 return gprs_bssgp_tx_dl_ud(msg);
245}
246
247static int gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
248{
Harald Weltec6ecafe2010-05-13 19:47:50 +0200249 DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x(%s) ",
Harald Welte9b455bf2010-03-14 15:45:01 +0800250 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
251 gph->fcs, gph->fcs_calc == gph->fcs ? "correct" : "WRONG");
252
253 if (gph->cmd)
Harald Weltec6ecafe2010-05-13 19:47:50 +0200254 DEBUGPC(DLLC, "CMD=%u ", gph->cmd);
Harald Welte9b455bf2010-03-14 15:45:01 +0800255
256 if (gph->data)
Harald Weltec6ecafe2010-05-13 19:47:50 +0200257 DEBUGPC(DLLC, "DATA ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800258
Harald Weltec6ecafe2010-05-13 19:47:50 +0200259 DEBUGPC(DLLC, "\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800260}
261static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
262 struct gprs_llc_lle *lle)
263{
264 switch (gph->cmd) {
265 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
266 lle->v_sent = lle->v_ack = lle->v_recv = 0;
267 if (lle->state == GPRS_LLS_ASSIGNED_ADM) {
268 /* start re-establishment (8.7.1) */
269 }
270 lle->state = GPRS_LLS_REMOTE_EST;
271 /* FIXME: Send UA */
272 lle->state = GPRS_LLS_ABM;
273 /* FIXME: process data */
274 break;
275 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
276 /* FIXME: Send UA */
277 /* terminate ABM */
278 lle->state = GPRS_LLS_ASSIGNED_ADM;
279 break;
280 case GPRS_LLC_UA: /* Section 6.4.1.3 */
281 if (lle->state == GPRS_LLS_LOCAL_EST)
282 lle->state = GPRS_LLS_ABM;
283 break;
284 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
285 if (lle->state == GPRS_LLS_LOCAL_EST)
286 lle->state = GPRS_LLS_ASSIGNED_ADM;
287 break;
288 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
289 break;
290 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte5658a1a2010-05-03 13:25:07 +0200291 /* FIXME: implement XID negotiation using SNDCP */
292 {
293 struct msgb *resp;
294 uint8_t *xid;
295 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
296 xid = msgb_put(resp, gph->data_len);
297 memcpy(xid, gph->data, gph->data_len);
298 gprs_llc_tx_xid(lle, resp);
299 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800300 break;
301 }
302
303 return 0;
304}
305
306/* parse a GPRS LLC header, also check for invalid frames */
307static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
Harald Welteeaa614c2010-05-02 11:26:34 +0200308 const uint8_t *llc_hdr, int len)
Harald Welte9b455bf2010-03-14 15:45:01 +0800309{
Harald Welteeaa614c2010-05-02 11:26:34 +0200310 uint8_t *ctrl = llc_hdr+1;
Harald Welte9b455bf2010-03-14 15:45:01 +0800311 int is_sack = 0;
312 unsigned int crc_length;
Harald Welteeaa614c2010-05-02 11:26:34 +0200313 uint32_t fcs_calc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800314
315 if (len <= CRC24_LENGTH)
316 return -EIO;
317
318 crc_length = len - CRC24_LENGTH;
319
320 ghp->ack_req = 0;
321
322 /* Section 5.5: FCS */
323 ghp->fcs = *(llc_hdr + len - 3);
324 ghp->fcs |= *(llc_hdr + len - 2) << 8;
325 ghp->fcs |= *(llc_hdr + len - 1) << 16;
326
327 /* Section 6.2.1: invalid PD field */
328 if (llc_hdr[0] & 0x80)
329 return -EIO;
330
331 /* This only works for the MS->SGSN direction */
332 if (llc_hdr[0] & 0x40)
333 ghp->is_cmd = 0;
334 else
335 ghp->is_cmd = 1;
336
337 ghp->sapi = llc_hdr[0] & 0xf;
338
339 /* Section 6.2.3: check for reserved SAPI */
340 switch (ghp->sapi) {
341 case 0:
342 case 4:
343 case 6:
344 case 0xa:
345 case 0xc:
346 case 0xd:
347 case 0xf:
348 return -EINVAL;
349 }
350
351 if ((ctrl[0] & 0x80) == 0) {
352 /* I (Information transfer + Supervisory) format */
Harald Welteeaa614c2010-05-02 11:26:34 +0200353 uint8_t k;
Harald Welte9b455bf2010-03-14 15:45:01 +0800354
355 ghp->data = ctrl + 3;
356
357 if (ctrl[0] & 0x40)
358 ghp->ack_req = 1;
359
360 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
361 ghp->seq_tx |= (ctrl[1] >> 4);
362
363 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
364 ghp->seq_rx |= (ctrl[2] >> 2);
365
366 switch (ctrl[2] & 0x03) {
367 case 0:
368 ghp->cmd = GPRS_LLC_RR;
369 break;
370 case 1:
371 ghp->cmd = GPRS_LLC_ACK;
372 break;
373 case 2:
374 ghp->cmd = GPRS_LLC_RNR;
375 break;
376 case 3:
377 ghp->cmd = GPRS_LLC_SACK;
378 k = ctrl[3] & 0x1f;
379 ghp->data += 1 + k;
380 break;
381 }
Harald Welte5658a1a2010-05-03 13:25:07 +0200382 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800383 } else if ((ctrl[0] & 0xc0) == 0x80) {
384 /* S (Supervisory) format */
385 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200386 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800387
388 if (ctrl[0] & 0x20)
389 ghp->ack_req = 1;
390 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
391 ghp->seq_rx |= (ctrl[1] >> 2);
392
393 switch (ctrl[1] & 0x03) {
394 case 0:
395 ghp->cmd = GPRS_LLC_RR;
396 break;
397 case 1:
398 ghp->cmd = GPRS_LLC_ACK;
399 break;
400 case 2:
401 ghp->cmd = GPRS_LLC_RNR;
402 break;
403 case 3:
404 ghp->cmd = GPRS_LLC_SACK;
405 break;
406 }
407 } else if ((ctrl[0] & 0xe0) == 0xc0) {
408 /* UI (Unconfirmed Inforamtion) format */
Harald Welte1ae09c72010-05-13 19:22:55 +0200409 ghp->cmd = GPRS_LLC_UI;
Harald Welte9b455bf2010-03-14 15:45:01 +0800410 ghp->data = ctrl + 2;
Harald Welte5658a1a2010-05-03 13:25:07 +0200411 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800412
413 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
414 ghp->seq_tx |= (ctrl[1] >> 2);
415 if (ctrl[1] & 0x02) {
416 ghp->is_encrypted = 1;
417 /* FIXME: encryption */
418 }
419 if (ctrl[1] & 0x01) {
420 /* FCS over hdr + all inf fields */
421 } else {
422 /* FCS over hdr + N202 octets (4) */
423 if (crc_length > UI_HDR_LEN + N202)
424 crc_length = UI_HDR_LEN + N202;
425 }
426 } else {
427 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
428 ghp->data = NULL;
Harald Welte5658a1a2010-05-03 13:25:07 +0200429 ghp->data_len = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800430
431 switch (ctrl[0] & 0xf) {
Harald Welte5658a1a2010-05-03 13:25:07 +0200432 case GPRS_LLC_U_NULL_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800433 ghp->cmd = GPRS_LLC_NULL;
434 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200435 case GPRS_LLC_U_DM_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800436 ghp->cmd = GPRS_LLC_DM;
437 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200438 case GPRS_LLC_U_DISC_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800439 ghp->cmd = GPRS_LLC_DISC;
440 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200441 case GPRS_LLC_U_UA_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800442 ghp->cmd = GPRS_LLC_UA;
443 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200444 case GPRS_LLC_U_SABM_CMD:
Harald Welte9b455bf2010-03-14 15:45:01 +0800445 ghp->cmd = GPRS_LLC_SABM;
446 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200447 case GPRS_LLC_U_FRMR_RESP:
Harald Welte9b455bf2010-03-14 15:45:01 +0800448 ghp->cmd = GPRS_LLC_FRMR;
449 break;
Harald Welte5658a1a2010-05-03 13:25:07 +0200450 case GPRS_LLC_U_XID:
Harald Welte9b455bf2010-03-14 15:45:01 +0800451 ghp->cmd = GPRS_LLC_XID;
Harald Welte5658a1a2010-05-03 13:25:07 +0200452 ghp->data = ctrl + 1;
453 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800454 break;
455 default:
456 return -EIO;
457 }
458 }
459
460 /* calculate what FCS we expect */
461 ghp->fcs_calc = gprs_llc_fcs(llc_hdr, crc_length);
462
463 /* FIXME: parse sack frame */
Harald Welte1ae09c72010-05-13 19:22:55 +0200464 if (ghp->cmd == GPRS_LLC_SACK) {
Harald Welte1b170d12010-05-13 19:49:06 +0200465 LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200466 return -EIO;
467 }
468
469 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800470}
471
Harald Weltea2665542010-05-02 09:28:11 +0200472/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800473int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
474{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200475 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte943c5bc2010-04-30 16:33:12 +0200476 struct gprs_llc_hdr *lh = msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800477 struct gprs_llc_hdr_parsed llhp;
Harald Welte10997d02010-05-03 12:28:12 +0200478 struct gprs_llc_lle *lle;
Harald Weltea2665542010-05-02 09:28:11 +0200479 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800480
Harald Welte11d7c102010-05-02 11:54:55 +0200481 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
482
Harald Welte9b455bf2010-03-14 15:45:01 +0800483 rc = gprs_llc_hdr_parse(&llhp, lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte9b455bf2010-03-14 15:45:01 +0800484 gprs_llc_hdr_dump(&llhp);
Harald Welte1ae09c72010-05-13 19:22:55 +0200485 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200486 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200487 return rc;
488 }
489
490 if (llhp.fcs != llhp.fcs_calc) {
Harald Welte1b170d12010-05-13 19:49:06 +0200491 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200492 return -EIO;
493 }
Harald Weltea2665542010-05-02 09:28:11 +0200494
495 /* find the LLC Entity for this TLLI+SAPI tuple */
496 lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi);
Harald Welte1ae09c72010-05-13 19:22:55 +0200497
498 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
499 * except UID and XID frames with SAPI=1 */
500 if (!lle && llhp.sapi == GPRS_SAPI_GMM &&
501 (llhp.cmd == GPRS_LLC_XID || llhp.cmd == GPRS_LLC_UI)) {
502 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
Harald Weltea2665542010-05-02 09:28:11 +0200503 lle = lle_alloc(msgb_tlli(msg), llhp.sapi);
Harald Welte1ae09c72010-05-13 19:22:55 +0200504 } else {
Harald Welte1b170d12010-05-13 19:49:06 +0200505 LOGP(DLLC, LOGL_NOTICE,
506 "unknown TLLI/SAPI: Silently dropping\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200507 return 0;
508 }
Harald Weltea2665542010-05-02 09:28:11 +0200509
Harald Welte10997d02010-05-03 12:28:12 +0200510 /* Update LLE's (BVCI, NSEI) tuple */
511 lle->bvci = msgb_bvci(msg);
512 lle->nsei = msgb_nsei(msg);
513
Harald Welte1ae09c72010-05-13 19:22:55 +0200514 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800515 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200516 if (rc < 0)
517 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800518
Harald Welte1ae09c72010-05-13 19:22:55 +0200519 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte9b455bf2010-03-14 15:45:01 +0800520 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200521 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800522 switch (llhp.sapi) {
523 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +0200524 /* send LL_UNITDATA_IND to GMM */
Harald Welte9b455bf2010-03-14 15:45:01 +0800525 rc = gsm0408_gprs_rcvmsg(msg);
Harald Weltea2665542010-05-02 09:28:11 +0200526 break;
527 case GPRS_SAPI_TOM2:
528 case GPRS_SAPI_TOM8:
Harald Welte1ae09c72010-05-13 19:22:55 +0200529 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +0200530 case GPRS_SAPI_SNDCP3:
531 case GPRS_SAPI_SNDCP5:
532 case GPRS_SAPI_SNDCP9:
533 case GPRS_SAPI_SNDCP11:
Harald Welte1ae09c72010-05-13 19:22:55 +0200534 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
Harald Weltea2665542010-05-02 09:28:11 +0200535 case GPRS_SAPI_SMS:
536 /* FIXME */
537 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +0200538 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +0200539 rc = -EINVAL;
540 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800541 }
542 }
543
Harald Weltea2665542010-05-02 09:28:11 +0200544 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800545}