2 * Routines for IPv6 packet disassembly
4 * $Id: packet-ipv6.c,v 1.25 1999/11/16 11:42:36 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>
46 #include "packet-ip.h"
47 #include "packet-ipv6.h"
51 * NOTE: ipv6.nxt is not very useful as we will have chained header.
52 * now testing ipv6.final, but it raises SEGV.
56 static int proto_ipv6 = -1;
57 static int hf_ipv6_version = -1;
58 static int hf_ipv6_class = -1;
59 static int hf_ipv6_flow = -1;
60 static int hf_ipv6_plen = -1;
61 static int hf_ipv6_nxt = -1;
62 static int hf_ipv6_hlim = -1;
63 static int hf_ipv6_src = -1;
64 static int hf_ipv6_dst = -1;
66 static int hf_ipv6_final = -1;
69 static gint ett_ipv6 = -1;
72 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
76 dissect_routing6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
79 proto_tree *rthdr_tree;
81 char buf[sizeof(struct ip6_rthdr0) + sizeof(struct e_in6_addr) * 23];
83 memcpy(&rt, (void *) &pd[offset], sizeof(rt));
84 len = (rt.ip6r_len + 1) << 3;
87 /* !!! specify length */
88 ti = proto_tree_add_text(tree, offset, len,
89 "Routing Header, Type %d", rt.ip6r_type);
90 rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
92 proto_tree_add_text(rthdr_tree,
93 offset + offsetof(struct ip6_rthdr, ip6r_nxt), 1,
94 "Next header: %s (0x%02x)", ipprotostr(rt.ip6r_nxt), rt.ip6r_nxt);
95 proto_tree_add_text(rthdr_tree,
96 offset + offsetof(struct ip6_rthdr, ip6r_len), 1,
97 "Length: %d (%d bytes)", rt.ip6r_len, len);
98 proto_tree_add_text(rthdr_tree,
99 offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
100 "Type: %d", rt.ip6r_type, len);
101 proto_tree_add_text(rthdr_tree,
102 offset + offsetof(struct ip6_rthdr, ip6r_segleft), 1,
103 "Segments left: %d", rt.ip6r_segleft, len);
105 if (rt.ip6r_type == 0 && len <= sizeof(buf)) {
106 struct e_in6_addr *a;
108 struct ip6_rthdr0 *rt0;
110 memcpy(buf, (void *) &pd[offset], len);
111 rt0 = (struct ip6_rthdr0 *)buf;
112 for (a = rt0->ip6r0_addr, n = 0;
113 a < (struct e_in6_addr *)(buf + len);
115 proto_tree_add_text(rthdr_tree,
116 offset + offsetof(struct ip6_rthdr0, ip6r0_addr) + n * sizeof(struct e_in6_addr),
117 sizeof(struct e_in6_addr),
119 "address %d: %s (%s)",
120 n, get_hostname6(a), ip6_to_str(a)
122 "address %d: %s", n, ip6_to_str(a)
135 dissect_frag6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
136 struct ip6_frag frag;
139 memcpy(&frag, (void *) &pd[offset], sizeof(frag));
142 if (check_col(fd, COL_INFO)) {
143 col_add_fstr(fd, COL_INFO,
144 "IPv6 fragment (nxt=%s (0x%02x) off=0x%04x id=0x%x)",
145 ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
146 (frag.ip6f_offlg >> 3) & 0x1fff, frag.ip6f_ident);
152 dissect_opts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
156 proto_tree *dstopt_tree;
159 static const value_string rtalertvals[] = {
160 { IP6OPT_RTALERT_MLD, "MLD" },
161 { IP6OPT_RTALERT_RSVP, "RSVP" },
165 memcpy(&ext, (void *) &pd[offset], sizeof(ext));
166 len = (ext.ip6e_len + 1) << 3;
169 /* !!! specify length */
170 ti = proto_tree_add_text(tree, offset, len,
171 "%s Header", optname);
172 dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
174 proto_tree_add_text(dstopt_tree,
175 offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
176 "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
177 proto_tree_add_text(dstopt_tree,
178 offset + offsetof(struct ip6_ext, ip6e_len), 1,
179 "Length: %d (%d bytes)", ext.ip6e_len, len);
181 p = (u_char *)(pd + offset + 2);
182 while (p < pd + offset + len) {
185 proto_tree_add_text(dstopt_tree, p - pd, 1,
190 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
191 "PadN: %d bytes", p[1] + 2);
197 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
198 "Jumbo payload: %u (%d bytes)",
199 ntohl(*(guint32 *)&p[2]), p[1] + 2);
201 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
202 "Jumbo payload: Invalid length (%d bytes)",
213 rta = val_to_str(ntohs(*(guint16 *)&p[2]), rtalertvals,
216 rta = "Invalid length";
217 ti = proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
218 "Router alert: %s (%d bytes)", rta, p[1] + 2);
224 p = (u_char *)(pd + offset + len);
236 dissect_hopopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
237 return dissect_opts(pd, offset, fd, tree, "Hop-by-hop Option");
241 dissect_dstopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
242 return dissect_opts(pd, offset, fd, tree, "Destination Option");
246 dissect_ipv6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
247 proto_tree *ipv6_tree;
255 memcpy(&ipv6, (void *) &pd[offset], sizeof(ipv6));
257 SET_ADDRESS(&pi.net_src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
258 SET_ADDRESS(&pi.src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
259 SET_ADDRESS(&pi.net_dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
260 SET_ADDRESS(&pi.dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
262 if (check_col(fd, COL_PROTOCOL))
263 col_add_str(fd, COL_PROTOCOL, "IPv6");
266 /* !!! specify length */
267 ti = proto_tree_add_item(tree, proto_ipv6, offset, 40, NULL);
268 ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
270 /* !!! warning: version also contains 4 Bit priority */
271 proto_tree_add_item(ipv6_tree, hf_ipv6_version,
272 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
273 (ipv6.ip6_vfc >> 4) & 0x0f);
276 proto_tree_add_item(ipv6_tree, hf_ipv6_class,
277 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
278 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
281 * there should be no alignment problems for ip6_flow, since it's the first
282 * guint32 in the ipv6 struct
284 proto_tree_add_item_format(ipv6_tree, hf_ipv6_flow,
285 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
286 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)),
287 "Flowlabel: 0x%05lx",
288 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)));
290 proto_tree_add_item(ipv6_tree, hf_ipv6_plen,
291 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
292 ntohs(ipv6.ip6_plen));
294 proto_tree_add_item_format(ipv6_tree, hf_ipv6_nxt,
295 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
297 "Next header: %s (0x%02x)",
298 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
300 proto_tree_add_item(ipv6_tree, hf_ipv6_hlim,
301 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
304 proto_tree_add_item_format(ipv6_tree, hf_ipv6_src,
305 offset + offsetof(struct ip6_hdr, ip6_src), 16,
308 "Source address: %s (%s)",
309 get_hostname6(&ipv6.ip6_src),
311 "Source address: %s",
313 ip6_to_str(&ipv6.ip6_src));
315 proto_tree_add_item_format(ipv6_tree, hf_ipv6_dst,
316 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
319 "Destination address: %s (%s)",
320 get_hostname6(&ipv6.ip6_dst),
322 "Destination address: %s",
324 ip6_to_str(&ipv6.ip6_dst));
327 /* start of the new header (could be a extension header) */
328 nxt = pd[poffset = offset + offsetof(struct ip6_hdr, ip6_nxt)];
329 offset += sizeof(struct ip6_hdr);
333 case IP_PROTO_HOPOPTS:
334 advance = dissect_hopopts(pd, offset, fd, tree);
335 nxt = pd[poffset = offset];
339 dissect_ip(pd, offset, fd, tree);
341 case IP_PROTO_ROUTING:
342 advance = dissect_routing6(pd, offset, fd, tree);
343 nxt = pd[poffset = offset];
346 case IP_PROTO_FRAGMENT:
347 advance = dissect_frag6(pd, offset, fd, tree);
348 nxt = pd[poffset = offset];
351 case IP_PROTO_ICMPV6:
353 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
355 dissect_icmpv6(pd, offset, fd, tree);
359 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
361 if (check_col(fd, COL_INFO)) {
362 col_add_fstr(fd, COL_INFO, "IPv6 no next header");
366 advance = dissect_ah(pd, offset, fd, tree);
367 nxt = pd[poffset = offset];
371 dissect_esp(pd, offset, fd, tree);
373 case IP_PROTO_DSTOPTS:
374 advance = dissect_dstopts(pd, offset, fd, tree);
375 nxt = pd[poffset = offset];
380 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
382 dissect_tcp(pd, offset, fd, tree);
386 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
388 dissect_udp(pd, offset, fd, tree);
392 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
394 dissect_pim(pd, offset, fd, tree);
396 case IP_PROTO_IPCOMP:
397 dissect_ipcomp(pd, offset, fd, tree);
401 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
403 if (check_col(fd, COL_INFO)) {
404 col_add_fstr(fd, COL_INFO, "%s (0x%02x)",
405 ipprotostr(nxt), nxt);
407 dissect_data(pd, offset, fd, tree);
413 proto_register_ipv6(void)
415 static hf_register_info hf[] = {
417 { "Version", "ipv6.version",
418 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
420 { "Traffic class", "ipv6.class",
421 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
423 { "Flowlabel", "ipv6.flow",
424 FT_UINT32, BASE_HEX, NULL, 0x0, "" }},
426 { "Payload length", "ipv6.plen",
427 FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
429 { "Next header", "ipv6.nxt",
430 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
432 { "Hop limit", "ipv6.hlim",
433 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
435 { "Source", "ipv6.src",
436 FT_IPv6, BASE_NONE, NULL, 0x0,
437 "Source IPv6 Address" }},
439 { "Destination", "ipv6.dst",
440 FT_IPv6, BASE_NONE, NULL, 0x0,
441 "Destination IPv6 Address" }},
444 { "Final next header", "ipv6.final",
445 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
448 static gint *ett[] = {
452 proto_ipv6 = proto_register_protocol("Internet Protocol Version 6", "ipv6");
453 proto_register_field_array(proto_ipv6, hf, array_length(hf));
454 proto_register_subtree_array(ett, array_length(ett));