Fix the rest of the signed/unsigned comparison warnings.
[metze/wireshark/wip.git] / packet-ipv6.c
1 /* packet-ipv6.c
2  * Routines for IPv6 packet disassembly
3  *
4  * $Id: packet-ipv6.c,v 1.64 2001/10/26 18:28:16 gram 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 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #ifdef HAVE_SYS_SOCKET_h
36 #include <sys/socket.h>
37 #endif
38
39 #ifdef HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42
43 #include <string.h>
44 #include <stdio.h>
45 #include <glib.h>
46 #include "packet.h"
47 #include "packet-ip.h"
48 #include "packet-ipsec.h"
49 #include "packet-ipv6.h"
50 #include "resolv.h"
51 #include "prefs.h"
52 #include "reassemble.h"
53 #include "ipproto.h"
54 #include "etypes.h"
55 #include "ppptypes.h"
56 #include "aftypes.h"
57
58 /*
59  * NOTE: ipv6.nxt is not very useful as we will have chained header.
60  * now testing ipv6.final, but it raises SEGV.
61 #define TEST_FINALHDR
62  */
63
64 static int proto_ipv6 = -1;
65 static int hf_ipv6_version = -1;
66 static int hf_ipv6_class = -1;
67 static int hf_ipv6_flow = -1;
68 static int hf_ipv6_plen = -1;
69 static int hf_ipv6_nxt = -1;
70 static int hf_ipv6_hlim = -1;
71 static int hf_ipv6_src = -1;
72 static int hf_ipv6_dst = -1;
73 static int hf_ipv6_addr = -1;
74 #ifdef TEST_FINALHDR
75 static int hf_ipv6_final = -1;
76 #endif
77 static int hf_ipv6_fragments = -1;
78 static int hf_ipv6_fragment = -1;
79 static int hf_ipv6_fragment_overlap = -1;
80 static int hf_ipv6_fragment_overlap_conflict = -1;
81 static int hf_ipv6_fragment_multiple_tails = -1;
82 static int hf_ipv6_fragment_too_long_fragment = -1;
83 static int hf_ipv6_fragment_error = -1;
84
85 static int hf_ipv6_mipv6_type = -1;
86 static int hf_ipv6_mipv6_length = -1;
87 static int hf_ipv6_mipv6_a_flag = -1;
88 static int hf_ipv6_mipv6_h_flag = -1;
89 static int hf_ipv6_mipv6_r_flag = -1;
90 static int hf_ipv6_mipv6_d_flag = -1;
91 static int hf_ipv6_mipv6_m_flag = -1;
92 static int hf_ipv6_mipv6_b_flag = -1;
93 static int hf_ipv6_mipv6_prefix_length = -1;
94 static int hf_ipv6_mipv6_sequence_number = -1;
95 static int hf_ipv6_mipv6_life_time = -1;
96 static int hf_ipv6_mipv6_status = -1;
97 static int hf_ipv6_mipv6_refresh = -1;
98 static int hf_ipv6_mipv6_home_address = -1;
99 static int hf_ipv6_mipv6_sub_type = -1;
100 static int hf_ipv6_mipv6_sub_length = -1;
101 static int hf_ipv6_mipv6_sub_unique_ID = -1;
102 static int hf_ipv6_mipv6_sub_alternative_COA = -1;
103
104 static gint ett_ipv6 = -1;
105 static gint ett_ipv6_fragments = -1;
106 static gint ett_ipv6_fragment  = -1;
107
108 /* Reassemble fragmented datagrams */
109 static gboolean ipv6_reassemble = FALSE;
110
111 #ifndef offsetof
112 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
113 #endif
114
115 /*
116  * defragmentation of IPv6
117  */
118 static GHashTable *ipv6_fragment_table = NULL;
119
120 static void
121 ipv6_reassemble_init(void)
122 {
123   fragment_table_init(&ipv6_fragment_table);
124 }
125
126 static int
127 dissect_routing6(tvbuff_t *tvb, int offset, proto_tree *tree) {
128     struct ip6_rthdr rt;
129     guint len;
130     proto_tree *rthdr_tree;
131         proto_item *ti;
132     char buf[sizeof(struct ip6_rthdr0) + sizeof(struct e_in6_addr) * 23];
133
134     tvb_memcpy(tvb, (guint8 *)&rt, offset, sizeof(rt));
135     len = (rt.ip6r_len + 1) << 3;
136
137     if (tree) {
138         /* !!! specify length */
139         ti = proto_tree_add_text(tree, tvb, offset, len,
140             "Routing Header, Type %u", rt.ip6r_type);
141         rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
142
143         proto_tree_add_text(rthdr_tree, tvb,
144             offset + offsetof(struct ip6_rthdr, ip6r_nxt), 1,
145             "Next header: %s (0x%02x)", ipprotostr(rt.ip6r_nxt), rt.ip6r_nxt);
146         proto_tree_add_text(rthdr_tree, tvb,
147             offset + offsetof(struct ip6_rthdr, ip6r_len), 1,
148             "Length: %u (%d bytes)", rt.ip6r_len, len);
149         proto_tree_add_text(rthdr_tree, tvb,
150             offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
151             "Type: %u", rt.ip6r_type);
152         proto_tree_add_text(rthdr_tree, tvb,
153             offset + offsetof(struct ip6_rthdr, ip6r_segleft), 1,
154             "Segments left: %u", rt.ip6r_segleft);
155
156         if (rt.ip6r_type == 0 && len <= sizeof(buf)) {
157             struct e_in6_addr *a;
158             int n;
159             struct ip6_rthdr0 *rt0;
160
161             tvb_memcpy(tvb, buf, offset, len);
162             rt0 = (struct ip6_rthdr0 *)buf;
163             for (a = rt0->ip6r0_addr, n = 0;
164                  a < (struct e_in6_addr *)(buf + len);
165                  a++, n++) {
166                 proto_tree_add_text(rthdr_tree, tvb,
167                     offset + offsetof(struct ip6_rthdr0, ip6r0_addr) + n * sizeof(struct e_in6_addr),
168                     sizeof(struct e_in6_addr),
169 #ifdef INET6
170                     "address %d: %s (%s)",
171                     n, get_hostname6(a), ip6_to_str(a)
172 #else
173                     "address %d: %s", n, ip6_to_str(a)
174 #endif
175                     );
176             }
177         }
178
179         /* decode... */
180     }
181
182     return len;
183 }
184
185 static int
186 dissect_frag6(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree,
187     guint16 *offlg, guint32 *ident) {
188     struct ip6_frag frag;
189     int len;
190     proto_item *ti;
191     proto_tree *rthdr_tree;
192
193     tvb_memcpy(tvb, (guint8 *)&frag, offset, sizeof(frag));
194     len = sizeof(frag);
195     frag.ip6f_offlg = ntohs(frag.ip6f_offlg);
196     *offlg = frag.ip6f_offlg;
197     *ident = frag.ip6f_ident;
198     if (check_col(pinfo->fd, COL_INFO)) {
199         col_add_fstr(pinfo->fd, COL_INFO,
200             "IPv6 fragment (nxt=%s (0x%02x) off=%u id=0x%x)",
201             ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
202             frag.ip6f_offlg & IP6F_OFF_MASK, frag.ip6f_ident);
203     }
204     if (tree) {
205            ti = proto_tree_add_text(tree, tvb, offset, len,
206                            "Fragmention Header");
207            rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
208
209            proto_tree_add_text(rthdr_tree, tvb,
210                          offset + offsetof(struct ip6_frag, ip6f_nxt), 1,
211                          "Next header: %s (0x%02x)",
212                          ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt);
213
214 #if 0
215            proto_tree_add_text(rthdr_tree, tvb,
216                          offset + offsetof(struct ip6_frag, ip6f_reserved), 1,
217                          "Reserved: %u",
218                          frag.ip6f_reserved);
219 #endif
220
221            proto_tree_add_text(rthdr_tree, tvb,
222                          offset + offsetof(struct ip6_frag, ip6f_offlg), 2,
223                          "Offset: %u",
224                          frag.ip6f_offlg & IP6F_OFF_MASK);
225
226            proto_tree_add_text(rthdr_tree, tvb,
227                          offset + offsetof(struct ip6_frag, ip6f_offlg), 2,
228                          "More fragments: %s",
229                                 frag.ip6f_offlg & IP6F_MORE_FRAG ?
230                                 "Yes" : "No");
231
232            proto_tree_add_text(rthdr_tree, tvb,
233                          offset + offsetof(struct ip6_frag, ip6f_ident), 4,
234                          "Identification: 0x%08x",
235                          frag.ip6f_ident);
236     }
237     return len;
238 }
239
240 /* Binding Update flag description */
241 static const true_false_string ipv6_mipv6_bu_a_flag_value = {
242     "Binding Acknowledgement requested",
243     "Binding Acknowledgement not requested"
244 };
245 static const true_false_string ipv6_mipv6_bu_h_flag_value = {
246     "Home Registration",
247     "No Home Registration"
248 };
249 static const true_false_string ipv6_mipv6_bu_r_flag_value = {
250     "Router",
251     "Not a Router"
252 };
253 static const true_false_string ipv6_mipv6_bu_d_flag_value = {
254     "Perform Duplicate Address Detection",
255     "Do not perform Duplicate Address Detection"
256 };
257 static const true_false_string ipv6_mipv6_bu_m_flag_value = {
258     "MAP Registration",
259     "No MAP Registration"
260 };
261 static const true_false_string ipv6_mipv6_bu_b_flag_value = {
262     "Request for bicasting",
263     "Do not request for bicasting"
264 };
265
266 static int
267 dissect_mipv6_ba(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset)
268 {
269     guint8 status, len = 0;
270     const char *status_text;
271     gboolean sub_options = FALSE;
272
273     proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_type, tvb,
274         offset + len, IP6_MIPv6_OPTION_TYPE_LENGTH,
275         tvb_get_guint8(tvb, offset + len),
276         "Option Type: %u (0x%02x) - Binding Acknowledgement",
277         tvb_get_guint8(tvb, offset + len),
278     tvb_get_guint8(tvb, offset + len));
279     len += IP6_MIPv6_OPTION_TYPE_LENGTH;
280     if (tvb_get_guint8(tvb, offset + len) > 11)
281         sub_options = TRUE;
282     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_length, tvb, offset + len,
283         IP6_MIPv6_OPTION_LENGTH_LENGTH, tvb_get_guint8(tvb, offset + len));
284     len += IP6_MIPv6_OPTION_LENGTH_LENGTH;
285     status = tvb_get_guint8(tvb, offset + len);
286     switch (status) {
287     case BA_OK:
288         status_text = "- Binding Update accepted";
289         break;
290     case BA_REAS_UNSPEC:
291         status_text = "- Binding Update was rejected - Reason unspecified";
292         break;
293     case BA_ADMIN_PROH:
294         status_text = "- Binding Update was rejected - Administratively prohibited";
295         break;
296     case BA_INSUF_RES:
297         status_text = "- Binding Update was rejected - Insufficient resources";
298         break;
299     case BA_NO_HR:
300         status_text = "- Binding Update was rejected - Home registration not supported";
301         break;
302     case BA_NO_SUBNET:
303         status_text = "- Binding Update was rejected - Not home subnet";
304         break;
305     case BA_ERR_ID_LEN:
306         status_text = "- Binding Update was rejected - Incorrect interface identifier length";
307         break;
308     case BA_NO_HA:
309         status_text = "- Binding Update was rejected - Not home agent for this mobile node";
310         break;
311     case BA_DUPL_ADDR:
312         status_text = "- Binding Update was rejected - Duplicate Address Detection failed";
313         break;
314     default:
315         status_text = NULL;
316         break;
317     }
318     if (!status_text) {
319         if (status > 128)
320             status_text = "- Binding Update was rejected";
321         else
322             status_text = "";
323     }
324     proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_status,
325         tvb, offset + len, IP6_MIPv6_STATUS_LENGTH,
326         tvb_get_guint8(tvb, offset + len),
327         "Status: %u %s", tvb_get_guint8(tvb, offset + len), status_text);
328     len += IP6_MIPv6_STATUS_LENGTH;
329     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_sequence_number,
330         tvb, offset + len, IP6_MIPv6_SEQUENCE_NUMBER_LENGTH,
331         tvb_get_ntohs(tvb, offset + len));
332     len += IP6_MIPv6_SEQUENCE_NUMBER_LENGTH;
333     if (tvb_get_ntohl(tvb, offset + len) == 0xffffffff) {
334         proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_life_time,
335             tvb, offset + len, IP6_MIPv6_LIFE_TIME_LENGTH,
336             tvb_get_ntohl(tvb, offset + len),
337             "Life Time: %u - Infinity", tvb_get_ntohl(tvb, offset + len));
338     } else {
339         proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_life_time,
340             tvb, offset + len, IP6_MIPv6_LIFE_TIME_LENGTH,
341             tvb_get_ntohl(tvb, offset + len));
342     }
343     len += IP6_MIPv6_LIFE_TIME_LENGTH;
344     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_refresh, tvb,
345         offset + len, IP6_MIPv6_REFRESH_LENGTH,
346         tvb_get_ntohl(tvb, offset + len));
347     len += IP6_MIPv6_REFRESH_LENGTH;
348     /* sub - options */
349     if (sub_options)
350         proto_tree_add_text(dstopt_tree, tvb, offset + len, 1, "Sub-Options");
351     return len;
352 }
353
354 static int
355 dissect_mipv6_bu(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset)
356 {
357     int len = 0;
358     gboolean sub_options = FALSE;
359
360     proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_type, tvb, offset,
361         IP6_MIPv6_OPTION_TYPE_LENGTH, tvb_get_guint8(tvb, offset),
362         "Option Type: %u (0x%02x) - Binding Update",
363         tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset));
364     len += IP6_MIPv6_OPTION_TYPE_LENGTH;
365     if (tvb_get_guint8(tvb, offset + len) > 8)
366         sub_options = TRUE;
367     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_length, tvb, offset + len,
368         IP6_MIPv6_OPTION_LENGTH_LENGTH, tvb_get_guint8(tvb, offset + len));
369     len += IP6_MIPv6_OPTION_LENGTH_LENGTH;
370     proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_a_flag, tvb, offset + len,
371         IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset + len));
372     proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_h_flag, tvb, offset + len,
373         IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset + len));
374     proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_r_flag, tvb, offset + len,
375         IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset + len));
376     proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_d_flag, tvb, offset + len,
377         IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset + len));
378     proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_m_flag, tvb, offset + len,
379         IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset + len));
380     proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_b_flag, tvb, offset + len,
381         IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset + len));
382     len += IP6_MIPv6_FLAGS_LENGTH;
383     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_prefix_length, tvb,
384         offset + len,
385         IP6_MIPv6_PREFIX_LENGTH_LENGTH, tvb_get_guint8(tvb, offset + len));
386     len += IP6_MIPv6_PREFIX_LENGTH_LENGTH;
387     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_sequence_number, tvb,
388         offset + len, IP6_MIPv6_SEQUENCE_NUMBER_LENGTH,
389         tvb_get_ntohs(tvb, offset + len));
390     len += IP6_MIPv6_SEQUENCE_NUMBER_LENGTH;
391     if (tvb_get_ntohl(tvb, offset + len) == 0xffffffff) {
392     proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_life_time, tvb,
393         offset + len, IP6_MIPv6_LIFE_TIME_LENGTH,
394         tvb_get_ntohl(tvb, offset + len), "Life Time: %u - Infinity",
395         tvb_get_ntohl(tvb, offset + len));
396     } else {
397         proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_life_time, tvb,
398             offset + len, IP6_MIPv6_LIFE_TIME_LENGTH, tvb_get_ntohl(tvb,
399             offset + len));
400     }
401     len += IP6_MIPv6_LIFE_TIME_LENGTH;
402     /* sub - options */
403     if (sub_options)
404         proto_tree_add_text(dstopt_tree, tvb, offset + len, 1, "Sub-Options");
405     return len;
406 }
407
408 static int
409 dissect_mipv6_ha(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset)
410 {
411     int len = 0;
412     gboolean sub_options = FALSE;
413
414     proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_type, tvb,
415         offset + len, IP6_MIPv6_OPTION_TYPE_LENGTH,
416         tvb_get_guint8(tvb, offset + len),
417         "Option Type: %u (0x%02x) - Home Address",
418         tvb_get_guint8(tvb, offset + len), tvb_get_guint8(tvb, offset + len));
419     len += IP6_MIPv6_OPTION_TYPE_LENGTH;
420     if (tvb_get_guint8(tvb, offset + len) > 16)
421         sub_options = TRUE;
422     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_length, tvb, offset + len,
423         IP6_MIPv6_OPTION_LENGTH_LENGTH, tvb_get_guint8(tvb, offset + len));
424     len += IP6_MIPv6_OPTION_LENGTH_LENGTH;
425     proto_tree_add_ipv6(dstopt_tree, hf_ipv6_mipv6_home_address, tvb,
426         offset + len, IP6_MIPv6_HOME_ADDRESS_LENGTH,
427         tvb_get_ptr(tvb, offset + len, IP6_MIPv6_HOME_ADDRESS_LENGTH));
428     len += IP6_MIPv6_HOME_ADDRESS_LENGTH;
429     /* sub - options */
430     if (sub_options)
431         proto_tree_add_text(dstopt_tree, tvb, offset + len, 1, "Sub-Options");
432     return len;
433 }
434
435 static int
436 dissect_mipv6_br(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset)
437 {
438     int len = 0;
439     gboolean sub_options = FALSE;
440
441     proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_type, tvb,
442         offset + len, IP6_MIPv6_OPTION_TYPE_LENGTH,
443         tvb_get_guint8(tvb, offset + len),
444         "Option Type: %u (0x%02x) - Binding Request",
445         tvb_get_guint8(tvb, offset + len), tvb_get_guint8(tvb, offset + len));
446     len += IP6_MIPv6_OPTION_TYPE_LENGTH;
447     if (tvb_get_guint8(tvb, offset + len) > 0)
448         sub_options = TRUE;
449     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_length, tvb, offset + len,
450         IP6_MIPv6_OPTION_LENGTH_LENGTH, tvb_get_guint8(tvb, offset + len));
451     len += IP6_MIPv6_OPTION_LENGTH_LENGTH;
452     /* sub - options */
453     if (sub_options)
454         proto_tree_add_text(dstopt_tree, tvb, offset + len, 1, "Sub-Options");
455     return len;
456 }
457
458 static int
459 dissect_mipv6_sub_u(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset)
460 {
461     int len = 0;
462                         
463     proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_sub_length, tvb,
464         offset + len, IP6_MIPv6_SUB_TYPE_LENGTH,
465         tvb_get_guint8(tvb, offset + len),
466         "Sub-Option Type: %u (0x%02x) - Unique Identifier Sub-Option",
467         tvb_get_guint8(tvb, offset + len), tvb_get_guint8(tvb, offset + len));
468     len += IP6_MIPv6_SUB_TYPE_LENGTH;
469     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_sub_length, tvb,
470         offset + len, IP6_MIPv6_SUB_LENGTH_LENGTH,
471         tvb_get_guint8(tvb, offset + len));
472     len += IP6_MIPv6_SUB_LENGTH_LENGTH;
473     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_sub_unique_ID, tvb,
474         offset + len, IP6_MIPv6_SUB_UNIQUE_ID_LENGTH,
475         tvb_get_ntohs(tvb, offset + len));
476     len += IP6_MIPv6_SUB_UNIQUE_ID_LENGTH;
477     return len;
478 }
479
480 static int
481 dissect_mipv6_sub_a_coa(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset)
482 {
483     int len = 0;
484
485     proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_sub_type, tvb,
486         offset + len, IP6_MIPv6_SUB_TYPE_LENGTH,
487         tvb_get_guint8(tvb, offset + len),
488         "Sub-Option Type: %u (0x%02x) - Alternative Care Of Address",
489         tvb_get_guint8(tvb, offset + len),
490     tvb_get_guint8(tvb, offset + len));
491     len += IP6_MIPv6_SUB_TYPE_LENGTH;
492     proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_sub_length, tvb,
493         offset + len, IP6_MIPv6_SUB_LENGTH_LENGTH,
494         tvb_get_guint8(tvb, offset + len));
495     len += IP6_MIPv6_SUB_LENGTH_LENGTH;
496     proto_tree_add_ipv6(dstopt_tree, hf_ipv6_mipv6_sub_alternative_COA, tvb,
497         offset + len, IP6_MIPv6_SUB_ALTERNATIVE_COA_LENGTH,
498         tvb_get_ptr(tvb, offset + len, IP6_MIPv6_SUB_ALTERNATIVE_COA_LENGTH));
499     len += IP6_MIPv6_SUB_ALTERNATIVE_COA_LENGTH;
500     return len;
501 }
502
503 static const value_string rtalertvals[] = {
504     { IP6OPT_RTALERT_MLD, "MLD" },
505     { IP6OPT_RTALERT_RSVP, "RSVP" },
506     { 0, NULL },
507 };
508
509 static int
510 dissect_opts(tvbuff_t *tvb, int offset, proto_tree *tree, char *optname)
511 {
512     struct ip6_ext ext;
513     int len;
514     proto_tree *dstopt_tree;
515     proto_item *ti;
516     gint p;
517     guint8 tmp;
518     int mip_offset = 0, delta = 0;
519
520     tvb_memcpy(tvb, (guint8 *)&ext, offset, sizeof(ext));
521     len = (ext.ip6e_len + 1) << 3;
522
523     if (tree) {
524         /* !!! specify length */
525         ti = proto_tree_add_text(tree, tvb, offset, len, "%s Header ", optname);
526
527         dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
528
529         proto_tree_add_text(dstopt_tree, tvb,
530             offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
531             "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
532         proto_tree_add_text(dstopt_tree, tvb,
533             offset + offsetof(struct ip6_ext, ip6e_len), 1,
534             "Length: %u (%d bytes)", ext.ip6e_len, len);
535
536         mip_offset = offset;
537         mip_offset += 2;
538
539         p = offset + 2;
540
541         while (p < offset + len) {
542             switch (tvb_get_guint8(tvb, p)) {
543             case IP6OPT_PAD1:
544                 proto_tree_add_text(dstopt_tree, tvb, p, 1, "Pad1");
545                 p++;
546                 mip_offset++;
547                 break;
548             case IP6OPT_PADN:
549                 tmp = tvb_get_guint8(tvb, p + 1);
550                 proto_tree_add_text(dstopt_tree, tvb, p, tmp + 2,
551                     "PadN: %u bytes", tmp + 2);
552                 p += tmp;
553                 p += 2;
554                 mip_offset += tvb_get_guint8(tvb, mip_offset + 1) + 2;
555                 break;
556             case IP6OPT_JUMBO:
557                 tmp = tvb_get_guint8(tvb, p + 1);
558                 if (tmp == 4) {
559                     proto_tree_add_text(dstopt_tree, tvb, p, tmp + 2,
560                         "Jumbo payload: %u (%u bytes)",
561                         tvb_get_ntohl(tvb, p + 2), tmp + 2);
562                 } else {
563                     proto_tree_add_text(dstopt_tree, tvb, p, tmp + 2,
564                         "Jumbo payload: Invalid length (%u bytes)",
565                         tmp + 2);
566                 }
567                 p += tmp;
568                 p += 2;
569                 mip_offset += tvb_get_guint8(tvb, mip_offset+1)+2;
570                 break;
571             case IP6OPT_RTALERT:
572               {
573                 char *rta;
574
575                 tmp = tvb_get_guint8(tvb, p + 1);
576                 if (tmp == 2) {
577                     rta = val_to_str(tvb_get_ntohs(tvb, p + 2), rtalertvals,
578                         "Unknown");
579                 } else
580                     rta = "Invalid length";
581                 ti = proto_tree_add_text(dstopt_tree, tvb, p , tmp + 2,
582                     "Router alert: %s (%u bytes)", rta, tmp + 2);
583                 p += tmp;
584                 p += 2;
585                 mip_offset += tvb_get_guint8(tvb, mip_offset + 1) + 2;
586                 break;
587               }
588             case IP6OPT_BINDING_UPDATE :
589                 delta = dissect_mipv6_bu(tvb, dstopt_tree, mip_offset);
590                 p += delta;
591                 mip_offset += delta;
592                 break;
593             case IP6OPT_BINDING_ACK :
594                 delta = dissect_mipv6_ba(tvb, dstopt_tree, mip_offset);
595                 p += delta;
596                 mip_offset += delta;
597                 break;
598             case IP6OPT_HOME_ADDRESS :
599                 delta = dissect_mipv6_ha(tvb, dstopt_tree, mip_offset);
600                 p += delta;
601                 mip_offset += delta;
602                 break;
603             case IP6OPT_BINDING_REQUEST :
604                 delta = dissect_mipv6_br(tvb, dstopt_tree, mip_offset);
605                 p += delta;
606                 mip_offset += delta;
607                 break;
608             case IP6OPT_MIPv6_UNIQUE_ID_SUB :
609                 delta = dissect_mipv6_sub_u(tvb, dstopt_tree, mip_offset);
610                 p += delta;
611                 mip_offset += delta;
612                 break;
613             case IP6OPT_MIPv6_ALTERNATIVE_COA_SUB :
614                 delta = dissect_mipv6_sub_a_coa(tvb, dstopt_tree, mip_offset);
615                 p += delta;
616                 mip_offset += delta;
617                 break;
618             default:
619                 p = offset + len;
620                 break;
621             }
622         }
623
624         /* decode... */
625     }
626     return len;
627 }
628
629 static int
630 dissect_hopopts(tvbuff_t *tvb, int offset, proto_tree *tree)
631 {
632     return dissect_opts(tvb, offset, tree, "Hop-by-hop Option");
633 }
634
635 static int
636 dissect_dstopts(tvbuff_t *tvb, int offset, proto_tree *tree)
637 {
638     return dissect_opts(tvb, offset, tree, "Destination Option");
639 }
640
641 static void
642 dissect_ipv6(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
643 {
644   proto_tree *ipv6_tree = NULL;
645   proto_item *ti;
646   guint8 nxt;
647   int advance;
648   int poffset;
649   guint16 plen;
650   gboolean frag;
651   guint16 offlg;
652   guint32 ident;
653   int offset;
654   fragment_data *ipfd_head;
655   tvbuff_t   *next_tvb;
656   packet_info save_pi;
657   gboolean must_restore_pi = FALSE;
658   gboolean update_col_info = TRUE;
659
660   struct ip6_hdr ipv6;
661
662   if (check_col(pinfo->fd, COL_PROTOCOL))
663     col_set_str(pinfo->fd, COL_PROTOCOL, "IPv6");
664   if (check_col(pinfo->fd, COL_INFO))
665     col_clear(pinfo->fd, COL_INFO);
666
667   offset = 0;
668   tvb_memcpy(tvb, (guint8 *)&ipv6, offset, sizeof(ipv6));
669
670   pinfo->ipproto = ipv6.ip6_nxt; /* XXX make work TCP follow (ipproto = 6) */
671
672   /* Get the payload length */
673   plen = ntohs(ipv6.ip6_plen);
674
675   /* Adjust the length of this tvbuff to include only the IPv6 datagram. */
676   set_actual_length(tvb, pinfo, plen + sizeof (struct ip6_hdr));
677
678   SET_ADDRESS(&pinfo->net_src, AT_IPv6, 16, tvb_get_ptr(tvb, offset + IP6H_SRC, 16));
679   SET_ADDRESS(&pinfo->src, AT_IPv6, 16, tvb_get_ptr(tvb, offset + IP6H_SRC, 16));
680   SET_ADDRESS(&pinfo->net_dst, AT_IPv6, 16, tvb_get_ptr(tvb, offset + IP6H_DST, 16));
681   SET_ADDRESS(&pinfo->dst, AT_IPv6, 16, tvb_get_ptr(tvb, offset + IP6H_DST, 16));
682
683   if (tree) {
684     /* !!! specify length */
685     ti = proto_tree_add_item(tree, proto_ipv6, tvb, offset, 40, FALSE);
686     ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
687
688     /* !!! warning: version also contains 4 Bit priority */
689     proto_tree_add_uint(ipv6_tree, hf_ipv6_version, tvb,
690                 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
691                 (ipv6.ip6_vfc >> 4) & 0x0f);
692
693     proto_tree_add_uint(ipv6_tree, hf_ipv6_class, tvb,
694                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
695                 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
696
697     /*
698      * there should be no alignment problems for ip6_flow, since it's the first
699      * guint32 in the ipv6 struct
700      */
701     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_flow, tvb,
702                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
703                 (unsigned long)(ntohl(ipv6.ip6_flow) & IPV6_FLOWLABEL_MASK),
704                 "Flowlabel: 0x%05lx",
705                 (unsigned long)(ntohl(ipv6.ip6_flow) & IPV6_FLOWLABEL_MASK));
706
707     proto_tree_add_uint(ipv6_tree, hf_ipv6_plen, tvb,
708                 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
709                 plen);
710
711     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_nxt, tvb,
712                 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
713                 ipv6.ip6_nxt,
714                 "Next header: %s (0x%02x)",
715                 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
716
717     proto_tree_add_uint(ipv6_tree, hf_ipv6_hlim, tvb,
718                 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
719                 ipv6.ip6_hlim);
720
721     proto_tree_add_ipv6_hidden(ipv6_tree, hf_ipv6_addr, tvb,
722                                offset + offsetof(struct ip6_hdr, ip6_src), 16,
723                                ipv6.ip6_src.s6_addr8);
724     proto_tree_add_ipv6_hidden(ipv6_tree, hf_ipv6_addr, tvb,
725                                offset + offsetof(struct ip6_hdr, ip6_dst), 16,
726                                ipv6.ip6_dst.s6_addr8);
727
728     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_src, tvb,
729                 offset + offsetof(struct ip6_hdr, ip6_src), 16,
730                 (guint8 *)&ipv6.ip6_src,
731 #ifdef INET6
732                 "Source address: %s (%s)",
733                 get_hostname6(&ipv6.ip6_src),
734 #else
735                 "Source address: %s",
736 #endif
737                 ip6_to_str(&ipv6.ip6_src));
738
739     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_dst, tvb,
740                 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
741                 (guint8 *)&ipv6.ip6_dst,
742 #ifdef INET6
743                 "Destination address: %s (%s)",
744                 get_hostname6(&ipv6.ip6_dst),
745 #else
746                 "Destination address: %s",
747 #endif
748                 ip6_to_str(&ipv6.ip6_dst));
749   }
750
751   /* start of the new header (could be a extension header) */
752   poffset = offset + offsetof(struct ip6_hdr, ip6_nxt);
753   nxt = tvb_get_guint8(tvb, poffset);
754   offset += sizeof(struct ip6_hdr);
755   offlg = 0;
756   ident = 0;
757
758 /* start out assuming this isn't fragmented */
759   frag = FALSE;
760
761 again:
762    switch (nxt) {
763    case IP_PROTO_HOPOPTS:
764                         advance = dissect_hopopts(tvb, offset, tree);
765                         nxt = tvb_get_guint8(tvb, offset);
766                         poffset = offset;
767                         offset += advance;
768                         plen -= advance;
769                         goto again;
770     case IP_PROTO_ROUTING:
771                         advance = dissect_routing6(tvb, offset, tree);
772                         nxt = tvb_get_guint8(tvb, offset);
773                         poffset = offset;
774                         offset += advance;
775                         plen -= advance;
776                         goto again;
777     case IP_PROTO_FRAGMENT:
778                         frag = TRUE;
779                         advance = dissect_frag6(tvb, offset, pinfo, tree,
780                             &offlg, &ident);
781                         nxt = tvb_get_guint8(tvb, offset);
782                         poffset = offset;
783                         offset += advance;
784                         plen -= advance;
785                         goto again;
786     case IP_PROTO_AH:
787                         advance = dissect_ah_header(
788                                   tvb_new_subset(tvb, offset, -1, -1),
789                                   pinfo, tree, NULL, NULL);
790                         nxt = tvb_get_guint8(tvb, offset);
791                         poffset = offset;
792                         offset += advance;
793                         plen -= advance;
794                         goto again;
795     case IP_PROTO_DSTOPTS:
796                         advance = dissect_dstopts(tvb, offset, tree);
797                         nxt = tvb_get_guint8(tvb, offset);
798                         poffset = offset;
799                         offset += advance;
800                         plen -= advance;
801                         goto again;
802     }
803
804 #ifdef TEST_FINALHDR
805   proto_tree_add_uint_hidden(ipv6_tree, hf_ipv6_final, tvb, poffset, 1, nxt);
806 #endif
807
808   /* If ipv6_reassemble is on and this is a fragment, then just add the fragment
809    * to the hashtable.
810    */
811   if (ipv6_reassemble && frag) {
812     /* We're reassembling, and this is part of a fragmented datagram.
813        Add the fragment to the hash table if the frame isn't truncated. */
814     if (tvb_reported_length(tvb) <= tvb_length(tvb)) {
815       ipfd_head = fragment_add(tvb, offset, pinfo, ident,
816                                ipv6_fragment_table,
817                                offlg & IP6F_OFF_MASK,
818                                plen,
819                                offlg & IP6F_MORE_FRAG);
820     } else {
821       ipfd_head = NULL;
822     }
823
824     if (ipfd_head != NULL) {
825       fragment_data *ipfd;
826       proto_tree *ft = NULL;
827       proto_item *fi = NULL;
828
829       /* OK, we have the complete reassembled payload. */
830       /* show all fragments */
831       fi = proto_tree_add_item(ipv6_tree, hf_ipv6_fragments,
832                 tvb, 0, 0, FALSE);
833       ft = proto_item_add_subtree(fi, ett_ipv6_fragments);
834       for (ipfd = ipfd_head->next; ipfd; ipfd = ipfd->next){
835         if (ipfd->flags & (FD_OVERLAP|FD_OVERLAPCONFLICT
836                           |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
837           /* this fragment has some flags set, create a subtree
838            * for it and display the flags.
839            */
840           proto_tree *fet = NULL;
841           proto_item *fei = NULL;
842           int hf;
843
844           if (ipfd->flags & (FD_OVERLAPCONFLICT
845                       |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
846             hf = hf_ipv6_fragment_error;
847           } else {
848             hf = hf_ipv6_fragment;
849           }
850           fei = proto_tree_add_none_format(ft, hf,
851                    tvb, 0, 0,
852                    "Frame:%d payload:%d-%d",
853                    ipfd->frame,
854                    ipfd->offset,
855                    ipfd->offset+ipfd->len-1
856           );
857           fet = proto_item_add_subtree(fei, ett_ipv6_fragment);
858           if (ipfd->flags&FD_OVERLAP) {
859             proto_tree_add_boolean(fet,
860                  hf_ipv6_fragment_overlap, tvb, 0, 0,
861                  TRUE);
862           }
863           if (ipfd->flags&FD_OVERLAPCONFLICT) {
864             proto_tree_add_boolean(fet,
865                  hf_ipv6_fragment_overlap_conflict, tvb, 0, 0,
866                  TRUE);
867           }
868           if (ipfd->flags&FD_MULTIPLETAILS) {
869             proto_tree_add_boolean(fet,
870                  hf_ipv6_fragment_multiple_tails, tvb, 0, 0,
871                  TRUE);
872           }
873           if (ipfd->flags&FD_TOOLONGFRAGMENT) {
874             proto_tree_add_boolean(fet,
875                  hf_ipv6_fragment_too_long_fragment, tvb, 0, 0,
876                  TRUE);
877           }
878         } else {
879           /* nothing of interest for this fragment */
880           proto_tree_add_none_format(ft, hf_ipv6_fragment,
881                    tvb, 0, 0,
882                    "Frame:%d payload:%d-%d",
883                    ipfd->frame,
884                    ipfd->offset,
885                    ipfd->offset+ipfd->len-1
886           );
887         }
888       }
889       if (ipfd_head->flags & (FD_OVERLAPCONFLICT
890                         |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
891         if (check_col(pinfo->fd, COL_INFO)) {
892           col_set_str(pinfo->fd, COL_INFO, "[Illegal fragments]");
893           update_col_info = FALSE;
894         }
895       }
896
897       /* Allocate a new tvbuff, referring to the reassembled payload. */
898       next_tvb = tvb_new_real_data(ipfd_head->data, ipfd_head->datalen,
899         ipfd_head->datalen, "Reassembled");
900
901       /* Add the tvbuff to the list of tvbuffs to which the tvbuff we
902          were handed refers, so it'll get cleaned up when that tvbuff
903          is cleaned up. */
904       tvb_set_child_real_data_tvbuff(tvb, next_tvb);
905
906       /* Add the defragmented data to the data source list. */
907       pinfo->fd->data_src = g_slist_append(pinfo->fd->data_src, next_tvb);
908
909       /* It's not fragmented. */
910       pinfo->fragmented = FALSE;
911
912       /* Save the current value of "pi", and adjust certain fields to
913          reflect the new tvbuff. */
914       save_pi = pi;
915       pi.compat_top_tvb = next_tvb;
916       pi.len = tvb_reported_length(next_tvb);
917       pi.captured_len = tvb_length(next_tvb);
918       must_restore_pi = TRUE;
919     } else {
920       /* We don't have the complete reassembled payload. */
921       next_tvb = NULL;
922     }
923   } else {
924     /* If this is the first fragment, dissect its contents, otherwise
925        just show it as a fragment.
926
927        XXX - if we eventually don't save the reassembled contents of all
928        fragmented datagrams, we may want to always reassemble. */
929     if (offlg & IP6F_OFF_MASK) {
930       /* Not the first fragment - don't dissect it. */
931       next_tvb = NULL;
932     } else {
933       /* First fragment, or not fragmented.  Dissect what we have here. */
934
935       /* Get a tvbuff for the payload. */
936       next_tvb = tvb_new_subset(tvb, offset, -1, -1);
937
938       /*
939        * If this is the first fragment, but not the only fragment,
940        * tell the next protocol that.
941        */
942       if (offlg & IP6F_MORE_FRAG)
943         pinfo->fragmented = TRUE;
944       else
945         pinfo->fragmented = FALSE;
946     }
947   }
948
949   if (next_tvb == NULL) {
950     /* Just show this as a fragment. */
951     /* COL_INFO was filled in by "dissect_frag6()" */
952     dissect_data(tvb, offset, pinfo, tree);
953
954     /* As we haven't reassembled anything, we haven't changed "pi", so
955        we don't have to restore it. */
956     return;
957   }
958
959   /* do lookup with the subdissector table */
960   if (!dissector_try_port(ip_dissector_table, nxt, next_tvb, pinfo, tree)) {
961     /* Unknown protocol */
962     if (check_col(pinfo->fd, COL_INFO))
963       col_add_fstr(pinfo->fd, COL_INFO, "%s (0x%02x)", ipprotostr(nxt),nxt);
964     dissect_data(next_tvb, 0, pinfo, tree);
965   }
966 }
967
968 static void
969 dissect_ipv6_none(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
970 {
971     if (hf_ipv6_mipv6_length != -1) {
972         if (check_col(pinfo->fd, COL_INFO))
973             col_add_fstr(pinfo->fd, COL_INFO, "Mobile IPv6 Destination Option");
974     } else {
975         if (check_col(pinfo->fd, COL_INFO))
976             col_add_fstr(pinfo->fd, COL_INFO, "IPv6 no next header");
977     }
978     /* XXX - dissect the payload as padding? */
979 }
980
981 void
982 proto_register_ipv6(void)
983 {
984   static hf_register_info hf[] = {
985     { &hf_ipv6_version,
986       { "Version",              "ipv6.version",
987                                 FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
988     { &hf_ipv6_class,
989       { "Traffic class",        "ipv6.class",
990                                 FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }},
991     { &hf_ipv6_flow,
992       { "Flowlabel",            "ipv6.flow",
993                                 FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL }},
994     { &hf_ipv6_plen,
995       { "Payload length",       "ipv6.plen",
996                                 FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
997     { &hf_ipv6_nxt,
998       { "Next header",          "ipv6.nxt",
999                                 FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }},
1000     { &hf_ipv6_hlim,
1001       { "Hop limit",            "ipv6.hlim",
1002                                 FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
1003     { &hf_ipv6_src,
1004       { "Source",               "ipv6.src",
1005                                 FT_IPv6, BASE_NONE, NULL, 0x0,
1006                                 "Source IPv6 Address", HFILL }},
1007     { &hf_ipv6_dst,
1008       { "Destination",          "ipv6.dst",
1009                                 FT_IPv6, BASE_NONE, NULL, 0x0,
1010                                 "Destination IPv6 Address", HFILL }},
1011     { &hf_ipv6_addr,
1012       { "Address",              "ipv6.addr",
1013                                 FT_IPv6, BASE_NONE, NULL, 0x0,
1014                                 "Source or Destination IPv6 Address", HFILL }},
1015
1016     { &hf_ipv6_fragment_overlap,
1017       { "Fragment overlap",     "ipv6.fragment.overlap",
1018                                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1019                                 "Fragment overlaps with other fragments", HFILL }},
1020
1021     { &hf_ipv6_fragment_overlap_conflict,
1022       { "Conflicting data in fragment overlap", "ipv6.fragment.overlap.conflict",
1023                                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1024                                 "Overlapping fragments contained conflicting data", HFILL }},
1025
1026     { &hf_ipv6_fragment_multiple_tails,
1027       { "Multiple tail fragments found", "ipv6.fragment.multipletails",
1028                                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1029                                 "Several tails were found when defragmenting the packet", HFILL }},
1030
1031     { &hf_ipv6_fragment_too_long_fragment,
1032       { "Fragment too long",    "ipv6.fragment.toolongfragment",
1033                                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1034                                 "Fragment contained data past end of packet", HFILL }},
1035
1036     { &hf_ipv6_fragment_error,
1037       { "Defragmentation error", "ipv6.fragment.error",
1038                                 FT_NONE, BASE_NONE, NULL, 0x0,
1039                                 "Defragmentation error due to illegal fragments", HFILL }},
1040
1041     { &hf_ipv6_fragment,
1042       { "IPv6 Fragment",        "ipv6.fragment",
1043                                 FT_NONE, BASE_NONE, NULL, 0x0,
1044                                 "IPv6 Fragment", HFILL }},
1045
1046     { &hf_ipv6_fragments,
1047       { "IPv6 Fragments",       "ipv6.fragments",
1048                                 FT_NONE, BASE_NONE, NULL, 0x0,
1049                                 "IPv6 Fragments", HFILL }},
1050
1051     /* BT INSERT BEGIN */
1052     { &hf_ipv6_mipv6_type,
1053       { "Option Type ",         "ipv6.mipv6_type",
1054                                 FT_UINT8, BASE_DEC, NULL, 0x0,
1055                                 "", HFILL }},
1056     { &hf_ipv6_mipv6_length,
1057       { "Option Length ",               "ipv6.mipv6_length",
1058                                 FT_UINT8, BASE_DEC, NULL, 0x0,
1059                                 "", HFILL }},
1060     { &hf_ipv6_mipv6_a_flag,
1061       { "Acknowledge (A) ",             "ipv6.mipv6_a_flag",
1062                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_a_flag_value),
1063                                 IP6_MIPv6_BU_A_FLAG,
1064                                 "", HFILL }},
1065     { &hf_ipv6_mipv6_h_flag,
1066       { "Home Registration (H) ",               "ipv6.mipv6_h_flag",
1067                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_h_flag_value),
1068                                 IP6_MIPv6_BU_H_FLAG,
1069                                 "", HFILL }},
1070     { &hf_ipv6_mipv6_r_flag,
1071       { "Router (R) ",          "ipv6.mipv6_r_flag",
1072                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_r_flag_value),
1073                                 IP6_MIPv6_BU_R_FLAG,
1074                                 "", HFILL }},
1075     { &hf_ipv6_mipv6_d_flag,
1076       { "Duplicate Address Detection (D) ",             "ipv6.mipv6_d_flag",
1077                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_d_flag_value),
1078                                 IP6_MIPv6_BU_D_FLAG,
1079                                 "", HFILL }},
1080     { &hf_ipv6_mipv6_m_flag,
1081       { "MAP Registration (M) ",                "ipv6.mipv6_m_flag",
1082                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_m_flag_value),
1083                                 IP6_MIPv6_BU_M_FLAG,
1084                                 "", HFILL }},
1085     { &hf_ipv6_mipv6_b_flag,
1086       { "Bicasting all (B) ",           "ipv6.mipv6_b_flag",
1087                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_b_flag_value),
1088                                 IP6_MIPv6_BU_B_FLAG,
1089                                 "", HFILL }},
1090     { &hf_ipv6_mipv6_prefix_length,
1091       { "Prefix Length ",               "ipv6.mipv6_prefix_length",
1092                                 FT_UINT8, BASE_DEC, NULL, 0x0,
1093                                 "", HFILL }},
1094     { &hf_ipv6_mipv6_sequence_number,
1095       { "Sequence Number ",             "ipv6.mipv6_sequence_number",
1096                                 FT_UINT16, BASE_DEC, NULL, 0x0,
1097                                 "", HFILL }},
1098     { &hf_ipv6_mipv6_life_time,
1099       { "Life Time ",           "ipv6.mipv6_life_time",
1100                                 FT_UINT32, BASE_DEC, NULL, 0x0,
1101                                 "", HFILL }},
1102     { &hf_ipv6_mipv6_status,
1103       { "Status ",              "ipv6.mipv6_status",
1104                                 FT_UINT8, BASE_DEC, NULL, 0x0,
1105                                 "", HFILL }},
1106     { &hf_ipv6_mipv6_refresh,
1107       { "Refresh ",             "ipv6.mipv6_refresh",
1108                                 FT_UINT32, BASE_DEC, NULL, 0x0,
1109                                 "", HFILL }},
1110     { &hf_ipv6_mipv6_home_address,
1111       { "Home Address ",                "ipv6.mipv6_home_address",
1112                                 FT_IPv6, BASE_HEX, NULL, 0x0,
1113                                 "", HFILL }},
1114     { &hf_ipv6_mipv6_sub_type,
1115       { "Sub-Option Type ",             "ipv6.mipv6_sub_type",
1116                                 FT_UINT8, BASE_DEC, NULL, 0x0,
1117                                 "", HFILL }},
1118     { &hf_ipv6_mipv6_sub_length,
1119       { "Sub-Option Length ",           "ipv6.mipv6_sub_length",
1120                                 FT_UINT8, BASE_DEC, NULL, 0x0,
1121                                 "", HFILL }},
1122     { &hf_ipv6_mipv6_sub_unique_ID,
1123       { "Unique Identifier ",           "ipv6.mipv6_sub_unique_ID",
1124                                 FT_UINT16, BASE_DEC, NULL, 0x0,
1125                                 "", HFILL }},
1126     { &hf_ipv6_mipv6_sub_alternative_COA,
1127       { "Alternative Care of Address ",         "ipv6.mipv6_sub_alternative_COA",
1128                                 FT_IPv6, BASE_HEX, NULL, 0x0,
1129                                 "", HFILL }},
1130
1131     /* BT INSERT END */
1132 #ifdef TEST_FINALHDR
1133     { &hf_ipv6_final,
1134       { "Final next header",    "ipv6.final",
1135                                 FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }},
1136 #endif
1137   };
1138   static gint *ett[] = {
1139     &ett_ipv6,
1140     &ett_ipv6_fragments,
1141     &ett_ipv6_fragment,
1142   };
1143   module_t *ipv6_module;
1144
1145   proto_ipv6 = proto_register_protocol("Internet Protocol Version 6", "IPv6", "ipv6");
1146   proto_register_field_array(proto_ipv6, hf, array_length(hf));
1147   proto_register_subtree_array(ett, array_length(ett));
1148
1149   /* Register configuration options */
1150   ipv6_module = prefs_register_protocol(proto_ipv6, NULL);
1151   prefs_register_bool_preference(ipv6_module, "defragment",
1152         "Reassemble fragmented IPv6 datagrams",
1153         "Whether fragmented IPv6 datagrams should be reassembled",
1154         &ipv6_reassemble);
1155
1156   register_dissector("ipv6", dissect_ipv6, proto_ipv6);
1157   register_init_routine(ipv6_reassemble_init);
1158 }
1159
1160 void
1161 proto_reg_handoff_ipv6(void)
1162 {
1163   dissector_add("ethertype", ETHERTYPE_IPv6, dissect_ipv6, proto_ipv6);
1164   dissector_add("ppp.protocol", PPP_IPV6, dissect_ipv6, proto_ipv6);
1165   dissector_add("ip.proto", IP_PROTO_IPV6, dissect_ipv6, proto_ipv6);
1166   dissector_add("null.type", BSD_AF_INET6_BSD, dissect_ipv6, proto_ipv6);
1167   dissector_add("null.type", BSD_AF_INET6_FREEBSD, dissect_ipv6, proto_ipv6);
1168   dissector_add("ip.proto", IP_PROTO_NONE, dissect_ipv6_none, proto_ipv6);
1169 }