blob: 52eeb75d5a3ee5e9a25fada9b93f31427d44a3a7 [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
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020033#include <osmocom/sgsn/debug.h>
34#include <osmocom/sgsn/gprs_llc.h>
35#include <osmocom/sgsn/sgsn.h>
36#include <osmocom/sgsn/gprs_sndcp.h>
37#include <osmocom/sgsn/gprs_llc_xid.h>
38#include <osmocom/sgsn/gprs_sndcp_xid.h>
39#include <osmocom/sgsn/gprs_sndcp_pcomp.h>
40#include <osmocom/sgsn/gprs_sndcp_dcomp.h>
41#include <osmocom/sgsn/gprs_sndcp_comp.h>
Philippf1f34362016-08-26 17:00:21 +020042
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) {
Philipp73f83d52016-09-02 13:38:01 +0200207 if (sgsn->cfg.pcomp_rfc1144.active || sgsn->cfg.pcomp_rfc1144.passive ||
208 sgsn->cfg.dcomp_v42bis.active || sgsn->cfg.dcomp_v42bis.passive)
Philippf1f34362016-08-26 17:00:21 +0200209 return true;
210 else
211 return false;
212}
213
Harald Weltece22f922010-06-03 21:21:21 +0200214/* Enqueue a fragment into the defragment queue */
Harald Weltef78a3b22010-06-30 17:21:19 +0200215static int defrag_enqueue(struct gprs_sndcp_entity *sne, uint8_t seg_nr,
Harald Welte3d6815a2010-07-02 17:16:07 +0200216 uint8_t *data, uint32_t data_len)
Harald Welteebabdea2010-06-01 18:28:10 +0200217{
Harald Weltece22f922010-06-03 21:21:21 +0200218 struct defrag_queue_entry *dqe;
Harald Welteebabdea2010-06-01 18:28:10 +0200219
Harald Weltece22f922010-06-03 21:21:21 +0200220 dqe = talloc_zero(tall_sndcp_ctx, struct defrag_queue_entry);
221 if (!dqe)
222 return -ENOMEM;
223 dqe->data = talloc_zero_size(dqe, data_len);
224 if (!dqe->data) {
225 talloc_free(dqe);
226 return -ENOMEM;
227 }
228 dqe->seg_nr = seg_nr;
229 dqe->data_len = data_len;
230
231 llist_add(&dqe->list, &sne->defrag.frag_list);
232
233 if (seg_nr > sne->defrag.highest_seg)
234 sne->defrag.highest_seg = seg_nr;
235
236 sne->defrag.seg_have |= (1 << seg_nr);
237 sne->defrag.tot_len += data_len;
238
Harald Welte8f0c0a32010-07-02 10:29:06 +0200239 memcpy(dqe->data, data, data_len);
240
Harald Weltece22f922010-06-03 21:21:21 +0200241 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200242}
243
Harald Weltece22f922010-06-03 21:21:21 +0200244/* return if we have all segments of this N-PDU */
Harald Weltef78a3b22010-06-30 17:21:19 +0200245static int defrag_have_all_segments(struct gprs_sndcp_entity *sne)
Harald Welteebabdea2010-06-01 18:28:10 +0200246{
Harald Weltece22f922010-06-03 21:21:21 +0200247 uint32_t seg_needed = 0;
248 unsigned int i;
Harald Welteebabdea2010-06-01 18:28:10 +0200249
Harald Weltece22f922010-06-03 21:21:21 +0200250 /* create a bitmask of needed segments */
Harald Welte951a12c2010-07-01 15:09:45 +0200251 for (i = 0; i <= sne->defrag.highest_seg; i++)
Harald Weltece22f922010-06-03 21:21:21 +0200252 seg_needed |= (1 << i);
253
254 if (seg_needed == sne->defrag.seg_have)
255 return 1;
256
257 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200258}
259
Harald Weltef78a3b22010-06-30 17:21:19 +0200260static struct defrag_queue_entry *defrag_get_seg(struct gprs_sndcp_entity *sne,
Harald Weltece22f922010-06-03 21:21:21 +0200261 uint32_t seg_nr)
Harald Welteebabdea2010-06-01 18:28:10 +0200262{
Harald Weltece22f922010-06-03 21:21:21 +0200263 struct defrag_queue_entry *dqe;
264
265 llist_for_each_entry(dqe, &sne->defrag.frag_list, list) {
266 if (dqe->seg_nr == seg_nr) {
267 llist_del(&dqe->list);
268 return dqe;
269 }
270 }
271 return NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200272}
Harald Weltece22f922010-06-03 21:21:21 +0200273
Harald Welte8b705f22010-07-02 16:18:59 +0200274/* Perform actual defragmentation and create an output packet */
Harald Weltef78a3b22010-06-30 17:21:19 +0200275static int defrag_segments(struct gprs_sndcp_entity *sne)
Harald Weltece22f922010-06-03 21:21:21 +0200276{
277 struct msgb *msg;
278 unsigned int seg_nr;
279 uint8_t *npdu;
Philippf1f34362016-08-26 17:00:21 +0200280 int npdu_len;
281 int rc;
282 uint8_t *expnd = NULL;
Harald Weltece22f922010-06-03 21:21:21 +0200283
Harald Welteab4094c2010-07-02 16:01:47 +0200284 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Defragment output PDU %u "
285 "num_seg=%u tot_len=%u\n", sne->lle->llme->tlli, sne->nsapi,
286 sne->defrag.npdu, sne->defrag.highest_seg, sne->defrag.tot_len);
Sylvain Munauteda125c2010-06-09 20:56:52 +0200287 msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag");
Harald Weltece22f922010-06-03 21:21:21 +0200288 if (!msg)
289 return -ENOMEM;
290
291 /* FIXME: message headers + identifiers */
292
293 npdu = msg->data;
294
Harald Welte993697c2010-07-02 10:11:42 +0200295 for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) {
Harald Weltece22f922010-06-03 21:21:21 +0200296 struct defrag_queue_entry *dqe;
297 uint8_t *data;
298
299 dqe = defrag_get_seg(sne, seg_nr);
300 if (!dqe) {
301 LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr);
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100302 msgb_free(msg);
Harald Weltece22f922010-06-03 21:21:21 +0200303 return -EIO;
304 }
305 /* actually append the segment to the N-PDU */
306 data = msgb_put(msg, dqe->data_len);
307 memcpy(data, dqe->data, dqe->data_len);
308
309 /* release memory for the fragment queue entry */
310 talloc_free(dqe);
311 }
312
Philippf1f34362016-08-26 17:00:21 +0200313 npdu_len = sne->defrag.tot_len;
314
Harald Welte8b705f22010-07-02 16:18:59 +0200315 /* FIXME: cancel timer */
316
Harald Weltece22f922010-06-03 21:21:21 +0200317 /* actually send the N-PDU to the SGSN core code, which then
318 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Philippf1f34362016-08-26 17:00:21 +0200319
320 /* Decompress packet */
321#if DEBUG_IP_PACKETS == 1
322 DEBUGP(DSNDCP, " \n");
323 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
324 DEBUGP(DSNDCP, "===================================================\n");
325#endif
326 if (any_pcomp_or_dcomp_active(sgsn)) {
327
Philipp73f83d52016-09-02 13:38:01 +0200328 expnd = talloc_zero_size(msg, npdu_len * MAX_DATADECOMPR_FAC +
329 MAX_HDRDECOMPR_INCR);
Philippf1f34362016-08-26 17:00:21 +0200330 memcpy(expnd, npdu, npdu_len);
331
Philipp73f83d52016-09-02 13:38:01 +0200332 /* Apply data decompression */
333 rc = gprs_sndcp_dcomp_expand(expnd, npdu_len, sne->defrag.dcomp,
334 sne->defrag.data);
335 if (rc < 0) {
336 LOGP(DSNDCP, LOGL_ERROR,
337 "Data decompression failed!\n");
338 talloc_free(expnd);
339 return -EIO;
340 }
341
Philippf1f34362016-08-26 17:00:21 +0200342 /* Apply header decompression */
Philipp73f83d52016-09-02 13:38:01 +0200343 rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp,
Philippf1f34362016-08-26 17:00:21 +0200344 sne->defrag.proto);
345 if (rc < 0) {
346 LOGP(DSNDCP, LOGL_ERROR,
347 "TCP/IP Header decompression failed!\n");
348 talloc_free(expnd);
349 return -EIO;
350 }
351
352 /* Modify npu length, expnd is handed directly handed
353 * over to gsn_rx_sndcp_ud_ind(), see below */
354 npdu_len = rc;
355 } else
356 expnd = npdu;
357#if DEBUG_IP_PACKETS == 1
358 debug_ip_packet(expnd, npdu_len, 1, "defrag_segments()");
359 DEBUGP(DSNDCP, "===================================================\n");
360 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
361 DEBUGP(DSNDCP, " \n");
362#endif
363
364 /* Hand off packet to gtp */
365 rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, sne->lle->llme->tlli,
366 sne->nsapi, msg, npdu_len, expnd);
367
368 if (any_pcomp_or_dcomp_active(sgsn))
369 talloc_free(expnd);
370
371 return rc;
Harald Weltece22f922010-06-03 21:21:21 +0200372}
373
Philippf1f34362016-08-26 17:00:21 +0200374static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg,
375 uint8_t *hdr, unsigned int len)
Harald Weltece22f922010-06-03 21:21:21 +0200376{
377 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200378 struct sndcp_udata_hdr *suh;
379 uint16_t npdu_num;
380 uint8_t *data;
381 int rc;
382
383 sch = (struct sndcp_common_hdr *) hdr;
384 if (sch->first) {
Harald Weltece22f922010-06-03 21:21:21 +0200385 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
386 } else
387 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
388
389 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
390
391 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
392
Harald Welteab4094c2010-07-02 16:01:47 +0200393 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u "
394 "Length %u %s %s\n", sne->lle->llme->tlli, sne->nsapi, npdu_num,
395 suh->seg_nr, len, sch->first ? "F " : "", sch->more ? "M" : "");
Harald Welteb87bc862010-07-01 20:29:20 +0200396
Harald Weltece22f922010-06-03 21:21:21 +0200397 if (sch->first) {
398 /* first segment of a new packet. Discard all leftover fragments of
399 * previous packet */
400 if (!llist_empty(&sne->defrag.frag_list)) {
Harald Welte65d96782010-07-01 12:19:02 +0200401 struct defrag_queue_entry *dqe, *dqe2;
Harald Welteb87bc862010-07-01 20:29:20 +0200402 LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping "
403 "SN-PDU %u due to insufficient segments (%04x)\n",
404 sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu,
405 sne->defrag.seg_have);
Harald Welte65d96782010-07-01 12:19:02 +0200406 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
Harald Weltece22f922010-06-03 21:21:21 +0200407 llist_del(&dqe->list);
408 talloc_free(dqe);
409 }
410 }
411 /* store the currently de-fragmented PDU number */
412 sne->defrag.npdu = npdu_num;
Harald Welte8b705f22010-07-02 16:18:59 +0200413
414 /* Re-set fragmentation state */
Harald Weltece22f922010-06-03 21:21:21 +0200415 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
Harald Welte8b705f22010-07-02 16:18:59 +0200416 sne->defrag.tot_len = 0;
417 /* FIXME: (re)start timer */
Harald Weltece22f922010-06-03 21:21:21 +0200418 }
419
420 if (sne->defrag.npdu != npdu_num) {
421 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
422 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
423 /* FIXME */
424 }
425
426 /* FIXME: check if seg_nr already exists */
Harald Welte3d6815a2010-07-02 17:16:07 +0200427 /* make sure to subtract length of SNDCP header from 'len' */
428 rc = defrag_enqueue(sne, suh->seg_nr, data, len - (data - hdr));
Harald Weltece22f922010-06-03 21:21:21 +0200429 if (rc < 0)
430 return rc;
431
432 if (!sch->more) {
433 /* this is suppsed to be the last segment of the N-PDU, but it
434 * might well be not the last to arrive */
435 sne->defrag.no_more = 1;
436 }
437
438 if (sne->defrag.no_more) {
439 /* we have already received the last segment before, let's check
440 * if all the previous segments exist */
441 if (defrag_have_all_segments(sne))
442 return defrag_segments(sne);
443 }
444
445 return 0;
446}
Harald Welteebabdea2010-06-01 18:28:10 +0200447
Harald Weltef78a3b22010-06-30 17:21:19 +0200448static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const 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 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
Harald Welteebabdea2010-06-01 18:28:10 +0200454 if (sne->lle == lle && sne->nsapi == nsapi)
455 return sne;
456 }
457 return NULL;
458}
459
Harald Weltef78a3b22010-06-30 17:21:19 +0200460static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200461 uint8_t nsapi)
462{
Harald Weltef78a3b22010-06-30 17:21:19 +0200463 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200464
Harald Weltef78a3b22010-06-30 17:21:19 +0200465 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
Harald Welteebabdea2010-06-01 18:28:10 +0200466 if (!sne)
467 return NULL;
468
469 sne->lle = lle;
470 sne->nsapi = nsapi;
Harald Weltece22f922010-06-03 21:21:21 +0200471 sne->defrag.timer.data = sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200472 //sne->fqueue.timer.cb = FIXME;
473 sne->rx_state = SNDCP_RX_S_FIRST;
Harald Welte362aea02010-07-01 12:31:10 +0200474 INIT_LLIST_HEAD(&sne->defrag.frag_list);
Harald Welteebabdea2010-06-01 18:28:10 +0200475
Harald Weltef78a3b22010-06-30 17:21:19 +0200476 llist_add(&sne->list, &gprs_sndcp_entities);
Harald Welte61444522010-06-02 12:40:48 +0200477
Harald Welteebabdea2010-06-01 18:28:10 +0200478 return sne;
479}
480
481/* Entry point for the SNSM-ACTIVATE.indication */
482int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
483{
Harald Welte61444522010-06-02 12:40:48 +0200484 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
485 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200486
Harald Weltef78a3b22010-06-30 17:21:19 +0200487 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200488 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
489 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
490 lle->llme->tlli, nsapi);
491 return -EEXIST;
492 }
493
Harald Weltef78a3b22010-06-30 17:21:19 +0200494 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200495 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200496 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200497 }
Harald Welteebabdea2010-06-01 18:28:10 +0200498
499 return 0;
500}
501
Harald Weltece22f922010-06-03 21:21:21 +0200502/* Entry point for the SNSM-DEACTIVATE.indication */
503int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
504{
Harald Weltef78a3b22010-06-30 17:21:19 +0200505 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200506
507 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
508 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
509
Harald Weltef78a3b22010-06-30 17:21:19 +0200510 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltece22f922010-06-03 21:21:21 +0200511 if (!sne) {
512 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
513 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
514 lle->sapi, nsapi);
515 return -ENOENT;
516 }
517 llist_del(&sne->list);
518 /* frag queue entries are hierarchically allocated, so no need to
519 * free them explicitly here */
520 talloc_free(sne);
521
522 return 0;
523}
524
525/* Fragmenter state */
526struct sndcp_frag_state {
527 uint8_t frag_nr;
528 struct msgb *msg; /* original message */
529 uint8_t *next_byte; /* first byte of next fragment */
530
Harald Weltef78a3b22010-06-30 17:21:19 +0200531 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200532 void *mmcontext;
533};
534
535/* returns '1' if there are more fragments to send, '0' if none */
Philippf1f34362016-08-26 17:00:21 +0200536static int sndcp_send_ud_frag(struct sndcp_frag_state *fs,
537 uint8_t pcomp, uint8_t dcomp)
Harald Weltece22f922010-06-03 21:21:21 +0200538{
Harald Weltef78a3b22010-06-30 17:21:19 +0200539 struct gprs_sndcp_entity *sne = fs->sne;
Harald Weltece22f922010-06-03 21:21:21 +0200540 struct gprs_llc_lle *lle = sne->lle;
541 struct sndcp_common_hdr *sch;
542 struct sndcp_comp_hdr *scomph;
543 struct sndcp_udata_hdr *suh;
544 struct msgb *fmsg;
545 unsigned int max_payload_len;
546 unsigned int len;
547 uint8_t *data;
548 int rc, more;
549
Sylvain Munauteda125c2010-06-09 20:56:52 +0200550 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
Harald Weltece22f922010-06-03 21:21:21 +0200551 "SNDCP Frag");
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200552 if (!fmsg) {
553 msgb_free(fs->msg);
Harald Weltece22f922010-06-03 21:21:21 +0200554 return -ENOMEM;
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200555 }
Harald Weltece22f922010-06-03 21:21:21 +0200556
557 /* make sure lower layers route the fragment like the original */
558 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
559 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
560 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
561
562 /* prepend common SNDCP header */
563 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
564 sch->nsapi = sne->nsapi;
565 /* Set FIRST bit if we are the first fragment in a series */
566 if (fs->frag_nr == 0)
567 sch->first = 1;
568 sch->type = 1;
569
570 /* append the compression header for first fragment */
571 if (sch->first) {
572 scomph = (struct sndcp_comp_hdr *)
573 msgb_put(fmsg, sizeof(*scomph));
Philippf1f34362016-08-26 17:00:21 +0200574 scomph->pcomp = pcomp;
575 scomph->dcomp = dcomp;
Harald Weltece22f922010-06-03 21:21:21 +0200576 }
577
578 /* append the user-data header */
579 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
580 suh->npdu_low = sne->tx_npdu_nr & 0xff;
581 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
582 suh->seg_nr = fs->frag_nr % 0xf;
583
584 /* calculate remaining length to be sent */
585 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
586 /* how much payload can we actually send via LLC? */
587 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
588 if (sch->first)
589 max_payload_len -= sizeof(*scomph);
590 /* check if we're exceeding the max */
591 if (len > max_payload_len)
592 len = max_payload_len;
593
594 /* copy the actual fragment data into our fmsg */
595 data = msgb_put(fmsg, len);
596 memcpy(data, fs->next_byte, len);
597
598 /* Increment fragment number and data pointer to next fragment */
599 fs->frag_nr++;
600 fs->next_byte += len;
601
602 /* determine if we have more fragemnts to send */
603 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
604 more = 0;
605 else
606 more = 1;
607
608 /* set the MORE bit of the SNDCP header accordingly */
609 sch->more = more;
610
Max82040102016-07-06 11:59:18 +0200611 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext, true);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200612 /* abort in case of error, do not advance frag_nr / next_byte */
Harald Weltece22f922010-06-03 21:21:21 +0200613 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200614 msgb_free(fs->msg);
Harald Weltece22f922010-06-03 21:21:21 +0200615 return rc;
616 }
617
618 if (!more) {
619 /* we've sent all fragments */
620 msgb_free(fs->msg);
621 memset(fs, 0, sizeof(*fs));
622 /* increment NPDU number for next frame */
623 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
624 return 0;
625 }
626
627 /* default: more fragments to send */
628 return 1;
629}
630
Harald Weltedb2c39f2010-06-03 07:14:59 +0200631/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Harald Weltebb1c8052010-06-03 06:38:38 +0200632int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
633 void *mmcontext)
634{
Harald Weltef78a3b22010-06-30 17:21:19 +0200635 struct gprs_sndcp_entity *sne;
Harald Weltebb1c8052010-06-03 06:38:38 +0200636 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200637 struct sndcp_comp_hdr *scomph;
Harald Weltebb1c8052010-06-03 06:38:38 +0200638 struct sndcp_udata_hdr *suh;
Harald Weltece22f922010-06-03 21:21:21 +0200639 struct sndcp_frag_state fs;
Philippf1f34362016-08-26 17:00:21 +0200640 uint8_t pcomp = 0;
641 uint8_t dcomp = 0;
642 int rc;
Harald Weltebb1c8052010-06-03 06:38:38 +0200643
644 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
645
Philippf1f34362016-08-26 17:00:21 +0200646 /* Compress packet */
647#if DEBUG_IP_PACKETS == 1
648 DEBUGP(DSNDCP, " \n");
649 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
650 DEBUGP(DSNDCP, "===================================================\n");
651 debug_ip_packet(msg->data, msg->len, 0, "sndcp_initdata_req()");
652#endif
653 if (any_pcomp_or_dcomp_active(sgsn)) {
654
655 /* Apply header compression */
656 rc = gprs_sndcp_pcomp_compress(msg->data, msg->len, &pcomp,
657 lle->llme->comp.proto, nsapi);
658 if (rc < 0) {
659 LOGP(DSNDCP, LOGL_ERROR,
660 "TCP/IP Header compression failed!\n");
661 return -EIO;
662 }
663
664 /* Fixup pointer locations and sizes in message buffer to match
665 * the new, compressed buffer size */
666 msgb_get(msg, msg->len);
667 msgb_put(msg, rc);
Philipp73f83d52016-09-02 13:38:01 +0200668
669 /* Apply data compression */
670 rc = gprs_sndcp_dcomp_compress(msg->data, msg->len, &dcomp,
671 lle->llme->comp.data, nsapi);
672 if (rc < 0) {
673 LOGP(DSNDCP, LOGL_ERROR, "Data compression failed!\n");
674 return -EIO;
675 }
676
677 /* Fixup pointer locations and sizes in message buffer to match
678 * the new, compressed buffer size */
679 msgb_get(msg, msg->len);
680 msgb_put(msg, rc);
Philippf1f34362016-08-26 17:00:21 +0200681 }
682#if DEBUG_IP_PACKETS == 1
683 DEBUGP(DSNDCP, "===================================================\n");
684 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
685 DEBUGP(DSNDCP, " \n");
686#endif
687
Harald Weltef78a3b22010-06-30 17:21:19 +0200688 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltebb1c8052010-06-03 06:38:38 +0200689 if (!sne) {
690 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200691 msgb_free(msg);
Harald Weltebb1c8052010-06-03 06:38:38 +0200692 return -EIO;
693 }
694
Harald Weltece22f922010-06-03 21:21:21 +0200695 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
696 if (msg->len > lle->params.n201_u -
697 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
698 /* initialize the fragmenter state */
699 fs.msg = msg;
700 fs.frag_nr = 0;
701 fs.next_byte = msg->data;
702 fs.sne = sne;
703 fs.mmcontext = mmcontext;
704
705 /* call function to generate and send fragments until all
706 * of the N-PDU has been sent */
707 while (1) {
Philippf1f34362016-08-26 17:00:21 +0200708 int rc = sndcp_send_ud_frag(&fs,pcomp,dcomp);
Harald Weltece22f922010-06-03 21:21:21 +0200709 if (rc == 0)
710 return 0;
711 if (rc < 0)
712 return rc;
713 }
714 /* not reached */
715 return 0;
716 }
717
718 /* this is the non-fragmenting case where we only build 1 SN-PDU */
719
Harald Weltebb1c8052010-06-03 06:38:38 +0200720 /* prepend the user-data header */
721 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Weltece22f922010-06-03 21:21:21 +0200722 suh->npdu_low = sne->tx_npdu_nr & 0xff;
723 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
724 suh->seg_nr = 0;
725 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
726
727 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
Philippf1f34362016-08-26 17:00:21 +0200728 scomph->pcomp = pcomp;
729 scomph->dcomp = dcomp;
Harald Weltebb1c8052010-06-03 06:38:38 +0200730
731 /* prepend common SNDCP header */
732 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
733 sch->first = 1;
734 sch->type = 1;
735 sch->nsapi = nsapi;
736
Max82040102016-07-06 11:59:18 +0200737 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext, true);
Harald Weltebb1c8052010-06-03 06:38:38 +0200738}
739
Harald Welteebabdea2010-06-01 18:28:10 +0200740/* Section 5.1.2.17 LL-UNITDATA.ind */
Harald Welte36f12172010-07-02 16:44:24 +0200741int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle,
742 uint8_t *hdr, uint16_t len)
Harald Welteebabdea2010-06-01 18:28:10 +0200743{
Harald Weltef78a3b22010-06-30 17:21:19 +0200744 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200745 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Weltece22f922010-06-03 21:21:21 +0200746 struct sndcp_comp_hdr *scomph = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200747 struct sndcp_udata_hdr *suh;
Harald Welte16836a32010-06-02 10:25:40 +0200748 uint8_t *npdu;
Holger Hans Peter Freythercfee9522014-04-04 12:43:08 +0200749 uint16_t npdu_num __attribute__((unused));
Harald Welteebabdea2010-06-01 18:28:10 +0200750 int npdu_len;
Philippf1f34362016-08-26 17:00:21 +0200751 int rc;
752 uint8_t *expnd = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200753
Harald Weltece22f922010-06-03 21:21:21 +0200754 sch = (struct sndcp_common_hdr *) hdr;
755 if (sch->first) {
756 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
757 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
758 } else
759 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
760
Harald Welteebabdea2010-06-01 18:28:10 +0200761 if (sch->type == 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200762 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Welte96f71f22010-05-03 19:28:05 +0200763 return -EINVAL;
764 }
765
Harald Welte16836a32010-06-02 10:25:40 +0200766 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200767 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welteebabdea2010-06-01 18:28:10 +0200768 return -EIO;
769 }
770
Harald Weltef78a3b22010-06-30 17:21:19 +0200771 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200772 if (!sne) {
Harald Welte69996cb2010-06-02 10:26:19 +0200773 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welte61444522010-06-02 12:40:48 +0200774 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
775 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200776 return -EIO;
777 }
Harald Welte8911cef2010-07-01 19:56:19 +0200778 /* FIXME: move this RA_ID up to the LLME or even higher */
779 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
Harald Welteebabdea2010-06-01 18:28:10 +0200780
Harald Welte7e5bb622016-09-28 08:20:58 +0800781 if (scomph) {
Philippf1f34362016-08-26 17:00:21 +0200782 sne->defrag.pcomp = scomph->pcomp;
783 sne->defrag.dcomp = scomph->dcomp;
784 sne->defrag.proto = lle->llme->comp.proto;
785 sne->defrag.data = lle->llme->comp.data;
786 }
787
Harald Welteab4094c2010-07-02 16:01:47 +0200788 /* any non-first segment is by definition something to defragment
789 * as is any segment that tells us there are more segments */
790 if (!sch->first || sch->more)
Harald Welte60da7d42010-07-02 15:45:12 +0200791 return defrag_input(sne, msg, hdr, len);
Harald Welteebabdea2010-06-01 18:28:10 +0200792
Harald Welte16836a32010-06-02 10:25:40 +0200793 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200794 npdu = (uint8_t *)suh + sizeof(*suh);
Alexander Couzens410bc9b2018-09-18 20:01:28 +0200795 npdu_len = (msg->data + msg->len) - npdu;
Philippf1f34362016-08-26 17:00:21 +0200796
Harald Welte61444522010-06-02 12:40:48 +0200797 if (npdu_len <= 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200798 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200799 return -EIO;
800 }
801 /* actually send the N-PDU to the SGSN core code, which then
802 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Philippf1f34362016-08-26 17:00:21 +0200803
804 /* Decompress packet */
805#if DEBUG_IP_PACKETS == 1
806 DEBUGP(DSNDCP, " \n");
807 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
808 DEBUGP(DSNDCP, "===================================================\n");
809#endif
810 if (any_pcomp_or_dcomp_active(sgsn)) {
811
Philipp73f83d52016-09-02 13:38:01 +0200812 expnd = talloc_zero_size(msg, npdu_len * MAX_DATADECOMPR_FAC +
813 MAX_HDRDECOMPR_INCR);
Philippf1f34362016-08-26 17:00:21 +0200814 memcpy(expnd, npdu, npdu_len);
815
Philipp73f83d52016-09-02 13:38:01 +0200816 /* Apply data decompression */
817 rc = gprs_sndcp_dcomp_expand(expnd, npdu_len, sne->defrag.dcomp,
818 sne->defrag.data);
819 if (rc < 0) {
820 LOGP(DSNDCP, LOGL_ERROR,
821 "Data decompression failed!\n");
822 talloc_free(expnd);
823 return -EIO;
824 }
825
Philippf1f34362016-08-26 17:00:21 +0200826 /* Apply header decompression */
Philipp73f83d52016-09-02 13:38:01 +0200827 rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp,
Philippf1f34362016-08-26 17:00:21 +0200828 sne->defrag.proto);
829 if (rc < 0) {
830 LOGP(DSNDCP, LOGL_ERROR,
831 "TCP/IP Header decompression failed!\n");
832 talloc_free(expnd);
833 return -EIO;
834 }
835
836 /* Modify npu length, expnd is handed directly handed
837 * over to gsn_rx_sndcp_ud_ind(), see below */
838 npdu_len = rc;
839 } else
840 expnd = npdu;
841#if DEBUG_IP_PACKETS == 1
842 debug_ip_packet(expnd, npdu_len, 1, "sndcp_llunitdata_ind()");
843 DEBUGP(DSNDCP, "===================================================\n");
844 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
845 DEBUGP(DSNDCP, " \n");
846#endif
847
848 /* Hand off packet to gtp */
849 rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, lle->llme->tlli,
850 sne->nsapi, msg, npdu_len, expnd);
851
852 if (any_pcomp_or_dcomp_active(sgsn))
853 talloc_free(expnd);
854
855 return rc;
Harald Welte96f71f22010-05-03 19:28:05 +0200856}
857
Holger Hans Peter Freythercfee9522014-04-04 12:43:08 +0200858#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200859/* Section 5.1.2.1 LL-RESET.ind */
Harald Weltef78a3b22010-06-30 17:21:19 +0200860static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200861{
862 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
863 /* reset all SNDCP XID parameters to default values */
Holger Hans Peter Freyther6142dc42011-10-14 23:37:27 +0200864 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
865 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200866}
867
Harald Welte2720e732010-05-17 00:44:57 +0200868static int sndcp_ll_status_ind()
869{
870 /* inform the SM sub-layer by means of SNSM-STATUS.req */
Holger Hans Peter Freyther6142dc42011-10-14 23:37:27 +0200871 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
872 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200873}
874
875static struct sndcp_state_list {{
876 uint32_t states;
877 unsigned int type;
Harald Weltef78a3b22010-06-30 17:21:19 +0200878 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
Harald Welte2720e732010-05-17 00:44:57 +0200879} sndcp_state_list[] = {
880 { ALL_STATES,
881 LL_RESET_IND, sndcp_ll_reset_ind },
882 { ALL_STATES,
883 LL_ESTABLISH_IND, sndcp_ll_est_ind },
884 { SBIT(SNDCP_S_EST_RQD),
885 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
886 { SBIT(SNDCP_S_EST_RQD),
887 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
888 { SBIT(SNDCP_S_
889};
890
891static int sndcp_rx_llc_prim()
892{
893 case LL_ESTABLISH_REQ:
894 case LL_RELEASE_REQ:
895 case LL_XID_REQ:
896 case LL_DATA_REQ:
897 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
898
899 switch (prim) {
900 case LL_RESET_IND:
901 case LL_ESTABLISH_IND:
902 case LL_ESTABLISH_RESP:
903 case LL_ESTABLISH_CONF:
904 case LL_RELEASE_IND:
905 case LL_RELEASE_CONF:
906 case LL_XID_IND:
907 case LL_XID_RESP:
908 case LL_XID_CONF:
909 case LL_DATA_IND:
910 case LL_DATA_CONF:
911 case LL_UNITDATA_IND:
912 case LL_STATUS_IND:
Neels Hofmeyrcc7db182016-12-18 23:52:38 +0100913 }
Harald Welte2720e732010-05-17 00:44:57 +0200914}
Harald Welteebabdea2010-06-01 18:28:10 +0200915#endif
Philippf1f34362016-08-26 17:00:21 +0200916
917/* Generate SNDCP-XID message */
918static int gprs_llc_gen_sndcp_xid(uint8_t *bytes, int bytes_len, uint8_t nsapi)
919{
920 int entity = 0;
921 LLIST_HEAD(comp_fields);
922 struct gprs_sndcp_pcomp_rfc1144_params rfc1144_params;
923 struct gprs_sndcp_comp_field rfc1144_comp_field;
Philipp73f83d52016-09-02 13:38:01 +0200924 struct gprs_sndcp_dcomp_v42bis_params v42bis_params;
925 struct gprs_sndcp_comp_field v42bis_comp_field;
Philippf1f34362016-08-26 17:00:21 +0200926
927 memset(&rfc1144_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
Philipp73f83d52016-09-02 13:38:01 +0200928 memset(&v42bis_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
Philippf1f34362016-08-26 17:00:21 +0200929
930 /* Setup rfc1144 */
931 if (sgsn->cfg.pcomp_rfc1144.active) {
932 rfc1144_params.nsapi[0] = nsapi;
933 rfc1144_params.nsapi_len = 1;
934 rfc1144_params.s01 = sgsn->cfg.pcomp_rfc1144.s01;
935 rfc1144_comp_field.p = 1;
936 rfc1144_comp_field.entity = entity;
937 rfc1144_comp_field.algo = RFC_1144;
938 rfc1144_comp_field.comp[RFC1144_PCOMP1] = 1;
939 rfc1144_comp_field.comp[RFC1144_PCOMP2] = 2;
940 rfc1144_comp_field.comp_len = RFC1144_PCOMP_NUM;
941 rfc1144_comp_field.rfc1144_params = &rfc1144_params;
942 entity++;
943 llist_add(&rfc1144_comp_field.list, &comp_fields);
944 }
945
Philipp73f83d52016-09-02 13:38:01 +0200946 /* Setup V.42bis */
947 if (sgsn->cfg.dcomp_v42bis.active) {
948 v42bis_params.nsapi[0] = nsapi;
949 v42bis_params.nsapi_len = 1;
950 v42bis_params.p0 = sgsn->cfg.dcomp_v42bis.p0;
951 v42bis_params.p1 = sgsn->cfg.dcomp_v42bis.p1;
952 v42bis_params.p2 = sgsn->cfg.dcomp_v42bis.p2;
953 v42bis_comp_field.p = 1;
954 v42bis_comp_field.entity = entity;
955 v42bis_comp_field.algo = V42BIS;
956 v42bis_comp_field.comp[V42BIS_DCOMP1] = 1;
957 v42bis_comp_field.comp_len = V42BIS_DCOMP_NUM;
958 v42bis_comp_field.v42bis_params = &v42bis_params;
959 entity++;
960 llist_add(&v42bis_comp_field.list, &comp_fields);
961 }
962
Philippdb142dc2016-12-22 14:15:20 +0100963 /* Do not attempt to compile anything if there is no data in the list */
964 if (llist_empty(&comp_fields))
965 return 0;
966
Philippf1f34362016-08-26 17:00:21 +0200967 /* Compile bytestream */
Philippdb142dc2016-12-22 14:15:20 +0100968 return gprs_sndcp_compile_xid(bytes, bytes_len, &comp_fields,
969 DEFAULT_SNDCP_VERSION);
Philippf1f34362016-08-26 17:00:21 +0200970}
971
972/* Set of SNDCP-XID bnegotiation (See also: TS 144 065,
973 * Section 6.8 XID parameter negotiation) */
974int sndcp_sn_xid_req(struct gprs_llc_lle *lle, uint8_t nsapi)
975{
976 /* Note: The specification requires the SNDCP-User to set of an
977 * SNDCP xid request. See also 3GPP TS 44.065, 6.8 XID parameter
978 * negotiation, Figure 11: SNDCP XID negotiation procedure. In
979 * our case the SNDCP-User is sgsn_libgtp.c, which calls
980 * sndcp_sn_xid_req directly. */
981
982 uint8_t l3params[1024];
983 int xid_len;
984 struct gprs_llc_xid_field xid_field_request;
985
986 /* Wipe off all compression entities and their states to
987 * get rid of possible leftovers from a previous session */
988 gprs_sndcp_comp_free(lle->llme->comp.proto);
989 gprs_sndcp_comp_free(lle->llme->comp.data);
990 lle->llme->comp.proto = gprs_sndcp_comp_alloc(lle->llme);
991 lle->llme->comp.data = gprs_sndcp_comp_alloc(lle->llme);
992 talloc_free(lle->llme->xid);
993 lle->llme->xid = NULL;
994
995 /* Generate compression parameter bytestream */
996 xid_len = gprs_llc_gen_sndcp_xid(l3params, sizeof(l3params), nsapi);
997
998 /* Send XID with the SNDCP-XID bytetsream included */
999 if (xid_len > 0) {
1000 xid_field_request.type = GPRS_LLC_XID_T_L3_PAR;
1001 xid_field_request.data = l3params;
1002 xid_field_request.data_len = xid_len;
1003 return gprs_ll_xid_req(lle, &xid_field_request);
1004 }
1005
1006 /* When bytestream can not be generated, proceed without SNDCP-XID */
1007 return gprs_ll_xid_req(lle, NULL);
1008
1009}
1010
1011/* Handle header compression entites */
1012static int handle_pcomp_entities(struct gprs_sndcp_comp_field *comp_field,
1013 struct gprs_llc_lle *lle)
1014{
1015 /* Note: This functions also transforms the comp_field into its
1016 * echo form (strips comp values, resets propose bit etc...)
1017 * the processed comp_fields can then be sent back as XID-
1018 * Response without further modification. */
1019
1020 /* Delete propose bit */
1021 comp_field->p = 0;
1022
1023 /* Process proposed parameters */
1024 switch (comp_field->algo) {
1025 case RFC_1144:
1026 if (sgsn->cfg.pcomp_rfc1144.passive
1027 && comp_field->rfc1144_params->nsapi_len > 0) {
1028 DEBUGP(DSNDCP,
1029 "Accepting RFC1144 header compression...\n");
1030 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.proto,
1031 comp_field);
1032 } else {
1033 DEBUGP(DSNDCP,
1034 "Rejecting RFC1144 header compression...\n");
1035 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1036 comp_field->entity);
1037 comp_field->rfc1144_params->nsapi_len = 0;
1038 }
1039 break;
1040 case RFC_2507:
1041 /* RFC 2507 is not yet supported,
1042 * so we set applicable nsapis to zero */
1043 DEBUGP(DSNDCP, "Rejecting RFC2507 header compression...\n");
1044 comp_field->rfc2507_params->nsapi_len = 0;
1045 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1046 comp_field->entity);
1047 break;
1048 case ROHC:
1049 /* ROHC is not yet supported,
1050 * so we set applicable nsapis to zero */
1051 DEBUGP(DSNDCP, "Rejecting ROHC header compression...\n");
1052 comp_field->rohc_params->nsapi_len = 0;
1053 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1054 comp_field->entity);
1055 break;
1056 }
1057
1058 return 0;
1059}
1060
1061/* Hanle data compression entites */
1062static int handle_dcomp_entities(struct gprs_sndcp_comp_field *comp_field,
1063 struct gprs_llc_lle *lle)
1064{
1065 /* See note in handle_pcomp_entities() */
1066
1067 /* Delete propose bit */
1068 comp_field->p = 0;
1069
1070 /* Process proposed parameters */
1071 switch (comp_field->algo) {
1072 case V42BIS:
Philipp73f83d52016-09-02 13:38:01 +02001073 if (sgsn->cfg.dcomp_v42bis.passive &&
1074 comp_field->v42bis_params->nsapi_len > 0) {
1075 DEBUGP(DSNDCP,
1076 "Accepting V.42bis data compression...\n");
1077 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.data,
1078 comp_field);
1079 } else {
1080 LOGP(DSNDCP, LOGL_DEBUG,
1081 "Rejecting V.42bis data compression...\n");
1082 gprs_sndcp_comp_delete(lle->llme->comp.data,
1083 comp_field->entity);
1084 comp_field->v42bis_params->nsapi_len = 0;
1085 }
Philippf1f34362016-08-26 17:00:21 +02001086 break;
1087 case V44:
1088 /* V44 is not yet supported,
1089 * so we set applicable nsapis to zero */
1090 DEBUGP(DSNDCP, "Rejecting V.44 data compression...\n");
1091 comp_field->v44_params->nsapi_len = 0;
1092 gprs_sndcp_comp_delete(lle->llme->comp.data,
1093 comp_field->entity);
1094 break;
1095 }
1096
1097 return 0;
1098
1099}
1100
1101/* Process SNDCP-XID indication
1102 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1103int sndcp_sn_xid_ind(struct gprs_llc_xid_field *xid_field_indication,
1104 struct gprs_llc_xid_field *xid_field_response,
1105 struct gprs_llc_lle *lle)
1106{
1107 /* Note: This function computes the SNDCP-XID response that is sent
1108 * back to the ms when a ms originated XID is received. The
1109 * Input XID fields are directly processed and the result is directly
1110 * handed back. */
1111
1112 int rc;
1113 int compclass;
Philippdb142dc2016-12-22 14:15:20 +01001114 int version;
Philippf1f34362016-08-26 17:00:21 +02001115
1116 struct llist_head *comp_fields;
1117 struct gprs_sndcp_comp_field *comp_field;
1118
1119 OSMO_ASSERT(xid_field_indication);
1120 OSMO_ASSERT(xid_field_response);
1121 OSMO_ASSERT(lle);
1122
1123 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001124 comp_fields = gprs_sndcp_parse_xid(&version, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001125 xid_field_indication->data,
1126 xid_field_indication->data_len,
1127 NULL);
1128 if (!comp_fields)
1129 return -EINVAL;
1130
Philippf1f34362016-08-26 17:00:21 +02001131 /* Handle compression entites */
1132 DEBUGP(DSNDCP, "SNDCP-XID-IND (ms):\n");
1133 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1134
1135 llist_for_each_entry(comp_field, comp_fields, list) {
1136 compclass = gprs_sndcp_get_compression_class(comp_field);
1137 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1138 rc = handle_pcomp_entities(comp_field, lle);
1139 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1140 rc = handle_dcomp_entities(comp_field, lle);
1141 else {
1142 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1143 comp_field->entity);
1144 gprs_sndcp_comp_delete(lle->llme->comp.data,
1145 comp_field->entity);
1146 rc = 0;
1147 }
1148
1149 if (rc < 0) {
1150 talloc_free(comp_fields);
1151 return -EINVAL;
1152 }
1153 }
1154
1155 DEBUGP(DSNDCP, "SNDCP-XID-RES (sgsn):\n");
1156 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1157
1158 /* Reserve some memory to store the modified SNDCP-XID bytes */
1159 xid_field_response->data =
1160 talloc_zero_size(lle->llme, xid_field_indication->data_len);
1161
1162 /* Set Type flag for response */
1163 xid_field_response->type = GPRS_LLC_XID_T_L3_PAR;
1164
1165 /* Compile modified SNDCP-XID bytes */
1166 rc = gprs_sndcp_compile_xid(xid_field_response->data,
1167 xid_field_indication->data_len,
Philippdb142dc2016-12-22 14:15:20 +01001168 comp_fields, 0);
Philippf1f34362016-08-26 17:00:21 +02001169
1170 if (rc > 0)
1171 xid_field_response->data_len = rc;
1172 else {
1173 talloc_free(xid_field_response->data);
1174 xid_field_response->data = NULL;
1175 xid_field_response->data_len = 0;
1176 return -EINVAL;
1177 }
1178
1179 talloc_free(comp_fields);
1180
1181 return 0;
1182}
1183
1184/* Process SNDCP-XID indication
1185 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1186int sndcp_sn_xid_conf(struct gprs_llc_xid_field *xid_field_conf,
1187 struct gprs_llc_xid_field *xid_field_request,
1188 struct gprs_llc_lle *lle)
1189{
1190 /* Note: This function handles an incomming SNDCP-XID confirmiation.
1191 * Since the confirmation fields may lack important parameters we
1192 * will reconstruct these missing fields using the original request
1193 * we have sent. After that we will create (or delete) the
1194 * compression entites */
1195
1196 struct llist_head *comp_fields_req;
1197 struct llist_head *comp_fields_conf;
1198 struct gprs_sndcp_comp_field *comp_field;
1199 int rc;
1200 int compclass;
1201
1202 /* We need both, the confirmation that is sent back by the ms,
1203 * and the original request we have sent. If one of this is missing
1204 * we can not process the confirmation, the caller must check if
1205 * request and confirmation fields are available. */
1206 OSMO_ASSERT(xid_field_conf);
1207 OSMO_ASSERT(xid_field_request);
1208
1209 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001210 comp_fields_req = gprs_sndcp_parse_xid(NULL, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001211 xid_field_request->data,
1212 xid_field_request->data_len,
1213 NULL);
1214 if (!comp_fields_req)
1215 return -EINVAL;
1216
1217 DEBUGP(DSNDCP, "SNDCP-XID-REQ (sgsn):\n");
1218 gprs_sndcp_dump_comp_fields(comp_fields_req, LOGL_DEBUG);
1219
1220 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001221 comp_fields_conf = gprs_sndcp_parse_xid(NULL, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001222 xid_field_conf->data,
1223 xid_field_conf->data_len,
1224 comp_fields_req);
1225 if (!comp_fields_conf)
1226 return -EINVAL;
1227
1228 DEBUGP(DSNDCP, "SNDCP-XID-CONF (ms):\n");
1229 gprs_sndcp_dump_comp_fields(comp_fields_conf, LOGL_DEBUG);
1230
1231 /* Handle compression entites */
1232 llist_for_each_entry(comp_field, comp_fields_conf, list) {
1233 compclass = gprs_sndcp_get_compression_class(comp_field);
1234 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1235 rc = handle_pcomp_entities(comp_field, lle);
1236 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1237 rc = handle_dcomp_entities(comp_field, lle);
1238 else {
1239 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1240 comp_field->entity);
1241 gprs_sndcp_comp_delete(lle->llme->comp.data,
1242 comp_field->entity);
1243 rc = 0;
1244 }
1245
1246 if (rc < 0) {
1247 talloc_free(comp_fields_req);
1248 talloc_free(comp_fields_conf);
1249 return -EINVAL;
1250 }
1251 }
1252
1253 talloc_free(comp_fields_req);
1254 talloc_free(comp_fields_conf);
1255
1256 return 0;
1257}