blob: 4d7deed9d04d7c5c3b099b333d6a894694581ea9 [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
Harald Weltef78a3b22010-06-30 17:21:19 +020038#include "gprs_sndcp.h"
39
Harald Welte96f71f22010-05-03 19:28:05 +020040/* Chapter 7.2: SN-PDU Formats */
41struct sndcp_common_hdr {
42 /* octet 1 */
43 uint8_t nsapi:4;
44 uint8_t more:1;
45 uint8_t type:1;
46 uint8_t first:1;
47 uint8_t spare:1;
Harald Weltece22f922010-06-03 21:21:21 +020048} __attribute__((packed));
49
50/* PCOMP / DCOMP only exist in first fragment */
51struct sndcp_comp_hdr {
Harald Welte96f71f22010-05-03 19:28:05 +020052 /* octet 2 */
Harald Welte5cc2bc32010-06-02 23:17:05 +020053 uint8_t pcomp:4;
54 uint8_t dcomp:4;
Harald Welteebabdea2010-06-01 18:28:10 +020055} __attribute__((packed));
Harald Welte96f71f22010-05-03 19:28:05 +020056
57struct sndcp_udata_hdr {
58 /* octet 3 */
59 uint8_t npdu_high:4;
60 uint8_t seg_nr:4;
61 /* octet 4 */
62 uint8_t npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +020063} __attribute__((packed));
64
Harald Welteebabdea2010-06-01 18:28:10 +020065
66static void *tall_sndcp_ctx;
67
68/* A fragment queue entry, containing one framgent of a N-PDU */
Harald Weltece22f922010-06-03 21:21:21 +020069struct defrag_queue_entry {
Harald Welteebabdea2010-06-01 18:28:10 +020070 struct llist_head list;
Harald Weltece22f922010-06-03 21:21:21 +020071 /* segment number of this fragment */
72 uint32_t seg_nr;
73 /* length of the data area of this fragment */
Harald Welteebabdea2010-06-01 18:28:10 +020074 uint32_t data_len;
Harald Weltece22f922010-06-03 21:21:21 +020075 /* pointer to the data of this fragment */
76 uint8_t *data;
Harald Welteebabdea2010-06-01 18:28:10 +020077};
78
Harald Weltef78a3b22010-06-30 17:21:19 +020079LLIST_HEAD(gprs_sndcp_entities);
Harald Welte96f71f22010-05-03 19:28:05 +020080
Harald Weltece22f922010-06-03 21:21:21 +020081/* Enqueue a fragment into the defragment queue */
Harald Weltef78a3b22010-06-30 17:21:19 +020082static int defrag_enqueue(struct gprs_sndcp_entity *sne, uint8_t seg_nr,
Harald Weltece22f922010-06-03 21:21:21 +020083 uint32_t data_len, uint8_t *data)
Harald Welteebabdea2010-06-01 18:28:10 +020084{
Harald Weltece22f922010-06-03 21:21:21 +020085 struct defrag_queue_entry *dqe;
Harald Welteebabdea2010-06-01 18:28:10 +020086
Harald Weltece22f922010-06-03 21:21:21 +020087 dqe = talloc_zero(tall_sndcp_ctx, struct defrag_queue_entry);
88 if (!dqe)
89 return -ENOMEM;
90 dqe->data = talloc_zero_size(dqe, data_len);
91 if (!dqe->data) {
92 talloc_free(dqe);
93 return -ENOMEM;
94 }
95 dqe->seg_nr = seg_nr;
96 dqe->data_len = data_len;
97
98 llist_add(&dqe->list, &sne->defrag.frag_list);
99
100 if (seg_nr > sne->defrag.highest_seg)
101 sne->defrag.highest_seg = seg_nr;
102
103 sne->defrag.seg_have |= (1 << seg_nr);
104 sne->defrag.tot_len += data_len;
105
Harald Welte8f0c0a32010-07-02 10:29:06 +0200106 memcpy(dqe->data, data, data_len);
107
Harald Weltece22f922010-06-03 21:21:21 +0200108 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200109}
110
Harald Weltece22f922010-06-03 21:21:21 +0200111/* return if we have all segments of this N-PDU */
Harald Weltef78a3b22010-06-30 17:21:19 +0200112static int defrag_have_all_segments(struct gprs_sndcp_entity *sne)
Harald Welteebabdea2010-06-01 18:28:10 +0200113{
Harald Weltece22f922010-06-03 21:21:21 +0200114 uint32_t seg_needed = 0;
115 unsigned int i;
Harald Welteebabdea2010-06-01 18:28:10 +0200116
Harald Weltece22f922010-06-03 21:21:21 +0200117 /* create a bitmask of needed segments */
Harald Welte951a12c2010-07-01 15:09:45 +0200118 for (i = 0; i <= sne->defrag.highest_seg; i++)
Harald Weltece22f922010-06-03 21:21:21 +0200119 seg_needed |= (1 << i);
120
121 if (seg_needed == sne->defrag.seg_have)
122 return 1;
123
124 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200125}
126
Harald Weltef78a3b22010-06-30 17:21:19 +0200127static struct defrag_queue_entry *defrag_get_seg(struct gprs_sndcp_entity *sne,
Harald Weltece22f922010-06-03 21:21:21 +0200128 uint32_t seg_nr)
Harald Welteebabdea2010-06-01 18:28:10 +0200129{
Harald Weltece22f922010-06-03 21:21:21 +0200130 struct defrag_queue_entry *dqe;
131
132 llist_for_each_entry(dqe, &sne->defrag.frag_list, list) {
133 if (dqe->seg_nr == seg_nr) {
134 llist_del(&dqe->list);
135 return dqe;
136 }
137 }
138 return NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200139}
Harald Weltece22f922010-06-03 21:21:21 +0200140
Harald Weltef78a3b22010-06-30 17:21:19 +0200141static int defrag_segments(struct gprs_sndcp_entity *sne)
Harald Weltece22f922010-06-03 21:21:21 +0200142{
143 struct msgb *msg;
144 unsigned int seg_nr;
145 uint8_t *npdu;
146
Harald Welteab4094c2010-07-02 16:01:47 +0200147 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);
Sylvain Munauteda125c2010-06-09 20:56:52 +0200150 msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag");
Harald Weltece22f922010-06-03 21:21:21 +0200151 if (!msg)
152 return -ENOMEM;
153
154 /* FIXME: message headers + identifiers */
155
156 npdu = msg->data;
157
Harald Welte993697c2010-07-02 10:11:42 +0200158 for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) {
Harald Weltece22f922010-06-03 21:21:21 +0200159 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 /* actually send the N-PDU to the SGSN core code, which then
177 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Harald Welte8911cef2010-07-01 19:56:19 +0200178 return sgsn_rx_sndcp_ud_ind(&sne->ra_id, sne->lle->llme->tlli,
179 sne->nsapi, msg, sne->defrag.tot_len, npdu);
Harald Weltece22f922010-06-03 21:21:21 +0200180}
181
Harald Welte60da7d42010-07-02 15:45:12 +0200182static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg, uint8_t *hdr,
183 unsigned int len)
Harald Weltece22f922010-06-03 21:21:21 +0200184{
185 struct sndcp_common_hdr *sch;
186 struct sndcp_comp_hdr *scomph = NULL;
187 struct sndcp_udata_hdr *suh;
188 uint16_t npdu_num;
189 uint8_t *data;
190 int rc;
191
192 sch = (struct sndcp_common_hdr *) hdr;
193 if (sch->first) {
194 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
195 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
196 } else
197 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
198
199 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
200
201 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
202
Harald Welteab4094c2010-07-02 16:01:47 +0200203 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u "
204 "Length %u %s %s\n", sne->lle->llme->tlli, sne->nsapi, npdu_num,
205 suh->seg_nr, len, sch->first ? "F " : "", sch->more ? "M" : "");
Harald Welteb87bc862010-07-01 20:29:20 +0200206
Harald Weltece22f922010-06-03 21:21:21 +0200207 if (sch->first) {
208 /* first segment of a new packet. Discard all leftover fragments of
209 * previous packet */
210 if (!llist_empty(&sne->defrag.frag_list)) {
Harald Welte65d96782010-07-01 12:19:02 +0200211 struct defrag_queue_entry *dqe, *dqe2;
Harald Welteb87bc862010-07-01 20:29:20 +0200212 LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping "
213 "SN-PDU %u due to insufficient segments (%04x)\n",
214 sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu,
215 sne->defrag.seg_have);
Harald Welte65d96782010-07-01 12:19:02 +0200216 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
Harald Weltece22f922010-06-03 21:21:21 +0200217 llist_del(&dqe->list);
218 talloc_free(dqe);
219 }
220 }
221 /* store the currently de-fragmented PDU number */
222 sne->defrag.npdu = npdu_num;
223 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
224 /* FIXME: Start timer */
225 }
226
227 if (sne->defrag.npdu != npdu_num) {
228 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
229 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
230 /* FIXME */
231 }
232
233 /* FIXME: check if seg_nr already exists */
Harald Welte60da7d42010-07-02 15:45:12 +0200234 rc = defrag_enqueue(sne, suh->seg_nr, len, data);
Harald Weltece22f922010-06-03 21:21:21 +0200235 if (rc < 0)
236 return rc;
237
238 if (!sch->more) {
239 /* this is suppsed to be the last segment of the N-PDU, but it
240 * might well be not the last to arrive */
241 sne->defrag.no_more = 1;
242 }
243
244 if (sne->defrag.no_more) {
245 /* we have already received the last segment before, let's check
246 * if all the previous segments exist */
247 if (defrag_have_all_segments(sne))
248 return defrag_segments(sne);
249 }
250
251 return 0;
252}
Harald Welteebabdea2010-06-01 18:28:10 +0200253
Harald Weltef78a3b22010-06-30 17:21:19 +0200254static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200255 uint8_t nsapi)
256{
Harald Weltef78a3b22010-06-30 17:21:19 +0200257 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200258
Harald Weltef78a3b22010-06-30 17:21:19 +0200259 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
Harald Welteebabdea2010-06-01 18:28:10 +0200260 if (sne->lle == lle && sne->nsapi == nsapi)
261 return sne;
262 }
263 return NULL;
264}
265
Harald Weltef78a3b22010-06-30 17:21:19 +0200266static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200267 uint8_t nsapi)
268{
Harald Weltef78a3b22010-06-30 17:21:19 +0200269 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200270
Harald Weltef78a3b22010-06-30 17:21:19 +0200271 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
Harald Welteebabdea2010-06-01 18:28:10 +0200272 if (!sne)
273 return NULL;
274
275 sne->lle = lle;
276 sne->nsapi = nsapi;
Harald Weltece22f922010-06-03 21:21:21 +0200277 sne->defrag.timer.data = sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200278 //sne->fqueue.timer.cb = FIXME;
279 sne->rx_state = SNDCP_RX_S_FIRST;
Harald Welte362aea02010-07-01 12:31:10 +0200280 INIT_LLIST_HEAD(&sne->defrag.frag_list);
Harald Welteebabdea2010-06-01 18:28:10 +0200281
Harald Weltef78a3b22010-06-30 17:21:19 +0200282 llist_add(&sne->list, &gprs_sndcp_entities);
Harald Welte61444522010-06-02 12:40:48 +0200283
Harald Welteebabdea2010-06-01 18:28:10 +0200284 return sne;
285}
286
287/* Entry point for the SNSM-ACTIVATE.indication */
288int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
289{
Harald Welte61444522010-06-02 12:40:48 +0200290 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
291 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200292
Harald Weltef78a3b22010-06-30 17:21:19 +0200293 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200294 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
295 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
296 lle->llme->tlli, nsapi);
297 return -EEXIST;
298 }
299
Harald Weltef78a3b22010-06-30 17:21:19 +0200300 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200301 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200302 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200303 }
Harald Welteebabdea2010-06-01 18:28:10 +0200304
305 return 0;
306}
307
Harald Weltece22f922010-06-03 21:21:21 +0200308/* Entry point for the SNSM-DEACTIVATE.indication */
309int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
310{
Harald Weltef78a3b22010-06-30 17:21:19 +0200311 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200312
313 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
314 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
315
Harald Weltef78a3b22010-06-30 17:21:19 +0200316 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltece22f922010-06-03 21:21:21 +0200317 if (!sne) {
318 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
319 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
320 lle->sapi, nsapi);
321 return -ENOENT;
322 }
323 llist_del(&sne->list);
324 /* frag queue entries are hierarchically allocated, so no need to
325 * free them explicitly here */
326 talloc_free(sne);
327
328 return 0;
329}
330
331/* Fragmenter state */
332struct sndcp_frag_state {
333 uint8_t frag_nr;
334 struct msgb *msg; /* original message */
335 uint8_t *next_byte; /* first byte of next fragment */
336
Harald Weltef78a3b22010-06-30 17:21:19 +0200337 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200338 void *mmcontext;
339};
340
341/* returns '1' if there are more fragments to send, '0' if none */
342static int sndcp_send_ud_frag(struct sndcp_frag_state *fs)
343{
Harald Weltef78a3b22010-06-30 17:21:19 +0200344 struct gprs_sndcp_entity *sne = fs->sne;
Harald Weltece22f922010-06-03 21:21:21 +0200345 struct gprs_llc_lle *lle = sne->lle;
346 struct sndcp_common_hdr *sch;
347 struct sndcp_comp_hdr *scomph;
348 struct sndcp_udata_hdr *suh;
349 struct msgb *fmsg;
350 unsigned int max_payload_len;
351 unsigned int len;
352 uint8_t *data;
353 int rc, more;
354
Sylvain Munauteda125c2010-06-09 20:56:52 +0200355 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
Harald Weltece22f922010-06-03 21:21:21 +0200356 "SNDCP Frag");
357 if (!fmsg)
358 return -ENOMEM;
359
360 /* make sure lower layers route the fragment like the original */
361 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
362 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
363 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
364
365 /* prepend common SNDCP header */
366 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
367 sch->nsapi = sne->nsapi;
368 /* Set FIRST bit if we are the first fragment in a series */
369 if (fs->frag_nr == 0)
370 sch->first = 1;
371 sch->type = 1;
372
373 /* append the compression header for first fragment */
374 if (sch->first) {
375 scomph = (struct sndcp_comp_hdr *)
376 msgb_put(fmsg, sizeof(*scomph));
377 scomph->pcomp = 0;
378 scomph->dcomp = 0;
379 }
380
381 /* append the user-data header */
382 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
383 suh->npdu_low = sne->tx_npdu_nr & 0xff;
384 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
385 suh->seg_nr = fs->frag_nr % 0xf;
386
387 /* calculate remaining length to be sent */
388 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
389 /* how much payload can we actually send via LLC? */
390 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
391 if (sch->first)
392 max_payload_len -= sizeof(*scomph);
393 /* check if we're exceeding the max */
394 if (len > max_payload_len)
395 len = max_payload_len;
396
397 /* copy the actual fragment data into our fmsg */
398 data = msgb_put(fmsg, len);
399 memcpy(data, fs->next_byte, len);
400
401 /* Increment fragment number and data pointer to next fragment */
402 fs->frag_nr++;
403 fs->next_byte += len;
404
405 /* determine if we have more fragemnts to send */
406 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
407 more = 0;
408 else
409 more = 1;
410
411 /* set the MORE bit of the SNDCP header accordingly */
412 sch->more = more;
413
414 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext);
415 if (rc < 0) {
416 /* abort in case of error, do not advance frag_nr / next_byte */
417 msgb_free(fmsg);
418 return rc;
419 }
420
421 if (!more) {
422 /* we've sent all fragments */
423 msgb_free(fs->msg);
424 memset(fs, 0, sizeof(*fs));
425 /* increment NPDU number for next frame */
426 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
427 return 0;
428 }
429
430 /* default: more fragments to send */
431 return 1;
432}
433
Harald Weltedb2c39f2010-06-03 07:14:59 +0200434/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Harald Weltebb1c8052010-06-03 06:38:38 +0200435int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
436 void *mmcontext)
437{
Harald Weltef78a3b22010-06-30 17:21:19 +0200438 struct gprs_sndcp_entity *sne;
Harald Weltebb1c8052010-06-03 06:38:38 +0200439 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200440 struct sndcp_comp_hdr *scomph;
Harald Weltebb1c8052010-06-03 06:38:38 +0200441 struct sndcp_udata_hdr *suh;
Harald Weltece22f922010-06-03 21:21:21 +0200442 struct sndcp_frag_state fs;
Harald Weltebb1c8052010-06-03 06:38:38 +0200443
444 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
445
Harald Weltef78a3b22010-06-30 17:21:19 +0200446 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltebb1c8052010-06-03 06:38:38 +0200447 if (!sne) {
448 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
449 return -EIO;
450 }
451
Harald Weltece22f922010-06-03 21:21:21 +0200452 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
453 if (msg->len > lle->params.n201_u -
454 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
455 /* initialize the fragmenter state */
456 fs.msg = msg;
457 fs.frag_nr = 0;
458 fs.next_byte = msg->data;
459 fs.sne = sne;
460 fs.mmcontext = mmcontext;
461
462 /* call function to generate and send fragments until all
463 * of the N-PDU has been sent */
464 while (1) {
465 int rc = sndcp_send_ud_frag(&fs);
466 if (rc == 0)
467 return 0;
468 if (rc < 0)
469 return rc;
470 }
471 /* not reached */
472 return 0;
473 }
474
475 /* this is the non-fragmenting case where we only build 1 SN-PDU */
476
Harald Weltebb1c8052010-06-03 06:38:38 +0200477 /* prepend the user-data header */
478 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Weltece22f922010-06-03 21:21:21 +0200479 suh->npdu_low = sne->tx_npdu_nr & 0xff;
480 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
481 suh->seg_nr = 0;
482 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
483
484 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
485 scomph->pcomp = 0;
486 scomph->dcomp = 0;
Harald Weltebb1c8052010-06-03 06:38:38 +0200487
488 /* prepend common SNDCP header */
489 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
490 sch->first = 1;
491 sch->type = 1;
492 sch->nsapi = nsapi;
493
494 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext);
495}
496
Harald Welteebabdea2010-06-01 18:28:10 +0200497/* Section 5.1.2.17 LL-UNITDATA.ind */
498int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t *hdr, uint8_t len)
499{
Harald Weltef78a3b22010-06-30 17:21:19 +0200500 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200501 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Weltece22f922010-06-03 21:21:21 +0200502 struct sndcp_comp_hdr *scomph = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200503 struct sndcp_udata_hdr *suh;
Harald Welte16836a32010-06-02 10:25:40 +0200504 uint8_t *npdu;
Harald Welteebabdea2010-06-01 18:28:10 +0200505 uint16_t npdu_num;
506 int npdu_len;
507
Harald Weltece22f922010-06-03 21:21:21 +0200508 sch = (struct sndcp_common_hdr *) hdr;
509 if (sch->first) {
510 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
511 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
512 } else
513 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
514
Harald Welteebabdea2010-06-01 18:28:10 +0200515 if (sch->type == 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200516 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Welte96f71f22010-05-03 19:28:05 +0200517 return -EINVAL;
518 }
519
Harald Welte16836a32010-06-02 10:25:40 +0200520 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200521 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welteebabdea2010-06-01 18:28:10 +0200522 return -EIO;
523 }
524
Harald Weltef78a3b22010-06-30 17:21:19 +0200525 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200526 if (!sne) {
Harald Welte69996cb2010-06-02 10:26:19 +0200527 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welte61444522010-06-02 12:40:48 +0200528 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
529 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200530 return -EIO;
531 }
Harald Welte8911cef2010-07-01 19:56:19 +0200532 /* FIXME: move this RA_ID up to the LLME or even higher */
533 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
Harald Welteebabdea2010-06-01 18:28:10 +0200534
Harald Welteab4094c2010-07-02 16:01:47 +0200535 /* any non-first segment is by definition something to defragment
536 * as is any segment that tells us there are more segments */
537 if (!sch->first || sch->more)
Harald Welte60da7d42010-07-02 15:45:12 +0200538 return defrag_input(sne, msg, hdr, len);
Harald Welteebabdea2010-06-01 18:28:10 +0200539
Harald Weltece22f922010-06-03 21:21:21 +0200540 if (scomph && (scomph->pcomp || scomph->dcomp)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200541 LOGP(DSNDCP, LOGL_ERROR, "We don't support compression yet\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200542 return -EIO;
543 }
Harald Welteebabdea2010-06-01 18:28:10 +0200544
Harald Welte16836a32010-06-02 10:25:40 +0200545 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200546 npdu = (uint8_t *)suh + sizeof(*suh);
547 npdu_len = (msg->data + msg->len) - npdu;
Harald Welte61444522010-06-02 12:40:48 +0200548 if (npdu_len <= 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200549 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200550 return -EIO;
551 }
552 /* actually send the N-PDU to the SGSN core code, which then
553 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Harald Welte8911cef2010-07-01 19:56:19 +0200554 return sgsn_rx_sndcp_ud_ind(&sne->ra_id, lle->llme->tlli, sne->nsapi, msg, npdu_len, npdu);
Harald Welte96f71f22010-05-03 19:28:05 +0200555}
556
Harald Welte2720e732010-05-17 00:44:57 +0200557/* Section 5.1.2.1 LL-RESET.ind */
Harald Weltef78a3b22010-06-30 17:21:19 +0200558static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200559{
560 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
561 /* reset all SNDCP XID parameters to default values */
562}
563
Harald Welte2720e732010-05-17 00:44:57 +0200564static int sndcp_ll_status_ind()
565{
566 /* inform the SM sub-layer by means of SNSM-STATUS.req */
567}
568
Harald Welteebabdea2010-06-01 18:28:10 +0200569#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200570static struct sndcp_state_list {{
571 uint32_t states;
572 unsigned int type;
Harald Weltef78a3b22010-06-30 17:21:19 +0200573 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
Harald Welte2720e732010-05-17 00:44:57 +0200574} sndcp_state_list[] = {
575 { ALL_STATES,
576 LL_RESET_IND, sndcp_ll_reset_ind },
577 { ALL_STATES,
578 LL_ESTABLISH_IND, sndcp_ll_est_ind },
579 { SBIT(SNDCP_S_EST_RQD),
580 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
581 { SBIT(SNDCP_S_EST_RQD),
582 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
583 { SBIT(SNDCP_S_
584};
585
586static int sndcp_rx_llc_prim()
587{
588 case LL_ESTABLISH_REQ:
589 case LL_RELEASE_REQ:
590 case LL_XID_REQ:
591 case LL_DATA_REQ:
592 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
593
594 switch (prim) {
595 case LL_RESET_IND:
596 case LL_ESTABLISH_IND:
597 case LL_ESTABLISH_RESP:
598 case LL_ESTABLISH_CONF:
599 case LL_RELEASE_IND:
600 case LL_RELEASE_CONF:
601 case LL_XID_IND:
602 case LL_XID_RESP:
603 case LL_XID_CONF:
604 case LL_DATA_IND:
605 case LL_DATA_CONF:
606 case LL_UNITDATA_IND:
607 case LL_STATUS_IND:
608}
Harald Welteebabdea2010-06-01 18:28:10 +0200609#endif