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