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