blob: 4f421e4511268a7a9151146a2de72cf416d5f8b3 [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* GPRS SNDCP protocol implementation as per 3GPP TS 04.65 */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by On-Waves
5 *
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 Affero General Public License as published by
10 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <errno.h>
24#include <stdint.h>
25
26#include <osmocore/msgb.h>
27#include <osmocore/linuxlist.h>
28#include <osmocore/timer.h>
29#include <osmocore/talloc.h>
30
31#include <openbsc/gsm_data.h>
32#include <openbsc/debug.h>
33#include <openbsc/gprs_bssgp.h>
34#include <openbsc/gprs_llc.h>
35#include <openbsc/sgsn.h>
36
37#include "gprs_sndcp.h"
38
39/* Chapter 7.2: SN-PDU Formats */
40struct sndcp_common_hdr {
41 /* octet 1 */
42 uint8_t nsapi:4;
43 uint8_t more:1;
44 uint8_t type:1;
45 uint8_t first:1;
46 uint8_t spare:1;
47} __attribute__((packed));
48
49/* PCOMP / DCOMP only exist in first fragment */
50struct sndcp_comp_hdr {
51 /* octet 2 */
52 uint8_t pcomp:4;
53 uint8_t dcomp:4;
54} __attribute__((packed));
55
56struct sndcp_udata_hdr {
57 /* octet 3 */
58 uint8_t npdu_high:4;
59 uint8_t seg_nr:4;
60 /* octet 4 */
61 uint8_t npdu_low;
62} __attribute__((packed));
63
64
65static void *tall_sndcp_ctx;
66
67/* A fragment queue entry, containing one framgent of a N-PDU */
68struct defrag_queue_entry {
69 struct llist_head list;
70 /* segment number of this fragment */
71 uint32_t seg_nr;
72 /* length of the data area of this fragment */
73 uint32_t data_len;
74 /* pointer to the data of this fragment */
75 uint8_t *data;
76};
77
78LLIST_HEAD(gprs_sndcp_entities);
79
80/* Enqueue a fragment into the defragment queue */
81static int defrag_enqueue(struct gprs_sndcp_entity *sne, uint8_t seg_nr,
82 uint8_t *data, uint32_t data_len)
83{
84 struct defrag_queue_entry *dqe;
85
86 dqe = talloc_zero(tall_sndcp_ctx, struct defrag_queue_entry);
87 if (!dqe)
88 return -ENOMEM;
89 dqe->data = talloc_zero_size(dqe, data_len);
90 if (!dqe->data) {
91 talloc_free(dqe);
92 return -ENOMEM;
93 }
94 dqe->seg_nr = seg_nr;
95 dqe->data_len = data_len;
96
97 llist_add(&dqe->list, &sne->defrag.frag_list);
98
99 if (seg_nr > sne->defrag.highest_seg)
100 sne->defrag.highest_seg = seg_nr;
101
102 sne->defrag.seg_have |= (1 << seg_nr);
103 sne->defrag.tot_len += data_len;
104
105 memcpy(dqe->data, data, data_len);
106
107 return 0;
108}
109
110/* return if we have all segments of this N-PDU */
111static int defrag_have_all_segments(struct gprs_sndcp_entity *sne)
112{
113 uint32_t seg_needed = 0;
114 unsigned int i;
115
116 /* create a bitmask of needed segments */
117 for (i = 0; i <= sne->defrag.highest_seg; i++)
118 seg_needed |= (1 << i);
119
120 if (seg_needed == sne->defrag.seg_have)
121 return 1;
122
123 return 0;
124}
125
126static struct defrag_queue_entry *defrag_get_seg(struct gprs_sndcp_entity *sne,
127 uint32_t seg_nr)
128{
129 struct defrag_queue_entry *dqe;
130
131 llist_for_each_entry(dqe, &sne->defrag.frag_list, list) {
132 if (dqe->seg_nr == seg_nr) {
133 llist_del(&dqe->list);
134 return dqe;
135 }
136 }
137 return NULL;
138}
139
140/* Perform actual defragmentation and create an output packet */
141static int defrag_segments(struct gprs_sndcp_entity *sne)
142{
143 struct msgb *msg;
144 unsigned int seg_nr;
145 uint8_t *npdu;
146
147 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Defragment output PDU %u "
148 "num_seg=%u tot_len=%u\n", sne->lle->llme->tlli, sne->nsapi,
149 sne->defrag.npdu, sne->defrag.highest_seg, sne->defrag.tot_len);
150 msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag");
151 if (!msg)
152 return -ENOMEM;
153
154 /* FIXME: message headers + identifiers */
155
156 npdu = msg->data;
157
158 for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) {
159 struct defrag_queue_entry *dqe;
160 uint8_t *data;
161
162 dqe = defrag_get_seg(sne, seg_nr);
163 if (!dqe) {
164 LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr);
165 talloc_free(msg);
166 return -EIO;
167 }
168 /* actually append the segment to the N-PDU */
169 data = msgb_put(msg, dqe->data_len);
170 memcpy(data, dqe->data, dqe->data_len);
171
172 /* release memory for the fragment queue entry */
173 talloc_free(dqe);
174 }
175
176 /* FIXME: cancel timer */
177
178 /* actually send the N-PDU to the SGSN core code, which then
179 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
180 return sgsn_rx_sndcp_ud_ind(&sne->ra_id, sne->lle->llme->tlli,
181 sne->nsapi, msg, sne->defrag.tot_len, npdu);
182}
183
184static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg, uint8_t *hdr,
185 unsigned int len)
186{
187 struct sndcp_common_hdr *sch;
188 struct sndcp_comp_hdr *scomph = NULL;
189 struct sndcp_udata_hdr *suh;
190 uint16_t npdu_num;
191 uint8_t *data;
192 int rc;
193
194 sch = (struct sndcp_common_hdr *) hdr;
195 if (sch->first) {
196 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
197 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
198 } else
199 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
200
201 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
202
203 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
204
205 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u "
206 "Length %u %s %s\n", sne->lle->llme->tlli, sne->nsapi, npdu_num,
207 suh->seg_nr, len, sch->first ? "F " : "", sch->more ? "M" : "");
208
209 if (sch->first) {
210 /* first segment of a new packet. Discard all leftover fragments of
211 * previous packet */
212 if (!llist_empty(&sne->defrag.frag_list)) {
213 struct defrag_queue_entry *dqe, *dqe2;
214 LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping "
215 "SN-PDU %u due to insufficient segments (%04x)\n",
216 sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu,
217 sne->defrag.seg_have);
218 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
219 llist_del(&dqe->list);
220 talloc_free(dqe);
221 }
222 }
223 /* store the currently de-fragmented PDU number */
224 sne->defrag.npdu = npdu_num;
225
226 /* Re-set fragmentation state */
227 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
228 sne->defrag.tot_len = 0;
229 /* FIXME: (re)start timer */
230 }
231
232 if (sne->defrag.npdu != npdu_num) {
233 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
234 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
235 /* FIXME */
236 }
237
238 /* FIXME: check if seg_nr already exists */
239 /* make sure to subtract length of SNDCP header from 'len' */
240 rc = defrag_enqueue(sne, suh->seg_nr, data, len - (data - hdr));
241 if (rc < 0)
242 return rc;
243
244 if (!sch->more) {
245 /* this is suppsed to be the last segment of the N-PDU, but it
246 * might well be not the last to arrive */
247 sne->defrag.no_more = 1;
248 }
249
250 if (sne->defrag.no_more) {
251 /* we have already received the last segment before, let's check
252 * if all the previous segments exist */
253 if (defrag_have_all_segments(sne))
254 return defrag_segments(sne);
255 }
256
257 return 0;
258}
259
260static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
261 uint8_t nsapi)
262{
263 struct gprs_sndcp_entity *sne;
264
265 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
266 if (sne->lle == lle && sne->nsapi == nsapi)
267 return sne;
268 }
269 return NULL;
270}
271
272static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle,
273 uint8_t nsapi)
274{
275 struct gprs_sndcp_entity *sne;
276
277 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
278 if (!sne)
279 return NULL;
280
281 sne->lle = lle;
282 sne->nsapi = nsapi;
283 sne->defrag.timer.data = sne;
284 //sne->fqueue.timer.cb = FIXME;
285 sne->rx_state = SNDCP_RX_S_FIRST;
286 INIT_LLIST_HEAD(&sne->defrag.frag_list);
287
288 llist_add(&sne->list, &gprs_sndcp_entities);
289
290 return sne;
291}
292
293/* Entry point for the SNSM-ACTIVATE.indication */
294int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
295{
296 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
297 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
298
299 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
300 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
301 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
302 lle->llme->tlli, nsapi);
303 return -EEXIST;
304 }
305
306 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
307 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
308 return -ENOMEM;
309 }
310
311 return 0;
312}
313
314/* Entry point for the SNSM-DEACTIVATE.indication */
315int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
316{
317 struct gprs_sndcp_entity *sne;
318
319 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
320 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
321
322 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
323 if (!sne) {
324 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
325 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
326 lle->sapi, nsapi);
327 return -ENOENT;
328 }
329 llist_del(&sne->list);
330 /* frag queue entries are hierarchically allocated, so no need to
331 * free them explicitly here */
332 talloc_free(sne);
333
334 return 0;
335}
336
337/* Fragmenter state */
338struct sndcp_frag_state {
339 uint8_t frag_nr;
340 struct msgb *msg; /* original message */
341 uint8_t *next_byte; /* first byte of next fragment */
342
343 struct gprs_sndcp_entity *sne;
344 void *mmcontext;
345};
346
347/* returns '1' if there are more fragments to send, '0' if none */
348static int sndcp_send_ud_frag(struct sndcp_frag_state *fs)
349{
350 struct gprs_sndcp_entity *sne = fs->sne;
351 struct gprs_llc_lle *lle = sne->lle;
352 struct sndcp_common_hdr *sch;
353 struct sndcp_comp_hdr *scomph;
354 struct sndcp_udata_hdr *suh;
355 struct msgb *fmsg;
356 unsigned int max_payload_len;
357 unsigned int len;
358 uint8_t *data;
359 int rc, more;
360
361 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
362 "SNDCP Frag");
363 if (!fmsg)
364 return -ENOMEM;
365
366 /* make sure lower layers route the fragment like the original */
367 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
368 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
369 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
370
371 /* prepend common SNDCP header */
372 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
373 sch->nsapi = sne->nsapi;
374 /* Set FIRST bit if we are the first fragment in a series */
375 if (fs->frag_nr == 0)
376 sch->first = 1;
377 sch->type = 1;
378
379 /* append the compression header for first fragment */
380 if (sch->first) {
381 scomph = (struct sndcp_comp_hdr *)
382 msgb_put(fmsg, sizeof(*scomph));
383 scomph->pcomp = 0;
384 scomph->dcomp = 0;
385 }
386
387 /* append the user-data header */
388 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
389 suh->npdu_low = sne->tx_npdu_nr & 0xff;
390 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
391 suh->seg_nr = fs->frag_nr % 0xf;
392
393 /* calculate remaining length to be sent */
394 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
395 /* how much payload can we actually send via LLC? */
396 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
397 if (sch->first)
398 max_payload_len -= sizeof(*scomph);
399 /* check if we're exceeding the max */
400 if (len > max_payload_len)
401 len = max_payload_len;
402
403 /* copy the actual fragment data into our fmsg */
404 data = msgb_put(fmsg, len);
405 memcpy(data, fs->next_byte, len);
406
407 /* Increment fragment number and data pointer to next fragment */
408 fs->frag_nr++;
409 fs->next_byte += len;
410
411 /* determine if we have more fragemnts to send */
412 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
413 more = 0;
414 else
415 more = 1;
416
417 /* set the MORE bit of the SNDCP header accordingly */
418 sch->more = more;
419
420 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext);
421 if (rc < 0) {
422 /* abort in case of error, do not advance frag_nr / next_byte */
423 msgb_free(fmsg);
424 return rc;
425 }
426
427 if (!more) {
428 /* we've sent all fragments */
429 msgb_free(fs->msg);
430 memset(fs, 0, sizeof(*fs));
431 /* increment NPDU number for next frame */
432 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
433 return 0;
434 }
435
436 /* default: more fragments to send */
437 return 1;
438}
439
440/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
441int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
442 void *mmcontext)
443{
444 struct gprs_sndcp_entity *sne;
445 struct sndcp_common_hdr *sch;
446 struct sndcp_comp_hdr *scomph;
447 struct sndcp_udata_hdr *suh;
448 struct sndcp_frag_state fs;
449
450 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
451
452 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
453 if (!sne) {
454 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
455 return -EIO;
456 }
457
458 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
459 if (msg->len > lle->params.n201_u -
460 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
461 /* initialize the fragmenter state */
462 fs.msg = msg;
463 fs.frag_nr = 0;
464 fs.next_byte = msg->data;
465 fs.sne = sne;
466 fs.mmcontext = mmcontext;
467
468 /* call function to generate and send fragments until all
469 * of the N-PDU has been sent */
470 while (1) {
471 int rc = sndcp_send_ud_frag(&fs);
472 if (rc == 0)
473 return 0;
474 if (rc < 0)
475 return rc;
476 }
477 /* not reached */
478 return 0;
479 }
480
481 /* this is the non-fragmenting case where we only build 1 SN-PDU */
482
483 /* prepend the user-data header */
484 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
485 suh->npdu_low = sne->tx_npdu_nr & 0xff;
486 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
487 suh->seg_nr = 0;
488 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
489
490 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
491 scomph->pcomp = 0;
492 scomph->dcomp = 0;
493
494 /* prepend common SNDCP header */
495 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
496 sch->first = 1;
497 sch->type = 1;
498 sch->nsapi = nsapi;
499
500 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext);
501}
502
503/* Section 5.1.2.17 LL-UNITDATA.ind */
504int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle,
505 uint8_t *hdr, uint16_t len)
506{
507 struct gprs_sndcp_entity *sne;
508 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
509 struct sndcp_comp_hdr *scomph = NULL;
510 struct sndcp_udata_hdr *suh;
511 uint8_t *npdu;
512 uint16_t npdu_num;
513 int npdu_len;
514
515 sch = (struct sndcp_common_hdr *) hdr;
516 if (sch->first) {
517 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
518 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
519 } else
520 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
521
522 if (sch->type == 0) {
523 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
524 return -EINVAL;
525 }
526
527 if (len < sizeof(*sch) + sizeof(*suh)) {
528 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
529 return -EIO;
530 }
531
532 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
533 if (!sne) {
534 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
535 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
536 lle->llme->tlli, lle->sapi, sch->nsapi);
537 return -EIO;
538 }
539 /* FIXME: move this RA_ID up to the LLME or even higher */
540 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
541
542 /* any non-first segment is by definition something to defragment
543 * as is any segment that tells us there are more segments */
544 if (!sch->first || sch->more)
545 return defrag_input(sne, msg, hdr, len);
546
547 if (scomph && (scomph->pcomp || scomph->dcomp)) {
548 LOGP(DSNDCP, LOGL_ERROR, "We don't support compression yet\n");
549 return -EIO;
550 }
551
552 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
553 npdu = (uint8_t *)suh + sizeof(*suh);
554 npdu_len = (msg->data + msg->len) - npdu;
555 if (npdu_len <= 0) {
556 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
557 return -EIO;
558 }
559 /* actually send the N-PDU to the SGSN core code, which then
560 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
561 return sgsn_rx_sndcp_ud_ind(&sne->ra_id, lle->llme->tlli, sne->nsapi, msg, npdu_len, npdu);
562}
563
564/* Section 5.1.2.1 LL-RESET.ind */
565static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
566{
567 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
568 /* reset all SNDCP XID parameters to default values */
569}
570
571static int sndcp_ll_status_ind()
572{
573 /* inform the SM sub-layer by means of SNSM-STATUS.req */
574}
575
576#if 0
577static struct sndcp_state_list {{
578 uint32_t states;
579 unsigned int type;
580 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
581} sndcp_state_list[] = {
582 { ALL_STATES,
583 LL_RESET_IND, sndcp_ll_reset_ind },
584 { ALL_STATES,
585 LL_ESTABLISH_IND, sndcp_ll_est_ind },
586 { SBIT(SNDCP_S_EST_RQD),
587 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
588 { SBIT(SNDCP_S_EST_RQD),
589 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
590 { SBIT(SNDCP_S_
591};
592
593static int sndcp_rx_llc_prim()
594{
595 case LL_ESTABLISH_REQ:
596 case LL_RELEASE_REQ:
597 case LL_XID_REQ:
598 case LL_DATA_REQ:
599 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
600
601 switch (prim) {
602 case LL_RESET_IND:
603 case LL_ESTABLISH_IND:
604 case LL_ESTABLISH_RESP:
605 case LL_ESTABLISH_CONF:
606 case LL_RELEASE_IND:
607 case LL_RELEASE_CONF:
608 case LL_XID_IND:
609 case LL_XID_RESP:
610 case LL_XID_CONF:
611 case LL_DATA_IND:
612 case LL_DATA_CONF:
613 case LL_UNITDATA_IND:
614 case LL_STATUS_IND:
615}
616#endif