2 * Routines for IPv6 packet disassembly
4 * $Id: packet-ipv6.c,v 1.39 2000/06/05 03:21:03 gram 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-ip.h"
49 #include "packet-ipsec.h"
50 #include "packet-ipv6.h"
51 #include "packet-tcp.h"
52 #include "packet-udp.h"
56 * NOTE: ipv6.nxt is not very useful as we will have chained header.
57 * now testing ipv6.final, but it raises SEGV.
61 static int proto_ipv6 = -1;
62 static int hf_ipv6_version = -1;
63 static int hf_ipv6_class = -1;
64 static int hf_ipv6_flow = -1;
65 static int hf_ipv6_plen = -1;
66 static int hf_ipv6_nxt = -1;
67 static int hf_ipv6_hlim = -1;
68 static int hf_ipv6_src = -1;
69 static int hf_ipv6_dst = -1;
71 static int hf_ipv6_final = -1;
74 static gint ett_ipv6 = -1;
77 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
81 dissect_routing6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
84 proto_tree *rthdr_tree;
86 char buf[sizeof(struct ip6_rthdr0) + sizeof(struct e_in6_addr) * 23];
88 memcpy(&rt, (void *) &pd[offset], sizeof(rt));
89 len = (rt.ip6r_len + 1) << 3;
92 /* !!! specify length */
93 ti = proto_tree_add_text(tree, NullTVB, offset, len,
94 "Routing Header, Type %u", rt.ip6r_type);
95 rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
97 proto_tree_add_text(rthdr_tree, NullTVB,
98 offset + offsetof(struct ip6_rthdr, ip6r_nxt), 1,
99 "Next header: %s (0x%02x)", ipprotostr(rt.ip6r_nxt), rt.ip6r_nxt);
100 proto_tree_add_text(rthdr_tree, NullTVB,
101 offset + offsetof(struct ip6_rthdr, ip6r_len), 1,
102 "Length: %u (%d bytes)", rt.ip6r_len, len);
103 proto_tree_add_text(rthdr_tree, NullTVB,
104 offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
105 "Type: %u", rt.ip6r_type);
106 proto_tree_add_text(rthdr_tree, NullTVB,
107 offset + offsetof(struct ip6_rthdr, ip6r_segleft), 1,
108 "Segments left: %u", rt.ip6r_segleft);
110 if (rt.ip6r_type == 0 && len <= sizeof(buf)) {
111 struct e_in6_addr *a;
113 struct ip6_rthdr0 *rt0;
115 memcpy(buf, (void *) &pd[offset], len);
116 rt0 = (struct ip6_rthdr0 *)buf;
117 for (a = rt0->ip6r0_addr, n = 0;
118 a < (struct e_in6_addr *)(buf + len);
120 proto_tree_add_text(rthdr_tree, NullTVB,
121 offset + offsetof(struct ip6_rthdr0, ip6r0_addr) + n * sizeof(struct e_in6_addr),
122 sizeof(struct e_in6_addr),
124 "address %d: %s (%s)",
125 n, get_hostname6(a), ip6_to_str(a)
127 "address %d: %s", n, ip6_to_str(a)
140 dissect_frag6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
142 struct ip6_frag frag;
145 memcpy(&frag, (void *) &pd[offset], sizeof(frag));
147 *fragstart = ntohs(frag.ip6f_offlg) & 0xfff8;
148 if (check_col(fd, COL_INFO)) {
149 col_add_fstr(fd, COL_INFO,
150 "IPv6 fragment (nxt=%s (0x%02x) off=%u id=0x%x)",
151 ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
152 *fragstart, frag.ip6f_ident);
158 dissect_opts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
162 proto_tree *dstopt_tree;
165 static const value_string rtalertvals[] = {
166 { IP6OPT_RTALERT_MLD, "MLD" },
167 { IP6OPT_RTALERT_RSVP, "RSVP" },
171 memcpy(&ext, (void *) &pd[offset], sizeof(ext));
172 len = (ext.ip6e_len + 1) << 3;
175 /* !!! specify length */
176 ti = proto_tree_add_text(tree, NullTVB, offset, len,
177 "%s Header", optname);
178 dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
180 proto_tree_add_text(dstopt_tree, NullTVB,
181 offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
182 "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
183 proto_tree_add_text(dstopt_tree, NullTVB,
184 offset + offsetof(struct ip6_ext, ip6e_len), 1,
185 "Length: %u (%d bytes)", ext.ip6e_len, len);
187 p = (u_char *)(pd + offset + 2);
188 while (p < pd + offset + len) {
191 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, 1,
196 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
197 "PadN: %u bytes", p[1] + 2);
203 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
204 "Jumbo payload: %u (%u bytes)",
205 pntohl(&p[2]), p[1] + 2);
207 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
208 "Jumbo payload: Invalid length (%u bytes)",
219 rta = val_to_str(pntohs(&p[2]), rtalertvals,
222 rta = "Invalid length";
223 ti = proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
224 "Router alert: %s (%u bytes)", rta, p[1] + 2);
230 p = (u_char *)(pd + offset + len);
242 dissect_hopopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
243 return dissect_opts(pd, offset, fd, tree, "Hop-by-hop Option");
247 dissect_dstopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
248 return dissect_opts(pd, offset, fd, tree, "Destination Option");
252 dissect_ipv6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
253 proto_tree *ipv6_tree;
262 memcpy(&ipv6, (void *) &pd[offset], sizeof(ipv6));
264 SET_ADDRESS(&pi.net_src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
265 SET_ADDRESS(&pi.src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
266 SET_ADDRESS(&pi.net_dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
267 SET_ADDRESS(&pi.dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
270 /* !!! specify length */
271 ti = proto_tree_add_item(tree, proto_ipv6, NullTVB, offset, 40, FALSE);
272 ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
274 /* !!! warning: version also contains 4 Bit priority */
275 proto_tree_add_uint(ipv6_tree, hf_ipv6_version, NullTVB,
276 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
277 (ipv6.ip6_vfc >> 4) & 0x0f);
280 proto_tree_add_uint(ipv6_tree, hf_ipv6_class, NullTVB,
281 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
282 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
285 * there should be no alignment problems for ip6_flow, since it's the first
286 * guint32 in the ipv6 struct
288 proto_tree_add_uint_format(ipv6_tree, hf_ipv6_flow, NullTVB,
289 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
290 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)),
291 "Flowlabel: 0x%05lx",
292 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)));
294 proto_tree_add_uint(ipv6_tree, hf_ipv6_plen, NullTVB,
295 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
296 ntohs(ipv6.ip6_plen));
298 proto_tree_add_uint_format(ipv6_tree, hf_ipv6_nxt, NullTVB,
299 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
301 "Next header: %s (0x%02x)",
302 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
304 proto_tree_add_uint(ipv6_tree, hf_ipv6_hlim, NullTVB,
305 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
308 proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_src, NullTVB,
309 offset + offsetof(struct ip6_hdr, ip6_src), 16,
310 (guint8 *)&ipv6.ip6_src,
312 "Source address: %s (%s)",
313 get_hostname6(&ipv6.ip6_src),
315 "Source address: %s",
317 ip6_to_str(&ipv6.ip6_src));
319 proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_dst, NullTVB,
320 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
321 (guint8 *)&ipv6.ip6_dst,
323 "Destination address: %s (%s)",
324 get_hostname6(&ipv6.ip6_dst),
326 "Destination address: %s",
328 ip6_to_str(&ipv6.ip6_dst));
331 /* start of the new header (could be a extension header) */
332 nxt = pd[poffset = offset + offsetof(struct ip6_hdr, ip6_nxt)];
333 offset += sizeof(struct ip6_hdr);
338 case IP_PROTO_HOPOPTS:
339 advance = dissect_hopopts(pd, offset, fd, tree);
340 nxt = pd[poffset = offset];
343 case IP_PROTO_ROUTING:
344 advance = dissect_routing6(pd, offset, fd, tree);
345 nxt = pd[poffset = offset];
348 case IP_PROTO_FRAGMENT:
349 advance = dissect_frag6(pd, offset, fd, tree, &frag);
350 nxt = pd[poffset = offset];
354 advance = dissect_ah_old(pd, offset, fd, tree);
355 nxt = pd[poffset = offset];
358 case IP_PROTO_DSTOPTS:
359 advance = dissect_dstopts(pd, offset, fd, tree);
360 nxt = pd[poffset = offset];
366 proto_tree_add_uint_hidden(ipv6_tree, hf_ipv6_final, NullTVB, poffset, 1, nxt);
370 if (check_col(fd, COL_PROTOCOL))
371 col_add_str(fd, COL_PROTOCOL, "IPv6");
372 /* COL_INFO was filled in by "dissect_frag6()" */
373 dissect_data(pd, offset, fd, tree);
375 /* do lookup with the subdissector table */
376 if (!dissector_try_port(ip_dissector_table, nxt, pd, offset, fd, tree)) {
377 /* Unknown protocol */
378 if (check_col(fd, COL_PROTOCOL))
379 col_add_str(fd, COL_PROTOCOL, "IPv6");
380 if (check_col(fd, COL_INFO))
381 col_add_fstr(fd, COL_INFO, "%s (0x%02x)", ipprotostr(nxt), nxt);
382 dissect_data(pd, offset, fd, tree);
388 dissect_ipv6_none(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
389 if (check_col(fd, COL_INFO))
390 col_add_fstr(fd, COL_INFO, "IPv6 no next header");
392 /* XXX - dissect the payload as padding? */
396 proto_register_ipv6(void)
398 static hf_register_info hf[] = {
400 { "Version", "ipv6.version",
401 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
403 { "Traffic class", "ipv6.class",
404 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
406 { "Flowlabel", "ipv6.flow",
407 FT_UINT32, BASE_HEX, NULL, 0x0, "" }},
409 { "Payload length", "ipv6.plen",
410 FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
412 { "Next header", "ipv6.nxt",
413 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
415 { "Hop limit", "ipv6.hlim",
416 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
418 { "Source", "ipv6.src",
419 FT_IPv6, BASE_NONE, NULL, 0x0,
420 "Source IPv6 Address" }},
422 { "Destination", "ipv6.dst",
423 FT_IPv6, BASE_NONE, NULL, 0x0,
424 "Destination IPv6 Address" }},
427 { "Final next header", "ipv6.final",
428 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
431 static gint *ett[] = {
435 proto_ipv6 = proto_register_protocol("Internet Protocol Version 6", "ipv6");
436 proto_register_field_array(proto_ipv6, hf, array_length(hf));
437 proto_register_subtree_array(ett, array_length(ett));
441 proto_reg_handoff_ipv6(void)
443 dissector_add("ethertype", ETHERTYPE_IPv6, dissect_ipv6);
444 dissector_add("ppp.protocol", PPP_IPV6, dissect_ipv6);
445 dissector_add("ip.proto", IP_PROTO_IPV6, dissect_ipv6);
446 dissector_add("ip.proto", IP_PROTO_NONE, dissect_ipv6_none);