blob: 46753c76cf4cad978ec1775990d6ba6c9f5e7e21 [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
106 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200107}
108
Harald Weltece22f922010-06-03 21:21:21 +0200109/* return if we have all segments of this N-PDU */
Harald Weltef78a3b22010-06-30 17:21:19 +0200110static int defrag_have_all_segments(struct gprs_sndcp_entity *sne)
Harald Welteebabdea2010-06-01 18:28:10 +0200111{
Harald Weltece22f922010-06-03 21:21:21 +0200112 uint32_t seg_needed = 0;
113 unsigned int i;
Harald Welteebabdea2010-06-01 18:28:10 +0200114
Harald Weltece22f922010-06-03 21:21:21 +0200115 /* create a bitmask of needed segments */
Harald Welte951a12c2010-07-01 15:09:45 +0200116 for (i = 0; i <= sne->defrag.highest_seg; i++)
Harald Weltece22f922010-06-03 21:21:21 +0200117 seg_needed |= (1 << i);
118
119 if (seg_needed == sne->defrag.seg_have)
120 return 1;
121
122 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200123}
124
Harald Weltef78a3b22010-06-30 17:21:19 +0200125static struct defrag_queue_entry *defrag_get_seg(struct gprs_sndcp_entity *sne,
Harald Weltece22f922010-06-03 21:21:21 +0200126 uint32_t seg_nr)
Harald Welteebabdea2010-06-01 18:28:10 +0200127{
Harald Weltece22f922010-06-03 21:21:21 +0200128 struct defrag_queue_entry *dqe;
129
130 llist_for_each_entry(dqe, &sne->defrag.frag_list, list) {
131 if (dqe->seg_nr == seg_nr) {
132 llist_del(&dqe->list);
133 return dqe;
134 }
135 }
136 return NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200137}
Harald Weltece22f922010-06-03 21:21:21 +0200138
Harald Weltef78a3b22010-06-30 17:21:19 +0200139static int defrag_segments(struct gprs_sndcp_entity *sne)
Harald Weltece22f922010-06-03 21:21:21 +0200140{
141 struct msgb *msg;
142 unsigned int seg_nr;
143 uint8_t *npdu;
144
Sylvain Munauteda125c2010-06-09 20:56:52 +0200145 msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag");
Harald Weltece22f922010-06-03 21:21:21 +0200146 if (!msg)
147 return -ENOMEM;
148
149 /* FIXME: message headers + identifiers */
150
151 npdu = msg->data;
152
Harald Welte993697c2010-07-02 10:11:42 +0200153 for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) {
Harald Weltece22f922010-06-03 21:21:21 +0200154 struct defrag_queue_entry *dqe;
155 uint8_t *data;
156
157 dqe = defrag_get_seg(sne, seg_nr);
158 if (!dqe) {
159 LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr);
160 talloc_free(msg);
161 return -EIO;
162 }
163 /* actually append the segment to the N-PDU */
164 data = msgb_put(msg, dqe->data_len);
165 memcpy(data, dqe->data, dqe->data_len);
166
167 /* release memory for the fragment queue entry */
168 talloc_free(dqe);
169 }
170
171 /* actually send the N-PDU to the SGSN core code, which then
172 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Harald Welte8911cef2010-07-01 19:56:19 +0200173 return sgsn_rx_sndcp_ud_ind(&sne->ra_id, sne->lle->llme->tlli,
174 sne->nsapi, msg, sne->defrag.tot_len, npdu);
Harald Weltece22f922010-06-03 21:21:21 +0200175}
176
Harald Weltef78a3b22010-06-30 17:21:19 +0200177static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg, uint8_t *hdr)
Harald Weltece22f922010-06-03 21:21:21 +0200178{
179 struct sndcp_common_hdr *sch;
180 struct sndcp_comp_hdr *scomph = NULL;
181 struct sndcp_udata_hdr *suh;
182 uint16_t npdu_num;
183 uint8_t *data;
184 int rc;
185
186 sch = (struct sndcp_common_hdr *) hdr;
187 if (sch->first) {
188 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
189 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
190 } else
191 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
192
193 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
194
195 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
196
Harald Welteb87bc862010-07-01 20:29:20 +0200197 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u %s%s\n",
198 sne->lle->llme->tlli, sne->nsapi, npdu_num, suh->seg_nr,
199 sch->first ? "F " : "", sch->more ? "M" : "");
200
Harald Weltece22f922010-06-03 21:21:21 +0200201 if (sch->first) {
202 /* first segment of a new packet. Discard all leftover fragments of
203 * previous packet */
204 if (!llist_empty(&sne->defrag.frag_list)) {
Harald Welte65d96782010-07-01 12:19:02 +0200205 struct defrag_queue_entry *dqe, *dqe2;
Harald Welteb87bc862010-07-01 20:29:20 +0200206 LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping "
207 "SN-PDU %u due to insufficient segments (%04x)\n",
208 sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu,
209 sne->defrag.seg_have);
Harald Welte65d96782010-07-01 12:19:02 +0200210 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
Harald Weltece22f922010-06-03 21:21:21 +0200211 llist_del(&dqe->list);
212 talloc_free(dqe);
213 }
214 }
215 /* store the currently de-fragmented PDU number */
216 sne->defrag.npdu = npdu_num;
217 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
218 /* FIXME: Start timer */
219 }
220
221 if (sne->defrag.npdu != npdu_num) {
222 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
223 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
224 /* FIXME */
225 }
226
227 /* FIXME: check if seg_nr already exists */
228 rc = defrag_enqueue(sne, suh->seg_nr, (msg->data + msg->len) - data, data);
229 if (rc < 0)
230 return rc;
231
232 if (!sch->more) {
233 /* this is suppsed to be the last segment of the N-PDU, but it
234 * might well be not the last to arrive */
235 sne->defrag.no_more = 1;
236 }
237
238 if (sne->defrag.no_more) {
239 /* we have already received the last segment before, let's check
240 * if all the previous segments exist */
241 if (defrag_have_all_segments(sne))
242 return defrag_segments(sne);
243 }
244
245 return 0;
246}
Harald Welteebabdea2010-06-01 18:28:10 +0200247
Harald Weltef78a3b22010-06-30 17:21:19 +0200248static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200249 uint8_t nsapi)
250{
Harald Weltef78a3b22010-06-30 17:21:19 +0200251 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200252
Harald Weltef78a3b22010-06-30 17:21:19 +0200253 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
Harald Welteebabdea2010-06-01 18:28:10 +0200254 if (sne->lle == lle && sne->nsapi == nsapi)
255 return sne;
256 }
257 return NULL;
258}
259
Harald Weltef78a3b22010-06-30 17:21:19 +0200260static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200261 uint8_t nsapi)
262{
Harald Weltef78a3b22010-06-30 17:21:19 +0200263 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200264
Harald Weltef78a3b22010-06-30 17:21:19 +0200265 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
Harald Welteebabdea2010-06-01 18:28:10 +0200266 if (!sne)
267 return NULL;
268
269 sne->lle = lle;
270 sne->nsapi = nsapi;
Harald Weltece22f922010-06-03 21:21:21 +0200271 sne->defrag.timer.data = sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200272 //sne->fqueue.timer.cb = FIXME;
273 sne->rx_state = SNDCP_RX_S_FIRST;
Harald Welte362aea02010-07-01 12:31:10 +0200274 INIT_LLIST_HEAD(&sne->defrag.frag_list);
Harald Welteebabdea2010-06-01 18:28:10 +0200275
Harald Weltef78a3b22010-06-30 17:21:19 +0200276 llist_add(&sne->list, &gprs_sndcp_entities);
Harald Welte61444522010-06-02 12:40:48 +0200277
Harald Welteebabdea2010-06-01 18:28:10 +0200278 return sne;
279}
280
281/* Entry point for the SNSM-ACTIVATE.indication */
282int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
283{
Harald Welte61444522010-06-02 12:40:48 +0200284 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
285 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200286
Harald Weltef78a3b22010-06-30 17:21:19 +0200287 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200288 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
289 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
290 lle->llme->tlli, nsapi);
291 return -EEXIST;
292 }
293
Harald Weltef78a3b22010-06-30 17:21:19 +0200294 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200295 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200296 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200297 }
Harald Welteebabdea2010-06-01 18:28:10 +0200298
299 return 0;
300}
301
Harald Weltece22f922010-06-03 21:21:21 +0200302/* Entry point for the SNSM-DEACTIVATE.indication */
303int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
304{
Harald Weltef78a3b22010-06-30 17:21:19 +0200305 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200306
307 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
308 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
309
Harald Weltef78a3b22010-06-30 17:21:19 +0200310 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltece22f922010-06-03 21:21:21 +0200311 if (!sne) {
312 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
313 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
314 lle->sapi, nsapi);
315 return -ENOENT;
316 }
317 llist_del(&sne->list);
318 /* frag queue entries are hierarchically allocated, so no need to
319 * free them explicitly here */
320 talloc_free(sne);
321
322 return 0;
323}
324
325/* Fragmenter state */
326struct sndcp_frag_state {
327 uint8_t frag_nr;
328 struct msgb *msg; /* original message */
329 uint8_t *next_byte; /* first byte of next fragment */
330
Harald Weltef78a3b22010-06-30 17:21:19 +0200331 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200332 void *mmcontext;
333};
334
335/* returns '1' if there are more fragments to send, '0' if none */
336static int sndcp_send_ud_frag(struct sndcp_frag_state *fs)
337{
Harald Weltef78a3b22010-06-30 17:21:19 +0200338 struct gprs_sndcp_entity *sne = fs->sne;
Harald Weltece22f922010-06-03 21:21:21 +0200339 struct gprs_llc_lle *lle = sne->lle;
340 struct sndcp_common_hdr *sch;
341 struct sndcp_comp_hdr *scomph;
342 struct sndcp_udata_hdr *suh;
343 struct msgb *fmsg;
344 unsigned int max_payload_len;
345 unsigned int len;
346 uint8_t *data;
347 int rc, more;
348
Sylvain Munauteda125c2010-06-09 20:56:52 +0200349 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
Harald Weltece22f922010-06-03 21:21:21 +0200350 "SNDCP Frag");
351 if (!fmsg)
352 return -ENOMEM;
353
354 /* make sure lower layers route the fragment like the original */
355 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
356 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
357 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
358
359 /* prepend common SNDCP header */
360 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
361 sch->nsapi = sne->nsapi;
362 /* Set FIRST bit if we are the first fragment in a series */
363 if (fs->frag_nr == 0)
364 sch->first = 1;
365 sch->type = 1;
366
367 /* append the compression header for first fragment */
368 if (sch->first) {
369 scomph = (struct sndcp_comp_hdr *)
370 msgb_put(fmsg, sizeof(*scomph));
371 scomph->pcomp = 0;
372 scomph->dcomp = 0;
373 }
374
375 /* append the user-data header */
376 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
377 suh->npdu_low = sne->tx_npdu_nr & 0xff;
378 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
379 suh->seg_nr = fs->frag_nr % 0xf;
380
381 /* calculate remaining length to be sent */
382 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
383 /* how much payload can we actually send via LLC? */
384 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
385 if (sch->first)
386 max_payload_len -= sizeof(*scomph);
387 /* check if we're exceeding the max */
388 if (len > max_payload_len)
389 len = max_payload_len;
390
391 /* copy the actual fragment data into our fmsg */
392 data = msgb_put(fmsg, len);
393 memcpy(data, fs->next_byte, len);
394
395 /* Increment fragment number and data pointer to next fragment */
396 fs->frag_nr++;
397 fs->next_byte += len;
398
399 /* determine if we have more fragemnts to send */
400 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
401 more = 0;
402 else
403 more = 1;
404
405 /* set the MORE bit of the SNDCP header accordingly */
406 sch->more = more;
407
408 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext);
409 if (rc < 0) {
410 /* abort in case of error, do not advance frag_nr / next_byte */
411 msgb_free(fmsg);
412 return rc;
413 }
414
415 if (!more) {
416 /* we've sent all fragments */
417 msgb_free(fs->msg);
418 memset(fs, 0, sizeof(*fs));
419 /* increment NPDU number for next frame */
420 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
421 return 0;
422 }
423
424 /* default: more fragments to send */
425 return 1;
426}
427
Harald Weltedb2c39f2010-06-03 07:14:59 +0200428/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Harald Weltebb1c8052010-06-03 06:38:38 +0200429int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
430 void *mmcontext)
431{
Harald Weltef78a3b22010-06-30 17:21:19 +0200432 struct gprs_sndcp_entity *sne;
Harald Weltebb1c8052010-06-03 06:38:38 +0200433 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200434 struct sndcp_comp_hdr *scomph;
Harald Weltebb1c8052010-06-03 06:38:38 +0200435 struct sndcp_udata_hdr *suh;
Harald Weltece22f922010-06-03 21:21:21 +0200436 struct sndcp_frag_state fs;
Harald Weltebb1c8052010-06-03 06:38:38 +0200437
438 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
439
Harald Weltef78a3b22010-06-30 17:21:19 +0200440 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltebb1c8052010-06-03 06:38:38 +0200441 if (!sne) {
442 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
443 return -EIO;
444 }
445
Harald Weltece22f922010-06-03 21:21:21 +0200446 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
447 if (msg->len > lle->params.n201_u -
448 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
449 /* initialize the fragmenter state */
450 fs.msg = msg;
451 fs.frag_nr = 0;
452 fs.next_byte = msg->data;
453 fs.sne = sne;
454 fs.mmcontext = mmcontext;
455
456 /* call function to generate and send fragments until all
457 * of the N-PDU has been sent */
458 while (1) {
459 int rc = sndcp_send_ud_frag(&fs);
460 if (rc == 0)
461 return 0;
462 if (rc < 0)
463 return rc;
464 }
465 /* not reached */
466 return 0;
467 }
468
469 /* this is the non-fragmenting case where we only build 1 SN-PDU */
470
Harald Weltebb1c8052010-06-03 06:38:38 +0200471 /* prepend the user-data header */
472 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Weltece22f922010-06-03 21:21:21 +0200473 suh->npdu_low = sne->tx_npdu_nr & 0xff;
474 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
475 suh->seg_nr = 0;
476 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
477
478 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
479 scomph->pcomp = 0;
480 scomph->dcomp = 0;
Harald Weltebb1c8052010-06-03 06:38:38 +0200481
482 /* prepend common SNDCP header */
483 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
484 sch->first = 1;
485 sch->type = 1;
486 sch->nsapi = nsapi;
487
488 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext);
489}
490
Harald Welteebabdea2010-06-01 18:28:10 +0200491/* Section 5.1.2.17 LL-UNITDATA.ind */
492int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t *hdr, uint8_t len)
493{
Harald Weltef78a3b22010-06-30 17:21:19 +0200494 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200495 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Weltece22f922010-06-03 21:21:21 +0200496 struct sndcp_comp_hdr *scomph = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200497 struct sndcp_udata_hdr *suh;
Harald Welte16836a32010-06-02 10:25:40 +0200498 uint8_t *npdu;
Harald Welteebabdea2010-06-01 18:28:10 +0200499 uint16_t npdu_num;
500 int npdu_len;
501
Harald Weltece22f922010-06-03 21:21:21 +0200502 sch = (struct sndcp_common_hdr *) hdr;
503 if (sch->first) {
504 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
505 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
506 } else
507 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
508
Harald Welteebabdea2010-06-01 18:28:10 +0200509 if (sch->type == 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200510 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Welte96f71f22010-05-03 19:28:05 +0200511 return -EINVAL;
512 }
513
Harald Welte16836a32010-06-02 10:25:40 +0200514 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200515 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welteebabdea2010-06-01 18:28:10 +0200516 return -EIO;
517 }
518
Harald Weltef78a3b22010-06-30 17:21:19 +0200519 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200520 if (!sne) {
Harald Welte69996cb2010-06-02 10:26:19 +0200521 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welte61444522010-06-02 12:40:48 +0200522 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
523 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200524 return -EIO;
525 }
Harald Welte8911cef2010-07-01 19:56:19 +0200526 /* FIXME: move this RA_ID up to the LLME or even higher */
527 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
Harald Welteebabdea2010-06-01 18:28:10 +0200528
529 if (!sch->first || sch->more) {
Harald Weltef78a3b22010-06-30 17:21:19 +0200530#if 0
Harald Welteebabdea2010-06-01 18:28:10 +0200531 /* FIXME: implement fragment re-assembly */
Harald Welte69996cb2010-06-02 10:26:19 +0200532 LOGP(DSNDCP, LOGL_ERROR, "We don't support reassembly yet\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200533 return -EIO;
Harald Weltef78a3b22010-06-30 17:21:19 +0200534#else
535 return defrag_input(sne, msg, hdr);
536#endif
Harald Welteebabdea2010-06-01 18:28:10 +0200537 }
538
Harald Weltece22f922010-06-03 21:21:21 +0200539 if (scomph && (scomph->pcomp || scomph->dcomp)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200540 LOGP(DSNDCP, LOGL_ERROR, "We don't support compression yet\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200541 return -EIO;
542 }
Harald Welteebabdea2010-06-01 18:28:10 +0200543
Harald Welte16836a32010-06-02 10:25:40 +0200544 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200545 npdu = (uint8_t *)suh + sizeof(*suh);
546 npdu_len = (msg->data + msg->len) - npdu;
Harald Welte61444522010-06-02 12:40:48 +0200547 if (npdu_len <= 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200548 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200549 return -EIO;
550 }
551 /* actually send the N-PDU to the SGSN core code, which then
552 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Harald Welte8911cef2010-07-01 19:56:19 +0200553 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 +0200554}
555
Harald Welte2720e732010-05-17 00:44:57 +0200556/* Section 5.1.2.1 LL-RESET.ind */
Harald Weltef78a3b22010-06-30 17:21:19 +0200557static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200558{
559 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
560 /* reset all SNDCP XID parameters to default values */
561}
562
Harald Welte2720e732010-05-17 00:44:57 +0200563static int sndcp_ll_status_ind()
564{
565 /* inform the SM sub-layer by means of SNSM-STATUS.req */
566}
567
Harald Welteebabdea2010-06-01 18:28:10 +0200568#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200569static struct sndcp_state_list {{
570 uint32_t states;
571 unsigned int type;
Harald Weltef78a3b22010-06-30 17:21:19 +0200572 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
Harald Welte2720e732010-05-17 00:44:57 +0200573} sndcp_state_list[] = {
574 { ALL_STATES,
575 LL_RESET_IND, sndcp_ll_reset_ind },
576 { ALL_STATES,
577 LL_ESTABLISH_IND, sndcp_ll_est_ind },
578 { SBIT(SNDCP_S_EST_RQD),
579 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
580 { SBIT(SNDCP_S_EST_RQD),
581 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
582 { SBIT(SNDCP_S_
583};
584
585static int sndcp_rx_llc_prim()
586{
587 case LL_ESTABLISH_REQ:
588 case LL_RELEASE_REQ:
589 case LL_XID_REQ:
590 case LL_DATA_REQ:
591 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
592
593 switch (prim) {
594 case LL_RESET_IND:
595 case LL_ESTABLISH_IND:
596 case LL_ESTABLISH_RESP:
597 case LL_ESTABLISH_CONF:
598 case LL_RELEASE_IND:
599 case LL_RELEASE_CONF:
600 case LL_XID_IND:
601 case LL_XID_RESP:
602 case LL_XID_CONF:
603 case LL_DATA_IND:
604 case LL_DATA_CONF:
605 case LL_UNITDATA_IND:
606 case LL_STATUS_IND:
607}
Harald Welteebabdea2010-06-01 18:28:10 +0200608#endif