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