]>
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
;
56 const struct ip6_srh
*srh
;
60 cp
= (const u_char
*)ip6
;
61 advance
= sizeof(struct ip6_hdr
);
62 nh
= GET_U_1(ip6
->ip6_nxt
);
63 dst_addr
= (const void *)ip6
->ip6_dst
;
65 while (cp
< ndo
->ndo_snapend
) {
72 case IPPROTO_MOBILITY_OLD
:
73 case IPPROTO_MOBILITY
:
75 * These have a header length byte, following
76 * the next header byte, giving the length of
77 * the header, in units of 8 octets, excluding
81 advance
= (GET_U_1(cp
+ 1) + 1) << 3;
85 case IPPROTO_FRAGMENT
:
87 * The byte following the next header byte is
88 * marked as reserved, and the header is always
92 advance
= sizeof(struct ip6_frag
);
100 dp
= (const struct ip6_rthdr
*)cp
;
102 len
= GET_U_1(dp
->ip6r_len
);
103 switch (GET_U_1(dp
->ip6r_type
)) {
105 case IPV6_RTHDR_TYPE_0
:
106 case IPV6_RTHDR_TYPE_2
: /* Mobile IPv6 ID-20 */
107 dp0
= (const struct ip6_rthdr0
*)dp
;
111 p
= (const u_char
*) dp0
->ip6r0_addr
;
112 for (i
= 0; i
< len
; i
++) {
114 dst_addr
= (const void *)p
;
118 case IPV6_RTHDR_TYPE_4
:
119 /* IPv6 Segment Routing Header (SRH) */
120 srh
= (const struct ip6_srh
*)dp
;
123 p
= (const u_char
*) srh
->srh_segments
;
125 * The list of segments are encoded in the reverse order.
126 * Accordingly, the final DA is encoded in srh_segments[0]
129 dst_addr
= (const void *)p
;
137 * Only one routing header to a customer.
146 * AH and ESP are, in the RFCs that describe them,
147 * described as being "viewed as an end-to-end
148 * payload" "in the IPv6 context, so that they
149 * "should appear after hop-by-hop, routing, and
150 * fragmentation extension headers". We assume
151 * that's the case, and stop as soon as we see
152 * one. (We can't handle an ESP header in
153 * the general case anyway, as its length depends
154 * on the encryption algorithm.)
156 * IPComp is also "viewed as an end-to-end
157 * payload" "in the IPv6 context".
159 * All other protocols are assumed to be the final
168 UNALIGNED_MEMCPY(dst
, dst_addr
, sizeof(nd_ipv6
));
172 * Compute a V6-style checksum by building a pseudoheader.
175 nextproto6_cksum(netdissect_options
*ndo
,
176 const struct ip6_hdr
*ip6
, const uint8_t *data
,
177 u_int len
, u_int covlen
, uint8_t next_proto
)
180 struct in6_addr ph_src
;
181 struct in6_addr ph_dst
;
186 struct cksum_vec vec
[2];
190 memset(&ph
, 0, sizeof(ph
));
191 UNALIGNED_MEMCPY(&ph
.ph_src
, ip6
->ip6_src
, sizeof (struct in6_addr
));
192 nh
= GET_U_1(ip6
->ip6_nxt
);
195 case IPPROTO_HOPOPTS
:
196 case IPPROTO_DSTOPTS
:
197 case IPPROTO_MOBILITY_OLD
:
198 case IPPROTO_MOBILITY
:
199 case IPPROTO_FRAGMENT
:
200 case IPPROTO_ROUTING
:
202 * The next header is either a routing header or a header
203 * after which there might be a routing header, so scan
204 * for a routing header.
206 ip6_finddst(ndo
, &ph
.ph_dst
, ip6
);
210 UNALIGNED_MEMCPY(&ph
.ph_dst
, ip6
->ip6_dst
,
211 sizeof (struct in6_addr
));
214 ph
.ph_len
= htonl(len
);
215 ph
.ph_nxt
= next_proto
;
217 vec
[0].ptr
= (const uint8_t *)(void *)&ph
;
218 vec
[0].len
= sizeof(ph
);
222 return in_cksum(vec
, 2);
226 * print an IP6 datagram.
229 ip6_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
231 const struct ip6_hdr
*ip6
;
240 ndo
->ndo_protocol
= "ip6";
241 ip6
= (const struct ip6_hdr
*)bp
;
244 if (length
< sizeof (struct ip6_hdr
)) {
245 ND_PRINT("truncated-ip6 %u", length
);
252 if (IP6_VERSION(ip6
) != 6) {
253 ND_PRINT("version error: %u != 6", IP6_VERSION(ip6
));
257 payload_len
= GET_BE_U_2(ip6
->ip6_plen
);
258 len
= payload_len
+ sizeof(struct ip6_hdr
);
260 ND_PRINT("truncated-ip6 - %u bytes missing!",
263 nh
= GET_U_1(ip6
->ip6_nxt
);
264 if (ndo
->ndo_vflag
) {
265 flow
= GET_BE_U_4(ip6
->ip6_flow
);
269 if (flow
& 0x0f000000)
270 ND_PRINT("pri 0x%02x, ", (flow
& 0x0f000000) >> 24);
271 if (flow
& 0x00ffffff)
272 ND_PRINT("flowlabel 0x%06x, ", flow
& 0x00ffffff);
275 if (flow
& 0x0ff00000)
276 ND_PRINT("class 0x%02x, ", (flow
& 0x0ff00000) >> 20);
277 if (flow
& 0x000fffff)
278 ND_PRINT("flowlabel 0x%05x, ", flow
& 0x000fffff);
281 ND_PRINT("hlim %u, next-header %s (%u) payload length: %u) ",
282 GET_U_1(ip6
->ip6_hlim
),
283 tok2str(ipproto_values
,"unknown",nh
),
289 * Cut off the snapshot length to the end of the IP payload.
291 nd_push_snapend(ndo
, bp
+ len
);
293 cp
= (const u_char
*)ip6
;
294 advance
= sizeof(struct ip6_hdr
);
295 /* Process extension headers */
296 while (cp
< ndo
->ndo_snapend
&& advance
> 0) {
297 if (len
< (u_int
)advance
)
302 if (cp
== (const u_char
*)(ip6
+ 1) &&
303 nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
304 nh
!= IPPROTO_DCCP
&& nh
!= IPPROTO_SCTP
) {
305 ND_PRINT("%s > %s: ", ip6addr_string(ndo
, ip6
->ip6_src
),
306 ip6addr_string(ndo
, ip6
->ip6_dst
));
311 case IPPROTO_HOPOPTS
:
312 advance
= hbhopt_print(ndo
, cp
);
314 nd_pop_packet_info(ndo
);
320 case IPPROTO_DSTOPTS
:
321 advance
= dstopt_print(ndo
, cp
);
323 nd_pop_packet_info(ndo
);
329 case IPPROTO_FRAGMENT
:
330 advance
= frag6_print(ndo
, cp
, (const u_char
*)ip6
);
331 if (advance
< 0 || ndo
->ndo_snapend
<= cp
+ advance
) {
332 nd_pop_packet_info(ndo
);
339 case IPPROTO_MOBILITY_OLD
:
340 case IPPROTO_MOBILITY
:
342 * XXX - we don't use "advance"; RFC 3775 says that
343 * the next header field in a mobility header
344 * should be IPPROTO_NONE, but speaks of
345 * the possiblity of a future extension in
346 * which payload can be piggybacked atop a
349 advance
= mobility_print(ndo
, cp
, (const u_char
*)ip6
);
351 nd_pop_packet_info(ndo
);
355 nd_pop_packet_info(ndo
);
358 case IPPROTO_ROUTING
:
360 advance
= rt6_print(ndo
, cp
, (const u_char
*)ip6
);
362 nd_pop_packet_info(ndo
);
370 * Not an extension header; hand off to the
371 * IP protocol demuxer.
373 ip_print_demux(ndo
, cp
, len
, 6, fragmented
,
374 GET_U_1(ip6
->ip6_hlim
), nh
, bp
);
375 nd_pop_packet_info(ndo
);
379 /* ndo_protocol reassignment after xxx_print() calls */
380 ndo
->ndo_protocol
= "ip6";
383 nd_pop_packet_info(ndo
);