]>
The Tcpdump Group git mirrors - tcpdump/blob - print-ip6.c
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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
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.
22 /* \summary: IPv6 printer */
28 #include "netdissect-stdinc.h"
32 #include "netdissect.h"
33 #include "addrtoname.h"
40 * If routing headers are presend and valid, set dst to the final destination.
41 * Otherwise, set it to the IPv6 destination.
43 * This is used for UDP and TCP pseudo-header in the checksum
47 ip6_finddst(netdissect_options
*ndo
, struct in6_addr
*dst
,
48 const struct ip6_hdr
*ip6
)
54 const struct ip6_rthdr
*dp
;
55 const struct ip6_rthdr0
*dp0
;
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
;
64 while (cp
< ndo
->ndo_snapend
) {
71 case IPPROTO_MOBILITY_OLD
:
72 case IPPROTO_MOBILITY
:
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
80 advance
= (EXTRACT_U_1(cp
+ 1) + 1) << 3;
84 case IPPROTO_FRAGMENT
:
86 * The byte following the next header byte is
87 * marked as reserved, and the header is always
91 advance
= sizeof(struct ip6_frag
);
99 dp
= (const struct ip6_rthdr
*)cp
;
101 len
= EXTRACT_U_1(dp
->ip6r_len
);
102 switch (EXTRACT_U_1(dp
->ip6r_type
)) {
104 case IPV6_RTHDR_TYPE_0
:
105 case IPV6_RTHDR_TYPE_2
: /* Mobile IPv6 ID-20 */
106 dp0
= (const struct ip6_rthdr0
*)dp
;
110 p
= (const u_char
*) dp0
->ip6r0_addr
;
111 for (i
= 0; i
< len
; i
++) {
113 dst_addr
= (const void *)p
;
123 * Only one routing header to a customer.
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.)
142 * IPComp is also "viewed as an end-to-end
143 * payload" "in the IPv6 context".
145 * All other protocols are assumed to be the final
154 UNALIGNED_MEMCPY(dst
, dst_addr
, sizeof(nd_ipv6
));
158 * Compute a V6-style checksum by building a pseudoheader.
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
)
166 struct in6_addr ph_src
;
167 struct in6_addr ph_dst
;
172 struct cksum_vec vec
[2];
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
);
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
:
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.
192 ip6_finddst(ndo
, &ph
.ph_dst
, ip6
);
196 UNALIGNED_MEMCPY(&ph
.ph_dst
, &ip6
->ip6_dst
, sizeof (struct in6_addr
));
199 ph
.ph_len
= htonl(len
);
200 ph
.ph_nxt
= next_proto
;
202 vec
[0].ptr
= (const uint8_t *)(void *)&ph
;
203 vec
[0].len
= sizeof(ph
);
207 return in_cksum(vec
, 2);
211 * print an IP6 datagram.
214 ip6_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
216 const struct ip6_hdr
*ip6
;
226 ip6
= (const struct ip6_hdr
*)bp
;
229 if (length
< sizeof (struct ip6_hdr
)) {
230 ND_PRINT("truncated-ip6 %u", length
);
237 if (IP6_VERSION(ip6
) != 6) {
238 ND_PRINT("version error: %u != 6", IP6_VERSION(ip6
));
242 payload_len
= EXTRACT_BE_U_2(ip6
->ip6_plen
);
243 len
= payload_len
+ sizeof(struct ip6_hdr
);
245 ND_PRINT("truncated-ip6 - %u bytes missing!",
248 nh
= EXTRACT_U_1(ip6
->ip6_nxt
);
249 if (ndo
->ndo_vflag
) {
250 flow
= EXTRACT_BE_U_4(ip6
->ip6_flow
);
254 if (flow
& 0x0f000000)
255 ND_PRINT("pri 0x%02x, ", (flow
& 0x0f000000) >> 24);
256 if (flow
& 0x00ffffff)
257 ND_PRINT("flowlabel 0x%06x, ", flow
& 0x00ffffff);
260 if (flow
& 0x0ff00000)
261 ND_PRINT("class 0x%02x, ", (flow
& 0x0ff00000) >> 20);
262 if (flow
& 0x000fffff)
263 ND_PRINT("flowlabel 0x%05x, ", flow
& 0x000fffff);
266 ND_PRINT("hlim %u, next-header %s (%u) payload length: %u) ",
267 EXTRACT_U_1(ip6
->ip6_hlim
),
268 tok2str(ipproto_values
,"unknown",nh
),
274 * Cut off the snapshot length to the end of the IP payload.
277 if (ipend
< ndo
->ndo_snapend
)
278 ndo
->ndo_snapend
= ipend
;
280 cp
= (const u_char
*)ip6
;
281 advance
= sizeof(struct ip6_hdr
);
282 while (cp
< ndo
->ndo_snapend
&& advance
> 0) {
283 if (len
< (u_int
)advance
)
288 if (cp
== (const u_char
*)(ip6
+ 1) &&
289 nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
290 nh
!= IPPROTO_DCCP
&& nh
!= IPPROTO_SCTP
) {
291 ND_PRINT("%s > %s: ", ip6addr_string(ndo
, &ip6
->ip6_src
),
292 ip6addr_string(ndo
, &ip6
->ip6_dst
));
296 case IPPROTO_HOPOPTS
:
297 advance
= hbhopt_print(ndo
, cp
);
300 nh
= EXTRACT_U_1(cp
);
302 case IPPROTO_DSTOPTS
:
303 advance
= dstopt_print(ndo
, cp
);
306 nh
= EXTRACT_U_1(cp
);
308 case IPPROTO_FRAGMENT
:
309 advance
= frag6_print(ndo
, cp
, (const u_char
*)ip6
);
310 if (advance
< 0 || ndo
->ndo_snapend
<= cp
+ advance
)
312 nh
= EXTRACT_U_1(cp
);
316 case IPPROTO_MOBILITY_OLD
:
317 case IPPROTO_MOBILITY
:
319 * XXX - we don't use "advance"; RFC 3775 says that
320 * the next header field in a mobility header
321 * should be IPPROTO_NONE, but speaks of
322 * the possiblity of a future extension in
323 * which payload can be piggybacked atop a
326 advance
= mobility_print(ndo
, cp
, (const u_char
*)ip6
);
329 nh
= EXTRACT_U_1(cp
);
331 case IPPROTO_ROUTING
:
333 advance
= rt6_print(ndo
, cp
, (const u_char
*)ip6
);
336 nh
= EXTRACT_U_1(cp
);
339 sctp_print(ndo
, cp
, (const u_char
*)ip6
, len
);
342 dccp_print(ndo
, cp
, (const u_char
*)ip6
, len
);
345 tcp_print(ndo
, cp
, len
, (const u_char
*)ip6
, fragmented
);
348 udp_print(ndo
, cp
, len
, (const u_char
*)ip6
, fragmented
);
351 icmp6_print(ndo
, cp
, len
, (const u_char
*)ip6
, fragmented
);
354 advance
= ah_print(ndo
, cp
);
357 nh
= EXTRACT_U_1(cp
);
362 advance
= esp_print(ndo
, cp
, len
, (const u_char
*)ip6
, &enh
, &padlen
);
371 ipcomp_print(ndo
, cp
);
373 * Either this has decompressed the payload and
374 * printed it, in which case there's nothing more
375 * to do, or it hasn't, in which case there's
376 * nothing more to do.
383 pim_print(ndo
, cp
, len
, (const u_char
*)ip6
);
387 ospf6_print(ndo
, cp
, len
);
391 ip6_print(ndo
, cp
, len
);
395 ip_print(ndo
, cp
, len
);
399 pgm_print(ndo
, cp
, len
, (const u_char
*)ip6
);
403 gre_print(ndo
, cp
, len
);
407 rsvp_print(ndo
, cp
, len
);
411 eigrp_print(ndo
, cp
, len
);
415 ND_PRINT("no next header");
419 ND_PRINT("ip-proto-%u %u", nh
, len
);