2 * Routines for IPv6 packet disassembly
4 * $Id: packet-ipv6.c,v 1.49 2001/01/03 06:55:29 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
34 #ifdef HAVE_SYS_SOCKET_h
35 #include <sys/socket.h>
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
49 #include "packet-ip.h"
50 #include "packet-ipsec.h"
51 #include "packet-ipv6.h"
52 #include "packet-tcp.h"
53 #include "packet-udp.h"
57 * NOTE: ipv6.nxt is not very useful as we will have chained header.
58 * now testing ipv6.final, but it raises SEGV.
62 static int proto_ipv6 = -1;
63 static int hf_ipv6_version = -1;
64 static int hf_ipv6_class = -1;
65 static int hf_ipv6_flow = -1;
66 static int hf_ipv6_plen = -1;
67 static int hf_ipv6_nxt = -1;
68 static int hf_ipv6_hlim = -1;
69 static int hf_ipv6_src = -1;
70 static int hf_ipv6_dst = -1;
71 static int hf_ipv6_addr = -1;
73 static int hf_ipv6_final = -1;
76 static gint ett_ipv6 = -1;
79 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
83 dissect_routing6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
86 proto_tree *rthdr_tree;
88 char buf[sizeof(struct ip6_rthdr0) + sizeof(struct e_in6_addr) * 23];
90 memcpy(&rt, (void *) &pd[offset], sizeof(rt));
91 len = (rt.ip6r_len + 1) << 3;
94 /* !!! specify length */
95 ti = proto_tree_add_text(tree, NullTVB, offset, len,
96 "Routing Header, Type %u", rt.ip6r_type);
97 rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
99 proto_tree_add_text(rthdr_tree, NullTVB,
100 offset + offsetof(struct ip6_rthdr, ip6r_nxt), 1,
101 "Next header: %s (0x%02x)", ipprotostr(rt.ip6r_nxt), rt.ip6r_nxt);
102 proto_tree_add_text(rthdr_tree, NullTVB,
103 offset + offsetof(struct ip6_rthdr, ip6r_len), 1,
104 "Length: %u (%d bytes)", rt.ip6r_len, len);
105 proto_tree_add_text(rthdr_tree, NullTVB,
106 offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
107 "Type: %u", rt.ip6r_type);
108 proto_tree_add_text(rthdr_tree, NullTVB,
109 offset + offsetof(struct ip6_rthdr, ip6r_segleft), 1,
110 "Segments left: %u", rt.ip6r_segleft);
112 if (rt.ip6r_type == 0 && len <= sizeof(buf)) {
113 struct e_in6_addr *a;
115 struct ip6_rthdr0 *rt0;
117 memcpy(buf, (void *) &pd[offset], len);
118 rt0 = (struct ip6_rthdr0 *)buf;
119 for (a = rt0->ip6r0_addr, n = 0;
120 a < (struct e_in6_addr *)(buf + len);
122 proto_tree_add_text(rthdr_tree, NullTVB,
123 offset + offsetof(struct ip6_rthdr0, ip6r0_addr) + n * sizeof(struct e_in6_addr),
124 sizeof(struct e_in6_addr),
126 "address %d: %s (%s)",
127 n, get_hostname6(a), ip6_to_str(a)
129 "address %d: %s", n, ip6_to_str(a)
142 dissect_frag6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
144 struct ip6_frag frag;
147 proto_tree *rthdr_tree;
149 memcpy(&frag, (void *) &pd[offset], sizeof(frag));
151 frag.ip6f_offlg = ntohs(frag.ip6f_offlg);
152 *fragstart = frag.ip6f_offlg & IP6F_OFF_MASK;
153 if (check_col(fd, COL_INFO)) {
154 col_add_fstr(fd, COL_INFO,
155 "IPv6 fragment (nxt=%s (0x%02x) off=%u id=0x%x)",
156 ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
157 *fragstart, frag.ip6f_ident);
160 ti = proto_tree_add_text(tree, NullTVB, offset, len,
161 "Fragmention Header");
162 rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
164 proto_tree_add_text(rthdr_tree, NullTVB,
165 offset + offsetof(struct ip6_frag, ip6f_nxt), 1,
166 "Next header: %s (0x%02x)",
167 ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt);
170 proto_tree_add_text(rthdr_tree, NullTVB,
171 offset + offsetof(struct ip6_frag, ip6f_reserved), 1,
176 proto_tree_add_text(rthdr_tree, NullTVB,
177 offset + offsetof(struct ip6_frag, ip6f_offlg), 2,
179 frag.ip6f_offlg & IP6F_OFF_MASK);
181 proto_tree_add_text(rthdr_tree, NullTVB,
182 offset + offsetof(struct ip6_frag, ip6f_offlg), 2,
183 "More fragments: %s",
184 frag.ip6f_offlg & IP6F_MORE_FRAG ?
187 proto_tree_add_text(rthdr_tree, NullTVB,
188 offset + offsetof(struct ip6_frag, ip6f_ident), 4,
189 "Identification: 0x%08x",
196 dissect_opts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
200 proto_tree *dstopt_tree;
203 static const value_string rtalertvals[] = {
204 { IP6OPT_RTALERT_MLD, "MLD" },
205 { IP6OPT_RTALERT_RSVP, "RSVP" },
209 memcpy(&ext, (void *) &pd[offset], sizeof(ext));
210 len = (ext.ip6e_len + 1) << 3;
213 /* !!! specify length */
214 ti = proto_tree_add_text(tree, NullTVB, offset, len,
215 "%s Header", optname);
216 dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
218 proto_tree_add_text(dstopt_tree, NullTVB,
219 offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
220 "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
221 proto_tree_add_text(dstopt_tree, NullTVB,
222 offset + offsetof(struct ip6_ext, ip6e_len), 1,
223 "Length: %u (%d bytes)", ext.ip6e_len, len);
225 p = (u_char *)(pd + offset + 2);
226 while (p < pd + offset + len) {
229 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, 1,
234 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
235 "PadN: %u bytes", p[1] + 2);
241 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
242 "Jumbo payload: %u (%u bytes)",
243 pntohl(&p[2]), p[1] + 2);
245 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
246 "Jumbo payload: Invalid length (%u bytes)",
257 rta = val_to_str(pntohs(&p[2]), rtalertvals,
260 rta = "Invalid length";
261 ti = proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
262 "Router alert: %s (%u bytes)", rta, p[1] + 2);
267 case IP6OPT_BINDING_UPDATE:
268 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
269 "Mobile IPv6 Binding Update");
273 case IP6OPT_BINDING_ACK:
274 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
275 "Mobile IPv6 Binding Acknowledgement");
279 case IP6OPT_BINDING_REQ:
280 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
281 "Mobile IPv6 Binding Request");
285 case IP6OPT_HOME_ADDRESS:
286 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
287 "Mobile IPv6 Home Address");
296 p = (u_char *)(pd + offset + len);
308 dissect_hopopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
309 return dissect_opts(pd, offset, fd, tree, "Hop-by-hop Option");
313 dissect_dstopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
314 return dissect_opts(pd, offset, fd, tree, "Destination Option");
318 dissect_ipv6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
319 proto_tree *ipv6_tree;
328 OLD_CHECK_DISPLAY_AS_DATA(proto_ipv6, pd, offset, fd, tree);
330 memcpy(&ipv6, (void *) &pd[offset], sizeof(ipv6));
332 pi.ipproto = ipv6.ip6_nxt; /* XXX make work TCP follow (ipproto = 6) */
334 SET_ADDRESS(&pi.net_src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
335 SET_ADDRESS(&pi.src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
336 SET_ADDRESS(&pi.net_dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
337 SET_ADDRESS(&pi.dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
340 /* !!! specify length */
341 ti = proto_tree_add_item(tree, proto_ipv6, NullTVB, offset, 40, FALSE);
342 ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
344 /* !!! warning: version also contains 4 Bit priority */
345 proto_tree_add_uint(ipv6_tree, hf_ipv6_version, NullTVB,
346 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
347 (ipv6.ip6_vfc >> 4) & 0x0f);
350 proto_tree_add_uint(ipv6_tree, hf_ipv6_class, NullTVB,
351 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
352 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
355 * there should be no alignment problems for ip6_flow, since it's the first
356 * guint32 in the ipv6 struct
358 proto_tree_add_uint_format(ipv6_tree, hf_ipv6_flow, NullTVB,
359 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
360 (unsigned long)(ntohl(ipv6.ip6_flow) & IPV6_FLOWLABEL_MASK),
361 "Flowlabel: 0x%05lx",
362 (unsigned long)(ntohl(ipv6.ip6_flow) & IPV6_FLOWLABEL_MASK));
364 proto_tree_add_uint(ipv6_tree, hf_ipv6_plen, NullTVB,
365 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
366 ntohs(ipv6.ip6_plen));
368 proto_tree_add_uint_format(ipv6_tree, hf_ipv6_nxt, NullTVB,
369 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
371 "Next header: %s (0x%02x)",
372 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
374 proto_tree_add_uint(ipv6_tree, hf_ipv6_hlim, NullTVB,
375 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
378 proto_tree_add_ipv6_hidden(ipv6_tree, hf_ipv6_addr, NullTVB,
379 offset + offsetof(struct ip6_hdr, ip6_src), 16,
380 ipv6.ip6_src.s6_addr8);
381 proto_tree_add_ipv6_hidden(ipv6_tree, hf_ipv6_addr, NullTVB,
382 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
383 ipv6.ip6_dst.s6_addr8);
385 proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_src, NullTVB,
386 offset + offsetof(struct ip6_hdr, ip6_src), 16,
387 (guint8 *)&ipv6.ip6_src,
389 "Source address: %s (%s)",
390 get_hostname6(&ipv6.ip6_src),
392 "Source address: %s",
394 ip6_to_str(&ipv6.ip6_src));
396 proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_dst, NullTVB,
397 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
398 (guint8 *)&ipv6.ip6_dst,
400 "Destination address: %s (%s)",
401 get_hostname6(&ipv6.ip6_dst),
403 "Destination address: %s",
405 ip6_to_str(&ipv6.ip6_dst));
408 /* start of the new header (could be a extension header) */
409 nxt = pd[poffset = offset + offsetof(struct ip6_hdr, ip6_nxt)];
410 offset += sizeof(struct ip6_hdr);
413 /* Start out assuming this isn't fragmented. */
414 pi.fragmented = FALSE;
418 case IP_PROTO_HOPOPTS:
419 advance = dissect_hopopts(pd, offset, fd, tree);
420 nxt = pd[poffset = offset];
423 case IP_PROTO_ROUTING:
424 advance = dissect_routing6(pd, offset, fd, tree);
425 nxt = pd[poffset = offset];
428 case IP_PROTO_FRAGMENT:
429 pi.fragmented = TRUE;
430 advance = dissect_frag6(pd, offset, fd, tree, &frag);
431 nxt = pd[poffset = offset];
435 advance = dissect_ah_old(pd, offset, fd, tree);
436 nxt = pd[poffset = offset];
439 case IP_PROTO_DSTOPTS:
440 advance = dissect_dstopts(pd, offset, fd, tree);
441 nxt = pd[poffset = offset];
447 proto_tree_add_uint_hidden(ipv6_tree, hf_ipv6_final, NullTVB, poffset, 1, nxt);
451 if (check_col(fd, COL_PROTOCOL))
452 col_set_str(fd, COL_PROTOCOL, "IPv6");
453 /* COL_INFO was filled in by "dissect_frag6()" */
454 old_dissect_data(pd, offset, fd, tree);
456 /* do lookup with the subdissector table */
457 if (!old_dissector_try_port(ip_dissector_table, nxt, pd, offset, fd, tree)) {
458 /* Unknown protocol */
459 if (check_col(fd, COL_PROTOCOL))
460 col_set_str(fd, COL_PROTOCOL, "IPv6");
461 if (check_col(fd, COL_INFO))
462 col_add_fstr(fd, COL_INFO, "%s (0x%02x)", ipprotostr(nxt), nxt);
463 old_dissect_data(pd, offset, fd, tree);
469 dissect_ipv6_none(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
470 if (check_col(fd, COL_INFO))
471 col_add_fstr(fd, COL_INFO, "IPv6 no next header");
473 /* XXX - dissect the payload as padding? */
477 proto_register_ipv6(void)
479 static hf_register_info hf[] = {
481 { "Version", "ipv6.version",
482 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
484 { "Traffic class", "ipv6.class",
485 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
487 { "Flowlabel", "ipv6.flow",
488 FT_UINT32, BASE_HEX, NULL, 0x0, "" }},
490 { "Payload length", "ipv6.plen",
491 FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
493 { "Next header", "ipv6.nxt",
494 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
496 { "Hop limit", "ipv6.hlim",
497 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
499 { "Source", "ipv6.src",
500 FT_IPv6, BASE_NONE, NULL, 0x0,
501 "Source IPv6 Address" }},
503 { "Destination", "ipv6.dst",
504 FT_IPv6, BASE_NONE, NULL, 0x0,
505 "Destination IPv6 Address" }},
507 { "Address", "ipv6.addr",
508 FT_IPv6, BASE_NONE, NULL, 0x0,
509 "Source or Destination IPv6 Address" }},
512 { "Final next header", "ipv6.final",
513 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
516 static gint *ett[] = {
520 proto_ipv6 = proto_register_protocol("Internet Protocol Version 6",
522 proto_register_field_array(proto_ipv6, hf, array_length(hf));
523 proto_register_subtree_array(ett, array_length(ett));
527 proto_reg_handoff_ipv6(void)
529 old_dissector_add("ethertype", ETHERTYPE_IPv6, dissect_ipv6);
530 old_dissector_add("ppp.protocol", PPP_IPV6, dissect_ipv6);
531 old_dissector_add("ip.proto", IP_PROTO_IPV6, dissect_ipv6);
532 old_dissector_add("null.type", BSD_AF_INET6_BSD, dissect_ipv6);
533 old_dissector_add("null.type", BSD_AF_INET6_FREEBSD, dissect_ipv6);
534 old_dissector_add("ip.proto", IP_PROTO_NONE, dissect_ipv6_none);