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