]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6.c
f3767f5db1e1df2bfb5d1a0aefe2beaa1ebff411
[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 #ifndef lint
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip6.c,v 1.42 2004-04-26 17:59:37 hannes Exp $";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #ifdef INET6
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "interface.h"
40 #include "addrtoname.h"
41 #include "extract.h"
42
43 #include "ip6.h"
44 #include "ipproto.h"
45
46 /*
47 * print an IP6 datagram.
48 */
49 void
50 ip6_print(register const u_char *bp, register u_int length)
51 {
52 register const struct ip6_hdr *ip6;
53 register int advance;
54 u_int len;
55 const u_char *ipend;
56 register const u_char *cp;
57 register u_int payload_len;
58 int nh;
59 int fragmented = 0;
60 u_int flow;
61
62 ip6 = (const struct ip6_hdr *)bp;
63
64 TCHECK(*ip6);
65 if (length < sizeof (struct ip6_hdr)) {
66 (void)printf("truncated-ip6 %d", length);
67 return;
68 }
69
70 printf("IP6 ");
71
72 payload_len = EXTRACT_16BITS(&ip6->ip6_plen);
73 len = payload_len + sizeof(struct ip6_hdr);
74 if (length < len)
75 (void)printf("truncated-ip6 - %d bytes missing!",
76 len - length);
77
78 if (vflag) {
79 flow = EXTRACT_32BITS(&ip6->ip6_flow);
80 printf("(");
81 #if 0
82 /* rfc1883 */
83 if (flow & 0x0f000000)
84 (void)printf("pri 0x%02x, ", (flow & 0x0f000000) >> 24);
85 if (flow & 0x00ffffff)
86 (void)printf("flowlabel 0x%06x, ", flow & 0x00ffffff);
87 #else
88 /* RFC 2460 */
89 if (flow & 0x0ff00000)
90 (void)printf("class 0x%02x, ", (flow & 0x0ff00000) >> 20);
91 if (flow & 0x000fffff)
92 (void)printf("flowlabel 0x%05x, ", flow & 0x000fffff);
93 #endif
94
95 (void)printf("hlim %u, next-header: %s (%u), payload-len %u) ",
96 ip6->ip6_hlim,
97 tok2str(ipproto_values,"unknown",ip6->ip6_nxt),
98 ip6->ip6_nxt,
99 payload_len);
100 }
101
102 /*
103 * Cut off the snapshot length to the end of the IP payload.
104 */
105 ipend = bp + len;
106 if (ipend < snapend)
107 snapend = ipend;
108
109 cp = (const u_char *)ip6;
110 advance = sizeof(struct ip6_hdr);
111 nh = ip6->ip6_nxt;
112 while (cp < snapend && advance > 0) {
113 cp += advance;
114 len -= advance;
115
116 if (cp == (const u_char *)(ip6 + 1) &&
117 nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
118 nh != IPPROTO_SCTP) {
119 (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src),
120 ip6addr_string(&ip6->ip6_dst));
121 }
122
123 switch (nh) {
124 case IPPROTO_HOPOPTS:
125 advance = hbhopt_print(cp);
126 nh = *cp;
127 break;
128 case IPPROTO_DSTOPTS:
129 advance = dstopt_print(cp);
130 nh = *cp;
131 break;
132 case IPPROTO_FRAGMENT:
133 advance = frag6_print(cp, (const u_char *)ip6);
134 if (snapend <= cp + advance)
135 return;
136 nh = *cp;
137 fragmented = 1;
138 break;
139
140 case IPPROTO_MOBILITY_OLD:
141 case IPPROTO_MOBILITY:
142 /*
143 * XXX - we don't use "advance"; the current
144 * "Mobility Support in IPv6" draft
145 * (draft-ietf-mobileip-ipv6-24) says that
146 * the next header field in a mobility header
147 * should be IPPROTO_NONE, but speaks of
148 * the possiblity of a future extension in
149 * which payload can be piggybacked atop a
150 * mobility header.
151 */
152 advance = mobility_print(cp, (const u_char *)ip6);
153 nh = *cp;
154 return;
155 case IPPROTO_ROUTING:
156 advance = rt6_print(cp, (const u_char *)ip6);
157 nh = *cp;
158 break;
159 case IPPROTO_SCTP:
160 sctp_print(cp, (const u_char *)ip6, len);
161 return;
162 case IPPROTO_TCP:
163 tcp_print(cp, len, (const u_char *)ip6, fragmented);
164 return;
165 case IPPROTO_UDP:
166 udp_print(cp, len, (const u_char *)ip6, fragmented);
167 return;
168 case IPPROTO_ICMPV6:
169 icmp6_print(cp, len, (const u_char *)ip6, fragmented);
170 return;
171 case IPPROTO_AH:
172 advance = ah_print(cp);
173 nh = *cp;
174 break;
175 case IPPROTO_ESP:
176 {
177 int enh, padlen;
178 advance = esp_print(gndo, cp, (const u_char *)ip6, &enh, &padlen);
179 nh = enh & 0xff;
180 len -= padlen;
181 break;
182 }
183 case IPPROTO_IPCOMP:
184 {
185 int enh;
186 advance = ipcomp_print(cp, &enh);
187 nh = enh & 0xff;
188 break;
189 }
190
191 case IPPROTO_PIM:
192 pim_print(cp, len);
193 return;
194 case IPPROTO_OSPF:
195 ospf6_print(cp, len);
196 return;
197
198 case IPPROTO_IPV6:
199 ip6_print(cp, len);
200 return;
201
202 case IPPROTO_IPV4:
203 ip_print(cp, len);
204 return;
205
206 case IPPROTO_NONE:
207 (void)printf("no next header");
208 return;
209
210 default:
211 (void)printf("ip-proto-%d %d", ip6->ip6_nxt, len);
212 return;
213 }
214 }
215
216 return;
217 trunc:
218 (void)printf("[|ip6]");
219 }
220
221 #endif /* INET6 */