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