blob: c006d9bcf00f4030976825eb738f85b00f7031eb [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
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte96f71f22010-05-03 19:28:05 +020011 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte96f71f22010-05-03 19:28:05 +020017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte96f71f22010-05-03 19:28:05 +020020 *
21 */
22
23#include <errno.h>
24#include <stdint.h>
Max82040102016-07-06 11:59:18 +020025#include <stdbool.h>
Harald Welte96f71f22010-05-03 19:28:05 +020026
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010027#include <osmocom/core/msgb.h>
28#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/timer.h>
30#include <osmocom/core/talloc.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080031#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte96f71f22010-05-03 19:28:05 +020032
33#include <openbsc/gsm_data.h>
34#include <openbsc/debug.h>
Harald Welte96f71f22010-05-03 19:28:05 +020035#include <openbsc/gprs_llc.h>
Harald Welteebabdea2010-06-01 18:28:10 +020036#include <openbsc/sgsn.h>
Philipp3ec03d52016-08-10 12:12:43 +020037#include <openbsc/gprs_sndcp.h>
Philippf1f34362016-08-26 17:00:21 +020038#include <openbsc/gprs_llc_xid.h>
39#include <openbsc/gprs_sndcp_xid.h>
40#include <openbsc/gprs_sndcp_pcomp.h>
41#include <openbsc/gprs_sndcp_comp.h>
42
43#define DEBUG_IP_PACKETS 0 /* 0=Disabled, 1=Enabled */
44
45#if DEBUG_IP_PACKETS == 1
46/* Calculate TCP/IP checksum */
47static uint16_t calc_ip_csum(uint8_t *data, int len)
48{
49 int i;
50 uint32_t accumulator = 0;
51 uint16_t *pointer = (uint16_t *) data;
52
53 for (i = len; i > 1; i -= 2) {
54 accumulator += *pointer;
55 pointer++;
56 }
57
58 if (len % 2)
59 accumulator += *pointer;
60
61 accumulator = (accumulator & 0xffff) + ((accumulator >> 16) & 0xffff);
62 accumulator += (accumulator >> 16) & 0xffff;
63 return (~accumulator);
64}
65
66/* Calculate TCP/IP checksum */
67static uint16_t calc_tcpip_csum(const void *ctx, uint8_t *packet, int len)
68{
69 uint8_t *buf;
70 uint16_t csum;
71
72 buf = talloc_zero_size(ctx, len);
73 memset(buf, 0, len);
74 memcpy(buf, packet + 12, 8);
75 buf[9] = packet[9];
76 buf[11] = (len - 20) & 0xFF;
77 buf[10] = (len - 20) >> 8 & 0xFF;
78 memcpy(buf + 12, packet + 20, len - 20);
79 csum = calc_ip_csum(buf, len - 20 + 12);
80 talloc_free(buf);
81 return csum;
82}
83
84/* Show some ip packet details */
85static void debug_ip_packet(uint8_t *data, int len, int dir, char *info)
86{
87 uint8_t tcp_flags;
88 char flags_debugmsg[256];
89 int len_short;
90 static unsigned int packet_count = 0;
91 static unsigned int tcp_csum_err_count = 0;
92 static unsigned int ip_csum_err_count = 0;
93
94 packet_count++;
95
96 if (len > 80)
97 len_short = 80;
98 else
99 len_short = len;
100
101 if (dir)
102 DEBUGP(DSNDCP, "%s: MS => SGSN: %s\n", info,
103 osmo_hexdump_nospc(data, len_short));
104 else
105 DEBUGP(DSNDCP, "%s: MS <= SGSN: %s\n", info,
106 osmo_hexdump_nospc(data, len_short));
107
108 DEBUGP(DSNDCP, "%s: Length.: %d\n", info, len);
109 DEBUGP(DSNDCP, "%s: NO.: %d\n", info, packet_count);
110
111 if (len < 20) {
112 DEBUGP(DSNDCP, "%s: Error: Short IP packet!\n", info);
113 return;
114 }
115
116 if (calc_ip_csum(data, 20) != 0) {
117 DEBUGP(DSNDCP, "%s: Bad IP-Header checksum!\n", info);
118 ip_csum_err_count++;
119 } else
120 DEBUGP(DSNDCP, "%s: IP-Header checksum ok.\n", info);
121
122 if (data[9] == 0x06) {
123 if (len < 40) {
124 DEBUGP(DSNDCP, "%s: Error: Short TCP packet!\n", info);
125 return;
126 }
127
128 DEBUGP(DSNDCP, "%s: Protocol type: TCP\n", info);
129 tcp_flags = data[33];
130
131 if (calc_tcpip_csum(NULL, data, len) != 0) {
132 DEBUGP(DSNDCP, "%s: Bad TCP checksum!\n", info);
133 tcp_csum_err_count++;
134 } else
135 DEBUGP(DSNDCP, "%s: TCP checksum ok.\n", info);
136
137 memset(flags_debugmsg, 0, sizeof(flags_debugmsg));
138 if (tcp_flags & 1)
139 strcat(flags_debugmsg, "FIN ");
140 if (tcp_flags & 2)
141 strcat(flags_debugmsg, "SYN ");
142 if (tcp_flags & 4)
143 strcat(flags_debugmsg, "RST ");
144 if (tcp_flags & 8)
145 strcat(flags_debugmsg, "PSH ");
146 if (tcp_flags & 16)
147 strcat(flags_debugmsg, "ACK ");
148 if (tcp_flags & 32)
149 strcat(flags_debugmsg, "URG ");
150 DEBUGP(DSNDCP, "%s: FLAGS: %s\n", info, flags_debugmsg);
151 } else if (data[9] == 0x11) {
152 DEBUGP(DSNDCP, "%s: Protocol type: UDP\n", info);
153 } else {
154 DEBUGP(DSNDCP, "%s: Protocol type: (%02x)\n", info, data[9]);
155 }
156
157 DEBUGP(DSNDCP, "%s: IP-Header checksum errors: %d\n", info,
158 ip_csum_err_count);
159 DEBUGP(DSNDCP, "%s: TCP-Checksum errors: %d\n", info,
160 tcp_csum_err_count);
161}
162#endif
Harald Weltef78a3b22010-06-30 17:21:19 +0200163
Harald Welte96f71f22010-05-03 19:28:05 +0200164/* Chapter 7.2: SN-PDU Formats */
165struct sndcp_common_hdr {
166 /* octet 1 */
167 uint8_t nsapi:4;
168 uint8_t more:1;
169 uint8_t type:1;
170 uint8_t first:1;
171 uint8_t spare:1;
Harald Weltece22f922010-06-03 21:21:21 +0200172} __attribute__((packed));
173
174/* PCOMP / DCOMP only exist in first fragment */
175struct sndcp_comp_hdr {
Harald Welte96f71f22010-05-03 19:28:05 +0200176 /* octet 2 */
Harald Welte5cc2bc32010-06-02 23:17:05 +0200177 uint8_t pcomp:4;
178 uint8_t dcomp:4;
Harald Welteebabdea2010-06-01 18:28:10 +0200179} __attribute__((packed));
Harald Welte96f71f22010-05-03 19:28:05 +0200180
181struct sndcp_udata_hdr {
182 /* octet 3 */
183 uint8_t npdu_high:4;
184 uint8_t seg_nr:4;
185 /* octet 4 */
186 uint8_t npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200187} __attribute__((packed));
188
Harald Welteebabdea2010-06-01 18:28:10 +0200189
190static void *tall_sndcp_ctx;
191
192/* A fragment queue entry, containing one framgent of a N-PDU */
Harald Weltece22f922010-06-03 21:21:21 +0200193struct defrag_queue_entry {
Harald Welteebabdea2010-06-01 18:28:10 +0200194 struct llist_head list;
Harald Weltece22f922010-06-03 21:21:21 +0200195 /* segment number of this fragment */
196 uint32_t seg_nr;
197 /* length of the data area of this fragment */
Harald Welteebabdea2010-06-01 18:28:10 +0200198 uint32_t data_len;
Harald Weltece22f922010-06-03 21:21:21 +0200199 /* pointer to the data of this fragment */
200 uint8_t *data;
Harald Welteebabdea2010-06-01 18:28:10 +0200201};
202
Harald Weltef78a3b22010-06-30 17:21:19 +0200203LLIST_HEAD(gprs_sndcp_entities);
Harald Welte96f71f22010-05-03 19:28:05 +0200204
Philippf1f34362016-08-26 17:00:21 +0200205/* Check if any compression parameters are set in the sgsn configuration */
206static inline int any_pcomp_or_dcomp_active(struct sgsn_instance *sgsn) {
207 if (sgsn->cfg.pcomp_rfc1144.active || sgsn->cfg.pcomp_rfc1144.passive)
208 return true;
209 else
210 return false;
211}
212
Harald Weltece22f922010-06-03 21:21:21 +0200213/* Enqueue a fragment into the defragment queue */
Harald Weltef78a3b22010-06-30 17:21:19 +0200214static int defrag_enqueue(struct gprs_sndcp_entity *sne, uint8_t seg_nr,
Harald Welte3d6815a2010-07-02 17:16:07 +0200215 uint8_t *data, uint32_t data_len)
Harald Welteebabdea2010-06-01 18:28:10 +0200216{
Harald Weltece22f922010-06-03 21:21:21 +0200217 struct defrag_queue_entry *dqe;
Harald Welteebabdea2010-06-01 18:28:10 +0200218
Harald Weltece22f922010-06-03 21:21:21 +0200219 dqe = talloc_zero(tall_sndcp_ctx, struct defrag_queue_entry);
220 if (!dqe)
221 return -ENOMEM;
222 dqe->data = talloc_zero_size(dqe, data_len);
223 if (!dqe->data) {
224 talloc_free(dqe);
225 return -ENOMEM;
226 }
227 dqe->seg_nr = seg_nr;
228 dqe->data_len = data_len;
229
230 llist_add(&dqe->list, &sne->defrag.frag_list);
231
232 if (seg_nr > sne->defrag.highest_seg)
233 sne->defrag.highest_seg = seg_nr;
234
235 sne->defrag.seg_have |= (1 << seg_nr);
236 sne->defrag.tot_len += data_len;
237
Harald Welte8f0c0a32010-07-02 10:29:06 +0200238 memcpy(dqe->data, data, data_len);
239
Harald Weltece22f922010-06-03 21:21:21 +0200240 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200241}
242
Harald Weltece22f922010-06-03 21:21:21 +0200243/* return if we have all segments of this N-PDU */
Harald Weltef78a3b22010-06-30 17:21:19 +0200244static int defrag_have_all_segments(struct gprs_sndcp_entity *sne)
Harald Welteebabdea2010-06-01 18:28:10 +0200245{
Harald Weltece22f922010-06-03 21:21:21 +0200246 uint32_t seg_needed = 0;
247 unsigned int i;
Harald Welteebabdea2010-06-01 18:28:10 +0200248
Harald Weltece22f922010-06-03 21:21:21 +0200249 /* create a bitmask of needed segments */
Harald Welte951a12c2010-07-01 15:09:45 +0200250 for (i = 0; i <= sne->defrag.highest_seg; i++)
Harald Weltece22f922010-06-03 21:21:21 +0200251 seg_needed |= (1 << i);
252
253 if (seg_needed == sne->defrag.seg_have)
254 return 1;
255
256 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200257}
258
Harald Weltef78a3b22010-06-30 17:21:19 +0200259static struct defrag_queue_entry *defrag_get_seg(struct gprs_sndcp_entity *sne,
Harald Weltece22f922010-06-03 21:21:21 +0200260 uint32_t seg_nr)
Harald Welteebabdea2010-06-01 18:28:10 +0200261{
Harald Weltece22f922010-06-03 21:21:21 +0200262 struct defrag_queue_entry *dqe;
263
264 llist_for_each_entry(dqe, &sne->defrag.frag_list, list) {
265 if (dqe->seg_nr == seg_nr) {
266 llist_del(&dqe->list);
267 return dqe;
268 }
269 }
270 return NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200271}
Harald Weltece22f922010-06-03 21:21:21 +0200272
Harald Welte8b705f22010-07-02 16:18:59 +0200273/* Perform actual defragmentation and create an output packet */
Harald Weltef78a3b22010-06-30 17:21:19 +0200274static int defrag_segments(struct gprs_sndcp_entity *sne)
Harald Weltece22f922010-06-03 21:21:21 +0200275{
276 struct msgb *msg;
277 unsigned int seg_nr;
278 uint8_t *npdu;
Philippf1f34362016-08-26 17:00:21 +0200279 int npdu_len;
280 int rc;
281 uint8_t *expnd = NULL;
Harald Weltece22f922010-06-03 21:21:21 +0200282
Harald Welteab4094c2010-07-02 16:01:47 +0200283 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Defragment output PDU %u "
284 "num_seg=%u tot_len=%u\n", sne->lle->llme->tlli, sne->nsapi,
285 sne->defrag.npdu, sne->defrag.highest_seg, sne->defrag.tot_len);
Sylvain Munauteda125c2010-06-09 20:56:52 +0200286 msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag");
Harald Weltece22f922010-06-03 21:21:21 +0200287 if (!msg)
288 return -ENOMEM;
289
290 /* FIXME: message headers + identifiers */
291
292 npdu = msg->data;
293
Harald Welte993697c2010-07-02 10:11:42 +0200294 for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) {
Harald Weltece22f922010-06-03 21:21:21 +0200295 struct defrag_queue_entry *dqe;
296 uint8_t *data;
297
298 dqe = defrag_get_seg(sne, seg_nr);
299 if (!dqe) {
300 LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr);
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100301 msgb_free(msg);
Harald Weltece22f922010-06-03 21:21:21 +0200302 return -EIO;
303 }
304 /* actually append the segment to the N-PDU */
305 data = msgb_put(msg, dqe->data_len);
306 memcpy(data, dqe->data, dqe->data_len);
307
308 /* release memory for the fragment queue entry */
309 talloc_free(dqe);
310 }
311
Philippf1f34362016-08-26 17:00:21 +0200312 npdu_len = sne->defrag.tot_len;
313
Harald Welte8b705f22010-07-02 16:18:59 +0200314 /* FIXME: cancel timer */
315
Harald Weltece22f922010-06-03 21:21:21 +0200316 /* actually send the N-PDU to the SGSN core code, which then
317 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Philippf1f34362016-08-26 17:00:21 +0200318
319 /* Decompress packet */
320#if DEBUG_IP_PACKETS == 1
321 DEBUGP(DSNDCP, " \n");
322 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
323 DEBUGP(DSNDCP, "===================================================\n");
324#endif
325 if (any_pcomp_or_dcomp_active(sgsn)) {
326
327 expnd = talloc_zero_size(msg, npdu_len + MAX_HDRDECOMPR_INCR);
328 memcpy(expnd, npdu, npdu_len);
329
330 /* Apply header decompression */
331 rc = gprs_sndcp_pcomp_expand(expnd, npdu_len, sne->defrag.pcomp,
332 sne->defrag.proto);
333 if (rc < 0) {
334 LOGP(DSNDCP, LOGL_ERROR,
335 "TCP/IP Header decompression failed!\n");
336 talloc_free(expnd);
337 return -EIO;
338 }
339
340 /* Modify npu length, expnd is handed directly handed
341 * over to gsn_rx_sndcp_ud_ind(), see below */
342 npdu_len = rc;
343 } else
344 expnd = npdu;
345#if DEBUG_IP_PACKETS == 1
346 debug_ip_packet(expnd, npdu_len, 1, "defrag_segments()");
347 DEBUGP(DSNDCP, "===================================================\n");
348 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
349 DEBUGP(DSNDCP, " \n");
350#endif
351
352 /* Hand off packet to gtp */
353 rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, sne->lle->llme->tlli,
354 sne->nsapi, msg, npdu_len, expnd);
355
356 if (any_pcomp_or_dcomp_active(sgsn))
357 talloc_free(expnd);
358
359 return rc;
Harald Weltece22f922010-06-03 21:21:21 +0200360}
361
Philippf1f34362016-08-26 17:00:21 +0200362static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg,
363 uint8_t *hdr, unsigned int len)
Harald Weltece22f922010-06-03 21:21:21 +0200364{
365 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200366 struct sndcp_udata_hdr *suh;
367 uint16_t npdu_num;
368 uint8_t *data;
369 int rc;
370
371 sch = (struct sndcp_common_hdr *) hdr;
372 if (sch->first) {
Harald Weltece22f922010-06-03 21:21:21 +0200373 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
374 } else
375 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
376
377 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
378
379 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
380
Harald Welteab4094c2010-07-02 16:01:47 +0200381 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u "
382 "Length %u %s %s\n", sne->lle->llme->tlli, sne->nsapi, npdu_num,
383 suh->seg_nr, len, sch->first ? "F " : "", sch->more ? "M" : "");
Harald Welteb87bc862010-07-01 20:29:20 +0200384
Harald Weltece22f922010-06-03 21:21:21 +0200385 if (sch->first) {
386 /* first segment of a new packet. Discard all leftover fragments of
387 * previous packet */
388 if (!llist_empty(&sne->defrag.frag_list)) {
Harald Welte65d96782010-07-01 12:19:02 +0200389 struct defrag_queue_entry *dqe, *dqe2;
Harald Welteb87bc862010-07-01 20:29:20 +0200390 LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping "
391 "SN-PDU %u due to insufficient segments (%04x)\n",
392 sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu,
393 sne->defrag.seg_have);
Harald Welte65d96782010-07-01 12:19:02 +0200394 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
Harald Weltece22f922010-06-03 21:21:21 +0200395 llist_del(&dqe->list);
396 talloc_free(dqe);
397 }
398 }
399 /* store the currently de-fragmented PDU number */
400 sne->defrag.npdu = npdu_num;
Harald Welte8b705f22010-07-02 16:18:59 +0200401
402 /* Re-set fragmentation state */
Harald Weltece22f922010-06-03 21:21:21 +0200403 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
Harald Welte8b705f22010-07-02 16:18:59 +0200404 sne->defrag.tot_len = 0;
405 /* FIXME: (re)start timer */
Harald Weltece22f922010-06-03 21:21:21 +0200406 }
407
408 if (sne->defrag.npdu != npdu_num) {
409 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
410 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
411 /* FIXME */
412 }
413
414 /* FIXME: check if seg_nr already exists */
Harald Welte3d6815a2010-07-02 17:16:07 +0200415 /* make sure to subtract length of SNDCP header from 'len' */
416 rc = defrag_enqueue(sne, suh->seg_nr, data, len - (data - hdr));
Harald Weltece22f922010-06-03 21:21:21 +0200417 if (rc < 0)
418 return rc;
419
420 if (!sch->more) {
421 /* this is suppsed to be the last segment of the N-PDU, but it
422 * might well be not the last to arrive */
423 sne->defrag.no_more = 1;
424 }
425
426 if (sne->defrag.no_more) {
427 /* we have already received the last segment before, let's check
428 * if all the previous segments exist */
429 if (defrag_have_all_segments(sne))
430 return defrag_segments(sne);
431 }
432
433 return 0;
434}
Harald Welteebabdea2010-06-01 18:28:10 +0200435
Harald Weltef78a3b22010-06-30 17:21:19 +0200436static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200437 uint8_t nsapi)
438{
Harald Weltef78a3b22010-06-30 17:21:19 +0200439 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200440
Harald Weltef78a3b22010-06-30 17:21:19 +0200441 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
Harald Welteebabdea2010-06-01 18:28:10 +0200442 if (sne->lle == lle && sne->nsapi == nsapi)
443 return sne;
444 }
445 return NULL;
446}
447
Harald Weltef78a3b22010-06-30 17:21:19 +0200448static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200449 uint8_t nsapi)
450{
Harald Weltef78a3b22010-06-30 17:21:19 +0200451 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200452
Harald Weltef78a3b22010-06-30 17:21:19 +0200453 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
Harald Welteebabdea2010-06-01 18:28:10 +0200454 if (!sne)
455 return NULL;
456
457 sne->lle = lle;
458 sne->nsapi = nsapi;
Harald Weltece22f922010-06-03 21:21:21 +0200459 sne->defrag.timer.data = sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200460 //sne->fqueue.timer.cb = FIXME;
461 sne->rx_state = SNDCP_RX_S_FIRST;
Harald Welte362aea02010-07-01 12:31:10 +0200462 INIT_LLIST_HEAD(&sne->defrag.frag_list);
Harald Welteebabdea2010-06-01 18:28:10 +0200463
Harald Weltef78a3b22010-06-30 17:21:19 +0200464 llist_add(&sne->list, &gprs_sndcp_entities);
Harald Welte61444522010-06-02 12:40:48 +0200465
Harald Welteebabdea2010-06-01 18:28:10 +0200466 return sne;
467}
468
469/* Entry point for the SNSM-ACTIVATE.indication */
470int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
471{
Harald Welte61444522010-06-02 12:40:48 +0200472 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
473 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200474
Harald Weltef78a3b22010-06-30 17:21:19 +0200475 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200476 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
477 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
478 lle->llme->tlli, nsapi);
479 return -EEXIST;
480 }
481
Harald Weltef78a3b22010-06-30 17:21:19 +0200482 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200483 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200484 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200485 }
Harald Welteebabdea2010-06-01 18:28:10 +0200486
487 return 0;
488}
489
Harald Weltece22f922010-06-03 21:21:21 +0200490/* Entry point for the SNSM-DEACTIVATE.indication */
491int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
492{
Harald Weltef78a3b22010-06-30 17:21:19 +0200493 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200494
495 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
496 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
497
Harald Weltef78a3b22010-06-30 17:21:19 +0200498 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltece22f922010-06-03 21:21:21 +0200499 if (!sne) {
500 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
501 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
502 lle->sapi, nsapi);
503 return -ENOENT;
504 }
505 llist_del(&sne->list);
506 /* frag queue entries are hierarchically allocated, so no need to
507 * free them explicitly here */
508 talloc_free(sne);
509
510 return 0;
511}
512
513/* Fragmenter state */
514struct sndcp_frag_state {
515 uint8_t frag_nr;
516 struct msgb *msg; /* original message */
517 uint8_t *next_byte; /* first byte of next fragment */
518
Harald Weltef78a3b22010-06-30 17:21:19 +0200519 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200520 void *mmcontext;
521};
522
523/* returns '1' if there are more fragments to send, '0' if none */
Philippf1f34362016-08-26 17:00:21 +0200524static int sndcp_send_ud_frag(struct sndcp_frag_state *fs,
525 uint8_t pcomp, uint8_t dcomp)
Harald Weltece22f922010-06-03 21:21:21 +0200526{
Harald Weltef78a3b22010-06-30 17:21:19 +0200527 struct gprs_sndcp_entity *sne = fs->sne;
Harald Weltece22f922010-06-03 21:21:21 +0200528 struct gprs_llc_lle *lle = sne->lle;
529 struct sndcp_common_hdr *sch;
530 struct sndcp_comp_hdr *scomph;
531 struct sndcp_udata_hdr *suh;
532 struct msgb *fmsg;
533 unsigned int max_payload_len;
534 unsigned int len;
535 uint8_t *data;
536 int rc, more;
537
Sylvain Munauteda125c2010-06-09 20:56:52 +0200538 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
Harald Weltece22f922010-06-03 21:21:21 +0200539 "SNDCP Frag");
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200540 if (!fmsg) {
541 msgb_free(fs->msg);
Harald Weltece22f922010-06-03 21:21:21 +0200542 return -ENOMEM;
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200543 }
Harald Weltece22f922010-06-03 21:21:21 +0200544
545 /* make sure lower layers route the fragment like the original */
546 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
547 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
548 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
549
550 /* prepend common SNDCP header */
551 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
552 sch->nsapi = sne->nsapi;
553 /* Set FIRST bit if we are the first fragment in a series */
554 if (fs->frag_nr == 0)
555 sch->first = 1;
556 sch->type = 1;
557
558 /* append the compression header for first fragment */
559 if (sch->first) {
560 scomph = (struct sndcp_comp_hdr *)
561 msgb_put(fmsg, sizeof(*scomph));
Philippf1f34362016-08-26 17:00:21 +0200562 scomph->pcomp = pcomp;
563 scomph->dcomp = dcomp;
Harald Weltece22f922010-06-03 21:21:21 +0200564 }
565
566 /* append the user-data header */
567 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
568 suh->npdu_low = sne->tx_npdu_nr & 0xff;
569 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
570 suh->seg_nr = fs->frag_nr % 0xf;
571
572 /* calculate remaining length to be sent */
573 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
574 /* how much payload can we actually send via LLC? */
575 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
576 if (sch->first)
577 max_payload_len -= sizeof(*scomph);
578 /* check if we're exceeding the max */
579 if (len > max_payload_len)
580 len = max_payload_len;
581
582 /* copy the actual fragment data into our fmsg */
583 data = msgb_put(fmsg, len);
584 memcpy(data, fs->next_byte, len);
585
586 /* Increment fragment number and data pointer to next fragment */
587 fs->frag_nr++;
588 fs->next_byte += len;
589
590 /* determine if we have more fragemnts to send */
591 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
592 more = 0;
593 else
594 more = 1;
595
596 /* set the MORE bit of the SNDCP header accordingly */
597 sch->more = more;
598
Max82040102016-07-06 11:59:18 +0200599 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext, true);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200600 /* abort in case of error, do not advance frag_nr / next_byte */
Harald Weltece22f922010-06-03 21:21:21 +0200601 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200602 msgb_free(fs->msg);
Harald Weltece22f922010-06-03 21:21:21 +0200603 return rc;
604 }
605
606 if (!more) {
607 /* we've sent all fragments */
608 msgb_free(fs->msg);
609 memset(fs, 0, sizeof(*fs));
610 /* increment NPDU number for next frame */
611 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
612 return 0;
613 }
614
615 /* default: more fragments to send */
616 return 1;
617}
618
Harald Weltedb2c39f2010-06-03 07:14:59 +0200619/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Harald Weltebb1c8052010-06-03 06:38:38 +0200620int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
621 void *mmcontext)
622{
Harald Weltef78a3b22010-06-30 17:21:19 +0200623 struct gprs_sndcp_entity *sne;
Harald Weltebb1c8052010-06-03 06:38:38 +0200624 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200625 struct sndcp_comp_hdr *scomph;
Harald Weltebb1c8052010-06-03 06:38:38 +0200626 struct sndcp_udata_hdr *suh;
Harald Weltece22f922010-06-03 21:21:21 +0200627 struct sndcp_frag_state fs;
Philippf1f34362016-08-26 17:00:21 +0200628 uint8_t pcomp = 0;
629 uint8_t dcomp = 0;
630 int rc;
Harald Weltebb1c8052010-06-03 06:38:38 +0200631
632 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
633
Philippf1f34362016-08-26 17:00:21 +0200634 /* Compress packet */
635#if DEBUG_IP_PACKETS == 1
636 DEBUGP(DSNDCP, " \n");
637 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
638 DEBUGP(DSNDCP, "===================================================\n");
639 debug_ip_packet(msg->data, msg->len, 0, "sndcp_initdata_req()");
640#endif
641 if (any_pcomp_or_dcomp_active(sgsn)) {
642
643 /* Apply header compression */
644 rc = gprs_sndcp_pcomp_compress(msg->data, msg->len, &pcomp,
645 lle->llme->comp.proto, nsapi);
646 if (rc < 0) {
647 LOGP(DSNDCP, LOGL_ERROR,
648 "TCP/IP Header compression failed!\n");
649 return -EIO;
650 }
651
652 /* Fixup pointer locations and sizes in message buffer to match
653 * the new, compressed buffer size */
654 msgb_get(msg, msg->len);
655 msgb_put(msg, rc);
656 }
657#if DEBUG_IP_PACKETS == 1
658 DEBUGP(DSNDCP, "===================================================\n");
659 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
660 DEBUGP(DSNDCP, " \n");
661#endif
662
Harald Weltef78a3b22010-06-30 17:21:19 +0200663 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltebb1c8052010-06-03 06:38:38 +0200664 if (!sne) {
665 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200666 msgb_free(msg);
Harald Weltebb1c8052010-06-03 06:38:38 +0200667 return -EIO;
668 }
669
Harald Weltece22f922010-06-03 21:21:21 +0200670 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
671 if (msg->len > lle->params.n201_u -
672 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
673 /* initialize the fragmenter state */
674 fs.msg = msg;
675 fs.frag_nr = 0;
676 fs.next_byte = msg->data;
677 fs.sne = sne;
678 fs.mmcontext = mmcontext;
679
680 /* call function to generate and send fragments until all
681 * of the N-PDU has been sent */
682 while (1) {
Philippf1f34362016-08-26 17:00:21 +0200683 int rc = sndcp_send_ud_frag(&fs,pcomp,dcomp);
Harald Weltece22f922010-06-03 21:21:21 +0200684 if (rc == 0)
685 return 0;
686 if (rc < 0)
687 return rc;
688 }
689 /* not reached */
690 return 0;
691 }
692
693 /* this is the non-fragmenting case where we only build 1 SN-PDU */
694
Harald Weltebb1c8052010-06-03 06:38:38 +0200695 /* prepend the user-data header */
696 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Weltece22f922010-06-03 21:21:21 +0200697 suh->npdu_low = sne->tx_npdu_nr & 0xff;
698 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
699 suh->seg_nr = 0;
700 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
701
702 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
Philippf1f34362016-08-26 17:00:21 +0200703 scomph->pcomp = pcomp;
704 scomph->dcomp = dcomp;
Harald Weltebb1c8052010-06-03 06:38:38 +0200705
706 /* prepend common SNDCP header */
707 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
708 sch->first = 1;
709 sch->type = 1;
710 sch->nsapi = nsapi;
711
Max82040102016-07-06 11:59:18 +0200712 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext, true);
Harald Weltebb1c8052010-06-03 06:38:38 +0200713}
714
Harald Welteebabdea2010-06-01 18:28:10 +0200715/* Section 5.1.2.17 LL-UNITDATA.ind */
Harald Welte36f12172010-07-02 16:44:24 +0200716int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle,
717 uint8_t *hdr, uint16_t len)
Harald Welteebabdea2010-06-01 18:28:10 +0200718{
Harald Weltef78a3b22010-06-30 17:21:19 +0200719 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200720 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Weltece22f922010-06-03 21:21:21 +0200721 struct sndcp_comp_hdr *scomph = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200722 struct sndcp_udata_hdr *suh;
Harald Welte16836a32010-06-02 10:25:40 +0200723 uint8_t *npdu;
Holger Hans Peter Freythercfee9522014-04-04 12:43:08 +0200724 uint16_t npdu_num __attribute__((unused));
Harald Welteebabdea2010-06-01 18:28:10 +0200725 int npdu_len;
Philippf1f34362016-08-26 17:00:21 +0200726 int rc;
727 uint8_t *expnd = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200728
Harald Weltece22f922010-06-03 21:21:21 +0200729 sch = (struct sndcp_common_hdr *) hdr;
730 if (sch->first) {
731 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
732 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
733 } else
734 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
735
Harald Welteebabdea2010-06-01 18:28:10 +0200736 if (sch->type == 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200737 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Welte96f71f22010-05-03 19:28:05 +0200738 return -EINVAL;
739 }
740
Harald Welte16836a32010-06-02 10:25:40 +0200741 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200742 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welteebabdea2010-06-01 18:28:10 +0200743 return -EIO;
744 }
745
Harald Weltef78a3b22010-06-30 17:21:19 +0200746 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200747 if (!sne) {
Harald Welte69996cb2010-06-02 10:26:19 +0200748 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welte61444522010-06-02 12:40:48 +0200749 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
750 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200751 return -EIO;
752 }
Harald Welte8911cef2010-07-01 19:56:19 +0200753 /* FIXME: move this RA_ID up to the LLME or even higher */
754 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
Harald Welteebabdea2010-06-01 18:28:10 +0200755
Philippf1f34362016-08-26 17:00:21 +0200756 if(scomph) {
757 sne->defrag.pcomp = scomph->pcomp;
758 sne->defrag.dcomp = scomph->dcomp;
759 sne->defrag.proto = lle->llme->comp.proto;
760 sne->defrag.data = lle->llme->comp.data;
761 }
762
Harald Welteab4094c2010-07-02 16:01:47 +0200763 /* any non-first segment is by definition something to defragment
764 * as is any segment that tells us there are more segments */
765 if (!sch->first || sch->more)
Harald Welte60da7d42010-07-02 15:45:12 +0200766 return defrag_input(sne, msg, hdr, len);
Harald Welteebabdea2010-06-01 18:28:10 +0200767
Harald Welte16836a32010-06-02 10:25:40 +0200768 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200769 npdu = (uint8_t *)suh + sizeof(*suh);
Philippf1f34362016-08-26 17:00:21 +0200770 npdu_len = (msg->data + msg->len) - npdu - 3; /* -3 'removes' the FCS */
771
Harald Welte61444522010-06-02 12:40:48 +0200772 if (npdu_len <= 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200773 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200774 return -EIO;
775 }
776 /* actually send the N-PDU to the SGSN core code, which then
777 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Philippf1f34362016-08-26 17:00:21 +0200778
779 /* Decompress packet */
780#if DEBUG_IP_PACKETS == 1
781 DEBUGP(DSNDCP, " \n");
782 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
783 DEBUGP(DSNDCP, "===================================================\n");
784#endif
785 if (any_pcomp_or_dcomp_active(sgsn)) {
786
787 expnd = talloc_zero_size(msg, npdu_len + MAX_HDRDECOMPR_INCR);
788 memcpy(expnd, npdu, npdu_len);
789
790 /* Apply header decompression */
791 rc = gprs_sndcp_pcomp_expand(expnd, npdu_len, sne->defrag.pcomp,
792 sne->defrag.proto);
793 if (rc < 0) {
794 LOGP(DSNDCP, LOGL_ERROR,
795 "TCP/IP Header decompression failed!\n");
796 talloc_free(expnd);
797 return -EIO;
798 }
799
800 /* Modify npu length, expnd is handed directly handed
801 * over to gsn_rx_sndcp_ud_ind(), see below */
802 npdu_len = rc;
803 } else
804 expnd = npdu;
805#if DEBUG_IP_PACKETS == 1
806 debug_ip_packet(expnd, npdu_len, 1, "sndcp_llunitdata_ind()");
807 DEBUGP(DSNDCP, "===================================================\n");
808 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
809 DEBUGP(DSNDCP, " \n");
810#endif
811
812 /* Hand off packet to gtp */
813 rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, lle->llme->tlli,
814 sne->nsapi, msg, npdu_len, expnd);
815
816 if (any_pcomp_or_dcomp_active(sgsn))
817 talloc_free(expnd);
818
819 return rc;
Harald Welte96f71f22010-05-03 19:28:05 +0200820}
821
Holger Hans Peter Freythercfee9522014-04-04 12:43:08 +0200822#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200823/* Section 5.1.2.1 LL-RESET.ind */
Harald Weltef78a3b22010-06-30 17:21:19 +0200824static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200825{
826 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
827 /* reset all SNDCP XID parameters to default values */
Holger Hans Peter Freyther6142dc42011-10-14 23:37:27 +0200828 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
829 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200830}
831
Harald Welte2720e732010-05-17 00:44:57 +0200832static int sndcp_ll_status_ind()
833{
834 /* inform the SM sub-layer by means of SNSM-STATUS.req */
Holger Hans Peter Freyther6142dc42011-10-14 23:37:27 +0200835 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
836 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200837}
838
839static struct sndcp_state_list {{
840 uint32_t states;
841 unsigned int type;
Harald Weltef78a3b22010-06-30 17:21:19 +0200842 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
Harald Welte2720e732010-05-17 00:44:57 +0200843} sndcp_state_list[] = {
844 { ALL_STATES,
845 LL_RESET_IND, sndcp_ll_reset_ind },
846 { ALL_STATES,
847 LL_ESTABLISH_IND, sndcp_ll_est_ind },
848 { SBIT(SNDCP_S_EST_RQD),
849 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
850 { SBIT(SNDCP_S_EST_RQD),
851 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
852 { SBIT(SNDCP_S_
853};
854
855static int sndcp_rx_llc_prim()
856{
857 case LL_ESTABLISH_REQ:
858 case LL_RELEASE_REQ:
859 case LL_XID_REQ:
860 case LL_DATA_REQ:
861 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
862
863 switch (prim) {
864 case LL_RESET_IND:
865 case LL_ESTABLISH_IND:
866 case LL_ESTABLISH_RESP:
867 case LL_ESTABLISH_CONF:
868 case LL_RELEASE_IND:
869 case LL_RELEASE_CONF:
870 case LL_XID_IND:
871 case LL_XID_RESP:
872 case LL_XID_CONF:
873 case LL_DATA_IND:
874 case LL_DATA_CONF:
875 case LL_UNITDATA_IND:
876 case LL_STATUS_IND:
877}
Harald Welteebabdea2010-06-01 18:28:10 +0200878#endif
Philippf1f34362016-08-26 17:00:21 +0200879
880/* Generate SNDCP-XID message */
881static int gprs_llc_gen_sndcp_xid(uint8_t *bytes, int bytes_len, uint8_t nsapi)
882{
883 int entity = 0;
884 LLIST_HEAD(comp_fields);
885 struct gprs_sndcp_pcomp_rfc1144_params rfc1144_params;
886 struct gprs_sndcp_comp_field rfc1144_comp_field;
887
888 memset(&rfc1144_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
889
890 /* Setup rfc1144 */
891 if (sgsn->cfg.pcomp_rfc1144.active) {
892 rfc1144_params.nsapi[0] = nsapi;
893 rfc1144_params.nsapi_len = 1;
894 rfc1144_params.s01 = sgsn->cfg.pcomp_rfc1144.s01;
895 rfc1144_comp_field.p = 1;
896 rfc1144_comp_field.entity = entity;
897 rfc1144_comp_field.algo = RFC_1144;
898 rfc1144_comp_field.comp[RFC1144_PCOMP1] = 1;
899 rfc1144_comp_field.comp[RFC1144_PCOMP2] = 2;
900 rfc1144_comp_field.comp_len = RFC1144_PCOMP_NUM;
901 rfc1144_comp_field.rfc1144_params = &rfc1144_params;
902 entity++;
903 llist_add(&rfc1144_comp_field.list, &comp_fields);
904 }
905
906 /* Compile bytestream */
907 return gprs_sndcp_compile_xid(bytes, bytes_len, &comp_fields);
908}
909
910/* Set of SNDCP-XID bnegotiation (See also: TS 144 065,
911 * Section 6.8 XID parameter negotiation) */
912int sndcp_sn_xid_req(struct gprs_llc_lle *lle, uint8_t nsapi)
913{
914 /* Note: The specification requires the SNDCP-User to set of an
915 * SNDCP xid request. See also 3GPP TS 44.065, 6.8 XID parameter
916 * negotiation, Figure 11: SNDCP XID negotiation procedure. In
917 * our case the SNDCP-User is sgsn_libgtp.c, which calls
918 * sndcp_sn_xid_req directly. */
919
920 uint8_t l3params[1024];
921 int xid_len;
922 struct gprs_llc_xid_field xid_field_request;
923
924 /* Wipe off all compression entities and their states to
925 * get rid of possible leftovers from a previous session */
926 gprs_sndcp_comp_free(lle->llme->comp.proto);
927 gprs_sndcp_comp_free(lle->llme->comp.data);
928 lle->llme->comp.proto = gprs_sndcp_comp_alloc(lle->llme);
929 lle->llme->comp.data = gprs_sndcp_comp_alloc(lle->llme);
930 talloc_free(lle->llme->xid);
931 lle->llme->xid = NULL;
932
933 /* Generate compression parameter bytestream */
934 xid_len = gprs_llc_gen_sndcp_xid(l3params, sizeof(l3params), nsapi);
935
936 /* Send XID with the SNDCP-XID bytetsream included */
937 if (xid_len > 0) {
938 xid_field_request.type = GPRS_LLC_XID_T_L3_PAR;
939 xid_field_request.data = l3params;
940 xid_field_request.data_len = xid_len;
941 return gprs_ll_xid_req(lle, &xid_field_request);
942 }
943
944 /* When bytestream can not be generated, proceed without SNDCP-XID */
945 return gprs_ll_xid_req(lle, NULL);
946
947}
948
949/* Handle header compression entites */
950static int handle_pcomp_entities(struct gprs_sndcp_comp_field *comp_field,
951 struct gprs_llc_lle *lle)
952{
953 /* Note: This functions also transforms the comp_field into its
954 * echo form (strips comp values, resets propose bit etc...)
955 * the processed comp_fields can then be sent back as XID-
956 * Response without further modification. */
957
958 /* Delete propose bit */
959 comp_field->p = 0;
960
961 /* Process proposed parameters */
962 switch (comp_field->algo) {
963 case RFC_1144:
964 if (sgsn->cfg.pcomp_rfc1144.passive
965 && comp_field->rfc1144_params->nsapi_len > 0) {
966 DEBUGP(DSNDCP,
967 "Accepting RFC1144 header compression...\n");
968 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.proto,
969 comp_field);
970 } else {
971 DEBUGP(DSNDCP,
972 "Rejecting RFC1144 header compression...\n");
973 gprs_sndcp_comp_delete(lle->llme->comp.proto,
974 comp_field->entity);
975 comp_field->rfc1144_params->nsapi_len = 0;
976 }
977 break;
978 case RFC_2507:
979 /* RFC 2507 is not yet supported,
980 * so we set applicable nsapis to zero */
981 DEBUGP(DSNDCP, "Rejecting RFC2507 header compression...\n");
982 comp_field->rfc2507_params->nsapi_len = 0;
983 gprs_sndcp_comp_delete(lle->llme->comp.proto,
984 comp_field->entity);
985 break;
986 case ROHC:
987 /* ROHC is not yet supported,
988 * so we set applicable nsapis to zero */
989 DEBUGP(DSNDCP, "Rejecting ROHC header compression...\n");
990 comp_field->rohc_params->nsapi_len = 0;
991 gprs_sndcp_comp_delete(lle->llme->comp.proto,
992 comp_field->entity);
993 break;
994 }
995
996 return 0;
997}
998
999/* Hanle data compression entites */
1000static int handle_dcomp_entities(struct gprs_sndcp_comp_field *comp_field,
1001 struct gprs_llc_lle *lle)
1002{
1003 /* See note in handle_pcomp_entities() */
1004
1005 /* Delete propose bit */
1006 comp_field->p = 0;
1007
1008 /* Process proposed parameters */
1009 switch (comp_field->algo) {
1010 case V42BIS:
1011 /* V42BIS is not yet supported,
1012 * so we set applicable nsapis to zero */
1013 LOGP(DSNDCP, LOGL_DEBUG,
1014 "Rejecting V.42bis data compression...\n");
1015 comp_field->v42bis_params->nsapi_len = 0;
1016 gprs_sndcp_comp_delete(lle->llme->comp.data,
1017 comp_field->entity);
1018 break;
1019 case V44:
1020 /* V44 is not yet supported,
1021 * so we set applicable nsapis to zero */
1022 DEBUGP(DSNDCP, "Rejecting V.44 data compression...\n");
1023 comp_field->v44_params->nsapi_len = 0;
1024 gprs_sndcp_comp_delete(lle->llme->comp.data,
1025 comp_field->entity);
1026 break;
1027 }
1028
1029 return 0;
1030
1031}
1032
1033/* Process SNDCP-XID indication
1034 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1035int sndcp_sn_xid_ind(struct gprs_llc_xid_field *xid_field_indication,
1036 struct gprs_llc_xid_field *xid_field_response,
1037 struct gprs_llc_lle *lle)
1038{
1039 /* Note: This function computes the SNDCP-XID response that is sent
1040 * back to the ms when a ms originated XID is received. The
1041 * Input XID fields are directly processed and the result is directly
1042 * handed back. */
1043
1044 int rc;
1045 int compclass;
1046
1047 struct llist_head *comp_fields;
1048 struct gprs_sndcp_comp_field *comp_field;
1049
1050 OSMO_ASSERT(xid_field_indication);
1051 OSMO_ASSERT(xid_field_response);
1052 OSMO_ASSERT(lle);
1053
1054 /* Parse SNDCP-CID XID-Field */
1055 comp_fields = gprs_sndcp_parse_xid(lle->llme,
1056 xid_field_indication->data,
1057 xid_field_indication->data_len,
1058 NULL);
1059 if (!comp_fields)
1060 return -EINVAL;
1061
1062 /* Don't bother with empty indications */
1063 if (llist_empty(comp_fields)) {
1064 xid_field_response->data = NULL;
1065 xid_field_response->data_len = 0;
1066 DEBUGP(DSNDCP,
1067 "SNDCP-XID indication did not contain any parameters!\n");
1068 return 0;
1069 }
1070
1071 /* Handle compression entites */
1072 DEBUGP(DSNDCP, "SNDCP-XID-IND (ms):\n");
1073 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1074
1075 llist_for_each_entry(comp_field, comp_fields, list) {
1076 compclass = gprs_sndcp_get_compression_class(comp_field);
1077 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1078 rc = handle_pcomp_entities(comp_field, lle);
1079 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1080 rc = handle_dcomp_entities(comp_field, lle);
1081 else {
1082 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1083 comp_field->entity);
1084 gprs_sndcp_comp_delete(lle->llme->comp.data,
1085 comp_field->entity);
1086 rc = 0;
1087 }
1088
1089 if (rc < 0) {
1090 talloc_free(comp_fields);
1091 return -EINVAL;
1092 }
1093 }
1094
1095 DEBUGP(DSNDCP, "SNDCP-XID-RES (sgsn):\n");
1096 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1097
1098 /* Reserve some memory to store the modified SNDCP-XID bytes */
1099 xid_field_response->data =
1100 talloc_zero_size(lle->llme, xid_field_indication->data_len);
1101
1102 /* Set Type flag for response */
1103 xid_field_response->type = GPRS_LLC_XID_T_L3_PAR;
1104
1105 /* Compile modified SNDCP-XID bytes */
1106 rc = gprs_sndcp_compile_xid(xid_field_response->data,
1107 xid_field_indication->data_len,
1108 comp_fields);
1109
1110 if (rc > 0)
1111 xid_field_response->data_len = rc;
1112 else {
1113 talloc_free(xid_field_response->data);
1114 xid_field_response->data = NULL;
1115 xid_field_response->data_len = 0;
1116 return -EINVAL;
1117 }
1118
1119 talloc_free(comp_fields);
1120
1121 return 0;
1122}
1123
1124/* Process SNDCP-XID indication
1125 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1126int sndcp_sn_xid_conf(struct gprs_llc_xid_field *xid_field_conf,
1127 struct gprs_llc_xid_field *xid_field_request,
1128 struct gprs_llc_lle *lle)
1129{
1130 /* Note: This function handles an incomming SNDCP-XID confirmiation.
1131 * Since the confirmation fields may lack important parameters we
1132 * will reconstruct these missing fields using the original request
1133 * we have sent. After that we will create (or delete) the
1134 * compression entites */
1135
1136 struct llist_head *comp_fields_req;
1137 struct llist_head *comp_fields_conf;
1138 struct gprs_sndcp_comp_field *comp_field;
1139 int rc;
1140 int compclass;
1141
1142 /* We need both, the confirmation that is sent back by the ms,
1143 * and the original request we have sent. If one of this is missing
1144 * we can not process the confirmation, the caller must check if
1145 * request and confirmation fields are available. */
1146 OSMO_ASSERT(xid_field_conf);
1147 OSMO_ASSERT(xid_field_request);
1148
1149 /* Parse SNDCP-CID XID-Field */
1150 comp_fields_req = gprs_sndcp_parse_xid(lle->llme,
1151 xid_field_request->data,
1152 xid_field_request->data_len,
1153 NULL);
1154 if (!comp_fields_req)
1155 return -EINVAL;
1156
1157 DEBUGP(DSNDCP, "SNDCP-XID-REQ (sgsn):\n");
1158 gprs_sndcp_dump_comp_fields(comp_fields_req, LOGL_DEBUG);
1159
1160 /* Parse SNDCP-CID XID-Field */
1161 comp_fields_conf = gprs_sndcp_parse_xid(lle->llme,
1162 xid_field_conf->data,
1163 xid_field_conf->data_len,
1164 comp_fields_req);
1165 if (!comp_fields_conf)
1166 return -EINVAL;
1167
1168 DEBUGP(DSNDCP, "SNDCP-XID-CONF (ms):\n");
1169 gprs_sndcp_dump_comp_fields(comp_fields_conf, LOGL_DEBUG);
1170
1171 /* Handle compression entites */
1172 llist_for_each_entry(comp_field, comp_fields_conf, list) {
1173 compclass = gprs_sndcp_get_compression_class(comp_field);
1174 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1175 rc = handle_pcomp_entities(comp_field, lle);
1176 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1177 rc = handle_dcomp_entities(comp_field, lle);
1178 else {
1179 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1180 comp_field->entity);
1181 gprs_sndcp_comp_delete(lle->llme->comp.data,
1182 comp_field->entity);
1183 rc = 0;
1184 }
1185
1186 if (rc < 0) {
1187 talloc_free(comp_fields_req);
1188 talloc_free(comp_fields_conf);
1189 return -EINVAL;
1190 }
1191 }
1192
1193 talloc_free(comp_fields_req);
1194 talloc_free(comp_fields_conf);
1195
1196 return 0;
1197}