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