Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 1 | /* GPRS SNDCP protocol implementation as per 3GPP TS 04.65 */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * 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 Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 11 | * (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * 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 Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <stdint.h> |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 25 | #include <stdbool.h> |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 26 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 27 | #include <osmocom/core/msgb.h> |
| 28 | #include <osmocom/core/linuxlist.h> |
| 29 | #include <osmocom/core/timer.h> |
| 30 | #include <osmocom/core/talloc.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 31 | #include <osmocom/gprs/gprs_bssgp.h> |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 32 | |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 33 | #include <osmocom/sgsn/debug.h> |
Alexander Couzens | a8f7825 | 2019-09-16 02:44:58 +0200 | [diff] [blame] | 34 | #include <osmocom/sgsn/gprs_gb.h> |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 35 | #include <osmocom/sgsn/gprs_llc.h> |
| 36 | #include <osmocom/sgsn/sgsn.h> |
| 37 | #include <osmocom/sgsn/gprs_sndcp.h> |
| 38 | #include <osmocom/sgsn/gprs_llc_xid.h> |
| 39 | #include <osmocom/sgsn/gprs_sndcp_xid.h> |
| 40 | #include <osmocom/sgsn/gprs_sndcp_pcomp.h> |
| 41 | #include <osmocom/sgsn/gprs_sndcp_dcomp.h> |
| 42 | #include <osmocom/sgsn/gprs_sndcp_comp.h> |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 43 | |
| 44 | #define DEBUG_IP_PACKETS 0 /* 0=Disabled, 1=Enabled */ |
| 45 | |
| 46 | #if DEBUG_IP_PACKETS == 1 |
| 47 | /* Calculate TCP/IP checksum */ |
| 48 | static 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 */ |
| 68 | static 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 */ |
| 86 | static 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 Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 164 | |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 165 | /* Chapter 7.2: SN-PDU Formats */ |
| 166 | struct 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 Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 173 | } __attribute__((packed)); |
| 174 | |
| 175 | /* PCOMP / DCOMP only exist in first fragment */ |
| 176 | struct sndcp_comp_hdr { |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 177 | /* octet 2 */ |
Harald Welte | 5cc2bc3 | 2010-06-02 23:17:05 +0200 | [diff] [blame] | 178 | uint8_t pcomp:4; |
| 179 | uint8_t dcomp:4; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 180 | } __attribute__((packed)); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 181 | |
| 182 | struct 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 Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 188 | } __attribute__((packed)); |
| 189 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 190 | |
| 191 | static void *tall_sndcp_ctx; |
| 192 | |
| 193 | /* A fragment queue entry, containing one framgent of a N-PDU */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 194 | struct defrag_queue_entry { |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 195 | struct llist_head list; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 196 | /* segment number of this fragment */ |
| 197 | uint32_t seg_nr; |
| 198 | /* length of the data area of this fragment */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 199 | uint32_t data_len; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 200 | /* pointer to the data of this fragment */ |
| 201 | uint8_t *data; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 202 | }; |
| 203 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 204 | LLIST_HEAD(gprs_sndcp_entities); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 205 | |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 206 | /* Check if any compression parameters are set in the sgsn configuration */ |
| 207 | static inline int any_pcomp_or_dcomp_active(struct sgsn_instance *sgsn) { |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 208 | if (sgsn->cfg.pcomp_rfc1144.active || sgsn->cfg.pcomp_rfc1144.passive || |
| 209 | sgsn->cfg.dcomp_v42bis.active || sgsn->cfg.dcomp_v42bis.passive) |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 210 | return true; |
| 211 | else |
| 212 | return false; |
| 213 | } |
| 214 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 215 | /* Enqueue a fragment into the defragment queue */ |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 216 | static int defrag_enqueue(struct gprs_sndcp_entity *sne, uint8_t seg_nr, |
Harald Welte | 3d6815a | 2010-07-02 17:16:07 +0200 | [diff] [blame] | 217 | uint8_t *data, uint32_t data_len) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 218 | { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 219 | struct defrag_queue_entry *dqe; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 220 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 221 | 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 Welte | 8f0c0a3 | 2010-07-02 10:29:06 +0200 | [diff] [blame] | 240 | memcpy(dqe->data, data, data_len); |
| 241 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 242 | return 0; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 245 | /* return if we have all segments of this N-PDU */ |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 246 | static int defrag_have_all_segments(struct gprs_sndcp_entity *sne) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 247 | { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 248 | uint32_t seg_needed = 0; |
| 249 | unsigned int i; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 250 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 251 | /* create a bitmask of needed segments */ |
Harald Welte | 951a12c | 2010-07-01 15:09:45 +0200 | [diff] [blame] | 252 | for (i = 0; i <= sne->defrag.highest_seg; i++) |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 253 | seg_needed |= (1 << i); |
| 254 | |
| 255 | if (seg_needed == sne->defrag.seg_have) |
| 256 | return 1; |
| 257 | |
| 258 | return 0; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 261 | static struct defrag_queue_entry *defrag_get_seg(struct gprs_sndcp_entity *sne, |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 262 | uint32_t seg_nr) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 263 | { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 264 | 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 Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 273 | } |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 274 | |
Harald Welte | 8b705f2 | 2010-07-02 16:18:59 +0200 | [diff] [blame] | 275 | /* Perform actual defragmentation and create an output packet */ |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 276 | static int defrag_segments(struct gprs_sndcp_entity *sne) |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 277 | { |
| 278 | struct msgb *msg; |
| 279 | unsigned int seg_nr; |
| 280 | uint8_t *npdu; |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 281 | int npdu_len; |
| 282 | int rc; |
| 283 | uint8_t *expnd = NULL; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 284 | |
Harald Welte | ab4094c | 2010-07-02 16:01:47 +0200 | [diff] [blame] | 285 | 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 Munaut | eda125c | 2010-06-09 20:56:52 +0200 | [diff] [blame] | 288 | msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag"); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 289 | if (!msg) |
| 290 | return -ENOMEM; |
| 291 | |
| 292 | /* FIXME: message headers + identifiers */ |
| 293 | |
| 294 | npdu = msg->data; |
| 295 | |
Harald Welte | 993697c | 2010-07-02 10:11:42 +0200 | [diff] [blame] | 296 | for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 297 | 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 Freyther | a8ddb08 | 2012-03-01 20:30:32 +0100 | [diff] [blame] | 303 | msgb_free(msg); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 304 | 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 | |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 314 | npdu_len = sne->defrag.tot_len; |
| 315 | |
Harald Welte | 8b705f2 | 2010-07-02 16:18:59 +0200 | [diff] [blame] | 316 | /* FIXME: cancel timer */ |
| 317 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 318 | /* 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() */ |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 320 | |
| 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 | |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 329 | expnd = talloc_zero_size(msg, npdu_len * MAX_DATADECOMPR_FAC + |
| 330 | MAX_HDRDECOMPR_INCR); |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 331 | memcpy(expnd, npdu, npdu_len); |
| 332 | |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 333 | /* 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 | |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 343 | /* Apply header decompression */ |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 344 | rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp, |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 345 | sne->defrag.proto); |
| 346 | if (rc < 0) { |
| 347 | LOGP(DSNDCP, LOGL_ERROR, |
| 348 | "TCP/IP Header decompression failed!\n"); |
| 349 | talloc_free(expnd); |
| 350 | return -EIO; |
| 351 | } |
| 352 | |
| 353 | /* Modify npu length, expnd is handed directly handed |
| 354 | * over to gsn_rx_sndcp_ud_ind(), see below */ |
| 355 | npdu_len = rc; |
| 356 | } else |
| 357 | expnd = npdu; |
| 358 | #if DEBUG_IP_PACKETS == 1 |
| 359 | debug_ip_packet(expnd, npdu_len, 1, "defrag_segments()"); |
| 360 | DEBUGP(DSNDCP, "===================================================\n"); |
| 361 | DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n"); |
| 362 | DEBUGP(DSNDCP, " \n"); |
| 363 | #endif |
| 364 | |
| 365 | /* Hand off packet to gtp */ |
| 366 | rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, sne->lle->llme->tlli, |
| 367 | sne->nsapi, msg, npdu_len, expnd); |
| 368 | |
Harald Welte | 627e285 | 2020-06-08 20:46:53 +0200 | [diff] [blame] | 369 | /* we must free the memory we allocated above; ownership is not transferred |
| 370 | * downwards in the call above */ |
| 371 | msgb_free(msg); |
| 372 | |
Philipp Maier | ef6205b | 2020-10-02 17:35:25 +0200 | [diff] [blame] | 373 | /* Note: We do not have to free expnd explicitly, because it is created |
| 374 | * within the talloc context of msg, which we just freed. */ |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 375 | |
| 376 | return rc; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 379 | static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg, |
| 380 | uint8_t *hdr, unsigned int len) |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 381 | { |
| 382 | struct sndcp_common_hdr *sch; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 383 | struct sndcp_udata_hdr *suh; |
| 384 | uint16_t npdu_num; |
| 385 | uint8_t *data; |
| 386 | int rc; |
| 387 | |
| 388 | sch = (struct sndcp_common_hdr *) hdr; |
| 389 | if (sch->first) { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 390 | suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr)); |
| 391 | } else |
| 392 | suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr)); |
| 393 | |
| 394 | data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr); |
| 395 | |
| 396 | npdu_num = (suh->npdu_high << 8) | suh->npdu_low; |
| 397 | |
Harald Welte | ab4094c | 2010-07-02 16:01:47 +0200 | [diff] [blame] | 398 | LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u " |
| 399 | "Length %u %s %s\n", sne->lle->llme->tlli, sne->nsapi, npdu_num, |
| 400 | suh->seg_nr, len, sch->first ? "F " : "", sch->more ? "M" : ""); |
Harald Welte | b87bc86 | 2010-07-01 20:29:20 +0200 | [diff] [blame] | 401 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 402 | if (sch->first) { |
| 403 | /* first segment of a new packet. Discard all leftover fragments of |
| 404 | * previous packet */ |
| 405 | if (!llist_empty(&sne->defrag.frag_list)) { |
Harald Welte | 65d9678 | 2010-07-01 12:19:02 +0200 | [diff] [blame] | 406 | struct defrag_queue_entry *dqe, *dqe2; |
Harald Welte | b87bc86 | 2010-07-01 20:29:20 +0200 | [diff] [blame] | 407 | LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping " |
| 408 | "SN-PDU %u due to insufficient segments (%04x)\n", |
| 409 | sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu, |
| 410 | sne->defrag.seg_have); |
Harald Welte | 65d9678 | 2010-07-01 12:19:02 +0200 | [diff] [blame] | 411 | llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 412 | llist_del(&dqe->list); |
| 413 | talloc_free(dqe); |
| 414 | } |
| 415 | } |
| 416 | /* store the currently de-fragmented PDU number */ |
| 417 | sne->defrag.npdu = npdu_num; |
Harald Welte | 8b705f2 | 2010-07-02 16:18:59 +0200 | [diff] [blame] | 418 | |
| 419 | /* Re-set fragmentation state */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 420 | sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0; |
Harald Welte | 8b705f2 | 2010-07-02 16:18:59 +0200 | [diff] [blame] | 421 | sne->defrag.tot_len = 0; |
| 422 | /* FIXME: (re)start timer */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | if (sne->defrag.npdu != npdu_num) { |
| 426 | LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU " |
| 427 | "(%u != %u)\n", npdu_num, sne->defrag.npdu); |
| 428 | /* FIXME */ |
| 429 | } |
| 430 | |
| 431 | /* FIXME: check if seg_nr already exists */ |
Harald Welte | 3d6815a | 2010-07-02 17:16:07 +0200 | [diff] [blame] | 432 | /* make sure to subtract length of SNDCP header from 'len' */ |
| 433 | rc = defrag_enqueue(sne, suh->seg_nr, data, len - (data - hdr)); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 434 | if (rc < 0) |
| 435 | return rc; |
| 436 | |
| 437 | if (!sch->more) { |
| 438 | /* this is suppsed to be the last segment of the N-PDU, but it |
| 439 | * might well be not the last to arrive */ |
| 440 | sne->defrag.no_more = 1; |
| 441 | } |
| 442 | |
| 443 | if (sne->defrag.no_more) { |
| 444 | /* we have already received the last segment before, let's check |
| 445 | * if all the previous segments exist */ |
| 446 | if (defrag_have_all_segments(sne)) |
| 447 | return defrag_segments(sne); |
| 448 | } |
| 449 | |
| 450 | return 0; |
| 451 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 452 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 453 | static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle, |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 454 | uint8_t nsapi) |
| 455 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 456 | struct gprs_sndcp_entity *sne; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 457 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 458 | llist_for_each_entry(sne, &gprs_sndcp_entities, list) { |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 459 | if (sne->lle == lle && sne->nsapi == nsapi) |
| 460 | return sne; |
| 461 | } |
| 462 | return NULL; |
| 463 | } |
| 464 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 465 | static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle, |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 466 | uint8_t nsapi) |
| 467 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 468 | struct gprs_sndcp_entity *sne; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 469 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 470 | sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 471 | if (!sne) |
| 472 | return NULL; |
| 473 | |
| 474 | sne->lle = lle; |
| 475 | sne->nsapi = nsapi; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 476 | sne->defrag.timer.data = sne; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 477 | //sne->fqueue.timer.cb = FIXME; |
| 478 | sne->rx_state = SNDCP_RX_S_FIRST; |
Harald Welte | 362aea0 | 2010-07-01 12:31:10 +0200 | [diff] [blame] | 479 | INIT_LLIST_HEAD(&sne->defrag.frag_list); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 480 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 481 | llist_add(&sne->list, &gprs_sndcp_entities); |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 482 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 483 | return sne; |
| 484 | } |
| 485 | |
| 486 | /* Entry point for the SNSM-ACTIVATE.indication */ |
| 487 | int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi) |
| 488 | { |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 489 | LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, " |
| 490 | "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 491 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 492 | if (gprs_sndcp_entity_by_lle(lle, nsapi)) { |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 493 | LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE " |
| 494 | "already-existing entity (TLLI=%08x, NSAPI=%u)\n", |
| 495 | lle->llme->tlli, nsapi); |
| 496 | return -EEXIST; |
| 497 | } |
| 498 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 499 | if (!gprs_sndcp_entity_alloc(lle, nsapi)) { |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 500 | LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n"); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 501 | return -ENOMEM; |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 502 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 507 | /* Entry point for the SNSM-DEACTIVATE.indication */ |
| 508 | int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi) |
| 509 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 510 | struct gprs_sndcp_entity *sne; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 511 | |
| 512 | LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, " |
| 513 | "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi); |
| 514 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 515 | sne = gprs_sndcp_entity_by_lle(lle, nsapi); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 516 | if (!sne) { |
| 517 | LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-" |
| 518 | "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli, |
| 519 | lle->sapi, nsapi); |
| 520 | return -ENOENT; |
| 521 | } |
| 522 | llist_del(&sne->list); |
| 523 | /* frag queue entries are hierarchically allocated, so no need to |
| 524 | * free them explicitly here */ |
| 525 | talloc_free(sne); |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | /* Fragmenter state */ |
| 531 | struct sndcp_frag_state { |
| 532 | uint8_t frag_nr; |
| 533 | struct msgb *msg; /* original message */ |
| 534 | uint8_t *next_byte; /* first byte of next fragment */ |
| 535 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 536 | struct gprs_sndcp_entity *sne; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 537 | void *mmcontext; |
| 538 | }; |
| 539 | |
| 540 | /* returns '1' if there are more fragments to send, '0' if none */ |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 541 | static int sndcp_send_ud_frag(struct sndcp_frag_state *fs, |
| 542 | uint8_t pcomp, uint8_t dcomp) |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 543 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 544 | struct gprs_sndcp_entity *sne = fs->sne; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 545 | struct gprs_llc_lle *lle = sne->lle; |
| 546 | struct sndcp_common_hdr *sch; |
| 547 | struct sndcp_comp_hdr *scomph; |
| 548 | struct sndcp_udata_hdr *suh; |
| 549 | struct msgb *fmsg; |
| 550 | unsigned int max_payload_len; |
| 551 | unsigned int len; |
| 552 | uint8_t *data; |
| 553 | int rc, more; |
| 554 | |
Sylvain Munaut | eda125c | 2010-06-09 20:56:52 +0200 | [diff] [blame] | 555 | fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128, |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 556 | "SNDCP Frag"); |
Holger Hans Peter Freyther | f9ffd1f | 2014-10-10 17:35:54 +0200 | [diff] [blame] | 557 | if (!fmsg) { |
| 558 | msgb_free(fs->msg); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 559 | return -ENOMEM; |
Holger Hans Peter Freyther | f9ffd1f | 2014-10-10 17:35:54 +0200 | [diff] [blame] | 560 | } |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 561 | |
| 562 | /* make sure lower layers route the fragment like the original */ |
| 563 | msgb_tlli(fmsg) = msgb_tlli(fs->msg); |
| 564 | msgb_bvci(fmsg) = msgb_bvci(fs->msg); |
| 565 | msgb_nsei(fmsg) = msgb_nsei(fs->msg); |
| 566 | |
| 567 | /* prepend common SNDCP header */ |
| 568 | sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch)); |
| 569 | sch->nsapi = sne->nsapi; |
| 570 | /* Set FIRST bit if we are the first fragment in a series */ |
| 571 | if (fs->frag_nr == 0) |
| 572 | sch->first = 1; |
| 573 | sch->type = 1; |
| 574 | |
| 575 | /* append the compression header for first fragment */ |
| 576 | if (sch->first) { |
| 577 | scomph = (struct sndcp_comp_hdr *) |
| 578 | msgb_put(fmsg, sizeof(*scomph)); |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 579 | scomph->pcomp = pcomp; |
| 580 | scomph->dcomp = dcomp; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* append the user-data header */ |
| 584 | suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh)); |
| 585 | suh->npdu_low = sne->tx_npdu_nr & 0xff; |
| 586 | suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf; |
| 587 | suh->seg_nr = fs->frag_nr % 0xf; |
| 588 | |
| 589 | /* calculate remaining length to be sent */ |
| 590 | len = (fs->msg->data + fs->msg->len) - fs->next_byte; |
| 591 | /* how much payload can we actually send via LLC? */ |
| 592 | max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh)); |
| 593 | if (sch->first) |
| 594 | max_payload_len -= sizeof(*scomph); |
| 595 | /* check if we're exceeding the max */ |
| 596 | if (len > max_payload_len) |
| 597 | len = max_payload_len; |
| 598 | |
| 599 | /* copy the actual fragment data into our fmsg */ |
| 600 | data = msgb_put(fmsg, len); |
| 601 | memcpy(data, fs->next_byte, len); |
| 602 | |
| 603 | /* Increment fragment number and data pointer to next fragment */ |
| 604 | fs->frag_nr++; |
| 605 | fs->next_byte += len; |
| 606 | |
| 607 | /* determine if we have more fragemnts to send */ |
| 608 | if ((fs->msg->data + fs->msg->len) <= fs->next_byte) |
| 609 | more = 0; |
| 610 | else |
| 611 | more = 1; |
| 612 | |
| 613 | /* set the MORE bit of the SNDCP header accordingly */ |
| 614 | sch->more = more; |
| 615 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 616 | rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext, true); |
Holger Hans Peter Freyther | f9ffd1f | 2014-10-10 17:35:54 +0200 | [diff] [blame] | 617 | /* abort in case of error, do not advance frag_nr / next_byte */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 618 | if (rc < 0) { |
Holger Hans Peter Freyther | f9ffd1f | 2014-10-10 17:35:54 +0200 | [diff] [blame] | 619 | msgb_free(fs->msg); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 620 | return rc; |
| 621 | } |
| 622 | |
| 623 | if (!more) { |
| 624 | /* we've sent all fragments */ |
| 625 | msgb_free(fs->msg); |
| 626 | memset(fs, 0, sizeof(*fs)); |
| 627 | /* increment NPDU number for next frame */ |
| 628 | sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff; |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | /* default: more fragments to send */ |
| 633 | return 1; |
| 634 | } |
| 635 | |
Harald Welte | db2c39f | 2010-06-03 07:14:59 +0200 | [diff] [blame] | 636 | /* Request transmission of a SN-PDU over specified LLC Entity + SAPI */ |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 637 | int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi, |
| 638 | void *mmcontext) |
| 639 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 640 | struct gprs_sndcp_entity *sne; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 641 | struct sndcp_common_hdr *sch; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 642 | struct sndcp_comp_hdr *scomph; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 643 | struct sndcp_udata_hdr *suh; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 644 | struct sndcp_frag_state fs; |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 645 | uint8_t pcomp = 0; |
| 646 | uint8_t dcomp = 0; |
| 647 | int rc; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 648 | |
| 649 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 650 | |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 651 | /* Compress packet */ |
| 652 | #if DEBUG_IP_PACKETS == 1 |
| 653 | DEBUGP(DSNDCP, " \n"); |
| 654 | DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n"); |
| 655 | DEBUGP(DSNDCP, "===================================================\n"); |
| 656 | debug_ip_packet(msg->data, msg->len, 0, "sndcp_initdata_req()"); |
| 657 | #endif |
| 658 | if (any_pcomp_or_dcomp_active(sgsn)) { |
| 659 | |
| 660 | /* Apply header compression */ |
| 661 | rc = gprs_sndcp_pcomp_compress(msg->data, msg->len, &pcomp, |
| 662 | lle->llme->comp.proto, nsapi); |
| 663 | if (rc < 0) { |
| 664 | LOGP(DSNDCP, LOGL_ERROR, |
| 665 | "TCP/IP Header compression failed!\n"); |
| 666 | return -EIO; |
| 667 | } |
| 668 | |
| 669 | /* Fixup pointer locations and sizes in message buffer to match |
| 670 | * the new, compressed buffer size */ |
| 671 | msgb_get(msg, msg->len); |
| 672 | msgb_put(msg, rc); |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 673 | |
| 674 | /* Apply data compression */ |
| 675 | rc = gprs_sndcp_dcomp_compress(msg->data, msg->len, &dcomp, |
| 676 | lle->llme->comp.data, nsapi); |
| 677 | if (rc < 0) { |
| 678 | LOGP(DSNDCP, LOGL_ERROR, "Data compression failed!\n"); |
| 679 | return -EIO; |
| 680 | } |
| 681 | |
| 682 | /* Fixup pointer locations and sizes in message buffer to match |
| 683 | * the new, compressed buffer size */ |
| 684 | msgb_get(msg, msg->len); |
| 685 | msgb_put(msg, rc); |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 686 | } |
| 687 | #if DEBUG_IP_PACKETS == 1 |
| 688 | DEBUGP(DSNDCP, "===================================================\n"); |
| 689 | DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n"); |
| 690 | DEBUGP(DSNDCP, " \n"); |
| 691 | #endif |
| 692 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 693 | sne = gprs_sndcp_entity_by_lle(lle, nsapi); |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 694 | if (!sne) { |
| 695 | LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n"); |
Holger Hans Peter Freyther | f9ffd1f | 2014-10-10 17:35:54 +0200 | [diff] [blame] | 696 | msgb_free(msg); |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 697 | return -EIO; |
| 698 | } |
| 699 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 700 | /* Check if we need to fragment this N-PDU into multiple SN-PDUs */ |
| 701 | if (msg->len > lle->params.n201_u - |
| 702 | (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) { |
| 703 | /* initialize the fragmenter state */ |
| 704 | fs.msg = msg; |
| 705 | fs.frag_nr = 0; |
| 706 | fs.next_byte = msg->data; |
| 707 | fs.sne = sne; |
| 708 | fs.mmcontext = mmcontext; |
| 709 | |
| 710 | /* call function to generate and send fragments until all |
| 711 | * of the N-PDU has been sent */ |
| 712 | while (1) { |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 713 | int rc = sndcp_send_ud_frag(&fs,pcomp,dcomp); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 714 | if (rc == 0) |
| 715 | return 0; |
| 716 | if (rc < 0) |
| 717 | return rc; |
| 718 | } |
| 719 | /* not reached */ |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | /* this is the non-fragmenting case where we only build 1 SN-PDU */ |
| 724 | |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 725 | /* prepend the user-data header */ |
| 726 | suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh)); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 727 | suh->npdu_low = sne->tx_npdu_nr & 0xff; |
| 728 | suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf; |
| 729 | suh->seg_nr = 0; |
| 730 | sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff; |
| 731 | |
| 732 | scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph)); |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 733 | scomph->pcomp = pcomp; |
| 734 | scomph->dcomp = dcomp; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 735 | |
| 736 | /* prepend common SNDCP header */ |
| 737 | sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch)); |
| 738 | sch->first = 1; |
| 739 | sch->type = 1; |
| 740 | sch->nsapi = nsapi; |
| 741 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 742 | return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext, true); |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 743 | } |
| 744 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 745 | /* Section 5.1.2.17 LL-UNITDATA.ind */ |
Harald Welte | 36f1217 | 2010-07-02 16:44:24 +0200 | [diff] [blame] | 746 | int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle, |
| 747 | uint8_t *hdr, uint16_t len) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 748 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 749 | struct gprs_sndcp_entity *sne; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 750 | struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 751 | struct sndcp_comp_hdr *scomph = NULL; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 752 | struct sndcp_udata_hdr *suh; |
Alexander Couzens | a8f7825 | 2019-09-16 02:44:58 +0200 | [diff] [blame] | 753 | struct sgsn_mm_ctx *mmctx; |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 754 | uint8_t *npdu; |
Holger Hans Peter Freyther | cfee952 | 2014-04-04 12:43:08 +0200 | [diff] [blame] | 755 | uint16_t npdu_num __attribute__((unused)); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 756 | int npdu_len; |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 757 | int rc; |
| 758 | uint8_t *expnd = NULL; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 759 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 760 | sch = (struct sndcp_common_hdr *) hdr; |
| 761 | if (sch->first) { |
| 762 | scomph = (struct sndcp_comp_hdr *) (hdr + 1); |
| 763 | suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr)); |
| 764 | } else |
| 765 | suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr)); |
| 766 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 767 | if (sch->type == 0) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 768 | LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n"); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 769 | return -EINVAL; |
| 770 | } |
| 771 | |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 772 | if (len < sizeof(*sch) + sizeof(*suh)) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 773 | LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 774 | return -EIO; |
| 775 | } |
| 776 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 777 | sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 778 | if (!sne) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 779 | LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity " |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 780 | "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle, |
| 781 | lle->llme->tlli, lle->sapi, sch->nsapi); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 782 | return -EIO; |
| 783 | } |
Harald Welte | 8911cef | 2010-07-01 19:56:19 +0200 | [diff] [blame] | 784 | /* FIXME: move this RA_ID up to the LLME or even higher */ |
| 785 | bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg)); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 786 | |
Alexander Couzens | a8f7825 | 2019-09-16 02:44:58 +0200 | [diff] [blame] | 787 | mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &sne->ra_id); |
| 788 | if (!mmctx) { |
| 789 | LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing MM ctx " |
| 790 | "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", |
| 791 | lle, lle->llme->tlli, lle->sapi, sch->nsapi); |
| 792 | return -EIO; |
| 793 | } |
| 794 | gprs_gb_recv_pdu(mmctx); |
| 795 | |
Harald Welte | 7e5bb62 | 2016-09-28 08:20:58 +0800 | [diff] [blame] | 796 | if (scomph) { |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 797 | sne->defrag.pcomp = scomph->pcomp; |
| 798 | sne->defrag.dcomp = scomph->dcomp; |
| 799 | sne->defrag.proto = lle->llme->comp.proto; |
| 800 | sne->defrag.data = lle->llme->comp.data; |
| 801 | } |
| 802 | |
Harald Welte | ab4094c | 2010-07-02 16:01:47 +0200 | [diff] [blame] | 803 | /* any non-first segment is by definition something to defragment |
| 804 | * as is any segment that tells us there are more segments */ |
| 805 | if (!sch->first || sch->more) |
Harald Welte | 60da7d4 | 2010-07-02 15:45:12 +0200 | [diff] [blame] | 806 | return defrag_input(sne, msg, hdr, len); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 807 | |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 808 | npdu_num = (suh->npdu_high << 8) | suh->npdu_low; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 809 | npdu = (uint8_t *)suh + sizeof(*suh); |
Alexander Couzens | 410bc9b | 2018-09-18 20:01:28 +0200 | [diff] [blame] | 810 | npdu_len = (msg->data + msg->len) - npdu; |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 811 | |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 812 | if (npdu_len <= 0) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 813 | LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 814 | return -EIO; |
| 815 | } |
| 816 | /* actually send the N-PDU to the SGSN core code, which then |
| 817 | * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */ |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 818 | |
| 819 | /* Decompress packet */ |
| 820 | #if DEBUG_IP_PACKETS == 1 |
| 821 | DEBUGP(DSNDCP, " \n"); |
| 822 | DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n"); |
| 823 | DEBUGP(DSNDCP, "===================================================\n"); |
| 824 | #endif |
| 825 | if (any_pcomp_or_dcomp_active(sgsn)) { |
| 826 | |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 827 | expnd = talloc_zero_size(msg, npdu_len * MAX_DATADECOMPR_FAC + |
| 828 | MAX_HDRDECOMPR_INCR); |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 829 | memcpy(expnd, npdu, npdu_len); |
| 830 | |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 831 | /* Apply data decompression */ |
| 832 | rc = gprs_sndcp_dcomp_expand(expnd, npdu_len, sne->defrag.dcomp, |
| 833 | sne->defrag.data); |
| 834 | if (rc < 0) { |
| 835 | LOGP(DSNDCP, LOGL_ERROR, |
| 836 | "Data decompression failed!\n"); |
| 837 | talloc_free(expnd); |
| 838 | return -EIO; |
| 839 | } |
| 840 | |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 841 | /* Apply header decompression */ |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 842 | rc = gprs_sndcp_pcomp_expand(expnd, rc, sne->defrag.pcomp, |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 843 | sne->defrag.proto); |
| 844 | if (rc < 0) { |
| 845 | LOGP(DSNDCP, LOGL_ERROR, |
| 846 | "TCP/IP Header decompression failed!\n"); |
| 847 | talloc_free(expnd); |
| 848 | return -EIO; |
| 849 | } |
| 850 | |
| 851 | /* Modify npu length, expnd is handed directly handed |
| 852 | * over to gsn_rx_sndcp_ud_ind(), see below */ |
| 853 | npdu_len = rc; |
| 854 | } else |
| 855 | expnd = npdu; |
| 856 | #if DEBUG_IP_PACKETS == 1 |
| 857 | debug_ip_packet(expnd, npdu_len, 1, "sndcp_llunitdata_ind()"); |
| 858 | DEBUGP(DSNDCP, "===================================================\n"); |
| 859 | DEBUGP(DSNDCP, ":::::::::::::::::::::::::::::::::::::::::::::::::::\n"); |
| 860 | DEBUGP(DSNDCP, " \n"); |
| 861 | #endif |
| 862 | |
| 863 | /* Hand off packet to gtp */ |
| 864 | rc = sgsn_rx_sndcp_ud_ind(&sne->ra_id, lle->llme->tlli, |
| 865 | sne->nsapi, msg, npdu_len, expnd); |
| 866 | |
| 867 | if (any_pcomp_or_dcomp_active(sgsn)) |
| 868 | talloc_free(expnd); |
| 869 | |
| 870 | return rc; |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 871 | } |
| 872 | |
Holger Hans Peter Freyther | cfee952 | 2014-04-04 12:43:08 +0200 | [diff] [blame] | 873 | #if 0 |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 874 | /* Section 5.1.2.1 LL-RESET.ind */ |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 875 | static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se) |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 876 | { |
| 877 | /* treat all outstanding SNDCP-LLC request type primitives as not sent */ |
| 878 | /* reset all SNDCP XID parameters to default values */ |
Holger Hans Peter Freyther | 6142dc4 | 2011-10-14 23:37:27 +0200 | [diff] [blame] | 879 | LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n"); |
| 880 | return 0; |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 881 | } |
| 882 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 883 | static int sndcp_ll_status_ind() |
| 884 | { |
| 885 | /* inform the SM sub-layer by means of SNSM-STATUS.req */ |
Holger Hans Peter Freyther | 6142dc4 | 2011-10-14 23:37:27 +0200 | [diff] [blame] | 886 | LOGP(DSNDCP, LOGL_NOTICE, "not implemented.\n"); |
| 887 | return 0; |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | static struct sndcp_state_list {{ |
| 891 | uint32_t states; |
| 892 | unsigned int type; |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 893 | int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg); |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 894 | } sndcp_state_list[] = { |
| 895 | { ALL_STATES, |
| 896 | LL_RESET_IND, sndcp_ll_reset_ind }, |
| 897 | { ALL_STATES, |
| 898 | LL_ESTABLISH_IND, sndcp_ll_est_ind }, |
| 899 | { SBIT(SNDCP_S_EST_RQD), |
| 900 | LL_ESTABLISH_RESP, sndcp_ll_est_ind }, |
| 901 | { SBIT(SNDCP_S_EST_RQD), |
| 902 | LL_ESTABLISH_CONF, sndcp_ll_est_conf }, |
| 903 | { SBIT(SNDCP_S_ |
| 904 | }; |
| 905 | |
| 906 | static int sndcp_rx_llc_prim() |
| 907 | { |
| 908 | case LL_ESTABLISH_REQ: |
| 909 | case LL_RELEASE_REQ: |
| 910 | case LL_XID_REQ: |
| 911 | case LL_DATA_REQ: |
| 912 | LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */ |
| 913 | |
| 914 | switch (prim) { |
| 915 | case LL_RESET_IND: |
| 916 | case LL_ESTABLISH_IND: |
| 917 | case LL_ESTABLISH_RESP: |
| 918 | case LL_ESTABLISH_CONF: |
| 919 | case LL_RELEASE_IND: |
| 920 | case LL_RELEASE_CONF: |
| 921 | case LL_XID_IND: |
| 922 | case LL_XID_RESP: |
| 923 | case LL_XID_CONF: |
| 924 | case LL_DATA_IND: |
| 925 | case LL_DATA_CONF: |
| 926 | case LL_UNITDATA_IND: |
| 927 | case LL_STATUS_IND: |
Neels Hofmeyr | cc7db18 | 2016-12-18 23:52:38 +0100 | [diff] [blame] | 928 | } |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 929 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 930 | #endif |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 931 | |
| 932 | /* Generate SNDCP-XID message */ |
| 933 | static int gprs_llc_gen_sndcp_xid(uint8_t *bytes, int bytes_len, uint8_t nsapi) |
| 934 | { |
| 935 | int entity = 0; |
| 936 | LLIST_HEAD(comp_fields); |
| 937 | struct gprs_sndcp_pcomp_rfc1144_params rfc1144_params; |
| 938 | struct gprs_sndcp_comp_field rfc1144_comp_field; |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 939 | struct gprs_sndcp_dcomp_v42bis_params v42bis_params; |
| 940 | struct gprs_sndcp_comp_field v42bis_comp_field; |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 941 | |
| 942 | memset(&rfc1144_comp_field, 0, sizeof(struct gprs_sndcp_comp_field)); |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 943 | memset(&v42bis_comp_field, 0, sizeof(struct gprs_sndcp_comp_field)); |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 944 | |
| 945 | /* Setup rfc1144 */ |
| 946 | if (sgsn->cfg.pcomp_rfc1144.active) { |
| 947 | rfc1144_params.nsapi[0] = nsapi; |
| 948 | rfc1144_params.nsapi_len = 1; |
| 949 | rfc1144_params.s01 = sgsn->cfg.pcomp_rfc1144.s01; |
| 950 | rfc1144_comp_field.p = 1; |
| 951 | rfc1144_comp_field.entity = entity; |
Stefan Sperling | c572154 | 2018-11-07 16:33:39 +0100 | [diff] [blame] | 952 | rfc1144_comp_field.algo.pcomp = RFC_1144; |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 953 | rfc1144_comp_field.comp[RFC1144_PCOMP1] = 1; |
| 954 | rfc1144_comp_field.comp[RFC1144_PCOMP2] = 2; |
| 955 | rfc1144_comp_field.comp_len = RFC1144_PCOMP_NUM; |
| 956 | rfc1144_comp_field.rfc1144_params = &rfc1144_params; |
| 957 | entity++; |
| 958 | llist_add(&rfc1144_comp_field.list, &comp_fields); |
| 959 | } |
| 960 | |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 961 | /* Setup V.42bis */ |
| 962 | if (sgsn->cfg.dcomp_v42bis.active) { |
| 963 | v42bis_params.nsapi[0] = nsapi; |
| 964 | v42bis_params.nsapi_len = 1; |
| 965 | v42bis_params.p0 = sgsn->cfg.dcomp_v42bis.p0; |
| 966 | v42bis_params.p1 = sgsn->cfg.dcomp_v42bis.p1; |
| 967 | v42bis_params.p2 = sgsn->cfg.dcomp_v42bis.p2; |
| 968 | v42bis_comp_field.p = 1; |
| 969 | v42bis_comp_field.entity = entity; |
Stefan Sperling | c572154 | 2018-11-07 16:33:39 +0100 | [diff] [blame] | 970 | v42bis_comp_field.algo.dcomp = V42BIS; |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 971 | v42bis_comp_field.comp[V42BIS_DCOMP1] = 1; |
| 972 | v42bis_comp_field.comp_len = V42BIS_DCOMP_NUM; |
| 973 | v42bis_comp_field.v42bis_params = &v42bis_params; |
| 974 | entity++; |
| 975 | llist_add(&v42bis_comp_field.list, &comp_fields); |
| 976 | } |
| 977 | |
Philipp | db142dc | 2016-12-22 14:15:20 +0100 | [diff] [blame] | 978 | /* Do not attempt to compile anything if there is no data in the list */ |
| 979 | if (llist_empty(&comp_fields)) |
| 980 | return 0; |
| 981 | |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 982 | /* Compile bytestream */ |
Philipp | db142dc | 2016-12-22 14:15:20 +0100 | [diff] [blame] | 983 | return gprs_sndcp_compile_xid(bytes, bytes_len, &comp_fields, |
| 984 | DEFAULT_SNDCP_VERSION); |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /* Set of SNDCP-XID bnegotiation (See also: TS 144 065, |
| 988 | * Section 6.8 XID parameter negotiation) */ |
| 989 | int sndcp_sn_xid_req(struct gprs_llc_lle *lle, uint8_t nsapi) |
| 990 | { |
| 991 | /* Note: The specification requires the SNDCP-User to set of an |
| 992 | * SNDCP xid request. See also 3GPP TS 44.065, 6.8 XID parameter |
| 993 | * negotiation, Figure 11: SNDCP XID negotiation procedure. In |
| 994 | * our case the SNDCP-User is sgsn_libgtp.c, which calls |
| 995 | * sndcp_sn_xid_req directly. */ |
| 996 | |
| 997 | uint8_t l3params[1024]; |
| 998 | int xid_len; |
| 999 | struct gprs_llc_xid_field xid_field_request; |
| 1000 | |
| 1001 | /* Wipe off all compression entities and their states to |
| 1002 | * get rid of possible leftovers from a previous session */ |
| 1003 | gprs_sndcp_comp_free(lle->llme->comp.proto); |
| 1004 | gprs_sndcp_comp_free(lle->llme->comp.data); |
| 1005 | lle->llme->comp.proto = gprs_sndcp_comp_alloc(lle->llme); |
| 1006 | lle->llme->comp.data = gprs_sndcp_comp_alloc(lle->llme); |
Harald Welte | af779d2 | 2019-04-12 16:56:04 +0200 | [diff] [blame] | 1007 | talloc_free(lle->xid); |
| 1008 | lle->xid = NULL; |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1009 | |
| 1010 | /* Generate compression parameter bytestream */ |
| 1011 | xid_len = gprs_llc_gen_sndcp_xid(l3params, sizeof(l3params), nsapi); |
| 1012 | |
| 1013 | /* Send XID with the SNDCP-XID bytetsream included */ |
| 1014 | if (xid_len > 0) { |
| 1015 | xid_field_request.type = GPRS_LLC_XID_T_L3_PAR; |
| 1016 | xid_field_request.data = l3params; |
| 1017 | xid_field_request.data_len = xid_len; |
| 1018 | return gprs_ll_xid_req(lle, &xid_field_request); |
| 1019 | } |
| 1020 | |
| 1021 | /* When bytestream can not be generated, proceed without SNDCP-XID */ |
| 1022 | return gprs_ll_xid_req(lle, NULL); |
| 1023 | |
| 1024 | } |
| 1025 | |
| 1026 | /* Handle header compression entites */ |
| 1027 | static int handle_pcomp_entities(struct gprs_sndcp_comp_field *comp_field, |
| 1028 | struct gprs_llc_lle *lle) |
| 1029 | { |
| 1030 | /* Note: This functions also transforms the comp_field into its |
| 1031 | * echo form (strips comp values, resets propose bit etc...) |
| 1032 | * the processed comp_fields can then be sent back as XID- |
| 1033 | * Response without further modification. */ |
| 1034 | |
| 1035 | /* Delete propose bit */ |
| 1036 | comp_field->p = 0; |
| 1037 | |
| 1038 | /* Process proposed parameters */ |
Stefan Sperling | c572154 | 2018-11-07 16:33:39 +0100 | [diff] [blame] | 1039 | switch (comp_field->algo.pcomp) { |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1040 | case RFC_1144: |
| 1041 | if (sgsn->cfg.pcomp_rfc1144.passive |
| 1042 | && comp_field->rfc1144_params->nsapi_len > 0) { |
| 1043 | DEBUGP(DSNDCP, |
| 1044 | "Accepting RFC1144 header compression...\n"); |
| 1045 | gprs_sndcp_comp_add(lle->llme, lle->llme->comp.proto, |
| 1046 | comp_field); |
| 1047 | } else { |
| 1048 | DEBUGP(DSNDCP, |
| 1049 | "Rejecting RFC1144 header compression...\n"); |
| 1050 | gprs_sndcp_comp_delete(lle->llme->comp.proto, |
| 1051 | comp_field->entity); |
| 1052 | comp_field->rfc1144_params->nsapi_len = 0; |
| 1053 | } |
| 1054 | break; |
| 1055 | case RFC_2507: |
| 1056 | /* RFC 2507 is not yet supported, |
| 1057 | * so we set applicable nsapis to zero */ |
| 1058 | DEBUGP(DSNDCP, "Rejecting RFC2507 header compression...\n"); |
| 1059 | comp_field->rfc2507_params->nsapi_len = 0; |
| 1060 | gprs_sndcp_comp_delete(lle->llme->comp.proto, |
| 1061 | comp_field->entity); |
| 1062 | break; |
| 1063 | case ROHC: |
| 1064 | /* ROHC is not yet supported, |
| 1065 | * so we set applicable nsapis to zero */ |
| 1066 | DEBUGP(DSNDCP, "Rejecting ROHC header compression...\n"); |
| 1067 | comp_field->rohc_params->nsapi_len = 0; |
| 1068 | gprs_sndcp_comp_delete(lle->llme->comp.proto, |
| 1069 | comp_field->entity); |
| 1070 | break; |
| 1071 | } |
| 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | /* Hanle data compression entites */ |
| 1077 | static int handle_dcomp_entities(struct gprs_sndcp_comp_field *comp_field, |
| 1078 | struct gprs_llc_lle *lle) |
| 1079 | { |
| 1080 | /* See note in handle_pcomp_entities() */ |
| 1081 | |
| 1082 | /* Delete propose bit */ |
| 1083 | comp_field->p = 0; |
| 1084 | |
| 1085 | /* Process proposed parameters */ |
Stefan Sperling | c572154 | 2018-11-07 16:33:39 +0100 | [diff] [blame] | 1086 | switch (comp_field->algo.dcomp) { |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1087 | case V42BIS: |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 1088 | if (sgsn->cfg.dcomp_v42bis.passive && |
| 1089 | comp_field->v42bis_params->nsapi_len > 0) { |
| 1090 | DEBUGP(DSNDCP, |
| 1091 | "Accepting V.42bis data compression...\n"); |
| 1092 | gprs_sndcp_comp_add(lle->llme, lle->llme->comp.data, |
| 1093 | comp_field); |
| 1094 | } else { |
| 1095 | LOGP(DSNDCP, LOGL_DEBUG, |
| 1096 | "Rejecting V.42bis data compression...\n"); |
| 1097 | gprs_sndcp_comp_delete(lle->llme->comp.data, |
| 1098 | comp_field->entity); |
| 1099 | comp_field->v42bis_params->nsapi_len = 0; |
| 1100 | } |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1101 | break; |
| 1102 | case V44: |
| 1103 | /* V44 is not yet supported, |
| 1104 | * so we set applicable nsapis to zero */ |
| 1105 | DEBUGP(DSNDCP, "Rejecting V.44 data compression...\n"); |
| 1106 | comp_field->v44_params->nsapi_len = 0; |
| 1107 | gprs_sndcp_comp_delete(lle->llme->comp.data, |
| 1108 | comp_field->entity); |
| 1109 | break; |
| 1110 | } |
| 1111 | |
| 1112 | return 0; |
| 1113 | |
| 1114 | } |
| 1115 | |
| 1116 | /* Process SNDCP-XID indication |
| 1117 | * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */ |
| 1118 | int sndcp_sn_xid_ind(struct gprs_llc_xid_field *xid_field_indication, |
| 1119 | struct gprs_llc_xid_field *xid_field_response, |
| 1120 | struct gprs_llc_lle *lle) |
| 1121 | { |
| 1122 | /* Note: This function computes the SNDCP-XID response that is sent |
| 1123 | * back to the ms when a ms originated XID is received. The |
| 1124 | * Input XID fields are directly processed and the result is directly |
| 1125 | * handed back. */ |
| 1126 | |
| 1127 | int rc; |
| 1128 | int compclass; |
Philipp | db142dc | 2016-12-22 14:15:20 +0100 | [diff] [blame] | 1129 | int version; |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1130 | |
| 1131 | struct llist_head *comp_fields; |
| 1132 | struct gprs_sndcp_comp_field *comp_field; |
| 1133 | |
| 1134 | OSMO_ASSERT(xid_field_indication); |
| 1135 | OSMO_ASSERT(xid_field_response); |
| 1136 | OSMO_ASSERT(lle); |
| 1137 | |
Keith | bfd67d2 | 2019-04-29 18:23:10 +0100 | [diff] [blame] | 1138 | /* Some phones send zero byte length SNDCP frames |
| 1139 | * and do require a confirmation response. */ |
| 1140 | if (xid_field_indication->data_len == 0) { |
| 1141 | xid_field_response->type = GPRS_LLC_XID_T_L3_PAR; |
| 1142 | xid_field_response->data_len = 0; |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1146 | /* Parse SNDCP-CID XID-Field */ |
Philipp | db142dc | 2016-12-22 14:15:20 +0100 | [diff] [blame] | 1147 | comp_fields = gprs_sndcp_parse_xid(&version, lle->llme, |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1148 | xid_field_indication->data, |
| 1149 | xid_field_indication->data_len, |
| 1150 | NULL); |
| 1151 | if (!comp_fields) |
| 1152 | return -EINVAL; |
| 1153 | |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1154 | /* Handle compression entites */ |
| 1155 | DEBUGP(DSNDCP, "SNDCP-XID-IND (ms):\n"); |
| 1156 | gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG); |
| 1157 | |
| 1158 | llist_for_each_entry(comp_field, comp_fields, list) { |
| 1159 | compclass = gprs_sndcp_get_compression_class(comp_field); |
| 1160 | if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION) |
| 1161 | rc = handle_pcomp_entities(comp_field, lle); |
| 1162 | else if (compclass == SNDCP_XID_DATA_COMPRESSION) |
| 1163 | rc = handle_dcomp_entities(comp_field, lle); |
| 1164 | else { |
| 1165 | gprs_sndcp_comp_delete(lle->llme->comp.proto, |
| 1166 | comp_field->entity); |
| 1167 | gprs_sndcp_comp_delete(lle->llme->comp.data, |
| 1168 | comp_field->entity); |
| 1169 | rc = 0; |
| 1170 | } |
| 1171 | |
| 1172 | if (rc < 0) { |
| 1173 | talloc_free(comp_fields); |
| 1174 | return -EINVAL; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | DEBUGP(DSNDCP, "SNDCP-XID-RES (sgsn):\n"); |
| 1179 | gprs_sndcp_dump_comp_fields(comp_fields, LOGL_DEBUG); |
| 1180 | |
| 1181 | /* Reserve some memory to store the modified SNDCP-XID bytes */ |
| 1182 | xid_field_response->data = |
| 1183 | talloc_zero_size(lle->llme, xid_field_indication->data_len); |
| 1184 | |
| 1185 | /* Set Type flag for response */ |
| 1186 | xid_field_response->type = GPRS_LLC_XID_T_L3_PAR; |
| 1187 | |
| 1188 | /* Compile modified SNDCP-XID bytes */ |
| 1189 | rc = gprs_sndcp_compile_xid(xid_field_response->data, |
| 1190 | xid_field_indication->data_len, |
Philipp | db142dc | 2016-12-22 14:15:20 +0100 | [diff] [blame] | 1191 | comp_fields, 0); |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1192 | |
| 1193 | if (rc > 0) |
| 1194 | xid_field_response->data_len = rc; |
| 1195 | else { |
| 1196 | talloc_free(xid_field_response->data); |
| 1197 | xid_field_response->data = NULL; |
| 1198 | xid_field_response->data_len = 0; |
| 1199 | return -EINVAL; |
| 1200 | } |
| 1201 | |
| 1202 | talloc_free(comp_fields); |
| 1203 | |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | /* Process SNDCP-XID indication |
| 1208 | * (See also: TS 144 065, Section 6.8 XID parameter negotiation) */ |
| 1209 | int sndcp_sn_xid_conf(struct gprs_llc_xid_field *xid_field_conf, |
| 1210 | struct gprs_llc_xid_field *xid_field_request, |
| 1211 | struct gprs_llc_lle *lle) |
| 1212 | { |
| 1213 | /* Note: This function handles an incomming SNDCP-XID confirmiation. |
| 1214 | * Since the confirmation fields may lack important parameters we |
| 1215 | * will reconstruct these missing fields using the original request |
| 1216 | * we have sent. After that we will create (or delete) the |
| 1217 | * compression entites */ |
| 1218 | |
| 1219 | struct llist_head *comp_fields_req; |
| 1220 | struct llist_head *comp_fields_conf; |
| 1221 | struct gprs_sndcp_comp_field *comp_field; |
| 1222 | int rc; |
| 1223 | int compclass; |
| 1224 | |
| 1225 | /* We need both, the confirmation that is sent back by the ms, |
| 1226 | * and the original request we have sent. If one of this is missing |
| 1227 | * we can not process the confirmation, the caller must check if |
| 1228 | * request and confirmation fields are available. */ |
| 1229 | OSMO_ASSERT(xid_field_conf); |
| 1230 | OSMO_ASSERT(xid_field_request); |
| 1231 | |
| 1232 | /* Parse SNDCP-CID XID-Field */ |
Philipp | db142dc | 2016-12-22 14:15:20 +0100 | [diff] [blame] | 1233 | comp_fields_req = gprs_sndcp_parse_xid(NULL, lle->llme, |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1234 | xid_field_request->data, |
| 1235 | xid_field_request->data_len, |
| 1236 | NULL); |
| 1237 | if (!comp_fields_req) |
| 1238 | return -EINVAL; |
| 1239 | |
| 1240 | DEBUGP(DSNDCP, "SNDCP-XID-REQ (sgsn):\n"); |
| 1241 | gprs_sndcp_dump_comp_fields(comp_fields_req, LOGL_DEBUG); |
| 1242 | |
| 1243 | /* Parse SNDCP-CID XID-Field */ |
Philipp | db142dc | 2016-12-22 14:15:20 +0100 | [diff] [blame] | 1244 | comp_fields_conf = gprs_sndcp_parse_xid(NULL, lle->llme, |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 1245 | xid_field_conf->data, |
| 1246 | xid_field_conf->data_len, |
| 1247 | comp_fields_req); |
| 1248 | if (!comp_fields_conf) |
| 1249 | return -EINVAL; |
| 1250 | |
| 1251 | DEBUGP(DSNDCP, "SNDCP-XID-CONF (ms):\n"); |
| 1252 | gprs_sndcp_dump_comp_fields(comp_fields_conf, LOGL_DEBUG); |
| 1253 | |
| 1254 | /* Handle compression entites */ |
| 1255 | llist_for_each_entry(comp_field, comp_fields_conf, list) { |
| 1256 | compclass = gprs_sndcp_get_compression_class(comp_field); |
| 1257 | if (compclass == SNDCP_XID_PROTOCOL_COMPRESSION) |
| 1258 | rc = handle_pcomp_entities(comp_field, lle); |
| 1259 | else if (compclass == SNDCP_XID_DATA_COMPRESSION) |
| 1260 | rc = handle_dcomp_entities(comp_field, lle); |
| 1261 | else { |
| 1262 | gprs_sndcp_comp_delete(lle->llme->comp.proto, |
| 1263 | comp_field->entity); |
| 1264 | gprs_sndcp_comp_delete(lle->llme->comp.data, |
| 1265 | comp_field->entity); |
| 1266 | rc = 0; |
| 1267 | } |
| 1268 | |
| 1269 | if (rc < 0) { |
| 1270 | talloc_free(comp_fields_req); |
| 1271 | talloc_free(comp_fields_conf); |
| 1272 | return -EINVAL; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | talloc_free(comp_fields_req); |
| 1277 | talloc_free(comp_fields_conf); |
| 1278 | |
| 1279 | return 0; |
| 1280 | } |