Added two new arguments to epan_init() and proto_init() to
[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.51 2001/01/23 02:49:55 gerald Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  *
9  * Copyright 1998 Gerald Combs
10  *
11  * MobileIPv6 support added by Tomislav Borosa <tomislav.borosa@siemens.hr>
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #ifdef HAVE_SYS_SOCKET_h
37 #include <sys/socket.h>
38 #endif
39
40 #ifdef HAVE_NETINET_IN_H
41 # include <netinet/in.h>
42 #endif
43
44 #include <string.h>
45 #include <stdio.h>
46 #include <glib.h>
47 #include "etypes.h"
48 #include "ppptypes.h"
49 #include "aftypes.h"
50 #include "packet.h"
51 #include "packet-ip.h"
52 #include "packet-ipsec.h"
53 #include "packet-ipv6.h"
54 #include "packet-tcp.h"
55 #include "packet-udp.h"
56 #include "resolv.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
78 /* BT INSERT BEGIN */
79 static int hf_ipv6_mipv6_type = -1;
80 static int hf_ipv6_mipv6_length = -1;
81 static int hf_ipv6_mipv6_a_flag = -1;
82 static int hf_ipv6_mipv6_h_flag = -1;
83 static int hf_ipv6_mipv6_r_flag = -1;
84 static int hf_ipv6_mipv6_d_flag = -1;
85 static int hf_ipv6_mipv6_m_flag = -1;
86 static int hf_ipv6_mipv6_b_flag = -1;
87 static int hf_ipv6_mipv6_prefix_length = -1;
88 static int hf_ipv6_mipv6_sequence_number = -1;
89 static int hf_ipv6_mipv6_life_time = -1;
90 static int hf_ipv6_mipv6_status = -1;
91 static int hf_ipv6_mipv6_refresh = -1;
92 static int hf_ipv6_mipv6_home_address = -1;
93 static int hf_ipv6_mipv6_sub_type = -1;
94 static int hf_ipv6_mipv6_sub_length = -1;
95 static int hf_ipv6_mipv6_sub_unique_ID = -1;
96 static int hf_ipv6_mipv6_sub_alternative_COA = -1;
97 /* BT INSERT END */
98
99 static gint ett_ipv6 = -1;
100
101 #ifndef offsetof
102 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
103 #endif
104
105 static int
106 dissect_routing6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
107     struct ip6_rthdr rt;
108     int len;
109     proto_tree *rthdr_tree;
110         proto_item *ti;
111     char buf[sizeof(struct ip6_rthdr0) + sizeof(struct e_in6_addr) * 23];
112         
113     memcpy(&rt, (void *) &pd[offset], sizeof(rt));
114     len = (rt.ip6r_len + 1) << 3;
115
116     if (tree) {
117         /* !!! specify length */
118         ti = proto_tree_add_text(tree, NullTVB, offset, len,
119             "Routing Header, Type %u", rt.ip6r_type);
120         rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
121
122         proto_tree_add_text(rthdr_tree, NullTVB,
123             offset + offsetof(struct ip6_rthdr, ip6r_nxt), 1,
124             "Next header: %s (0x%02x)", ipprotostr(rt.ip6r_nxt), rt.ip6r_nxt);
125         proto_tree_add_text(rthdr_tree, NullTVB,
126             offset + offsetof(struct ip6_rthdr, ip6r_len), 1,
127             "Length: %u (%d bytes)", rt.ip6r_len, len);
128         proto_tree_add_text(rthdr_tree, NullTVB,
129             offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
130             "Type: %u", rt.ip6r_type);
131         proto_tree_add_text(rthdr_tree, NullTVB,
132             offset + offsetof(struct ip6_rthdr, ip6r_segleft), 1,
133             "Segments left: %u", rt.ip6r_segleft);
134
135         if (rt.ip6r_type == 0 && len <= sizeof(buf)) {
136             struct e_in6_addr *a;
137             int n;
138             struct ip6_rthdr0 *rt0;
139
140             memcpy(buf, (void *) &pd[offset], len);
141             rt0 = (struct ip6_rthdr0 *)buf;
142             for (a = rt0->ip6r0_addr, n = 0;
143                  a < (struct e_in6_addr *)(buf + len);
144                  a++, n++) {
145                 proto_tree_add_text(rthdr_tree, NullTVB,
146                     offset + offsetof(struct ip6_rthdr0, ip6r0_addr) + n * sizeof(struct e_in6_addr),
147                     sizeof(struct e_in6_addr),
148 #ifdef INET6
149                     "address %d: %s (%s)",
150                     n, get_hostname6(a), ip6_to_str(a)
151 #else
152                     "address %d: %s", n, ip6_to_str(a)
153 #endif
154                     );
155             }
156         }
157   
158         /* decode... */
159     }
160
161     return len;
162 }
163
164 static int
165 dissect_frag6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, 
166     int *fragstart) {
167     struct ip6_frag frag;
168     int len;
169     proto_item *ti;
170     proto_tree *rthdr_tree;
171
172     memcpy(&frag, (void *) &pd[offset], sizeof(frag));
173     len = sizeof(frag);
174     frag.ip6f_offlg = ntohs(frag.ip6f_offlg);
175     *fragstart = frag.ip6f_offlg & IP6F_OFF_MASK;
176     if (check_col(fd, COL_INFO)) {
177         col_add_fstr(fd, COL_INFO,
178             "IPv6 fragment (nxt=%s (0x%02x) off=%u id=0x%x)",
179             ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
180             *fragstart, frag.ip6f_ident);
181     }
182     if (tree) {
183            ti = proto_tree_add_text(tree, NullTVB, offset, len,
184                            "Fragmention Header");
185            rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
186
187            proto_tree_add_text(rthdr_tree, NullTVB,
188                          offset + offsetof(struct ip6_frag, ip6f_nxt), 1,
189                          "Next header: %s (0x%02x)",
190                          ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt);
191
192         #if 0
193            proto_tree_add_text(rthdr_tree, NullTVB,
194                          offset + offsetof(struct ip6_frag, ip6f_reserved), 1,
195                          "Reserved: %u",
196                          frag.ip6f_reserved);
197         #endif
198
199            proto_tree_add_text(rthdr_tree, NullTVB,
200                          offset + offsetof(struct ip6_frag, ip6f_offlg), 2,
201                          "Offset: %u",
202                          frag.ip6f_offlg & IP6F_OFF_MASK);
203
204            proto_tree_add_text(rthdr_tree, NullTVB,
205                          offset + offsetof(struct ip6_frag, ip6f_offlg), 2,
206                          "More fragments: %s",
207                                 frag.ip6f_offlg & IP6F_MORE_FRAG ?
208                                 "Yes" : "No");
209
210            proto_tree_add_text(rthdr_tree, NullTVB,
211                          offset + offsetof(struct ip6_frag, ip6f_ident), 4,
212                          "Identification: 0x%08x",
213                          frag.ip6f_ident);
214     }
215     return len;
216 }
217
218 /* BT INSERT BEGIN */
219 static int
220 dissect_mipv6_ba(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset) {
221         guint8 status, len=0;
222         gchar status_text[80]="";
223         gboolean sub_options=FALSE;
224
225         proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_type, tvb, offset+len,
226         IP6_MIPv6_OPTION_TYPE_LENGTH, tvb_get_guint8(tvb,offset+len),
227         "Option Type: %u (0x%02x) - Binding Acknowledgement", tvb_get_guint8(tvb,offset+len),
228         tvb_get_guint8(tvb,offset+len));
229   len+=IP6_MIPv6_OPTION_TYPE_LENGTH;
230   if (tvb_get_guint8(tvb,offset+len)>11)
231                 sub_options=TRUE;
232   proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_length, tvb, offset+len,
233         IP6_MIPv6_OPTION_LENGTH_LENGTH, tvb_get_guint8(tvb,offset+len));
234   len+=IP6_MIPv6_OPTION_LENGTH_LENGTH;
235         status = tvb_get_guint8(tvb,offset+len);
236         switch (status){
237                 case BA_OK:
238                         strcpy(status_text,"- Binding Update accepted");
239                         break;
240                 case BA_REAS_UNSPEC:
241                         strcpy(status_text,"- Binding Update was rejected - Reason unspecified");
242                         break;
243                 case BA_ADMIN_PROH:
244                         strcpy(status_text,"- Binding Update was rejected - Administratively prohibited");
245                         break;
246                 case BA_INSUF_RES:
247                         strcpy(status_text,"- Binding Update was rejected - Insufficient resources");
248                         break;
249                 case BA_NO_HR:
250                         strcpy(status_text,"- Binding Update was rejected - Home registration not supported");
251                         break;
252                 case BA_NO_SUBNET:
253                         strcpy(status_text,"- Binding Update was rejected - Not home subnet");
254                         break;
255                 case BA_ERR_ID_LEN:
256                         strcpy(status_text,"- Binding Update was rejected - Incorrect interface identifier length");
257                         break;
258                 case BA_NO_HA:
259                         strcpy(status_text,"- Binding Update was rejected - Not home agent for this mobile node");
260                         break;
261                 case BA_DUPL_ADDR:
262                         strcpy(status_text,"- Binding Update was rejected - Duplicate Address Detection failed");
263                         break;
264                 default:
265                         strcpy(status_text,"");
266                         break;
267         }
268   if ((status>128)&&(strlen(status_text)==0))
269                 strcpy(status_text,"- Binding Update was rejected");
270         proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_status, tvb, offset+len,
271         IP6_MIPv6_STATUS_LENGTH, tvb_get_guint8(tvb,offset+len),
272         "Status: %u %s", tvb_get_guint8(tvb,offset+len), status_text);
273   len+=IP6_MIPv6_STATUS_LENGTH;
274   proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_sequence_number, tvb, offset+len,
275         IP6_MIPv6_SEQUENCE_NUMBER_LENGTH, tvb_get_ntohs(tvb,offset+len));
276   len+=IP6_MIPv6_SEQUENCE_NUMBER_LENGTH;
277   if (tvb_get_ntohl(tvb,offset+len)==0xffffffff)
278         {
279                 proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_life_time, tvb, offset+len,
280                 IP6_MIPv6_LIFE_TIME_LENGTH, tvb_get_ntohl(tvb,offset+len),
281                 "Life Time: %u - Infinity", tvb_get_ntohl(tvb,offset+len));
282         } else
283         {
284                 proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_life_time, tvb, offset+len,
285                 IP6_MIPv6_LIFE_TIME_LENGTH, tvb_get_ntohl(tvb,offset+len));
286   }
287   len+=IP6_MIPv6_LIFE_TIME_LENGTH;
288         proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_refresh, tvb, offset+len,
289         IP6_MIPv6_REFRESH_LENGTH, tvb_get_ntohl(tvb,offset+len));
290   len+=IP6_MIPv6_REFRESH_LENGTH;
291   /* sub - options */
292   if (sub_options)
293   {
294                 proto_tree_add_text(dstopt_tree, NullTVB, offset+len, 1, "Sub-Options");
295         }
296   return len;
297 }
298
299 static int
300 dissect_mipv6_bu(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset) {
301         int len=0;
302         gboolean sub_options=FALSE;
303
304         proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_type, tvb, offset,
305         IP6_MIPv6_OPTION_TYPE_LENGTH, tvb_get_guint8(tvb,offset),
306         "Option Type: %u (0x%02x) - Binding Update", tvb_get_guint8(tvb,offset),
307         tvb_get_guint8(tvb,offset));
308   len+=IP6_MIPv6_OPTION_TYPE_LENGTH;
309   if (tvb_get_guint8(tvb,offset+len)>8)
310   sub_options=TRUE;
311   proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_length, tvb, offset+len,
312   IP6_MIPv6_OPTION_LENGTH_LENGTH, tvb_get_guint8(tvb,offset+len));
313   len+=IP6_MIPv6_OPTION_LENGTH_LENGTH;
314   proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_a_flag, tvb, offset+len,
315   IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset+len));
316   proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_h_flag, tvb, offset+len,
317   IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset+len));
318   proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_r_flag, tvb, offset+len,
319   IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset+len));
320   proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_d_flag, tvb, offset+len,
321   IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset+len));
322   proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_m_flag, tvb, offset+len,
323   IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset+len));
324   proto_tree_add_boolean(dstopt_tree, hf_ipv6_mipv6_b_flag, tvb, offset+len,
325   IP6_MIPv6_FLAGS_LENGTH, tvb_get_guint8(tvb, offset+len));
326   len+=IP6_MIPv6_FLAGS_LENGTH;
327   proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_prefix_length, tvb, offset+len,
328         IP6_MIPv6_PREFIX_LENGTH_LENGTH, tvb_get_guint8(tvb,offset+len));
329   len+=IP6_MIPv6_PREFIX_LENGTH_LENGTH;
330   proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_sequence_number, tvb, offset+len,
331         IP6_MIPv6_SEQUENCE_NUMBER_LENGTH, tvb_get_ntohs(tvb,offset+len));
332   len+=IP6_MIPv6_SEQUENCE_NUMBER_LENGTH;
333   if (tvb_get_ntohl(tvb,offset+len)==0xffffffff)
334         {
335                 proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_life_time, tvb, offset+len,
336                 IP6_MIPv6_LIFE_TIME_LENGTH, tvb_get_ntohl(tvb,offset+len),
337                 "Life Time: %u - Infinity", tvb_get_ntohl(tvb,offset+len));
338         } else
339         {
340                 proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_life_time, tvb, offset+len,
341                 IP6_MIPv6_LIFE_TIME_LENGTH, tvb_get_ntohl(tvb,offset+len));
342   }
343   len+=IP6_MIPv6_LIFE_TIME_LENGTH;
344   /* sub - options */
345   if (sub_options)
346   {
347                 proto_tree_add_text(dstopt_tree, NullTVB, offset+len, 1, "Sub-Options");
348         }
349   return len;
350 }
351
352 static int
353 dissect_mipv6_ha(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset) {
354         int len=0;
355         gboolean sub_options=FALSE;
356
357         proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_type, tvb, offset+len,
358         IP6_MIPv6_OPTION_TYPE_LENGTH, tvb_get_guint8(tvb,offset+len),
359         "Option Type: %u (0x%02x) - Home Address", tvb_get_guint8(tvb,offset+len),
360         tvb_get_guint8(tvb,offset+len));
361   len+=IP6_MIPv6_OPTION_TYPE_LENGTH;
362   if (tvb_get_guint8(tvb,offset+len)>16)
363                 sub_options=TRUE;
364   proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_length, tvb, offset+len,
365         IP6_MIPv6_OPTION_LENGTH_LENGTH, tvb_get_guint8(tvb,offset+len));
366   len+=IP6_MIPv6_OPTION_LENGTH_LENGTH;
367   proto_tree_add_ipv6(dstopt_tree, hf_ipv6_mipv6_home_address, tvb, offset+len,
368         IP6_MIPv6_HOME_ADDRESS_LENGTH, tvb_get_ptr(tvb,offset+len,IP6_MIPv6_HOME_ADDRESS_LENGTH));
369   len+=IP6_MIPv6_HOME_ADDRESS_LENGTH;                                           
370   /* sub - options */
371   if (sub_options)
372   {
373                 proto_tree_add_text(dstopt_tree, NullTVB, offset+len, 1, "Sub-Options");
374         }
375         return len;
376 }
377
378 static int
379 dissect_mipv6_br(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset) {
380         int len=0;      
381         gboolean sub_options=FALSE;
382
383         proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_type, tvb, offset+len,
384         IP6_MIPv6_OPTION_TYPE_LENGTH, tvb_get_guint8(tvb,offset+len),
385         "Option Type: %u (0x%02x) - Binding Request", tvb_get_guint8(tvb,offset+len),
386         tvb_get_guint8(tvb,offset+len));
387   len+=IP6_MIPv6_OPTION_TYPE_LENGTH;
388   if (tvb_get_guint8(tvb,offset+len)>0)
389                 sub_options=TRUE;
390   proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_length, tvb, offset+len,
391         IP6_MIPv6_OPTION_LENGTH_LENGTH, tvb_get_guint8(tvb,offset+len));
392   len+=IP6_MIPv6_OPTION_LENGTH_LENGTH;
393   /* sub - options */
394   if (sub_options)
395   {
396                 proto_tree_add_text(dstopt_tree, NullTVB, offset+len, 1, "Sub-Options");
397         }
398         return len;
399 }
400
401 static int
402 dissect_mipv6_sub_u(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset) {
403         int len=0;
404                                         
405         proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_sub_length, tvb, offset+len,
406         IP6_MIPv6_SUB_TYPE_LENGTH, tvb_get_guint8(tvb,offset+len),
407         "Sub-Option Type: %u (0x%02x) - Unique Identifier Sub-Option", tvb_get_guint8(tvb,offset+len),
408         tvb_get_guint8(tvb,offset+len));
409   len+=IP6_MIPv6_SUB_TYPE_LENGTH;
410   proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_sub_length, tvb, offset+len,
411         IP6_MIPv6_SUB_LENGTH_LENGTH, tvb_get_guint8(tvb,offset+len));
412   len+=IP6_MIPv6_SUB_LENGTH_LENGTH;
413   proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_sub_unique_ID, tvb, offset+len,
414         IP6_MIPv6_SUB_UNIQUE_ID_LENGTH, tvb_get_ntohs(tvb,offset+len));
415   len+=IP6_MIPv6_SUB_UNIQUE_ID_LENGTH;                                          
416         return len;
417 }
418
419 static int
420 dissect_mipv6_sub_a_coa(tvbuff_t *tvb, proto_tree *dstopt_tree, int offset) {
421         int len=0;
422
423         proto_tree_add_uint_format(dstopt_tree, hf_ipv6_mipv6_sub_type, tvb, offset+len,
424         IP6_MIPv6_SUB_TYPE_LENGTH, tvb_get_guint8(tvb,offset+len),
425         "Sub-Option Type: %u (0x%02x) - Alternative Care Of Address", tvb_get_guint8(tvb,offset+len),
426         tvb_get_guint8(tvb,offset+len));
427   len+=IP6_MIPv6_SUB_TYPE_LENGTH;
428   proto_tree_add_uint(dstopt_tree, hf_ipv6_mipv6_sub_length, tvb, offset+len,
429         IP6_MIPv6_SUB_LENGTH_LENGTH, tvb_get_guint8(tvb,offset+len));
430   len+=IP6_MIPv6_SUB_LENGTH_LENGTH;
431   proto_tree_add_ipv6(dstopt_tree, hf_ipv6_mipv6_sub_alternative_COA, tvb, offset+len,
432         IP6_MIPv6_SUB_ALTERNATIVE_COA_LENGTH, tvb_get_ptr(tvb,offset+len,IP6_MIPv6_SUB_ALTERNATIVE_COA_LENGTH));
433   len+=IP6_MIPv6_SUB_ALTERNATIVE_COA_LENGTH;                                            
434         return len;
435 }
436 /* BT INSERT END */
437
438 static int
439 dissect_opts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
440   char *optname) {
441         struct ip6_ext ext;
442   int len;
443   proto_tree *dstopt_tree;
444         proto_item *ti;
445   u_char *p;
446
447         /* BT INSERT BEGIN */
448         tvbuff_t *tvb;
449   int mip_offset=0, delta=0;
450   /* BT INSERT END */
451
452   static const value_string rtalertvals[] = {
453         { IP6OPT_RTALERT_MLD, "MLD" },
454         { IP6OPT_RTALERT_RSVP, "RSVP" },
455         { 0, NULL },
456     };
457
458   memcpy(&ext, (void *) &pd[offset], sizeof(ext));
459   len = (ext.ip6e_len + 1) << 3;
460
461   /* BT INSERT BEGIN */
462         tvb = tvb_create_from_top(offset);
463   /* BT INSERT END */
464
465         if (tree) {
466                 /* !!! specify length */
467                 ti = proto_tree_add_text(tree, NullTVB, offset, len,
468             "%s Header ", optname);
469
470                 dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
471
472                 proto_tree_add_text(dstopt_tree, NullTVB,
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, NullTVB,
476                 offset + offsetof(struct ip6_ext, ip6e_len), 1,
477           "Length: %u (%d bytes)", ext.ip6e_len, len);
478
479     /* BT INSERT BEGIN */
480     mip_offset+=2;
481     /* BT INSERT END */
482
483                 p = (u_char *)(pd + offset + 2);
484
485                 while (p < pd + offset + len) {
486                 switch (p[0]) {
487                 case IP6OPT_PAD1:
488                                         proto_tree_add_text(dstopt_tree, NullTVB, p - pd, 1,
489                         "Pad1");
490                                         p++;
491           /* BT INSERT BEGIN */
492                                         mip_offset++;
493           /* BT INSERT END */
494                                         break;
495                 case IP6OPT_PADN:
496                                         proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
497                         "PadN: %u bytes", p[1] + 2);
498                                         p += p[1];
499                                         p += 2;
500           /* BT INSERT BEGIN */
501                                         mip_offset+=tvb_get_guint8(tvb,mip_offset+1)+2;
502           /* BT INSERT END */
503                                         break;
504                 case IP6OPT_JUMBO:
505                                         if (p[1] == 4) {
506                                 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
507                                                 "Jumbo payload: %u (%u bytes)",
508                                                 pntohl(&p[2]), p[1] + 2);
509                                         } else {
510                                 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
511                                                 "Jumbo payload: Invalid length (%u bytes)",
512                                                 p[1] + 2);
513                                         }
514                                         p += p[1];
515                                         p += 2;
516           /* BT INSERT BEGIN */
517                                         mip_offset+=tvb_get_guint8(tvb,mip_offset+1)+2;
518           /* BT INSERT END */
519                                         break;
520                 case IP6OPT_RTALERT:
521                 {
522                                         char *rta;
523
524                                         if (p[1] == 2) {
525                                 rta = val_to_str(pntohs(&p[2]), rtalertvals,
526                                                 "Unknown");
527                                         } else
528                                 rta = "Invalid length";
529                                         ti = proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
530                                         "Router alert: %s (%u bytes)", rta, p[1] + 2);
531                                         p += p[1];
532                                         p += 2;
533                                         /* BT INSERT BEGIN */
534                                         mip_offset+=tvb_get_guint8(tvb,mip_offset+1)+2;
535           /* BT INSERT END */
536                                         break;
537                 }
538         /* BT INSERT BEGIN */
539                         case IP6OPT_BINDING_UPDATE :
540                 {       
541             delta=dissect_mipv6_bu(tvb, dstopt_tree, mip_offset);
542                                                 p+=delta;
543                                                 mip_offset+=delta;
544                         break;
545                 }
546                         case IP6OPT_BINDING_ACK :
547                 {                       
548             delta=dissect_mipv6_ba(tvb, dstopt_tree, mip_offset);
549                                                 p+=delta;
550                                                 mip_offset+=delta;
551                                                 break;                  
552                                         }
553                         case IP6OPT_HOME_ADDRESS :
554                 {
555             delta=dissect_mipv6_ha(tvb, dstopt_tree, mip_offset);
556                                                 p+=delta;
557                                                 mip_offset+=delta;
558                                                 break;                  
559           }
560                         case IP6OPT_BINDING_REQUEST :
561                 {
562                   delta=dissect_mipv6_br(tvb, dstopt_tree, mip_offset);
563                                                 p+=delta;
564                                                 mip_offset+=delta;
565                                                 break;
566                                         }
567                         case IP6OPT_MIPv6_UNIQUE_ID_SUB :
568                 {
569                   delta=dissect_mipv6_sub_u(tvb, dstopt_tree, mip_offset);
570                                                 p+=delta;
571                                                 mip_offset+=delta;
572                                                 break;
573                                         }
574                         case IP6OPT_MIPv6_ALTERNATIVE_COA_SUB :
575                 {
576                   delta=dissect_mipv6_sub_a_coa(tvb, dstopt_tree, mip_offset);
577                                                 p+=delta;
578                                                 mip_offset+=delta;
579                                                 break;
580                                         }
581         /* BT INSERT END */
582                 default:
583                                         p = (u_char *)(pd + offset + len);
584                                         break;
585                 }
586                 }
587
588         /* decode... */
589
590         }
591     return len;
592 }
593
594 static int
595 dissect_hopopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
596     return dissect_opts(pd, offset, fd, tree, "Hop-by-hop Option");
597 }
598
599 static int
600 dissect_dstopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
601     return dissect_opts(pd, offset, fd, tree, "Destination Option");
602 }
603
604 void
605 dissect_ipv6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
606   proto_tree *ipv6_tree;
607   proto_item *ti;
608   guint8 nxt;
609   int advance;
610   int poffset;
611   int frag;
612
613   struct ip6_hdr ipv6;
614
615   OLD_CHECK_DISPLAY_AS_DATA(proto_ipv6, pd, offset, fd, tree);
616
617   memcpy(&ipv6, (void *) &pd[offset], sizeof(ipv6)); 
618
619   pi.ipproto = ipv6.ip6_nxt; /* XXX make work TCP follow (ipproto = 6) */
620
621   SET_ADDRESS(&pi.net_src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
622   SET_ADDRESS(&pi.src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
623   SET_ADDRESS(&pi.net_dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
624   SET_ADDRESS(&pi.dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
625
626   if (tree) {
627     /* !!! specify length */
628     ti = proto_tree_add_item(tree, proto_ipv6, NullTVB, offset, 40, FALSE);
629     ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
630
631     /* !!! warning: version also contains 4 Bit priority */
632     proto_tree_add_uint(ipv6_tree, hf_ipv6_version, NullTVB,
633                 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
634                 (ipv6.ip6_vfc >> 4) & 0x0f);
635
636
637     proto_tree_add_uint(ipv6_tree, hf_ipv6_class, NullTVB,
638                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
639                 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
640
641     /*
642      * there should be no alignment problems for ip6_flow, since it's the first
643      * guint32 in the ipv6 struct
644      */
645     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_flow, NullTVB,
646                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
647                 (unsigned long)(ntohl(ipv6.ip6_flow) & IPV6_FLOWLABEL_MASK),
648                 "Flowlabel: 0x%05lx",
649                 (unsigned long)(ntohl(ipv6.ip6_flow) & IPV6_FLOWLABEL_MASK));
650
651     proto_tree_add_uint(ipv6_tree, hf_ipv6_plen, NullTVB,
652                 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
653                 ntohs(ipv6.ip6_plen));
654
655     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_nxt, NullTVB,
656                 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
657                 ipv6.ip6_nxt,
658                 "Next header: %s (0x%02x)",
659                 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
660
661     proto_tree_add_uint(ipv6_tree, hf_ipv6_hlim, NullTVB,
662                 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
663                 ipv6.ip6_hlim);
664
665     proto_tree_add_ipv6_hidden(ipv6_tree, hf_ipv6_addr, NullTVB, 
666                                offset + offsetof(struct ip6_hdr, ip6_src), 16,
667                                ipv6.ip6_src.s6_addr8);
668     proto_tree_add_ipv6_hidden(ipv6_tree, hf_ipv6_addr, NullTVB, 
669                                offset + offsetof(struct ip6_hdr, ip6_dst), 16,
670                                ipv6.ip6_dst.s6_addr8);
671
672     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_src, NullTVB,
673                 offset + offsetof(struct ip6_hdr, ip6_src), 16,
674                 (guint8 *)&ipv6.ip6_src,
675 #ifdef INET6
676                 "Source address: %s (%s)",
677                 get_hostname6(&ipv6.ip6_src),
678 #else
679                 "Source address: %s",
680 #endif
681                 ip6_to_str(&ipv6.ip6_src));
682
683     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_dst, NullTVB,
684                 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
685                 (guint8 *)&ipv6.ip6_dst,
686 #ifdef INET6
687                 "Destination address: %s (%s)",
688                 get_hostname6(&ipv6.ip6_dst),
689 #else
690                 "Destination address: %s",
691 #endif
692                 ip6_to_str(&ipv6.ip6_dst));
693   }
694
695   /* start of the new header (could be a extension header) */
696   nxt = pd[poffset = offset + offsetof(struct ip6_hdr, ip6_nxt)];
697   offset += sizeof(struct ip6_hdr);
698   frag = 0;
699
700 /* start out assuming this insn't fragmented */
701         pi.fragmented = FALSE;
702
703 again:
704     switch (nxt) {
705     case IP_PROTO_HOPOPTS:
706                         advance = dissect_hopopts(pd, offset, fd, tree);
707                         nxt = pd[poffset = offset];
708                         offset += advance;
709                         goto again;
710     case IP_PROTO_ROUTING:
711                         advance = dissect_routing6(pd, offset, fd, tree);
712                         nxt = pd[poffset = offset];
713                         offset += advance;
714                         goto again;
715     case IP_PROTO_FRAGMENT:
716                         pi.fragmented = TRUE;
717                         advance = dissect_frag6(pd, offset, fd, tree, &frag);
718                         nxt = pd[poffset = offset];
719                         offset += advance;
720                         goto again;
721     case IP_PROTO_AH:
722                         advance = dissect_ah_old(pd, offset, fd, tree);
723                         nxt = pd[poffset = offset];
724                         offset += advance;
725         goto again;
726     case IP_PROTO_DSTOPTS:
727                         advance = dissect_dstopts(pd, offset, fd, tree);
728                         nxt = pd[poffset = offset];
729                         offset += advance;
730                         goto again;
731     }
732
733 #ifdef TEST_FINALHDR
734   proto_tree_add_uint_hidden(ipv6_tree, hf_ipv6_final, NullTVB, poffset, 1, nxt);
735 #endif
736   if (frag) {
737     /* fragmented */
738     if (check_col(fd, COL_PROTOCOL))
739       col_set_str(fd, COL_PROTOCOL, "IPv6");
740     /* COL_INFO was filled in by "dissect_frag6()" */
741     old_dissect_data(pd, offset, fd, tree);
742   }     else {
743     /* do lookup with the subdissector table */
744     if (!old_dissector_try_port(ip_dissector_table, nxt, pd, offset, fd, tree)) {
745       /* Unknown protocol */
746       if (check_col(fd, COL_PROTOCOL))
747         col_set_str(fd, COL_PROTOCOL, "IPv6");
748       if (check_col(fd, COL_INFO))
749         col_add_fstr(fd, COL_INFO, "%s (0x%02x)", ipprotostr(nxt),nxt);
750       old_dissect_data(pd, offset, fd, tree);
751     }
752   }
753 }
754
755 static void
756 dissect_ipv6_none(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
757   /* BT INSERT BEGIN */
758   if (hf_ipv6_mipv6_length!=-1) {
759         if (check_col(fd, COL_PROTOCOL))
760         col_set_str(fd, COL_PROTOCOL, "IPv6");
761         if (check_col(fd, COL_INFO))
762         col_add_fstr(fd, COL_INFO, "MobileIPv6 Destination Option");
763         } else {
764   /* BT INSERT END */
765   if (check_col(fd, COL_INFO))
766     col_add_fstr(fd, COL_INFO, "IPv6 no next header");
767   }
768   /* XXX - dissect the payload as padding? */
769 }
770
771 void
772 proto_register_ipv6(void)
773 {
774   static hf_register_info hf[] = {
775     { &hf_ipv6_version,
776       { "Version",              "ipv6.version",
777                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
778     { &hf_ipv6_class,
779       { "Traffic class",        "ipv6.class",
780                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
781     { &hf_ipv6_flow,
782       { "Flowlabel",            "ipv6.flow",
783                                 FT_UINT32, BASE_HEX, NULL, 0x0, "" }},
784     { &hf_ipv6_plen,
785       { "Payload length",       "ipv6.plen",
786                                 FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
787     { &hf_ipv6_nxt,
788       { "Next header",          "ipv6.nxt",
789                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
790     { &hf_ipv6_hlim,
791       { "Hop limit",            "ipv6.hlim",
792                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
793     { &hf_ipv6_src,
794       { "Source",               "ipv6.src",
795                                 FT_IPv6, BASE_NONE, NULL, 0x0,
796                                 "Source IPv6 Address" }},
797     { &hf_ipv6_dst,
798       { "Destination",          "ipv6.dst",
799                                 FT_IPv6, BASE_NONE, NULL, 0x0,
800                                 "Destination IPv6 Address" }},
801     { &hf_ipv6_addr,
802       { "Address",              "ipv6.addr",
803                                 FT_IPv6, BASE_NONE, NULL, 0x0,
804                                 "Source or Destination IPv6 Address" }},
805
806     /* BT INSERT BEGIN */
807     { &hf_ipv6_mipv6_type,
808       { "Option Type ",         "ipv6.mipv6_type",
809                                 FT_UINT8, BASE_DEC, NULL, 0x0,
810                                 "" }},
811     { &hf_ipv6_mipv6_length,
812       { "Option Length ",               "ipv6.mipv6_length",
813                                 FT_UINT8, BASE_DEC, NULL, 0x0,
814                                 "" }},
815     { &hf_ipv6_mipv6_a_flag,
816       { "Acknowledge (A) ",             "ipv6.mipv6_a_flag",
817                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_a_flag_value),
818                                 IP6_MIPv6_BU_A_FLAG,
819                                 "" }},
820     { &hf_ipv6_mipv6_h_flag,
821       { "Home Registration (H) ",               "ipv6.mipv6_h_flag",
822                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_h_flag_value),
823                                 IP6_MIPv6_BU_H_FLAG,
824                                 "" }},
825     { &hf_ipv6_mipv6_r_flag,
826       { "Router (R) ",          "ipv6.mipv6_r_flag",
827                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_r_flag_value),
828                                 IP6_MIPv6_BU_R_FLAG,
829                                 "" }},
830     { &hf_ipv6_mipv6_d_flag,
831       { "Duplicate Address Detection (D) ",             "ipv6.mipv6_d_flag",
832                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_d_flag_value),
833                                 IP6_MIPv6_BU_D_FLAG,
834                                 "" }},
835     { &hf_ipv6_mipv6_m_flag,
836       { "MAP Registration (M) ",                "ipv6.mipv6_m_flag",
837                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_m_flag_value),
838                                 IP6_MIPv6_BU_M_FLAG,
839                                 "" }},
840     { &hf_ipv6_mipv6_b_flag,
841       { "Bicasting all (B) ",           "ipv6.mipv6_b_flag",
842                                 FT_BOOLEAN, 8, TFS(&ipv6_mipv6_bu_b_flag_value),
843                                 IP6_MIPv6_BU_B_FLAG,
844                                 "" }},
845     { &hf_ipv6_mipv6_prefix_length,
846       { "Prefix Length ",               "ipv6.mipv6_prefix_length",
847                                 FT_UINT8, BASE_DEC, NULL, 0x0,
848                                 "" }},
849     { &hf_ipv6_mipv6_sequence_number,
850       { "Sequence Number ",             "ipv6.mipv6_sequence_number",
851                                 FT_UINT16, BASE_DEC, NULL, 0x0,
852                                 "" }},
853     { &hf_ipv6_mipv6_life_time,
854       { "Life Time ",           "ipv6.mipv6_life_time",
855                                 FT_UINT32, BASE_DEC, NULL, 0x0,
856                                 "" }},
857     { &hf_ipv6_mipv6_status,
858       { "Status ",              "ipv6.mipv6_status",
859                                 FT_UINT8, BASE_DEC, NULL, 0x0,
860                                 "" }},
861     { &hf_ipv6_mipv6_refresh,
862       { "Refresh ",             "ipv6.mipv6_refresh",
863                                 FT_UINT32, BASE_DEC, NULL, 0x0,
864                                 "" }},
865     { &hf_ipv6_mipv6_home_address,
866       { "Home Address ",                "ipv6.mipv6_home_address",
867                                 FT_IPv6, BASE_HEX, NULL, 0x0,
868                                 "" }},
869     { &hf_ipv6_mipv6_sub_type,
870       { "Sub-Option Type ",             "ipv6.mipv6_sub_type",
871                                 FT_UINT8, BASE_NONE, NULL, 0x0,
872                                 "" }},
873     { &hf_ipv6_mipv6_sub_length,
874       { "Sub-Option Length ",           "ipv6.mipv6_sub_length",
875                                 FT_UINT8, BASE_NONE, NULL, 0x0,
876                                 "" }},
877     { &hf_ipv6_mipv6_sub_unique_ID,
878       { "Unique Identifier ",           "ipv6.mipv6_sub_unique_ID",
879                                 FT_UINT16, BASE_NONE, NULL, 0x0,
880                                 "" }},
881     { &hf_ipv6_mipv6_sub_alternative_COA,
882       { "Alternative Care of Address ",         "ipv6.mipv6_sub_alternative_COA",
883                                 FT_IPv6, BASE_HEX, NULL, 0x0,
884                                 "" }},
885
886     /* BT INSERT END */
887 #ifdef TEST_FINALHDR
888     { &hf_ipv6_final,
889       { "Final next header",    "ipv6.final",
890                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
891 #endif
892   };
893   static gint *ett[] = {
894     &ett_ipv6,
895   };
896
897   proto_ipv6 = proto_register_protocol("Internet Protocol Version 6", "IPv6", "ipv6");
898   proto_register_field_array(proto_ipv6, hf, array_length(hf));
899   proto_register_subtree_array(ett, array_length(ett));
900 }
901
902 void
903 proto_reg_handoff_ipv6(void)
904 {
905         old_dissector_add("ethertype", ETHERTYPE_IPv6, dissect_ipv6, proto_ipv6);
906         old_dissector_add("ppp.protocol", PPP_IPV6, dissect_ipv6, proto_ipv6);
907         old_dissector_add("ip.proto", IP_PROTO_IPV6, dissect_ipv6, proto_ipv6);
908         old_dissector_add("null.type", BSD_AF_INET6_BSD, dissect_ipv6, proto_ipv6);
909         old_dissector_add("null.type", BSD_AF_INET6_FREEBSD, dissect_ipv6, proto_ipv6);
910         old_dissector_add("ip.proto", IP_PROTO_NONE, dissect_ipv6_none, proto_ipv6);
911 }