1 /* $OpenBSD: print-sl.c,v 1.11 2000/10/13 08:32:17 jakob Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 */ 23 24 #ifndef lint 25 static const char rcsid[] = 26 "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/print-sl.c,v 1.11 2000/10/13 08:32:17 jakob Exp $ (LBL)"; 27 #endif 28 29 #ifdef HAVE_NET_SLIP_H 30 #include <sys/param.h> 31 #include <sys/time.h> 32 #include <sys/timeb.h> 33 #include <sys/file.h> 34 #include <sys/ioctl.h> 35 #include <sys/mbuf.h> 36 #include <sys/socket.h> 37 38 #ifdef __STDC__ 39 struct rtentry; 40 #endif 41 #include <net/if.h> 42 43 #include <netinet/in.h> 44 #include <netinet/in_systm.h> 45 #include <netinet/ip.h> 46 #include <netinet/if_ether.h> 47 #include <netinet/udp.h> 48 #include <netinet/tcp.h> 49 50 #include <net/slcompress.h> 51 #include <net/slip.h> 52 53 #include <ctype.h> 54 #include <netdb.h> 55 #include <pcap.h> 56 #include <stdio.h> 57 58 #include "interface.h" 59 #include "addrtoname.h" 60 #include "extract.h" /* must come after interface.h */ 61 62 static u_int lastlen[2][256]; 63 static u_int lastconn = 255; 64 65 static void sliplink_print(const u_char *, const struct ip *, u_int); 66 static void compressed_sl_print(const u_char *, const struct ip *, u_int, int); 67 68 /* XXX BSD/OS 2.1 compatibility */ 69 #if !defined(SLIP_HDRLEN) && defined(SLC_BPFHDR) 70 #define SLIP_HDRLEN SLC_BPFHDR 71 #define SLX_DIR 0 72 #define SLX_CHDR (SLC_BPFHDRLEN - 1) 73 #define CHDR_LEN (SLC_BPFHDR - SLC_BPFHDRLEN) 74 #endif 75 76 /* XXX needs more hacking to work right */ 77 78 void 79 sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 80 { 81 register u_int caplen = h->caplen; 82 register u_int length = h->len; 83 register const struct ip *ip; 84 85 ts_print(&h->ts); 86 87 if (caplen < SLIP_HDRLEN) { 88 printf("[|slip]"); 89 goto out; 90 } 91 /* 92 * Some printers want to get back at the link level addresses, 93 * and/or check that they're not walking off the end of the packet. 94 * Rather than pass them all the way down, we set these globals. 95 */ 96 packetp = p; 97 snapend = p + caplen; 98 99 length -= SLIP_HDRLEN; 100 101 ip = (struct ip *)(p + SLIP_HDRLEN); 102 103 if (eflag) 104 sliplink_print(p, ip, length); 105 106 switch (ip->ip_v) { 107 case 4: 108 ip_print((u_char *)ip, length); 109 break; 110 #ifdef INET6 111 case 6: 112 ip6_print((u_char *)ip, length); 113 break; 114 #endif 115 default: 116 printf ("ip v%d", ip->ip_v); 117 } 118 119 if (xflag) 120 default_print((u_char *)ip, caplen - SLIP_HDRLEN); 121 out: 122 putchar('\n'); 123 } 124 125 126 void 127 sl_bsdos_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 128 { 129 register u_int caplen = h->caplen; 130 register u_int length = h->len; 131 register const struct ip *ip; 132 133 ts_print(&h->ts); 134 135 if (caplen < SLIP_HDRLEN) { 136 printf("[|slip]"); 137 goto out; 138 } 139 /* 140 * Some printers want to get back at the link level addresses, 141 * and/or check that they're not walking off the end of the packet. 142 * Rather than pass them all the way down, we set these globals. 143 */ 144 packetp = p; 145 snapend = p + caplen; 146 147 length -= SLIP_HDRLEN; 148 149 ip = (struct ip *)(p + SLIP_HDRLEN); 150 151 #ifdef notdef 152 if (eflag) 153 sliplink_print(p, ip, length); 154 #endif 155 156 ip_print((u_char *)ip, length); 157 158 if (xflag) 159 default_print((u_char *)ip, caplen - SLIP_HDRLEN); 160 out: 161 putchar('\n'); 162 } 163 164 static void 165 sliplink_print(register const u_char *p, register const struct ip *ip, 166 register u_int length) 167 { 168 int dir; 169 u_int hlen; 170 171 dir = p[SLX_DIR]; 172 putchar(dir == SLIPDIR_IN ? 'I' : 'O'); 173 putchar(' '); 174 175 if (nflag) { 176 /* XXX just dump the header */ 177 register int i; 178 179 for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i) 180 printf("%02x.", p[i]); 181 printf("%02x: ", p[SLX_CHDR + CHDR_LEN - 1]); 182 return; 183 } 184 switch (p[SLX_CHDR] & 0xf0) { 185 186 case TYPE_IP: 187 printf("ip %d: ", length + SLIP_HDRLEN); 188 break; 189 190 case TYPE_UNCOMPRESSED_TCP: 191 /* 192 * The connection id is stored in the IP protocol field. 193 * Get it from the link layer since sl_uncompress_tcp() 194 * has restored the IP header copy to IPPROTO_TCP. 195 */ 196 lastconn = ((struct ip *)&p[SLX_CHDR])->ip_p; 197 hlen = ip->ip_hl; 198 hlen += ((struct tcphdr *)&((int *)ip)[hlen])->th_off; 199 lastlen[dir][lastconn] = length - (hlen << 2); 200 printf("utcp %d: ", lastconn); 201 break; 202 203 default: 204 if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) { 205 compressed_sl_print(&p[SLX_CHDR], ip, 206 length, dir); 207 printf(": "); 208 } else 209 printf("slip-%d!: ", p[SLX_CHDR]); 210 } 211 } 212 213 static const u_char * 214 print_sl_change(const char *str, register const u_char *cp) 215 { 216 register u_int i; 217 218 if ((i = *cp++) == 0) { 219 i = EXTRACT_16BITS(cp); 220 cp += 2; 221 } 222 printf(" %s%d", str, i); 223 return (cp); 224 } 225 226 static const u_char * 227 print_sl_winchange(register const u_char *cp) 228 { 229 register short i; 230 231 if ((i = *cp++) == 0) { 232 i = EXTRACT_16BITS(cp); 233 cp += 2; 234 } 235 if (i >= 0) 236 printf(" W+%d", i); 237 else 238 printf(" W%d", i); 239 return (cp); 240 } 241 242 static void 243 compressed_sl_print(const u_char *chdr, const struct ip *ip, 244 u_int length, int dir) 245 { 246 register const u_char *cp = chdr; 247 register u_int flags, hlen; 248 249 flags = *cp++; 250 if (flags & NEW_C) { 251 lastconn = *cp++; 252 printf("ctcp %d", lastconn); 253 } else 254 printf("ctcp *"); 255 256 /* skip tcp checksum */ 257 cp += 2; 258 259 switch (flags & SPECIALS_MASK) { 260 case SPECIAL_I: 261 printf(" *SA+%d", lastlen[dir][lastconn]); 262 break; 263 264 case SPECIAL_D: 265 printf(" *S+%d", lastlen[dir][lastconn]); 266 break; 267 268 default: 269 if (flags & NEW_U) 270 cp = print_sl_change("U=", cp); 271 if (flags & NEW_W) 272 cp = print_sl_winchange(cp); 273 if (flags & NEW_A) 274 cp = print_sl_change("A+", cp); 275 if (flags & NEW_S) 276 cp = print_sl_change("S+", cp); 277 break; 278 } 279 if (flags & NEW_I) 280 cp = print_sl_change("I+", cp); 281 282 /* 283 * 'hlen' is the length of the uncompressed TCP/IP header (in words). 284 * 'cp - chdr' is the length of the compressed header. 285 * 'length - hlen' is the amount of data in the packet. 286 */ 287 hlen = ip->ip_hl; 288 hlen += ((struct tcphdr *)&((int32_t *)ip)[hlen])->th_off; 289 lastlen[dir][lastconn] = length - (hlen << 2); 290 printf(" %d (%d)", lastlen[dir][lastconn], (int)(cp - chdr)); 291 } 292 #else 293 #include <sys/types.h> 294 #include <sys/time.h> 295 296 #include <pcap.h> 297 #include <stdio.h> 298 299 #include "interface.h" 300 301 void 302 sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 303 { 304 305 error("not configured for slip"); 306 /* NOTREACHED */ 307 } 308 309 void 310 sl_bsdos_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 311 { 312 313 error("not configured for slip"); 314 /* NOTREACHED */ 315 } 316 #endif 317