1 /* $OpenBSD: print-sl.c,v 1.17 2014/11/20 04:01:01 jsg 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 #ifdef HAVE_NET_SLIP_H 25 #include <sys/param.h> 26 #include <sys/time.h> 27 #include <sys/file.h> 28 #include <sys/ioctl.h> 29 #include <sys/mbuf.h> 30 #include <sys/socket.h> 31 32 struct rtentry; 33 #include <net/if.h> 34 35 #include <netinet/in.h> 36 #include <netinet/ip.h> 37 #include <netinet/if_ether.h> 38 #include <netinet/udp.h> 39 #include <netinet/tcp.h> 40 41 #include <net/slcompress.h> 42 #include <net/slip.h> 43 44 #include <ctype.h> 45 #include <netdb.h> 46 #include <pcap.h> 47 #include <stdio.h> 48 49 #include "interface.h" 50 #include "addrtoname.h" 51 #include "extract.h" /* must come after interface.h */ 52 53 static u_int lastlen[2][256]; 54 static u_int lastconn = 255; 55 56 static void sliplink_print(const u_char *, const struct ip *, u_int); 57 static void compressed_sl_print(const u_char *, const struct ip *, u_int, int); 58 59 /* XXX BSD/OS 2.1 compatibility */ 60 #if !defined(SLIP_HDRLEN) && defined(SLC_BPFHDR) 61 #define SLIP_HDRLEN SLC_BPFHDR 62 #define SLX_DIR 0 63 #define SLX_CHDR (SLC_BPFHDRLEN - 1) 64 #define CHDR_LEN (SLC_BPFHDR - SLC_BPFHDRLEN) 65 #endif 66 67 /* XXX needs more hacking to work right */ 68 69 void 70 sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 71 { 72 register u_int caplen = h->caplen; 73 register u_int length = h->len; 74 register const struct ip *ip; 75 76 ts_print(&h->ts); 77 78 if (caplen < SLIP_HDRLEN || length < SLIP_HDRLEN) { 79 printf("[|slip]"); 80 goto out; 81 } 82 /* 83 * Some printers want to get back at the link level addresses, 84 * and/or check that they're not walking off the end of the packet. 85 * Rather than pass them all the way down, we set these globals. 86 */ 87 packetp = p; 88 snapend = p + caplen; 89 90 length -= SLIP_HDRLEN; 91 92 ip = (struct ip *)(p + SLIP_HDRLEN); 93 94 if (eflag) 95 sliplink_print(p, ip, length); 96 97 switch (ip->ip_v) { 98 case 4: 99 ip_print((u_char *)ip, length); 100 break; 101 #ifdef INET6 102 case 6: 103 ip6_print((u_char *)ip, length); 104 break; 105 #endif 106 default: 107 printf ("ip v%d", ip->ip_v); 108 } 109 110 if (xflag) 111 default_print((u_char *)ip, caplen - SLIP_HDRLEN); 112 out: 113 putchar('\n'); 114 } 115 116 117 void 118 sl_bsdos_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 119 { 120 register u_int caplen = h->caplen; 121 register u_int length = h->len; 122 register const struct ip *ip; 123 124 ts_print(&h->ts); 125 126 if (caplen < SLIP_HDRLEN) { 127 printf("[|slip]"); 128 goto out; 129 } 130 /* 131 * Some printers want to get back at the link level addresses, 132 * and/or check that they're not walking off the end of the packet. 133 * Rather than pass them all the way down, we set these globals. 134 */ 135 packetp = p; 136 snapend = p + caplen; 137 138 length -= SLIP_HDRLEN; 139 140 ip = (struct ip *)(p + SLIP_HDRLEN); 141 142 #ifdef notdef 143 if (eflag) 144 sliplink_print(p, ip, length); 145 #endif 146 147 ip_print((u_char *)ip, length); 148 149 if (xflag) 150 default_print((u_char *)ip, caplen - SLIP_HDRLEN); 151 out: 152 putchar('\n'); 153 } 154 155 static void 156 sliplink_print(register const u_char *p, register const struct ip *ip, 157 register u_int length) 158 { 159 int dir; 160 u_int hlen; 161 162 dir = p[SLX_DIR]; 163 putchar(dir == SLIPDIR_IN ? 'I' : 'O'); 164 putchar(' '); 165 166 if (nflag) { 167 /* XXX just dump the header */ 168 register int i; 169 170 for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i) 171 printf("%02x.", p[i]); 172 printf("%02x: ", p[SLX_CHDR + CHDR_LEN - 1]); 173 return; 174 } 175 switch (p[SLX_CHDR] & 0xf0) { 176 177 case TYPE_IP: 178 printf("ip %d: ", length + SLIP_HDRLEN); 179 break; 180 181 case TYPE_UNCOMPRESSED_TCP: 182 /* 183 * The connection id is stored in the IP protocol field. 184 * Get it from the link layer since sl_uncompress_tcp() 185 * has restored the IP header copy to IPPROTO_TCP. 186 */ 187 lastconn = ((struct ip *)&p[SLX_CHDR])->ip_p; 188 hlen = ip->ip_hl; 189 hlen += ((struct tcphdr *)&((int *)ip)[hlen])->th_off; 190 lastlen[dir][lastconn] = length - (hlen << 2); 191 printf("utcp %d: ", lastconn); 192 break; 193 194 default: 195 if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) { 196 compressed_sl_print(&p[SLX_CHDR], ip, 197 length, dir); 198 printf(": "); 199 } else 200 printf("slip-%d!: ", p[SLX_CHDR]); 201 } 202 } 203 204 static const u_char * 205 print_sl_change(const char *str, register const u_char *cp) 206 { 207 register u_int i; 208 209 if ((i = *cp++) == 0) { 210 i = EXTRACT_16BITS(cp); 211 cp += 2; 212 } 213 printf(" %s%d", str, i); 214 return (cp); 215 } 216 217 static const u_char * 218 print_sl_winchange(register const u_char *cp) 219 { 220 register short i; 221 222 if ((i = *cp++) == 0) { 223 i = EXTRACT_16BITS(cp); 224 cp += 2; 225 } 226 if (i >= 0) 227 printf(" W+%d", i); 228 else 229 printf(" W%d", i); 230 return (cp); 231 } 232 233 static void 234 compressed_sl_print(const u_char *chdr, const struct ip *ip, 235 u_int length, int dir) 236 { 237 register const u_char *cp = chdr; 238 register u_int flags, hlen; 239 240 flags = *cp++; 241 if (flags & NEW_C) { 242 lastconn = *cp++; 243 printf("ctcp %d", lastconn); 244 } else 245 printf("ctcp *"); 246 247 /* skip tcp checksum */ 248 cp += 2; 249 250 switch (flags & SPECIALS_MASK) { 251 case SPECIAL_I: 252 printf(" *SA+%d", lastlen[dir][lastconn]); 253 break; 254 255 case SPECIAL_D: 256 printf(" *S+%d", lastlen[dir][lastconn]); 257 break; 258 259 default: 260 if (flags & NEW_U) 261 cp = print_sl_change("U=", cp); 262 if (flags & NEW_W) 263 cp = print_sl_winchange(cp); 264 if (flags & NEW_A) 265 cp = print_sl_change("A+", cp); 266 if (flags & NEW_S) 267 cp = print_sl_change("S+", cp); 268 break; 269 } 270 if (flags & NEW_I) 271 cp = print_sl_change("I+", cp); 272 273 /* 274 * 'hlen' is the length of the uncompressed TCP/IP header (in words). 275 * 'cp - chdr' is the length of the compressed header. 276 * 'length - hlen' is the amount of data in the packet. 277 */ 278 hlen = ip->ip_hl; 279 hlen += ((struct tcphdr *)&((int32_t *)ip)[hlen])->th_off; 280 lastlen[dir][lastconn] = length - (hlen << 2); 281 printf(" %d (%d)", lastlen[dir][lastconn], (int)(cp - chdr)); 282 } 283 #else 284 #include <sys/types.h> 285 #include <sys/time.h> 286 287 #include <pcap.h> 288 #include <stdio.h> 289 290 #include "interface.h" 291 292 void 293 sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 294 { 295 296 error("not configured for slip"); 297 /* NOTREACHED */ 298 } 299 300 void 301 sl_bsdos_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 302 { 303 304 error("not configured for slip"); 305 /* NOTREACHED */ 306 } 307 #endif 308