]> The Tcpdump Group git mirrors - tcpdump/blob - print-sl.c
Document the "any" interface, and note that captures on it won't be done
[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.56 2000-10-10 05:06:10 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33
34 #include <netinet/in.h>
35
36 #include <ctype.h>
37 #include <netdb.h>
38 #include <pcap.h>
39 #include <stdio.h>
40
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h" /* must come after interface.h */
44
45 #include "ip.h"
46 #include "tcp.h"
47 #include "slip.h"
48 #include "slcompress.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 void
57 sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
58 {
59 register u_int caplen = h->caplen;
60 register u_int length = h->len;
61 register const struct ip *ip;
62
63 ts_print(&h->ts);
64
65 if (caplen < SLIP_HDRLEN) {
66 printf("[|slip]");
67 goto out;
68 }
69 /*
70 * Some printers want to get back at the link level addresses,
71 * and/or check that they're not walking off the end of the packet.
72 * Rather than pass them all the way down, we set these globals.
73 */
74 packetp = p;
75 snapend = p + caplen;
76
77 length -= SLIP_HDRLEN;
78
79 ip = (struct ip *)(p + SLIP_HDRLEN);
80
81 if (eflag)
82 sliplink_print(p, ip, length);
83
84 switch (IP_V(ip)) {
85 case 4:
86 ip_print((u_char *)ip, length);
87 break;
88 #ifdef INET6
89 case 6:
90 ip6_print((u_char *)ip, length);
91 break;
92 #endif
93 default:
94 printf ("ip v%d", IP_V(ip));
95 }
96
97 if (xflag)
98 default_print((u_char *)ip, caplen - SLIP_HDRLEN);
99 out:
100 putchar('\n');
101 }
102
103
104 void
105 sl_bsdos_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
106 {
107 register u_int caplen = h->caplen;
108 register u_int length = h->len;
109 register const struct ip *ip;
110
111 ts_print(&h->ts);
112
113 if (caplen < SLIP_HDRLEN) {
114 printf("[|slip]");
115 goto out;
116 }
117 /*
118 * Some printers want to get back at the link level addresses,
119 * and/or check that they're not walking off the end of the packet.
120 * Rather than pass them all the way down, we set these globals.
121 */
122 packetp = p;
123 snapend = p + caplen;
124
125 length -= SLIP_HDRLEN;
126
127 ip = (struct ip *)(p + SLIP_HDRLEN);
128
129 #ifdef notdef
130 if (eflag)
131 sliplink_print(p, ip, length);
132 #endif
133
134 ip_print((u_char *)ip, length);
135
136 if (xflag)
137 default_print((u_char *)ip, caplen - SLIP_HDRLEN);
138 out:
139 putchar('\n');
140 }
141
142 static void
143 sliplink_print(register const u_char *p, register const struct ip *ip,
144 register u_int length)
145 {
146 int dir;
147 u_int hlen;
148
149 dir = p[SLX_DIR];
150 putchar(dir == SLIPDIR_IN ? 'I' : 'O');
151 putchar(' ');
152
153 if (nflag) {
154 /* XXX just dump the header */
155 register int i;
156
157 for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i)
158 printf("%02x.", p[i]);
159 printf("%02x: ", p[SLX_CHDR + CHDR_LEN - 1]);
160 return;
161 }
162 switch (p[SLX_CHDR] & 0xf0) {
163
164 case TYPE_IP:
165 printf("ip %d: ", length + SLIP_HDRLEN);
166 break;
167
168 case TYPE_UNCOMPRESSED_TCP:
169 /*
170 * The connection id is stored in the IP protocol field.
171 * Get it from the link layer since sl_uncompress_tcp()
172 * has restored the IP header copy to IPPROTO_TCP.
173 */
174 lastconn = ((struct ip *)&p[SLX_CHDR])->ip_p;
175 hlen = IP_HL(ip);
176 hlen += TH_OFF((struct tcphdr *)&((int *)ip)[hlen]);
177 lastlen[dir][lastconn] = length - (hlen << 2);
178 printf("utcp %d: ", lastconn);
179 break;
180
181 default:
182 if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
183 compressed_sl_print(&p[SLX_CHDR], ip,
184 length, dir);
185 printf(": ");
186 } else
187 printf("slip-%d!: ", p[SLX_CHDR]);
188 }
189 }
190
191 static const u_char *
192 print_sl_change(const char *str, register const u_char *cp)
193 {
194 register u_int i;
195
196 if ((i = *cp++) == 0) {
197 i = EXTRACT_16BITS(cp);
198 cp += 2;
199 }
200 printf(" %s%d", str, i);
201 return (cp);
202 }
203
204 static const u_char *
205 print_sl_winchange(register const u_char *cp)
206 {
207 register short i;
208
209 if ((i = *cp++) == 0) {
210 i = EXTRACT_16BITS(cp);
211 cp += 2;
212 }
213 if (i >= 0)
214 printf(" W+%d", i);
215 else
216 printf(" W%d", i);
217 return (cp);
218 }
219
220 static void
221 compressed_sl_print(const u_char *chdr, const struct ip *ip,
222 u_int length, int dir)
223 {
224 register const u_char *cp = chdr;
225 register u_int flags, hlen;
226
227 flags = *cp++;
228 if (flags & NEW_C) {
229 lastconn = *cp++;
230 printf("ctcp %d", lastconn);
231 } else
232 printf("ctcp *");
233
234 /* skip tcp checksum */
235 cp += 2;
236
237 switch (flags & SPECIALS_MASK) {
238 case SPECIAL_I:
239 printf(" *SA+%d", lastlen[dir][lastconn]);
240 break;
241
242 case SPECIAL_D:
243 printf(" *S+%d", lastlen[dir][lastconn]);
244 break;
245
246 default:
247 if (flags & NEW_U)
248 cp = print_sl_change("U=", cp);
249 if (flags & NEW_W)
250 cp = print_sl_winchange(cp);
251 if (flags & NEW_A)
252 cp = print_sl_change("A+", cp);
253 if (flags & NEW_S)
254 cp = print_sl_change("S+", cp);
255 break;
256 }
257 if (flags & NEW_I)
258 cp = print_sl_change("I+", cp);
259
260 /*
261 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
262 * 'cp - chdr' is the length of the compressed header.
263 * 'length - hlen' is the amount of data in the packet.
264 */
265 hlen = IP_HL(ip);
266 hlen += TH_OFF((struct tcphdr *)&((int32_t *)ip)[hlen]);
267 lastlen[dir][lastconn] = length - (hlen << 2);
268 printf(" %d (%ld)", lastlen[dir][lastconn], (long)(cp - chdr));
269 }