]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6.c
Add and use tstr[]
[tcpdump] / print-ip6.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
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: IPv6 printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include <string.h>
31
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "extract.h"
35
36 #include "ip6.h"
37 #include "ipproto.h"
38
39 /*
40 * If routing headers are presend and valid, set dst to the final destination.
41 * Otherwise, set it to the IPv6 destination.
42 *
43 * This is used for UDP and TCP pseudo-header in the checksum
44 * calculation.
45 */
46 static void
47 ip6_finddst(netdissect_options *ndo, struct in6_addr *dst,
48 const struct ip6_hdr *ip6)
49 {
50 const u_char *cp;
51 u_int advance;
52 u_int nh;
53 const void *dst_addr;
54 const struct ip6_rthdr *dp;
55 const struct ip6_rthdr0 *dp0;
56 const u_char *p;
57 int i, len;
58
59 cp = (const u_char *)ip6;
60 advance = sizeof(struct ip6_hdr);
61 nh = EXTRACT_U_1(ip6->ip6_nxt);
62 dst_addr = (const void *)ip6->ip6_dst;
63
64 while (cp < ndo->ndo_snapend) {
65 cp += advance;
66
67 switch (nh) {
68
69 case IPPROTO_HOPOPTS:
70 case IPPROTO_DSTOPTS:
71 case IPPROTO_MOBILITY_OLD:
72 case IPPROTO_MOBILITY:
73 /*
74 * These have a header length byte, following
75 * the next header byte, giving the length of
76 * the header, in units of 8 octets, excluding
77 * the first 8 octets.
78 */
79 ND_TCHECK_2(cp);
80 advance = (EXTRACT_U_1(cp + 1) + 1) << 3;
81 nh = EXTRACT_U_1(cp);
82 break;
83
84 case IPPROTO_FRAGMENT:
85 /*
86 * The byte following the next header byte is
87 * marked as reserved, and the header is always
88 * the same size.
89 */
90 ND_TCHECK_1(cp);
91 advance = sizeof(struct ip6_frag);
92 nh = EXTRACT_U_1(cp);
93 break;
94
95 case IPPROTO_ROUTING:
96 /*
97 * OK, we found it.
98 */
99 dp = (const struct ip6_rthdr *)cp;
100 ND_TCHECK_SIZE(dp);
101 len = EXTRACT_U_1(dp->ip6r_len);
102 switch (EXTRACT_U_1(dp->ip6r_type)) {
103
104 case IPV6_RTHDR_TYPE_0:
105 case IPV6_RTHDR_TYPE_2: /* Mobile IPv6 ID-20 */
106 dp0 = (const struct ip6_rthdr0 *)dp;
107 if (len % 2 == 1)
108 goto trunc;
109 len >>= 1;
110 p = (const u_char *) dp0->ip6r0_addr;
111 for (i = 0; i < len; i++) {
112 ND_TCHECK_16(p);
113 dst_addr = (const void *)p;
114 p += 16;
115 }
116 break;
117
118 default:
119 break;
120 }
121
122 /*
123 * Only one routing header to a customer.
124 */
125 goto done;
126
127 case IPPROTO_AH:
128 case IPPROTO_ESP:
129 case IPPROTO_IPCOMP:
130 default:
131 /*
132 * AH and ESP are, in the RFCs that describe them,
133 * described as being "viewed as an end-to-end
134 * payload" "in the IPv6 context, so that they
135 * "should appear after hop-by-hop, routing, and
136 * fragmentation extension headers". We assume
137 * that's the case, and stop as soon as we see
138 * one. (We can't handle an ESP header in
139 * the general case anyway, as its length depends
140 * on the encryption algorithm.)
141 *
142 * IPComp is also "viewed as an end-to-end
143 * payload" "in the IPv6 context".
144 *
145 * All other protocols are assumed to be the final
146 * protocol.
147 */
148 goto done;
149 }
150 }
151
152 done:
153 trunc:
154 UNALIGNED_MEMCPY(dst, dst_addr, sizeof(nd_ipv6));
155 }
156
157 /*
158 * Compute a V6-style checksum by building a pseudoheader.
159 */
160 int
161 nextproto6_cksum(netdissect_options *ndo,
162 const struct ip6_hdr *ip6, const uint8_t *data,
163 u_int len, u_int covlen, u_int next_proto)
164 {
165 struct {
166 struct in6_addr ph_src;
167 struct in6_addr ph_dst;
168 uint32_t ph_len;
169 uint8_t ph_zero[3];
170 uint8_t ph_nxt;
171 } ph;
172 struct cksum_vec vec[2];
173 u_int nh;
174
175 /* pseudo-header */
176 memset(&ph, 0, sizeof(ph));
177 UNALIGNED_MEMCPY(&ph.ph_src, ip6->ip6_src, sizeof (struct in6_addr));
178 nh = EXTRACT_U_1(ip6->ip6_nxt);
179 switch (nh) {
180
181 case IPPROTO_HOPOPTS:
182 case IPPROTO_DSTOPTS:
183 case IPPROTO_MOBILITY_OLD:
184 case IPPROTO_MOBILITY:
185 case IPPROTO_FRAGMENT:
186 case IPPROTO_ROUTING:
187 /*
188 * The next header is either a routing header or a header
189 * after which there might be a routing header, so scan
190 * for a routing header.
191 */
192 ip6_finddst(ndo, &ph.ph_dst, ip6);
193 break;
194
195 default:
196 UNALIGNED_MEMCPY(&ph.ph_dst, ip6->ip6_dst,
197 sizeof (struct in6_addr));
198 break;
199 }
200 ph.ph_len = htonl(len);
201 ph.ph_nxt = next_proto;
202
203 vec[0].ptr = (const uint8_t *)(void *)&ph;
204 vec[0].len = sizeof(ph);
205 vec[1].ptr = data;
206 vec[1].len = covlen;
207
208 return in_cksum(vec, 2);
209 }
210
211 /*
212 * print an IP6 datagram.
213 */
214 void
215 ip6_print(netdissect_options *ndo, const u_char *bp, u_int length)
216 {
217 const struct ip6_hdr *ip6;
218 int advance;
219 u_int len;
220 const u_char *ipend;
221 const u_char *cp;
222 u_int payload_len;
223 u_int nh;
224 int fragmented = 0;
225 u_int flow;
226
227 ip6 = (const struct ip6_hdr *)bp;
228
229 ND_TCHECK_SIZE(ip6);
230 if (length < sizeof (struct ip6_hdr)) {
231 ND_PRINT("truncated-ip6 %u", length);
232 return;
233 }
234
235 if (!ndo->ndo_eflag)
236 ND_PRINT("IP6 ");
237
238 if (IP6_VERSION(ip6) != 6) {
239 ND_PRINT("version error: %u != 6", IP6_VERSION(ip6));
240 return;
241 }
242
243 payload_len = EXTRACT_BE_U_2(ip6->ip6_plen);
244 len = payload_len + sizeof(struct ip6_hdr);
245 if (length < len)
246 ND_PRINT("truncated-ip6 - %u bytes missing!",
247 len - length);
248
249 nh = EXTRACT_U_1(ip6->ip6_nxt);
250 if (ndo->ndo_vflag) {
251 flow = EXTRACT_BE_U_4(ip6->ip6_flow);
252 ND_PRINT("(");
253 #if 0
254 /* rfc1883 */
255 if (flow & 0x0f000000)
256 ND_PRINT("pri 0x%02x, ", (flow & 0x0f000000) >> 24);
257 if (flow & 0x00ffffff)
258 ND_PRINT("flowlabel 0x%06x, ", flow & 0x00ffffff);
259 #else
260 /* RFC 2460 */
261 if (flow & 0x0ff00000)
262 ND_PRINT("class 0x%02x, ", (flow & 0x0ff00000) >> 20);
263 if (flow & 0x000fffff)
264 ND_PRINT("flowlabel 0x%05x, ", flow & 0x000fffff);
265 #endif
266
267 ND_PRINT("hlim %u, next-header %s (%u) payload length: %u) ",
268 EXTRACT_U_1(ip6->ip6_hlim),
269 tok2str(ipproto_values,"unknown",nh),
270 nh,
271 payload_len);
272 }
273
274 /*
275 * Cut off the snapshot length to the end of the IP payload.
276 */
277 ipend = bp + len;
278 if (ipend < ndo->ndo_snapend)
279 ndo->ndo_snapend = ipend;
280
281 cp = (const u_char *)ip6;
282 advance = sizeof(struct ip6_hdr);
283 while (cp < ndo->ndo_snapend && advance > 0) {
284 if (len < (u_int)advance)
285 goto trunc;
286 cp += advance;
287 len -= advance;
288
289 if (cp == (const u_char *)(ip6 + 1) &&
290 nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
291 nh != IPPROTO_DCCP && nh != IPPROTO_SCTP) {
292 ND_PRINT("%s > %s: ", ip6addr_string(ndo, ip6->ip6_src),
293 ip6addr_string(ndo, ip6->ip6_dst));
294 }
295
296 switch (nh) {
297 case IPPROTO_HOPOPTS:
298 advance = hbhopt_print(ndo, cp);
299 if (advance < 0)
300 return;
301 nh = EXTRACT_U_1(cp);
302 break;
303 case IPPROTO_DSTOPTS:
304 advance = dstopt_print(ndo, cp);
305 if (advance < 0)
306 return;
307 nh = EXTRACT_U_1(cp);
308 break;
309 case IPPROTO_FRAGMENT:
310 advance = frag6_print(ndo, cp, (const u_char *)ip6);
311 if (advance < 0 || ndo->ndo_snapend <= cp + advance)
312 return;
313 nh = EXTRACT_U_1(cp);
314 fragmented = 1;
315 break;
316
317 case IPPROTO_MOBILITY_OLD:
318 case IPPROTO_MOBILITY:
319 /*
320 * XXX - we don't use "advance"; RFC 3775 says that
321 * the next header field in a mobility header
322 * should be IPPROTO_NONE, but speaks of
323 * the possiblity of a future extension in
324 * which payload can be piggybacked atop a
325 * mobility header.
326 */
327 advance = mobility_print(ndo, cp, (const u_char *)ip6);
328 if (advance < 0)
329 return;
330 nh = EXTRACT_U_1(cp);
331 return;
332 case IPPROTO_ROUTING:
333 ND_TCHECK_1(cp);
334 advance = rt6_print(ndo, cp, (const u_char *)ip6);
335 if (advance < 0)
336 return;
337 nh = EXTRACT_U_1(cp);
338 break;
339 case IPPROTO_SCTP:
340 sctp_print(ndo, cp, (const u_char *)ip6, len);
341 return;
342 case IPPROTO_DCCP:
343 dccp_print(ndo, cp, (const u_char *)ip6, len);
344 return;
345 case IPPROTO_TCP:
346 tcp_print(ndo, cp, len, (const u_char *)ip6, fragmented);
347 return;
348 case IPPROTO_UDP:
349 udp_print(ndo, cp, len, (const u_char *)ip6, fragmented);
350 return;
351 case IPPROTO_ICMPV6:
352 icmp6_print(ndo, cp, len, (const u_char *)ip6, fragmented);
353 return;
354 case IPPROTO_AH:
355 advance = ah_print(ndo, cp);
356 if (advance < 0)
357 return;
358 nh = EXTRACT_U_1(cp);
359 break;
360 case IPPROTO_ESP:
361 {
362 u_int enh, padlen;
363 advance = esp_print(ndo, cp, len, (const u_char *)ip6, &enh, &padlen);
364 if (advance < 0)
365 return;
366 nh = enh & 0xff;
367 len -= padlen;
368 break;
369 }
370 case IPPROTO_IPCOMP:
371 {
372 ipcomp_print(ndo, cp);
373 /*
374 * Either this has decompressed the payload and
375 * printed it, in which case there's nothing more
376 * to do, or it hasn't, in which case there's
377 * nothing more to do.
378 */
379 advance = -1;
380 break;
381 }
382
383 case IPPROTO_PIM:
384 pim_print(ndo, cp, len, (const u_char *)ip6);
385 return;
386
387 case IPPROTO_OSPF:
388 ospf6_print(ndo, cp, len);
389 return;
390
391 case IPPROTO_IPV6:
392 ip6_print(ndo, cp, len);
393 return;
394
395 case IPPROTO_IPV4:
396 ip_print(ndo, cp, len);
397 return;
398
399 case IPPROTO_PGM:
400 pgm_print(ndo, cp, len, (const u_char *)ip6);
401 return;
402
403 case IPPROTO_GRE:
404 gre_print(ndo, cp, len);
405 return;
406
407 case IPPROTO_RSVP:
408 rsvp_print(ndo, cp, len);
409 return;
410
411 case IPPROTO_EIGRP:
412 eigrp_print(ndo, cp, len);
413 return;
414
415 case IPPROTO_NONE:
416 ND_PRINT("no next header");
417 return;
418
419 default:
420 ND_PRINT("ip-proto-%u %u", nh, len);
421 return;
422 }
423 }
424
425 return;
426 trunc:
427 ND_PRINT("[|ip6]");
428 }