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