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