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