The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that: (1) source code distributions |
| 7 | * retain the above copyright notice and this paragraph in its entirety, (2) |
| 8 | * distributions including binary code include the above copyright notice and |
| 9 | * this paragraph in its entirety in the documentation or other materials |
| 10 | * provided with the distribution, and (3) all advertising materials mentioning |
| 11 | * features or use of this software display the following acknowledgement: |
| 12 | * ``This product includes software developed by the University of California, |
| 13 | * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of |
| 14 | * the University nor the names of its contributors may be used to endorse |
| 15 | * or promote products derived from this software without specific prior |
| 16 | * written permission. |
| 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 20 | */ |
| 21 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 22 | /* \summary: Compressed Serial Line Internet Protocol printer */ |
| 23 | |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include "config.h" |
| 26 | #endif |
| 27 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 28 | #include <netdissect-stdinc.h> |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 29 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 30 | #include "netdissect.h" |
| 31 | #include "extract.h" |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 32 | |
| 33 | #include "ip.h" |
| 34 | #include "tcp.h" |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 35 | #include "slcompress.h" |
| 36 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 37 | /* |
| 38 | * definitions of the pseudo- link-level header attached to slip |
| 39 | * packets grabbed by the packet filter (bpf) traffic monitor. |
| 40 | */ |
| 41 | #define SLIP_HDRLEN 16 |
| 42 | |
| 43 | #define SLX_DIR 0 |
| 44 | #define SLX_CHDR 1 |
| 45 | #define CHDR_LEN 15 |
| 46 | |
| 47 | #define SLIPDIR_IN 0 |
| 48 | #define SLIPDIR_OUT 1 |
| 49 | |
| 50 | static const char tstr[] = "[|slip]"; |
| 51 | |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 52 | static u_int lastlen[2][256]; |
| 53 | static u_int lastconn = 255; |
| 54 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 55 | static void sliplink_print(netdissect_options *, const u_char *, const struct ip *, u_int); |
| 56 | static void compressed_sl_print(netdissect_options *, const u_char *, const struct ip *, u_int, int); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 57 | |
| 58 | u_int |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 59 | sl_if_print(netdissect_options *ndo, |
| 60 | const struct pcap_pkthdr *h, const u_char *p) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 61 | { |
| 62 | register u_int caplen = h->caplen; |
| 63 | register u_int length = h->len; |
| 64 | register const struct ip *ip; |
| 65 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 66 | if (caplen < SLIP_HDRLEN || length < SLIP_HDRLEN) { |
| 67 | ND_PRINT((ndo, "%s", tstr)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 68 | return (caplen); |
| 69 | } |
| 70 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 71 | caplen -= SLIP_HDRLEN; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 72 | length -= SLIP_HDRLEN; |
| 73 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 74 | ip = (const struct ip *)(p + SLIP_HDRLEN); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 75 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 76 | if (ndo->ndo_eflag) |
| 77 | sliplink_print(ndo, p, ip, length); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 78 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 79 | if (caplen < 1 || length < 1) { |
| 80 | ND_PRINT((ndo, "%s", tstr)); |
| 81 | return (caplen + SLIP_HDRLEN); |
| 82 | } |
| 83 | |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 84 | switch (IP_V(ip)) { |
| 85 | case 4: |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 86 | ip_print(ndo, (const u_char *)ip, length); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 87 | break; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 88 | case 6: |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 89 | ip6_print(ndo, (const u_char *)ip, length); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 90 | break; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 91 | default: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 92 | ND_PRINT((ndo, "ip v%d", IP_V(ip))); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return (SLIP_HDRLEN); |
| 96 | } |
| 97 | |
| 98 | u_int |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 99 | sl_bsdos_if_print(netdissect_options *ndo, |
| 100 | const struct pcap_pkthdr *h, const u_char *p) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 101 | { |
| 102 | register u_int caplen = h->caplen; |
| 103 | register u_int length = h->len; |
| 104 | register const struct ip *ip; |
| 105 | |
| 106 | if (caplen < SLIP_HDRLEN) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 107 | ND_PRINT((ndo, "%s", tstr)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 108 | return (caplen); |
| 109 | } |
| 110 | |
| 111 | length -= SLIP_HDRLEN; |
| 112 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 113 | ip = (const struct ip *)(p + SLIP_HDRLEN); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 114 | |
| 115 | #ifdef notdef |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 116 | if (ndo->ndo_eflag) |
| 117 | sliplink_print(ndo, p, ip, length); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 118 | #endif |
| 119 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 120 | ip_print(ndo, (const u_char *)ip, length); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 121 | |
| 122 | return (SLIP_HDRLEN); |
| 123 | } |
| 124 | |
| 125 | static void |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 126 | sliplink_print(netdissect_options *ndo, |
| 127 | register const u_char *p, register const struct ip *ip, |
| 128 | register u_int length) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 129 | { |
| 130 | int dir; |
| 131 | u_int hlen; |
| 132 | |
| 133 | dir = p[SLX_DIR]; |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 134 | switch (dir) { |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 135 | |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 136 | case SLIPDIR_IN: |
| 137 | ND_PRINT((ndo, "I ")); |
| 138 | break; |
| 139 | |
| 140 | case SLIPDIR_OUT: |
| 141 | ND_PRINT((ndo, "O ")); |
| 142 | break; |
| 143 | |
| 144 | default: |
| 145 | ND_PRINT((ndo, "Invalid direction %d ", dir)); |
| 146 | dir = -1; |
| 147 | break; |
| 148 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 149 | if (ndo->ndo_nflag) { |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 150 | /* XXX just dump the header */ |
| 151 | register int i; |
| 152 | |
| 153 | for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 154 | ND_PRINT((ndo, "%02x.", p[i])); |
| 155 | ND_PRINT((ndo, "%02x: ", p[SLX_CHDR + CHDR_LEN - 1])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | switch (p[SLX_CHDR] & 0xf0) { |
| 159 | |
| 160 | case TYPE_IP: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 161 | ND_PRINT((ndo, "ip %d: ", length + SLIP_HDRLEN)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 162 | break; |
| 163 | |
| 164 | case TYPE_UNCOMPRESSED_TCP: |
| 165 | /* |
| 166 | * The connection id is stored in the IP protocol field. |
| 167 | * Get it from the link layer since sl_uncompress_tcp() |
| 168 | * has restored the IP header copy to IPPROTO_TCP. |
| 169 | */ |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 170 | lastconn = ((const struct ip *)&p[SLX_CHDR])->ip_p; |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 171 | ND_PRINT((ndo, "utcp %d: ", lastconn)); |
| 172 | if (dir == -1) { |
| 173 | /* Direction is bogus, don't use it */ |
| 174 | return; |
| 175 | } |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 176 | hlen = IP_HL(ip); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 177 | hlen += TH_OFF((const struct tcphdr *)&((const int *)ip)[hlen]); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 178 | lastlen[dir][lastconn] = length - (hlen << 2); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 179 | break; |
| 180 | |
| 181 | default: |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 182 | if (dir == -1) { |
| 183 | /* Direction is bogus, don't use it */ |
| 184 | return; |
| 185 | } |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 186 | if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 187 | compressed_sl_print(ndo, &p[SLX_CHDR], ip, |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 188 | length, dir); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 189 | ND_PRINT((ndo, ": ")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 190 | } else |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 191 | ND_PRINT((ndo, "slip-%d!: ", p[SLX_CHDR])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | static const u_char * |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 196 | print_sl_change(netdissect_options *ndo, |
| 197 | const char *str, register const u_char *cp) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 198 | { |
| 199 | register u_int i; |
| 200 | |
| 201 | if ((i = *cp++) == 0) { |
| 202 | i = EXTRACT_16BITS(cp); |
| 203 | cp += 2; |
| 204 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 205 | ND_PRINT((ndo, " %s%d", str, i)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 206 | return (cp); |
| 207 | } |
| 208 | |
| 209 | static const u_char * |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 210 | print_sl_winchange(netdissect_options *ndo, |
| 211 | register const u_char *cp) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 212 | { |
| 213 | register short i; |
| 214 | |
| 215 | if ((i = *cp++) == 0) { |
| 216 | i = EXTRACT_16BITS(cp); |
| 217 | cp += 2; |
| 218 | } |
| 219 | if (i >= 0) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 220 | ND_PRINT((ndo, " W+%d", i)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 221 | else |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 222 | ND_PRINT((ndo, " W%d", i)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 223 | return (cp); |
| 224 | } |
| 225 | |
| 226 | static void |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 227 | compressed_sl_print(netdissect_options *ndo, |
| 228 | const u_char *chdr, const struct ip *ip, |
| 229 | u_int length, int dir) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 230 | { |
| 231 | register const u_char *cp = chdr; |
| 232 | register u_int flags, hlen; |
| 233 | |
| 234 | flags = *cp++; |
| 235 | if (flags & NEW_C) { |
| 236 | lastconn = *cp++; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 237 | ND_PRINT((ndo, "ctcp %d", lastconn)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 238 | } else |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 239 | ND_PRINT((ndo, "ctcp *")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 240 | |
| 241 | /* skip tcp checksum */ |
| 242 | cp += 2; |
| 243 | |
| 244 | switch (flags & SPECIALS_MASK) { |
| 245 | case SPECIAL_I: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 246 | ND_PRINT((ndo, " *SA+%d", lastlen[dir][lastconn])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 247 | break; |
| 248 | |
| 249 | case SPECIAL_D: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 250 | ND_PRINT((ndo, " *S+%d", lastlen[dir][lastconn])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 251 | break; |
| 252 | |
| 253 | default: |
| 254 | if (flags & NEW_U) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 255 | cp = print_sl_change(ndo, "U=", cp); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 256 | if (flags & NEW_W) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 257 | cp = print_sl_winchange(ndo, cp); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 258 | if (flags & NEW_A) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 259 | cp = print_sl_change(ndo, "A+", cp); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 260 | if (flags & NEW_S) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 261 | cp = print_sl_change(ndo, "S+", cp); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 262 | break; |
| 263 | } |
| 264 | if (flags & NEW_I) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 265 | cp = print_sl_change(ndo, "I+", cp); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * 'hlen' is the length of the uncompressed TCP/IP header (in words). |
| 269 | * 'cp - chdr' is the length of the compressed header. |
| 270 | * 'length - hlen' is the amount of data in the packet. |
| 271 | */ |
| 272 | hlen = IP_HL(ip); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 273 | hlen += TH_OFF((const struct tcphdr *)&((const int32_t *)ip)[hlen]); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 274 | lastlen[dir][lastconn] = length - (hlen << 2); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 275 | ND_PRINT((ndo, " %d (%ld)", lastlen[dir][lastconn], (long)(cp - chdr))); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 276 | } |