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