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