Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 1 | /* Test SLHC/RFC1144 TCP/IP Header compression/decompression */ |
| 2 | |
| 3 | /* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Philipp Maier |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 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 |
| 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 |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 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/>. |
| 20 | */ |
| 21 | |
| 22 | #include <openbsc/slhc.h> |
| 23 | #include <openbsc/debug.h> |
| 24 | |
| 25 | #include <osmocom/core/talloc.h> |
| 26 | #include <osmocom/core/utils.h> |
| 27 | |
| 28 | #include <osmocom/core/application.h> |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <arpa/inet.h> |
| 33 | |
| 34 | /* Number of compression slots (S0-1) */ |
| 35 | #define SLOTS 8 |
| 36 | |
| 37 | /* Maximum packet bytes to display */ |
| 38 | #define DISP_MAX_BYTES 100 |
| 39 | |
| 40 | /* Sample packets to test with */ |
Philipp | 91327a4 | 2016-09-26 17:04:39 +0200 | [diff] [blame] | 41 | #define PACKETS_LEN 15 |
Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 42 | char *packets[] = { |
Philipp | 91327a4 | 2016-09-26 17:04:39 +0200 | [diff] [blame] | 43 | /* With TCP Option 10 (Timestamps) in place (forces UNCOMPRESSED_TCP) */ |
Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 44 | "4510004046dd40004006a9a7c0a8646ec0a864640017ad8b81980100f3ac984d801800e32a1600000101080a000647de06d1bf5efffd18fffd20fffd23fffd27", |
| 45 | "4510005b46de40004006a98bc0a8646ec0a864640017ad8b8198010cf3ac984d801800e3867500000101080a000647df06d1bf61fffb03fffd1ffffd21fffe22fffb05fffa2001fff0fffa2301fff0fffa2701fff0fffa1801fff0", |
| 46 | "4510003746df40004006a9aec0a8646ec0a864640017ad8b81980133f3ac989f801800e35fd700000101080a000647e106d1bf63fffd01", |
| 47 | "4510003746e040004006a9adc0a8646ec0a864640017ad8b81980136f3ac98a2801800e35fd200000101080a000647e106d1bf64fffb01", |
| 48 | "4510007446e140004006a96fc0a8646ec0a864640017ad8b81980139f3ac98a5801800e37b9b00000101080a000647e206d1bf640d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d0d0a57656c6c636f6d6520746f20706f6c6c75780d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d0d0a0d0a", |
Philipp | 91327a4 | 2016-09-26 17:04:39 +0200 | [diff] [blame] | 49 | "4510004246e240004006a9a0c0a8646ec0a864640017ad8b81980179f3ac98a5801800e3dab000000101080a000647ec06d1bf6f706f6c6c7578206c6f67696e3a20", |
| 50 | /* Regular TCP packets (COMPRESSED_TCP) */ |
| 51 | "4510003446dd40004006a9b3c0a8646ec0a864640017ad8b81980100f3ac984d501800e371410000fffd18fffd20fffd23fffd27", |
| 52 | "4510004f46de40004006a997c0a8646ec0a864640017ad8b8198010cf3ac984d501800e3cda40000fffb03fffd1ffffd21fffe22fffb05fffa2001fff0fffa2301fff0fffa2701fff0fffa1801fff0", |
| 53 | "4510002b46df40004006a9bac0a8646ec0a864640017ad8b81980133f3ac989f501800e3a70a0000fffd01", |
| 54 | "4510002b46e040004006a9b9c0a8646ec0a864640017ad8b81980136f3ac98a2501800e3a7060000fffb01", |
| 55 | "4510006846e140004006a97bc0a8646ec0a864640017ad8b81980139f3ac98a5501800e3c2d000000d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d0d0a57656c6c636f6d6520746f20706f6c6c75780d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d0d0a0d0a", |
| 56 | "4510003646e240004006a9acc0a8646ec0a864640017ad8b81980179f3ac98a5501800e321fb0000706f6c6c7578206c6f67696e3a20", |
| 57 | /* UDP packets (TYPE_IP */ |
| 58 | "450000396e0b40004011a0310a0901650a09170105da003500255489a60f01000001000000000000076f736d6f636f6d036f72670000010001", |
| 59 | "450000dc9eeb00004011aeae0a0917010a090165003505da00c83fbaa60f81800001000100030004076f736d6f636f6d036f72670000010001c00c00010001000079be0004904c2b4cc00c000200010000173d00130773756e6265616d08676e756d6f6e6b73c014c00c000200010000173d000603646e73c041c00c000200010000173d000a0767616e65736861c041c058000100010000173d0004d55f2e45c058001c00010000173d0010200107800045f0460000000000690001c06a0001000100006a710004d55f1b78c039000100010000173d000453ecb2cb", |
| 60 | "45000037652340004011a91b0a0901650a091701ef1b0035002376a2c3910100000100000000000006676f6f676c650264650000010001", |
| 61 | "0050b6162c10000db93a3ff908004500004726a6000038114083080808080a0901650035ef1b00338a8cc3918180000100010000000006676f6f676c650264650000010001c00c000100010000012b0004d83ad503", |
Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /* Compress a packet using Van Jacobson RFC1144 header compression */ |
| 65 | static int compress(uint8_t *data_o, uint8_t *data_i, int len, |
| 66 | struct slcompress *comp) |
| 67 | { |
| 68 | uint8_t *comp_ptr; /* Not used */ |
| 69 | int compr_len; |
| 70 | |
| 71 | /* Create a working copy of the incoming data */ |
| 72 | memcpy(data_o, data_i, len); |
| 73 | |
| 74 | /* Run compressor */ |
| 75 | compr_len = slhc_compress(comp, data_i, len, data_o, &comp_ptr, 0); |
| 76 | return compr_len; |
| 77 | } |
| 78 | |
| 79 | /* Expand a packet using Van Jacobson RFC1144 header compression */ |
| 80 | static int expand(uint8_t *data_o, uint8_t *data_i, int len, |
| 81 | struct slcompress *comp) |
| 82 | { |
| 83 | int data_decompressed_len; |
| 84 | |
| 85 | /* Create a working copy of the incoming data */ |
| 86 | memcpy(data_o, data_i, len); |
| 87 | |
| 88 | /* Handle an uncompressed packet (learn header information */ |
| 89 | if ((data_i[0] & SL_TYPE_UNCOMPRESSED_TCP) == SL_TYPE_UNCOMPRESSED_TCP) { |
| 90 | data_o[0] &= 0x4F; |
| 91 | data_decompressed_len = slhc_remember(comp, data_o, len); |
| 92 | return data_decompressed_len; |
| 93 | } |
| 94 | |
| 95 | /* Uncompress compressed packets */ |
| 96 | else if (data_o[0] & SL_TYPE_COMPRESSED_TCP) { |
| 97 | data_decompressed_len = slhc_uncompress(comp, data_o, len); |
| 98 | return data_decompressed_len; |
| 99 | } |
| 100 | |
| 101 | /* Regular or unknown packets will not be touched */ |
| 102 | return len; |
| 103 | } |
| 104 | |
| 105 | /* Calculate IP Header checksum */ |
| 106 | static uint16_t calc_ip_csum(uint8_t *data, int len) |
| 107 | { |
| 108 | int i; |
| 109 | uint32_t accumulator = 0; |
| 110 | uint16_t *pointer = (uint16_t *) data; |
| 111 | |
| 112 | for (i = len; i > 1; i -= 2) { |
| 113 | accumulator += *pointer; |
| 114 | pointer++; |
| 115 | } |
| 116 | |
| 117 | if (len % 2) |
| 118 | accumulator += *pointer; |
| 119 | |
| 120 | accumulator = (accumulator & 0xffff) + ((accumulator >> 16) & 0xffff); |
| 121 | accumulator += (accumulator >> 16) & 0xffff; |
| 122 | return (~accumulator); |
| 123 | } |
| 124 | |
| 125 | /* Calculate TCP/IP checksum */ |
| 126 | static uint16_t calc_tcpip_csum(const void *ctx, uint8_t *packet, int len) |
| 127 | { |
| 128 | uint8_t *buf; |
| 129 | uint16_t csum; |
| 130 | |
| 131 | buf = talloc_zero_size(ctx, len); |
| 132 | memset(buf, 0, len); |
| 133 | memcpy(buf, packet + 12, 8); |
| 134 | buf[9] = packet[9]; |
| 135 | buf[11] = (len - 20) & 0xFF; |
| 136 | buf[10] = (len - 20) >> 8 & 0xFF; |
| 137 | memcpy(buf + 12, packet + 20, len - 20); |
| 138 | csum = calc_ip_csum(buf, len - 20 + 12); |
| 139 | talloc_free(buf); |
| 140 | return csum; |
| 141 | } |
| 142 | |
| 143 | /* Check TCP/IP packet */ |
| 144 | static void check_packet(const void *ctx, uint8_t *packet, int len) |
| 145 | { |
| 146 | /* Check IP header */ |
| 147 | OSMO_ASSERT(len > 20); |
| 148 | OSMO_ASSERT(calc_ip_csum(packet, 20) == 0); |
| 149 | |
Philipp | 91327a4 | 2016-09-26 17:04:39 +0200 | [diff] [blame] | 150 | printf("packet[9]=%02x\n", packet[9]); |
| 151 | |
Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 152 | /* Check TCP packet */ |
| 153 | if (packet[9] != 0x06) |
| 154 | return; |
| 155 | OSMO_ASSERT(len > 40); |
| 156 | OSMO_ASSERT(calc_tcpip_csum(ctx, packet, len) == 0); |
| 157 | } |
| 158 | |
Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 159 | /* Compress / Decompress packets */ |
| 160 | static void test_slhc(const void *ctx) |
| 161 | { |
| 162 | char packet_ascii[2048]; |
| 163 | int i; |
| 164 | |
| 165 | struct slcompress *comp; |
| 166 | uint8_t packet[1024]; |
| 167 | int packet_len; |
| 168 | uint8_t packet_compr[1024]; |
| 169 | int packet_compr_len; |
| 170 | uint8_t packet_decompr[1024]; |
| 171 | int packet_decompr_len; |
| 172 | |
| 173 | printf("Allocating compression state...\n"); |
| 174 | comp = slhc_init(ctx, SLOTS, SLOTS); |
| 175 | OSMO_ASSERT(comp); |
| 176 | |
Philipp | 91327a4 | 2016-09-26 17:04:39 +0200 | [diff] [blame] | 177 | for (i = 0; i < PACKETS_LEN; i++) { |
| 178 | printf("Testing with packet No. %d\n", i); |
| 179 | |
Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 180 | /* Read input file */ |
| 181 | memset(packet_ascii, 0, sizeof(packet_ascii)); |
| 182 | memset(packet, 0, sizeof(packet)); |
| 183 | memset(packet_compr, 0, sizeof(packet_compr)); |
| 184 | memset(packet_decompr, 0, sizeof(packet_decompr)); |
Philipp | 3c175ed | 2016-09-28 15:10:14 +0200 | [diff] [blame] | 185 | |
| 186 | OSMO_ASSERT(strlen(packets[i]) < sizeof(packet_ascii)); |
Philipp | 91327a4 | 2016-09-26 17:04:39 +0200 | [diff] [blame] | 187 | strcpy(packet_ascii, packets[i]); |
Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 188 | |
| 189 | packet_len = |
| 190 | osmo_hexparse(packet_ascii, packet, sizeof(packet)); |
| 191 | check_packet(ctx, packet, packet_len); |
Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 192 | |
| 193 | /* Run compression/decompression algorithm */ |
| 194 | printf("Compressing...\n"); |
| 195 | packet_compr_len = |
| 196 | compress(packet_compr, packet, packet_len, comp); |
| 197 | printf("Decompressing...\n"); |
| 198 | packet_decompr_len = |
| 199 | expand(packet_decompr, packet_compr, packet_compr_len, |
| 200 | comp); |
| 201 | OSMO_ASSERT(packet_decompr_len == packet_len); |
Philipp | 91327a4 | 2016-09-26 17:04:39 +0200 | [diff] [blame] | 202 | check_packet(ctx, packet_decompr, packet_decompr_len); |
Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 203 | |
| 204 | /* Display results */ |
| 205 | printf("Results:\n"); |
| 206 | if (packet_compr_len > DISP_MAX_BYTES) |
| 207 | packet_compr_len = DISP_MAX_BYTES; |
| 208 | if (packet_len > DISP_MAX_BYTES) |
| 209 | packet_len = DISP_MAX_BYTES; |
| 210 | if (packet_decompr_len > DISP_MAX_BYTES) |
| 211 | packet_decompr_len = DISP_MAX_BYTES; |
| 212 | printf("Original Packet: (%i bytes) %s\n", packet_len, |
| 213 | osmo_hexdump_nospc(packet, packet_len)); |
| 214 | printf("DecompressedPacket: (%i bytes) %s\n", |
| 215 | packet_decompr_len, osmo_hexdump_nospc(packet_decompr, |
| 216 | packet_decompr_len)); |
| 217 | printf("CompressedPacket: (%i bytes) %s\n", packet_compr_len, |
| 218 | osmo_hexdump_nospc(packet_compr, packet_compr_len)); |
| 219 | slhc_o_status(comp); |
| 220 | slhc_o_status(comp); |
| 221 | |
| 222 | printf("\n"); |
| 223 | } |
| 224 | |
| 225 | printf("Freeing compression state...\n"); |
| 226 | slhc_free(comp); |
| 227 | printf("\n"); |
| 228 | } |
| 229 | |
| 230 | static struct log_info_cat gprs_categories[] = { |
| 231 | [DSNDCP] = { |
| 232 | .name = "DSNDCP", |
| 233 | .description = |
| 234 | "GPRS Sub-Network Dependent Control Protocol (SNDCP)", |
| 235 | .enabled = 1,.loglevel = LOGL_DEBUG, |
| 236 | }, |
| 237 | [DSLHC] = { |
| 238 | .name = "DSLHC", |
| 239 | .description = |
| 240 | "Van Jacobson RFC1144 TCP/IP header compression (SLHC)", |
| 241 | .enabled = 1,.loglevel = LOGL_DEBUG, |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | static struct log_info info = { |
| 246 | .cat = gprs_categories, |
| 247 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 248 | }; |
| 249 | |
| 250 | int main(int argc, char **argv) |
| 251 | { |
| 252 | void *ctx; |
| 253 | |
| 254 | osmo_init_logging(&info); |
| 255 | |
| 256 | ctx = talloc_named_const(NULL, 0, "slhc_ctx"); |
| 257 | |
| 258 | test_slhc(ctx); |
| 259 | |
| 260 | printf("Done\n"); |
| 261 | |
| 262 | talloc_report_full(ctx, stderr); |
| 263 | OSMO_ASSERT(talloc_total_blocks(ctx) == 1); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* stubs */ |
| 268 | struct osmo_prim_hdr; |
| 269 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 270 | { |
| 271 | abort(); |
| 272 | } |