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