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