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