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 22 #include <sys/cdefs.h> 23 #ifndef lint 24 __RCSID("$NetBSD: print-sl.c,v 1.7 2017/02/05 04:05:05 spz Exp $"); 25 #endif 26 27 /* \summary: Compressed Serial Line Internet Protocol printer */ 28 29 #ifdef HAVE_CONFIG_H 30 #include "config.h" 31 #endif 32 33 #include <netdissect-stdinc.h> 34 35 #include "netdissect.h" 36 #include "extract.h" 37 38 #include "ip.h" 39 #include "tcp.h" 40 #include "slcompress.h" 41 42 /* 43 * definitions of the pseudo- link-level header attached to slip 44 * packets grabbed by the packet filter (bpf) traffic monitor. 45 */ 46 #define SLIP_HDRLEN 16 47 48 #define SLX_DIR 0 49 #define SLX_CHDR 1 50 #define CHDR_LEN 15 51 52 #define SLIPDIR_IN 0 53 #define SLIPDIR_OUT 1 54 55 static const char tstr[] = "[|slip]"; 56 57 static u_int lastlen[2][256]; 58 static u_int lastconn = 255; 59 60 static void sliplink_print(netdissect_options *, const u_char *, const struct ip *, u_int); 61 static void compressed_sl_print(netdissect_options *, const u_char *, const struct ip *, u_int, int); 62 63 u_int 64 sl_if_print(netdissect_options *ndo, 65 const struct pcap_pkthdr *h, const u_char *p) 66 { 67 register u_int caplen = h->caplen; 68 register u_int length = h->len; 69 register const struct ip *ip; 70 71 if (caplen < SLIP_HDRLEN || length < SLIP_HDRLEN) { 72 ND_PRINT((ndo, "%s", tstr)); 73 return (caplen); 74 } 75 76 caplen -= SLIP_HDRLEN; 77 length -= SLIP_HDRLEN; 78 79 ip = (const struct ip *)(p + SLIP_HDRLEN); 80 81 if (ndo->ndo_eflag) 82 sliplink_print(ndo, p, ip, length); 83 84 if (caplen < 1 || length < 1) { 85 ND_PRINT((ndo, "%s", tstr)); 86 return (caplen + SLIP_HDRLEN); 87 } 88 89 switch (IP_V(ip)) { 90 case 4: 91 ip_print(ndo, (const u_char *)ip, length); 92 break; 93 case 6: 94 ip6_print(ndo, (const u_char *)ip, length); 95 break; 96 default: 97 ND_PRINT((ndo, "ip v%d", IP_V(ip))); 98 } 99 100 return (SLIP_HDRLEN); 101 } 102 103 u_int 104 sl_bsdos_if_print(netdissect_options *ndo, 105 const struct pcap_pkthdr *h, const u_char *p) 106 { 107 register u_int caplen = h->caplen; 108 register u_int length = h->len; 109 register const struct ip *ip; 110 111 if (caplen < SLIP_HDRLEN) { 112 ND_PRINT((ndo, "%s", tstr)); 113 return (caplen); 114 } 115 116 length -= SLIP_HDRLEN; 117 118 ip = (const struct ip *)(p + SLIP_HDRLEN); 119 120 #ifdef notdef 121 if (ndo->ndo_eflag) 122 sliplink_print(ndo, p, ip, length); 123 #endif 124 125 ip_print(ndo, (const u_char *)ip, length); 126 127 return (SLIP_HDRLEN); 128 } 129 130 static void 131 sliplink_print(netdissect_options *ndo, 132 register const u_char *p, register const struct ip *ip, 133 register u_int length) 134 { 135 int dir; 136 u_int hlen; 137 138 dir = p[SLX_DIR]; 139 ND_PRINT((ndo, dir == SLIPDIR_IN ? "I " : "O ")); 140 141 if (ndo->ndo_nflag) { 142 /* XXX just dump the header */ 143 register int i; 144 145 for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i) 146 ND_PRINT((ndo, "%02x.", p[i])); 147 ND_PRINT((ndo, "%02x: ", p[SLX_CHDR + CHDR_LEN - 1])); 148 return; 149 } 150 switch (p[SLX_CHDR] & 0xf0) { 151 152 case TYPE_IP: 153 ND_PRINT((ndo, "ip %d: ", length + SLIP_HDRLEN)); 154 break; 155 156 case TYPE_UNCOMPRESSED_TCP: 157 /* 158 * The connection id is stored in the IP protocol field. 159 * Get it from the link layer since sl_uncompress_tcp() 160 * has restored the IP header copy to IPPROTO_TCP. 161 */ 162 lastconn = ((const struct ip *)&p[SLX_CHDR])->ip_p; 163 hlen = IP_HL(ip); 164 hlen += TH_OFF((const struct tcphdr *)&((const int *)ip)[hlen]); 165 lastlen[dir][lastconn] = length - (hlen << 2); 166 ND_PRINT((ndo, "utcp %d: ", lastconn)); 167 break; 168 169 default: 170 if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) { 171 compressed_sl_print(ndo, &p[SLX_CHDR], ip, 172 length, dir); 173 ND_PRINT((ndo, ": ")); 174 } else 175 ND_PRINT((ndo, "slip-%d!: ", p[SLX_CHDR])); 176 } 177 } 178 179 static const u_char * 180 print_sl_change(netdissect_options *ndo, 181 const char *str, register const u_char *cp) 182 { 183 register u_int i; 184 185 if ((i = *cp++) == 0) { 186 i = EXTRACT_16BITS(cp); 187 cp += 2; 188 } 189 ND_PRINT((ndo, " %s%d", str, i)); 190 return (cp); 191 } 192 193 static const u_char * 194 print_sl_winchange(netdissect_options *ndo, 195 register const u_char *cp) 196 { 197 register short i; 198 199 if ((i = *cp++) == 0) { 200 i = EXTRACT_16BITS(cp); 201 cp += 2; 202 } 203 if (i >= 0) 204 ND_PRINT((ndo, " W+%d", i)); 205 else 206 ND_PRINT((ndo, " W%d", i)); 207 return (cp); 208 } 209 210 static void 211 compressed_sl_print(netdissect_options *ndo, 212 const u_char *chdr, const struct ip *ip, 213 u_int length, int dir) 214 { 215 register const u_char *cp = chdr; 216 register u_int flags, hlen; 217 218 flags = *cp++; 219 if (flags & NEW_C) { 220 lastconn = *cp++; 221 ND_PRINT((ndo, "ctcp %d", lastconn)); 222 } else 223 ND_PRINT((ndo, "ctcp *")); 224 225 /* skip tcp checksum */ 226 cp += 2; 227 228 switch (flags & SPECIALS_MASK) { 229 case SPECIAL_I: 230 ND_PRINT((ndo, " *SA+%d", lastlen[dir][lastconn])); 231 break; 232 233 case SPECIAL_D: 234 ND_PRINT((ndo, " *S+%d", lastlen[dir][lastconn])); 235 break; 236 237 default: 238 if (flags & NEW_U) 239 cp = print_sl_change(ndo, "U=", cp); 240 if (flags & NEW_W) 241 cp = print_sl_winchange(ndo, cp); 242 if (flags & NEW_A) 243 cp = print_sl_change(ndo, "A+", cp); 244 if (flags & NEW_S) 245 cp = print_sl_change(ndo, "S+", cp); 246 break; 247 } 248 if (flags & NEW_I) 249 cp = print_sl_change(ndo, "I+", cp); 250 251 /* 252 * 'hlen' is the length of the uncompressed TCP/IP header (in words). 253 * 'cp - chdr' is the length of the compressed header. 254 * 'length - hlen' is the amount of data in the packet. 255 */ 256 hlen = IP_HL(ip); 257 hlen += TH_OFF((const struct tcphdr *)&((const int32_t *)ip)[hlen]); 258 lastlen[dir][lastconn] = length - (hlen << 2); 259 ND_PRINT((ndo, " %d (%ld)", lastlen[dir][lastconn], (long)(cp - chdr))); 260 } 261