warning: overflow in implicit constant conversion
[obnox/wireshark/wip.git] / packet-ipv6.c
1 /* packet-ipv6.c
2  * Routines for IPv6 packet disassembly
3  *
4  * $Id: packet-ipv6.c,v 1.100 2003/08/28 04:19:28 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * MobileIPv6 support added by Tomislav Borosa <tomislav.borosa@siemens.hr>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <string.h>
32 #include <stdio.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include "packet-ipsec.h"
36 #include "packet-ipv6.h"
37 #include "ip_opts.h"
38 #include <epan/resolv.h>
39 #include "prefs.h"
40 #include "reassemble.h"
41 #include "ipproto.h"
42 #include "etypes.h"
43 #include "ppptypes.h"
44 #include "aftypes.h"
45 #include "nlpid.h"
46 #include "arcnet_pids.h"
47
48 /*
49  * NOTE: ipv6.nxt is not very useful as we will have chained header.
50  * now testing ipv6.final, but it raises SEGV.
51 #define TEST_FINALHDR
52  */
53
54 static int proto_ipv6 = -1;
55 static int hf_ipv6_version = -1;
56 static int hf_ipv6_class = -1;
57 static int hf_ipv6_flow = -1;
58 static int hf_ipv6_plen = -1;
59 static int hf_ipv6_nxt = -1;
60 static int hf_ipv6_hlim = -1;
61 static int hf_ipv6_src = -1;
62 static int hf_ipv6_dst = -1;
63 static int hf_ipv6_addr = -1;
64 #ifdef TEST_FINALHDR
65 static int hf_ipv6_final = -1;
66 #endif
67 static int hf_ipv6_fragments = -1;
68 static int hf_ipv6_fragment = -1;
69 static int hf_ipv6_fragment_overlap = -1;
70 static int hf_ipv6_fragment_overlap_conflict = -1;
71 static int hf_ipv6_fragment_multiple_tails = -1;
72 static int hf_ipv6_fragment_too_long_fragment = -1;
73 static int hf_ipv6_fragment_error = -1;
74 static int hf_ipv6_reassembled_in = -1;
75
76 static int hf_ipv6_mipv6_type = -1;
77 static int hf_ipv6_mipv6_length = -1;
78 static int hf_ipv6_mipv6_home_address = -1;
79
80 static gint ett_ipv6 = -1;
81 static gint ett_ipv6_fragments = -1;
82 static gint ett_ipv6_fragment  = -1;
83
84 static const fragment_items ipv6_frag_items = {
85         &ett_ipv6_fragment,
86         &ett_ipv6_fragments,
87         &hf_ipv6_fragments,
88         &hf_ipv6_fragment,
89         &hf_ipv6_fragment_overlap,
90         &hf_ipv6_fragment_overlap_conflict,
91         &hf_ipv6_fragment_multiple_tails,
92         &hf_ipv6_fragment_too_long_fragment,
93         &hf_ipv6_fragment_error,
94         &hf_ipv6_reassembled_in,
95         "fragments"
96 };
97
98 static dissector_handle_t data_handle;
99
100 static dissector_table_t ip_dissector_table;
101
102 /* Reassemble fragmented datagrams */
103 static gboolean ipv6_reassemble = FALSE;
104
105 #ifndef offsetof
106 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
107 #endif
108
109 /*
110  * defragmentation of IPv6
111  */
112 static GHashTable *ipv6_fragment_table = NULL;
113 static GHashTable *ipv6_reassembled_table = NULL;
114
115 void
116 capture_ipv6(const guchar *pd, int offset, int len, packet_counts *ld)
117 {
118   guint8 nxt;
119   int advance;
120
121   if (!BYTES_ARE_IN_FRAME(offset, len, 4+4+16+16)) {
122     ld->other++;
123     return;
124   }
125   nxt = pd[offset+6];           /* get the "next header" value */
126   offset += 4+4+16+16;          /* skip past the IPv6 header */
127   len -= 4+4+16+16;
128
129 again:
130    switch (nxt) {
131    case IP_PROTO_HOPOPTS:
132    case IP_PROTO_ROUTING:
133    case IP_PROTO_DSTOPTS:
134      if (!BYTES_ARE_IN_FRAME(offset, len, 2)) {
135        ld->other++;
136        return;
137      }
138      nxt = pd[offset];
139      advance = (pd[offset+1] + 1) << 3;
140      if (!BYTES_ARE_IN_FRAME(offset, len, advance)) {
141        ld->other++;
142        return;
143      }
144      offset += advance;
145      len -= advance;
146      goto again;
147    case IP_PROTO_FRAGMENT:
148      if (!BYTES_ARE_IN_FRAME(offset, len, 2)) {
149        ld->other++;
150        return;
151      }
152      nxt = pd[offset];
153      advance = 8;
154      if (!BYTES_ARE_IN_FRAME(offset, len, advance)) {
155        ld->other++;
156        return;
157      }
158      offset += advance;
159      len -= advance;
160      goto again;
161    case IP_PROTO_AH:
162      if (!BYTES_ARE_IN_FRAME(offset, len, 2)) {
163        ld->other++;
164        return;
165      }
166      nxt = pd[offset];
167      advance = 8 + ((pd[offset+1] - 1) << 2);
168      if (!BYTES_ARE_IN_FRAME(offset, len, advance)) {
169        ld->other++;
170        return;
171      }
172      offset += advance;
173      len -= advance;
174      goto again;
175    }
176
177   switch(nxt) {
178     case IP_PROTO_SCTP:
179       ld->sctp++;
180       break;
181     case IP_PROTO_TCP:
182       ld->tcp++;
183       break;
184     case IP_PROTO_UDP:
185       ld->udp++;
186       break;
187     case IP_PROTO_ICMP:
188     case IP_PROTO_ICMPV6:       /* XXX - separate counters? */
189       ld->icmp++;
190       break;
191     case IP_PROTO_OSPF:
192       ld->ospf++;
193       break;
194     case IP_PROTO_GRE:
195       ld->gre++;
196       break;
197     case IP_PROTO_VINES:
198       ld->vines++;
199       break;
200     default:
201       ld->other++;
202   }
203 }
204
205 static void
206 ipv6_reassemble_init(void)
207 {
208   fragment_table_init(&ipv6_fragment_table);
209   reassembled_table_init(&ipv6_reassembled_table);
210 }
211
212 static int
213 dissect_routing6(tvbuff_t *tvb, int offset, proto_tree *tree) {
214     struct ip6_rthdr rt;
215     guint len;
216     proto_tree *rthdr_tree;
217         proto_item *ti;
218     char buf[sizeof(struct ip6_rthdr0) + sizeof(struct e_in6_addr) * 23];
219
220     tvb_memcpy(tvb, (guint8 *)&rt, offset, sizeof(rt));
221     len = (rt.ip6r_len + 1) << 3;
222
223     if (tree) {
224         /* !!! specify length */
225         ti = proto_tree_add_text(tree, tvb, offset, len,
226             "Routing Header, Type %u", rt.ip6r_type);
227         rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
228
229         proto_tree_add_text(rthdr_tree, tvb,
230             offset + offsetof(struct ip6_rthdr, ip6r_nxt), 1,
231             "Next header: %s (0x%02x)", ipprotostr(rt.ip6r_nxt), rt.ip6r_nxt);
232         proto_tree_add_text(rthdr_tree, tvb,
233             offset + offsetof(struct ip6_rthdr, ip6r_len), 1,
234             "Length: %u (%d bytes)", rt.ip6r_len, len);
235         proto_tree_add_text(rthdr_tree, tvb,
236             offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
237             "Type: %u", rt.ip6r_type);
238         proto_tree_add_text(rthdr_tree, tvb,
239             offset + offsetof(struct ip6_rthdr, ip6r_segleft), 1,
240             "Segments left: %u", rt.ip6r_segleft);
241
242         if (rt.ip6r_type == 0 && len <= sizeof(buf)) {
243             struct e_in6_addr *a;
244             int n;
245             struct ip6_rthdr0 *rt0;
246
247             tvb_memcpy(tvb, buf, offset, len);
248             rt0 = (struct ip6_rthdr0 *)buf;
249             for (a = rt0->ip6r0_addr, n = 0;
250                  a < (struct e_in6_addr *)(buf + len);
251                  a++, n++) {
252                 proto_tree_add_text(rthdr_tree, tvb,
253                     offset + offsetof(struct ip6_rthdr0, ip6r0_addr) + n * sizeof(struct e_in6_addr),
254                     sizeof(struct e_in6_addr),
255 #ifdef INET6
256                     "address %d: %s (%s)",
257                     n, get_hostname6(a), ip6_to_str(a)
258 #else
259                     "address %d: %s", n, ip6_to_str(a)
260 #endif
261                     );
262             }
263         }
264         if (rt.ip6r_type == 2) {
265             proto_tree_add_ipv6(rthdr_tree, hf_ipv6_mipv6_home_address,
266                                        tvb, offset + 8, 16,
267                                        tvb_get_ptr(tvb, offset + 8, 16));
268         }
269     }
270
271     return len;
272 }
273
274 static int
275 dissect_frag6(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree,
276     guint16 *offlg, guint32 *ident) {
277     struct ip6_frag frag;
278     int len;
279     proto_item *ti;
280     proto_tree *rthdr_tree;
281
282     tvb_memcpy(tvb, (guint8 *)&frag, offset, sizeof(frag));
283     len = sizeof(frag);
284     frag.ip6f_offlg = g_ntohs(frag.ip6f_offlg);
285     *offlg = frag.ip6f_offlg;
286     *ident = frag.ip6f_ident;
287     if (check_col(pinfo->cinfo, COL_INFO)) {
288         col_add_fstr(pinfo->cinfo, COL_INFO,
289             "IPv6 fragment (nxt=%s (0x%02x) off=%u id=0x%x)",
290             ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
291             frag.ip6f_offlg & IP6F_OFF_MASK, frag.ip6f_ident);
292     }
293     if (tree) {
294            ti = proto_tree_add_text(tree, tvb, offset, len,
295                            "Fragmentation Header");
296            rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
297
298            proto_tree_add_text(rthdr_tree, tvb,
299                          offset + offsetof(struct ip6_frag, ip6f_nxt), 1,
300                          "Next header: %s (0x%02x)",
301                          ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt);
302
303 #if 0
304            proto_tree_add_text(rthdr_tree, tvb,
305                          offset + offsetof(struct ip6_frag, ip6f_reserved), 1,
306                          "Reserved: %u",
307                          frag.ip6f_reserved);
308 #endif
309
310            proto_tree_add_text(rthdr_tree, tvb,
311                          offset + offsetof(struct ip6_frag, ip6f_offlg), 2,
312                          "Offset: %u",
313                          frag.ip6f_offlg & IP6F_OFF_MASK);
314
315            proto_tree_add_text(rthdr_tree, tvb,
316                          offset + offsetof(struct ip6_frag, ip6f_offlg), 2,
317                          "More fragments: %s",
318                                 frag.ip6f_offlg & IP6F_MORE_FRAG ?
319                                 "Yes" : "No");
320
321            proto_tree_add_text(rthdr_tree, tvb,
322                          offset + offsetof(struct ip6_frag, ip6f_ident), 4,
323                          "Identification: 0x%08x",
324                          frag.ip6f_ident);
325     }
326     return len;
327 }
328
329 static int
330 dissect_mipv6_hoa(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset)
331 {
332     int len = 0;
333
334     proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_type, tvb,
335         offset + len, 1,
336         tvb_get_guint8(tvb, offset + len),
337         "Option Type: %u (0x%02x) - Home Address Option",
338         tvb_get_guint8(tvb, offset + len),
339         tvb_get_guint8(tvb, offset + len));
340     len += 1;
341
342     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_length, tvb, offset + len,
343         1, tvb_get_guint8(tvb, offset + len));
344     len += 1;
345
346     proto_tree_add_ipv6(dstopt_tree, hf_ipv6_mipv6_home_address, tvb,
347         offset + len, 16, tvb_get_ptr(tvb, offset + len, 16));
348     len += 16;
349     return len;
350 }
351
352 static const value_string rtalertvals[] = {
353     { IP6OPT_RTALERT_MLD, "MLD" },
354     { IP6OPT_RTALERT_RSVP, "RSVP" },
355     { 0, NULL },
356 };
357
358 /* Like "dissect_ip_tcp_options()", but assumes the length of an option
359    *doesn't* include the type and length bytes. */
360 void
361 dissect_ipv6_options(tvbuff_t *tvb, int offset, guint length,
362                         const ip_tcp_opt *opttab, int nopts, int eol,
363                         packet_info *pinfo, proto_tree *opt_tree)
364 {
365   guchar            opt;
366   const ip_tcp_opt *optp;
367   opt_len_type      len_type;
368   unsigned int      optlen;
369   char             *name;
370   char              name_str[7+1+1+2+2+1+1];    /* "Unknown (0x%02x)" */
371   void            (*dissect)(const struct ip_tcp_opt *, tvbuff_t *,
372                                 int, guint, packet_info *, proto_tree *);
373   guint             len;
374
375   while (length > 0) {
376     opt = tvb_get_guint8(tvb, offset);
377     for (optp = &opttab[0]; optp < &opttab[nopts]; optp++) {
378       if (optp->optcode == opt)
379         break;
380     }
381     if (optp == &opttab[nopts]) {
382       /* We assume that the only NO_LENGTH options are Pad1 options,
383          so that we can treat unknown options as VARIABLE_LENGTH with a
384          minimum of 0, and at least be able to move on to the next option
385          by using the length in the option. */
386       optp = NULL;      /* indicate that we don't know this option */
387       len_type = VARIABLE_LENGTH;
388       optlen = 0;
389       snprintf(name_str, sizeof name_str, "Unknown (0x%02x)", opt);
390       name = name_str;
391       dissect = NULL;
392     } else {
393       len_type = optp->len_type;
394       optlen = optp->optlen;
395       name = optp->name;
396       dissect = optp->dissect;
397     }
398     --length;      /* account for type byte */
399     if (len_type != NO_LENGTH) {
400       /* Option has a length. Is it in the packet? */
401       if (length == 0) {
402         /* Bogus - packet must at least include option code byte and
403            length byte! */
404         proto_tree_add_text(opt_tree, tvb, offset,      1,
405               "%s (length byte past end of options)", name);
406         return;
407       }
408       len = tvb_get_guint8(tvb, offset + 1);  /* total including type, len */
409       --length;    /* account for length byte */
410       if (len > length) {
411         /* Bogus - option goes past the end of the header. */
412         proto_tree_add_text(opt_tree, tvb, offset,      length,
413               "%s (option length = %u byte%s says option goes past end of options)",
414               name, len, plurality(len, "", "s"));
415         return;
416       } else if (len_type == FIXED_LENGTH && len != optlen) {
417         /* Bogus - option length isn't what it's supposed to be for this
418            option. */
419         proto_tree_add_text(opt_tree, tvb, offset,      2 + len,
420               "%s (with option length = %u byte%s; should be %u)", name,
421               len, plurality(len, "", "s"), optlen);
422         return;
423       } else if (len_type == VARIABLE_LENGTH && len < optlen) {
424         /* Bogus - option length is less than what it's supposed to be for
425            this option. */
426         proto_tree_add_text(opt_tree, tvb, offset,      2 + len,
427               "%s (with option length = %u byte%s; should be >= %u)", name,
428               len, plurality(len, "", "s"), optlen);
429         return;
430       } else {
431         if (optp == NULL) {
432           proto_tree_add_text(opt_tree, tvb, offset,    2 + len, "%s (%u byte%s)",
433                                 name, len, plurality(len, "", "s"));
434         } else {
435           if (dissect != NULL) {
436             /* Option has a dissector. */
437             (*dissect)(optp, tvb, offset,          2 + len, pinfo, opt_tree);
438           } else {
439             /* Option has no data, hence no dissector. */
440             proto_tree_add_text(opt_tree, tvb, offset,  2 + len, "%s", name);
441           }
442         }
443         offset += 2 + len;
444       }
445       length -= len;
446     } else {
447       proto_tree_add_text(opt_tree, tvb, offset,      1, "%s", name);
448       offset += 1;
449     }
450     if (opt == eol)
451       break;
452   }
453 }
454
455 static int
456 dissect_opts(tvbuff_t *tvb, int offset, proto_tree *tree, char *optname)
457 {
458     struct ip6_ext ext;
459     int len;
460     proto_tree *dstopt_tree;
461     proto_item *ti;
462     gint p;
463     guint8 tmp;
464     int mip_offset = 0, delta = 0;
465
466     tvb_memcpy(tvb, (guint8 *)&ext, offset, sizeof(ext));
467     len = (ext.ip6e_len + 1) << 3;
468
469     if (tree) {
470         /* !!! specify length */
471         ti = proto_tree_add_text(tree, tvb, offset, len, "%s Header ", optname);
472
473         dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
474
475         proto_tree_add_text(dstopt_tree, tvb,
476             offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
477             "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
478         proto_tree_add_text(dstopt_tree, tvb,
479             offset + offsetof(struct ip6_ext, ip6e_len), 1,
480             "Length: %u (%d bytes)", ext.ip6e_len, len);
481
482         mip_offset = offset;
483         mip_offset += 2;
484
485         p = offset + 2;
486
487         while (p < offset + len) {
488             switch (tvb_get_guint8(tvb, p)) {
489             case IP6OPT_PAD1:
490                 proto_tree_add_text(dstopt_tree, tvb, p, 1, "Pad1");
491                 p++;
492                 mip_offset++;
493                 break;
494             case IP6OPT_PADN:
495                 tmp = tvb_get_guint8(tvb, p + 1);
496                 proto_tree_add_text(dstopt_tree, tvb, p, tmp + 2,
497                     "PadN: %u bytes", tmp + 2);
498                 p += tmp;
499                 p += 2;
500                 mip_offset += tvb_get_guint8(tvb, mip_offset + 1) + 2;
501                 break;
502             case IP6OPT_JUMBO:
503                 tmp = tvb_get_guint8(tvb, p + 1);
504                 if (tmp == 4) {
505                     proto_tree_add_text(dstopt_tree, tvb, p, tmp + 2,
506                         "Jumbo payload: %u (%u bytes)",
507                         tvb_get_ntohl(tvb, p + 2), tmp + 2);
508                 } else {
509                     proto_tree_add_text(dstopt_tree, tvb, p, tmp + 2,
510                         "Jumbo payload: Invalid length (%u bytes)",
511                         tmp + 2);
512                 }
513                 p += tmp;
514                 p += 2;
515                 mip_offset += tvb_get_guint8(tvb, mip_offset+1)+2;
516                 break;
517             case IP6OPT_RTALERT:
518               {
519                 char *rta;
520
521                 tmp = tvb_get_guint8(tvb, p + 1);
522                 if (tmp == 2) {
523                     rta = val_to_str(tvb_get_ntohs(tvb, p + 2), rtalertvals,
524                         "Unknown");
525                 } else
526                     rta = "Invalid length";
527                 ti = proto_tree_add_text(dstopt_tree, tvb, p , tmp + 2,
528                     "Router alert: %s (%u bytes)", rta, tmp + 2);
529                 p += tmp;
530                 p += 2;
531                 mip_offset += tvb_get_guint8(tvb, mip_offset + 1) + 2;
532                 break;
533               }
534             case IP6OPT_HOME_ADDRESS:
535                 delta = dissect_mipv6_hoa(tvb, dstopt_tree, mip_offset);
536                 p += delta;
537                 mip_offset += delta;
538                 break;
539             default:
540                 p = offset + len;
541                 break;
542             }
543         }
544
545         /* decode... */
546     }
547     return len;
548 }
549
550 static int
551 dissect_hopopts(tvbuff_t *tvb, int offset, proto_tree *tree)
552 {
553     return dissect_opts(tvb, offset, tree, "Hop-by-hop Option");
554 }
555
556 static int
557 dissect_dstopts(tvbuff_t *tvb, int offset, proto_tree *tree)
558 {
559     return dissect_opts(tvb, offset, tree, "Destination Option");
560 }
561
562 static void
563 dissect_ipv6(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
564 {
565   proto_tree *ipv6_tree = NULL;
566   proto_item *ti;
567   guint8 nxt;
568   int advance;
569   int poffset;
570   guint16 plen;
571   gboolean hopopts, routing, frag, ah, dstopts;
572   guint16 offlg;
573   guint32 ident;
574   int offset;
575   fragment_data *ipfd_head;
576   tvbuff_t   *next_tvb;
577   gboolean update_col_info = TRUE;
578   gboolean save_fragmented;
579
580   struct ip6_hdr ipv6;
581
582   if (check_col(pinfo->cinfo, COL_PROTOCOL))
583     col_set_str(pinfo->cinfo, COL_PROTOCOL, "IPv6");
584   if (check_col(pinfo->cinfo, COL_INFO))
585     col_clear(pinfo->cinfo, COL_INFO);
586
587   offset = 0;
588   tvb_memcpy(tvb, (guint8 *)&ipv6, offset, sizeof(ipv6));
589
590   pinfo->ipproto = ipv6.ip6_nxt; /* XXX make work TCP follow (ipproto = 6) */
591
592   /* Get the payload length */
593   plen = g_ntohs(ipv6.ip6_plen);
594
595   /* Adjust the length of this tvbuff to include only the IPv6 datagram. */
596   set_actual_length(tvb, plen + sizeof (struct ip6_hdr));
597
598   SET_ADDRESS(&pinfo->net_src, AT_IPv6, 16, tvb_get_ptr(tvb, offset + IP6H_SRC, 16));
599   SET_ADDRESS(&pinfo->src, AT_IPv6, 16, tvb_get_ptr(tvb, offset + IP6H_SRC, 16));
600   SET_ADDRESS(&pinfo->net_dst, AT_IPv6, 16, tvb_get_ptr(tvb, offset + IP6H_DST, 16));
601   SET_ADDRESS(&pinfo->dst, AT_IPv6, 16, tvb_get_ptr(tvb, offset + IP6H_DST, 16));
602
603   if (tree) {
604     /* !!! specify length */
605     ti = proto_tree_add_item(tree, proto_ipv6, tvb, offset, 40, FALSE);
606     ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
607
608     /* !!! warning: version also contains 4 Bit priority */
609     proto_tree_add_uint(ipv6_tree, hf_ipv6_version, tvb,
610                 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
611                 (ipv6.ip6_vfc >> 4) & 0x0f);
612
613     proto_tree_add_uint(ipv6_tree, hf_ipv6_class, tvb,
614                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
615                 (guint8)((g_ntohl(ipv6.ip6_flow) >> 20) & 0xff));
616
617     /*
618      * there should be no alignment problems for ip6_flow, since it's the first
619      * guint32 in the ipv6 struct
620      */
621     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_flow, tvb,
622                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
623                 (unsigned long)(g_ntohl(ipv6.ip6_flow) & IPV6_FLOWLABEL_MASK),
624                 "Flowlabel: 0x%05lx",
625                 (unsigned long)(g_ntohl(ipv6.ip6_flow) & IPV6_FLOWLABEL_MASK));
626
627     proto_tree_add_uint(ipv6_tree, hf_ipv6_plen, tvb,
628                 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
629                 plen);
630
631     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_nxt, tvb,
632                 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
633                 ipv6.ip6_nxt,
634                 "Next header: %s (0x%02x)",
635                 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
636
637     proto_tree_add_uint(ipv6_tree, hf_ipv6_hlim, tvb,
638                 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
639                 ipv6.ip6_hlim);
640
641     proto_tree_add_ipv6_hidden(ipv6_tree, hf_ipv6_addr, tvb,
642                                offset + offsetof(struct ip6_hdr, ip6_src), 16,
643                                ipv6.ip6_src.s6_addr8);
644     proto_tree_add_ipv6_hidden(ipv6_tree, hf_ipv6_addr, tvb,
645                                offset + offsetof(struct ip6_hdr, ip6_dst), 16,
646                                ipv6.ip6_dst.s6_addr8);
647
648     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_src, tvb,
649                 offset + offsetof(struct ip6_hdr, ip6_src), 16,
650                 (guint8 *)&ipv6.ip6_src,
651 #ifdef INET6
652                 "Source address: %s (%s)",
653                 get_hostname6(&ipv6.ip6_src),
654 #else
655                 "Source address: %s",
656 #endif
657                 ip6_to_str(&ipv6.ip6_src));
658
659     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_dst, tvb,
660                 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
661                 (guint8 *)&ipv6.ip6_dst,
662 #ifdef INET6
663                 "Destination address: %s (%s)",
664                 get_hostname6(&ipv6.ip6_dst),
665 #else
666                 "Destination address: %s",
667 #endif
668                 ip6_to_str(&ipv6.ip6_dst));
669   }
670
671   /* start of the new header (could be a extension header) */
672   poffset = offset + offsetof(struct ip6_hdr, ip6_nxt);
673   nxt = tvb_get_guint8(tvb, poffset);
674   offset += sizeof(struct ip6_hdr);
675   offlg = 0;
676   ident = 0;
677
678 /* start out assuming this isn't fragmented, and has none of the other
679    non-final headers */
680   hopopts = FALSE;
681   routing = FALSE;
682   frag = FALSE;
683   ah = FALSE;
684   dstopts = FALSE;
685
686 again:
687    switch (nxt) {
688    case IP_PROTO_HOPOPTS:
689                         hopopts = TRUE;
690                         advance = dissect_hopopts(tvb, offset, tree);
691                         nxt = tvb_get_guint8(tvb, offset);
692                         poffset = offset;
693                         offset += advance;
694                         plen -= advance;
695                         goto again;
696     case IP_PROTO_ROUTING:
697                         routing = TRUE;
698                         advance = dissect_routing6(tvb, offset, tree);
699                         nxt = tvb_get_guint8(tvb, offset);
700                         poffset = offset;
701                         offset += advance;
702                         plen -= advance;
703                         goto again;
704     case IP_PROTO_FRAGMENT:
705                         frag = TRUE;
706                         advance = dissect_frag6(tvb, offset, pinfo, tree,
707                             &offlg, &ident);
708                         nxt = tvb_get_guint8(tvb, offset);
709                         poffset = offset;
710                         offset += advance;
711                         plen -= advance;
712                         goto again;
713     case IP_PROTO_AH:
714                         ah = TRUE;
715                         advance = dissect_ah_header(
716                                   tvb_new_subset(tvb, offset, -1, -1),
717                                   pinfo, tree, NULL, NULL);
718                         nxt = tvb_get_guint8(tvb, offset);
719                         poffset = offset;
720                         offset += advance;
721                         plen -= advance;
722                         goto again;
723     case IP_PROTO_DSTOPTS:
724                         dstopts = TRUE;
725                         advance = dissect_dstopts(tvb, offset, tree);
726                         nxt = tvb_get_guint8(tvb, offset);
727                         poffset = offset;
728                         offset += advance;
729                         plen -= advance;
730                         goto again;
731     }
732
733 #ifdef TEST_FINALHDR
734   proto_tree_add_uint_hidden(ipv6_tree, hf_ipv6_final, tvb, poffset, 1, nxt);
735 #endif
736
737   /* If ipv6_reassemble is on, this is a fragment, and we have all the data
738    * in the fragment, then just add the fragment to the hashtable.
739    */
740   save_fragmented = pinfo->fragmented;
741   if (ipv6_reassemble && frag && tvb_bytes_exist(tvb, offset, plen)) {
742     ipfd_head = fragment_add_check(tvb, offset, pinfo, ident,
743                              ipv6_fragment_table,
744                              ipv6_reassembled_table,
745                              offlg & IP6F_OFF_MASK,
746                              plen,
747                              offlg & IP6F_MORE_FRAG);
748
749     next_tvb = process_reassembled_data(tvb, offset, pinfo, "Reassembled IPv6",
750           ipfd_head, &ipv6_frag_items, &update_col_info, ipv6_tree);
751   } else {
752     /* If this is the first fragment, dissect its contents, otherwise
753        just show it as a fragment.
754
755        XXX - if we eventually don't save the reassembled contents of all
756        fragmented datagrams, we may want to always reassemble. */
757     if (offlg & IP6F_OFF_MASK) {
758       /* Not the first fragment - don't dissect it. */
759       next_tvb = NULL;
760     } else {
761       /* First fragment, or not fragmented.  Dissect what we have here. */
762
763       /* Get a tvbuff for the payload. */
764       next_tvb = tvb_new_subset(tvb, offset, -1, -1);
765
766       /*
767        * If this is the first fragment, but not the only fragment,
768        * tell the next protocol that.
769        */
770       if (offlg & IP6F_MORE_FRAG)
771         pinfo->fragmented = TRUE;
772       else
773         pinfo->fragmented = FALSE;
774     }
775   }
776
777   if (next_tvb == NULL) {
778     /* Just show this as a fragment. */
779     /* COL_INFO was filled in by "dissect_frag6()" */
780     call_dissector(data_handle, tvb_new_subset(tvb, offset, -1, -1), pinfo, tree);
781
782     /* As we haven't reassembled anything, we haven't changed "pi", so
783        we don't have to restore it. */
784     pinfo->fragmented = save_fragmented;
785     return;
786   }
787
788   /* do lookup with the subdissector table */
789   if (!dissector_try_port(ip_dissector_table, nxt, next_tvb, pinfo, tree)) {
790     /* Unknown protocol.
791        Handle "no next header" specially. */
792     if (nxt == IP_PROTO_NONE) {
793       if (check_col(pinfo->cinfo, COL_INFO)) {
794         /* If we had an Authentication Header, the AH dissector already
795            put something in the Info column; leave it there. */
796         if (!ah) {
797           if (hopopts || routing || dstopts) {
798             char *sep = "IPv6 ";
799             if (hopopts) {
800               col_append_fstr(pinfo->cinfo, COL_INFO, "%shop-by-hop options",
801                              sep);
802               sep = ", ";
803             }
804             if (routing) {
805               col_append_fstr(pinfo->cinfo, COL_INFO, "%srouting", sep);
806               sep = ", ";
807             }
808             if (dstopts) {
809               col_append_fstr(pinfo->cinfo, COL_INFO, "%sdestination options",
810                               sep);
811             }
812           } else
813             col_set_str(pinfo->cinfo, COL_INFO, "IPv6 no next header");
814         }
815       }
816     } else {
817       if (check_col(pinfo->cinfo, COL_INFO))
818         col_add_fstr(pinfo->cinfo, COL_INFO, "%s (0x%02x)", ipprotostr(nxt),nxt);
819     }
820     call_dissector(data_handle, next_tvb, pinfo, tree);
821   }
822   pinfo->fragmented = save_fragmented;
823 }
824
825 void
826 proto_register_ipv6(void)
827 {
828   static hf_register_info hf[] = {
829     { &hf_ipv6_version,
830       { "Version",              "ipv6.version",
831                                 FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
832     { &hf_ipv6_class,
833       { "Traffic class",        "ipv6.class",
834                                 FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }},
835     { &hf_ipv6_flow,
836       { "Flowlabel",            "ipv6.flow",
837                                 FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL }},
838     { &hf_ipv6_plen,
839       { "Payload length",       "ipv6.plen",
840                                 FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
841     { &hf_ipv6_nxt,
842       { "Next header",          "ipv6.nxt",
843                                 FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }},
844     { &hf_ipv6_hlim,
845       { "Hop limit",            "ipv6.hlim",
846                                 FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
847     { &hf_ipv6_src,
848       { "Source",               "ipv6.src",
849                                 FT_IPv6, BASE_NONE, NULL, 0x0,
850                                 "Source IPv6 Address", HFILL }},
851     { &hf_ipv6_dst,
852       { "Destination",          "ipv6.dst",
853                                 FT_IPv6, BASE_NONE, NULL, 0x0,
854                                 "Destination IPv6 Address", HFILL }},
855     { &hf_ipv6_addr,
856       { "Address",              "ipv6.addr",
857                                 FT_IPv6, BASE_NONE, NULL, 0x0,
858                                 "Source or Destination IPv6 Address", HFILL }},
859
860     { &hf_ipv6_fragment_overlap,
861       { "Fragment overlap",     "ipv6.fragment.overlap",
862                                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
863                                 "Fragment overlaps with other fragments", HFILL }},
864
865     { &hf_ipv6_fragment_overlap_conflict,
866       { "Conflicting data in fragment overlap", "ipv6.fragment.overlap.conflict",
867                                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
868                                 "Overlapping fragments contained conflicting data", HFILL }},
869
870     { &hf_ipv6_fragment_multiple_tails,
871       { "Multiple tail fragments found", "ipv6.fragment.multipletails",
872                                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
873                                 "Several tails were found when defragmenting the packet", HFILL }},
874
875     { &hf_ipv6_fragment_too_long_fragment,
876       { "Fragment too long",    "ipv6.fragment.toolongfragment",
877                                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
878                                 "Fragment contained data past end of packet", HFILL }},
879
880     { &hf_ipv6_fragment_error,
881       { "Defragmentation error", "ipv6.fragment.error",
882                                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
883                                 "Defragmentation error due to illegal fragments", HFILL }},
884
885     { &hf_ipv6_fragment,
886       { "IPv6 Fragment",        "ipv6.fragment",
887                                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
888                                 "IPv6 Fragment", HFILL }},
889
890     { &hf_ipv6_fragments,
891       { "IPv6 Fragments",       "ipv6.fragments",
892                                 FT_NONE, BASE_NONE, NULL, 0x0,
893                                 "IPv6 Fragments", HFILL }},
894
895     { &hf_ipv6_reassembled_in,
896       { "Reassembled IPv6 in frame", "ipv6.reassembled_in",
897                                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
898                                 "This IPv6 packet is reassembled in this frame", HFILL }},
899
900     /* Mobile IPv6 */
901     { &hf_ipv6_mipv6_type,
902       { "Option Type ",         "ipv6.mipv6_type",
903                                 FT_UINT8, BASE_DEC, NULL, 0x0,
904                                 "", HFILL }},
905     { &hf_ipv6_mipv6_length,
906       { "Option Length ",       "ipv6.mipv6_length",
907                                 FT_UINT8, BASE_DEC, NULL, 0x0,
908                                 "", HFILL }},
909     { &hf_ipv6_mipv6_home_address,
910       { "Home Address ",        "ipv6.mipv6_home_address",
911                                 FT_IPv6, BASE_HEX, NULL, 0x0,
912                                 "", HFILL }},
913
914 #ifdef TEST_FINALHDR
915     { &hf_ipv6_final,
916       { "Final next header",    "ipv6.final",
917                                 FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }},
918 #endif
919   };
920   static gint *ett[] = {
921     &ett_ipv6,
922     &ett_ipv6_fragments,
923     &ett_ipv6_fragment,
924   };
925   module_t *ipv6_module;
926
927   proto_ipv6 = proto_register_protocol("Internet Protocol Version 6", "IPv6", "ipv6");
928   proto_register_field_array(proto_ipv6, hf, array_length(hf));
929   proto_register_subtree_array(ett, array_length(ett));
930
931   /* Register configuration options */
932   ipv6_module = prefs_register_protocol(proto_ipv6, NULL);
933   prefs_register_bool_preference(ipv6_module, "defragment",
934         "Reassemble fragmented IPv6 datagrams",
935         "Whether fragmented IPv6 datagrams should be reassembled",
936         &ipv6_reassemble);
937
938   register_dissector("ipv6", dissect_ipv6, proto_ipv6);
939   register_init_routine(ipv6_reassemble_init);
940 }
941
942 void
943 proto_reg_handoff_ipv6(void)
944 {
945   dissector_handle_t ipv6_handle;
946
947   data_handle = find_dissector("data");
948   ipv6_handle = find_dissector("ipv6");
949   dissector_add("ethertype", ETHERTYPE_IPv6, ipv6_handle);
950   dissector_add("ppp.protocol", PPP_IPV6, ipv6_handle);
951   dissector_add("ppp.protocol", ETHERTYPE_IPv6, ipv6_handle);
952   dissector_add("gre.proto", ETHERTYPE_IPv6, ipv6_handle);
953   dissector_add("ip.proto", IP_PROTO_IPV6, ipv6_handle);
954   dissector_add("null.type", BSD_AF_INET6_BSD, ipv6_handle);
955   dissector_add("null.type", BSD_AF_INET6_FREEBSD, ipv6_handle);
956   dissector_add("null.type", BSD_AF_INET6_DARWIN, ipv6_handle);
957   dissector_add("chdlctype", ETHERTYPE_IPv6, ipv6_handle);
958   dissector_add("fr.ietf", NLPID_IP6, ipv6_handle);
959   dissector_add("x.25.spi", NLPID_IP6, ipv6_handle);
960   dissector_add("arcnet.protocol_id", ARCNET_PROTO_IPv6, ipv6_handle);
961
962   ip_dissector_table = find_dissector_table("ip.proto");
963 }