2 * Routines for IPv6 packet disassembly
4 * $Id: packet-ipv6.c,v 1.34 2000/04/16 22:46:20 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>
48 #include "packet-icmpv6.h"
49 #include "packet-ip.h"
50 #include "packet-ipsec.h"
51 #include "packet-ipv6.h"
52 #include "packet-pim.h"
53 #include "packet-tcp.h"
54 #include "packet-udp.h"
58 * NOTE: ipv6.nxt is not very useful as we will have chained header.
59 * now testing ipv6.final, but it raises SEGV.
63 static int proto_ipv6 = -1;
64 static int hf_ipv6_version = -1;
65 static int hf_ipv6_class = -1;
66 static int hf_ipv6_flow = -1;
67 static int hf_ipv6_plen = -1;
68 static int hf_ipv6_nxt = -1;
69 static int hf_ipv6_hlim = -1;
70 static int hf_ipv6_src = -1;
71 static int hf_ipv6_dst = -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, 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,
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,
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,
106 offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
107 "Type: %u", rt.ip6r_type);
108 proto_tree_add_text(rthdr_tree,
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,
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 memcpy(&frag, (void *) &pd[offset], sizeof(frag));
149 *fragstart = ntohs(frag.ip6f_offlg) & 0xfff8;
150 if (check_col(fd, COL_INFO)) {
151 col_add_fstr(fd, COL_INFO,
152 "IPv6 fragment (nxt=%s (0x%02x) off=0x%04x id=0x%x)",
153 ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
154 *fragstart, frag.ip6f_ident);
160 dissect_opts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
164 proto_tree *dstopt_tree;
167 static const value_string rtalertvals[] = {
168 { IP6OPT_RTALERT_MLD, "MLD" },
169 { IP6OPT_RTALERT_RSVP, "RSVP" },
173 memcpy(&ext, (void *) &pd[offset], sizeof(ext));
174 len = (ext.ip6e_len + 1) << 3;
177 /* !!! specify length */
178 ti = proto_tree_add_text(tree, offset, len,
179 "%s Header", optname);
180 dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
182 proto_tree_add_text(dstopt_tree,
183 offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
184 "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
185 proto_tree_add_text(dstopt_tree,
186 offset + offsetof(struct ip6_ext, ip6e_len), 1,
187 "Length: %u (%d bytes)", ext.ip6e_len, len);
189 p = (u_char *)(pd + offset + 2);
190 while (p < pd + offset + len) {
193 proto_tree_add_text(dstopt_tree, p - pd, 1,
198 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
199 "PadN: %u bytes", p[1] + 2);
205 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
206 "Jumbo payload: %u (%u bytes)",
207 pntohl(&p[2]), p[1] + 2);
209 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
210 "Jumbo payload: Invalid length (%u bytes)",
221 rta = val_to_str(pntohs(&p[2]), rtalertvals,
224 rta = "Invalid length";
225 ti = proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
226 "Router alert: %s (%u bytes)", rta, p[1] + 2);
232 p = (u_char *)(pd + offset + len);
244 dissect_hopopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
245 return dissect_opts(pd, offset, fd, tree, "Hop-by-hop Option");
249 dissect_dstopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
250 return dissect_opts(pd, offset, fd, tree, "Destination Option");
254 dissect_ipv6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
255 proto_tree *ipv6_tree;
264 memcpy(&ipv6, (void *) &pd[offset], sizeof(ipv6));
266 SET_ADDRESS(&pi.net_src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
267 SET_ADDRESS(&pi.src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
268 SET_ADDRESS(&pi.net_dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
269 SET_ADDRESS(&pi.dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
271 if (check_col(fd, COL_PROTOCOL))
272 col_add_str(fd, COL_PROTOCOL, "IPv6");
275 /* !!! specify length */
276 ti = proto_tree_add_item(tree, proto_ipv6, offset, 40, NULL);
277 ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
279 /* !!! warning: version also contains 4 Bit priority */
280 proto_tree_add_item(ipv6_tree, hf_ipv6_version,
281 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
282 (ipv6.ip6_vfc >> 4) & 0x0f);
285 proto_tree_add_item(ipv6_tree, hf_ipv6_class,
286 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
287 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
290 * there should be no alignment problems for ip6_flow, since it's the first
291 * guint32 in the ipv6 struct
293 proto_tree_add_uint_format(ipv6_tree, hf_ipv6_flow,
294 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
295 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)),
296 "Flowlabel: 0x%05lx",
297 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)));
299 proto_tree_add_item(ipv6_tree, hf_ipv6_plen,
300 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
301 ntohs(ipv6.ip6_plen));
303 proto_tree_add_uint_format(ipv6_tree, hf_ipv6_nxt,
304 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
306 "Next header: %s (0x%02x)",
307 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
309 proto_tree_add_item(ipv6_tree, hf_ipv6_hlim,
310 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
313 proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_src,
314 offset + offsetof(struct ip6_hdr, ip6_src), 16,
315 (guint8 *)&ipv6.ip6_src,
317 "Source address: %s (%s)",
318 get_hostname6(&ipv6.ip6_src),
320 "Source address: %s",
322 ip6_to_str(&ipv6.ip6_src));
324 proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_dst,
325 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
326 (guint8 *)&ipv6.ip6_dst,
328 "Destination address: %s (%s)",
329 get_hostname6(&ipv6.ip6_dst),
331 "Destination address: %s",
333 ip6_to_str(&ipv6.ip6_dst));
336 /* start of the new header (could be a extension header) */
337 nxt = pd[poffset = offset + offsetof(struct ip6_hdr, ip6_nxt)];
338 offset += sizeof(struct ip6_hdr);
343 case IP_PROTO_HOPOPTS:
344 advance = dissect_hopopts(pd, offset, fd, tree);
345 nxt = pd[poffset = offset];
349 dissect_ip(pd, offset, fd, tree);
351 case IP_PROTO_ROUTING:
352 advance = dissect_routing6(pd, offset, fd, tree);
353 nxt = pd[poffset = offset];
356 case IP_PROTO_FRAGMENT:
357 advance = dissect_frag6(pd, offset, fd, tree, &frag);
358 nxt = pd[poffset = offset];
361 case IP_PROTO_ICMPV6:
363 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
366 dissect_icmpv6(pd, offset, fd, tree);
368 dissect_data(pd, offset, fd, tree);
372 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
374 if (check_col(fd, COL_INFO)) {
375 col_add_fstr(fd, COL_INFO, "IPv6 no next header");
379 advance = dissect_ah(pd, offset, fd, tree);
380 nxt = pd[poffset = offset];
384 dissect_esp(pd, offset, fd, tree);
386 case IP_PROTO_DSTOPTS:
387 advance = dissect_dstopts(pd, offset, fd, tree);
388 nxt = pd[poffset = offset];
393 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
396 dissect_tcp(pd, offset, fd, tree);
398 dissect_data(pd, offset, fd, tree);
402 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
405 dissect_udp(pd, offset, fd, tree);
407 dissect_data(pd, offset, fd, tree);
411 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
413 dissect_pim(pd, offset, fd, tree);
415 case IP_PROTO_IPCOMP:
416 dissect_ipcomp(pd, offset, fd, tree);
420 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
422 if (check_col(fd, COL_INFO)) {
423 col_add_fstr(fd, COL_INFO, "%s (0x%02x)",
424 ipprotostr(nxt), nxt);
426 dissect_data(pd, offset, fd, tree);
432 proto_register_ipv6(void)
434 static hf_register_info hf[] = {
436 { "Version", "ipv6.version",
437 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
439 { "Traffic class", "ipv6.class",
440 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
442 { "Flowlabel", "ipv6.flow",
443 FT_UINT32, BASE_HEX, NULL, 0x0, "" }},
445 { "Payload length", "ipv6.plen",
446 FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
448 { "Next header", "ipv6.nxt",
449 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
451 { "Hop limit", "ipv6.hlim",
452 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
454 { "Source", "ipv6.src",
455 FT_IPv6, BASE_NONE, NULL, 0x0,
456 "Source IPv6 Address" }},
458 { "Destination", "ipv6.dst",
459 FT_IPv6, BASE_NONE, NULL, 0x0,
460 "Destination IPv6 Address" }},
463 { "Final next header", "ipv6.final",
464 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
467 static gint *ett[] = {
471 proto_ipv6 = proto_register_protocol("Internet Protocol Version 6", "ipv6");
472 proto_register_field_array(proto_ipv6, hf, array_length(hf));
473 proto_register_subtree_array(ett, array_length(ett));
477 proto_reg_handoff_ipv6(void)
479 dissector_add("ethertype", ETHERTYPE_IPv6, dissect_ipv6);
480 dissector_add("ppp.protocol", PPP_IPV6, dissect_ipv6);
481 dissector_add("ip.proto", IP_PROTO_IPV6, dissect_ipv6);