blob: 1cbeede09a90929333f181ae098f656c9eca408b [file] [log] [blame]
Harald Weltee661b272010-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 Welte08965682010-06-03 21:21:21 +02004 * (C) 2010 by On-Waves
Harald Weltee661b272010-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 Welte0e3e88e2011-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 Weltee661b272010-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 Welte0e3e88e2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Weltee661b272010-05-03 19:28:05 +020017 *
Harald Welte0e3e88e2011-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 Weltee661b272010-05-03 19:28:05 +020020 *
21 */
22
23#include <errno.h>
24#include <stdint.h>
Max4bc28212016-07-06 11:59:18 +020025#include <stdbool.h>
Harald Weltee661b272010-05-03 19:28:05 +020026
Pablo Neira Ayusodd5fff42011-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 Weltecfb6b282012-06-16 14:59:56 +080031#include <osmocom/gprs/gprs_bssgp.h>
Harald Weltee661b272010-05-03 19:28:05 +020032
33#include <openbsc/gsm_data.h>
34#include <openbsc/debug.h>
Harald Weltee661b272010-05-03 19:28:05 +020035#include <openbsc/gprs_llc.h>
Harald Welted0e36dc2010-06-01 18:28:10 +020036#include <openbsc/sgsn.h>
Philipp247a3392016-08-10 12:12:43 +020037#include <openbsc/gprs_sndcp.h>
Philipp91df16c2016-08-26 17:00:21 +020038#include <openbsc/gprs_llc_xid.h>
39#include <openbsc/gprs_sndcp_xid.h>
40#include <openbsc/gprs_sndcp_pcomp.h>
Philipp2c6dd812016-09-02 13:38:01 +020041#include <openbsc/gprs_sndcp_dcomp.h>
Philipp91df16c2016-08-26 17:00:21 +020042#include <openbsc/gprs_sndcp_comp.h>
43
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 Welte94135032010-06-30 17:21:19 +0200164
Harald Weltee661b272010-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 Welte08965682010-06-03 21:21:21 +0200173} __attribute__((packed));
174
175/* PCOMP / DCOMP only exist in first fragment */
176struct sndcp_comp_hdr {
Harald Weltee661b272010-05-03 19:28:05 +0200177 /* octet 2 */
Harald Weltecc2279b2010-06-02 23:17:05 +0200178 uint8_t pcomp:4;
179 uint8_t dcomp:4;
Harald Welted0e36dc2010-06-01 18:28:10 +0200180} __attribute__((packed));
Harald Weltee661b272010-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 Welted0e36dc2010-06-01 18:28:10 +0200188} __attribute__((packed));
189
Harald Welted0e36dc2010-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 Welte08965682010-06-03 21:21:21 +0200194struct defrag_queue_entry {
Harald Welted0e36dc2010-06-01 18:28:10 +0200195 struct llist_head list;
Harald Welte08965682010-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 Welted0e36dc2010-06-01 18:28:10 +0200199 uint32_t data_len;
Harald Welte08965682010-06-03 21:21:21 +0200200 /* pointer to the data of this fragment */
201 uint8_t *data;
Harald Welted0e36dc2010-06-01 18:28:10 +0200202};
203
Harald Welte94135032010-06-30 17:21:19 +0200204LLIST_HEAD(gprs_sndcp_entities);
Harald Weltee661b272010-05-03 19:28:05 +0200205
Philipp91df16c2016-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) {
Philipp2c6dd812016-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)
Philipp91df16c2016-08-26 17:00:21 +0200210 return true;
211 else
212 return false;
213}
214
Harald Welte08965682010-06-03 21:21:21 +0200215/* Enqueue a fragment into the defragment queue */
Harald Welte94135032010-06-30 17:21:19 +0200216static int defrag_enqueue(struct gprs_sndcp_entity *sne, uint8_t seg_nr,
Harald Welte80b97ab2010-07-02 17:16:07 +0200217 uint8_t *data, uint32_t data_len)
Harald Welted0e36dc2010-06-01 18:28:10 +0200218{
Harald Welte08965682010-06-03 21:21:21 +0200219 struct defrag_queue_entry *dqe;
Harald Welted0e36dc2010-06-01 18:28:10 +0200220
Harald Welte08965682010-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 Weltecb26ed32010-07-02 10:29:06 +0200240 memcpy(dqe->data, data, data_len);
241
Harald Welte08965682010-06-03 21:21:21 +0200242 return 0;
Harald Welted0e36dc2010-06-01 18:28:10 +0200243}
244
Harald Welte08965682010-06-03 21:21:21 +0200245/* return if we have all segments of this N-PDU */
Harald Welte94135032010-06-30 17:21:19 +0200246static int defrag_have_all_segments(struct gprs_sndcp_entity *sne)
Harald Welted0e36dc2010-06-01 18:28:10 +0200247{
Harald Welte08965682010-06-03 21:21:21 +0200248 uint32_t seg_needed = 0;
249 unsigned int i;
Harald Welted0e36dc2010-06-01 18:28:10 +0200250
Harald Welte08965682010-06-03 21:21:21 +0200251 /* create a bitmask of needed segments */
Harald Weltef2e790f2010-07-01 15:09:45 +0200252 for (i = 0; i <= sne->defrag.highest_seg; i++)
Harald Welte08965682010-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 Welted0e36dc2010-06-01 18:28:10 +0200259}
260
Harald Welte94135032010-06-30 17:21:19 +0200261static struct defrag_queue_entry *defrag_get_seg(struct gprs_sndcp_entity *sne,
Harald Welte08965682010-06-03 21:21:21 +0200262 uint32_t seg_nr)
Harald Welted0e36dc2010-06-01 18:28:10 +0200263{
Harald Welte08965682010-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 Welted0e36dc2010-06-01 18:28:10 +0200273}
Harald Welte08965682010-06-03 21:21:21 +0200274
Harald Weltef4ffbf72010-07-02 16:18:59 +0200275/* Perform actual defragmentation and create an output packet */
Harald Welte94135032010-06-30 17:21:19 +0200276static int defrag_segments(struct gprs_sndcp_entity *sne)
Harald Welte08965682010-06-03 21:21:21 +0200277{
278 struct msgb *msg;
279 unsigned int seg_nr;
280 uint8_t *npdu;
Philipp91df16c2016-08-26 17:00:21 +0200281 int npdu_len;
282 int rc;
283 uint8_t *expnd = NULL;
Harald Welte08965682010-06-03 21:21:21 +0200284
Harald Weltee6ae2732010-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 Munaut7eec3152010-06-09 20:56:52 +0200288 msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag");
Harald Welte08965682010-06-03 21:21:21 +0200289 if (!msg)
290 return -ENOMEM;
291
292 /* FIXME: message headers + identifiers */
293
294 npdu = msg->data;
295
Harald Welte5deda422010-07-02 10:11:42 +0200296 for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) {
Harald Welte08965682010-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 Freyther1bcd6372012-03-01 20:30:32 +0100303 msgb_free(msg);
Harald Welte08965682010-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
Philipp91df16c2016-08-26 17:00:21 +0200314 npdu_len = sne->defrag.tot_len;
315
Harald Weltef4ffbf72010-07-02 16:18:59 +0200316 /* FIXME: cancel timer */
317
Harald Welte08965682010-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() */
Philipp91df16c2016-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
Philipp2c6dd812016-09-02 13:38:01 +0200329 expnd = talloc_zero_size(msg, npdu_len * MAX_DATADECOMPR_FAC +
330 MAX_HDRDECOMPR_INCR);
Philipp91df16c2016-08-26 17:00:21 +0200331 memcpy(expnd, npdu, npdu_len);
332
Philipp2c6dd812016-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
Philipp91df16c2016-08-26 17:00:21 +0200343 /* Apply header decompression */
Philipp2c6dd812016-09-02 13:38:01 +0200344 rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp,
Philipp91df16c2016-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
369 if (any_pcomp_or_dcomp_active(sgsn))
370 talloc_free(expnd);
371
372 return rc;
Harald Welte08965682010-06-03 21:21:21 +0200373}
374
Philipp91df16c2016-08-26 17:00:21 +0200375static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg,
376 uint8_t *hdr, unsigned int len)
Harald Welte08965682010-06-03 21:21:21 +0200377{
378 struct sndcp_common_hdr *sch;
Harald Welte08965682010-06-03 21:21:21 +0200379 struct sndcp_udata_hdr *suh;
380 uint16_t npdu_num;
381 uint8_t *data;
382 int rc;
383
384 sch = (struct sndcp_common_hdr *) hdr;
385 if (sch->first) {
Harald Welte08965682010-06-03 21:21:21 +0200386 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
387 } else
388 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
389
390 data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr);
391
392 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
393
Harald Weltee6ae2732010-07-02 16:01:47 +0200394 LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u "
395 "Length %u %s %s\n", sne->lle->llme->tlli, sne->nsapi, npdu_num,
396 suh->seg_nr, len, sch->first ? "F " : "", sch->more ? "M" : "");
Harald Welte723319b2010-07-01 20:29:20 +0200397
Harald Welte08965682010-06-03 21:21:21 +0200398 if (sch->first) {
399 /* first segment of a new packet. Discard all leftover fragments of
400 * previous packet */
401 if (!llist_empty(&sne->defrag.frag_list)) {
Harald Welte2b6a9da2010-07-01 12:19:02 +0200402 struct defrag_queue_entry *dqe, *dqe2;
Harald Welte723319b2010-07-01 20:29:20 +0200403 LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping "
404 "SN-PDU %u due to insufficient segments (%04x)\n",
405 sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu,
406 sne->defrag.seg_have);
Harald Welte2b6a9da2010-07-01 12:19:02 +0200407 llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) {
Harald Welte08965682010-06-03 21:21:21 +0200408 llist_del(&dqe->list);
409 talloc_free(dqe);
410 }
411 }
412 /* store the currently de-fragmented PDU number */
413 sne->defrag.npdu = npdu_num;
Harald Weltef4ffbf72010-07-02 16:18:59 +0200414
415 /* Re-set fragmentation state */
Harald Welte08965682010-06-03 21:21:21 +0200416 sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0;
Harald Weltef4ffbf72010-07-02 16:18:59 +0200417 sne->defrag.tot_len = 0;
418 /* FIXME: (re)start timer */
Harald Welte08965682010-06-03 21:21:21 +0200419 }
420
421 if (sne->defrag.npdu != npdu_num) {
422 LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU "
423 "(%u != %u)\n", npdu_num, sne->defrag.npdu);
424 /* FIXME */
425 }
426
427 /* FIXME: check if seg_nr already exists */
Harald Welte80b97ab2010-07-02 17:16:07 +0200428 /* make sure to subtract length of SNDCP header from 'len' */
429 rc = defrag_enqueue(sne, suh->seg_nr, data, len - (data - hdr));
Harald Welte08965682010-06-03 21:21:21 +0200430 if (rc < 0)
431 return rc;
432
433 if (!sch->more) {
434 /* this is suppsed to be the last segment of the N-PDU, but it
435 * might well be not the last to arrive */
436 sne->defrag.no_more = 1;
437 }
438
439 if (sne->defrag.no_more) {
440 /* we have already received the last segment before, let's check
441 * if all the previous segments exist */
442 if (defrag_have_all_segments(sne))
443 return defrag_segments(sne);
444 }
445
446 return 0;
447}
Harald Welted0e36dc2010-06-01 18:28:10 +0200448
Harald Welte94135032010-06-30 17:21:19 +0200449static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
Harald Welted0e36dc2010-06-01 18:28:10 +0200450 uint8_t nsapi)
451{
Harald Welte94135032010-06-30 17:21:19 +0200452 struct gprs_sndcp_entity *sne;
Harald Welted0e36dc2010-06-01 18:28:10 +0200453
Harald Welte94135032010-06-30 17:21:19 +0200454 llist_for_each_entry(sne, &gprs_sndcp_entities, list) {
Harald Welted0e36dc2010-06-01 18:28:10 +0200455 if (sne->lle == lle && sne->nsapi == nsapi)
456 return sne;
457 }
458 return NULL;
459}
460
Harald Welte94135032010-06-30 17:21:19 +0200461static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle,
Harald Welted0e36dc2010-06-01 18:28:10 +0200462 uint8_t nsapi)
463{
Harald Welte94135032010-06-30 17:21:19 +0200464 struct gprs_sndcp_entity *sne;
Harald Welted0e36dc2010-06-01 18:28:10 +0200465
Harald Welte94135032010-06-30 17:21:19 +0200466 sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity);
Harald Welted0e36dc2010-06-01 18:28:10 +0200467 if (!sne)
468 return NULL;
469
470 sne->lle = lle;
471 sne->nsapi = nsapi;
Harald Welte08965682010-06-03 21:21:21 +0200472 sne->defrag.timer.data = sne;
Harald Welted0e36dc2010-06-01 18:28:10 +0200473 //sne->fqueue.timer.cb = FIXME;
474 sne->rx_state = SNDCP_RX_S_FIRST;
Harald Welteae33cd22010-07-01 12:31:10 +0200475 INIT_LLIST_HEAD(&sne->defrag.frag_list);
Harald Welted0e36dc2010-06-01 18:28:10 +0200476
Harald Welte94135032010-06-30 17:21:19 +0200477 llist_add(&sne->list, &gprs_sndcp_entities);
Harald Welted5b31a82010-06-02 12:40:48 +0200478
Harald Welted0e36dc2010-06-01 18:28:10 +0200479 return sne;
480}
481
482/* Entry point for the SNSM-ACTIVATE.indication */
483int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
484{
Harald Welted5b31a82010-06-02 12:40:48 +0200485 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, "
486 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
Harald Welted0e36dc2010-06-01 18:28:10 +0200487
Harald Welte94135032010-06-30 17:21:19 +0200488 if (gprs_sndcp_entity_by_lle(lle, nsapi)) {
Harald Weltef6554f72010-06-02 10:25:40 +0200489 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
490 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
491 lle->llme->tlli, nsapi);
492 return -EEXIST;
493 }
494
Harald Welte94135032010-06-30 17:21:19 +0200495 if (!gprs_sndcp_entity_alloc(lle, nsapi)) {
Harald Weltef6554f72010-06-02 10:25:40 +0200496 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welted0e36dc2010-06-01 18:28:10 +0200497 return -ENOMEM;
Harald Weltef6554f72010-06-02 10:25:40 +0200498 }
Harald Welted0e36dc2010-06-01 18:28:10 +0200499
500 return 0;
501}
502
Harald Welte08965682010-06-03 21:21:21 +0200503/* Entry point for the SNSM-DEACTIVATE.indication */
504int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
505{
Harald Welte94135032010-06-30 17:21:19 +0200506 struct gprs_sndcp_entity *sne;
Harald Welte08965682010-06-03 21:21:21 +0200507
508 LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, "
509 "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi);
510
Harald Welte94135032010-06-30 17:21:19 +0200511 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Welte08965682010-06-03 21:21:21 +0200512 if (!sne) {
513 LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-"
514 "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli,
515 lle->sapi, nsapi);
516 return -ENOENT;
517 }
518 llist_del(&sne->list);
519 /* frag queue entries are hierarchically allocated, so no need to
520 * free them explicitly here */
521 talloc_free(sne);
522
523 return 0;
524}
525
526/* Fragmenter state */
527struct sndcp_frag_state {
528 uint8_t frag_nr;
529 struct msgb *msg; /* original message */
530 uint8_t *next_byte; /* first byte of next fragment */
531
Harald Welte94135032010-06-30 17:21:19 +0200532 struct gprs_sndcp_entity *sne;
Harald Welte08965682010-06-03 21:21:21 +0200533 void *mmcontext;
534};
535
536/* returns '1' if there are more fragments to send, '0' if none */
Philipp91df16c2016-08-26 17:00:21 +0200537static int sndcp_send_ud_frag(struct sndcp_frag_state *fs,
538 uint8_t pcomp, uint8_t dcomp)
Harald Welte08965682010-06-03 21:21:21 +0200539{
Harald Welte94135032010-06-30 17:21:19 +0200540 struct gprs_sndcp_entity *sne = fs->sne;
Harald Welte08965682010-06-03 21:21:21 +0200541 struct gprs_llc_lle *lle = sne->lle;
542 struct sndcp_common_hdr *sch;
543 struct sndcp_comp_hdr *scomph;
544 struct sndcp_udata_hdr *suh;
545 struct msgb *fmsg;
546 unsigned int max_payload_len;
547 unsigned int len;
548 uint8_t *data;
549 int rc, more;
550
Sylvain Munaut7eec3152010-06-09 20:56:52 +0200551 fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128,
Harald Welte08965682010-06-03 21:21:21 +0200552 "SNDCP Frag");
Holger Hans Peter Freyther13293f32014-10-10 17:35:54 +0200553 if (!fmsg) {
554 msgb_free(fs->msg);
Harald Welte08965682010-06-03 21:21:21 +0200555 return -ENOMEM;
Holger Hans Peter Freyther13293f32014-10-10 17:35:54 +0200556 }
Harald Welte08965682010-06-03 21:21:21 +0200557
558 /* make sure lower layers route the fragment like the original */
559 msgb_tlli(fmsg) = msgb_tlli(fs->msg);
560 msgb_bvci(fmsg) = msgb_bvci(fs->msg);
561 msgb_nsei(fmsg) = msgb_nsei(fs->msg);
562
563 /* prepend common SNDCP header */
564 sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch));
565 sch->nsapi = sne->nsapi;
566 /* Set FIRST bit if we are the first fragment in a series */
567 if (fs->frag_nr == 0)
568 sch->first = 1;
569 sch->type = 1;
570
571 /* append the compression header for first fragment */
572 if (sch->first) {
573 scomph = (struct sndcp_comp_hdr *)
574 msgb_put(fmsg, sizeof(*scomph));
Philipp91df16c2016-08-26 17:00:21 +0200575 scomph->pcomp = pcomp;
576 scomph->dcomp = dcomp;
Harald Welte08965682010-06-03 21:21:21 +0200577 }
578
579 /* append the user-data header */
580 suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh));
581 suh->npdu_low = sne->tx_npdu_nr & 0xff;
582 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
583 suh->seg_nr = fs->frag_nr % 0xf;
584
585 /* calculate remaining length to be sent */
586 len = (fs->msg->data + fs->msg->len) - fs->next_byte;
587 /* how much payload can we actually send via LLC? */
588 max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh));
589 if (sch->first)
590 max_payload_len -= sizeof(*scomph);
591 /* check if we're exceeding the max */
592 if (len > max_payload_len)
593 len = max_payload_len;
594
595 /* copy the actual fragment data into our fmsg */
596 data = msgb_put(fmsg, len);
597 memcpy(data, fs->next_byte, len);
598
599 /* Increment fragment number and data pointer to next fragment */
600 fs->frag_nr++;
601 fs->next_byte += len;
602
603 /* determine if we have more fragemnts to send */
604 if ((fs->msg->data + fs->msg->len) <= fs->next_byte)
605 more = 0;
606 else
607 more = 1;
608
609 /* set the MORE bit of the SNDCP header accordingly */
610 sch->more = more;
611
Max4bc28212016-07-06 11:59:18 +0200612 rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext, true);
Holger Hans Peter Freyther13293f32014-10-10 17:35:54 +0200613 /* abort in case of error, do not advance frag_nr / next_byte */
Harald Welte08965682010-06-03 21:21:21 +0200614 if (rc < 0) {
Holger Hans Peter Freyther13293f32014-10-10 17:35:54 +0200615 msgb_free(fs->msg);
Harald Welte08965682010-06-03 21:21:21 +0200616 return rc;
617 }
618
619 if (!more) {
620 /* we've sent all fragments */
621 msgb_free(fs->msg);
622 memset(fs, 0, sizeof(*fs));
623 /* increment NPDU number for next frame */
624 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
625 return 0;
626 }
627
628 /* default: more fragments to send */
629 return 1;
630}
631
Harald Welted14be782010-06-03 07:14:59 +0200632/* Request transmission of a SN-PDU over specified LLC Entity + SAPI */
Harald Welteef1bef72010-06-03 06:38:38 +0200633int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi,
634 void *mmcontext)
635{
Harald Welte94135032010-06-30 17:21:19 +0200636 struct gprs_sndcp_entity *sne;
Harald Welteef1bef72010-06-03 06:38:38 +0200637 struct sndcp_common_hdr *sch;
Harald Welte08965682010-06-03 21:21:21 +0200638 struct sndcp_comp_hdr *scomph;
Harald Welteef1bef72010-06-03 06:38:38 +0200639 struct sndcp_udata_hdr *suh;
Harald Welte08965682010-06-03 21:21:21 +0200640 struct sndcp_frag_state fs;
Philipp91df16c2016-08-26 17:00:21 +0200641 uint8_t pcomp = 0;
642 uint8_t dcomp = 0;
643 int rc;
Harald Welteef1bef72010-06-03 06:38:38 +0200644
645 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
646
Philipp91df16c2016-08-26 17:00:21 +0200647 /* Compress packet */
648#if DEBUG_IP_PACKETS == 1
649 DEBUGP(DSNDCP, " \n");
650 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
651 DEBUGP(DSNDCP, "===================================================\n");
652 debug_ip_packet(msg->data, msg->len, 0, "sndcp_initdata_req()");
653#endif
654 if (any_pcomp_or_dcomp_active(sgsn)) {
655
656 /* Apply header compression */
657 rc = gprs_sndcp_pcomp_compress(msg->data, msg->len, &pcomp,
658 lle->llme->comp.proto, nsapi);
659 if (rc < 0) {
660 LOGP(DSNDCP, LOGL_ERROR,
661 "TCP/IP Header compression failed!\n");
662 return -EIO;
663 }
664
665 /* Fixup pointer locations and sizes in message buffer to match
666 * the new, compressed buffer size */
667 msgb_get(msg, msg->len);
668 msgb_put(msg, rc);
Philipp2c6dd812016-09-02 13:38:01 +0200669
670 /* Apply data compression */
671 rc = gprs_sndcp_dcomp_compress(msg->data, msg->len, &dcomp,
672 lle->llme->comp.data, nsapi);
673 if (rc < 0) {
674 LOGP(DSNDCP, LOGL_ERROR, "Data compression failed!\n");
675 return -EIO;
676 }
677
678 /* Fixup pointer locations and sizes in message buffer to match
679 * the new, compressed buffer size */
680 msgb_get(msg, msg->len);
681 msgb_put(msg, rc);
Philipp91df16c2016-08-26 17:00:21 +0200682 }
683#if DEBUG_IP_PACKETS == 1
684 DEBUGP(DSNDCP, "===================================================\n");
685 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
686 DEBUGP(DSNDCP, " \n");
687#endif
688
Harald Welte94135032010-06-30 17:21:19 +0200689 sne = gprs_sndcp_entity_by_lle(lle, nsapi);
Harald Welteef1bef72010-06-03 06:38:38 +0200690 if (!sne) {
691 LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n");
Holger Hans Peter Freyther13293f32014-10-10 17:35:54 +0200692 msgb_free(msg);
Harald Welteef1bef72010-06-03 06:38:38 +0200693 return -EIO;
694 }
695
Harald Welte08965682010-06-03 21:21:21 +0200696 /* Check if we need to fragment this N-PDU into multiple SN-PDUs */
697 if (msg->len > lle->params.n201_u -
698 (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) {
699 /* initialize the fragmenter state */
700 fs.msg = msg;
701 fs.frag_nr = 0;
702 fs.next_byte = msg->data;
703 fs.sne = sne;
704 fs.mmcontext = mmcontext;
705
706 /* call function to generate and send fragments until all
707 * of the N-PDU has been sent */
708 while (1) {
Philipp91df16c2016-08-26 17:00:21 +0200709 int rc = sndcp_send_ud_frag(&fs,pcomp,dcomp);
Harald Welte08965682010-06-03 21:21:21 +0200710 if (rc == 0)
711 return 0;
712 if (rc < 0)
713 return rc;
714 }
715 /* not reached */
716 return 0;
717 }
718
719 /* this is the non-fragmenting case where we only build 1 SN-PDU */
720
Harald Welteef1bef72010-06-03 06:38:38 +0200721 /* prepend the user-data header */
722 suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh));
Harald Welte08965682010-06-03 21:21:21 +0200723 suh->npdu_low = sne->tx_npdu_nr & 0xff;
724 suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf;
725 suh->seg_nr = 0;
726 sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff;
727
728 scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph));
Philipp91df16c2016-08-26 17:00:21 +0200729 scomph->pcomp = pcomp;
730 scomph->dcomp = dcomp;
Harald Welteef1bef72010-06-03 06:38:38 +0200731
732 /* prepend common SNDCP header */
733 sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch));
734 sch->first = 1;
735 sch->type = 1;
736 sch->nsapi = nsapi;
737
Max4bc28212016-07-06 11:59:18 +0200738 return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext, true);
Harald Welteef1bef72010-06-03 06:38:38 +0200739}
740
Harald Welted0e36dc2010-06-01 18:28:10 +0200741/* Section 5.1.2.17 LL-UNITDATA.ind */
Harald Welte86289832010-07-02 16:44:24 +0200742int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle,
743 uint8_t *hdr, uint16_t len)
Harald Welted0e36dc2010-06-01 18:28:10 +0200744{
Harald Welte94135032010-06-30 17:21:19 +0200745 struct gprs_sndcp_entity *sne;
Harald Welted0e36dc2010-06-01 18:28:10 +0200746 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
Harald Welte08965682010-06-03 21:21:21 +0200747 struct sndcp_comp_hdr *scomph = NULL;
Harald Welted0e36dc2010-06-01 18:28:10 +0200748 struct sndcp_udata_hdr *suh;
Harald Weltef6554f72010-06-02 10:25:40 +0200749 uint8_t *npdu;
Holger Hans Peter Freyther82151f92014-04-04 12:43:08 +0200750 uint16_t npdu_num __attribute__((unused));
Harald Welted0e36dc2010-06-01 18:28:10 +0200751 int npdu_len;
Philipp91df16c2016-08-26 17:00:21 +0200752 int rc;
753 uint8_t *expnd = NULL;
Harald Welted0e36dc2010-06-01 18:28:10 +0200754
Harald Welte08965682010-06-03 21:21:21 +0200755 sch = (struct sndcp_common_hdr *) hdr;
756 if (sch->first) {
757 scomph = (struct sndcp_comp_hdr *) (hdr + 1);
758 suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr));
759 } else
760 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
761
Harald Welted0e36dc2010-06-01 18:28:10 +0200762 if (sch->type == 0) {
Harald Welted83e3242010-06-02 10:26:19 +0200763 LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
Harald Weltee661b272010-05-03 19:28:05 +0200764 return -EINVAL;
765 }
766
Harald Weltef6554f72010-06-02 10:25:40 +0200767 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welted83e3242010-06-02 10:26:19 +0200768 LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
Harald Welted0e36dc2010-06-01 18:28:10 +0200769 return -EIO;
770 }
771
Harald Welte94135032010-06-30 17:21:19 +0200772 sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi);
Harald Welted0e36dc2010-06-01 18:28:10 +0200773 if (!sne) {
Harald Welted83e3242010-06-02 10:26:19 +0200774 LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity "
Harald Welted5b31a82010-06-02 12:40:48 +0200775 "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle,
776 lle->llme->tlli, lle->sapi, sch->nsapi);
Harald Welted0e36dc2010-06-01 18:28:10 +0200777 return -EIO;
778 }
Harald Welte3322cb32010-07-01 19:56:19 +0200779 /* FIXME: move this RA_ID up to the LLME or even higher */
780 bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg));
Harald Welted0e36dc2010-06-01 18:28:10 +0200781
Harald Weltea1cfebc2016-09-28 08:20:58 +0800782 if (scomph) {
Philipp91df16c2016-08-26 17:00:21 +0200783 sne->defrag.pcomp = scomph->pcomp;
784 sne->defrag.dcomp = scomph->dcomp;
785 sne->defrag.proto = lle->llme->comp.proto;
786 sne->defrag.data = lle->llme->comp.data;
787 }
788
Harald Weltee6ae2732010-07-02 16:01:47 +0200789 /* any non-first segment is by definition something to defragment
790 * as is any segment that tells us there are more segments */
791 if (!sch->first || sch->more)
Harald Welte62f07d32010-07-02 15:45:12 +0200792 return defrag_input(sne, msg, hdr, len);
Harald Welted0e36dc2010-06-01 18:28:10 +0200793
Harald Weltef6554f72010-06-02 10:25:40 +0200794 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welted0e36dc2010-06-01 18:28:10 +0200795 npdu = (uint8_t *)suh + sizeof(*suh);
Philipp91df16c2016-08-26 17:00:21 +0200796 npdu_len = (msg->data + msg->len) - npdu - 3; /* -3 'removes' the FCS */
797
Harald Welted5b31a82010-06-02 12:40:48 +0200798 if (npdu_len <= 0) {
Harald Welted83e3242010-06-02 10:26:19 +0200799 LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
Harald Welted0e36dc2010-06-01 18:28:10 +0200800 return -EIO;
801 }
802 /* actually send the N-PDU to the SGSN core code, which then
803 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
Philipp91df16c2016-08-26 17:00:21 +0200804
805 /* Decompress packet */
806#if DEBUG_IP_PACKETS == 1
807 DEBUGP(DSNDCP, " \n");
808 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
809 DEBUGP(DSNDCP, "===================================================\n");
810#endif
811 if (any_pcomp_or_dcomp_active(sgsn)) {
812
Philipp2c6dd812016-09-02 13:38:01 +0200813 expnd = talloc_zero_size(msg, npdu_len * MAX_DATADECOMPR_FAC +
814 MAX_HDRDECOMPR_INCR);
Philipp91df16c2016-08-26 17:00:21 +0200815 memcpy(expnd, npdu, npdu_len);
816
Philipp2c6dd812016-09-02 13:38:01 +0200817 /* Apply data decompression */
818 rc = gprs_sndcp_dcomp_expand(expnd, npdu_len, sne->defrag.dcomp,
819 sne->defrag.data);
820 if (rc < 0) {
821 LOGP(DSNDCP, LOGL_ERROR,
822 "Data decompression failed!\n");
823 talloc_free(expnd);
824 return -EIO;
825 }
826
Philipp91df16c2016-08-26 17:00:21 +0200827 /* Apply header decompression */
Philipp2c6dd812016-09-02 13:38:01 +0200828 rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp,
Philipp91df16c2016-08-26 17:00:21 +0200829 sne->defrag.proto);
830 if (rc < 0) {
831 LOGP(DSNDCP, LOGL_ERROR,
832 "TCP/IP Header decompression failed!\n");
833 talloc_free(expnd);
834 return -EIO;
835 }
836
837 /* Modify npu length, expnd is handed directly handed
838 * over to gsn_rx_sndcp_ud_ind(), see below */
839 npdu_len = rc;
840 } else
841 expnd = npdu;
842#if DEBUG_IP_PACKETS == 1
843 debug_ip_packet(expnd, npdu_len, 1, "sndcp_llunitdata_ind()");
844 DEBUGP(DSNDCP, "===================================================\n");
845 DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n");
846 DEBUGP(DSNDCP, " \n");
847#endif
848
849 /* Hand off packet to gtp */
850 rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, lle->llme->tlli,
851 sne->nsapi, msg, npdu_len, expnd);
852
853 if (any_pcomp_or_dcomp_active(sgsn))
854 talloc_free(expnd);
855
856 return rc;
Harald Weltee661b272010-05-03 19:28:05 +0200857}
858
Holger Hans Peter Freyther82151f92014-04-04 12:43:08 +0200859#if 0
Harald Welte8f77f192010-05-17 00:44:57 +0200860/* Section 5.1.2.1 LL-RESET.ind */
Harald Welte94135032010-06-30 17:21:19 +0200861static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se)
Harald Welte8f77f192010-05-17 00:44:57 +0200862{
863 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
864 /* reset all SNDCP XID parameters to default values */
Holger Hans Peter Freyther8e621dc2011-10-14 23:37:27 +0200865 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
866 return 0;
Harald Welte8f77f192010-05-17 00:44:57 +0200867}
868
Harald Welte8f77f192010-05-17 00:44:57 +0200869static int sndcp_ll_status_ind()
870{
871 /* inform the SM sub-layer by means of SNSM-STATUS.req */
Holger Hans Peter Freyther8e621dc2011-10-14 23:37:27 +0200872 LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n");
873 return 0;
Harald Welte8f77f192010-05-17 00:44:57 +0200874}
875
876static struct sndcp_state_list {{
877 uint32_t states;
878 unsigned int type;
Harald Welte94135032010-06-30 17:21:19 +0200879 int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg);
Harald Welte8f77f192010-05-17 00:44:57 +0200880} sndcp_state_list[] = {
881 { ALL_STATES,
882 LL_RESET_IND, sndcp_ll_reset_ind },
883 { ALL_STATES,
884 LL_ESTABLISH_IND, sndcp_ll_est_ind },
885 { SBIT(SNDCP_S_EST_RQD),
886 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
887 { SBIT(SNDCP_S_EST_RQD),
888 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
889 { SBIT(SNDCP_S_
890};
891
892static int sndcp_rx_llc_prim()
893{
894 case LL_ESTABLISH_REQ:
895 case LL_RELEASE_REQ:
896 case LL_XID_REQ:
897 case LL_DATA_REQ:
898 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
899
900 switch (prim) {
901 case LL_RESET_IND:
902 case LL_ESTABLISH_IND:
903 case LL_ESTABLISH_RESP:
904 case LL_ESTABLISH_CONF:
905 case LL_RELEASE_IND:
906 case LL_RELEASE_CONF:
907 case LL_XID_IND:
908 case LL_XID_RESP:
909 case LL_XID_CONF:
910 case LL_DATA_IND:
911 case LL_DATA_CONF:
912 case LL_UNITDATA_IND:
913 case LL_STATUS_IND:
914}
Harald Welted0e36dc2010-06-01 18:28:10 +0200915#endif
Philipp91df16c2016-08-26 17:00:21 +0200916
917/* Generate SNDCP-XID message */
918static int gprs_llc_gen_sndcp_xid(uint8_t *bytes, int bytes_len, uint8_t nsapi)
919{
920 int entity = 0;
921 LLIST_HEAD(comp_fields);
922 struct gprs_sndcp_pcomp_rfc1144_params rfc1144_params;
923 struct gprs_sndcp_comp_field rfc1144_comp_field;
Philipp2c6dd812016-09-02 13:38:01 +0200924 struct gprs_sndcp_dcomp_v42bis_params v42bis_params;
925 struct gprs_sndcp_comp_field v42bis_comp_field;
Philipp91df16c2016-08-26 17:00:21 +0200926
927 memset(&rfc1144_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
Philipp2c6dd812016-09-02 13:38:01 +0200928 memset(&v42bis_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
Philipp91df16c2016-08-26 17:00:21 +0200929
930 /* Setup rfc1144 */
931 if (sgsn->cfg.pcomp_rfc1144.active) {
932 rfc1144_params.nsapi[0] = nsapi;
933 rfc1144_params.nsapi_len = 1;
934 rfc1144_params.s01 = sgsn->cfg.pcomp_rfc1144.s01;
935 rfc1144_comp_field.p = 1;
936 rfc1144_comp_field.entity = entity;
937 rfc1144_comp_field.algo = RFC_1144;
938 rfc1144_comp_field.comp[RFC1144_PCOMP1] = 1;
939 rfc1144_comp_field.comp[RFC1144_PCOMP2] = 2;
940 rfc1144_comp_field.comp_len = RFC1144_PCOMP_NUM;
941 rfc1144_comp_field.rfc1144_params = &rfc1144_params;
942 entity++;
943 llist_add(&rfc1144_comp_field.list, &comp_fields);
944 }
945
Philipp2c6dd812016-09-02 13:38:01 +0200946 /* Setup V.42bis */
947 if (sgsn->cfg.dcomp_v42bis.active) {
948 v42bis_params.nsapi[0] = nsapi;
949 v42bis_params.nsapi_len = 1;
950 v42bis_params.p0 = sgsn->cfg.dcomp_v42bis.p0;
951 v42bis_params.p1 = sgsn->cfg.dcomp_v42bis.p1;
952 v42bis_params.p2 = sgsn->cfg.dcomp_v42bis.p2;
953 v42bis_comp_field.p = 1;
954 v42bis_comp_field.entity = entity;
955 v42bis_comp_field.algo = V42BIS;
956 v42bis_comp_field.comp[V42BIS_DCOMP1] = 1;
957 v42bis_comp_field.comp_len = V42BIS_DCOMP_NUM;
958 v42bis_comp_field.v42bis_params = &v42bis_params;
959 entity++;
960 llist_add(&v42bis_comp_field.list, &comp_fields);
961 }
962
Philipp91df16c2016-08-26 17:00:21 +0200963 /* Compile bytestream */
964 return gprs_sndcp_compile_xid(bytes, bytes_len, &comp_fields);
965}
966
967/* Set of SNDCP-XID bnegotiation (See also: TS 144 065,
968 * Section 6.8 XID parameter negotiation) */
969int sndcp_sn_xid_req(struct gprs_llc_lle *lle, uint8_t nsapi)
970{
971 /* Note: The specification requires the SNDCP-User to set of an
972 * SNDCP xid request. See also 3GPP TS 44.065, 6.8 XID parameter
973 * negotiation, Figure 11: SNDCP XID negotiation procedure. In
974 * our case the SNDCP-User is sgsn_libgtp.c, which calls
975 * sndcp_sn_xid_req directly. */
976
977 uint8_t l3params[1024];
978 int xid_len;
979 struct gprs_llc_xid_field xid_field_request;
980
981 /* Wipe off all compression entities and their states to
982 * get rid of possible leftovers from a previous session */
983 gprs_sndcp_comp_free(lle->llme->comp.proto);
984 gprs_sndcp_comp_free(lle->llme->comp.data);
985 lle->llme->comp.proto = gprs_sndcp_comp_alloc(lle->llme);
986 lle->llme->comp.data = gprs_sndcp_comp_alloc(lle->llme);
987 talloc_free(lle->llme->xid);
988 lle->llme->xid = NULL;
989
990 /* Generate compression parameter bytestream */
991 xid_len = gprs_llc_gen_sndcp_xid(l3params, sizeof(l3params), nsapi);
992
993 /* Send XID with the SNDCP-XID bytetsream included */
994 if (xid_len > 0) {
995 xid_field_request.type = GPRS_LLC_XID_T_L3_PAR;
996 xid_field_request.data = l3params;
997 xid_field_request.data_len = xid_len;
998 return gprs_ll_xid_req(lle, &xid_field_request);
999 }
1000
1001 /* When bytestream can not be generated, proceed without SNDCP-XID */
1002 return gprs_ll_xid_req(lle, NULL);
1003
1004}
1005
1006/* Handle header compression entites */
1007static int handle_pcomp_entities(struct gprs_sndcp_comp_field *comp_field,
1008 struct gprs_llc_lle *lle)
1009{
1010 /* Note: This functions also transforms the comp_field into its
1011 * echo form (strips comp values, resets propose bit etc...)
1012 * the processed comp_fields can then be sent back as XID-
1013 * Response without further modification. */
1014
1015 /* Delete propose bit */
1016 comp_field->p = 0;
1017
1018 /* Process proposed parameters */
1019 switch (comp_field->algo) {
1020 case RFC_1144:
1021 if (sgsn->cfg.pcomp_rfc1144.passive
1022 && comp_field->rfc1144_params->nsapi_len > 0) {
1023 DEBUGP(DSNDCP,
1024 "Accepting RFC1144 header compression...\n");
1025 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.proto,
1026 comp_field);
1027 } else {
1028 DEBUGP(DSNDCP,
1029 "Rejecting RFC1144 header compression...\n");
1030 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1031 comp_field->entity);
1032 comp_field->rfc1144_params->nsapi_len = 0;
1033 }
1034 break;
1035 case RFC_2507:
1036 /* RFC 2507 is not yet supported,
1037 * so we set applicable nsapis to zero */
1038 DEBUGP(DSNDCP, "Rejecting RFC2507 header compression...\n");
1039 comp_field->rfc2507_params->nsapi_len = 0;
1040 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1041 comp_field->entity);
1042 break;
1043 case ROHC:
1044 /* ROHC is not yet supported,
1045 * so we set applicable nsapis to zero */
1046 DEBUGP(DSNDCP, "Rejecting ROHC header compression...\n");
1047 comp_field->rohc_params->nsapi_len = 0;
1048 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1049 comp_field->entity);
1050 break;
1051 }
1052
1053 return 0;
1054}
1055
1056/* Hanle data compression entites */
1057static int handle_dcomp_entities(struct gprs_sndcp_comp_field *comp_field,
1058 struct gprs_llc_lle *lle)
1059{
1060 /* See note in handle_pcomp_entities() */
1061
1062 /* Delete propose bit */
1063 comp_field->p = 0;
1064
1065 /* Process proposed parameters */
1066 switch (comp_field->algo) {
1067 case V42BIS:
Philipp2c6dd812016-09-02 13:38:01 +02001068 if (sgsn->cfg.dcomp_v42bis.passive &&
1069 comp_field->v42bis_params->nsapi_len > 0) {
1070 DEBUGP(DSNDCP,
1071 "Accepting V.42bis data compression...\n");
1072 gprs_sndcp_comp_add(lle->llme, lle->llme->comp.data,
1073 comp_field);
1074 } else {
1075 LOGP(DSNDCP, LOGL_DEBUG,
1076 "Rejecting V.42bis data compression...\n");
1077 gprs_sndcp_comp_delete(lle->llme->comp.data,
1078 comp_field->entity);
1079 comp_field->v42bis_params->nsapi_len = 0;
1080 }
Philipp91df16c2016-08-26 17:00:21 +02001081 break;
1082 case V44:
1083 /* V44 is not yet supported,
1084 * so we set applicable nsapis to zero */
1085 DEBUGP(DSNDCP, "Rejecting V.44 data compression...\n");
1086 comp_field->v44_params->nsapi_len = 0;
1087 gprs_sndcp_comp_delete(lle->llme->comp.data,
1088 comp_field->entity);
1089 break;
1090 }
1091
1092 return 0;
1093
1094}
1095
1096/* Process SNDCP-XID indication
1097 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1098int sndcp_sn_xid_ind(struct gprs_llc_xid_field *xid_field_indication,
1099 struct gprs_llc_xid_field *xid_field_response,
1100 struct gprs_llc_lle *lle)
1101{
1102 /* Note: This function computes the SNDCP-XID response that is sent
1103 * back to the ms when a ms originated XID is received. The
1104 * Input XID fields are directly processed and the result is directly
1105 * handed back. */
1106
1107 int rc;
1108 int compclass;
1109
1110 struct llist_head *comp_fields;
1111 struct gprs_sndcp_comp_field *comp_field;
1112
1113 OSMO_ASSERT(xid_field_indication);
1114 OSMO_ASSERT(xid_field_response);
1115 OSMO_ASSERT(lle);
1116
1117 /* Parse SNDCP-CID XID-Field */
1118 comp_fields = gprs_sndcp_parse_xid(lle->llme,
1119 xid_field_indication->data,
1120 xid_field_indication->data_len,
1121 NULL);
1122 if (!comp_fields)
1123 return -EINVAL;
1124
1125 /* Don't bother with empty indications */
1126 if (llist_empty(comp_fields)) {
1127 xid_field_response->data = NULL;
1128 xid_field_response->data_len = 0;
1129 DEBUGP(DSNDCP,
1130 "SNDCP-XID indication did not contain any parameters!\n");
1131 return 0;
1132 }
1133
1134 /* Handle compression entites */
1135 DEBUGP(DSNDCP, "SNDCP-XID-IND (ms):\n");
1136 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1137
1138 llist_for_each_entry(comp_field, comp_fields, list) {
1139 compclass = gprs_sndcp_get_compression_class(comp_field);
1140 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1141 rc = handle_pcomp_entities(comp_field, lle);
1142 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1143 rc = handle_dcomp_entities(comp_field, lle);
1144 else {
1145 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1146 comp_field->entity);
1147 gprs_sndcp_comp_delete(lle->llme->comp.data,
1148 comp_field->entity);
1149 rc = 0;
1150 }
1151
1152 if (rc < 0) {
1153 talloc_free(comp_fields);
1154 return -EINVAL;
1155 }
1156 }
1157
1158 DEBUGP(DSNDCP, "SNDCP-XID-RES (sgsn):\n");
1159 gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG);
1160
1161 /* Reserve some memory to store the modified SNDCP-XID bytes */
1162 xid_field_response->data =
1163 talloc_zero_size(lle->llme, xid_field_indication->data_len);
1164
1165 /* Set Type flag for response */
1166 xid_field_response->type = GPRS_LLC_XID_T_L3_PAR;
1167
1168 /* Compile modified SNDCP-XID bytes */
1169 rc = gprs_sndcp_compile_xid(xid_field_response->data,
1170 xid_field_indication->data_len,
1171 comp_fields);
1172
1173 if (rc > 0)
1174 xid_field_response->data_len = rc;
1175 else {
1176 talloc_free(xid_field_response->data);
1177 xid_field_response->data = NULL;
1178 xid_field_response->data_len = 0;
1179 return -EINVAL;
1180 }
1181
1182 talloc_free(comp_fields);
1183
1184 return 0;
1185}
1186
1187/* Process SNDCP-XID indication
1188 * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */
1189int sndcp_sn_xid_conf(struct gprs_llc_xid_field *xid_field_conf,
1190 struct gprs_llc_xid_field *xid_field_request,
1191 struct gprs_llc_lle *lle)
1192{
1193 /* Note: This function handles an incomming SNDCP-XID confirmiation.
1194 * Since the confirmation fields may lack important parameters we
1195 * will reconstruct these missing fields using the original request
1196 * we have sent. After that we will create (or delete) the
1197 * compression entites */
1198
1199 struct llist_head *comp_fields_req;
1200 struct llist_head *comp_fields_conf;
1201 struct gprs_sndcp_comp_field *comp_field;
1202 int rc;
1203 int compclass;
1204
1205 /* We need both, the confirmation that is sent back by the ms,
1206 * and the original request we have sent. If one of this is missing
1207 * we can not process the confirmation, the caller must check if
1208 * request and confirmation fields are available. */
1209 OSMO_ASSERT(xid_field_conf);
1210 OSMO_ASSERT(xid_field_request);
1211
1212 /* Parse SNDCP-CID XID-Field */
1213 comp_fields_req = gprs_sndcp_parse_xid(lle->llme,
1214 xid_field_request->data,
1215 xid_field_request->data_len,
1216 NULL);
1217 if (!comp_fields_req)
1218 return -EINVAL;
1219
1220 DEBUGP(DSNDCP, "SNDCP-XID-REQ (sgsn):\n");
1221 gprs_sndcp_dump_comp_fields(comp_fields_req, LOGL_DEBUG);
1222
1223 /* Parse SNDCP-CID XID-Field */
1224 comp_fields_conf = gprs_sndcp_parse_xid(lle->llme,
1225 xid_field_conf->data,
1226 xid_field_conf->data_len,
1227 comp_fields_req);
1228 if (!comp_fields_conf)
1229 return -EINVAL;
1230
1231 DEBUGP(DSNDCP, "SNDCP-XID-CONF (ms):\n");
1232 gprs_sndcp_dump_comp_fields(comp_fields_conf, LOGL_DEBUG);
1233
1234 /* Handle compression entites */
1235 llist_for_each_entry(comp_field, comp_fields_conf, list) {
1236 compclass = gprs_sndcp_get_compression_class(comp_field);
1237 if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION)
1238 rc = handle_pcomp_entities(comp_field, lle);
1239 else if (compclass == SNDCP_XID_DATA_COMPRESSION)
1240 rc = handle_dcomp_entities(comp_field, lle);
1241 else {
1242 gprs_sndcp_comp_delete(lle->llme->comp.proto,
1243 comp_field->entity);
1244 gprs_sndcp_comp_delete(lle->llme->comp.data,
1245 comp_field->entity);
1246 rc = 0;
1247 }
1248
1249 if (rc < 0) {
1250 talloc_free(comp_fields_req);
1251 talloc_free(comp_fields_conf);
1252 return -EINVAL;
1253 }
1254 }
1255
1256 talloc_free(comp_fields_req);
1257 talloc_free(comp_fields_conf);
1258
1259 return 0;
1260}