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