blob: 9c13fbde0128cda3f40ee5462b452cfbce64b3fe [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
Sylvain Munauteda125c2010-06-09 20:56:52 +0200147 msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag");
Harald Weltece22f922010-06-03 21:21:21 +0200148 if (!msg)
149 return -ENOMEM;
150
151 /* FIXME: message headers + identifiers */
152
153 npdu = msg->data;
154
Harald Welte993697c2010-07-02 10:11:42 +0200155 for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) {
Harald Weltece22f922010-06-03 21:21:21 +0200156 struct defrag_queue_entry *dqe;
157 uint8_t *data;
158
159 dqe = defrag_get_seg(sne, seg_nr);
160 if (!dqe) {
161 LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr);
162 talloc_free(msg);
163 return -EIO;
164 }
165 /* actually append the segment to the N-PDU */
166 data = msgb_put(msg, dqe->data_len);
167 memcpy(data, dqe->data, dqe->data_len);
168
169 /* release memory for the fragment queue entry */
170 talloc_free(dqe);
171 }
172
173 /* actually send the N-PDU to the SGSN core code, which then
174 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Harald Welte8911cef2010-07-01 19:56:19 +0200175 return sgsn_rx_sndcp_ud_ind(&sne->ra_id, sne->lle->llme->tlli,
176 sne->nsapi, msg, sne->defrag.tot_len, npdu);
Harald Weltece22f922010-06-03 21:21:21 +0200177}
178
Harald Welte60da7d42010-07-02 15:45:12 +0200179static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg, uint8_t *hdr,
180 unsigned int len)
Harald Weltece22f922010-06-03 21:21:21 +0200181{
182 struct sndcp_common_hdr *sch;
183 struct sndcp_comp_hdr *scomph = NULL;
184 struct sndcp_udata_hdr *suh;
185 uint16_t npdu_num;
186 uint8_t *data;
187 int rc;
188
189 sch = (struct sndcp_common_hdr *) hdr;
190 if (sch->first) {
191 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
192 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
193 } else
194 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
195
196 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
197
198 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
199
Harald Welteb87bc862010-07-01 20:29:20 +0200200 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u %s%s\n",
201 sne->lle->llme->tlli, sne->nsapi, npdu_num, suh->seg_nr,
202 sch->first ? "F " : "", sch->more ? "M" : "");
203
Harald Weltece22f922010-06-03 21:21:21 +0200204 if (sch->first) {
205 /* first segment of a new packet. Discard all leftover fragments of
206 * previous packet */
207 if (!llist_empty(&sne->defrag.frag_list)) {
Harald Welte65d96782010-07-01 12:19:02 +0200208 struct defrag_queue_entry *dqe, *dqe2;
Harald Welteb87bc862010-07-01 20:29:20 +0200209 LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping "
210 "SN-PDU %u due to insufficient segments (%04x)\n",
211 sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu,
212 sne->defrag.seg_have);
Harald Welte65d96782010-07-01 12:19:02 +0200213 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
Harald Weltece22f922010-06-03 21:21:21 +0200214 llist_del(&dqe->list);
215 talloc_free(dqe);
216 }
217 }
218 /* store the currently de-fragmented PDU number */
219 sne->defrag.npdu = npdu_num;
220 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
221 /* FIXME: Start timer */
222 }
223
224 if (sne->defrag.npdu != npdu_num) {
225 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
226 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
227 /* FIXME */
228 }
229
230 /* FIXME: check if seg_nr already exists */
Harald Welte60da7d42010-07-02 15:45:12 +0200231 rc = defrag_enqueue(sne, suh->seg_nr, len, data);
Harald Weltece22f922010-06-03 21:21:21 +0200232 if (rc < 0)
233 return rc;
234
235 if (!sch->more) {
236 /* this is suppsed to be the last segment of the N-PDU, but it
237 * might well be not the last to arrive */
238 sne->defrag.no_more = 1;
239 }
240
241 if (sne->defrag.no_more) {
242 /* we have already received the last segment before, let's check
243 * if all the previous segments exist */
244 if (defrag_have_all_segments(sne))
245 return defrag_segments(sne);
246 }
247
248 return 0;
249}
Harald Welteebabdea2010-06-01 18:28:10 +0200250
Harald Weltef78a3b22010-06-30 17:21:19 +0200251static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200252 uint8_t nsapi)
253{
Harald Weltef78a3b22010-06-30 17:21:19 +0200254 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200255
Harald Weltef78a3b22010-06-30 17:21:19 +0200256 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
Harald Welteebabdea2010-06-01 18:28:10 +0200257 if (sne->lle == lle && sne->nsapi == nsapi)
258 return sne;
259 }
260 return NULL;
261}
262
Harald Weltef78a3b22010-06-30 17:21:19 +0200263static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200264 uint8_t nsapi)
265{
Harald Weltef78a3b22010-06-30 17:21:19 +0200266 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200267
Harald Weltef78a3b22010-06-30 17:21:19 +0200268 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
Harald Welteebabdea2010-06-01 18:28:10 +0200269 if (!sne)
270 return NULL;
271
272 sne->lle = lle;
273 sne->nsapi = nsapi;
Harald Weltece22f922010-06-03 21:21:21 +0200274 sne->defrag.timer.data = sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200275 //sne->fqueue.timer.cb = FIXME;
276 sne->rx_state = SNDCP_RX_S_FIRST;
Harald Welte362aea02010-07-01 12:31:10 +0200277 INIT_LLIST_HEAD(&sne->defrag.frag_list);
Harald Welteebabdea2010-06-01 18:28:10 +0200278
Harald Weltef78a3b22010-06-30 17:21:19 +0200279 llist_add(&sne->list, &gprs_sndcp_entities);
Harald Welte61444522010-06-02 12:40:48 +0200280
Harald Welteebabdea2010-06-01 18:28:10 +0200281 return sne;
282}
283
284/* Entry point for the SNSM-ACTIVATE.indication */
285int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
286{
Harald Welte61444522010-06-02 12:40:48 +0200287 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
288 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200289
Harald Weltef78a3b22010-06-30 17:21:19 +0200290 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200291 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
292 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
293 lle->llme->tlli, nsapi);
294 return -EEXIST;
295 }
296
Harald Weltef78a3b22010-06-30 17:21:19 +0200297 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200298 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200299 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200300 }
Harald Welteebabdea2010-06-01 18:28:10 +0200301
302 return 0;
303}
304
Harald Weltece22f922010-06-03 21:21:21 +0200305/* Entry point for the SNSM-DEACTIVATE.indication */
306int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
307{
Harald Weltef78a3b22010-06-30 17:21:19 +0200308 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200309
310 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
311 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
312
Harald Weltef78a3b22010-06-30 17:21:19 +0200313 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltece22f922010-06-03 21:21:21 +0200314 if (!sne) {
315 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
316 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
317 lle->sapi, nsapi);
318 return -ENOENT;
319 }
320 llist_del(&sne->list);
321 /* frag queue entries are hierarchically allocated, so no need to
322 * free them explicitly here */
323 talloc_free(sne);
324
325 return 0;
326}
327
328/* Fragmenter state */
329struct sndcp_frag_state {
330 uint8_t frag_nr;
331 struct msgb *msg; /* original message */
332 uint8_t *next_byte; /* first byte of next fragment */
333
Harald Weltef78a3b22010-06-30 17:21:19 +0200334 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200335 void *mmcontext;
336};
337
338/* returns '1' if there are more fragments to send, '0' if none */
339static int sndcp_send_ud_frag(struct sndcp_frag_state *fs)
340{
Harald Weltef78a3b22010-06-30 17:21:19 +0200341 struct gprs_sndcp_entity *sne = fs->sne;
Harald Weltece22f922010-06-03 21:21:21 +0200342 struct gprs_llc_lle *lle = sne->lle;
343 struct sndcp_common_hdr *sch;
344 struct sndcp_comp_hdr *scomph;
345 struct sndcp_udata_hdr *suh;
346 struct msgb *fmsg;
347 unsigned int max_payload_len;
348 unsigned int len;
349 uint8_t *data;
350 int rc, more;
351
Sylvain Munauteda125c2010-06-09 20:56:52 +0200352 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
Harald Weltece22f922010-06-03 21:21:21 +0200353 "SNDCP Frag");
354 if (!fmsg)
355 return -ENOMEM;
356
357 /* make sure lower layers route the fragment like the original */
358 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
359 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
360 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
361
362 /* prepend common SNDCP header */
363 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
364 sch->nsapi = sne->nsapi;
365 /* Set FIRST bit if we are the first fragment in a series */
366 if (fs->frag_nr == 0)
367 sch->first = 1;
368 sch->type = 1;
369
370 /* append the compression header for first fragment */
371 if (sch->first) {
372 scomph = (struct sndcp_comp_hdr *)
373 msgb_put(fmsg, sizeof(*scomph));
374 scomph->pcomp = 0;
375 scomph->dcomp = 0;
376 }
377
378 /* append the user-data header */
379 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
380 suh->npdu_low = sne->tx_npdu_nr & 0xff;
381 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
382 suh->seg_nr = fs->frag_nr % 0xf;
383
384 /* calculate remaining length to be sent */
385 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
386 /* how much payload can we actually send via LLC? */
387 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
388 if (sch->first)
389 max_payload_len -= sizeof(*scomph);
390 /* check if we're exceeding the max */
391 if (len > max_payload_len)
392 len = max_payload_len;
393
394 /* copy the actual fragment data into our fmsg */
395 data = msgb_put(fmsg, len);
396 memcpy(data, fs->next_byte, len);
397
398 /* Increment fragment number and data pointer to next fragment */
399 fs->frag_nr++;
400 fs->next_byte += len;
401
402 /* determine if we have more fragemnts to send */
403 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
404 more = 0;
405 else
406 more = 1;
407
408 /* set the MORE bit of the SNDCP header accordingly */
409 sch->more = more;
410
411 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext);
412 if (rc < 0) {
413 /* abort in case of error, do not advance frag_nr / next_byte */
414 msgb_free(fmsg);
415 return rc;
416 }
417
418 if (!more) {
419 /* we've sent all fragments */
420 msgb_free(fs->msg);
421 memset(fs, 0, sizeof(*fs));
422 /* increment NPDU number for next frame */
423 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
424 return 0;
425 }
426
427 /* default: more fragments to send */
428 return 1;
429}
430
Harald Weltedb2c39f2010-06-03 07:14:59 +0200431/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Harald Weltebb1c8052010-06-03 06:38:38 +0200432int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
433 void *mmcontext)
434{
Harald Weltef78a3b22010-06-30 17:21:19 +0200435 struct gprs_sndcp_entity *sne;
Harald Weltebb1c8052010-06-03 06:38:38 +0200436 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200437 struct sndcp_comp_hdr *scomph;
Harald Weltebb1c8052010-06-03 06:38:38 +0200438 struct sndcp_udata_hdr *suh;
Harald Weltece22f922010-06-03 21:21:21 +0200439 struct sndcp_frag_state fs;
Harald Weltebb1c8052010-06-03 06:38:38 +0200440
441 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
442
Harald Weltef78a3b22010-06-30 17:21:19 +0200443 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltebb1c8052010-06-03 06:38:38 +0200444 if (!sne) {
445 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
446 return -EIO;
447 }
448
Harald Weltece22f922010-06-03 21:21:21 +0200449 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
450 if (msg->len > lle->params.n201_u -
451 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
452 /* initialize the fragmenter state */
453 fs.msg = msg;
454 fs.frag_nr = 0;
455 fs.next_byte = msg->data;
456 fs.sne = sne;
457 fs.mmcontext = mmcontext;
458
459 /* call function to generate and send fragments until all
460 * of the N-PDU has been sent */
461 while (1) {
462 int rc = sndcp_send_ud_frag(&fs);
463 if (rc == 0)
464 return 0;
465 if (rc < 0)
466 return rc;
467 }
468 /* not reached */
469 return 0;
470 }
471
472 /* this is the non-fragmenting case where we only build 1 SN-PDU */
473
Harald Weltebb1c8052010-06-03 06:38:38 +0200474 /* prepend the user-data header */
475 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Weltece22f922010-06-03 21:21:21 +0200476 suh->npdu_low = sne->tx_npdu_nr & 0xff;
477 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
478 suh->seg_nr = 0;
479 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
480
481 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
482 scomph->pcomp = 0;
483 scomph->dcomp = 0;
Harald Weltebb1c8052010-06-03 06:38:38 +0200484
485 /* prepend common SNDCP header */
486 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
487 sch->first = 1;
488 sch->type = 1;
489 sch->nsapi = nsapi;
490
491 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext);
492}
493
Harald Welteebabdea2010-06-01 18:28:10 +0200494/* Section 5.1.2.17 LL-UNITDATA.ind */
495int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t *hdr, uint8_t len)
496{
Harald Weltef78a3b22010-06-30 17:21:19 +0200497 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200498 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Weltece22f922010-06-03 21:21:21 +0200499 struct sndcp_comp_hdr *scomph = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200500 struct sndcp_udata_hdr *suh;
Harald Welte16836a32010-06-02 10:25:40 +0200501 uint8_t *npdu;
Harald Welteebabdea2010-06-01 18:28:10 +0200502 uint16_t npdu_num;
503 int npdu_len;
504
Harald Weltece22f922010-06-03 21:21:21 +0200505 sch = (struct sndcp_common_hdr *) hdr;
506 if (sch->first) {
507 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
508 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
509 } else
510 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
511
Harald Welteebabdea2010-06-01 18:28:10 +0200512 if (sch->type == 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200513 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Welte96f71f22010-05-03 19:28:05 +0200514 return -EINVAL;
515 }
516
Harald Welte16836a32010-06-02 10:25:40 +0200517 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200518 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welteebabdea2010-06-01 18:28:10 +0200519 return -EIO;
520 }
521
Harald Weltef78a3b22010-06-30 17:21:19 +0200522 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200523 if (!sne) {
Harald Welte69996cb2010-06-02 10:26:19 +0200524 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welte61444522010-06-02 12:40:48 +0200525 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
526 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200527 return -EIO;
528 }
Harald Welte8911cef2010-07-01 19:56:19 +0200529 /* FIXME: move this RA_ID up to the LLME or even higher */
530 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
Harald Welteebabdea2010-06-01 18:28:10 +0200531
532 if (!sch->first || sch->more) {
Harald Weltef78a3b22010-06-30 17:21:19 +0200533#if 0
Harald Welteebabdea2010-06-01 18:28:10 +0200534 /* FIXME: implement fragment re-assembly */
Harald Welte69996cb2010-06-02 10:26:19 +0200535 LOGP(DSNDCP, LOGL_ERROR, "We don't support reassembly yet\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200536 return -EIO;
Harald Weltef78a3b22010-06-30 17:21:19 +0200537#else
Harald Welte60da7d42010-07-02 15:45:12 +0200538 return defrag_input(sne, msg, hdr, len);
Harald Weltef78a3b22010-06-30 17:21:19 +0200539#endif
Harald Welteebabdea2010-06-01 18:28:10 +0200540 }
541
Harald Weltece22f922010-06-03 21:21:21 +0200542 if (scomph && (scomph->pcomp || scomph->dcomp)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200543 LOGP(DSNDCP, LOGL_ERROR, "We don't support compression yet\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200544 return -EIO;
545 }
Harald Welteebabdea2010-06-01 18:28:10 +0200546
Harald Welte16836a32010-06-02 10:25:40 +0200547 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200548 npdu = (uint8_t *)suh + sizeof(*suh);
549 npdu_len = (msg->data + msg->len) - npdu;
Harald Welte61444522010-06-02 12:40:48 +0200550 if (npdu_len <= 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200551 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200552 return -EIO;
553 }
554 /* actually send the N-PDU to the SGSN core code, which then
555 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Harald Welte8911cef2010-07-01 19:56:19 +0200556 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 +0200557}
558
Harald Welte2720e732010-05-17 00:44:57 +0200559/* Section 5.1.2.1 LL-RESET.ind */
Harald Weltef78a3b22010-06-30 17:21:19 +0200560static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200561{
562 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
563 /* reset all SNDCP XID parameters to default values */
564}
565
Harald Welte2720e732010-05-17 00:44:57 +0200566static int sndcp_ll_status_ind()
567{
568 /* inform the SM sub-layer by means of SNSM-STATUS.req */
569}
570
Harald Welteebabdea2010-06-01 18:28:10 +0200571#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200572static struct sndcp_state_list {{
573 uint32_t states;
574 unsigned int type;
Harald Weltef78a3b22010-06-30 17:21:19 +0200575 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
Harald Welte2720e732010-05-17 00:44:57 +0200576} sndcp_state_list[] = {
577 { ALL_STATES,
578 LL_RESET_IND, sndcp_ll_reset_ind },
579 { ALL_STATES,
580 LL_ESTABLISH_IND, sndcp_ll_est_ind },
581 { SBIT(SNDCP_S_EST_RQD),
582 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
583 { SBIT(SNDCP_S_EST_RQD),
584 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
585 { SBIT(SNDCP_S_
586};
587
588static int sndcp_rx_llc_prim()
589{
590 case LL_ESTABLISH_REQ:
591 case LL_RELEASE_REQ:
592 case LL_XID_REQ:
593 case LL_DATA_REQ:
594 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
595
596 switch (prim) {
597 case LL_RESET_IND:
598 case LL_ESTABLISH_IND:
599 case LL_ESTABLISH_RESP:
600 case LL_ESTABLISH_CONF:
601 case LL_RELEASE_IND:
602 case LL_RELEASE_CONF:
603 case LL_XID_IND:
604 case LL_XID_RESP:
605 case LL_XID_CONF:
606 case LL_DATA_IND:
607 case LL_DATA_CONF:
608 case LL_UNITDATA_IND:
609 case LL_STATUS_IND:
610}
Harald Welteebabdea2010-06-01 18:28:10 +0200611#endif