]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6.c
Handle the IPv6 Jumbo Payload option.
[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 /* \summary: IPv6 printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include <string.h>
31
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "extract.h"
35
36 #include "ip6.h"
37 #include "ipproto.h"
38
39 /*
40 * If routing headers are presend and valid, set dst to the final destination.
41 * Otherwise, set it to the IPv6 destination.
42 *
43 * This is used for UDP and TCP pseudo-header in the checksum
44 * calculation.
45 */
46 static void
47 ip6_finddst(netdissect_options *ndo, struct in6_addr *dst,
48 const struct ip6_hdr *ip6)
49 {
50 const u_char *cp;
51 u_int advance;
52 u_int nh;
53 const void *dst_addr;
54 const struct ip6_rthdr *dp;
55 const struct ip6_rthdr0 *dp0;
56 const struct ip6_srh *srh;
57 const u_char *p;
58 int i, len;
59
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;
64
65 while (cp < ndo->ndo_snapend) {
66 cp += advance;
67
68 switch (nh) {
69
70 case IPPROTO_HOPOPTS:
71 case IPPROTO_DSTOPTS:
72 case IPPROTO_MOBILITY_OLD:
73 case IPPROTO_MOBILITY:
74 /*
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
78 * the first 8 octets.
79 */
80 ND_TCHECK_2(cp);
81 advance = (GET_U_1(cp + 1) + 1) << 3;
82 nh = GET_U_1(cp);
83 break;
84
85 case IPPROTO_FRAGMENT:
86 /*
87 * The byte following the next header byte is
88 * marked as reserved, and the header is always
89 * the same size.
90 */
91 ND_TCHECK_1(cp);
92 advance = sizeof(struct ip6_frag);
93 nh = GET_U_1(cp);
94 break;
95
96 case IPPROTO_ROUTING:
97 /*
98 * OK, we found it.
99 */
100 dp = (const struct ip6_rthdr *)cp;
101 ND_TCHECK_SIZE(dp);
102 len = GET_U_1(dp->ip6r_len);
103 switch (GET_U_1(dp->ip6r_type)) {
104
105 case IPV6_RTHDR_TYPE_0:
106 case IPV6_RTHDR_TYPE_2: /* Mobile IPv6 ID-20 */
107 dp0 = (const struct ip6_rthdr0 *)dp;
108 if (len % 2 == 1)
109 goto trunc;
110 len >>= 1;
111 p = (const u_char *) dp0->ip6r0_addr;
112 for (i = 0; i < len; i++) {
113 ND_TCHECK_16(p);
114 dst_addr = (const void *)p;
115 p += 16;
116 }
117 break;
118 case IPV6_RTHDR_TYPE_4:
119 /* IPv6 Segment Routing Header (SRH) */
120 srh = (const struct ip6_srh *)dp;
121 if (len % 2 == 1)
122 goto trunc;
123 p = (const u_char *) srh->srh_segments;
124 /*
125 * The list of segments are encoded in the reverse order.
126 * Accordingly, the final DA is encoded in srh_segments[0]
127 */
128 ND_TCHECK_16(p);
129 dst_addr = (const void *)p;
130 break;
131
132 default:
133 break;
134 }
135
136 /*
137 * Only one routing header to a customer.
138 */
139 goto done;
140
141 case IPPROTO_AH:
142 case IPPROTO_ESP:
143 case IPPROTO_IPCOMP:
144 default:
145 /*
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.)
155 *
156 * IPComp is also "viewed as an end-to-end
157 * payload" "in the IPv6 context".
158 *
159 * All other protocols are assumed to be the final
160 * protocol.
161 */
162 goto done;
163 }
164 }
165
166 done:
167 trunc:
168 UNALIGNED_MEMCPY(dst, dst_addr, sizeof(nd_ipv6));
169 }
170
171 /*
172 * Compute a V6-style checksum by building a pseudoheader.
173 */
174 uint16_t
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)
178 {
179 struct {
180 struct in6_addr ph_src;
181 struct in6_addr ph_dst;
182 uint32_t ph_len;
183 uint8_t ph_zero[3];
184 uint8_t ph_nxt;
185 } ph;
186 struct cksum_vec vec[2];
187 u_int nh;
188
189 /* pseudo-header */
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);
193 switch (nh) {
194
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:
201 /*
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.
205 */
206 ip6_finddst(ndo, &ph.ph_dst, ip6);
207 break;
208
209 default:
210 UNALIGNED_MEMCPY(&ph.ph_dst, ip6->ip6_dst,
211 sizeof (struct in6_addr));
212 break;
213 }
214 ph.ph_len = htonl(len);
215 ph.ph_nxt = next_proto;
216
217 vec[0].ptr = (const uint8_t *)(void *)&ph;
218 vec[0].len = sizeof(ph);
219 vec[1].ptr = data;
220 vec[1].len = covlen;
221
222 return in_cksum(vec, 2);
223 }
224
225 /*
226 * print an IP6 datagram.
227 */
228 void
229 ip6_print(netdissect_options *ndo, const u_char *bp, u_int length)
230 {
231 const struct ip6_hdr *ip6;
232 int advance;
233 u_int len;
234 u_int total_advance;
235 const u_char *cp;
236 uint32_t payload_len;
237 uint8_t nh;
238 int fragmented = 0;
239 u_int flow;
240 int found_extension_header;
241 int found_jumbo;
242
243 ndo->ndo_protocol = "ip6";
244 ip6 = (const struct ip6_hdr *)bp;
245
246 ND_TCHECK_SIZE(ip6);
247 if (length < sizeof (struct ip6_hdr)) {
248 ND_PRINT("truncated-ip6 %u", length);
249 return;
250 }
251
252 if (!ndo->ndo_eflag)
253 ND_PRINT("IP6 ");
254
255 if (IP6_VERSION(ip6) != 6) {
256 ND_PRINT("version error: %u != 6", IP6_VERSION(ip6));
257 return;
258 }
259
260 payload_len = GET_BE_U_2(ip6->ip6_plen);
261 /*
262 * RFC 1883 says:
263 *
264 * The Payload Length field in the IPv6 header must be set to zero
265 * in every packet that carries the Jumbo Payload option. If a
266 * packet is received with a valid Jumbo Payload option present and
267 * a non-zero IPv6 Payload Length field, an ICMP Parameter Problem
268 * message, Code 0, should be sent to the packet's source, pointing
269 * to the Option Type field of the Jumbo Payload option.
270 *
271 * Later versions of the IPv6 spec don't discuss the Jumbo Payload
272 * option.
273 *
274 * If the payload length is 0, we temporarily just set the total
275 * length to the remaining data in the packet (which, for Ethernet,
276 * could include frame padding, but if it's a Jumbo Payload frame,
277 * it shouldn't even be sendable over Ethernet, so we don't worry
278 * about that), so we can process the extension headers in order
279 * to *find* a Jumbo Payload hop-by-hop option and, when we've
280 * processed all the extension headers, check whether we found
281 * a Jumbo Payload option, and fail if we haven't.
282 */
283 if (payload_len != 0) {
284 len = payload_len + sizeof(struct ip6_hdr);
285 if (length < len)
286 ND_PRINT("truncated-ip6 - %u bytes missing!",
287 len - length);
288 } else
289 len = length + sizeof(struct ip6_hdr);
290
291 nh = GET_U_1(ip6->ip6_nxt);
292 if (ndo->ndo_vflag) {
293 flow = GET_BE_U_4(ip6->ip6_flow);
294 ND_PRINT("(");
295 #if 0
296 /* rfc1883 */
297 if (flow & 0x0f000000)
298 ND_PRINT("pri 0x%02x, ", (flow & 0x0f000000) >> 24);
299 if (flow & 0x00ffffff)
300 ND_PRINT("flowlabel 0x%06x, ", flow & 0x00ffffff);
301 #else
302 /* RFC 2460 */
303 if (flow & 0x0ff00000)
304 ND_PRINT("class 0x%02x, ", (flow & 0x0ff00000) >> 20);
305 if (flow & 0x000fffff)
306 ND_PRINT("flowlabel 0x%05x, ", flow & 0x000fffff);
307 #endif
308
309 ND_PRINT("hlim %u, next-header %s (%u) payload length: %u) ",
310 GET_U_1(ip6->ip6_hlim),
311 tok2str(ipproto_values,"unknown",nh),
312 nh,
313 payload_len);
314 }
315
316 /*
317 * Cut off the snapshot length to the end of the IP payload.
318 */
319 nd_push_snapend(ndo, bp + len);
320
321 cp = (const u_char *)ip6;
322 advance = sizeof(struct ip6_hdr);
323 total_advance = 0;
324 /* Process extension headers */
325 found_extension_header = 0;
326 found_jumbo = 0;
327 while (cp < ndo->ndo_snapend && advance > 0) {
328 if (len < (u_int)advance)
329 goto trunc;
330 cp += advance;
331 len -= advance;
332 total_advance += advance;
333
334 if (cp == (const u_char *)(ip6 + 1) &&
335 nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
336 nh != IPPROTO_DCCP && nh != IPPROTO_SCTP) {
337 ND_PRINT("%s > %s: ", ip6addr_string(ndo, ip6->ip6_src),
338 ip6addr_string(ndo, ip6->ip6_dst));
339 }
340
341 switch (nh) {
342
343 case IPPROTO_HOPOPTS:
344 advance = hbhopt_process(ndo, cp, &found_jumbo, &payload_len);
345 if (advance < 0) {
346 nd_pop_packet_info(ndo);
347 return;
348 }
349 found_extension_header = 1;
350 nh = GET_U_1(cp);
351 break;
352
353 case IPPROTO_DSTOPTS:
354 advance = dstopt_process(ndo, cp);
355 if (advance < 0) {
356 nd_pop_packet_info(ndo);
357 return;
358 }
359 found_extension_header = 1;
360 nh = GET_U_1(cp);
361 break;
362
363 case IPPROTO_FRAGMENT:
364 advance = frag6_print(ndo, cp, (const u_char *)ip6);
365 if (advance < 0 || ndo->ndo_snapend <= cp + advance) {
366 nd_pop_packet_info(ndo);
367 return;
368 }
369 found_extension_header = 1;
370 nh = GET_U_1(cp);
371 fragmented = 1;
372 break;
373
374 case IPPROTO_MOBILITY_OLD:
375 case IPPROTO_MOBILITY:
376 /*
377 * XXX - we don't use "advance"; RFC 3775 says that
378 * the next header field in a mobility header
379 * should be IPPROTO_NONE, but speaks of
380 * the possiblity of a future extension in
381 * which payload can be piggybacked atop a
382 * mobility header.
383 */
384 advance = mobility_print(ndo, cp, (const u_char *)ip6);
385 if (advance < 0) {
386 nd_pop_packet_info(ndo);
387 return;
388 }
389 found_extension_header = 1;
390 nh = GET_U_1(cp);
391 nd_pop_packet_info(ndo);
392 return;
393
394 case IPPROTO_ROUTING:
395 ND_TCHECK_1(cp);
396 advance = rt6_print(ndo, cp, (const u_char *)ip6);
397 if (advance < 0) {
398 nd_pop_packet_info(ndo);
399 return;
400 }
401 found_extension_header = 1;
402 nh = GET_U_1(cp);
403 break;
404
405 default:
406 /*
407 * Not an extension header; hand off to the
408 * IP protocol demuxer.
409 */
410 if (found_jumbo) {
411 /*
412 * We saw a Jumbo Payload option.
413 * Set the length to the payload length
414 * plus the IPv6 header length, and
415 * change the snapshot length accordingly.
416 */
417 len = payload_len + sizeof(struct ip6_hdr);
418 if (length < len)
419 ND_PRINT("truncated-ip6 - %u bytes missing!",
420 len - length);
421 nd_change_snapend(ndo, bp + len);
422
423 /*
424 * Now subtract the length of the IPv6
425 * header plus extension headers to get
426 * the payload length.
427 */
428 len -= total_advance;
429 } else {
430 /*
431 * We didn't see a Jumbo Payload option;
432 * was the payload length zero?
433 */
434 if (payload_len == 0) {
435 /*
436 * Yes. If we found an extension
437 * header, treat that as a truncated
438 * packet header, as there was
439 * no payload to contain an
440 * extension header.
441 */
442 if (found_extension_header)
443 goto trunc;
444
445 /*
446 * OK, we didn't see any extnesion
447 * header, but that means we have
448 * no payload, so set the length
449 * to the IPv6 header length,
450 * and change the snapshot length
451 * accordingly.
452 */
453 len = sizeof(struct ip6_hdr);
454 nd_change_snapend(ndo, bp + len);
455
456 /*
457 * Now subtract the length of
458 * the IPv6 header plus extension
459 * headers (there weren't any, so
460 * that's just the IPv6 header
461 * length) to get the payload length.
462 */
463 len -= total_advance;
464 }
465 }
466 ip_print_demux(ndo, cp, len, 6, fragmented,
467 GET_U_1(ip6->ip6_hlim), nh, bp);
468 nd_pop_packet_info(ndo);
469 return;
470 }
471
472 /* ndo_protocol reassignment after xxx_print() calls */
473 ndo->ndo_protocol = "ip6";
474 }
475
476 nd_pop_packet_info(ndo);
477 return;
478 trunc:
479 nd_print_trunc(ndo);
480 }