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