2 * Routines for IPv6 packet disassembly
4 * $Id: packet-ipv6.c,v 1.27 2000/02/15 21:02:21 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>
46 #include "packet-icmpv6.h"
47 #include "packet-ip.h"
48 #include "packet-ipsec.h"
49 #include "packet-ipv6.h"
50 #include "packet-pim.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, offset, len,
94 "Routing Header, Type %d", rt.ip6r_type);
95 rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
97 proto_tree_add_text(rthdr_tree,
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,
101 offset + offsetof(struct ip6_rthdr, ip6r_len), 1,
102 "Length: %d (%d bytes)", rt.ip6r_len, len);
103 proto_tree_add_text(rthdr_tree,
104 offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
105 "Type: %d", rt.ip6r_type, len);
106 proto_tree_add_text(rthdr_tree,
107 offset + offsetof(struct ip6_rthdr, ip6r_segleft), 1,
108 "Segments left: %d", rt.ip6r_segleft, len);
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,
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) {
141 struct ip6_frag frag;
144 memcpy(&frag, (void *) &pd[offset], sizeof(frag));
147 if (check_col(fd, COL_INFO)) {
148 col_add_fstr(fd, COL_INFO,
149 "IPv6 fragment (nxt=%s (0x%02x) off=0x%04x id=0x%x)",
150 ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
151 (frag.ip6f_offlg >> 3) & 0x1fff, frag.ip6f_ident);
157 dissect_opts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
161 proto_tree *dstopt_tree;
164 static const value_string rtalertvals[] = {
165 { IP6OPT_RTALERT_MLD, "MLD" },
166 { IP6OPT_RTALERT_RSVP, "RSVP" },
170 memcpy(&ext, (void *) &pd[offset], sizeof(ext));
171 len = (ext.ip6e_len + 1) << 3;
174 /* !!! specify length */
175 ti = proto_tree_add_text(tree, offset, len,
176 "%s Header", optname);
177 dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
179 proto_tree_add_text(dstopt_tree,
180 offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
181 "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
182 proto_tree_add_text(dstopt_tree,
183 offset + offsetof(struct ip6_ext, ip6e_len), 1,
184 "Length: %d (%d bytes)", ext.ip6e_len, len);
186 p = (u_char *)(pd + offset + 2);
187 while (p < pd + offset + len) {
190 proto_tree_add_text(dstopt_tree, p - pd, 1,
195 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
196 "PadN: %d bytes", p[1] + 2);
202 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
203 "Jumbo payload: %u (%d bytes)",
204 pntohl(&p[2]), p[1] + 2);
206 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
207 "Jumbo payload: Invalid length (%d bytes)",
218 rta = val_to_str(pntohs(&p[2]), rtalertvals,
221 rta = "Invalid length";
222 ti = proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
223 "Router alert: %s (%d bytes)", rta, p[1] + 2);
229 p = (u_char *)(pd + offset + len);
241 dissect_hopopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
242 return dissect_opts(pd, offset, fd, tree, "Hop-by-hop Option");
246 dissect_dstopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
247 return dissect_opts(pd, offset, fd, tree, "Destination Option");
251 dissect_ipv6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
252 proto_tree *ipv6_tree;
260 memcpy(&ipv6, (void *) &pd[offset], sizeof(ipv6));
262 SET_ADDRESS(&pi.net_src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
263 SET_ADDRESS(&pi.src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
264 SET_ADDRESS(&pi.net_dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
265 SET_ADDRESS(&pi.dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
267 if (check_col(fd, COL_PROTOCOL))
268 col_add_str(fd, COL_PROTOCOL, "IPv6");
271 /* !!! specify length */
272 ti = proto_tree_add_item(tree, proto_ipv6, offset, 40, NULL);
273 ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
275 /* !!! warning: version also contains 4 Bit priority */
276 proto_tree_add_item(ipv6_tree, hf_ipv6_version,
277 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
278 (ipv6.ip6_vfc >> 4) & 0x0f);
281 proto_tree_add_item(ipv6_tree, hf_ipv6_class,
282 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
283 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
286 * there should be no alignment problems for ip6_flow, since it's the first
287 * guint32 in the ipv6 struct
289 proto_tree_add_item_format(ipv6_tree, hf_ipv6_flow,
290 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
291 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)),
292 "Flowlabel: 0x%05lx",
293 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)));
295 proto_tree_add_item(ipv6_tree, hf_ipv6_plen,
296 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
297 ntohs(ipv6.ip6_plen));
299 proto_tree_add_item_format(ipv6_tree, hf_ipv6_nxt,
300 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
302 "Next header: %s (0x%02x)",
303 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
305 proto_tree_add_item(ipv6_tree, hf_ipv6_hlim,
306 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
309 proto_tree_add_item_format(ipv6_tree, hf_ipv6_src,
310 offset + offsetof(struct ip6_hdr, ip6_src), 16,
313 "Source address: %s (%s)",
314 get_hostname6(&ipv6.ip6_src),
316 "Source address: %s",
318 ip6_to_str(&ipv6.ip6_src));
320 proto_tree_add_item_format(ipv6_tree, hf_ipv6_dst,
321 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
324 "Destination address: %s (%s)",
325 get_hostname6(&ipv6.ip6_dst),
327 "Destination address: %s",
329 ip6_to_str(&ipv6.ip6_dst));
332 /* start of the new header (could be a extension header) */
333 nxt = pd[poffset = offset + offsetof(struct ip6_hdr, ip6_nxt)];
334 offset += sizeof(struct ip6_hdr);
338 case IP_PROTO_HOPOPTS:
339 advance = dissect_hopopts(pd, offset, fd, tree);
340 nxt = pd[poffset = offset];
344 dissect_ip(pd, offset, fd, tree);
346 case IP_PROTO_ROUTING:
347 advance = dissect_routing6(pd, offset, fd, tree);
348 nxt = pd[poffset = offset];
351 case IP_PROTO_FRAGMENT:
352 advance = dissect_frag6(pd, offset, fd, tree);
353 nxt = pd[poffset = offset];
356 case IP_PROTO_ICMPV6:
358 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
360 dissect_icmpv6(pd, offset, fd, tree);
364 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
366 if (check_col(fd, COL_INFO)) {
367 col_add_fstr(fd, COL_INFO, "IPv6 no next header");
371 advance = dissect_ah(pd, offset, fd, tree);
372 nxt = pd[poffset = offset];
376 dissect_esp(pd, offset, fd, tree);
378 case IP_PROTO_DSTOPTS:
379 advance = dissect_dstopts(pd, offset, fd, tree);
380 nxt = pd[poffset = offset];
385 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
387 dissect_tcp(pd, offset, fd, tree);
391 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
393 dissect_udp(pd, offset, fd, tree);
397 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
399 dissect_pim(pd, offset, fd, tree);
401 case IP_PROTO_IPCOMP:
402 dissect_ipcomp(pd, offset, fd, tree);
406 proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
408 if (check_col(fd, COL_INFO)) {
409 col_add_fstr(fd, COL_INFO, "%s (0x%02x)",
410 ipprotostr(nxt), nxt);
412 dissect_data(pd, offset, fd, tree);
418 proto_register_ipv6(void)
420 static hf_register_info hf[] = {
422 { "Version", "ipv6.version",
423 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
425 { "Traffic class", "ipv6.class",
426 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
428 { "Flowlabel", "ipv6.flow",
429 FT_UINT32, BASE_HEX, NULL, 0x0, "" }},
431 { "Payload length", "ipv6.plen",
432 FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
434 { "Next header", "ipv6.nxt",
435 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
437 { "Hop limit", "ipv6.hlim",
438 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
440 { "Source", "ipv6.src",
441 FT_IPv6, BASE_NONE, NULL, 0x0,
442 "Source IPv6 Address" }},
444 { "Destination", "ipv6.dst",
445 FT_IPv6, BASE_NONE, NULL, 0x0,
446 "Destination IPv6 Address" }},
449 { "Final next header", "ipv6.final",
450 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
453 static gint *ett[] = {
457 proto_ipv6 = proto_register_protocol("Internet Protocol Version 6", "ipv6");
458 proto_register_field_array(proto_ipv6, hf, array_length(hf));
459 proto_register_subtree_array(ett, array_length(ett));