blob: 6e2d314218657835b7c6665111b051e47b1348a9 [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
153 for (seg_nr = 0; seg_nr < sne->defrag.highest_seg; seg_nr++) {
154 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
197 if (sch->first) {
198 /* first segment of a new packet. Discard all leftover fragments of
199 * previous packet */
200 if (!llist_empty(&sne->defrag.frag_list)) {
Harald Welte65d96782010-07-01 12:19:02 +0200201 struct defrag_queue_entry *dqe, *dqe2;
Harald Weltece22f922010-06-03 21:21:21 +0200202 LOGP(DSNDCP, LOGL_INFO, "Dropping SN-PDU due to "
203 "insufficient segments\n");
Harald Welte65d96782010-07-01 12:19:02 +0200204 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
Harald Weltece22f922010-06-03 21:21:21 +0200205 llist_del(&dqe->list);
206 talloc_free(dqe);
207 }
208 }
209 /* store the currently de-fragmented PDU number */
210 sne->defrag.npdu = npdu_num;
211 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
212 /* FIXME: Start timer */
213 }
214
215 if (sne->defrag.npdu != npdu_num) {
216 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
217 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
218 /* FIXME */
219 }
220
221 /* FIXME: check if seg_nr already exists */
222 rc = defrag_enqueue(sne, suh->seg_nr, (msg->data + msg->len) - data, data);
223 if (rc < 0)
224 return rc;
225
226 if (!sch->more) {
227 /* this is suppsed to be the last segment of the N-PDU, but it
228 * might well be not the last to arrive */
229 sne->defrag.no_more = 1;
230 }
231
232 if (sne->defrag.no_more) {
233 /* we have already received the last segment before, let's check
234 * if all the previous segments exist */
235 if (defrag_have_all_segments(sne))
236 return defrag_segments(sne);
237 }
238
239 return 0;
240}
Harald Welteebabdea2010-06-01 18:28:10 +0200241
Harald Weltef78a3b22010-06-30 17:21:19 +0200242static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200243 uint8_t nsapi)
244{
Harald Weltef78a3b22010-06-30 17:21:19 +0200245 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200246
Harald Weltef78a3b22010-06-30 17:21:19 +0200247 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
Harald Welteebabdea2010-06-01 18:28:10 +0200248 if (sne->lle == lle && sne->nsapi == nsapi)
249 return sne;
250 }
251 return NULL;
252}
253
Harald Weltef78a3b22010-06-30 17:21:19 +0200254static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(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 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
Harald Welteebabdea2010-06-01 18:28:10 +0200260 if (!sne)
261 return NULL;
262
263 sne->lle = lle;
264 sne->nsapi = nsapi;
Harald Weltece22f922010-06-03 21:21:21 +0200265 sne->defrag.timer.data = sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200266 //sne->fqueue.timer.cb = FIXME;
267 sne->rx_state = SNDCP_RX_S_FIRST;
Harald Welte362aea02010-07-01 12:31:10 +0200268 INIT_LLIST_HEAD(&sne->defrag.frag_list);
Harald Welteebabdea2010-06-01 18:28:10 +0200269
Harald Weltef78a3b22010-06-30 17:21:19 +0200270 llist_add(&sne->list, &gprs_sndcp_entities);
Harald Welte61444522010-06-02 12:40:48 +0200271
Harald Welteebabdea2010-06-01 18:28:10 +0200272 return sne;
273}
274
275/* Entry point for the SNSM-ACTIVATE.indication */
276int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
277{
Harald Welte61444522010-06-02 12:40:48 +0200278 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
279 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200280
Harald Weltef78a3b22010-06-30 17:21:19 +0200281 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200282 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
283 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
284 lle->llme->tlli, nsapi);
285 return -EEXIST;
286 }
287
Harald Weltef78a3b22010-06-30 17:21:19 +0200288 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200289 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200290 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200291 }
Harald Welteebabdea2010-06-01 18:28:10 +0200292
293 return 0;
294}
295
Harald Weltece22f922010-06-03 21:21:21 +0200296/* Entry point for the SNSM-DEACTIVATE.indication */
297int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
298{
Harald Weltef78a3b22010-06-30 17:21:19 +0200299 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200300
301 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
302 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
303
Harald Weltef78a3b22010-06-30 17:21:19 +0200304 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltece22f922010-06-03 21:21:21 +0200305 if (!sne) {
306 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
307 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
308 lle->sapi, nsapi);
309 return -ENOENT;
310 }
311 llist_del(&sne->list);
312 /* frag queue entries are hierarchically allocated, so no need to
313 * free them explicitly here */
314 talloc_free(sne);
315
316 return 0;
317}
318
319/* Fragmenter state */
320struct sndcp_frag_state {
321 uint8_t frag_nr;
322 struct msgb *msg; /* original message */
323 uint8_t *next_byte; /* first byte of next fragment */
324
Harald Weltef78a3b22010-06-30 17:21:19 +0200325 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200326 void *mmcontext;
327};
328
329/* returns '1' if there are more fragments to send, '0' if none */
330static int sndcp_send_ud_frag(struct sndcp_frag_state *fs)
331{
Harald Weltef78a3b22010-06-30 17:21:19 +0200332 struct gprs_sndcp_entity *sne = fs->sne;
Harald Weltece22f922010-06-03 21:21:21 +0200333 struct gprs_llc_lle *lle = sne->lle;
334 struct sndcp_common_hdr *sch;
335 struct sndcp_comp_hdr *scomph;
336 struct sndcp_udata_hdr *suh;
337 struct msgb *fmsg;
338 unsigned int max_payload_len;
339 unsigned int len;
340 uint8_t *data;
341 int rc, more;
342
Sylvain Munauteda125c2010-06-09 20:56:52 +0200343 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
Harald Weltece22f922010-06-03 21:21:21 +0200344 "SNDCP Frag");
345 if (!fmsg)
346 return -ENOMEM;
347
348 /* make sure lower layers route the fragment like the original */
349 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
350 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
351 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
352
353 /* prepend common SNDCP header */
354 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
355 sch->nsapi = sne->nsapi;
356 /* Set FIRST bit if we are the first fragment in a series */
357 if (fs->frag_nr == 0)
358 sch->first = 1;
359 sch->type = 1;
360
361 /* append the compression header for first fragment */
362 if (sch->first) {
363 scomph = (struct sndcp_comp_hdr *)
364 msgb_put(fmsg, sizeof(*scomph));
365 scomph->pcomp = 0;
366 scomph->dcomp = 0;
367 }
368
369 /* append the user-data header */
370 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
371 suh->npdu_low = sne->tx_npdu_nr & 0xff;
372 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
373 suh->seg_nr = fs->frag_nr % 0xf;
374
375 /* calculate remaining length to be sent */
376 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
377 /* how much payload can we actually send via LLC? */
378 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
379 if (sch->first)
380 max_payload_len -= sizeof(*scomph);
381 /* check if we're exceeding the max */
382 if (len > max_payload_len)
383 len = max_payload_len;
384
385 /* copy the actual fragment data into our fmsg */
386 data = msgb_put(fmsg, len);
387 memcpy(data, fs->next_byte, len);
388
389 /* Increment fragment number and data pointer to next fragment */
390 fs->frag_nr++;
391 fs->next_byte += len;
392
393 /* determine if we have more fragemnts to send */
394 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
395 more = 0;
396 else
397 more = 1;
398
399 /* set the MORE bit of the SNDCP header accordingly */
400 sch->more = more;
401
402 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext);
403 if (rc < 0) {
404 /* abort in case of error, do not advance frag_nr / next_byte */
405 msgb_free(fmsg);
406 return rc;
407 }
408
409 if (!more) {
410 /* we've sent all fragments */
411 msgb_free(fs->msg);
412 memset(fs, 0, sizeof(*fs));
413 /* increment NPDU number for next frame */
414 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
415 return 0;
416 }
417
418 /* default: more fragments to send */
419 return 1;
420}
421
Harald Weltedb2c39f2010-06-03 07:14:59 +0200422/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Harald Weltebb1c8052010-06-03 06:38:38 +0200423int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
424 void *mmcontext)
425{
Harald Weltef78a3b22010-06-30 17:21:19 +0200426 struct gprs_sndcp_entity *sne;
Harald Weltebb1c8052010-06-03 06:38:38 +0200427 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200428 struct sndcp_comp_hdr *scomph;
Harald Weltebb1c8052010-06-03 06:38:38 +0200429 struct sndcp_udata_hdr *suh;
Harald Weltece22f922010-06-03 21:21:21 +0200430 struct sndcp_frag_state fs;
Harald Weltebb1c8052010-06-03 06:38:38 +0200431
432 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
433
Harald Weltef78a3b22010-06-30 17:21:19 +0200434 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltebb1c8052010-06-03 06:38:38 +0200435 if (!sne) {
436 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
437 return -EIO;
438 }
439
Harald Weltece22f922010-06-03 21:21:21 +0200440 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
441 if (msg->len > lle->params.n201_u -
442 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
443 /* initialize the fragmenter state */
444 fs.msg = msg;
445 fs.frag_nr = 0;
446 fs.next_byte = msg->data;
447 fs.sne = sne;
448 fs.mmcontext = mmcontext;
449
450 /* call function to generate and send fragments until all
451 * of the N-PDU has been sent */
452 while (1) {
453 int rc = sndcp_send_ud_frag(&fs);
454 if (rc == 0)
455 return 0;
456 if (rc < 0)
457 return rc;
458 }
459 /* not reached */
460 return 0;
461 }
462
463 /* this is the non-fragmenting case where we only build 1 SN-PDU */
464
Harald Weltebb1c8052010-06-03 06:38:38 +0200465 /* prepend the user-data header */
466 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Weltece22f922010-06-03 21:21:21 +0200467 suh->npdu_low = sne->tx_npdu_nr & 0xff;
468 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
469 suh->seg_nr = 0;
470 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
471
472 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
473 scomph->pcomp = 0;
474 scomph->dcomp = 0;
Harald Weltebb1c8052010-06-03 06:38:38 +0200475
476 /* prepend common SNDCP header */
477 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
478 sch->first = 1;
479 sch->type = 1;
480 sch->nsapi = nsapi;
481
482 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext);
483}
484
Harald Welteebabdea2010-06-01 18:28:10 +0200485/* Section 5.1.2.17 LL-UNITDATA.ind */
486int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t *hdr, uint8_t len)
487{
Harald Weltef78a3b22010-06-30 17:21:19 +0200488 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200489 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Weltece22f922010-06-03 21:21:21 +0200490 struct sndcp_comp_hdr *scomph = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200491 struct sndcp_udata_hdr *suh;
Harald Welte16836a32010-06-02 10:25:40 +0200492 uint8_t *npdu;
Harald Welteebabdea2010-06-01 18:28:10 +0200493 uint16_t npdu_num;
494 int npdu_len;
495
Harald Weltece22f922010-06-03 21:21:21 +0200496 sch = (struct sndcp_common_hdr *) hdr;
497 if (sch->first) {
498 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
499 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
500 } else
501 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
502
Harald Welteebabdea2010-06-01 18:28:10 +0200503 if (sch->type == 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200504 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Welte96f71f22010-05-03 19:28:05 +0200505 return -EINVAL;
506 }
507
Harald Welte16836a32010-06-02 10:25:40 +0200508 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200509 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welteebabdea2010-06-01 18:28:10 +0200510 return -EIO;
511 }
512
Harald Weltef78a3b22010-06-30 17:21:19 +0200513 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200514 if (!sne) {
Harald Welte69996cb2010-06-02 10:26:19 +0200515 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welte61444522010-06-02 12:40:48 +0200516 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
517 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200518 return -EIO;
519 }
Harald Welte8911cef2010-07-01 19:56:19 +0200520 /* FIXME: move this RA_ID up to the LLME or even higher */
521 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
Harald Welteebabdea2010-06-01 18:28:10 +0200522
523 if (!sch->first || sch->more) {
Harald Weltef78a3b22010-06-30 17:21:19 +0200524#if 0
Harald Welteebabdea2010-06-01 18:28:10 +0200525 /* FIXME: implement fragment re-assembly */
Harald Welte69996cb2010-06-02 10:26:19 +0200526 LOGP(DSNDCP, LOGL_ERROR, "We don't support reassembly yet\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200527 return -EIO;
Harald Weltef78a3b22010-06-30 17:21:19 +0200528#else
529 return defrag_input(sne, msg, hdr);
530#endif
Harald Welteebabdea2010-06-01 18:28:10 +0200531 }
532
Harald Weltece22f922010-06-03 21:21:21 +0200533 if (scomph && (scomph->pcomp || scomph->dcomp)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200534 LOGP(DSNDCP, LOGL_ERROR, "We don't support compression yet\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200535 return -EIO;
536 }
Harald Welteebabdea2010-06-01 18:28:10 +0200537
Harald Welte16836a32010-06-02 10:25:40 +0200538 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200539 npdu = (uint8_t *)suh + sizeof(*suh);
540 npdu_len = (msg->data + msg->len) - npdu;
Harald Welte61444522010-06-02 12:40:48 +0200541 if (npdu_len <= 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200542 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200543 return -EIO;
544 }
545 /* actually send the N-PDU to the SGSN core code, which then
546 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Harald Welte8911cef2010-07-01 19:56:19 +0200547 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 +0200548}
549
Harald Welte2720e732010-05-17 00:44:57 +0200550/* Section 5.1.2.1 LL-RESET.ind */
Harald Weltef78a3b22010-06-30 17:21:19 +0200551static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200552{
553 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
554 /* reset all SNDCP XID parameters to default values */
555}
556
Harald Welte2720e732010-05-17 00:44:57 +0200557static int sndcp_ll_status_ind()
558{
559 /* inform the SM sub-layer by means of SNSM-STATUS.req */
560}
561
Harald Welteebabdea2010-06-01 18:28:10 +0200562#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200563static struct sndcp_state_list {{
564 uint32_t states;
565 unsigned int type;
Harald Weltef78a3b22010-06-30 17:21:19 +0200566 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
Harald Welte2720e732010-05-17 00:44:57 +0200567} sndcp_state_list[] = {
568 { ALL_STATES,
569 LL_RESET_IND, sndcp_ll_reset_ind },
570 { ALL_STATES,
571 LL_ESTABLISH_IND, sndcp_ll_est_ind },
572 { SBIT(SNDCP_S_EST_RQD),
573 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
574 { SBIT(SNDCP_S_EST_RQD),
575 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
576 { SBIT(SNDCP_S_
577};
578
579static int sndcp_rx_llc_prim()
580{
581 case LL_ESTABLISH_REQ:
582 case LL_RELEASE_REQ:
583 case LL_XID_REQ:
584 case LL_DATA_REQ:
585 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
586
587 switch (prim) {
588 case LL_RESET_IND:
589 case LL_ESTABLISH_IND:
590 case LL_ESTABLISH_RESP:
591 case LL_ESTABLISH_CONF:
592 case LL_RELEASE_IND:
593 case LL_RELEASE_CONF:
594 case LL_XID_IND:
595 case LL_XID_RESP:
596 case LL_XID_CONF:
597 case LL_DATA_IND:
598 case LL_DATA_CONF:
599 case LL_UNITDATA_IND:
600 case LL_STATUS_IND:
601}
Harald Welteebabdea2010-06-01 18:28:10 +0200602#endif