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