xref: /freebsd/contrib/tcpdump/print-sl.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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[] _U_ =
26     "@(#) $Header: /tcpdump/master/tcpdump/print-sl.c,v 1.65 2005/04/06 21:32:42 mcr Exp $ (LBL)";
27 #endif
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <tcpdump-stdinc.h>
34 
35 #include <pcap.h>
36 #include <stdio.h>
37 
38 #include "interface.h"
39 #include "addrtoname.h"
40 #include "extract.h"			/* must come after interface.h */
41 
42 #include "ip.h"
43 #include "tcp.h"
44 #include "slip.h"
45 #include "slcompress.h"
46 
47 static u_int lastlen[2][256];
48 static u_int lastconn = 255;
49 
50 static void sliplink_print(const u_char *, const struct ip *, u_int);
51 static void compressed_sl_print(const u_char *, const struct ip *, u_int, int);
52 
53 u_int
54 sl_if_print(const struct pcap_pkthdr *h, const u_char *p)
55 {
56 	register u_int caplen = h->caplen;
57 	register u_int length = h->len;
58 	register const struct ip *ip;
59 
60 	if (caplen < SLIP_HDRLEN) {
61 		printf("[|slip]");
62 		return (caplen);
63 	}
64 
65 	length -= SLIP_HDRLEN;
66 
67 	ip = (struct ip *)(p + SLIP_HDRLEN);
68 
69 	if (eflag)
70 		sliplink_print(p, ip, length);
71 
72 	switch (IP_V(ip)) {
73 	case 4:
74 	        ip_print(gndo, (u_char *)ip, length);
75 		break;
76 #ifdef INET6
77 	case 6:
78 		ip6_print((u_char *)ip, length);
79 		break;
80 #endif
81 	default:
82 		printf ("ip v%d", IP_V(ip));
83 	}
84 
85 	return (SLIP_HDRLEN);
86 }
87 
88 u_int
89 sl_bsdos_if_print(const struct pcap_pkthdr *h, const u_char *p)
90 {
91 	register u_int caplen = h->caplen;
92 	register u_int length = h->len;
93 	register const struct ip *ip;
94 
95 	if (caplen < SLIP_HDRLEN) {
96 		printf("[|slip]");
97 		return (caplen);
98 	}
99 
100 	length -= SLIP_HDRLEN;
101 
102 	ip = (struct ip *)(p + SLIP_HDRLEN);
103 
104 #ifdef notdef
105 	if (eflag)
106 		sliplink_print(p, ip, length);
107 #endif
108 
109 	ip_print(gndo, (u_char *)ip, length);
110 
111 	return (SLIP_HDRLEN);
112 }
113 
114 static void
115 sliplink_print(register const u_char *p, register const struct ip *ip,
116 	       register u_int length)
117 {
118 	int dir;
119 	u_int hlen;
120 
121 	dir = p[SLX_DIR];
122 	putchar(dir == SLIPDIR_IN ? 'I' : 'O');
123 	putchar(' ');
124 
125 	if (nflag) {
126 		/* XXX just dump the header */
127 		register int i;
128 
129 		for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i)
130 			printf("%02x.", p[i]);
131 		printf("%02x: ", p[SLX_CHDR + CHDR_LEN - 1]);
132 		return;
133 	}
134 	switch (p[SLX_CHDR] & 0xf0) {
135 
136 	case TYPE_IP:
137 		printf("ip %d: ", length + SLIP_HDRLEN);
138 		break;
139 
140 	case TYPE_UNCOMPRESSED_TCP:
141 		/*
142 		 * The connection id is stored in the IP protocol field.
143 		 * Get it from the link layer since sl_uncompress_tcp()
144 		 * has restored the IP header copy to IPPROTO_TCP.
145 		 */
146 		lastconn = ((struct ip *)&p[SLX_CHDR])->ip_p;
147 		hlen = IP_HL(ip);
148 		hlen += TH_OFF((struct tcphdr *)&((int *)ip)[hlen]);
149 		lastlen[dir][lastconn] = length - (hlen << 2);
150 		printf("utcp %d: ", lastconn);
151 		break;
152 
153 	default:
154 		if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
155 			compressed_sl_print(&p[SLX_CHDR], ip,
156 			    length, dir);
157 			printf(": ");
158 		} else
159 			printf("slip-%d!: ", p[SLX_CHDR]);
160 	}
161 }
162 
163 static const u_char *
164 print_sl_change(const char *str, register const u_char *cp)
165 {
166 	register u_int i;
167 
168 	if ((i = *cp++) == 0) {
169 		i = EXTRACT_16BITS(cp);
170 		cp += 2;
171 	}
172 	printf(" %s%d", str, i);
173 	return (cp);
174 }
175 
176 static const u_char *
177 print_sl_winchange(register const u_char *cp)
178 {
179 	register short i;
180 
181 	if ((i = *cp++) == 0) {
182 		i = EXTRACT_16BITS(cp);
183 		cp += 2;
184 	}
185 	if (i >= 0)
186 		printf(" W+%d", i);
187 	else
188 		printf(" W%d", i);
189 	return (cp);
190 }
191 
192 static void
193 compressed_sl_print(const u_char *chdr, const struct ip *ip,
194 		    u_int length, int dir)
195 {
196 	register const u_char *cp = chdr;
197 	register u_int flags, hlen;
198 
199 	flags = *cp++;
200 	if (flags & NEW_C) {
201 		lastconn = *cp++;
202 		printf("ctcp %d", lastconn);
203 	} else
204 		printf("ctcp *");
205 
206 	/* skip tcp checksum */
207 	cp += 2;
208 
209 	switch (flags & SPECIALS_MASK) {
210 	case SPECIAL_I:
211 		printf(" *SA+%d", lastlen[dir][lastconn]);
212 		break;
213 
214 	case SPECIAL_D:
215 		printf(" *S+%d", lastlen[dir][lastconn]);
216 		break;
217 
218 	default:
219 		if (flags & NEW_U)
220 			cp = print_sl_change("U=", cp);
221 		if (flags & NEW_W)
222 			cp = print_sl_winchange(cp);
223 		if (flags & NEW_A)
224 			cp = print_sl_change("A+", cp);
225 		if (flags & NEW_S)
226 			cp = print_sl_change("S+", cp);
227 		break;
228 	}
229 	if (flags & NEW_I)
230 		cp = print_sl_change("I+", cp);
231 
232 	/*
233 	 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
234 	 * 'cp - chdr' is the length of the compressed header.
235 	 * 'length - hlen' is the amount of data in the packet.
236 	 */
237 	hlen = IP_HL(ip);
238 	hlen += TH_OFF((struct tcphdr *)&((int32_t *)ip)[hlen]);
239 	lastlen[dir][lastconn] = length - (hlen << 2);
240 	printf(" %d (%ld)", lastlen[dir][lastconn], (long)(cp - chdr));
241 }
242