]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6.c
ether_encap_print() is a utility for use by printers for protocols that
[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.52 2007-09-21 07:05:33 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 * Compute a V6-style checksum by building a pseudoheader.
48 */
49 int
50 nextproto6_cksum(const struct ip6_hdr *ip6, const u_short *data,
51 u_int len, u_int next_proto)
52 {
53 size_t i;
54 u_int32_t sum = 0;
55 union ip6_pseudo_hdr phu;
56
57 /* pseudo-header */
58 memset(&phu, 0, sizeof(phu));
59 phu.ph.ph_src = ip6->ip6_src;
60 phu.ph.ph_dst = ip6->ip6_dst;
61 phu.ph.ph_len = htonl(len);
62 phu.ph.ph_nxt = next_proto;
63
64 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++) {
65 sum += phu.pa[i];
66 }
67
68 return in_cksum(data, len, sum);
69 }
70
71 /*
72 * print an IP6 datagram.
73 */
74 void
75 ip6_print(register const u_char *bp, register u_int length)
76 {
77 register const struct ip6_hdr *ip6;
78 register int advance;
79 u_int len;
80 const u_char *ipend;
81 register const u_char *cp;
82 register u_int payload_len;
83 int nh;
84 int fragmented = 0;
85 u_int flow;
86
87 ip6 = (const struct ip6_hdr *)bp;
88
89 TCHECK(*ip6);
90 if (length < sizeof (struct ip6_hdr)) {
91 (void)printf("truncated-ip6 %u", length);
92 return;
93 }
94
95 if (!eflag)
96 printf("IP6 ");
97
98 payload_len = EXTRACT_16BITS(&ip6->ip6_plen);
99 len = payload_len + sizeof(struct ip6_hdr);
100 if (length < len)
101 (void)printf("truncated-ip6 - %u bytes missing!",
102 len - length);
103
104 if (vflag) {
105 flow = EXTRACT_32BITS(&ip6->ip6_flow);
106 printf("(");
107 #if 0
108 /* rfc1883 */
109 if (flow & 0x0f000000)
110 (void)printf("pri 0x%02x, ", (flow & 0x0f000000) >> 24);
111 if (flow & 0x00ffffff)
112 (void)printf("flowlabel 0x%06x, ", flow & 0x00ffffff);
113 #else
114 /* RFC 2460 */
115 if (flow & 0x0ff00000)
116 (void)printf("class 0x%02x, ", (flow & 0x0ff00000) >> 20);
117 if (flow & 0x000fffff)
118 (void)printf("flowlabel 0x%05x, ", flow & 0x000fffff);
119 #endif
120
121 (void)printf("hlim %u, next-header %s (%u) payload length: %u) ",
122 ip6->ip6_hlim,
123 tok2str(ipproto_values,"unknown",ip6->ip6_nxt),
124 ip6->ip6_nxt,
125 payload_len);
126 }
127
128 /*
129 * Cut off the snapshot length to the end of the IP payload.
130 */
131 ipend = bp + len;
132 if (ipend < snapend)
133 snapend = ipend;
134
135 cp = (const u_char *)ip6;
136 advance = sizeof(struct ip6_hdr);
137 nh = ip6->ip6_nxt;
138 while (cp < snapend && advance > 0) {
139 cp += advance;
140 len -= advance;
141
142 if (cp == (const u_char *)(ip6 + 1) &&
143 nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
144 nh != IPPROTO_DCCP && nh != IPPROTO_SCTP) {
145 (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src),
146 ip6addr_string(&ip6->ip6_dst));
147 }
148
149 switch (nh) {
150 case IPPROTO_HOPOPTS:
151 advance = hbhopt_print(cp);
152 nh = *cp;
153 break;
154 case IPPROTO_DSTOPTS:
155 advance = dstopt_print(cp);
156 nh = *cp;
157 break;
158 case IPPROTO_FRAGMENT:
159 advance = frag6_print(cp, (const u_char *)ip6);
160 if (snapend <= cp + advance)
161 return;
162 nh = *cp;
163 fragmented = 1;
164 break;
165
166 case IPPROTO_MOBILITY_OLD:
167 case IPPROTO_MOBILITY:
168 /*
169 * XXX - we don't use "advance"; the current
170 * "Mobility Support in IPv6" draft
171 * (draft-ietf-mobileip-ipv6-24) says that
172 * the next header field in a mobility header
173 * should be IPPROTO_NONE, but speaks of
174 * the possiblity of a future extension in
175 * which payload can be piggybacked atop a
176 * mobility header.
177 */
178 advance = mobility_print(cp, (const u_char *)ip6);
179 nh = *cp;
180 return;
181 case IPPROTO_ROUTING:
182 advance = rt6_print(cp, (const u_char *)ip6);
183 nh = *cp;
184 break;
185 case IPPROTO_SCTP:
186 sctp_print(cp, (const u_char *)ip6, len);
187 return;
188 case IPPROTO_DCCP:
189 dccp_print(cp, (const u_char *)ip6, len);
190 return;
191 case IPPROTO_TCP:
192 tcp_print(cp, len, (const u_char *)ip6, fragmented);
193 return;
194 case IPPROTO_UDP:
195 udp_print(cp, len, (const u_char *)ip6, fragmented);
196 return;
197 case IPPROTO_ICMPV6:
198 icmp6_print(gndo, cp, len, (const u_char *)ip6, fragmented);
199 return;
200 case IPPROTO_AH:
201 advance = ah_print(cp);
202 nh = *cp;
203 break;
204 case IPPROTO_ESP:
205 {
206 int enh, padlen;
207 advance = esp_print(gndo, cp, len, (const u_char *)ip6, &enh, &padlen);
208 nh = enh & 0xff;
209 len -= padlen;
210 break;
211 }
212 case IPPROTO_IPCOMP:
213 {
214 int enh;
215 advance = ipcomp_print(cp, &enh);
216 nh = enh & 0xff;
217 break;
218 }
219
220 case IPPROTO_PIM:
221 pim_print(cp, len, nextproto6_cksum(ip6, (u_short *)cp, len,
222 IPPROTO_PIM));
223 return;
224
225 case IPPROTO_OSPF:
226 ospf6_print(cp, len);
227 return;
228
229 case IPPROTO_IPV6:
230 ip6_print(cp, len);
231 return;
232
233 case IPPROTO_IPV4:
234 ip_print(gndo, cp, len);
235 return;
236
237 case IPPROTO_PGM:
238 pgm_print(cp, len, (const u_char *)ip6);
239 return;
240
241 case IPPROTO_GRE:
242 gre_print(cp, len);
243 return;
244
245 case IPPROTO_RSVP:
246 rsvp_print(cp, len);
247 return;
248
249 case IPPROTO_NONE:
250 (void)printf("no next header");
251 return;
252
253 default:
254 (void)printf("ip-proto-%d %d", nh, len);
255 return;
256 }
257 }
258
259 return;
260 trunc:
261 (void)printf("[|ip6]");
262 }
263
264 #endif /* INET6 */