1 /* $OpenBSD: print-sl.c,v 1.21 2018/10/22 16:12:45 kn 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 #include <sys/time.h> 25 #include <sys/file.h> 26 #include <sys/ioctl.h> 27 #include <sys/socket.h> 28 29 struct rtentry; 30 #include <net/if.h> 31 32 #include <netinet/in.h> 33 #include <netinet/ip.h> 34 #include <netinet/if_ether.h> 35 #include <netinet/udp.h> 36 #include <netinet/tcp.h> 37 38 #include <net/slcompress.h> 39 40 #include <ctype.h> 41 #include <netdb.h> 42 #include <pcap.h> 43 #include <stdio.h> 44 #include <limits.h> 45 46 #include "interface.h" 47 #include "addrtoname.h" 48 #include "extract.h" /* must come after interface.h */ 49 50 static u_int lastlen[2][256]; 51 static u_int lastconn = 255; 52 53 static void sliplink_print(const u_char *, const struct ip *, u_int); 54 static void compressed_sl_print(const u_char *, const struct ip *, u_int, int); 55 56 /* 57 * Definitions of the pseudo-link-level header attached to slip 58 * packets grabbed by the packet filter (bpf) traffic monitor. 59 */ 60 #define SLIP_HDRLEN 16 /* BPF SLIP header length */ 61 62 /* Offsets into BPF SLIP header. */ 63 #define SLX_DIR 0 /* direction; see below */ 64 #define SLX_CHDR 1 /* compressed header data */ 65 #define CHDR_LEN 15 /* length of compressed header data */ 66 67 #define SLIPDIR_IN 0 /* incoming */ 68 #define SLIPDIR_OUT 1 /* outgoing */ 69 70 /* XXX needs more hacking to work right */ 71 72 void 73 sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 74 { 75 u_int caplen = h->caplen; 76 u_int length = h->len; 77 const struct ip *ip; 78 79 ts_print(&h->ts); 80 81 if (caplen < SLIP_HDRLEN || length < SLIP_HDRLEN) { 82 printf("[|slip]"); 83 goto out; 84 } 85 /* 86 * Some printers want to get back at the link level addresses, 87 * and/or check that they're not walking off the end of the packet. 88 * Rather than pass them all the way down, we set these globals. 89 */ 90 packetp = p; 91 snapend = p + caplen; 92 93 length -= SLIP_HDRLEN; 94 95 ip = (struct ip *)(p + SLIP_HDRLEN); 96 97 if (eflag) 98 sliplink_print(p, ip, length); 99 100 switch (ip->ip_v) { 101 case 4: 102 ip_print((u_char *)ip, length); 103 break; 104 case 6: 105 ip6_print((u_char *)ip, length); 106 break; 107 default: 108 printf ("ip v%d", ip->ip_v); 109 } 110 111 if (xflag) 112 default_print((u_char *)ip, caplen - SLIP_HDRLEN); 113 out: 114 putchar('\n'); 115 } 116 117 118 void 119 sl_bsdos_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 120 { 121 u_int caplen = h->caplen; 122 u_int length = h->len; 123 const struct ip *ip; 124 125 ts_print(&h->ts); 126 127 if (caplen < SLIP_HDRLEN) { 128 printf("[|slip]"); 129 goto out; 130 } 131 /* 132 * Some printers want to get back at the link level addresses, 133 * and/or check that they're not walking off the end of the packet. 134 * Rather than pass them all the way down, we set these globals. 135 */ 136 packetp = p; 137 snapend = p + caplen; 138 139 length -= SLIP_HDRLEN; 140 141 ip = (struct ip *)(p + SLIP_HDRLEN); 142 143 #ifdef notdef 144 if (eflag) 145 sliplink_print(p, ip, length); 146 #endif 147 148 ip_print((u_char *)ip, length); 149 150 if (xflag) 151 default_print((u_char *)ip, caplen - SLIP_HDRLEN); 152 out: 153 putchar('\n'); 154 } 155 156 static void 157 sliplink_print(const u_char *p, const struct ip *ip, 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 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, const u_char *cp) 206 { 207 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(const u_char *cp) 219 { 220 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 const u_char *cp = chdr; 238 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