blob: 3eae127fce1205117c7b488e49f1ca35b2d03df8 [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>
Pau Espin Pedrol4398ac02022-12-23 17:12:39 +010035#include <osmocom/sgsn/gprs_ns.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>
Pau Espin Pedrol05d5f282022-12-23 17:09:08 +010044#include <osmocom/sgsn/gprs_gmm.h>
Pau Espin Pedrol58101ea2023-01-09 12:29:27 +010045#include <osmocom/sgsn/mmctx.h>
Pau Espin Pedrol51028e22023-01-05 18:43:39 +010046#include <osmocom/sgsn/gtp.h>
Philippf1f34362016-08-26 17:00:21 +020047
48#define DEBUG_IP_PACKETS 0 /* 0=Disabled, 1=Enabled */
49
50#if DEBUG_IP_PACKETS == 1
51/* Calculate TCP/IP checksum */
52static uint16_t calc_ip_csum(uint8_t *data, int len)
53{
54 int i;
55 uint32_t accumulator = 0;
56 uint16_t *pointer = (uint16_t *) data;
57
58 for (i = len; i > 1; i -= 2) {
59 accumulator += *pointer;
60 pointer++;
61 }
62
63 if (len % 2)
64 accumulator += *pointer;
65
66 accumulator = (accumulator & 0xffff) + ((accumulator >> 16) & 0xffff);
67 accumulator += (accumulator >> 16) & 0xffff;
68 return (~accumulator);
69}
70
71/* Calculate TCP/IP checksum */
Maxcaff83e2022-10-09 13:32:09 +030072static uint16_t calc_tcpip_csum(const void *ctx, const uint8_t *packet, int len)
Philippf1f34362016-08-26 17:00:21 +020073{
74 uint8_t *buf;
75 uint16_t csum;
76
77 buf = talloc_zero_size(ctx, len);
78 memset(buf, 0, len);
79 memcpy(buf, packet + 12, 8);
80 buf[9] = packet[9];
81 buf[11] = (len - 20) & 0xFF;
82 buf[10] = (len - 20) >> 8 & 0xFF;
83 memcpy(buf + 12, packet + 20, len - 20);
84 csum = calc_ip_csum(buf, len - 20 + 12);
85 talloc_free(buf);
86 return csum;
87}
88
89/* Show some ip packet details */
Maxcaff83e2022-10-09 13:32:09 +030090static void debug_ip_packet(const uint8_t *data, int len, int dir, const char *info)
Philippf1f34362016-08-26 17:00:21 +020091{
92 uint8_t tcp_flags;
93 char flags_debugmsg[256];
94 int len_short;
95 static unsigned int packet_count = 0;
96 static unsigned int tcp_csum_err_count = 0;
97 static unsigned int ip_csum_err_count = 0;
98
99 packet_count++;
100
101 if (len > 80)
102 len_short = 80;
103 else
104 len_short = len;
105
106 if (dir)
107 DEBUGP(DSNDCP, "%s: MS => SGSN: %s\n", info,
108 osmo_hexdump_nospc(data, len_short));
109 else
110 DEBUGP(DSNDCP, "%s: MS <= SGSN: %s\n", info,
111 osmo_hexdump_nospc(data, len_short));
112
113 DEBUGP(DSNDCP, "%s: Length.: %d\n", info, len);
114 DEBUGP(DSNDCP, "%s: NO.: %d\n", info, packet_count);
115
116 if (len < 20) {
117 DEBUGP(DSNDCP, "%s: Error: Short IP packet!\n", info);
118 return;
119 }
120
121 if (calc_ip_csum(data, 20) != 0) {
122 DEBUGP(DSNDCP, "%s: Bad IP-Header checksum!\n", info);
123 ip_csum_err_count++;
124 } else
125 DEBUGP(DSNDCP, "%s: IP-Header checksum ok.\n", info);
126
127 if (data[9] == 0x06) {
128 if (len < 40) {
129 DEBUGP(DSNDCP, "%s: Error: Short TCP packet!\n", info);
130 return;
131 }
132
133 DEBUGP(DSNDCP, "%s: Protocol type: TCP\n", info);
134 tcp_flags = data[33];
135
136 if (calc_tcpip_csum(NULL, data, len) != 0) {
137 DEBUGP(DSNDCP, "%s: Bad TCP checksum!\n", info);
138 tcp_csum_err_count++;
139 } else
140 DEBUGP(DSNDCP, "%s: TCP checksum ok.\n", info);
141
142 memset(flags_debugmsg, 0, sizeof(flags_debugmsg));
143 if (tcp_flags & 1)
144 strcat(flags_debugmsg, "FIN ");
145 if (tcp_flags & 2)
146 strcat(flags_debugmsg, "SYN ");
147 if (tcp_flags & 4)
148 strcat(flags_debugmsg, "RST ");
149 if (tcp_flags & 8)
150 strcat(flags_debugmsg, "PSH ");
151 if (tcp_flags & 16)
152 strcat(flags_debugmsg, "ACK ");
153 if (tcp_flags & 32)
154 strcat(flags_debugmsg, "URG ");
155 DEBUGP(DSNDCP, "%s: FLAGS: %s\n", info, flags_debugmsg);
156 } else if (data[9] == 0x11) {
157 DEBUGP(DSNDCP, "%s: Protocol type: UDP\n", info);
158 } else {
159 DEBUGP(DSNDCP, "%s: Protocol type: (%02x)\n", info, data[9]);
160 }
161
162 DEBUGP(DSNDCP, "%s: IP-Header checksum errors: %d\n", info,
163 ip_csum_err_count);
164 DEBUGP(DSNDCP, "%s: TCP-Checksum errors: %d\n", info,
165 tcp_csum_err_count);
166}
167#endif
Harald Weltef78a3b22010-06-30 17:21:19 +0200168
Harald Welte96f71f22010-05-03 19:28:05 +0200169/* Chapter 7.2: SN-PDU Formats */
170struct sndcp_common_hdr {
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100171#if OSMO_IS_LITTLE_ENDIAN
Harald Welte96f71f22010-05-03 19:28:05 +0200172 /* octet 1 */
173 uint8_t nsapi:4;
174 uint8_t more:1;
175 uint8_t type:1;
176 uint8_t first:1;
177 uint8_t spare:1;
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100178#elif OSMO_IS_BIG_ENDIAN
Oliver Smithf8a50662023-02-20 10:51:48 +0100179/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianness.py) */
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100180 uint8_t spare:1, first:1, type:1, more:1, nsapi:4;
181#endif
Harald Weltece22f922010-06-03 21:21:21 +0200182} __attribute__((packed));
183
184/* PCOMP / DCOMP only exist in first fragment */
185struct sndcp_comp_hdr {
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100186#if OSMO_IS_LITTLE_ENDIAN
Harald Welte96f71f22010-05-03 19:28:05 +0200187 /* octet 2 */
Harald Welte5cc2bc32010-06-02 23:17:05 +0200188 uint8_t pcomp:4;
189 uint8_t dcomp:4;
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100190#elif OSMO_IS_BIG_ENDIAN
Oliver Smithf8a50662023-02-20 10:51:48 +0100191/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianness.py) */
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100192 uint8_t dcomp:4, pcomp:4;
193#endif
Harald Welteebabdea2010-06-01 18:28:10 +0200194} __attribute__((packed));
Harald Welte96f71f22010-05-03 19:28:05 +0200195
196struct sndcp_udata_hdr {
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100197#if OSMO_IS_LITTLE_ENDIAN
Harald Welte96f71f22010-05-03 19:28:05 +0200198 /* octet 3 */
199 uint8_t npdu_high:4;
200 uint8_t seg_nr:4;
201 /* octet 4 */
202 uint8_t npdu_low;
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100203#elif OSMO_IS_BIG_ENDIAN
Oliver Smithf8a50662023-02-20 10:51:48 +0100204/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianness.py) */
Pau Espin Pedrol4be5ab32021-02-04 12:49:40 +0100205 uint8_t seg_nr:4, npdu_high:4;
206 uint8_t npdu_low;
207#endif
Harald Welteebabdea2010-06-01 18:28:10 +0200208} __attribute__((packed));
209
Harald Welteebabdea2010-06-01 18:28:10 +0200210
211static void *tall_sndcp_ctx;
212
213/* A fragment queue entry, containing one framgent of a N-PDU */
Harald Weltece22f922010-06-03 21:21:21 +0200214struct defrag_queue_entry {
Harald Welteebabdea2010-06-01 18:28:10 +0200215 struct llist_head list;
Harald Weltece22f922010-06-03 21:21:21 +0200216 /* segment number of this fragment */
217 uint32_t seg_nr;
218 /* length of the data area of this fragment */
Harald Welteebabdea2010-06-01 18:28:10 +0200219 uint32_t data_len;
Harald Weltece22f922010-06-03 21:21:21 +0200220 /* pointer to the data of this fragment */
221 uint8_t *data;
Harald Welteebabdea2010-06-01 18:28:10 +0200222};
223
Harald Weltef78a3b22010-06-30 17:21:19 +0200224LLIST_HEAD(gprs_sndcp_entities);
Harald Welte96f71f22010-05-03 19:28:05 +0200225
Philippf1f34362016-08-26 17:00:21 +0200226/* Check if any compression parameters are set in the sgsn configuration */
Maxcaff83e2022-10-09 13:32:09 +0300227static inline int any_pcomp_or_dcomp_active(const struct sgsn_instance *sgsn)
228{
Philipp73f83d52016-09-02 13:38:01 +0200229 if (sgsn->cfg.pcomp_rfc1144.active || sgsn->cfg.pcomp_rfc1144.passive ||
230 sgsn->cfg.dcomp_v42bis.active || sgsn->cfg.dcomp_v42bis.passive)
Philippf1f34362016-08-26 17:00:21 +0200231 return true;
232 else
233 return false;
234}
235
Harald Weltece22f922010-06-03 21:21:21 +0200236/* Enqueue a fragment into the defragment queue */
Harald Weltef78a3b22010-06-30 17:21:19 +0200237static int defrag_enqueue(struct gprs_sndcp_entity *sne, uint8_t seg_nr,
Harald Welte3d6815a2010-07-02 17:16:07 +0200238 uint8_t *data, uint32_t data_len)
Harald Welteebabdea2010-06-01 18:28:10 +0200239{
Harald Weltece22f922010-06-03 21:21:21 +0200240 struct defrag_queue_entry *dqe;
Harald Welteebabdea2010-06-01 18:28:10 +0200241
Harald Weltece22f922010-06-03 21:21:21 +0200242 dqe = talloc_zero(tall_sndcp_ctx, struct defrag_queue_entry);
243 if (!dqe)
244 return -ENOMEM;
245 dqe->data = talloc_zero_size(dqe, data_len);
246 if (!dqe->data) {
247 talloc_free(dqe);
248 return -ENOMEM;
249 }
250 dqe->seg_nr = seg_nr;
251 dqe->data_len = data_len;
252
253 llist_add(&dqe->list, &sne->defrag.frag_list);
254
255 if (seg_nr > sne->defrag.highest_seg)
256 sne->defrag.highest_seg = seg_nr;
257
258 sne->defrag.seg_have |= (1 << seg_nr);
259 sne->defrag.tot_len += data_len;
260
Harald Welte8f0c0a32010-07-02 10:29:06 +0200261 memcpy(dqe->data, data, data_len);
262
Harald Weltece22f922010-06-03 21:21:21 +0200263 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200264}
265
Harald Weltece22f922010-06-03 21:21:21 +0200266/* return if we have all segments of this N-PDU */
Maxcaff83e2022-10-09 13:32:09 +0300267static int defrag_have_all_segments(const struct gprs_sndcp_entity *sne)
Harald Welteebabdea2010-06-01 18:28:10 +0200268{
Harald Weltece22f922010-06-03 21:21:21 +0200269 uint32_t seg_needed = 0;
270 unsigned int i;
Harald Welteebabdea2010-06-01 18:28:10 +0200271
Harald Weltece22f922010-06-03 21:21:21 +0200272 /* create a bitmask of needed segments */
Harald Welte951a12c2010-07-01 15:09:45 +0200273 for (i = 0; i <= sne->defrag.highest_seg; i++)
Harald Weltece22f922010-06-03 21:21:21 +0200274 seg_needed |= (1 << i);
275
276 if (seg_needed == sne->defrag.seg_have)
277 return 1;
278
279 return 0;
Harald Welteebabdea2010-06-01 18:28:10 +0200280}
281
Maxcaff83e2022-10-09 13:32:09 +0300282static struct defrag_queue_entry *defrag_get_seg(const struct gprs_sndcp_entity *sne,
Harald Weltece22f922010-06-03 21:21:21 +0200283 uint32_t seg_nr)
Harald Welteebabdea2010-06-01 18:28:10 +0200284{
Harald Weltece22f922010-06-03 21:21:21 +0200285 struct defrag_queue_entry *dqe;
286
287 llist_for_each_entry(dqe, &sne->defrag.frag_list, list) {
288 if (dqe->seg_nr == seg_nr) {
289 llist_del(&dqe->list);
290 return dqe;
291 }
292 }
293 return NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200294}
Harald Weltece22f922010-06-03 21:21:21 +0200295
Pau Espin Pedrol57b63872022-12-06 11:50:26 +0100296/* Returns talloced buffer containing decompressed data, NULL on error. */
297static uint8_t *decompress_segment(struct gprs_sndcp_entity *sne, void *ctx,
298 const uint8_t *compressed_data, unsigned int compressed_data_len,
299 unsigned int *decompressed_data_len)
300{
301 int rc;
302 uint8_t *expnd = NULL;
303 *decompressed_data_len = 0;
304
305#if DEBUG_IP_PACKETS == 1
306 DEBUGP(DSNDCP, "\n");
307 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
308 DEBUGP(DSNDCP, "===================================================\n");
309#endif
310
311 expnd = talloc_zero_size(ctx, compressed_data_len * MAX_DATADECOMPR_FAC +
312 MAX_HDRDECOMPR_INCR);
313 memcpy(expnd, compressed_data, compressed_data_len);
314
315 /* Apply data decompression */
316 rc = gprs_sndcp_dcomp_expand(expnd, compressed_data_len, sne->defrag.dcomp,
317 sne->defrag.data);
318 if (rc < 0) {
319 LOGP(DSNDCP, LOGL_ERROR,
320 "Data decompression failed!\n");
321 talloc_free(expnd);
322 return NULL;
323 }
324
325 /* Apply header decompression */
326 rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp, sne->defrag.proto);
327 if (rc < 0) {
328 LOGP(DSNDCP, LOGL_ERROR,
329 "TCP/IP Header decompression failed!\n");
330 talloc_free(expnd);
331 return NULL;
332 }
333
334 *decompressed_data_len = rc;
335
336#if DEBUG_IP_PACKETS == 1
337 debug_ip_packet(expnd, *decompressed_data_len, 1, "defrag_segments()");
338 DEBUGP(DSNDCP, "===================================================\n");
339 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
340 DEBUGP(DSNDCP, "\n");
341#endif
342 return expnd;
343}
344
Harald Welte8b705f22010-07-02 16:18:59 +0200345/* Perform actual defragmentation and create an output packet */
Harald Weltef78a3b22010-06-30 17:21:19 +0200346static int defrag_segments(struct gprs_sndcp_entity *sne)
Harald Weltece22f922010-06-03 21:21:21 +0200347{
348 struct msgb *msg;
349 unsigned int seg_nr;
350 uint8_t *npdu;
Pau Espin Pedrol57b63872022-12-06 11:50:26 +0100351 unsigned int npdu_len;
Philippf1f34362016-08-26 17:00:21 +0200352 int rc;
353 uint8_t *expnd = NULL;
Harald Weltece22f922010-06-03 21:21:21 +0200354
Harald Welteab4094c2010-07-02 16:01:47 +0200355 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Defragment output PDU %u "
356 "num_seg=%u tot_len=%u\n", sne->lle->llme->tlli, sne->nsapi,
357 sne->defrag.npdu, sne->defrag.highest_seg, sne->defrag.tot_len);
Sylvain Munauteda125c2010-06-09 20:56:52 +0200358 msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag");
Harald Weltece22f922010-06-03 21:21:21 +0200359 if (!msg)
360 return -ENOMEM;
361
362 /* FIXME: message headers + identifiers */
363
364 npdu = msg->data;
365
Harald Welte993697c2010-07-02 10:11:42 +0200366 for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) {
Harald Weltece22f922010-06-03 21:21:21 +0200367 struct defrag_queue_entry *dqe;
368 uint8_t *data;
369
370 dqe = defrag_get_seg(sne, seg_nr);
371 if (!dqe) {
372 LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr);
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100373 msgb_free(msg);
Harald Weltece22f922010-06-03 21:21:21 +0200374 return -EIO;
375 }
376 /* actually append the segment to the N-PDU */
377 data = msgb_put(msg, dqe->data_len);
378 memcpy(data, dqe->data, dqe->data_len);
379
380 /* release memory for the fragment queue entry */
381 talloc_free(dqe);
382 }
383
Philippf1f34362016-08-26 17:00:21 +0200384 npdu_len = sne->defrag.tot_len;
385
Harald Welte8b705f22010-07-02 16:18:59 +0200386 /* FIXME: cancel timer */
387
Harald Weltece22f922010-06-03 21:21:21 +0200388 /* actually send the N-PDU to the SGSN core code, which then
389 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Philippf1f34362016-08-26 17:00:21 +0200390
391 /* Decompress packet */
Philippf1f34362016-08-26 17:00:21 +0200392 if (any_pcomp_or_dcomp_active(sgsn)) {
Pau Espin Pedrol57b63872022-12-06 11:50:26 +0100393 expnd = decompress_segment(sne, msg, npdu, npdu_len, &npdu_len);
394 if (!expnd) {
395 rc = -EIO;
396 goto ret_free;
Philipp73f83d52016-09-02 13:38:01 +0200397 }
Pau Espin Pedrol57b63872022-12-06 11:50:26 +0100398 } else {
Philippf1f34362016-08-26 17:00:21 +0200399 expnd = npdu;
Pau Espin Pedrol57b63872022-12-06 11:50:26 +0100400 }
Philippf1f34362016-08-26 17:00:21 +0200401
Pau Espin Pedrol51028e22023-01-05 18:43:39 +0100402 /* Hand off packet to SGSN (SNDCP SN-UNITDATA.ind), which will forward it to GGSN (GTP): */
Pau Espin Pedrol4bd6f662023-01-05 19:00:49 +0100403 rc = sndcp_sn_unitdata_ind(sne, msg, npdu_len, expnd);
Philippf1f34362016-08-26 17:00:21 +0200404
Pau Espin Pedrol57b63872022-12-06 11:50:26 +0100405ret_free:
Harald Welte627e2852020-06-08 20:46:53 +0200406 /* we must free the memory we allocated above; ownership is not transferred
407 * downwards in the call above */
408 msgb_free(msg);
409
Philipp Maieref6205b2020-10-02 17:35:25 +0200410 /* Note: We do not have to free expnd explicitly, because it is created
411 * within the talloc context of msg, which we just freed. */
Philippf1f34362016-08-26 17:00:21 +0200412
413 return rc;
Harald Weltece22f922010-06-03 21:21:21 +0200414}
415
Philippf1f34362016-08-26 17:00:21 +0200416static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg,
417 uint8_t *hdr, unsigned int len)
Harald Weltece22f922010-06-03 21:21:21 +0200418{
419 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200420 struct sndcp_udata_hdr *suh;
421 uint16_t npdu_num;
422 uint8_t *data;
423 int rc;
424
425 sch = (struct sndcp_common_hdr *) hdr;
426 if (sch->first) {
Harald Weltece22f922010-06-03 21:21:21 +0200427 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
428 } else
429 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
430
431 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
432
433 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
434
Harald Welteab4094c2010-07-02 16:01:47 +0200435 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u "
436 "Length %u %s %s\n", sne->lle->llme->tlli, sne->nsapi, npdu_num,
437 suh->seg_nr, len, sch->first ? "F " : "", sch->more ? "M" : "");
Harald Welteb87bc862010-07-01 20:29:20 +0200438
Harald Weltece22f922010-06-03 21:21:21 +0200439 if (sch->first) {
440 /* first segment of a new packet. Discard all leftover fragments of
441 * previous packet */
442 if (!llist_empty(&sne->defrag.frag_list)) {
Harald Welte65d96782010-07-01 12:19:02 +0200443 struct defrag_queue_entry *dqe, *dqe2;
Harald Welteb87bc862010-07-01 20:29:20 +0200444 LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping "
445 "SN-PDU %u due to insufficient segments (%04x)\n",
446 sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu,
447 sne->defrag.seg_have);
Harald Welte65d96782010-07-01 12:19:02 +0200448 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
Harald Weltece22f922010-06-03 21:21:21 +0200449 llist_del(&dqe->list);
450 talloc_free(dqe);
451 }
452 }
453 /* store the currently de-fragmented PDU number */
454 sne->defrag.npdu = npdu_num;
Harald Welte8b705f22010-07-02 16:18:59 +0200455
456 /* Re-set fragmentation state */
Harald Weltece22f922010-06-03 21:21:21 +0200457 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
Harald Welte8b705f22010-07-02 16:18:59 +0200458 sne->defrag.tot_len = 0;
459 /* FIXME: (re)start timer */
Harald Weltece22f922010-06-03 21:21:21 +0200460 }
461
462 if (sne->defrag.npdu != npdu_num) {
463 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
464 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
465 /* FIXME */
466 }
467
468 /* FIXME: check if seg_nr already exists */
Harald Welte3d6815a2010-07-02 17:16:07 +0200469 /* make sure to subtract length of SNDCP header from 'len' */
470 rc = defrag_enqueue(sne, suh->seg_nr, data, len - (data - hdr));
Harald Weltece22f922010-06-03 21:21:21 +0200471 if (rc < 0)
472 return rc;
473
474 if (!sch->more) {
475 /* this is suppsed to be the last segment of the N-PDU, but it
476 * might well be not the last to arrive */
477 sne->defrag.no_more = 1;
478 }
479
480 if (sne->defrag.no_more) {
481 /* we have already received the last segment before, let's check
482 * if all the previous segments exist */
483 if (defrag_have_all_segments(sne))
484 return defrag_segments(sne);
485 }
486
487 return 0;
488}
Harald Welteebabdea2010-06-01 18:28:10 +0200489
Harald Weltef78a3b22010-06-30 17:21:19 +0200490static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200491 uint8_t nsapi)
492{
Harald Weltef78a3b22010-06-30 17:21:19 +0200493 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200494
Harald Weltef78a3b22010-06-30 17:21:19 +0200495 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
Harald Welteebabdea2010-06-01 18:28:10 +0200496 if (sne->lle == lle && sne->nsapi == nsapi)
497 return sne;
498 }
499 return NULL;
500}
501
Harald Weltef78a3b22010-06-30 17:21:19 +0200502static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle,
Harald Welteebabdea2010-06-01 18:28:10 +0200503 uint8_t nsapi)
504{
Harald Weltef78a3b22010-06-30 17:21:19 +0200505 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200506
Harald Weltef78a3b22010-06-30 17:21:19 +0200507 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
Harald Welteebabdea2010-06-01 18:28:10 +0200508 if (!sne)
509 return NULL;
510
511 sne->lle = lle;
512 sne->nsapi = nsapi;
Harald Weltece22f922010-06-03 21:21:21 +0200513 sne->defrag.timer.data = sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200514 //sne->fqueue.timer.cb = FIXME;
515 sne->rx_state = SNDCP_RX_S_FIRST;
Harald Welte362aea02010-07-01 12:31:10 +0200516 INIT_LLIST_HEAD(&sne->defrag.frag_list);
Harald Welteebabdea2010-06-01 18:28:10 +0200517
Harald Weltef78a3b22010-06-30 17:21:19 +0200518 llist_add(&sne->list, &gprs_sndcp_entities);
Harald Welte61444522010-06-02 12:40:48 +0200519
Harald Welteebabdea2010-06-01 18:28:10 +0200520 return sne;
521}
522
523/* Entry point for the SNSM-ACTIVATE.indication */
524int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
525{
Harald Welte61444522010-06-02 12:40:48 +0200526 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
527 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200528
Harald Weltef78a3b22010-06-30 17:21:19 +0200529 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200530 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
531 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
532 lle->llme->tlli, nsapi);
533 return -EEXIST;
534 }
535
Harald Weltef78a3b22010-06-30 17:21:19 +0200536 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
Harald Welte16836a32010-06-02 10:25:40 +0200537 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200538 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200539 }
Harald Welteebabdea2010-06-01 18:28:10 +0200540
541 return 0;
542}
543
Harald Weltece22f922010-06-03 21:21:21 +0200544/* Entry point for the SNSM-DEACTIVATE.indication */
Maxcaff83e2022-10-09 13:32:09 +0300545int sndcp_sm_deactivate_ind(const struct gprs_llc_lle *lle, uint8_t nsapi)
Harald Weltece22f922010-06-03 21:21:21 +0200546{
Harald Weltef78a3b22010-06-30 17:21:19 +0200547 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200548
549 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
550 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
551
Harald Weltef78a3b22010-06-30 17:21:19 +0200552 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltece22f922010-06-03 21:21:21 +0200553 if (!sne) {
554 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
555 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
556 lle->sapi, nsapi);
557 return -ENOENT;
558 }
559 llist_del(&sne->list);
560 /* frag queue entries are hierarchically allocated, so no need to
561 * free them explicitly here */
562 talloc_free(sne);
563
564 return 0;
565}
566
Oliver Smithf7642852021-12-07 13:16:17 +0100567/* Clean up all gprs_sndcp_entities related to llme (OS#4824) */
Maxcaff83e2022-10-09 13:32:09 +0300568void gprs_sndcp_sm_deactivate_ind_by_llme(const struct gprs_llc_llme *llme)
Oliver Smithf7642852021-12-07 13:16:17 +0100569{
570 struct gprs_sndcp_entity *sne, *sne2;
571
572 llist_for_each_entry_safe(sne, sne2, &gprs_sndcp_entities, list) {
573 if (sne->lle->llme == llme) {
574 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind for SNDCP attached to llme=%p\n", llme);
575 /* Free and remove from list */
576 sndcp_sm_deactivate_ind(sne->lle, sne->nsapi);
577 }
578 }
579}
580
Harald Weltece22f922010-06-03 21:21:21 +0200581/* Fragmenter state */
582struct sndcp_frag_state {
583 uint8_t frag_nr;
584 struct msgb *msg; /* original message */
585 uint8_t *next_byte; /* first byte of next fragment */
586
Harald Weltef78a3b22010-06-30 17:21:19 +0200587 struct gprs_sndcp_entity *sne;
Harald Weltece22f922010-06-03 21:21:21 +0200588 void *mmcontext;
589};
590
591/* returns '1' if there are more fragments to send, '0' if none */
Philippf1f34362016-08-26 17:00:21 +0200592static int sndcp_send_ud_frag(struct sndcp_frag_state *fs,
593 uint8_t pcomp, uint8_t dcomp)
Harald Weltece22f922010-06-03 21:21:21 +0200594{
Harald Weltef78a3b22010-06-30 17:21:19 +0200595 struct gprs_sndcp_entity *sne = fs->sne;
Harald Weltece22f922010-06-03 21:21:21 +0200596 struct gprs_llc_lle *lle = sne->lle;
597 struct sndcp_common_hdr *sch;
598 struct sndcp_comp_hdr *scomph;
599 struct sndcp_udata_hdr *suh;
600 struct msgb *fmsg;
601 unsigned int max_payload_len;
602 unsigned int len;
603 uint8_t *data;
604 int rc, more;
605
Sylvain Munauteda125c2010-06-09 20:56:52 +0200606 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
Harald Weltece22f922010-06-03 21:21:21 +0200607 "SNDCP Frag");
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200608 if (!fmsg) {
609 msgb_free(fs->msg);
Harald Weltece22f922010-06-03 21:21:21 +0200610 return -ENOMEM;
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200611 }
Harald Weltece22f922010-06-03 21:21:21 +0200612
613 /* make sure lower layers route the fragment like the original */
614 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
615 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
616 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
617
618 /* prepend common SNDCP header */
619 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
620 sch->nsapi = sne->nsapi;
621 /* Set FIRST bit if we are the first fragment in a series */
622 if (fs->frag_nr == 0)
623 sch->first = 1;
624 sch->type = 1;
625
626 /* append the compression header for first fragment */
627 if (sch->first) {
628 scomph = (struct sndcp_comp_hdr *)
629 msgb_put(fmsg, sizeof(*scomph));
Philippf1f34362016-08-26 17:00:21 +0200630 scomph->pcomp = pcomp;
631 scomph->dcomp = dcomp;
Harald Weltece22f922010-06-03 21:21:21 +0200632 }
633
634 /* append the user-data header */
635 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
636 suh->npdu_low = sne->tx_npdu_nr & 0xff;
637 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
638 suh->seg_nr = fs->frag_nr % 0xf;
639
640 /* calculate remaining length to be sent */
641 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
642 /* how much payload can we actually send via LLC? */
643 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
644 if (sch->first)
645 max_payload_len -= sizeof(*scomph);
646 /* check if we're exceeding the max */
647 if (len > max_payload_len)
648 len = max_payload_len;
649
650 /* copy the actual fragment data into our fmsg */
651 data = msgb_put(fmsg, len);
652 memcpy(data, fs->next_byte, len);
653
654 /* Increment fragment number and data pointer to next fragment */
655 fs->frag_nr++;
656 fs->next_byte += len;
657
658 /* determine if we have more fragemnts to send */
659 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
660 more = 0;
661 else
662 more = 1;
663
664 /* set the MORE bit of the SNDCP header accordingly */
665 sch->more = more;
666
Max82040102016-07-06 11:59:18 +0200667 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext, true);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200668 /* abort in case of error, do not advance frag_nr / next_byte */
Harald Weltece22f922010-06-03 21:21:21 +0200669 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200670 msgb_free(fs->msg);
Harald Weltece22f922010-06-03 21:21:21 +0200671 return rc;
672 }
673
674 if (!more) {
675 /* we've sent all fragments */
676 msgb_free(fs->msg);
677 memset(fs, 0, sizeof(*fs));
678 /* increment NPDU number for next frame */
679 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
680 return 0;
681 }
682
683 /* default: more fragments to send */
684 return 1;
685}
686
Harald Weltedb2c39f2010-06-03 07:14:59 +0200687/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Pau Espin Pedrol4bd6f662023-01-05 19:00:49 +0100688int sndcp_sn_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
Harald Weltebb1c8052010-06-03 06:38:38 +0200689 void *mmcontext)
690{
Harald Weltef78a3b22010-06-30 17:21:19 +0200691 struct gprs_sndcp_entity *sne;
Harald Weltebb1c8052010-06-03 06:38:38 +0200692 struct sndcp_common_hdr *sch;
Harald Weltece22f922010-06-03 21:21:21 +0200693 struct sndcp_comp_hdr *scomph;
Harald Weltebb1c8052010-06-03 06:38:38 +0200694 struct sndcp_udata_hdr *suh;
Harald Weltece22f922010-06-03 21:21:21 +0200695 struct sndcp_frag_state fs;
Philippf1f34362016-08-26 17:00:21 +0200696 uint8_t pcomp = 0;
697 uint8_t dcomp = 0;
698 int rc;
Harald Weltebb1c8052010-06-03 06:38:38 +0200699
700 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
701
Philippf1f34362016-08-26 17:00:21 +0200702 /* Compress packet */
703#if DEBUG_IP_PACKETS == 1
704 DEBUGP(DSNDCP, " \n");
705 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
706 DEBUGP(DSNDCP, "===================================================\n");
707 debug_ip_packet(msg->data, msg->len, 0, "sndcp_initdata_req()");
708#endif
709 if (any_pcomp_or_dcomp_active(sgsn)) {
710
711 /* Apply header compression */
712 rc = gprs_sndcp_pcomp_compress(msg->data, msg->len, &pcomp,
713 lle->llme->comp.proto, nsapi);
714 if (rc < 0) {
715 LOGP(DSNDCP, LOGL_ERROR,
716 "TCP/IP Header compression failed!\n");
717 return -EIO;
718 }
719
720 /* Fixup pointer locations and sizes in message buffer to match
721 * the new, compressed buffer size */
722 msgb_get(msg, msg->len);
723 msgb_put(msg, rc);
Philipp73f83d52016-09-02 13:38:01 +0200724
725 /* Apply data compression */
726 rc = gprs_sndcp_dcomp_compress(msg->data, msg->len, &dcomp,
727 lle->llme->comp.data, nsapi);
728 if (rc < 0) {
729 LOGP(DSNDCP, LOGL_ERROR, "Data compression failed!\n");
730 return -EIO;
731 }
732
733 /* Fixup pointer locations and sizes in message buffer to match
734 * the new, compressed buffer size */
735 msgb_get(msg, msg->len);
736 msgb_put(msg, rc);
Philippf1f34362016-08-26 17:00:21 +0200737 }
738#if DEBUG_IP_PACKETS == 1
739 DEBUGP(DSNDCP, "===================================================\n");
740 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
741 DEBUGP(DSNDCP, " \n");
742#endif
743
Harald Weltef78a3b22010-06-30 17:21:19 +0200744 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Weltebb1c8052010-06-03 06:38:38 +0200745 if (!sne) {
Max559636a2022-10-08 20:24:22 +0300746 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity (lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n",
747 lle, lle->llme->tlli, lle->sapi, nsapi);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200748 msgb_free(msg);
Harald Weltebb1c8052010-06-03 06:38:38 +0200749 return -EIO;
750 }
751
Harald Weltece22f922010-06-03 21:21:21 +0200752 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
Max559636a2022-10-08 20:24:22 +0300753 if (msg->len > lle->params.n201_u -
Harald Weltece22f922010-06-03 21:21:21 +0200754 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
755 /* initialize the fragmenter state */
756 fs.msg = msg;
757 fs.frag_nr = 0;
758 fs.next_byte = msg->data;
759 fs.sne = sne;
760 fs.mmcontext = mmcontext;
761
762 /* call function to generate and send fragments until all
763 * of the N-PDU has been sent */
764 while (1) {
Philippf1f34362016-08-26 17:00:21 +0200765 int rc = sndcp_send_ud_frag(&fs,pcomp,dcomp);
Harald Weltece22f922010-06-03 21:21:21 +0200766 if (rc == 0)
767 return 0;
768 if (rc < 0)
769 return rc;
770 }
771 /* not reached */
772 return 0;
773 }
774
775 /* this is the non-fragmenting case where we only build 1 SN-PDU */
776
Harald Weltebb1c8052010-06-03 06:38:38 +0200777 /* prepend the user-data header */
778 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Weltece22f922010-06-03 21:21:21 +0200779 suh->npdu_low = sne->tx_npdu_nr & 0xff;
780 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
781 suh->seg_nr = 0;
782 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
783
784 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
Philippf1f34362016-08-26 17:00:21 +0200785 scomph->pcomp = pcomp;
786 scomph->dcomp = dcomp;
Harald Weltebb1c8052010-06-03 06:38:38 +0200787
788 /* prepend common SNDCP header */
789 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
790 sch->first = 1;
791 sch->type = 1;
792 sch->nsapi = nsapi;
793
Max82040102016-07-06 11:59:18 +0200794 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext, true);
Harald Weltebb1c8052010-06-03 06:38:38 +0200795}
796
Harald Welteebabdea2010-06-01 18:28:10 +0200797/* Section 5.1.2.17 LL-UNITDATA.ind */
Pau Espin Pedrol4bd6f662023-01-05 19:00:49 +0100798int sndcp_ll_unitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle,
Harald Welte36f12172010-07-02 16:44:24 +0200799 uint8_t *hdr, uint16_t len)
Harald Welteebabdea2010-06-01 18:28:10 +0200800{
Harald Weltef78a3b22010-06-30 17:21:19 +0200801 struct gprs_sndcp_entity *sne;
Harald Welteebabdea2010-06-01 18:28:10 +0200802 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Weltece22f922010-06-03 21:21:21 +0200803 struct sndcp_comp_hdr *scomph = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200804 struct sndcp_udata_hdr *suh;
Alexander Couzensa8f78252019-09-16 02:44:58 +0200805 struct sgsn_mm_ctx *mmctx;
Harald Welte16836a32010-06-02 10:25:40 +0200806 uint8_t *npdu;
Holger Hans Peter Freythercfee9522014-04-04 12:43:08 +0200807 uint16_t npdu_num __attribute__((unused));
Harald Welteebabdea2010-06-01 18:28:10 +0200808 int npdu_len;
Philippf1f34362016-08-26 17:00:21 +0200809 int rc;
810 uint8_t *expnd = NULL;
Harald Welteebabdea2010-06-01 18:28:10 +0200811
Harald Weltece22f922010-06-03 21:21:21 +0200812 sch = (struct sndcp_common_hdr *) hdr;
813 if (sch->first) {
814 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
815 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
816 } else
817 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
818
Harald Welteebabdea2010-06-01 18:28:10 +0200819 if (sch->type == 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200820 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Welte96f71f22010-05-03 19:28:05 +0200821 return -EINVAL;
822 }
823
Harald Welte16836a32010-06-02 10:25:40 +0200824 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welte69996cb2010-06-02 10:26:19 +0200825 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welteebabdea2010-06-01 18:28:10 +0200826 return -EIO;
827 }
828
Harald Weltef78a3b22010-06-30 17:21:19 +0200829 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200830 if (!sne) {
Harald Welte69996cb2010-06-02 10:26:19 +0200831 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welte61444522010-06-02 12:40:48 +0200832 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
833 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200834 return -EIO;
835 }
Harald Welte8911cef2010-07-01 19:56:19 +0200836 /* FIXME: move this RA_ID up to the LLME or even higher */
837 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
Harald Welteebabdea2010-06-01 18:28:10 +0200838
Alexander Couzensa8f78252019-09-16 02:44:58 +0200839 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &sne->ra_id);
840 if (!mmctx) {
841 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing MM ctx "
842 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n",
843 lle, lle->llme->tlli, lle->sapi, sch->nsapi);
844 return -EIO;
845 }
Pau Espin Pedrol11ccc432021-02-16 13:59:05 +0100846 gprs_gb_recv_pdu(mmctx, msg);
Alexander Couzensa8f78252019-09-16 02:44:58 +0200847
Harald Welte7e5bb622016-09-28 08:20:58 +0800848 if (scomph) {
Philippf1f34362016-08-26 17:00:21 +0200849 sne->defrag.pcomp = scomph->pcomp;
850 sne->defrag.dcomp = scomph->dcomp;
851 sne->defrag.proto = lle->llme->comp.proto;
852 sne->defrag.data = lle->llme->comp.data;
853 }
854
Harald Welteab4094c2010-07-02 16:01:47 +0200855 /* any non-first segment is by definition something to defragment
856 * as is any segment that tells us there are more segments */
857 if (!sch->first || sch->more)
Harald Welte60da7d42010-07-02 15:45:12 +0200858 return defrag_input(sne, msg, hdr, len);
Harald Welteebabdea2010-06-01 18:28:10 +0200859
Harald Welte16836a32010-06-02 10:25:40 +0200860 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200861 npdu = (uint8_t *)suh + sizeof(*suh);
Alexander Couzens410bc9b2018-09-18 20:01:28 +0200862 npdu_len = (msg->data + msg->len) - npdu;
Philippf1f34362016-08-26 17:00:21 +0200863
Harald Welte61444522010-06-02 12:40:48 +0200864 if (npdu_len <= 0) {
Harald Welte69996cb2010-06-02 10:26:19 +0200865 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200866 return -EIO;
867 }
Pau Espin Pedrol51028e22023-01-05 18:43:39 +0100868 /* actually send the N-PDU to the SGSN core code (SNDCP SN-UNITDATA.ind) */
Philippf1f34362016-08-26 17:00:21 +0200869
870 /* Decompress packet */
Philippf1f34362016-08-26 17:00:21 +0200871 if (any_pcomp_or_dcomp_active(sgsn)) {
Pau Espin Pedrol57b63872022-12-06 11:50:26 +0100872 expnd = decompress_segment(sne, msg, npdu, npdu_len, (unsigned int *)&npdu_len);
873 if (!expnd) {
874 rc = -EIO;
875 goto ret_free;
Philipp73f83d52016-09-02 13:38:01 +0200876 }
Pau Espin Pedrol57b63872022-12-06 11:50:26 +0100877 } else {
Philippf1f34362016-08-26 17:00:21 +0200878 expnd = npdu;
Pau Espin Pedrol57b63872022-12-06 11:50:26 +0100879 }
Philippf1f34362016-08-26 17:00:21 +0200880
881 /* Hand off packet to gtp */
Pau Espin Pedrol4bd6f662023-01-05 19:00:49 +0100882 rc = sndcp_sn_unitdata_ind(sne, msg, npdu_len, expnd);
Philippf1f34362016-08-26 17:00:21 +0200883
Pau Espin Pedrol57b63872022-12-06 11:50:26 +0100884ret_free:
Philippf1f34362016-08-26 17:00:21 +0200885 if (any_pcomp_or_dcomp_active(sgsn))
886 talloc_free(expnd);
887
888 return rc;
Harald Welte96f71f22010-05-03 19:28:05 +0200889}
890
Pau Espin Pedrol51028e22023-01-05 18:43:39 +0100891/* 5.1.1.4 SN-UNITDATA.indication
892 * Called by SNDCP when it has received/re-assembled a N-PDU
893 */
Pau Espin Pedrol4bd6f662023-01-05 19:00:49 +0100894int sndcp_sn_unitdata_ind(struct gprs_sndcp_entity *sne,
Pau Espin Pedrol51028e22023-01-05 18:43:39 +0100895 struct msgb *msg, uint32_t npdu_len, uint8_t *npdu)
896{
897 /* Hand it off N-PDU to the correct GTP tunnel + GGSN: */
898 return sgsn_gtp_data_req(&sne->ra_id, sne->lle->llme->tlli,
899 sne->nsapi, msg, npdu_len, npdu);
900}
901
Holger Hans Peter Freythercfee9522014-04-04 12:43:08 +0200902#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200903/* Section 5.1.2.1 LL-RESET.ind */
Harald Weltef78a3b22010-06-30 17:21:19 +0200904static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200905{
906 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
907 /* reset all SNDCP XID parameters to default values */
Holger Hans Peter Freyther6142dc42011-10-14 23:37:27 +0200908 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
909 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200910}
911
Harald Welte2720e732010-05-17 00:44:57 +0200912static int sndcp_ll_status_ind()
913{
914 /* inform the SM sub-layer by means of SNSM-STATUS.req */
Holger Hans Peter Freyther6142dc42011-10-14 23:37:27 +0200915 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
916 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200917}
918
919static struct sndcp_state_list {{
920 uint32_t states;
921 unsigned int type;
Harald Weltef78a3b22010-06-30 17:21:19 +0200922 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
Harald Welte2720e732010-05-17 00:44:57 +0200923} sndcp_state_list[] = {
924 { ALL_STATES,
925 LL_RESET_IND, sndcp_ll_reset_ind },
926 { ALL_STATES,
927 LL_ESTABLISH_IND, sndcp_ll_est_ind },
928 { SBIT(SNDCP_S_EST_RQD),
929 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
930 { SBIT(SNDCP_S_EST_RQD),
931 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
932 { SBIT(SNDCP_S_
933};
934
935static int sndcp_rx_llc_prim()
936{
937 case LL_ESTABLISH_REQ:
938 case LL_RELEASE_REQ:
939 case LL_XID_REQ:
940 case LL_DATA_REQ:
941 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
942
943 switch (prim) {
944 case LL_RESET_IND:
945 case LL_ESTABLISH_IND:
946 case LL_ESTABLISH_RESP:
947 case LL_ESTABLISH_CONF:
948 case LL_RELEASE_IND:
949 case LL_RELEASE_CONF:
950 case LL_XID_IND:
951 case LL_XID_RESP:
952 case LL_XID_CONF:
953 case LL_DATA_IND:
954 case LL_DATA_CONF:
955 case LL_UNITDATA_IND:
956 case LL_STATUS_IND:
Neels Hofmeyrcc7db182016-12-18 23:52:38 +0100957 }
Harald Welte2720e732010-05-17 00:44:57 +0200958}
Harald Welteebabdea2010-06-01 18:28:10 +0200959#endif
Philippf1f34362016-08-26 17:00:21 +0200960
961/* Generate SNDCP-XID message */
962static int gprs_llc_gen_sndcp_xid(uint8_t *bytes, int bytes_len, uint8_t nsapi)
963{
964 int entity = 0;
965 LLIST_HEAD(comp_fields);
966 struct gprs_sndcp_pcomp_rfc1144_params rfc1144_params;
967 struct gprs_sndcp_comp_field rfc1144_comp_field;
Philipp73f83d52016-09-02 13:38:01 +0200968 struct gprs_sndcp_dcomp_v42bis_params v42bis_params;
969 struct gprs_sndcp_comp_field v42bis_comp_field;
Philippf1f34362016-08-26 17:00:21 +0200970
971 memset(&rfc1144_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
Philipp73f83d52016-09-02 13:38:01 +0200972 memset(&v42bis_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
Philippf1f34362016-08-26 17:00:21 +0200973
974 /* Setup rfc1144 */
975 if (sgsn->cfg.pcomp_rfc1144.active) {
976 rfc1144_params.nsapi[0] = nsapi;
977 rfc1144_params.nsapi_len = 1;
978 rfc1144_params.s01 = sgsn->cfg.pcomp_rfc1144.s01;
979 rfc1144_comp_field.p = 1;
980 rfc1144_comp_field.entity = entity;
Stefan Sperlingc5721542018-11-07 16:33:39 +0100981 rfc1144_comp_field.algo.pcomp = RFC_1144;
Philippf1f34362016-08-26 17:00:21 +0200982 rfc1144_comp_field.comp[RFC1144_PCOMP1] = 1;
983 rfc1144_comp_field.comp[RFC1144_PCOMP2] = 2;
984 rfc1144_comp_field.comp_len = RFC1144_PCOMP_NUM;
985 rfc1144_comp_field.rfc1144_params = &rfc1144_params;
986 entity++;
987 llist_add(&rfc1144_comp_field.list, &comp_fields);
988 }
989
Philipp73f83d52016-09-02 13:38:01 +0200990 /* Setup V.42bis */
991 if (sgsn->cfg.dcomp_v42bis.active) {
992 v42bis_params.nsapi[0] = nsapi;
993 v42bis_params.nsapi_len = 1;
994 v42bis_params.p0 = sgsn->cfg.dcomp_v42bis.p0;
995 v42bis_params.p1 = sgsn->cfg.dcomp_v42bis.p1;
996 v42bis_params.p2 = sgsn->cfg.dcomp_v42bis.p2;
997 v42bis_comp_field.p = 1;
998 v42bis_comp_field.entity = entity;
Stefan Sperlingc5721542018-11-07 16:33:39 +0100999 v42bis_comp_field.algo.dcomp = V42BIS;
Philipp73f83d52016-09-02 13:38:01 +02001000 v42bis_comp_field.comp[V42BIS_DCOMP1] = 1;
1001 v42bis_comp_field.comp_len = V42BIS_DCOMP_NUM;
1002 v42bis_comp_field.v42bis_params = &v42bis_params;
1003 entity++;
1004 llist_add(&v42bis_comp_field.list, &comp_fields);
1005 }
1006
Philippdb142dc2016-12-22 14:15:20 +01001007 /* Do not attempt to compile anything if there is no data in the list */
1008 if (llist_empty(&comp_fields))
1009 return 0;
1010
Philippf1f34362016-08-26 17:00:21 +02001011 /* Compile bytestream */
Philippdb142dc2016-12-22 14:15:20 +01001012 return gprs_sndcp_compile_xid(bytes, bytes_len, &comp_fields,
1013 DEFAULT_SNDCP_VERSION);
Philippf1f34362016-08-26 17:00:21 +02001014}
1015
1016/* Set of SNDCP-XID bnegotiation (See also: TS 144 065,
1017 * Section 6.8 XID parameter negotiation) */
1018int sndcp_sn_xid_req(struct gprs_llc_lle *lle, uint8_t nsapi)
1019{
1020 /* Note: The specification requires the SNDCP-User to set of an
1021 * SNDCP xid request. See also 3GPP TS 44.065, 6.8 XID parameter
1022 * negotiation, Figure 11: SNDCP XID negotiation procedure. In
1023 * our case the SNDCP-User is sgsn_libgtp.c, which calls
1024 * sndcp_sn_xid_req directly. */
1025
1026 uint8_t l3params[1024];
1027 int xid_len;
1028 struct gprs_llc_xid_field xid_field_request;
1029
1030 /* Wipe off all compression entities and their states to
1031 * get rid of possible leftovers from a previous session */
1032 gprs_sndcp_comp_free(lle->llme->comp.proto);
1033 gprs_sndcp_comp_free(lle->llme->comp.data);
1034 lle->llme->comp.proto = gprs_sndcp_comp_alloc(lle->llme);
1035 lle->llme->comp.data = gprs_sndcp_comp_alloc(lle->llme);
Harald Welteaf779d22019-04-12 16:56:04 +02001036 talloc_free(lle->xid);
1037 lle->xid = NULL;
Philippf1f34362016-08-26 17:00:21 +02001038
1039 /* Generate compression parameter bytestream */
1040 xid_len = gprs_llc_gen_sndcp_xid(l3params, sizeof(l3params), nsapi);
1041
1042 /* Send XID with the SNDCP-XID bytetsream included */
1043 if (xid_len > 0) {
1044 xid_field_request.type = GPRS_LLC_XID_T_L3_PAR;
1045 xid_field_request.data = l3params;
1046 xid_field_request.data_len = xid_len;
1047 return gprs_ll_xid_req(lle, &xid_field_request);
1048 }
1049
1050 /* When bytestream can not be generated, proceed without SNDCP-XID */
1051 return gprs_ll_xid_req(lle, NULL);
1052
1053}
1054
1055/* Handle header compression entites */
1056static int handle_pcomp_entities(struct gprs_sndcp_comp_field *comp_field,
Maxcaff83e2022-10-09 13:32:09 +03001057 const struct gprs_llc_lle *lle)
Philippf1f34362016-08-26 17:00:21 +02001058{
1059 /* Note: This functions also transforms the comp_field into its
1060 * echo form (strips comp values, resets propose bit etc...)
1061 * the processed comp_fields can then be sent back as XID-
1062 * Response without further modification. */
1063
1064 /* Delete propose bit */
1065 comp_field->p = 0;
1066
1067 /* Process proposed parameters */
Stefan Sperlingc5721542018-11-07 16:33:39 +01001068 switch (comp_field->algo.pcomp) {
Philippf1f34362016-08-26 17:00:21 +02001069 case RFC_1144:
1070 if (sgsn->cfg.pcomp_rfc1144.passive
1071 && comp_field->rfc1144_params->nsapi_len > 0) {
1072 DEBUGP(DSNDCP,
1073 "Accepting RFC1144 header compression...\n");
1074 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.proto,
1075 comp_field);
1076 } else {
1077 DEBUGP(DSNDCP,
1078 "Rejecting RFC1144 header compression...\n");
1079 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1080 comp_field->entity);
1081 comp_field->rfc1144_params->nsapi_len = 0;
1082 }
1083 break;
1084 case RFC_2507:
1085 /* RFC 2507 is not yet supported,
1086 * so we set applicable nsapis to zero */
1087 DEBUGP(DSNDCP, "Rejecting RFC2507 header compression...\n");
1088 comp_field->rfc2507_params->nsapi_len = 0;
1089 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1090 comp_field->entity);
1091 break;
1092 case ROHC:
1093 /* ROHC is not yet supported,
1094 * so we set applicable nsapis to zero */
1095 DEBUGP(DSNDCP, "Rejecting ROHC header compression...\n");
1096 comp_field->rohc_params->nsapi_len = 0;
1097 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1098 comp_field->entity);
1099 break;
1100 }
1101
1102 return 0;
1103}
1104
1105/* Hanle data compression entites */
1106static int handle_dcomp_entities(struct gprs_sndcp_comp_field *comp_field,
Maxcaff83e2022-10-09 13:32:09 +03001107 const struct gprs_llc_lle *lle)
Philippf1f34362016-08-26 17:00:21 +02001108{
1109 /* See note in handle_pcomp_entities() */
1110
1111 /* Delete propose bit */
1112 comp_field->p = 0;
1113
1114 /* Process proposed parameters */
Stefan Sperlingc5721542018-11-07 16:33:39 +01001115 switch (comp_field->algo.dcomp) {
Philippf1f34362016-08-26 17:00:21 +02001116 case V42BIS:
Philipp73f83d52016-09-02 13:38:01 +02001117 if (sgsn->cfg.dcomp_v42bis.passive &&
1118 comp_field->v42bis_params->nsapi_len > 0) {
1119 DEBUGP(DSNDCP,
1120 "Accepting V.42bis data compression...\n");
1121 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.data,
1122 comp_field);
1123 } else {
1124 LOGP(DSNDCP, LOGL_DEBUG,
1125 "Rejecting V.42bis data compression...\n");
1126 gprs_sndcp_comp_delete(lle->llme->comp.data,
1127 comp_field->entity);
1128 comp_field->v42bis_params->nsapi_len = 0;
1129 }
Philippf1f34362016-08-26 17:00:21 +02001130 break;
1131 case V44:
1132 /* V44 is not yet supported,
1133 * so we set applicable nsapis to zero */
1134 DEBUGP(DSNDCP, "Rejecting V.44 data compression...\n");
1135 comp_field->v44_params->nsapi_len = 0;
1136 gprs_sndcp_comp_delete(lle->llme->comp.data,
1137 comp_field->entity);
1138 break;
1139 }
1140
1141 return 0;
1142
1143}
1144
1145/* Process SNDCP-XID indication
1146 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1147int sndcp_sn_xid_ind(struct gprs_llc_xid_field *xid_field_indication,
1148 struct gprs_llc_xid_field *xid_field_response,
Maxcaff83e2022-10-09 13:32:09 +03001149 const struct gprs_llc_lle *lle)
Philippf1f34362016-08-26 17:00:21 +02001150{
1151 /* Note: This function computes the SNDCP-XID response that is sent
1152 * back to the ms when a ms originated XID is received. The
1153 * Input XID fields are directly processed and the result is directly
1154 * handed back. */
1155
1156 int rc;
1157 int compclass;
Philippdb142dc2016-12-22 14:15:20 +01001158 int version;
Philippf1f34362016-08-26 17:00:21 +02001159
1160 struct llist_head *comp_fields;
1161 struct gprs_sndcp_comp_field *comp_field;
1162
1163 OSMO_ASSERT(xid_field_indication);
1164 OSMO_ASSERT(xid_field_response);
1165 OSMO_ASSERT(lle);
1166
Keithbfd67d22019-04-29 18:23:10 +01001167 /* Some phones send zero byte length SNDCP frames
1168 * and do require a confirmation response. */
1169 if (xid_field_indication->data_len == 0) {
1170 xid_field_response->type = GPRS_LLC_XID_T_L3_PAR;
1171 xid_field_response->data_len = 0;
1172 return 0;
1173 }
1174
Philippf1f34362016-08-26 17:00:21 +02001175 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001176 comp_fields = gprs_sndcp_parse_xid(&version, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001177 xid_field_indication->data,
1178 xid_field_indication->data_len,
1179 NULL);
1180 if (!comp_fields)
1181 return -EINVAL;
1182
Philippf1f34362016-08-26 17:00:21 +02001183 /* Handle compression entites */
1184 DEBUGP(DSNDCP, "SNDCP-XID-IND (ms):\n");
1185 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1186
1187 llist_for_each_entry(comp_field, comp_fields, list) {
1188 compclass = gprs_sndcp_get_compression_class(comp_field);
1189 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1190 rc = handle_pcomp_entities(comp_field, lle);
1191 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1192 rc = handle_dcomp_entities(comp_field, lle);
1193 else {
1194 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1195 comp_field->entity);
1196 gprs_sndcp_comp_delete(lle->llme->comp.data,
1197 comp_field->entity);
1198 rc = 0;
1199 }
1200
1201 if (rc < 0) {
1202 talloc_free(comp_fields);
1203 return -EINVAL;
1204 }
1205 }
1206
1207 DEBUGP(DSNDCP, "SNDCP-XID-RES (sgsn):\n");
1208 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1209
1210 /* Reserve some memory to store the modified SNDCP-XID bytes */
1211 xid_field_response->data =
1212 talloc_zero_size(lle->llme, xid_field_indication->data_len);
1213
1214 /* Set Type flag for response */
1215 xid_field_response->type = GPRS_LLC_XID_T_L3_PAR;
1216
1217 /* Compile modified SNDCP-XID bytes */
1218 rc = gprs_sndcp_compile_xid(xid_field_response->data,
1219 xid_field_indication->data_len,
Philippdb142dc2016-12-22 14:15:20 +01001220 comp_fields, 0);
Philippf1f34362016-08-26 17:00:21 +02001221
1222 if (rc > 0)
1223 xid_field_response->data_len = rc;
1224 else {
1225 talloc_free(xid_field_response->data);
1226 xid_field_response->data = NULL;
1227 xid_field_response->data_len = 0;
1228 return -EINVAL;
1229 }
1230
1231 talloc_free(comp_fields);
1232
1233 return 0;
1234}
1235
1236/* Process SNDCP-XID indication
1237 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1238int sndcp_sn_xid_conf(struct gprs_llc_xid_field *xid_field_conf,
1239 struct gprs_llc_xid_field *xid_field_request,
1240 struct gprs_llc_lle *lle)
1241{
1242 /* Note: This function handles an incomming SNDCP-XID confirmiation.
1243 * Since the confirmation fields may lack important parameters we
1244 * will reconstruct these missing fields using the original request
1245 * we have sent. After that we will create (or delete) the
1246 * compression entites */
1247
1248 struct llist_head *comp_fields_req;
1249 struct llist_head *comp_fields_conf;
1250 struct gprs_sndcp_comp_field *comp_field;
1251 int rc;
1252 int compclass;
1253
1254 /* We need both, the confirmation that is sent back by the ms,
1255 * and the original request we have sent. If one of this is missing
1256 * we can not process the confirmation, the caller must check if
1257 * request and confirmation fields are available. */
1258 OSMO_ASSERT(xid_field_conf);
1259 OSMO_ASSERT(xid_field_request);
1260
1261 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001262 comp_fields_req = gprs_sndcp_parse_xid(NULL, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001263 xid_field_request->data,
1264 xid_field_request->data_len,
1265 NULL);
1266 if (!comp_fields_req)
1267 return -EINVAL;
1268
1269 DEBUGP(DSNDCP, "SNDCP-XID-REQ (sgsn):\n");
1270 gprs_sndcp_dump_comp_fields(comp_fields_req, LOGL_DEBUG);
1271
1272 /* Parse SNDCP-CID XID-Field */
Philippdb142dc2016-12-22 14:15:20 +01001273 comp_fields_conf = gprs_sndcp_parse_xid(NULL, lle->llme,
Philippf1f34362016-08-26 17:00:21 +02001274 xid_field_conf->data,
1275 xid_field_conf->data_len,
1276 comp_fields_req);
1277 if (!comp_fields_conf)
1278 return -EINVAL;
1279
1280 DEBUGP(DSNDCP, "SNDCP-XID-CONF (ms):\n");
1281 gprs_sndcp_dump_comp_fields(comp_fields_conf, LOGL_DEBUG);
1282
1283 /* Handle compression entites */
1284 llist_for_each_entry(comp_field, comp_fields_conf, list) {
1285 compclass = gprs_sndcp_get_compression_class(comp_field);
1286 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1287 rc = handle_pcomp_entities(comp_field, lle);
1288 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1289 rc = handle_dcomp_entities(comp_field, lle);
1290 else {
1291 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1292 comp_field->entity);
1293 gprs_sndcp_comp_delete(lle->llme->comp.data,
1294 comp_field->entity);
1295 rc = 0;
1296 }
1297
1298 if (rc < 0) {
1299 talloc_free(comp_fields_req);
1300 talloc_free(comp_fields_conf);
1301 return -EINVAL;
1302 }
1303 }
1304
1305 talloc_free(comp_fields_req);
1306 talloc_free(comp_fields_conf);
1307
1308 return 0;
1309}