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