blob: d8982accb110a389e70d8b776cb50224fb68f217 [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>
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +010031#include <osmocom/core/endian.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080032#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte96f71f22010-05-03 19:28:05 +020033
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020034#include <osmocom/sgsn/debug.h>
Alexander Couzensa8f78252019-09-16 02:44:58 +020035#include <osmocom/sgsn/gprs_gb.h>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020036#include <osmocom/sgsn/gprs_llc.h>
37#include <osmocom/sgsn/sgsn.h>
38#include <osmocom/sgsn/gprs_sndcp.h>
39#include <osmocom/sgsn/gprs_llc_xid.h>
40#include <osmocom/sgsn/gprs_sndcp_xid.h>
41#include <osmocom/sgsn/gprs_sndcp_pcomp.h>
42#include <osmocom/sgsn/gprs_sndcp_dcomp.h>
43#include <osmocom/sgsn/gprs_sndcp_comp.h>
Philippf1f34362016-08-26 17:00:21 +020044
45#define DEBUG_IP_PACKETS 0 /* 0=Disabled, 1=Enabled */
46
47#if DEBUG_IP_PACKETS == 1
48/* Calculate TCP/IP checksum */
49static uint16_t calc_ip_csum(uint8_t *data, int len)
50{
51 int i;
52 uint32_t accumulator = 0;
53 uint16_t *pointer = (uint16_t *) data;
54
55 for (i = len; i > 1; i -= 2) {
56 accumulator += *pointer;
57 pointer++;
58 }
59
60 if (len % 2)
61 accumulator += *pointer;
62
63 accumulator = (accumulator & 0xffff) + ((accumulator >> 16) & 0xffff);
64 accumulator += (accumulator >> 16) & 0xffff;
65 return (~accumulator);
66}
67
68/* Calculate TCP/IP checksum */
69static uint16_t calc_tcpip_csum(const void *ctx, uint8_t *packet, int len)
70{
71 uint8_t *buf;
72 uint16_t csum;
73
74 buf = talloc_zero_size(ctx, len);
75 memset(buf, 0, len);
76 memcpy(buf, packet + 12, 8);
77 buf[9] = packet[9];
78 buf[11] = (len - 20) & 0xFF;
79 buf[10] = (len - 20) >> 8 & 0xFF;
80 memcpy(buf + 12, packet + 20, len - 20);
81 csum = calc_ip_csum(buf, len - 20 + 12);
82 talloc_free(buf);
83 return csum;
84}
85
86/* Show some ip packet details */
87static void debug_ip_packet(uint8_t *data, int len, int dir, char *info)
88{
89 uint8_t tcp_flags;
90 char flags_debugmsg[256];
91 int len_short;
92 static unsigned int packet_count = 0;
93 static unsigned int tcp_csum_err_count = 0;
94 static unsigned int ip_csum_err_count = 0;
95
96 packet_count++;
97
98 if (len > 80)
99 len_short = 80;
100 else
101 len_short = len;
102
103 if (dir)
104 DEBUGP(DSNDCP, "%s: MS => SGSN: %s\n", info,
105 osmo_hexdump_nospc(data, len_short));
106 else
107 DEBUGP(DSNDCP, "%s: MS <= SGSN: %s\n", info,
108 osmo_hexdump_nospc(data, len_short));
109
110 DEBUGP(DSNDCP, "%s: Length.: %d\n", info, len);
111 DEBUGP(DSNDCP, "%s: NO.: %d\n", info, packet_count);
112
113 if (len < 20) {
114 DEBUGP(DSNDCP, "%s: Error: Short IP packet!\n", info);
115 return;
116 }
117
118 if (calc_ip_csum(data, 20) != 0) {
119 DEBUGP(DSNDCP, "%s: Bad IP-Header checksum!\n", info);
120 ip_csum_err_count++;
121 } else
122 DEBUGP(DSNDCP, "%s: IP-Header checksum ok.\n", info);
123
124 if (data[9] == 0x06) {
125 if (len < 40) {
126 DEBUGP(DSNDCP, "%s: Error: Short TCP packet!\n", info);
127 return;
128 }
129
130 DEBUGP(DSNDCP, "%s: Protocol type: TCP\n", info);
131 tcp_flags = data[33];
132
133 if (calc_tcpip_csum(NULL, data, len) != 0) {
134 DEBUGP(DSNDCP, "%s: Bad TCP checksum!\n", info);
135 tcp_csum_err_count++;
136 } else
137 DEBUGP(DSNDCP, "%s: TCP checksum ok.\n", info);
138
139 memset(flags_debugmsg, 0, sizeof(flags_debugmsg));
140 if (tcp_flags & 1)
141 strcat(flags_debugmsg, "FIN ");
142 if (tcp_flags & 2)
143 strcat(flags_debugmsg, "SYN ");
144 if (tcp_flags & 4)
145 strcat(flags_debugmsg, "RST ");
146 if (tcp_flags & 8)
147 strcat(flags_debugmsg, "PSH ");
148 if (tcp_flags & 16)
149 strcat(flags_debugmsg, "ACK ");
150 if (tcp_flags & 32)
151 strcat(flags_debugmsg, "URG ");
152 DEBUGP(DSNDCP, "%s: FLAGS: %s\n", info, flags_debugmsg);
153 } else if (data[9] == 0x11) {
154 DEBUGP(DSNDCP, "%s: Protocol type: UDP\n", info);
155 } else {
156 DEBUGP(DSNDCP, "%s: Protocol type: (%02x)\n", info, data[9]);
157 }
158
159 DEBUGP(DSNDCP, "%s: IP-Header checksum errors: %d\n", info,
160 ip_csum_err_count);
161 DEBUGP(DSNDCP, "%s: TCP-Checksum errors: %d\n", info,
162 tcp_csum_err_count);
163}
164#endif
Harald Weltef78a3b22010-06-30 17:21:19 +0200165
Harald Welte96f71f22010-05-03 19:28:05 +0200166/* Chapter 7.2: SN-PDU Formats */
167struct sndcp_common_hdr {
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100168#if OSMO_IS_LITTLE_ENDIAN
Harald Welte96f71f22010-05-03 19:28:05 +0200169 /* octet 1 */
170 uint8_t nsapi:4;
171 uint8_t more:1;
172 uint8_t type:1;
173 uint8_t first:1;
174 uint8_t spare:1;
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100175#elif OSMO_IS_BIG_ENDIAN
176/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
177 uint8_t spare:1, first:1, type:1, more:1, nsapi:4;
178#endif
Harald Weltece22f922010-06-03 21:21:21 +0200179} __attribute__((packed));
180
181/* PCOMP / DCOMP only exist in first fragment */
182struct sndcp_comp_hdr {
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100183#if OSMO_IS_LITTLE_ENDIAN
Harald Welte96f71f22010-05-03 19:28:05 +0200184 /* octet 2 */
Harald Welte5cc2bc32010-06-02 23:17:05 +0200185 uint8_t pcomp:4;
186 uint8_t dcomp:4;
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100187#elif OSMO_IS_BIG_ENDIAN
188/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
189 uint8_t dcomp:4, pcomp:4;
190#endif
Harald Welteebabdea2010-06-01 18:28:10 +0200191} __attribute__((packed));
Harald Welte96f71f22010-05-03 19:28:05 +0200192
193struct sndcp_udata_hdr {
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100194#if OSMO_IS_LITTLE_ENDIAN
Harald Welte96f71f22010-05-03 19:28:05 +0200195 /* octet 3 */
196 uint8_t npdu_high:4;
197 uint8_t seg_nr:4;
198 /* octet 4 */
199 uint8_t npdu_low;
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100200#elif OSMO_IS_BIG_ENDIAN
201/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
202 uint8_t seg_nr:4, npdu_high:4;
203 uint8_t npdu_low;
204#endif
Harald Welteebabdea2010-06-01 18:28:10 +0200205} __attribute__((packed));
206
Harald Welteebabdea2010-06-01 18:28:10 +0200207
208static void *tall_sndcp_ctx;
209
210/* A fragment queue entry, containing one framgent of a N-PDU */
Harald Weltece22f922010-06-03 21:21:21 +0200211struct defrag_queue_entry {
Harald Welteebabdea2010-06-01 18:28:10 +0200212 struct llist_head list;
Harald Weltece22f922010-06-03 21:21:21 +0200213 /* segment number of this fragment */
214 uint32_t seg_nr;
215 /* length of the data area of this fragment */
Harald Welteebabdea2010-06-01 18:28:10 +0200216 uint32_t data_len;
Harald Weltece22f922010-06-03 21:21:21 +0200217 /* pointer to the data of this fragment */
218 uint8_t *data;
Harald Welteebabdea2010-06-01 18:28:10 +0200219};
220
Harald Weltef78a3b22010-06-30 17:21:19 +0200221LLIST_HEAD(gprs_sndcp_entities);
Harald Welte96f71f22010-05-03 19:28:05 +0200222
Philippf1f34362016-08-26 17:00:21 +0200223/* Check if any compression parameters are set in the sgsn configuration */
224static inline int any_pcomp_or_dcomp_active(struct sgsn_instance *sgsn) {
Philipp73f83d52016-09-02 13:38:01 +0200225 if (sgsn->cfg.pcomp_rfc1144.active || sgsn->cfg.pcomp_rfc1144.passive ||
226 sgsn->cfg.dcomp_v42bis.active || sgsn->cfg.dcomp_v42bis.passive)
Philippf1f34362016-08-26 17:00:21 +0200227 return true;
228 else
229 return false;
230}
231
Harald Weltece22f922010-06-03 21:21:21 +0200232/* Enqueue a fragment into the defragment queue */
Harald Weltef78a3b22010-06-30 17:21:19 +0200233static int defrag_enqueue(struct gprs_sndcp_entity *sne, uint8_t seg_nr,
Harald Welte3d6815a2010-07-02 17:16:07 +0200234 uint8_t *data, uint32_t data_len)
Harald Welteebabdea2010-06-01 18:28:10 +0200235{
Harald Weltece22f922010-06-03 21:21:21 +0200236 struct defrag_queue_entry *dqe;
Harald Welteebabdea2010-06-01 18:28:10 +0200237
Harald Weltece22f922010-06-03 21:21:21 +0200238 dqe = talloc_zero(tall_sndcp_ctx, struct defrag_queue_entry);
239 if (!dqe)
240 return -ENOMEM;
241 dqe->data = talloc_zero_size(dqe, data_len);
242 if (!dqe->data) {
243 talloc_free(dqe);
244 return -ENOMEM;
245 }
246 dqe->seg_nr = seg_nr;
247 dqe->data_len = data_len;
248
249 llist_add(&dqe->list, &sne->defrag.frag_list);
250
251 if (seg_nr > sne->defrag.highest_seg)
252 sne->defrag.highest_seg = seg_nr;
253
254 sne->defrag.seg_have |= (1 << seg_nr);
255 sne->defrag.tot_len += data_len;
256
Harald Welte8f0c0a32010-07-02 10:29:06 +0200257 memcpy(dqe->data, data, data_len);
258
Harald Weltece22f922010-06-03 21:21:21 +0200259 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200260}
261
Harald Weltece22f922010-06-03 21:21:21 +0200262/* return if we have all segments of this N-PDU */
Harald Weltef78a3b22010-06-30 17:21:19 +0200263static int defrag_have_all_segments(struct gprs_sndcp_entity *sne)
Harald Welteebabdea2010-06-01 18:28:10 +0200264{
Harald Weltece22f922010-06-03 21:21:21 +0200265 uint32_t seg_needed = 0;
266 unsigned int i;
Harald Welteebabdea2010-06-01 18:28:10 +0200267
Harald Weltece22f922010-06-03 21:21:21 +0200268 /* create a bitmask of needed segments */
Harald Welte951a12c2010-07-01 15:09:45 +0200269 for (i = 0; i <= sne->defrag.highest_seg; i++)
Harald Weltece22f922010-06-03 21:21:21 +0200270 seg_needed |= (1 << i);
271
272 if (seg_needed == sne->defrag.seg_have)
273 return 1;
274
275 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200276}
277
Harald Weltef78a3b22010-06-30 17:21:19 +0200278static struct defrag_queue_entry *defrag_get_seg(struct gprs_sndcp_entity *sne,
Harald Weltece22f922010-06-03 21:21:21 +0200279 uint32_t seg_nr)
Harald Welteebabdea2010-06-01 18:28:10 +0200280{
Harald Weltece22f922010-06-03 21:21:21 +0200281 struct defrag_queue_entry *dqe;
282
283 llist_for_each_entry(dqe, &sne->defrag.frag_list, list) {
284 if (dqe->seg_nr == seg_nr) {
285 llist_del(&dqe->list);
286 return dqe;
287 }
288 }
289 return NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200290}
Harald Weltece22f922010-06-03 21:21:21 +0200291
Harald Welte8b705f22010-07-02 16:18:59 +0200292/* Perform actual defragmentation and create an output packet */
Harald Weltef78a3b22010-06-30 17:21:19 +0200293static int defrag_segments(struct gprs_sndcp_entity *sne)
Harald Weltece22f922010-06-03 21:21:21 +0200294{
295 struct msgb *msg;
296 unsigned int seg_nr;
297 uint8_t *npdu;
Philippf1f34362016-08-26 17:00:21 +0200298 int npdu_len;
299 int rc;
300 uint8_t *expnd = NULL;
Harald Weltece22f922010-06-03 21:21:21 +0200301
Harald Welteab4094c2010-07-02 16:01:47 +0200302 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Defragment output PDU %u "
303 "num_seg=%u tot_len=%u\n", sne->lle->llme->tlli, sne->nsapi,
304 sne->defrag.npdu, sne->defrag.highest_seg, sne->defrag.tot_len);
Sylvain Munauteda125c2010-06-09 20:56:52 +0200305 msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag");
Harald Weltece22f922010-06-03 21:21:21 +0200306 if (!msg)
307 return -ENOMEM;
308
309 /* FIXME: message headers + identifiers */
310
311 npdu = msg->data;
312
Harald Welte993697c2010-07-02 10:11:42 +0200313 for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) {
Harald Weltece22f922010-06-03 21:21:21 +0200314 struct defrag_queue_entry *dqe;
315 uint8_t *data;
316
317 dqe = defrag_get_seg(sne, seg_nr);
318 if (!dqe) {
319 LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr);
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100320 msgb_free(msg);
Harald Weltece22f922010-06-03 21:21:21 +0200321 return -EIO;
322 }
323 /* actually append the segment to the N-PDU */
324 data = msgb_put(msg, dqe->data_len);
325 memcpy(data, dqe->data, dqe->data_len);
326
327 /* release memory for the fragment queue entry */
328 talloc_free(dqe);
329 }
330
Philippf1f34362016-08-26 17:00:21 +0200331 npdu_len = sne->defrag.tot_len;
332
Harald Welte8b705f22010-07-02 16:18:59 +0200333 /* FIXME: cancel timer */
334
Harald Weltece22f922010-06-03 21:21:21 +0200335 /* actually send the N-PDU to the SGSN core code, which then
336 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Philippf1f34362016-08-26 17:00:21 +0200337
338 /* Decompress packet */
339#if DEBUG_IP_PACKETS == 1
340 DEBUGP(DSNDCP, " \n");
341 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
342 DEBUGP(DSNDCP, "===================================================\n");
343#endif
344 if (any_pcomp_or_dcomp_active(sgsn)) {
345
Philipp73f83d52016-09-02 13:38:01 +0200346 expnd = talloc_zero_size(msg, npdu_len * MAX_DATADECOMPR_FAC +
347 MAX_HDRDECOMPR_INCR);
Philippf1f34362016-08-26 17:00:21 +0200348 memcpy(expnd, npdu, npdu_len);
349
Philipp73f83d52016-09-02 13:38:01 +0200350 /* Apply data decompression */
351 rc = gprs_sndcp_dcomp_expand(expnd, npdu_len, sne->defrag.dcomp,
352 sne->defrag.data);
353 if (rc < 0) {
354 LOGP(DSNDCP, LOGL_ERROR,
355 "Data decompression failed!\n");
356 talloc_free(expnd);
357 return -EIO;
358 }
359
Philippf1f34362016-08-26 17:00:21 +0200360 /* Apply header decompression */
Philipp73f83d52016-09-02 13:38:01 +0200361 rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp,
Philippf1f34362016-08-26 17:00:21 +0200362 sne->defrag.proto);
363 if (rc < 0) {
364 LOGP(DSNDCP, LOGL_ERROR,
365 "TCP/IP Header decompression failed!\n");
366 talloc_free(expnd);
367 return -EIO;
368 }
369
370 /* Modify npu length, expnd is handed directly handed
371 * over to gsn_rx_sndcp_ud_ind(), see below */
372 npdu_len = rc;
373 } else
374 expnd = npdu;
375#if DEBUG_IP_PACKETS == 1
376 debug_ip_packet(expnd, npdu_len, 1, "defrag_segments()");
377 DEBUGP(DSNDCP, "===================================================\n");
378 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
379 DEBUGP(DSNDCP, " \n");
380#endif
381
382 /* Hand off packet to gtp */
383 rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, sne->lle->llme->tlli,
384 sne->nsapi, msg, npdu_len, expnd);
385
Harald Welte627e2852020-06-08 20:46:53 +0200386 /* we must free the memory we allocated above; ownership is not transferred
387 * downwards in the call above */
388 msgb_free(msg);
389
Philipp Maieref6205b2020-10-02 17:35:25 +0200390 /* Note: We do not have to free expnd explicitly, because it is created
391 * within the talloc context of msg, which we just freed. */
Philippf1f34362016-08-26 17:00:21 +0200392
393 return rc;
Harald Weltece22f922010-06-03 21:21:21 +0200394}
395
Philippf1f34362016-08-26 17:00:21 +0200396static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg,
397 uint8_t *hdr, unsigned int len)
Harald Weltece22f922010-06-03 21:21:21 +0200398{
399 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200400 struct sndcp_udata_hdr *suh;
401 uint16_t npdu_num;
402 uint8_t *data;
403 int rc;
404
405 sch = (struct sndcp_common_hdr *) hdr;
406 if (sch->first) {
Harald Weltece22f922010-06-03 21:21:21 +0200407 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
408 } else
409 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
410
411 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
412
413 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
414
Harald Welteab4094c2010-07-02 16:01:47 +0200415 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u "
416 "Length %u %s %s\n", sne->lle->llme->tlli, sne->nsapi, npdu_num,
417 suh->seg_nr, len, sch->first ? "F " : "", sch->more ? "M" : "");
Harald Welteb87bc862010-07-01 20:29:20 +0200418
Harald Weltece22f922010-06-03 21:21:21 +0200419 if (sch->first) {
420 /* first segment of a new packet. Discard all leftover fragments of
421 * previous packet */
422 if (!llist_empty(&sne->defrag.frag_list)) {
Harald Welte65d96782010-07-01 12:19:02 +0200423 struct defrag_queue_entry *dqe, *dqe2;
Harald Welteb87bc862010-07-01 20:29:20 +0200424 LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping "
425 "SN-PDU %u due to insufficient segments (%04x)\n",
426 sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu,
427 sne->defrag.seg_have);
Harald Welte65d96782010-07-01 12:19:02 +0200428 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
Harald Weltece22f922010-06-03 21:21:21 +0200429 llist_del(&dqe->list);
430 talloc_free(dqe);
431 }
432 }
433 /* store the currently de-fragmented PDU number */
434 sne->defrag.npdu = npdu_num;
Harald Welte8b705f22010-07-02 16:18:59 +0200435
436 /* Re-set fragmentation state */
Harald Weltece22f922010-06-03 21:21:21 +0200437 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
Harald Welte8b705f22010-07-02 16:18:59 +0200438 sne->defrag.tot_len = 0;
439 /* FIXME: (re)start timer */
Harald Weltece22f922010-06-03 21:21:21 +0200440 }
441
442 if (sne->defrag.npdu != npdu_num) {
443 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
444 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
445 /* FIXME */
446 }
447
448 /* FIXME: check if seg_nr already exists */
Harald Welte3d6815a2010-07-02 17:16:07 +0200449 /* make sure to subtract length of SNDCP header from 'len' */
450 rc = defrag_enqueue(sne, suh->seg_nr, data, len - (data - hdr));
Harald Weltece22f922010-06-03 21:21:21 +0200451 if (rc < 0)
452 return rc;
453
454 if (!sch->more) {
455 /* this is suppsed to be the last segment of the N-PDU, but it
456 * might well be not the last to arrive */
457 sne->defrag.no_more = 1;
458 }
459
460 if (sne->defrag.no_more) {
461 /* we have already received the last segment before, let's check
462 * if all the previous segments exist */
463 if (defrag_have_all_segments(sne))
464 return defrag_segments(sne);
465 }
466
467 return 0;
468}
Harald Welteebabdea2010-06-01 18:28:10 +0200469
Harald Weltef78a3b22010-06-30 17:21:19 +0200470static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200471 uint8_t nsapi)
472{
Harald Weltef78a3b22010-06-30 17:21:19 +0200473 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200474
Harald Weltef78a3b22010-06-30 17:21:19 +0200475 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
Harald Welteebabdea2010-06-01 18:28:10 +0200476 if (sne->lle == lle && sne->nsapi == nsapi)
477 return sne;
478 }
479 return NULL;
480}
481
Harald Weltef78a3b22010-06-30 17:21:19 +0200482static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200483 uint8_t nsapi)
484{
Harald Weltef78a3b22010-06-30 17:21:19 +0200485 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200486
Harald Weltef78a3b22010-06-30 17:21:19 +0200487 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
Harald Welteebabdea2010-06-01 18:28:10 +0200488 if (!sne)
489 return NULL;
490
491 sne->lle = lle;
492 sne->nsapi = nsapi;
Harald Weltece22f922010-06-03 21:21:21 +0200493 sne->defrag.timer.data = sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200494 //sne->fqueue.timer.cb = FIXME;
495 sne->rx_state = SNDCP_RX_S_FIRST;
Harald Welte362aea02010-07-01 12:31:10 +0200496 INIT_LLIST_HEAD(&sne->defrag.frag_list);
Harald Welteebabdea2010-06-01 18:28:10 +0200497
Harald Weltef78a3b22010-06-30 17:21:19 +0200498 llist_add(&sne->list, &gprs_sndcp_entities);
Harald Welte61444522010-06-02 12:40:48 +0200499
Harald Welteebabdea2010-06-01 18:28:10 +0200500 return sne;
501}
502
503/* Entry point for the SNSM-ACTIVATE.indication */
504int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
505{
Harald Welte61444522010-06-02 12:40:48 +0200506 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
507 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200508
Harald Weltef78a3b22010-06-30 17:21:19 +0200509 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200510 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
511 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
512 lle->llme->tlli, nsapi);
513 return -EEXIST;
514 }
515
Harald Weltef78a3b22010-06-30 17:21:19 +0200516 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200517 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200518 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200519 }
Harald Welteebabdea2010-06-01 18:28:10 +0200520
521 return 0;
522}
523
Harald Weltece22f922010-06-03 21:21:21 +0200524/* Entry point for the SNSM-DEACTIVATE.indication */
525int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
526{
Harald Weltef78a3b22010-06-30 17:21:19 +0200527 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200528
529 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
530 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
531
Harald Weltef78a3b22010-06-30 17:21:19 +0200532 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltece22f922010-06-03 21:21:21 +0200533 if (!sne) {
534 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
535 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
536 lle->sapi, nsapi);
537 return -ENOENT;
538 }
539 llist_del(&sne->list);
540 /* frag queue entries are hierarchically allocated, so no need to
541 * free them explicitly here */
542 talloc_free(sne);
543
544 return 0;
545}
546
Oliver Smithf7642852021-12-07 13:16:17 +0100547/* Clean up all gprs_sndcp_entities related to llme (OS#4824) */
548void gprs_sndcp_sm_deactivate_ind_by_llme(struct gprs_llc_llme *llme)
549{
550 struct gprs_sndcp_entity *sne, *sne2;
551
552 llist_for_each_entry_safe(sne, sne2, &gprs_sndcp_entities, list) {
553 if (sne->lle->llme == llme) {
554 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind for SNDCP attached to llme=%p\n", llme);
555 /* Free and remove from list */
556 sndcp_sm_deactivate_ind(sne->lle, sne->nsapi);
557 }
558 }
559}
560
Harald Weltece22f922010-06-03 21:21:21 +0200561/* Fragmenter state */
562struct sndcp_frag_state {
563 uint8_t frag_nr;
564 struct msgb *msg; /* original message */
565 uint8_t *next_byte; /* first byte of next fragment */
566
Harald Weltef78a3b22010-06-30 17:21:19 +0200567 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200568 void *mmcontext;
569};
570
571/* returns '1' if there are more fragments to send, '0' if none */
Philippf1f34362016-08-26 17:00:21 +0200572static int sndcp_send_ud_frag(struct sndcp_frag_state *fs,
573 uint8_t pcomp, uint8_t dcomp)
Harald Weltece22f922010-06-03 21:21:21 +0200574{
Harald Weltef78a3b22010-06-30 17:21:19 +0200575 struct gprs_sndcp_entity *sne = fs->sne;
Harald Weltece22f922010-06-03 21:21:21 +0200576 struct gprs_llc_lle *lle = sne->lle;
577 struct sndcp_common_hdr *sch;
578 struct sndcp_comp_hdr *scomph;
579 struct sndcp_udata_hdr *suh;
580 struct msgb *fmsg;
581 unsigned int max_payload_len;
582 unsigned int len;
583 uint8_t *data;
584 int rc, more;
585
Sylvain Munauteda125c2010-06-09 20:56:52 +0200586 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
Harald Weltece22f922010-06-03 21:21:21 +0200587 "SNDCP Frag");
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200588 if (!fmsg) {
589 msgb_free(fs->msg);
Harald Weltece22f922010-06-03 21:21:21 +0200590 return -ENOMEM;
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200591 }
Harald Weltece22f922010-06-03 21:21:21 +0200592
593 /* make sure lower layers route the fragment like the original */
594 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
595 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
596 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
597
598 /* prepend common SNDCP header */
599 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
600 sch->nsapi = sne->nsapi;
601 /* Set FIRST bit if we are the first fragment in a series */
602 if (fs->frag_nr == 0)
603 sch->first = 1;
604 sch->type = 1;
605
606 /* append the compression header for first fragment */
607 if (sch->first) {
608 scomph = (struct sndcp_comp_hdr *)
609 msgb_put(fmsg, sizeof(*scomph));
Philippf1f34362016-08-26 17:00:21 +0200610 scomph->pcomp = pcomp;
611 scomph->dcomp = dcomp;
Harald Weltece22f922010-06-03 21:21:21 +0200612 }
613
614 /* append the user-data header */
615 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
616 suh->npdu_low = sne->tx_npdu_nr & 0xff;
617 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
618 suh->seg_nr = fs->frag_nr % 0xf;
619
620 /* calculate remaining length to be sent */
621 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
622 /* how much payload can we actually send via LLC? */
623 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
624 if (sch->first)
625 max_payload_len -= sizeof(*scomph);
626 /* check if we're exceeding the max */
627 if (len > max_payload_len)
628 len = max_payload_len;
629
630 /* copy the actual fragment data into our fmsg */
631 data = msgb_put(fmsg, len);
632 memcpy(data, fs->next_byte, len);
633
634 /* Increment fragment number and data pointer to next fragment */
635 fs->frag_nr++;
636 fs->next_byte += len;
637
638 /* determine if we have more fragemnts to send */
639 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
640 more = 0;
641 else
642 more = 1;
643
644 /* set the MORE bit of the SNDCP header accordingly */
645 sch->more = more;
646
Max82040102016-07-06 11:59:18 +0200647 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext, true);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200648 /* abort in case of error, do not advance frag_nr / next_byte */
Harald Weltece22f922010-06-03 21:21:21 +0200649 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200650 msgb_free(fs->msg);
Harald Weltece22f922010-06-03 21:21:21 +0200651 return rc;
652 }
653
654 if (!more) {
655 /* we've sent all fragments */
656 msgb_free(fs->msg);
657 memset(fs, 0, sizeof(*fs));
658 /* increment NPDU number for next frame */
659 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
660 return 0;
661 }
662
663 /* default: more fragments to send */
664 return 1;
665}
666
Harald Weltedb2c39f2010-06-03 07:14:59 +0200667/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Harald Weltebb1c8052010-06-03 06:38:38 +0200668int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
669 void *mmcontext)
670{
Harald Weltef78a3b22010-06-30 17:21:19 +0200671 struct gprs_sndcp_entity *sne;
Harald Weltebb1c8052010-06-03 06:38:38 +0200672 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200673 struct sndcp_comp_hdr *scomph;
Harald Weltebb1c8052010-06-03 06:38:38 +0200674 struct sndcp_udata_hdr *suh;
Harald Weltece22f922010-06-03 21:21:21 +0200675 struct sndcp_frag_state fs;
Philippf1f34362016-08-26 17:00:21 +0200676 uint8_t pcomp = 0;
677 uint8_t dcomp = 0;
678 int rc;
Harald Weltebb1c8052010-06-03 06:38:38 +0200679
680 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
681
Philippf1f34362016-08-26 17:00:21 +0200682 /* Compress packet */
683#if DEBUG_IP_PACKETS == 1
684 DEBUGP(DSNDCP, " \n");
685 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
686 DEBUGP(DSNDCP, "===================================================\n");
687 debug_ip_packet(msg->data, msg->len, 0, "sndcp_initdata_req()");
688#endif
689 if (any_pcomp_or_dcomp_active(sgsn)) {
690
691 /* Apply header compression */
692 rc = gprs_sndcp_pcomp_compress(msg->data, msg->len, &pcomp,
693 lle->llme->comp.proto, nsapi);
694 if (rc < 0) {
695 LOGP(DSNDCP, LOGL_ERROR,
696 "TCP/IP Header compression failed!\n");
697 return -EIO;
698 }
699
700 /* Fixup pointer locations and sizes in message buffer to match
701 * the new, compressed buffer size */
702 msgb_get(msg, msg->len);
703 msgb_put(msg, rc);
Philipp73f83d52016-09-02 13:38:01 +0200704
705 /* Apply data compression */
706 rc = gprs_sndcp_dcomp_compress(msg->data, msg->len, &dcomp,
707 lle->llme->comp.data, nsapi);
708 if (rc < 0) {
709 LOGP(DSNDCP, LOGL_ERROR, "Data compression failed!\n");
710 return -EIO;
711 }
712
713 /* Fixup pointer locations and sizes in message buffer to match
714 * the new, compressed buffer size */
715 msgb_get(msg, msg->len);
716 msgb_put(msg, rc);
Philippf1f34362016-08-26 17:00:21 +0200717 }
718#if DEBUG_IP_PACKETS == 1
719 DEBUGP(DSNDCP, "===================================================\n");
720 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
721 DEBUGP(DSNDCP, " \n");
722#endif
723
Harald Weltef78a3b22010-06-30 17:21:19 +0200724 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltebb1c8052010-06-03 06:38:38 +0200725 if (!sne) {
726 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200727 msgb_free(msg);
Harald Weltebb1c8052010-06-03 06:38:38 +0200728 return -EIO;
729 }
730
Harald Weltece22f922010-06-03 21:21:21 +0200731 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
732 if (msg->len > lle->params.n201_u -
733 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
734 /* initialize the fragmenter state */
735 fs.msg = msg;
736 fs.frag_nr = 0;
737 fs.next_byte = msg->data;
738 fs.sne = sne;
739 fs.mmcontext = mmcontext;
740
741 /* call function to generate and send fragments until all
742 * of the N-PDU has been sent */
743 while (1) {
Philippf1f34362016-08-26 17:00:21 +0200744 int rc = sndcp_send_ud_frag(&fs,pcomp,dcomp);
Harald Weltece22f922010-06-03 21:21:21 +0200745 if (rc == 0)
746 return 0;
747 if (rc < 0)
748 return rc;
749 }
750 /* not reached */
751 return 0;
752 }
753
754 /* this is the non-fragmenting case where we only build 1 SN-PDU */
755
Harald Weltebb1c8052010-06-03 06:38:38 +0200756 /* prepend the user-data header */
757 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Weltece22f922010-06-03 21:21:21 +0200758 suh->npdu_low = sne->tx_npdu_nr & 0xff;
759 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
760 suh->seg_nr = 0;
761 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
762
763 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
Philippf1f34362016-08-26 17:00:21 +0200764 scomph->pcomp = pcomp;
765 scomph->dcomp = dcomp;
Harald Weltebb1c8052010-06-03 06:38:38 +0200766
767 /* prepend common SNDCP header */
768 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
769 sch->first = 1;
770 sch->type = 1;
771 sch->nsapi = nsapi;
772
Max82040102016-07-06 11:59:18 +0200773 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext, true);
Harald Weltebb1c8052010-06-03 06:38:38 +0200774}
775
Harald Welteebabdea2010-06-01 18:28:10 +0200776/* Section 5.1.2.17 LL-UNITDATA.ind */
Harald Welte36f12172010-07-02 16:44:24 +0200777int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle,
778 uint8_t *hdr, uint16_t len)
Harald Welteebabdea2010-06-01 18:28:10 +0200779{
Harald Weltef78a3b22010-06-30 17:21:19 +0200780 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200781 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Weltece22f922010-06-03 21:21:21 +0200782 struct sndcp_comp_hdr *scomph = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200783 struct sndcp_udata_hdr *suh;
Alexander Couzensa8f78252019-09-16 02:44:58 +0200784 struct sgsn_mm_ctx *mmctx;
Harald Welte16836a32010-06-02 10:25:40 +0200785 uint8_t *npdu;
Holger Hans Peter Freythercfee9522014-04-04 12:43:08 +0200786 uint16_t npdu_num __attribute__((unused));
Harald Welteebabdea2010-06-01 18:28:10 +0200787 int npdu_len;
Philippf1f34362016-08-26 17:00:21 +0200788 int rc;
789 uint8_t *expnd = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200790
Harald Weltece22f922010-06-03 21:21:21 +0200791 sch = (struct sndcp_common_hdr *) hdr;
792 if (sch->first) {
793 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
794 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
795 } else
796 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
797
Harald Welteebabdea2010-06-01 18:28:10 +0200798 if (sch->type == 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200799 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Welte96f71f22010-05-03 19:28:05 +0200800 return -EINVAL;
801 }
802
Harald Welte16836a32010-06-02 10:25:40 +0200803 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200804 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welteebabdea2010-06-01 18:28:10 +0200805 return -EIO;
806 }
807
Harald Weltef78a3b22010-06-30 17:21:19 +0200808 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200809 if (!sne) {
Harald Welte69996cb2010-06-02 10:26:19 +0200810 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welte61444522010-06-02 12:40:48 +0200811 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
812 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200813 return -EIO;
814 }
Harald Welte8911cef2010-07-01 19:56:19 +0200815 /* FIXME: move this RA_ID up to the LLME or even higher */
816 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
Harald Welteebabdea2010-06-01 18:28:10 +0200817
Alexander Couzensa8f78252019-09-16 02:44:58 +0200818 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &sne->ra_id);
819 if (!mmctx) {
820 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing MM ctx "
821 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n",
822 lle, lle->llme->tlli, lle->sapi, sch->nsapi);
823 return -EIO;
824 }
Pau Espin Pedrol11ccc432021-02-16 13:59:05 +0100825 gprs_gb_recv_pdu(mmctx, msg);
Alexander Couzensa8f78252019-09-16 02:44:58 +0200826
Harald Welte7e5bb622016-09-28 08:20:58 +0800827 if (scomph) {
Philippf1f34362016-08-26 17:00:21 +0200828 sne->defrag.pcomp = scomph->pcomp;
829 sne->defrag.dcomp = scomph->dcomp;
830 sne->defrag.proto = lle->llme->comp.proto;
831 sne->defrag.data = lle->llme->comp.data;
832 }
833
Harald Welteab4094c2010-07-02 16:01:47 +0200834 /* any non-first segment is by definition something to defragment
835 * as is any segment that tells us there are more segments */
836 if (!sch->first || sch->more)
Harald Welte60da7d42010-07-02 15:45:12 +0200837 return defrag_input(sne, msg, hdr, len);
Harald Welteebabdea2010-06-01 18:28:10 +0200838
Harald Welte16836a32010-06-02 10:25:40 +0200839 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200840 npdu = (uint8_t *)suh + sizeof(*suh);
Alexander Couzens410bc9b2018-09-18 20:01:28 +0200841 npdu_len = (msg->data + msg->len) - npdu;
Philippf1f34362016-08-26 17:00:21 +0200842
Harald Welte61444522010-06-02 12:40:48 +0200843 if (npdu_len <= 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200844 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200845 return -EIO;
846 }
847 /* actually send the N-PDU to the SGSN core code, which then
848 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Philippf1f34362016-08-26 17:00:21 +0200849
850 /* Decompress packet */
851#if DEBUG_IP_PACKETS == 1
852 DEBUGP(DSNDCP, " \n");
853 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
854 DEBUGP(DSNDCP, "===================================================\n");
855#endif
856 if (any_pcomp_or_dcomp_active(sgsn)) {
857
Philipp73f83d52016-09-02 13:38:01 +0200858 expnd = talloc_zero_size(msg, npdu_len * MAX_DATADECOMPR_FAC +
859 MAX_HDRDECOMPR_INCR);
Philippf1f34362016-08-26 17:00:21 +0200860 memcpy(expnd, npdu, npdu_len);
861
Philipp73f83d52016-09-02 13:38:01 +0200862 /* Apply data decompression */
863 rc = gprs_sndcp_dcomp_expand(expnd, npdu_len, sne->defrag.dcomp,
864 sne->defrag.data);
865 if (rc < 0) {
866 LOGP(DSNDCP, LOGL_ERROR,
867 "Data decompression failed!\n");
868 talloc_free(expnd);
869 return -EIO;
870 }
871
Philippf1f34362016-08-26 17:00:21 +0200872 /* Apply header decompression */
Philipp73f83d52016-09-02 13:38:01 +0200873 rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp,
Philippf1f34362016-08-26 17:00:21 +0200874 sne->defrag.proto);
875 if (rc < 0) {
876 LOGP(DSNDCP, LOGL_ERROR,
877 "TCP/IP Header decompression failed!\n");
878 talloc_free(expnd);
879 return -EIO;
880 }
881
882 /* Modify npu length, expnd is handed directly handed
883 * over to gsn_rx_sndcp_ud_ind(), see below */
884 npdu_len = rc;
885 } else
886 expnd = npdu;
887#if DEBUG_IP_PACKETS == 1
888 debug_ip_packet(expnd, npdu_len, 1, "sndcp_llunitdata_ind()");
889 DEBUGP(DSNDCP, "===================================================\n");
890 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
891 DEBUGP(DSNDCP, " \n");
892#endif
893
894 /* Hand off packet to gtp */
895 rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, lle->llme->tlli,
896 sne->nsapi, msg, npdu_len, expnd);
897
898 if (any_pcomp_or_dcomp_active(sgsn))
899 talloc_free(expnd);
900
901 return rc;
Harald Welte96f71f22010-05-03 19:28:05 +0200902}
903
Holger Hans Peter Freythercfee9522014-04-04 12:43:08 +0200904#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200905/* Section 5.1.2.1 LL-RESET.ind */
Harald Weltef78a3b22010-06-30 17:21:19 +0200906static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200907{
908 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
909 /* reset all SNDCP XID parameters to default values */
Holger Hans Peter Freyther6142dc42011-10-14 23:37:27 +0200910 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
911 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200912}
913
Harald Welte2720e732010-05-17 00:44:57 +0200914static int sndcp_ll_status_ind()
915{
916 /* inform the SM sub-layer by means of SNSM-STATUS.req */
Holger Hans Peter Freyther6142dc42011-10-14 23:37:27 +0200917 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
918 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200919}
920
921static struct sndcp_state_list {{
922 uint32_t states;
923 unsigned int type;
Harald Weltef78a3b22010-06-30 17:21:19 +0200924 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
Harald Welte2720e732010-05-17 00:44:57 +0200925} sndcp_state_list[] = {
926 { ALL_STATES,
927 LL_RESET_IND, sndcp_ll_reset_ind },
928 { ALL_STATES,
929 LL_ESTABLISH_IND, sndcp_ll_est_ind },
930 { SBIT(SNDCP_S_EST_RQD),
931 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
932 { SBIT(SNDCP_S_EST_RQD),
933 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
934 { SBIT(SNDCP_S_
935};
936
937static int sndcp_rx_llc_prim()
938{
939 case LL_ESTABLISH_REQ:
940 case LL_RELEASE_REQ:
941 case LL_XID_REQ:
942 case LL_DATA_REQ:
943 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
944
945 switch (prim) {
946 case LL_RESET_IND:
947 case LL_ESTABLISH_IND:
948 case LL_ESTABLISH_RESP:
949 case LL_ESTABLISH_CONF:
950 case LL_RELEASE_IND:
951 case LL_RELEASE_CONF:
952 case LL_XID_IND:
953 case LL_XID_RESP:
954 case LL_XID_CONF:
955 case LL_DATA_IND:
956 case LL_DATA_CONF:
957 case LL_UNITDATA_IND:
958 case LL_STATUS_IND:
Neels Hofmeyrcc7db182016-12-18 23:52:38 +0100959 }
Harald Welte2720e732010-05-17 00:44:57 +0200960}
Harald Welteebabdea2010-06-01 18:28:10 +0200961#endif
Philippf1f34362016-08-26 17:00:21 +0200962
963/* Generate SNDCP-XID message */
964static int gprs_llc_gen_sndcp_xid(uint8_t *bytes, int bytes_len, uint8_t nsapi)
965{
966 int entity = 0;
967 LLIST_HEAD(comp_fields);
968 struct gprs_sndcp_pcomp_rfc1144_params rfc1144_params;
969 struct gprs_sndcp_comp_field rfc1144_comp_field;
Philipp73f83d52016-09-02 13:38:01 +0200970 struct gprs_sndcp_dcomp_v42bis_params v42bis_params;
971 struct gprs_sndcp_comp_field v42bis_comp_field;
Philippf1f34362016-08-26 17:00:21 +0200972
973 memset(&rfc1144_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
Philipp73f83d52016-09-02 13:38:01 +0200974 memset(&v42bis_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
Philippf1f34362016-08-26 17:00:21 +0200975
976 /* Setup rfc1144 */
977 if (sgsn->cfg.pcomp_rfc1144.active) {
978 rfc1144_params.nsapi[0] = nsapi;
979 rfc1144_params.nsapi_len = 1;
980 rfc1144_params.s01 = sgsn->cfg.pcomp_rfc1144.s01;
981 rfc1144_comp_field.p = 1;
982 rfc1144_comp_field.entity = entity;
Stefan Sperlingc5721542018-11-07 16:33:39 +0100983 rfc1144_comp_field.algo.pcomp = RFC_1144;
Philippf1f34362016-08-26 17:00:21 +0200984 rfc1144_comp_field.comp[RFC1144_PCOMP1] = 1;
985 rfc1144_comp_field.comp[RFC1144_PCOMP2] = 2;
986 rfc1144_comp_field.comp_len = RFC1144_PCOMP_NUM;
987 rfc1144_comp_field.rfc1144_params = &rfc1144_params;
988 entity++;
989 llist_add(&rfc1144_comp_field.list, &comp_fields);
990 }
991
Philipp73f83d52016-09-02 13:38:01 +0200992 /* Setup V.42bis */
993 if (sgsn->cfg.dcomp_v42bis.active) {
994 v42bis_params.nsapi[0] = nsapi;
995 v42bis_params.nsapi_len = 1;
996 v42bis_params.p0 = sgsn->cfg.dcomp_v42bis.p0;
997 v42bis_params.p1 = sgsn->cfg.dcomp_v42bis.p1;
998 v42bis_params.p2 = sgsn->cfg.dcomp_v42bis.p2;
999 v42bis_comp_field.p = 1;
1000 v42bis_comp_field.entity = entity;
Stefan Sperlingc5721542018-11-07 16:33:39 +01001001 v42bis_comp_field.algo.dcomp = V42BIS;
Philipp73f83d52016-09-02 13:38:01 +02001002 v42bis_comp_field.comp[V42BIS_DCOMP1] = 1;
1003 v42bis_comp_field.comp_len = V42BIS_DCOMP_NUM;
1004 v42bis_comp_field.v42bis_params = &v42bis_params;
1005 entity++;
1006 llist_add(&v42bis_comp_field.list, &comp_fields);
1007 }
1008
Philippdb142dc2016-12-22 14:15:20 +01001009 /* Do not attempt to compile anything if there is no data in the list */
1010 if (llist_empty(&comp_fields))
1011 return 0;
1012
Philippf1f34362016-08-26 17:00:21 +02001013 /* Compile bytestream */
Philippdb142dc2016-12-22 14:15:20 +01001014 return gprs_sndcp_compile_xid(bytes, bytes_len, &comp_fields,
1015 DEFAULT_SNDCP_VERSION);
Philippf1f34362016-08-26 17:00:21 +02001016}
1017
1018/* Set of SNDCP-XID bnegotiation (See also: TS 144 065,
1019 * Section 6.8 XID parameter negotiation) */
1020int sndcp_sn_xid_req(struct gprs_llc_lle *lle, uint8_t nsapi)
1021{
1022 /* Note: The specification requires the SNDCP-User to set of an
1023 * SNDCP xid request. See also 3GPP TS 44.065, 6.8 XID parameter
1024 * negotiation, Figure 11: SNDCP XID negotiation procedure. In
1025 * our case the SNDCP-User is sgsn_libgtp.c, which calls
1026 * sndcp_sn_xid_req directly. */
1027
1028 uint8_t l3params[1024];
1029 int xid_len;
1030 struct gprs_llc_xid_field xid_field_request;
1031
1032 /* Wipe off all compression entities and their states to
1033 * get rid of possible leftovers from a previous session */
1034 gprs_sndcp_comp_free(lle->llme->comp.proto);
1035 gprs_sndcp_comp_free(lle->llme->comp.data);
1036 lle->llme->comp.proto = gprs_sndcp_comp_alloc(lle->llme);
1037 lle->llme->comp.data = gprs_sndcp_comp_alloc(lle->llme);
Harald Welteaf779d22019-04-12 16:56:04 +02001038 talloc_free(lle->xid);
1039 lle->xid = NULL;
Philippf1f34362016-08-26 17:00:21 +02001040
1041 /* Generate compression parameter bytestream */
1042 xid_len = gprs_llc_gen_sndcp_xid(l3params, sizeof(l3params), nsapi);
1043
1044 /* Send XID with the SNDCP-XID bytetsream included */
1045 if (xid_len > 0) {
1046 xid_field_request.type = GPRS_LLC_XID_T_L3_PAR;
1047 xid_field_request.data = l3params;
1048 xid_field_request.data_len = xid_len;
1049 return gprs_ll_xid_req(lle, &xid_field_request);
1050 }
1051
1052 /* When bytestream can not be generated, proceed without SNDCP-XID */
1053 return gprs_ll_xid_req(lle, NULL);
1054
1055}
1056
1057/* Handle header compression entites */
1058static int handle_pcomp_entities(struct gprs_sndcp_comp_field *comp_field,
1059 struct gprs_llc_lle *lle)
1060{
1061 /* Note: This functions also transforms the comp_field into its
1062 * echo form (strips comp values, resets propose bit etc...)
1063 * the processed comp_fields can then be sent back as XID-
1064 * Response without further modification. */
1065
1066 /* Delete propose bit */
1067 comp_field->p = 0;
1068
1069 /* Process proposed parameters */
Stefan Sperlingc5721542018-11-07 16:33:39 +01001070 switch (comp_field->algo.pcomp) {
Philippf1f34362016-08-26 17:00:21 +02001071 case RFC_1144:
1072 if (sgsn->cfg.pcomp_rfc1144.passive
1073 && comp_field->rfc1144_params->nsapi_len > 0) {
1074 DEBUGP(DSNDCP,
1075 "Accepting RFC1144 header compression...\n");
1076 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.proto,
1077 comp_field);
1078 } else {
1079 DEBUGP(DSNDCP,
1080 "Rejecting RFC1144 header compression...\n");
1081 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1082 comp_field->entity);
1083 comp_field->rfc1144_params->nsapi_len = 0;
1084 }
1085 break;
1086 case RFC_2507:
1087 /* RFC 2507 is not yet supported,
1088 * so we set applicable nsapis to zero */
1089 DEBUGP(DSNDCP, "Rejecting RFC2507 header compression...\n");
1090 comp_field->rfc2507_params->nsapi_len = 0;
1091 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1092 comp_field->entity);
1093 break;
1094 case ROHC:
1095 /* ROHC is not yet supported,
1096 * so we set applicable nsapis to zero */
1097 DEBUGP(DSNDCP, "Rejecting ROHC header compression...\n");
1098 comp_field->rohc_params->nsapi_len = 0;
1099 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1100 comp_field->entity);
1101 break;
1102 }
1103
1104 return 0;
1105}
1106
1107/* Hanle data compression entites */
1108static int handle_dcomp_entities(struct gprs_sndcp_comp_field *comp_field,
1109 struct gprs_llc_lle *lle)
1110{
1111 /* See note in handle_pcomp_entities() */
1112
1113 /* Delete propose bit */
1114 comp_field->p = 0;
1115
1116 /* Process proposed parameters */
Stefan Sperlingc5721542018-11-07 16:33:39 +01001117 switch (comp_field->algo.dcomp) {
Philippf1f34362016-08-26 17:00:21 +02001118 case V42BIS:
Philipp73f83d52016-09-02 13:38:01 +02001119 if (sgsn->cfg.dcomp_v42bis.passive &&
1120 comp_field->v42bis_params->nsapi_len > 0) {
1121 DEBUGP(DSNDCP,
1122 "Accepting V.42bis data compression...\n");
1123 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.data,
1124 comp_field);
1125 } else {
1126 LOGP(DSNDCP, LOGL_DEBUG,
1127 "Rejecting V.42bis data compression...\n");
1128 gprs_sndcp_comp_delete(lle->llme->comp.data,
1129 comp_field->entity);
1130 comp_field->v42bis_params->nsapi_len = 0;
1131 }
Philippf1f34362016-08-26 17:00:21 +02001132 break;
1133 case V44:
1134 /* V44 is not yet supported,
1135 * so we set applicable nsapis to zero */
1136 DEBUGP(DSNDCP, "Rejecting V.44 data compression...\n");
1137 comp_field->v44_params->nsapi_len = 0;
1138 gprs_sndcp_comp_delete(lle->llme->comp.data,
1139 comp_field->entity);
1140 break;
1141 }
1142
1143 return 0;
1144
1145}
1146
1147/* Process SNDCP-XID indication
1148 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1149int sndcp_sn_xid_ind(struct gprs_llc_xid_field *xid_field_indication,
1150 struct gprs_llc_xid_field *xid_field_response,
1151 struct gprs_llc_lle *lle)
1152{
1153 /* Note: This function computes the SNDCP-XID response that is sent
1154 * back to the ms when a ms originated XID is received. The
1155 * Input XID fields are directly processed and the result is directly
1156 * handed back. */
1157
1158 int rc;
1159 int compclass;
Philippdb142dc2016-12-22 14:15:20 +01001160 int version;
Philippf1f34362016-08-26 17:00:21 +02001161
1162 struct llist_head *comp_fields;
1163 struct gprs_sndcp_comp_field *comp_field;
1164
1165 OSMO_ASSERT(xid_field_indication);
1166 OSMO_ASSERT(xid_field_response);
1167 OSMO_ASSERT(lle);
1168
Keithbfd67d22019-04-29 18:23:10 +01001169 /* Some phones send zero byte length SNDCP frames
1170 * and do require a confirmation response. */
1171 if (xid_field_indication->data_len == 0) {
1172 xid_field_response->type = GPRS_LLC_XID_T_L3_PAR;
1173 xid_field_response->data_len = 0;
1174 return 0;
1175 }
1176
Philippf1f34362016-08-26 17:00:21 +02001177 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001178 comp_fields = gprs_sndcp_parse_xid(&version, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001179 xid_field_indication->data,
1180 xid_field_indication->data_len,
1181 NULL);
1182 if (!comp_fields)
1183 return -EINVAL;
1184
Philippf1f34362016-08-26 17:00:21 +02001185 /* Handle compression entites */
1186 DEBUGP(DSNDCP, "SNDCP-XID-IND (ms):\n");
1187 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1188
1189 llist_for_each_entry(comp_field, comp_fields, list) {
1190 compclass = gprs_sndcp_get_compression_class(comp_field);
1191 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1192 rc = handle_pcomp_entities(comp_field, lle);
1193 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1194 rc = handle_dcomp_entities(comp_field, lle);
1195 else {
1196 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1197 comp_field->entity);
1198 gprs_sndcp_comp_delete(lle->llme->comp.data,
1199 comp_field->entity);
1200 rc = 0;
1201 }
1202
1203 if (rc < 0) {
1204 talloc_free(comp_fields);
1205 return -EINVAL;
1206 }
1207 }
1208
1209 DEBUGP(DSNDCP, "SNDCP-XID-RES (sgsn):\n");
1210 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1211
1212 /* Reserve some memory to store the modified SNDCP-XID bytes */
1213 xid_field_response->data =
1214 talloc_zero_size(lle->llme, xid_field_indication->data_len);
1215
1216 /* Set Type flag for response */
1217 xid_field_response->type = GPRS_LLC_XID_T_L3_PAR;
1218
1219 /* Compile modified SNDCP-XID bytes */
1220 rc = gprs_sndcp_compile_xid(xid_field_response->data,
1221 xid_field_indication->data_len,
Philippdb142dc2016-12-22 14:15:20 +01001222 comp_fields, 0);
Philippf1f34362016-08-26 17:00:21 +02001223
1224 if (rc > 0)
1225 xid_field_response->data_len = rc;
1226 else {
1227 talloc_free(xid_field_response->data);
1228 xid_field_response->data = NULL;
1229 xid_field_response->data_len = 0;
1230 return -EINVAL;
1231 }
1232
1233 talloc_free(comp_fields);
1234
1235 return 0;
1236}
1237
1238/* Process SNDCP-XID indication
1239 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1240int sndcp_sn_xid_conf(struct gprs_llc_xid_field *xid_field_conf,
1241 struct gprs_llc_xid_field *xid_field_request,
1242 struct gprs_llc_lle *lle)
1243{
1244 /* Note: This function handles an incomming SNDCP-XID confirmiation.
1245 * Since the confirmation fields may lack important parameters we
1246 * will reconstruct these missing fields using the original request
1247 * we have sent. After that we will create (or delete) the
1248 * compression entites */
1249
1250 struct llist_head *comp_fields_req;
1251 struct llist_head *comp_fields_conf;
1252 struct gprs_sndcp_comp_field *comp_field;
1253 int rc;
1254 int compclass;
1255
1256 /* We need both, the confirmation that is sent back by the ms,
1257 * and the original request we have sent. If one of this is missing
1258 * we can not process the confirmation, the caller must check if
1259 * request and confirmation fields are available. */
1260 OSMO_ASSERT(xid_field_conf);
1261 OSMO_ASSERT(xid_field_request);
1262
1263 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001264 comp_fields_req = gprs_sndcp_parse_xid(NULL, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001265 xid_field_request->data,
1266 xid_field_request->data_len,
1267 NULL);
1268 if (!comp_fields_req)
1269 return -EINVAL;
1270
1271 DEBUGP(DSNDCP, "SNDCP-XID-REQ (sgsn):\n");
1272 gprs_sndcp_dump_comp_fields(comp_fields_req, LOGL_DEBUG);
1273
1274 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001275 comp_fields_conf = gprs_sndcp_parse_xid(NULL, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001276 xid_field_conf->data,
1277 xid_field_conf->data_len,
1278 comp_fields_req);
1279 if (!comp_fields_conf)
1280 return -EINVAL;
1281
1282 DEBUGP(DSNDCP, "SNDCP-XID-CONF (ms):\n");
1283 gprs_sndcp_dump_comp_fields(comp_fields_conf, LOGL_DEBUG);
1284
1285 /* Handle compression entites */
1286 llist_for_each_entry(comp_field, comp_fields_conf, list) {
1287 compclass = gprs_sndcp_get_compression_class(comp_field);
1288 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1289 rc = handle_pcomp_entities(comp_field, lle);
1290 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1291 rc = handle_dcomp_entities(comp_field, lle);
1292 else {
1293 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1294 comp_field->entity);
1295 gprs_sndcp_comp_delete(lle->llme->comp.data,
1296 comp_field->entity);
1297 rc = 0;
1298 }
1299
1300 if (rc < 0) {
1301 talloc_free(comp_fields_req);
1302 talloc_free(comp_fields_conf);
1303 return -EINVAL;
1304 }
1305 }
1306
1307 talloc_free(comp_fields_req);
1308 talloc_free(comp_fields_conf);
1309
1310 return 0;
1311}