blob: b03979ab362d0c74ce4eeff2c379e49fd8e8335c [file] [log] [blame]
Harald Welte96f71f22010-05-03 19:28:05 +02001/* GPRS SNDCP protocol implementation as per 3GPP TS 04.65 */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
Harald Weltece22f922010-06-03 21:21:21 +02004 * (C) 2010 by On-Waves
Harald Welte96f71f22010-05-03 19:28:05 +02005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <errno.h>
25#include <stdint.h>
26
27#include <osmocore/msgb.h>
28#include <osmocore/linuxlist.h>
29#include <osmocore/timer.h>
30#include <osmocore/talloc.h>
31
32#include <openbsc/gsm_data.h>
33#include <openbsc/debug.h>
34#include <openbsc/gprs_bssgp.h>
35#include <openbsc/gprs_llc.h>
Harald Welteebabdea2010-06-01 18:28:10 +020036#include <openbsc/sgsn.h>
Harald Welte96f71f22010-05-03 19:28:05 +020037
38/* Chapter 7.2: SN-PDU Formats */
39struct sndcp_common_hdr {
40 /* octet 1 */
41 uint8_t nsapi:4;
42 uint8_t more:1;
43 uint8_t type:1;
44 uint8_t first:1;
45 uint8_t spare:1;
Harald Weltece22f922010-06-03 21:21:21 +020046} __attribute__((packed));
47
48/* PCOMP / DCOMP only exist in first fragment */
49struct sndcp_comp_hdr {
Harald Welte96f71f22010-05-03 19:28:05 +020050 /* octet 2 */
Harald Welte5cc2bc32010-06-02 23:17:05 +020051 uint8_t pcomp:4;
52 uint8_t dcomp:4;
Harald Welteebabdea2010-06-01 18:28:10 +020053} __attribute__((packed));
Harald Welte96f71f22010-05-03 19:28:05 +020054
55struct sndcp_udata_hdr {
56 /* octet 3 */
57 uint8_t npdu_high:4;
58 uint8_t seg_nr:4;
59 /* octet 4 */
60 uint8_t npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +020061} __attribute__((packed));
62
63/* See 6.7.1.2 Reassembly */
64enum sndcp_rx_state {
65 SNDCP_RX_S_FIRST,
66 SNDCP_RX_S_SUBSEQ,
67 SNDCP_RX_S_DISCARD,
68};
69
70
71static void *tall_sndcp_ctx;
72
73/* A fragment queue entry, containing one framgent of a N-PDU */
Harald Weltece22f922010-06-03 21:21:21 +020074struct defrag_queue_entry {
Harald Welteebabdea2010-06-01 18:28:10 +020075 struct llist_head list;
Harald Weltece22f922010-06-03 21:21:21 +020076 /* segment number of this fragment */
77 uint32_t seg_nr;
78 /* length of the data area of this fragment */
Harald Welteebabdea2010-06-01 18:28:10 +020079 uint32_t data_len;
Harald Weltece22f922010-06-03 21:21:21 +020080 /* pointer to the data of this fragment */
81 uint8_t *data;
Harald Welteebabdea2010-06-01 18:28:10 +020082};
83
84/* A fragment queue header, maintaining list of fragments for one N-PDU */
Harald Weltece22f922010-06-03 21:21:21 +020085struct defrag_state {
86 /* PDU number for which the defragmentation state applies */
Harald Welteebabdea2010-06-01 18:28:10 +020087 uint16_t npdu;
Harald Weltece22f922010-06-03 21:21:21 +020088 /* highest segment number we have received so far */
89 uint8_t highest_seg;
90 /* bitmask of the segments we already have */
91 uint32_t seg_have;
92 /* do we still expect more segments? */
93 unsigned int no_more;
94 /* total length of all segments together */
95 unsigned int tot_len;
Harald Welteebabdea2010-06-01 18:28:10 +020096
Harald Weltece22f922010-06-03 21:21:21 +020097 /* linked list of defrag_queue_entry: one for each fragment */
Harald Welteebabdea2010-06-01 18:28:10 +020098 struct llist_head frag_list;
99
100 struct timer_list timer;
Harald Welte96f71f22010-05-03 19:28:05 +0200101};
102
Harald Welte2720e732010-05-17 00:44:57 +0200103struct sndcp_entity {
Harald Welteebabdea2010-06-01 18:28:10 +0200104 struct llist_head list;
105
Harald Weltebb1c8052010-06-03 06:38:38 +0200106 /* reference to the LLC Entity below this SNDCP entity */
Harald Welteebabdea2010-06-01 18:28:10 +0200107 struct gprs_llc_lle *lle;
Harald Weltebb1c8052010-06-03 06:38:38 +0200108 /* The NSAPI we shall use on top of LLC */
Harald Welteebabdea2010-06-01 18:28:10 +0200109 uint8_t nsapi;
110
Harald Weltebb1c8052010-06-03 06:38:38 +0200111 /* NPDU number for the GTP->SNDCP side */
Harald Weltece22f922010-06-03 21:21:21 +0200112 uint16_t tx_npdu_nr;
Harald Weltebb1c8052010-06-03 06:38:38 +0200113 /* SNDCP eeceiver state */
Harald Welteebabdea2010-06-01 18:28:10 +0200114 enum sndcp_rx_state rx_state;
Harald Weltebb1c8052010-06-03 06:38:38 +0200115 /* The defragmentation queue */
Harald Weltece22f922010-06-03 21:21:21 +0200116 struct defrag_state defrag;
Harald Welte2720e732010-05-17 00:44:57 +0200117};
118
Harald Welteebabdea2010-06-01 18:28:10 +0200119LLIST_HEAD(sndcp_entities);
Harald Welte96f71f22010-05-03 19:28:05 +0200120
Harald Weltece22f922010-06-03 21:21:21 +0200121/* Enqueue a fragment into the defragment queue */
122static int defrag_enqueue(struct sndcp_entity *sne, uint8_t seg_nr,
123 uint32_t data_len, uint8_t *data)
Harald Welteebabdea2010-06-01 18:28:10 +0200124{
Harald Weltece22f922010-06-03 21:21:21 +0200125 struct defrag_queue_entry *dqe;
Harald Welteebabdea2010-06-01 18:28:10 +0200126
Harald Weltece22f922010-06-03 21:21:21 +0200127 dqe = talloc_zero(tall_sndcp_ctx, struct defrag_queue_entry);
128 if (!dqe)
129 return -ENOMEM;
130 dqe->data = talloc_zero_size(dqe, data_len);
131 if (!dqe->data) {
132 talloc_free(dqe);
133 return -ENOMEM;
134 }
135 dqe->seg_nr = seg_nr;
136 dqe->data_len = data_len;
137
138 llist_add(&dqe->list, &sne->defrag.frag_list);
139
140 if (seg_nr > sne->defrag.highest_seg)
141 sne->defrag.highest_seg = seg_nr;
142
143 sne->defrag.seg_have |= (1 << seg_nr);
144 sne->defrag.tot_len += data_len;
145
146 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200147}
148
Harald Weltece22f922010-06-03 21:21:21 +0200149/* return if we have all segments of this N-PDU */
150static int defrag_have_all_segments(struct sndcp_entity *sne)
Harald Welteebabdea2010-06-01 18:28:10 +0200151{
Harald Weltece22f922010-06-03 21:21:21 +0200152 uint32_t seg_needed = 0;
153 unsigned int i;
Harald Welteebabdea2010-06-01 18:28:10 +0200154
Harald Weltece22f922010-06-03 21:21:21 +0200155 /* create a bitmask of needed segments */
156 for (i = 0; i < sne->defrag.highest_seg; i++)
157 seg_needed |= (1 << i);
158
159 if (seg_needed == sne->defrag.seg_have)
160 return 1;
161
162 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200163}
164
Harald Weltece22f922010-06-03 21:21:21 +0200165static struct defrag_queue_entry *defrag_get_seg(struct sndcp_entity *sne,
166 uint32_t seg_nr)
Harald Welteebabdea2010-06-01 18:28:10 +0200167{
Harald Weltece22f922010-06-03 21:21:21 +0200168 struct defrag_queue_entry *dqe;
169
170 llist_for_each_entry(dqe, &sne->defrag.frag_list, list) {
171 if (dqe->seg_nr == seg_nr) {
172 llist_del(&dqe->list);
173 return dqe;
174 }
175 }
176 return NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200177}
Harald Weltece22f922010-06-03 21:21:21 +0200178
179static int defrag_segments(struct sndcp_entity *sne)
180{
181 struct msgb *msg;
182 unsigned int seg_nr;
183 uint8_t *npdu;
184
185 msg = msgb_alloc_headroom(sne->defrag.tot_len+128, 128, "SNDCP Defrag");
186 if (!msg)
187 return -ENOMEM;
188
189 /* FIXME: message headers + identifiers */
190
191 npdu = msg->data;
192
193 for (seg_nr = 0; seg_nr < sne->defrag.highest_seg; seg_nr++) {
194 struct defrag_queue_entry *dqe;
195 uint8_t *data;
196
197 dqe = defrag_get_seg(sne, seg_nr);
198 if (!dqe) {
199 LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr);
200 talloc_free(msg);
201 return -EIO;
202 }
203 /* actually append the segment to the N-PDU */
204 data = msgb_put(msg, dqe->data_len);
205 memcpy(data, dqe->data, dqe->data_len);
206
207 /* release memory for the fragment queue entry */
208 talloc_free(dqe);
209 }
210
211 /* actually send the N-PDU to the SGSN core code, which then
212 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
213 return sgsn_rx_sndcp_ud_ind(sne->lle->llme->tlli, sne->nsapi, msg,
214 sne->defrag.tot_len, npdu);
215}
216
217static int defrag_input(struct sndcp_entity *sne, struct msgb *msg, uint8_t *hdr)
218{
219 struct sndcp_common_hdr *sch;
220 struct sndcp_comp_hdr *scomph = NULL;
221 struct sndcp_udata_hdr *suh;
222 uint16_t npdu_num;
223 uint8_t *data;
224 int rc;
225
226 sch = (struct sndcp_common_hdr *) hdr;
227 if (sch->first) {
228 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
229 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
230 } else
231 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
232
233 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
234
235 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
236
237 if (sch->first) {
238 /* first segment of a new packet. Discard all leftover fragments of
239 * previous packet */
240 if (!llist_empty(&sne->defrag.frag_list)) {
241 struct defrag_queue_entry *dqe;
242 LOGP(DSNDCP, LOGL_INFO, "Dropping SN-PDU due to "
243 "insufficient segments\n");
244 llist_for_each_entry(dqe, &sne->defrag.frag_list, list) {
245 llist_del(&dqe->list);
246 talloc_free(dqe);
247 }
248 }
249 /* store the currently de-fragmented PDU number */
250 sne->defrag.npdu = npdu_num;
251 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
252 /* FIXME: Start timer */
253 }
254
255 if (sne->defrag.npdu != npdu_num) {
256 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
257 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
258 /* FIXME */
259 }
260
261 /* FIXME: check if seg_nr already exists */
262 rc = defrag_enqueue(sne, suh->seg_nr, (msg->data + msg->len) - data, data);
263 if (rc < 0)
264 return rc;
265
266 if (!sch->more) {
267 /* this is suppsed to be the last segment of the N-PDU, but it
268 * might well be not the last to arrive */
269 sne->defrag.no_more = 1;
270 }
271
272 if (sne->defrag.no_more) {
273 /* we have already received the last segment before, let's check
274 * if all the previous segments exist */
275 if (defrag_have_all_segments(sne))
276 return defrag_segments(sne);
277 }
278
279 return 0;
280}
Harald Welteebabdea2010-06-01 18:28:10 +0200281
282static struct sndcp_entity *sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
283 uint8_t nsapi)
284{
285 struct sndcp_entity *sne;
286
287 llist_for_each_entry(sne, &sndcp_entities, list) {
288 if (sne->lle == lle && sne->nsapi == nsapi)
289 return sne;
290 }
291 return NULL;
292}
293
294static struct sndcp_entity *sndcp_entity_alloc(struct gprs_llc_lle *lle,
295 uint8_t nsapi)
296{
297 struct sndcp_entity *sne;
298
299 sne = talloc_zero(tall_sndcp_ctx, struct sndcp_entity);
300 if (!sne)
301 return NULL;
302
303 sne->lle = lle;
304 sne->nsapi = nsapi;
Harald Weltece22f922010-06-03 21:21:21 +0200305 sne->defrag.timer.data = sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200306 //sne->fqueue.timer.cb = FIXME;
307 sne->rx_state = SNDCP_RX_S_FIRST;
308
Harald Welte61444522010-06-02 12:40:48 +0200309 llist_add(&sne->list, &sndcp_entities);
310
Harald Welteebabdea2010-06-01 18:28:10 +0200311 return sne;
312}
313
314/* Entry point for the SNSM-ACTIVATE.indication */
315int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
316{
Harald Welte61444522010-06-02 12:40:48 +0200317 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
318 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200319
Harald Welte16836a32010-06-02 10:25:40 +0200320 if (sndcp_entity_by_lle(lle, nsapi)) {
321 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
322 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
323 lle->llme->tlli, nsapi);
324 return -EEXIST;
325 }
326
327 if (!sndcp_entity_alloc(lle, nsapi)) {
328 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200329 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200330 }
Harald Welteebabdea2010-06-01 18:28:10 +0200331
332 return 0;
333}
334
Harald Weltece22f922010-06-03 21:21:21 +0200335/* Entry point for the SNSM-DEACTIVATE.indication */
336int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
337{
338 struct sndcp_entity *sne;
339
340 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
341 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
342
343 sne = sndcp_entity_by_lle(lle, nsapi);
344 if (!sne) {
345 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
346 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
347 lle->sapi, nsapi);
348 return -ENOENT;
349 }
350 llist_del(&sne->list);
351 /* frag queue entries are hierarchically allocated, so no need to
352 * free them explicitly here */
353 talloc_free(sne);
354
355 return 0;
356}
357
358/* Fragmenter state */
359struct sndcp_frag_state {
360 uint8_t frag_nr;
361 struct msgb *msg; /* original message */
362 uint8_t *next_byte; /* first byte of next fragment */
363
364 struct sndcp_entity *sne;
365 void *mmcontext;
366};
367
368/* returns '1' if there are more fragments to send, '0' if none */
369static int sndcp_send_ud_frag(struct sndcp_frag_state *fs)
370{
371 struct sndcp_entity *sne = fs->sne;
372 struct gprs_llc_lle *lle = sne->lle;
373 struct sndcp_common_hdr *sch;
374 struct sndcp_comp_hdr *scomph;
375 struct sndcp_udata_hdr *suh;
376 struct msgb *fmsg;
377 unsigned int max_payload_len;
378 unsigned int len;
379 uint8_t *data;
380 int rc, more;
381
382 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+128, 128,
383 "SNDCP Frag");
384 if (!fmsg)
385 return -ENOMEM;
386
387 /* make sure lower layers route the fragment like the original */
388 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
389 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
390 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
391
392 /* prepend common SNDCP header */
393 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
394 sch->nsapi = sne->nsapi;
395 /* Set FIRST bit if we are the first fragment in a series */
396 if (fs->frag_nr == 0)
397 sch->first = 1;
398 sch->type = 1;
399
400 /* append the compression header for first fragment */
401 if (sch->first) {
402 scomph = (struct sndcp_comp_hdr *)
403 msgb_put(fmsg, sizeof(*scomph));
404 scomph->pcomp = 0;
405 scomph->dcomp = 0;
406 }
407
408 /* append the user-data header */
409 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
410 suh->npdu_low = sne->tx_npdu_nr & 0xff;
411 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
412 suh->seg_nr = fs->frag_nr % 0xf;
413
414 /* calculate remaining length to be sent */
415 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
416 /* how much payload can we actually send via LLC? */
417 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
418 if (sch->first)
419 max_payload_len -= sizeof(*scomph);
420 /* check if we're exceeding the max */
421 if (len > max_payload_len)
422 len = max_payload_len;
423
424 /* copy the actual fragment data into our fmsg */
425 data = msgb_put(fmsg, len);
426 memcpy(data, fs->next_byte, len);
427
428 /* Increment fragment number and data pointer to next fragment */
429 fs->frag_nr++;
430 fs->next_byte += len;
431
432 /* determine if we have more fragemnts to send */
433 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
434 more = 0;
435 else
436 more = 1;
437
438 /* set the MORE bit of the SNDCP header accordingly */
439 sch->more = more;
440
441 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext);
442 if (rc < 0) {
443 /* abort in case of error, do not advance frag_nr / next_byte */
444 msgb_free(fmsg);
445 return rc;
446 }
447
448 if (!more) {
449 /* we've sent all fragments */
450 msgb_free(fs->msg);
451 memset(fs, 0, sizeof(*fs));
452 /* increment NPDU number for next frame */
453 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
454 return 0;
455 }
456
457 /* default: more fragments to send */
458 return 1;
459}
460
Harald Weltedb2c39f2010-06-03 07:14:59 +0200461/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Harald Weltebb1c8052010-06-03 06:38:38 +0200462int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
463 void *mmcontext)
464{
465 struct sndcp_entity *sne;
466 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200467 struct sndcp_comp_hdr *scomph;
Harald Weltebb1c8052010-06-03 06:38:38 +0200468 struct sndcp_udata_hdr *suh;
Harald Weltece22f922010-06-03 21:21:21 +0200469 struct sndcp_frag_state fs;
Harald Weltebb1c8052010-06-03 06:38:38 +0200470
471 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
472
473 sne = sndcp_entity_by_lle(lle, nsapi);
474 if (!sne) {
475 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
476 return -EIO;
477 }
478
Harald Weltece22f922010-06-03 21:21:21 +0200479 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
480 if (msg->len > lle->params.n201_u -
481 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
482 /* initialize the fragmenter state */
483 fs.msg = msg;
484 fs.frag_nr = 0;
485 fs.next_byte = msg->data;
486 fs.sne = sne;
487 fs.mmcontext = mmcontext;
488
489 /* call function to generate and send fragments until all
490 * of the N-PDU has been sent */
491 while (1) {
492 int rc = sndcp_send_ud_frag(&fs);
493 if (rc == 0)
494 return 0;
495 if (rc < 0)
496 return rc;
497 }
498 /* not reached */
499 return 0;
500 }
501
502 /* this is the non-fragmenting case where we only build 1 SN-PDU */
503
Harald Weltebb1c8052010-06-03 06:38:38 +0200504 /* prepend the user-data header */
505 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Weltece22f922010-06-03 21:21:21 +0200506 suh->npdu_low = sne->tx_npdu_nr & 0xff;
507 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
508 suh->seg_nr = 0;
509 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
510
511 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
512 scomph->pcomp = 0;
513 scomph->dcomp = 0;
Harald Weltebb1c8052010-06-03 06:38:38 +0200514
515 /* prepend common SNDCP header */
516 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
517 sch->first = 1;
518 sch->type = 1;
519 sch->nsapi = nsapi;
520
521 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext);
522}
523
Harald Welteebabdea2010-06-01 18:28:10 +0200524/* Section 5.1.2.17 LL-UNITDATA.ind */
525int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t *hdr, uint8_t len)
526{
527 struct sndcp_entity *sne;
528 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Weltece22f922010-06-03 21:21:21 +0200529 struct sndcp_comp_hdr *scomph = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200530 struct sndcp_udata_hdr *suh;
Harald Welte16836a32010-06-02 10:25:40 +0200531 uint8_t *npdu;
Harald Welteebabdea2010-06-01 18:28:10 +0200532 uint16_t npdu_num;
533 int npdu_len;
534
Harald Weltece22f922010-06-03 21:21:21 +0200535 sch = (struct sndcp_common_hdr *) hdr;
536 if (sch->first) {
537 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
538 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
539 } else
540 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
541
Harald Welteebabdea2010-06-01 18:28:10 +0200542 if (sch->type == 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200543 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Welte96f71f22010-05-03 19:28:05 +0200544 return -EINVAL;
545 }
546
Harald Welte16836a32010-06-02 10:25:40 +0200547 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200548 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welteebabdea2010-06-01 18:28:10 +0200549 return -EIO;
550 }
551
552 sne = sndcp_entity_by_lle(lle, sch->nsapi);
553 if (!sne) {
Harald Welte69996cb2010-06-02 10:26:19 +0200554 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welte61444522010-06-02 12:40:48 +0200555 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
556 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200557 return -EIO;
558 }
559
560 if (!sch->first || sch->more) {
561 /* FIXME: implement fragment re-assembly */
Harald Welte69996cb2010-06-02 10:26:19 +0200562 LOGP(DSNDCP, LOGL_ERROR, "We don't support reassembly yet\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200563 return -EIO;
564 }
565
Harald Weltece22f922010-06-03 21:21:21 +0200566 if (scomph && (scomph->pcomp || scomph->dcomp)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200567 LOGP(DSNDCP, LOGL_ERROR, "We don't support compression yet\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200568 return -EIO;
569 }
Harald Welteebabdea2010-06-01 18:28:10 +0200570
Harald Welte16836a32010-06-02 10:25:40 +0200571 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200572 npdu = (uint8_t *)suh + sizeof(*suh);
573 npdu_len = (msg->data + msg->len) - npdu;
Harald Welte61444522010-06-02 12:40:48 +0200574 if (npdu_len <= 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200575 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200576 return -EIO;
577 }
578 /* actually send the N-PDU to the SGSN core code, which then
579 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
580 return sgsn_rx_sndcp_ud_ind(lle->llme->tlli, sne->nsapi, msg, npdu_len, npdu);
Harald Welte96f71f22010-05-03 19:28:05 +0200581}
582
Harald Welte2720e732010-05-17 00:44:57 +0200583/* Section 5.1.2.1 LL-RESET.ind */
Harald Welteebabdea2010-06-01 18:28:10 +0200584static int sndcp_ll_reset_ind(struct sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200585{
586 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
587 /* reset all SNDCP XID parameters to default values */
588}
589
Harald Welte2720e732010-05-17 00:44:57 +0200590static int sndcp_ll_status_ind()
591{
592 /* inform the SM sub-layer by means of SNSM-STATUS.req */
593}
594
Harald Welteebabdea2010-06-01 18:28:10 +0200595#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200596static struct sndcp_state_list {{
597 uint32_t states;
598 unsigned int type;
599 int (*rout)(struct sndcp_entity *se, struct msgb *msg);
600} sndcp_state_list[] = {
601 { ALL_STATES,
602 LL_RESET_IND, sndcp_ll_reset_ind },
603 { ALL_STATES,
604 LL_ESTABLISH_IND, sndcp_ll_est_ind },
605 { SBIT(SNDCP_S_EST_RQD),
606 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
607 { SBIT(SNDCP_S_EST_RQD),
608 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
609 { SBIT(SNDCP_S_
610};
611
612static int sndcp_rx_llc_prim()
613{
614 case LL_ESTABLISH_REQ:
615 case LL_RELEASE_REQ:
616 case LL_XID_REQ:
617 case LL_DATA_REQ:
618 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
619
620 switch (prim) {
621 case LL_RESET_IND:
622 case LL_ESTABLISH_IND:
623 case LL_ESTABLISH_RESP:
624 case LL_ESTABLISH_CONF:
625 case LL_RELEASE_IND:
626 case LL_RELEASE_CONF:
627 case LL_XID_IND:
628 case LL_XID_RESP:
629 case LL_XID_CONF:
630 case LL_DATA_IND:
631 case LL_DATA_CONF:
632 case LL_UNITDATA_IND:
633 case LL_STATUS_IND:
634}
Harald Welteebabdea2010-06-01 18:28:10 +0200635#endif