blob: 19d87121e47c6c02716e7d4cdb57316284f8c09e [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>
Alexander Couzensa8f78252019-09-16 02:44:58 +020034#include <osmocom/sgsn/gprs_gb.h>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020035#include <osmocom/sgsn/gprs_llc.h>
36#include <osmocom/sgsn/sgsn.h>
37#include <osmocom/sgsn/gprs_sndcp.h>
38#include <osmocom/sgsn/gprs_llc_xid.h>
39#include <osmocom/sgsn/gprs_sndcp_xid.h>
40#include <osmocom/sgsn/gprs_sndcp_pcomp.h>
41#include <osmocom/sgsn/gprs_sndcp_dcomp.h>
42#include <osmocom/sgsn/gprs_sndcp_comp.h>
Philippf1f34362016-08-26 17:00:21 +020043
44#define DEBUG_IP_PACKETS 0 /* 0=Disabled, 1=Enabled */
45
46#if DEBUG_IP_PACKETS == 1
47/* Calculate TCP/IP checksum */
48static uint16_t calc_ip_csum(uint8_t *data, int len)
49{
50 int i;
51 uint32_t accumulator = 0;
52 uint16_t *pointer = (uint16_t *) data;
53
54 for (i = len; i > 1; i -= 2) {
55 accumulator += *pointer;
56 pointer++;
57 }
58
59 if (len % 2)
60 accumulator += *pointer;
61
62 accumulator = (accumulator & 0xffff) + ((accumulator >> 16) & 0xffff);
63 accumulator += (accumulator >> 16) & 0xffff;
64 return (~accumulator);
65}
66
67/* Calculate TCP/IP checksum */
68static uint16_t calc_tcpip_csum(const void *ctx, uint8_t *packet, int len)
69{
70 uint8_t *buf;
71 uint16_t csum;
72
73 buf = talloc_zero_size(ctx, len);
74 memset(buf, 0, len);
75 memcpy(buf, packet + 12, 8);
76 buf[9] = packet[9];
77 buf[11] = (len - 20) & 0xFF;
78 buf[10] = (len - 20) >> 8 & 0xFF;
79 memcpy(buf + 12, packet + 20, len - 20);
80 csum = calc_ip_csum(buf, len - 20 + 12);
81 talloc_free(buf);
82 return csum;
83}
84
85/* Show some ip packet details */
86static void debug_ip_packet(uint8_t *data, int len, int dir, char *info)
87{
88 uint8_t tcp_flags;
89 char flags_debugmsg[256];
90 int len_short;
91 static unsigned int packet_count = 0;
92 static unsigned int tcp_csum_err_count = 0;
93 static unsigned int ip_csum_err_count = 0;
94
95 packet_count++;
96
97 if (len > 80)
98 len_short = 80;
99 else
100 len_short = len;
101
102 if (dir)
103 DEBUGP(DSNDCP, "%s: MS => SGSN: %s\n", info,
104 osmo_hexdump_nospc(data, len_short));
105 else
106 DEBUGP(DSNDCP, "%s: MS <= SGSN: %s\n", info,
107 osmo_hexdump_nospc(data, len_short));
108
109 DEBUGP(DSNDCP, "%s: Length.: %d\n", info, len);
110 DEBUGP(DSNDCP, "%s: NO.: %d\n", info, packet_count);
111
112 if (len < 20) {
113 DEBUGP(DSNDCP, "%s: Error: Short IP packet!\n", info);
114 return;
115 }
116
117 if (calc_ip_csum(data, 20) != 0) {
118 DEBUGP(DSNDCP, "%s: Bad IP-Header checksum!\n", info);
119 ip_csum_err_count++;
120 } else
121 DEBUGP(DSNDCP, "%s: IP-Header checksum ok.\n", info);
122
123 if (data[9] == 0x06) {
124 if (len < 40) {
125 DEBUGP(DSNDCP, "%s: Error: Short TCP packet!\n", info);
126 return;
127 }
128
129 DEBUGP(DSNDCP, "%s: Protocol type: TCP\n", info);
130 tcp_flags = data[33];
131
132 if (calc_tcpip_csum(NULL, data, len) != 0) {
133 DEBUGP(DSNDCP, "%s: Bad TCP checksum!\n", info);
134 tcp_csum_err_count++;
135 } else
136 DEBUGP(DSNDCP, "%s: TCP checksum ok.\n", info);
137
138 memset(flags_debugmsg, 0, sizeof(flags_debugmsg));
139 if (tcp_flags & 1)
140 strcat(flags_debugmsg, "FIN ");
141 if (tcp_flags & 2)
142 strcat(flags_debugmsg, "SYN ");
143 if (tcp_flags & 4)
144 strcat(flags_debugmsg, "RST ");
145 if (tcp_flags & 8)
146 strcat(flags_debugmsg, "PSH ");
147 if (tcp_flags & 16)
148 strcat(flags_debugmsg, "ACK ");
149 if (tcp_flags & 32)
150 strcat(flags_debugmsg, "URG ");
151 DEBUGP(DSNDCP, "%s: FLAGS: %s\n", info, flags_debugmsg);
152 } else if (data[9] == 0x11) {
153 DEBUGP(DSNDCP, "%s: Protocol type: UDP\n", info);
154 } else {
155 DEBUGP(DSNDCP, "%s: Protocol type: (%02x)\n", info, data[9]);
156 }
157
158 DEBUGP(DSNDCP, "%s: IP-Header checksum errors: %d\n", info,
159 ip_csum_err_count);
160 DEBUGP(DSNDCP, "%s: TCP-Checksum errors: %d\n", info,
161 tcp_csum_err_count);
162}
163#endif
Harald Weltef78a3b22010-06-30 17:21:19 +0200164
Harald Welte96f71f22010-05-03 19:28:05 +0200165/* Chapter 7.2: SN-PDU Formats */
166struct sndcp_common_hdr {
167 /* octet 1 */
168 uint8_t nsapi:4;
169 uint8_t more:1;
170 uint8_t type:1;
171 uint8_t first:1;
172 uint8_t spare:1;
Harald Weltece22f922010-06-03 21:21:21 +0200173} __attribute__((packed));
174
175/* PCOMP / DCOMP only exist in first fragment */
176struct sndcp_comp_hdr {
Harald Welte96f71f22010-05-03 19:28:05 +0200177 /* octet 2 */
Harald Welte5cc2bc32010-06-02 23:17:05 +0200178 uint8_t pcomp:4;
179 uint8_t dcomp:4;
Harald Welteebabdea2010-06-01 18:28:10 +0200180} __attribute__((packed));
Harald Welte96f71f22010-05-03 19:28:05 +0200181
182struct sndcp_udata_hdr {
183 /* octet 3 */
184 uint8_t npdu_high:4;
185 uint8_t seg_nr:4;
186 /* octet 4 */
187 uint8_t npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200188} __attribute__((packed));
189
Harald Welteebabdea2010-06-01 18:28:10 +0200190
191static void *tall_sndcp_ctx;
192
193/* A fragment queue entry, containing one framgent of a N-PDU */
Harald Weltece22f922010-06-03 21:21:21 +0200194struct defrag_queue_entry {
Harald Welteebabdea2010-06-01 18:28:10 +0200195 struct llist_head list;
Harald Weltece22f922010-06-03 21:21:21 +0200196 /* segment number of this fragment */
197 uint32_t seg_nr;
198 /* length of the data area of this fragment */
Harald Welteebabdea2010-06-01 18:28:10 +0200199 uint32_t data_len;
Harald Weltece22f922010-06-03 21:21:21 +0200200 /* pointer to the data of this fragment */
201 uint8_t *data;
Harald Welteebabdea2010-06-01 18:28:10 +0200202};
203
Harald Weltef78a3b22010-06-30 17:21:19 +0200204LLIST_HEAD(gprs_sndcp_entities);
Harald Welte96f71f22010-05-03 19:28:05 +0200205
Philippf1f34362016-08-26 17:00:21 +0200206/* Check if any compression parameters are set in the sgsn configuration */
207static inline int any_pcomp_or_dcomp_active(struct sgsn_instance *sgsn) {
Philipp73f83d52016-09-02 13:38:01 +0200208 if (sgsn->cfg.pcomp_rfc1144.active || sgsn->cfg.pcomp_rfc1144.passive ||
209 sgsn->cfg.dcomp_v42bis.active || sgsn->cfg.dcomp_v42bis.passive)
Philippf1f34362016-08-26 17:00:21 +0200210 return true;
211 else
212 return false;
213}
214
Harald Weltece22f922010-06-03 21:21:21 +0200215/* Enqueue a fragment into the defragment queue */
Harald Weltef78a3b22010-06-30 17:21:19 +0200216static int defrag_enqueue(struct gprs_sndcp_entity *sne, uint8_t seg_nr,
Harald Welte3d6815a2010-07-02 17:16:07 +0200217 uint8_t *data, uint32_t data_len)
Harald Welteebabdea2010-06-01 18:28:10 +0200218{
Harald Weltece22f922010-06-03 21:21:21 +0200219 struct defrag_queue_entry *dqe;
Harald Welteebabdea2010-06-01 18:28:10 +0200220
Harald Weltece22f922010-06-03 21:21:21 +0200221 dqe = talloc_zero(tall_sndcp_ctx, struct defrag_queue_entry);
222 if (!dqe)
223 return -ENOMEM;
224 dqe->data = talloc_zero_size(dqe, data_len);
225 if (!dqe->data) {
226 talloc_free(dqe);
227 return -ENOMEM;
228 }
229 dqe->seg_nr = seg_nr;
230 dqe->data_len = data_len;
231
232 llist_add(&dqe->list, &sne->defrag.frag_list);
233
234 if (seg_nr > sne->defrag.highest_seg)
235 sne->defrag.highest_seg = seg_nr;
236
237 sne->defrag.seg_have |= (1 << seg_nr);
238 sne->defrag.tot_len += data_len;
239
Harald Welte8f0c0a32010-07-02 10:29:06 +0200240 memcpy(dqe->data, data, data_len);
241
Harald Weltece22f922010-06-03 21:21:21 +0200242 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200243}
244
Harald Weltece22f922010-06-03 21:21:21 +0200245/* return if we have all segments of this N-PDU */
Harald Weltef78a3b22010-06-30 17:21:19 +0200246static int defrag_have_all_segments(struct gprs_sndcp_entity *sne)
Harald Welteebabdea2010-06-01 18:28:10 +0200247{
Harald Weltece22f922010-06-03 21:21:21 +0200248 uint32_t seg_needed = 0;
249 unsigned int i;
Harald Welteebabdea2010-06-01 18:28:10 +0200250
Harald Weltece22f922010-06-03 21:21:21 +0200251 /* create a bitmask of needed segments */
Harald Welte951a12c2010-07-01 15:09:45 +0200252 for (i = 0; i <= sne->defrag.highest_seg; i++)
Harald Weltece22f922010-06-03 21:21:21 +0200253 seg_needed |= (1 << i);
254
255 if (seg_needed == sne->defrag.seg_have)
256 return 1;
257
258 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200259}
260
Harald Weltef78a3b22010-06-30 17:21:19 +0200261static struct defrag_queue_entry *defrag_get_seg(struct gprs_sndcp_entity *sne,
Harald Weltece22f922010-06-03 21:21:21 +0200262 uint32_t seg_nr)
Harald Welteebabdea2010-06-01 18:28:10 +0200263{
Harald Weltece22f922010-06-03 21:21:21 +0200264 struct defrag_queue_entry *dqe;
265
266 llist_for_each_entry(dqe, &sne->defrag.frag_list, list) {
267 if (dqe->seg_nr == seg_nr) {
268 llist_del(&dqe->list);
269 return dqe;
270 }
271 }
272 return NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200273}
Harald Weltece22f922010-06-03 21:21:21 +0200274
Harald Welte8b705f22010-07-02 16:18:59 +0200275/* Perform actual defragmentation and create an output packet */
Harald Weltef78a3b22010-06-30 17:21:19 +0200276static int defrag_segments(struct gprs_sndcp_entity *sne)
Harald Weltece22f922010-06-03 21:21:21 +0200277{
278 struct msgb *msg;
279 unsigned int seg_nr;
280 uint8_t *npdu;
Philippf1f34362016-08-26 17:00:21 +0200281 int npdu_len;
282 int rc;
283 uint8_t *expnd = NULL;
Harald Weltece22f922010-06-03 21:21:21 +0200284
Harald Welteab4094c2010-07-02 16:01:47 +0200285 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Defragment output PDU %u "
286 "num_seg=%u tot_len=%u\n", sne->lle->llme->tlli, sne->nsapi,
287 sne->defrag.npdu, sne->defrag.highest_seg, sne->defrag.tot_len);
Sylvain Munauteda125c2010-06-09 20:56:52 +0200288 msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag");
Harald Weltece22f922010-06-03 21:21:21 +0200289 if (!msg)
290 return -ENOMEM;
291
292 /* FIXME: message headers + identifiers */
293
294 npdu = msg->data;
295
Harald Welte993697c2010-07-02 10:11:42 +0200296 for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) {
Harald Weltece22f922010-06-03 21:21:21 +0200297 struct defrag_queue_entry *dqe;
298 uint8_t *data;
299
300 dqe = defrag_get_seg(sne, seg_nr);
301 if (!dqe) {
302 LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr);
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100303 msgb_free(msg);
Harald Weltece22f922010-06-03 21:21:21 +0200304 return -EIO;
305 }
306 /* actually append the segment to the N-PDU */
307 data = msgb_put(msg, dqe->data_len);
308 memcpy(data, dqe->data, dqe->data_len);
309
310 /* release memory for the fragment queue entry */
311 talloc_free(dqe);
312 }
313
Philippf1f34362016-08-26 17:00:21 +0200314 npdu_len = sne->defrag.tot_len;
315
Harald Welte8b705f22010-07-02 16:18:59 +0200316 /* FIXME: cancel timer */
317
Harald Weltece22f922010-06-03 21:21:21 +0200318 /* actually send the N-PDU to the SGSN core code, which then
319 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Philippf1f34362016-08-26 17:00:21 +0200320
321 /* Decompress packet */
322#if DEBUG_IP_PACKETS == 1
323 DEBUGP(DSNDCP, " \n");
324 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
325 DEBUGP(DSNDCP, "===================================================\n");
326#endif
327 if (any_pcomp_or_dcomp_active(sgsn)) {
328
Philipp73f83d52016-09-02 13:38:01 +0200329 expnd = talloc_zero_size(msg, npdu_len * MAX_DATADECOMPR_FAC +
330 MAX_HDRDECOMPR_INCR);
Philippf1f34362016-08-26 17:00:21 +0200331 memcpy(expnd, npdu, npdu_len);
332
Philipp73f83d52016-09-02 13:38:01 +0200333 /* Apply data decompression */
334 rc = gprs_sndcp_dcomp_expand(expnd, npdu_len, sne->defrag.dcomp,
335 sne->defrag.data);
336 if (rc < 0) {
337 LOGP(DSNDCP, LOGL_ERROR,
338 "Data decompression failed!\n");
339 talloc_free(expnd);
340 return -EIO;
341 }
342
Philippf1f34362016-08-26 17:00:21 +0200343 /* Apply header decompression */
Philipp73f83d52016-09-02 13:38:01 +0200344 rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp,
Philippf1f34362016-08-26 17:00:21 +0200345 sne->defrag.proto);
346 if (rc < 0) {
347 LOGP(DSNDCP, LOGL_ERROR,
348 "TCP/IP Header decompression failed!\n");
349 talloc_free(expnd);
350 return -EIO;
351 }
352
353 /* Modify npu length, expnd is handed directly handed
354 * over to gsn_rx_sndcp_ud_ind(), see below */
355 npdu_len = rc;
356 } else
357 expnd = npdu;
358#if DEBUG_IP_PACKETS == 1
359 debug_ip_packet(expnd, npdu_len, 1, "defrag_segments()");
360 DEBUGP(DSNDCP, "===================================================\n");
361 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
362 DEBUGP(DSNDCP, " \n");
363#endif
364
365 /* Hand off packet to gtp */
366 rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, sne->lle->llme->tlli,
367 sne->nsapi, msg, npdu_len, expnd);
368
Harald Welte627e2852020-06-08 20:46:53 +0200369 /* we must free the memory we allocated above; ownership is not transferred
370 * downwards in the call above */
371 msgb_free(msg);
372
Philipp Maieref6205b2020-10-02 17:35:25 +0200373 /* Note: We do not have to free expnd explicitly, because it is created
374 * within the talloc context of msg, which we just freed. */
Philippf1f34362016-08-26 17:00:21 +0200375
376 return rc;
Harald Weltece22f922010-06-03 21:21:21 +0200377}
378
Philippf1f34362016-08-26 17:00:21 +0200379static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg,
380 uint8_t *hdr, unsigned int len)
Harald Weltece22f922010-06-03 21:21:21 +0200381{
382 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200383 struct sndcp_udata_hdr *suh;
384 uint16_t npdu_num;
385 uint8_t *data;
386 int rc;
387
388 sch = (struct sndcp_common_hdr *) hdr;
389 if (sch->first) {
Harald Weltece22f922010-06-03 21:21:21 +0200390 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
391 } else
392 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
393
394 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
395
396 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
397
Harald Welteab4094c2010-07-02 16:01:47 +0200398 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u "
399 "Length %u %s %s\n", sne->lle->llme->tlli, sne->nsapi, npdu_num,
400 suh->seg_nr, len, sch->first ? "F " : "", sch->more ? "M" : "");
Harald Welteb87bc862010-07-01 20:29:20 +0200401
Harald Weltece22f922010-06-03 21:21:21 +0200402 if (sch->first) {
403 /* first segment of a new packet. Discard all leftover fragments of
404 * previous packet */
405 if (!llist_empty(&sne->defrag.frag_list)) {
Harald Welte65d96782010-07-01 12:19:02 +0200406 struct defrag_queue_entry *dqe, *dqe2;
Harald Welteb87bc862010-07-01 20:29:20 +0200407 LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping "
408 "SN-PDU %u due to insufficient segments (%04x)\n",
409 sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu,
410 sne->defrag.seg_have);
Harald Welte65d96782010-07-01 12:19:02 +0200411 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
Harald Weltece22f922010-06-03 21:21:21 +0200412 llist_del(&dqe->list);
413 talloc_free(dqe);
414 }
415 }
416 /* store the currently de-fragmented PDU number */
417 sne->defrag.npdu = npdu_num;
Harald Welte8b705f22010-07-02 16:18:59 +0200418
419 /* Re-set fragmentation state */
Harald Weltece22f922010-06-03 21:21:21 +0200420 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
Harald Welte8b705f22010-07-02 16:18:59 +0200421 sne->defrag.tot_len = 0;
422 /* FIXME: (re)start timer */
Harald Weltece22f922010-06-03 21:21:21 +0200423 }
424
425 if (sne->defrag.npdu != npdu_num) {
426 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
427 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
428 /* FIXME */
429 }
430
431 /* FIXME: check if seg_nr already exists */
Harald Welte3d6815a2010-07-02 17:16:07 +0200432 /* make sure to subtract length of SNDCP header from 'len' */
433 rc = defrag_enqueue(sne, suh->seg_nr, data, len - (data - hdr));
Harald Weltece22f922010-06-03 21:21:21 +0200434 if (rc < 0)
435 return rc;
436
437 if (!sch->more) {
438 /* this is suppsed to be the last segment of the N-PDU, but it
439 * might well be not the last to arrive */
440 sne->defrag.no_more = 1;
441 }
442
443 if (sne->defrag.no_more) {
444 /* we have already received the last segment before, let's check
445 * if all the previous segments exist */
446 if (defrag_have_all_segments(sne))
447 return defrag_segments(sne);
448 }
449
450 return 0;
451}
Harald Welteebabdea2010-06-01 18:28:10 +0200452
Harald Weltef78a3b22010-06-30 17:21:19 +0200453static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200454 uint8_t nsapi)
455{
Harald Weltef78a3b22010-06-30 17:21:19 +0200456 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200457
Harald Weltef78a3b22010-06-30 17:21:19 +0200458 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
Harald Welteebabdea2010-06-01 18:28:10 +0200459 if (sne->lle == lle && sne->nsapi == nsapi)
460 return sne;
461 }
462 return NULL;
463}
464
Harald Weltef78a3b22010-06-30 17:21:19 +0200465static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200466 uint8_t nsapi)
467{
Harald Weltef78a3b22010-06-30 17:21:19 +0200468 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200469
Harald Weltef78a3b22010-06-30 17:21:19 +0200470 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
Harald Welteebabdea2010-06-01 18:28:10 +0200471 if (!sne)
472 return NULL;
473
474 sne->lle = lle;
475 sne->nsapi = nsapi;
Harald Weltece22f922010-06-03 21:21:21 +0200476 sne->defrag.timer.data = sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200477 //sne->fqueue.timer.cb = FIXME;
478 sne->rx_state = SNDCP_RX_S_FIRST;
Harald Welte362aea02010-07-01 12:31:10 +0200479 INIT_LLIST_HEAD(&sne->defrag.frag_list);
Harald Welteebabdea2010-06-01 18:28:10 +0200480
Harald Weltef78a3b22010-06-30 17:21:19 +0200481 llist_add(&sne->list, &gprs_sndcp_entities);
Harald Welte61444522010-06-02 12:40:48 +0200482
Harald Welteebabdea2010-06-01 18:28:10 +0200483 return sne;
484}
485
486/* Entry point for the SNSM-ACTIVATE.indication */
487int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
488{
Harald Welte61444522010-06-02 12:40:48 +0200489 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
490 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200491
Harald Weltef78a3b22010-06-30 17:21:19 +0200492 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200493 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
494 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
495 lle->llme->tlli, nsapi);
496 return -EEXIST;
497 }
498
Harald Weltef78a3b22010-06-30 17:21:19 +0200499 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200500 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200501 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200502 }
Harald Welteebabdea2010-06-01 18:28:10 +0200503
504 return 0;
505}
506
Harald Weltece22f922010-06-03 21:21:21 +0200507/* Entry point for the SNSM-DEACTIVATE.indication */
508int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
509{
Harald Weltef78a3b22010-06-30 17:21:19 +0200510 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200511
512 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
513 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
514
Harald Weltef78a3b22010-06-30 17:21:19 +0200515 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltece22f922010-06-03 21:21:21 +0200516 if (!sne) {
517 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
518 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
519 lle->sapi, nsapi);
520 return -ENOENT;
521 }
522 llist_del(&sne->list);
523 /* frag queue entries are hierarchically allocated, so no need to
524 * free them explicitly here */
525 talloc_free(sne);
526
527 return 0;
528}
529
530/* Fragmenter state */
531struct sndcp_frag_state {
532 uint8_t frag_nr;
533 struct msgb *msg; /* original message */
534 uint8_t *next_byte; /* first byte of next fragment */
535
Harald Weltef78a3b22010-06-30 17:21:19 +0200536 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200537 void *mmcontext;
538};
539
540/* returns '1' if there are more fragments to send, '0' if none */
Philippf1f34362016-08-26 17:00:21 +0200541static int sndcp_send_ud_frag(struct sndcp_frag_state *fs,
542 uint8_t pcomp, uint8_t dcomp)
Harald Weltece22f922010-06-03 21:21:21 +0200543{
Harald Weltef78a3b22010-06-30 17:21:19 +0200544 struct gprs_sndcp_entity *sne = fs->sne;
Harald Weltece22f922010-06-03 21:21:21 +0200545 struct gprs_llc_lle *lle = sne->lle;
546 struct sndcp_common_hdr *sch;
547 struct sndcp_comp_hdr *scomph;
548 struct sndcp_udata_hdr *suh;
549 struct msgb *fmsg;
550 unsigned int max_payload_len;
551 unsigned int len;
552 uint8_t *data;
553 int rc, more;
554
Sylvain Munauteda125c2010-06-09 20:56:52 +0200555 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
Harald Weltece22f922010-06-03 21:21:21 +0200556 "SNDCP Frag");
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200557 if (!fmsg) {
558 msgb_free(fs->msg);
Harald Weltece22f922010-06-03 21:21:21 +0200559 return -ENOMEM;
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200560 }
Harald Weltece22f922010-06-03 21:21:21 +0200561
562 /* make sure lower layers route the fragment like the original */
563 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
564 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
565 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
566
567 /* prepend common SNDCP header */
568 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
569 sch->nsapi = sne->nsapi;
570 /* Set FIRST bit if we are the first fragment in a series */
571 if (fs->frag_nr == 0)
572 sch->first = 1;
573 sch->type = 1;
574
575 /* append the compression header for first fragment */
576 if (sch->first) {
577 scomph = (struct sndcp_comp_hdr *)
578 msgb_put(fmsg, sizeof(*scomph));
Philippf1f34362016-08-26 17:00:21 +0200579 scomph->pcomp = pcomp;
580 scomph->dcomp = dcomp;
Harald Weltece22f922010-06-03 21:21:21 +0200581 }
582
583 /* append the user-data header */
584 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
585 suh->npdu_low = sne->tx_npdu_nr & 0xff;
586 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
587 suh->seg_nr = fs->frag_nr % 0xf;
588
589 /* calculate remaining length to be sent */
590 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
591 /* how much payload can we actually send via LLC? */
592 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
593 if (sch->first)
594 max_payload_len -= sizeof(*scomph);
595 /* check if we're exceeding the max */
596 if (len > max_payload_len)
597 len = max_payload_len;
598
599 /* copy the actual fragment data into our fmsg */
600 data = msgb_put(fmsg, len);
601 memcpy(data, fs->next_byte, len);
602
603 /* Increment fragment number and data pointer to next fragment */
604 fs->frag_nr++;
605 fs->next_byte += len;
606
607 /* determine if we have more fragemnts to send */
608 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
609 more = 0;
610 else
611 more = 1;
612
613 /* set the MORE bit of the SNDCP header accordingly */
614 sch->more = more;
615
Max82040102016-07-06 11:59:18 +0200616 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext, true);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200617 /* abort in case of error, do not advance frag_nr / next_byte */
Harald Weltece22f922010-06-03 21:21:21 +0200618 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200619 msgb_free(fs->msg);
Harald Weltece22f922010-06-03 21:21:21 +0200620 return rc;
621 }
622
623 if (!more) {
624 /* we've sent all fragments */
625 msgb_free(fs->msg);
626 memset(fs, 0, sizeof(*fs));
627 /* increment NPDU number for next frame */
628 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
629 return 0;
630 }
631
632 /* default: more fragments to send */
633 return 1;
634}
635
Harald Weltedb2c39f2010-06-03 07:14:59 +0200636/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Harald Weltebb1c8052010-06-03 06:38:38 +0200637int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
638 void *mmcontext)
639{
Harald Weltef78a3b22010-06-30 17:21:19 +0200640 struct gprs_sndcp_entity *sne;
Harald Weltebb1c8052010-06-03 06:38:38 +0200641 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200642 struct sndcp_comp_hdr *scomph;
Harald Weltebb1c8052010-06-03 06:38:38 +0200643 struct sndcp_udata_hdr *suh;
Harald Weltece22f922010-06-03 21:21:21 +0200644 struct sndcp_frag_state fs;
Philippf1f34362016-08-26 17:00:21 +0200645 uint8_t pcomp = 0;
646 uint8_t dcomp = 0;
647 int rc;
Harald Weltebb1c8052010-06-03 06:38:38 +0200648
649 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
650
Philippf1f34362016-08-26 17:00:21 +0200651 /* Compress packet */
652#if DEBUG_IP_PACKETS == 1
653 DEBUGP(DSNDCP, " \n");
654 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
655 DEBUGP(DSNDCP, "===================================================\n");
656 debug_ip_packet(msg->data, msg->len, 0, "sndcp_initdata_req()");
657#endif
658 if (any_pcomp_or_dcomp_active(sgsn)) {
659
660 /* Apply header compression */
661 rc = gprs_sndcp_pcomp_compress(msg->data, msg->len, &pcomp,
662 lle->llme->comp.proto, nsapi);
663 if (rc < 0) {
664 LOGP(DSNDCP, LOGL_ERROR,
665 "TCP/IP Header compression failed!\n");
666 return -EIO;
667 }
668
669 /* Fixup pointer locations and sizes in message buffer to match
670 * the new, compressed buffer size */
671 msgb_get(msg, msg->len);
672 msgb_put(msg, rc);
Philipp73f83d52016-09-02 13:38:01 +0200673
674 /* Apply data compression */
675 rc = gprs_sndcp_dcomp_compress(msg->data, msg->len, &dcomp,
676 lle->llme->comp.data, nsapi);
677 if (rc < 0) {
678 LOGP(DSNDCP, LOGL_ERROR, "Data compression failed!\n");
679 return -EIO;
680 }
681
682 /* Fixup pointer locations and sizes in message buffer to match
683 * the new, compressed buffer size */
684 msgb_get(msg, msg->len);
685 msgb_put(msg, rc);
Philippf1f34362016-08-26 17:00:21 +0200686 }
687#if DEBUG_IP_PACKETS == 1
688 DEBUGP(DSNDCP, "===================================================\n");
689 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
690 DEBUGP(DSNDCP, " \n");
691#endif
692
Harald Weltef78a3b22010-06-30 17:21:19 +0200693 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltebb1c8052010-06-03 06:38:38 +0200694 if (!sne) {
695 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200696 msgb_free(msg);
Harald Weltebb1c8052010-06-03 06:38:38 +0200697 return -EIO;
698 }
699
Harald Weltece22f922010-06-03 21:21:21 +0200700 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
701 if (msg->len > lle->params.n201_u -
702 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
703 /* initialize the fragmenter state */
704 fs.msg = msg;
705 fs.frag_nr = 0;
706 fs.next_byte = msg->data;
707 fs.sne = sne;
708 fs.mmcontext = mmcontext;
709
710 /* call function to generate and send fragments until all
711 * of the N-PDU has been sent */
712 while (1) {
Philippf1f34362016-08-26 17:00:21 +0200713 int rc = sndcp_send_ud_frag(&fs,pcomp,dcomp);
Harald Weltece22f922010-06-03 21:21:21 +0200714 if (rc == 0)
715 return 0;
716 if (rc < 0)
717 return rc;
718 }
719 /* not reached */
720 return 0;
721 }
722
723 /* this is the non-fragmenting case where we only build 1 SN-PDU */
724
Harald Weltebb1c8052010-06-03 06:38:38 +0200725 /* prepend the user-data header */
726 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Weltece22f922010-06-03 21:21:21 +0200727 suh->npdu_low = sne->tx_npdu_nr & 0xff;
728 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
729 suh->seg_nr = 0;
730 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
731
732 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
Philippf1f34362016-08-26 17:00:21 +0200733 scomph->pcomp = pcomp;
734 scomph->dcomp = dcomp;
Harald Weltebb1c8052010-06-03 06:38:38 +0200735
736 /* prepend common SNDCP header */
737 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
738 sch->first = 1;
739 sch->type = 1;
740 sch->nsapi = nsapi;
741
Max82040102016-07-06 11:59:18 +0200742 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext, true);
Harald Weltebb1c8052010-06-03 06:38:38 +0200743}
744
Harald Welteebabdea2010-06-01 18:28:10 +0200745/* Section 5.1.2.17 LL-UNITDATA.ind */
Harald Welte36f12172010-07-02 16:44:24 +0200746int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle,
747 uint8_t *hdr, uint16_t len)
Harald Welteebabdea2010-06-01 18:28:10 +0200748{
Harald Weltef78a3b22010-06-30 17:21:19 +0200749 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200750 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Weltece22f922010-06-03 21:21:21 +0200751 struct sndcp_comp_hdr *scomph = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200752 struct sndcp_udata_hdr *suh;
Alexander Couzensa8f78252019-09-16 02:44:58 +0200753 struct sgsn_mm_ctx *mmctx;
Harald Welte16836a32010-06-02 10:25:40 +0200754 uint8_t *npdu;
Holger Hans Peter Freythercfee9522014-04-04 12:43:08 +0200755 uint16_t npdu_num __attribute__((unused));
Harald Welteebabdea2010-06-01 18:28:10 +0200756 int npdu_len;
Philippf1f34362016-08-26 17:00:21 +0200757 int rc;
758 uint8_t *expnd = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200759
Harald Weltece22f922010-06-03 21:21:21 +0200760 sch = (struct sndcp_common_hdr *) hdr;
761 if (sch->first) {
762 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
763 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
764 } else
765 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
766
Harald Welteebabdea2010-06-01 18:28:10 +0200767 if (sch->type == 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200768 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Welte96f71f22010-05-03 19:28:05 +0200769 return -EINVAL;
770 }
771
Harald Welte16836a32010-06-02 10:25:40 +0200772 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200773 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welteebabdea2010-06-01 18:28:10 +0200774 return -EIO;
775 }
776
Harald Weltef78a3b22010-06-30 17:21:19 +0200777 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200778 if (!sne) {
Harald Welte69996cb2010-06-02 10:26:19 +0200779 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welte61444522010-06-02 12:40:48 +0200780 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
781 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200782 return -EIO;
783 }
Harald Welte8911cef2010-07-01 19:56:19 +0200784 /* FIXME: move this RA_ID up to the LLME or even higher */
785 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
Harald Welteebabdea2010-06-01 18:28:10 +0200786
Alexander Couzensa8f78252019-09-16 02:44:58 +0200787 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &sne->ra_id);
788 if (!mmctx) {
789 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing MM ctx "
790 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n",
791 lle, lle->llme->tlli, lle->sapi, sch->nsapi);
792 return -EIO;
793 }
794 gprs_gb_recv_pdu(mmctx);
795
Harald Welte7e5bb622016-09-28 08:20:58 +0800796 if (scomph) {
Philippf1f34362016-08-26 17:00:21 +0200797 sne->defrag.pcomp = scomph->pcomp;
798 sne->defrag.dcomp = scomph->dcomp;
799 sne->defrag.proto = lle->llme->comp.proto;
800 sne->defrag.data = lle->llme->comp.data;
801 }
802
Harald Welteab4094c2010-07-02 16:01:47 +0200803 /* any non-first segment is by definition something to defragment
804 * as is any segment that tells us there are more segments */
805 if (!sch->first || sch->more)
Harald Welte60da7d42010-07-02 15:45:12 +0200806 return defrag_input(sne, msg, hdr, len);
Harald Welteebabdea2010-06-01 18:28:10 +0200807
Harald Welte16836a32010-06-02 10:25:40 +0200808 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200809 npdu = (uint8_t *)suh + sizeof(*suh);
Alexander Couzens410bc9b2018-09-18 20:01:28 +0200810 npdu_len = (msg->data + msg->len) - npdu;
Philippf1f34362016-08-26 17:00:21 +0200811
Harald Welte61444522010-06-02 12:40:48 +0200812 if (npdu_len <= 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200813 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200814 return -EIO;
815 }
816 /* actually send the N-PDU to the SGSN core code, which then
817 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Philippf1f34362016-08-26 17:00:21 +0200818
819 /* Decompress packet */
820#if DEBUG_IP_PACKETS == 1
821 DEBUGP(DSNDCP, " \n");
822 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
823 DEBUGP(DSNDCP, "===================================================\n");
824#endif
825 if (any_pcomp_or_dcomp_active(sgsn)) {
826
Philipp73f83d52016-09-02 13:38:01 +0200827 expnd = talloc_zero_size(msg, npdu_len * MAX_DATADECOMPR_FAC +
828 MAX_HDRDECOMPR_INCR);
Philippf1f34362016-08-26 17:00:21 +0200829 memcpy(expnd, npdu, npdu_len);
830
Philipp73f83d52016-09-02 13:38:01 +0200831 /* Apply data decompression */
832 rc = gprs_sndcp_dcomp_expand(expnd, npdu_len, sne->defrag.dcomp,
833 sne->defrag.data);
834 if (rc < 0) {
835 LOGP(DSNDCP, LOGL_ERROR,
836 "Data decompression failed!\n");
837 talloc_free(expnd);
838 return -EIO;
839 }
840
Philippf1f34362016-08-26 17:00:21 +0200841 /* Apply header decompression */
Philipp73f83d52016-09-02 13:38:01 +0200842 rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp,
Philippf1f34362016-08-26 17:00:21 +0200843 sne->defrag.proto);
844 if (rc < 0) {
845 LOGP(DSNDCP, LOGL_ERROR,
846 "TCP/IP Header decompression failed!\n");
847 talloc_free(expnd);
848 return -EIO;
849 }
850
851 /* Modify npu length, expnd is handed directly handed
852 * over to gsn_rx_sndcp_ud_ind(), see below */
853 npdu_len = rc;
854 } else
855 expnd = npdu;
856#if DEBUG_IP_PACKETS == 1
857 debug_ip_packet(expnd, npdu_len, 1, "sndcp_llunitdata_ind()");
858 DEBUGP(DSNDCP, "===================================================\n");
859 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
860 DEBUGP(DSNDCP, " \n");
861#endif
862
863 /* Hand off packet to gtp */
864 rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, lle->llme->tlli,
865 sne->nsapi, msg, npdu_len, expnd);
866
867 if (any_pcomp_or_dcomp_active(sgsn))
868 talloc_free(expnd);
869
870 return rc;
Harald Welte96f71f22010-05-03 19:28:05 +0200871}
872
Holger Hans Peter Freythercfee9522014-04-04 12:43:08 +0200873#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200874/* Section 5.1.2.1 LL-RESET.ind */
Harald Weltef78a3b22010-06-30 17:21:19 +0200875static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200876{
877 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
878 /* reset all SNDCP XID parameters to default values */
Holger Hans Peter Freyther6142dc42011-10-14 23:37:27 +0200879 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
880 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200881}
882
Harald Welte2720e732010-05-17 00:44:57 +0200883static int sndcp_ll_status_ind()
884{
885 /* inform the SM sub-layer by means of SNSM-STATUS.req */
Holger Hans Peter Freyther6142dc42011-10-14 23:37:27 +0200886 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
887 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200888}
889
890static struct sndcp_state_list {{
891 uint32_t states;
892 unsigned int type;
Harald Weltef78a3b22010-06-30 17:21:19 +0200893 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
Harald Welte2720e732010-05-17 00:44:57 +0200894} sndcp_state_list[] = {
895 { ALL_STATES,
896 LL_RESET_IND, sndcp_ll_reset_ind },
897 { ALL_STATES,
898 LL_ESTABLISH_IND, sndcp_ll_est_ind },
899 { SBIT(SNDCP_S_EST_RQD),
900 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
901 { SBIT(SNDCP_S_EST_RQD),
902 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
903 { SBIT(SNDCP_S_
904};
905
906static int sndcp_rx_llc_prim()
907{
908 case LL_ESTABLISH_REQ:
909 case LL_RELEASE_REQ:
910 case LL_XID_REQ:
911 case LL_DATA_REQ:
912 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
913
914 switch (prim) {
915 case LL_RESET_IND:
916 case LL_ESTABLISH_IND:
917 case LL_ESTABLISH_RESP:
918 case LL_ESTABLISH_CONF:
919 case LL_RELEASE_IND:
920 case LL_RELEASE_CONF:
921 case LL_XID_IND:
922 case LL_XID_RESP:
923 case LL_XID_CONF:
924 case LL_DATA_IND:
925 case LL_DATA_CONF:
926 case LL_UNITDATA_IND:
927 case LL_STATUS_IND:
Neels Hofmeyrcc7db182016-12-18 23:52:38 +0100928 }
Harald Welte2720e732010-05-17 00:44:57 +0200929}
Harald Welteebabdea2010-06-01 18:28:10 +0200930#endif
Philippf1f34362016-08-26 17:00:21 +0200931
932/* Generate SNDCP-XID message */
933static int gprs_llc_gen_sndcp_xid(uint8_t *bytes, int bytes_len, uint8_t nsapi)
934{
935 int entity = 0;
936 LLIST_HEAD(comp_fields);
937 struct gprs_sndcp_pcomp_rfc1144_params rfc1144_params;
938 struct gprs_sndcp_comp_field rfc1144_comp_field;
Philipp73f83d52016-09-02 13:38:01 +0200939 struct gprs_sndcp_dcomp_v42bis_params v42bis_params;
940 struct gprs_sndcp_comp_field v42bis_comp_field;
Philippf1f34362016-08-26 17:00:21 +0200941
942 memset(&rfc1144_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
Philipp73f83d52016-09-02 13:38:01 +0200943 memset(&v42bis_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
Philippf1f34362016-08-26 17:00:21 +0200944
945 /* Setup rfc1144 */
946 if (sgsn->cfg.pcomp_rfc1144.active) {
947 rfc1144_params.nsapi[0] = nsapi;
948 rfc1144_params.nsapi_len = 1;
949 rfc1144_params.s01 = sgsn->cfg.pcomp_rfc1144.s01;
950 rfc1144_comp_field.p = 1;
951 rfc1144_comp_field.entity = entity;
Stefan Sperlingc5721542018-11-07 16:33:39 +0100952 rfc1144_comp_field.algo.pcomp = RFC_1144;
Philippf1f34362016-08-26 17:00:21 +0200953 rfc1144_comp_field.comp[RFC1144_PCOMP1] = 1;
954 rfc1144_comp_field.comp[RFC1144_PCOMP2] = 2;
955 rfc1144_comp_field.comp_len = RFC1144_PCOMP_NUM;
956 rfc1144_comp_field.rfc1144_params = &rfc1144_params;
957 entity++;
958 llist_add(&rfc1144_comp_field.list, &comp_fields);
959 }
960
Philipp73f83d52016-09-02 13:38:01 +0200961 /* Setup V.42bis */
962 if (sgsn->cfg.dcomp_v42bis.active) {
963 v42bis_params.nsapi[0] = nsapi;
964 v42bis_params.nsapi_len = 1;
965 v42bis_params.p0 = sgsn->cfg.dcomp_v42bis.p0;
966 v42bis_params.p1 = sgsn->cfg.dcomp_v42bis.p1;
967 v42bis_params.p2 = sgsn->cfg.dcomp_v42bis.p2;
968 v42bis_comp_field.p = 1;
969 v42bis_comp_field.entity = entity;
Stefan Sperlingc5721542018-11-07 16:33:39 +0100970 v42bis_comp_field.algo.dcomp = V42BIS;
Philipp73f83d52016-09-02 13:38:01 +0200971 v42bis_comp_field.comp[V42BIS_DCOMP1] = 1;
972 v42bis_comp_field.comp_len = V42BIS_DCOMP_NUM;
973 v42bis_comp_field.v42bis_params = &v42bis_params;
974 entity++;
975 llist_add(&v42bis_comp_field.list, &comp_fields);
976 }
977
Philippdb142dc2016-12-22 14:15:20 +0100978 /* Do not attempt to compile anything if there is no data in the list */
979 if (llist_empty(&comp_fields))
980 return 0;
981
Philippf1f34362016-08-26 17:00:21 +0200982 /* Compile bytestream */
Philippdb142dc2016-12-22 14:15:20 +0100983 return gprs_sndcp_compile_xid(bytes, bytes_len, &comp_fields,
984 DEFAULT_SNDCP_VERSION);
Philippf1f34362016-08-26 17:00:21 +0200985}
986
987/* Set of SNDCP-XID bnegotiation (See also: TS 144 065,
988 * Section 6.8 XID parameter negotiation) */
989int sndcp_sn_xid_req(struct gprs_llc_lle *lle, uint8_t nsapi)
990{
991 /* Note: The specification requires the SNDCP-User to set of an
992 * SNDCP xid request. See also 3GPP TS 44.065, 6.8 XID parameter
993 * negotiation, Figure 11: SNDCP XID negotiation procedure. In
994 * our case the SNDCP-User is sgsn_libgtp.c, which calls
995 * sndcp_sn_xid_req directly. */
996
997 uint8_t l3params[1024];
998 int xid_len;
999 struct gprs_llc_xid_field xid_field_request;
1000
1001 /* Wipe off all compression entities and their states to
1002 * get rid of possible leftovers from a previous session */
1003 gprs_sndcp_comp_free(lle->llme->comp.proto);
1004 gprs_sndcp_comp_free(lle->llme->comp.data);
1005 lle->llme->comp.proto = gprs_sndcp_comp_alloc(lle->llme);
1006 lle->llme->comp.data = gprs_sndcp_comp_alloc(lle->llme);
Harald Welteaf779d22019-04-12 16:56:04 +02001007 talloc_free(lle->xid);
1008 lle->xid = NULL;
Philippf1f34362016-08-26 17:00:21 +02001009
1010 /* Generate compression parameter bytestream */
1011 xid_len = gprs_llc_gen_sndcp_xid(l3params, sizeof(l3params), nsapi);
1012
1013 /* Send XID with the SNDCP-XID bytetsream included */
1014 if (xid_len > 0) {
1015 xid_field_request.type = GPRS_LLC_XID_T_L3_PAR;
1016 xid_field_request.data = l3params;
1017 xid_field_request.data_len = xid_len;
1018 return gprs_ll_xid_req(lle, &xid_field_request);
1019 }
1020
1021 /* When bytestream can not be generated, proceed without SNDCP-XID */
1022 return gprs_ll_xid_req(lle, NULL);
1023
1024}
1025
1026/* Handle header compression entites */
1027static int handle_pcomp_entities(struct gprs_sndcp_comp_field *comp_field,
1028 struct gprs_llc_lle *lle)
1029{
1030 /* Note: This functions also transforms the comp_field into its
1031 * echo form (strips comp values, resets propose bit etc...)
1032 * the processed comp_fields can then be sent back as XID-
1033 * Response without further modification. */
1034
1035 /* Delete propose bit */
1036 comp_field->p = 0;
1037
1038 /* Process proposed parameters */
Stefan Sperlingc5721542018-11-07 16:33:39 +01001039 switch (comp_field->algo.pcomp) {
Philippf1f34362016-08-26 17:00:21 +02001040 case RFC_1144:
1041 if (sgsn->cfg.pcomp_rfc1144.passive
1042 && comp_field->rfc1144_params->nsapi_len > 0) {
1043 DEBUGP(DSNDCP,
1044 "Accepting RFC1144 header compression...\n");
1045 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.proto,
1046 comp_field);
1047 } else {
1048 DEBUGP(DSNDCP,
1049 "Rejecting RFC1144 header compression...\n");
1050 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1051 comp_field->entity);
1052 comp_field->rfc1144_params->nsapi_len = 0;
1053 }
1054 break;
1055 case RFC_2507:
1056 /* RFC 2507 is not yet supported,
1057 * so we set applicable nsapis to zero */
1058 DEBUGP(DSNDCP, "Rejecting RFC2507 header compression...\n");
1059 comp_field->rfc2507_params->nsapi_len = 0;
1060 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1061 comp_field->entity);
1062 break;
1063 case ROHC:
1064 /* ROHC is not yet supported,
1065 * so we set applicable nsapis to zero */
1066 DEBUGP(DSNDCP, "Rejecting ROHC header compression...\n");
1067 comp_field->rohc_params->nsapi_len = 0;
1068 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1069 comp_field->entity);
1070 break;
1071 }
1072
1073 return 0;
1074}
1075
1076/* Hanle data compression entites */
1077static int handle_dcomp_entities(struct gprs_sndcp_comp_field *comp_field,
1078 struct gprs_llc_lle *lle)
1079{
1080 /* See note in handle_pcomp_entities() */
1081
1082 /* Delete propose bit */
1083 comp_field->p = 0;
1084
1085 /* Process proposed parameters */
Stefan Sperlingc5721542018-11-07 16:33:39 +01001086 switch (comp_field->algo.dcomp) {
Philippf1f34362016-08-26 17:00:21 +02001087 case V42BIS:
Philipp73f83d52016-09-02 13:38:01 +02001088 if (sgsn->cfg.dcomp_v42bis.passive &&
1089 comp_field->v42bis_params->nsapi_len > 0) {
1090 DEBUGP(DSNDCP,
1091 "Accepting V.42bis data compression...\n");
1092 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.data,
1093 comp_field);
1094 } else {
1095 LOGP(DSNDCP, LOGL_DEBUG,
1096 "Rejecting V.42bis data compression...\n");
1097 gprs_sndcp_comp_delete(lle->llme->comp.data,
1098 comp_field->entity);
1099 comp_field->v42bis_params->nsapi_len = 0;
1100 }
Philippf1f34362016-08-26 17:00:21 +02001101 break;
1102 case V44:
1103 /* V44 is not yet supported,
1104 * so we set applicable nsapis to zero */
1105 DEBUGP(DSNDCP, "Rejecting V.44 data compression...\n");
1106 comp_field->v44_params->nsapi_len = 0;
1107 gprs_sndcp_comp_delete(lle->llme->comp.data,
1108 comp_field->entity);
1109 break;
1110 }
1111
1112 return 0;
1113
1114}
1115
1116/* Process SNDCP-XID indication
1117 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1118int sndcp_sn_xid_ind(struct gprs_llc_xid_field *xid_field_indication,
1119 struct gprs_llc_xid_field *xid_field_response,
1120 struct gprs_llc_lle *lle)
1121{
1122 /* Note: This function computes the SNDCP-XID response that is sent
1123 * back to the ms when a ms originated XID is received. The
1124 * Input XID fields are directly processed and the result is directly
1125 * handed back. */
1126
1127 int rc;
1128 int compclass;
Philippdb142dc2016-12-22 14:15:20 +01001129 int version;
Philippf1f34362016-08-26 17:00:21 +02001130
1131 struct llist_head *comp_fields;
1132 struct gprs_sndcp_comp_field *comp_field;
1133
1134 OSMO_ASSERT(xid_field_indication);
1135 OSMO_ASSERT(xid_field_response);
1136 OSMO_ASSERT(lle);
1137
Keithbfd67d22019-04-29 18:23:10 +01001138 /* Some phones send zero byte length SNDCP frames
1139 * and do require a confirmation response. */
1140 if (xid_field_indication->data_len == 0) {
1141 xid_field_response->type = GPRS_LLC_XID_T_L3_PAR;
1142 xid_field_response->data_len = 0;
1143 return 0;
1144 }
1145
Philippf1f34362016-08-26 17:00:21 +02001146 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001147 comp_fields = gprs_sndcp_parse_xid(&version, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001148 xid_field_indication->data,
1149 xid_field_indication->data_len,
1150 NULL);
1151 if (!comp_fields)
1152 return -EINVAL;
1153
Philippf1f34362016-08-26 17:00:21 +02001154 /* Handle compression entites */
1155 DEBUGP(DSNDCP, "SNDCP-XID-IND (ms):\n");
1156 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1157
1158 llist_for_each_entry(comp_field, comp_fields, list) {
1159 compclass = gprs_sndcp_get_compression_class(comp_field);
1160 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1161 rc = handle_pcomp_entities(comp_field, lle);
1162 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1163 rc = handle_dcomp_entities(comp_field, lle);
1164 else {
1165 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1166 comp_field->entity);
1167 gprs_sndcp_comp_delete(lle->llme->comp.data,
1168 comp_field->entity);
1169 rc = 0;
1170 }
1171
1172 if (rc < 0) {
1173 talloc_free(comp_fields);
1174 return -EINVAL;
1175 }
1176 }
1177
1178 DEBUGP(DSNDCP, "SNDCP-XID-RES (sgsn):\n");
1179 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1180
1181 /* Reserve some memory to store the modified SNDCP-XID bytes */
1182 xid_field_response->data =
1183 talloc_zero_size(lle->llme, xid_field_indication->data_len);
1184
1185 /* Set Type flag for response */
1186 xid_field_response->type = GPRS_LLC_XID_T_L3_PAR;
1187
1188 /* Compile modified SNDCP-XID bytes */
1189 rc = gprs_sndcp_compile_xid(xid_field_response->data,
1190 xid_field_indication->data_len,
Philippdb142dc2016-12-22 14:15:20 +01001191 comp_fields, 0);
Philippf1f34362016-08-26 17:00:21 +02001192
1193 if (rc > 0)
1194 xid_field_response->data_len = rc;
1195 else {
1196 talloc_free(xid_field_response->data);
1197 xid_field_response->data = NULL;
1198 xid_field_response->data_len = 0;
1199 return -EINVAL;
1200 }
1201
1202 talloc_free(comp_fields);
1203
1204 return 0;
1205}
1206
1207/* Process SNDCP-XID indication
1208 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1209int sndcp_sn_xid_conf(struct gprs_llc_xid_field *xid_field_conf,
1210 struct gprs_llc_xid_field *xid_field_request,
1211 struct gprs_llc_lle *lle)
1212{
1213 /* Note: This function handles an incomming SNDCP-XID confirmiation.
1214 * Since the confirmation fields may lack important parameters we
1215 * will reconstruct these missing fields using the original request
1216 * we have sent. After that we will create (or delete) the
1217 * compression entites */
1218
1219 struct llist_head *comp_fields_req;
1220 struct llist_head *comp_fields_conf;
1221 struct gprs_sndcp_comp_field *comp_field;
1222 int rc;
1223 int compclass;
1224
1225 /* We need both, the confirmation that is sent back by the ms,
1226 * and the original request we have sent. If one of this is missing
1227 * we can not process the confirmation, the caller must check if
1228 * request and confirmation fields are available. */
1229 OSMO_ASSERT(xid_field_conf);
1230 OSMO_ASSERT(xid_field_request);
1231
1232 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001233 comp_fields_req = gprs_sndcp_parse_xid(NULL, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001234 xid_field_request->data,
1235 xid_field_request->data_len,
1236 NULL);
1237 if (!comp_fields_req)
1238 return -EINVAL;
1239
1240 DEBUGP(DSNDCP, "SNDCP-XID-REQ (sgsn):\n");
1241 gprs_sndcp_dump_comp_fields(comp_fields_req, LOGL_DEBUG);
1242
1243 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001244 comp_fields_conf = gprs_sndcp_parse_xid(NULL, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001245 xid_field_conf->data,
1246 xid_field_conf->data_len,
1247 comp_fields_req);
1248 if (!comp_fields_conf)
1249 return -EINVAL;
1250
1251 DEBUGP(DSNDCP, "SNDCP-XID-CONF (ms):\n");
1252 gprs_sndcp_dump_comp_fields(comp_fields_conf, LOGL_DEBUG);
1253
1254 /* Handle compression entites */
1255 llist_for_each_entry(comp_field, comp_fields_conf, list) {
1256 compclass = gprs_sndcp_get_compression_class(comp_field);
1257 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1258 rc = handle_pcomp_entities(comp_field, lle);
1259 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1260 rc = handle_dcomp_entities(comp_field, lle);
1261 else {
1262 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1263 comp_field->entity);
1264 gprs_sndcp_comp_delete(lle->llme->comp.data,
1265 comp_field->entity);
1266 rc = 0;
1267 }
1268
1269 if (rc < 0) {
1270 talloc_free(comp_fields_req);
1271 talloc_free(comp_fields_conf);
1272 return -EINVAL;
1273 }
1274 }
1275
1276 talloc_free(comp_fields_req);
1277 talloc_free(comp_fields_conf);
1278
1279 return 0;
1280}