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