In the final scene of the horror movie, just when you think the monster
[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.39 2000/06/05 03:21:03 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_SYS_SOCKET_h
35 #include <sys/socket.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <string.h>
43 #include <stdio.h>
44 #include <glib.h>
45 #include "etypes.h"
46 #include "ppptypes.h"
47 #include "packet.h"
48 #include "packet-ip.h"
49 #include "packet-ipsec.h"
50 #include "packet-ipv6.h"
51 #include "packet-tcp.h"
52 #include "packet-udp.h"
53 #include "resolv.h"
54
55 /*
56  * NOTE: ipv6.nxt is not very useful as we will have chained header.
57  * now testing ipv6.final, but it raises SEGV.
58 #define TEST_FINALHDR
59  */
60
61 static int proto_ipv6 = -1;
62 static int hf_ipv6_version = -1;
63 static int hf_ipv6_class = -1;
64 static int hf_ipv6_flow = -1;
65 static int hf_ipv6_plen = -1;
66 static int hf_ipv6_nxt = -1;
67 static int hf_ipv6_hlim = -1;
68 static int hf_ipv6_src = -1;
69 static int hf_ipv6_dst = -1;
70 #ifdef TEST_FINALHDR
71 static int hf_ipv6_final = -1;
72 #endif
73
74 static gint ett_ipv6 = -1;
75
76 #ifndef offsetof
77 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
78 #endif
79
80 static int
81 dissect_routing6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
82     struct ip6_rthdr rt;
83     int len;
84     proto_tree *rthdr_tree;
85         proto_item *ti;
86     char buf[sizeof(struct ip6_rthdr0) + sizeof(struct e_in6_addr) * 23];
87
88     memcpy(&rt, (void *) &pd[offset], sizeof(rt));
89     len = (rt.ip6r_len + 1) << 3;
90
91     if (tree) {
92         /* !!! specify length */
93         ti = proto_tree_add_text(tree, NullTVB, offset, len,
94             "Routing Header, Type %u", rt.ip6r_type);
95         rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
96
97         proto_tree_add_text(rthdr_tree, NullTVB,
98             offset + offsetof(struct ip6_rthdr, ip6r_nxt), 1,
99             "Next header: %s (0x%02x)", ipprotostr(rt.ip6r_nxt), rt.ip6r_nxt);
100         proto_tree_add_text(rthdr_tree, NullTVB,
101             offset + offsetof(struct ip6_rthdr, ip6r_len), 1,
102             "Length: %u (%d bytes)", rt.ip6r_len, len);
103         proto_tree_add_text(rthdr_tree, NullTVB,
104             offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
105             "Type: %u", rt.ip6r_type);
106         proto_tree_add_text(rthdr_tree, NullTVB,
107             offset + offsetof(struct ip6_rthdr, ip6r_segleft), 1,
108             "Segments left: %u", rt.ip6r_segleft);
109
110         if (rt.ip6r_type == 0 && len <= sizeof(buf)) {
111             struct e_in6_addr *a;
112             int n;
113             struct ip6_rthdr0 *rt0;
114
115             memcpy(buf, (void *) &pd[offset], len);
116             rt0 = (struct ip6_rthdr0 *)buf;
117             for (a = rt0->ip6r0_addr, n = 0;
118                  a < (struct e_in6_addr *)(buf + len);
119                  a++, n++) {
120                 proto_tree_add_text(rthdr_tree, NullTVB,
121                     offset + offsetof(struct ip6_rthdr0, ip6r0_addr) + n * sizeof(struct e_in6_addr),
122                     sizeof(struct e_in6_addr),
123 #ifdef INET6
124                     "address %d: %s (%s)",
125                     n, get_hostname6(a), ip6_to_str(a)
126 #else
127                     "address %d: %s", n, ip6_to_str(a)
128 #endif
129                     );
130             }
131         }
132   
133         /* decode... */
134     }
135
136     return len;
137 }
138
139 static int
140 dissect_frag6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, 
141     int *fragstart) {
142     struct ip6_frag frag;
143     int len;
144
145     memcpy(&frag, (void *) &pd[offset], sizeof(frag));
146     len = sizeof(frag);
147     *fragstart = ntohs(frag.ip6f_offlg) & 0xfff8;
148     if (check_col(fd, COL_INFO)) {
149         col_add_fstr(fd, COL_INFO,
150             "IPv6 fragment (nxt=%s (0x%02x) off=%u id=0x%x)",
151             ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
152             *fragstart, frag.ip6f_ident);
153     }
154     return len;
155 }
156
157 static int
158 dissect_opts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
159     char *optname) {
160     struct ip6_ext ext;
161     int len;
162     proto_tree *dstopt_tree;
163         proto_item *ti;
164     u_char *p;
165     static const value_string rtalertvals[] = {
166         { IP6OPT_RTALERT_MLD, "MLD" },
167         { IP6OPT_RTALERT_RSVP, "RSVP" },
168         { 0, NULL },
169     };
170
171     memcpy(&ext, (void *) &pd[offset], sizeof(ext)); 
172     len = (ext.ip6e_len + 1) << 3;
173
174     if (tree) {
175         /* !!! specify length */
176         ti = proto_tree_add_text(tree, NullTVB, offset, len,
177             "%s Header", optname);
178         dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
179
180         proto_tree_add_text(dstopt_tree, NullTVB,
181             offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
182             "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
183         proto_tree_add_text(dstopt_tree, NullTVB,
184             offset + offsetof(struct ip6_ext, ip6e_len), 1,
185             "Length: %u (%d bytes)", ext.ip6e_len, len);
186
187         p = (u_char *)(pd + offset + 2);
188         while (p < pd + offset + len) {
189             switch (p[0]) {
190             case IP6OPT_PAD1:
191                 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, 1,
192                     "Pad1");
193                 p++;
194                 break;
195             case IP6OPT_PADN:
196                 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
197                     "PadN: %u bytes", p[1] + 2);
198                 p += p[1];
199                 p += 2;
200                 break;
201             case IP6OPT_JUMBO:
202                 if (p[1] == 4) {
203                     proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
204                         "Jumbo payload: %u (%u bytes)",
205                         pntohl(&p[2]), p[1] + 2);
206                 } else {
207                     proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
208                         "Jumbo payload: Invalid length (%u bytes)",
209                         p[1] + 2);
210                 }
211                 p += p[1];
212                 p += 2;
213                 break;
214             case IP6OPT_RTALERT:
215               {
216                 char *rta;
217
218                 if (p[1] == 2) {
219                     rta = val_to_str(pntohs(&p[2]), rtalertvals,
220                                 "Unknown");
221                 } else
222                     rta = "Invalid length";
223                 ti = proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
224                     "Router alert: %s (%u bytes)", rta, p[1] + 2);
225                 p += p[1];
226                 p += 2;
227                 break;
228               }
229             default:
230                 p = (u_char *)(pd + offset + len);
231                 break;
232             }
233         }
234
235         /* decode... */
236     }
237
238     return len;
239 }
240
241 static int
242 dissect_hopopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
243     return dissect_opts(pd, offset, fd, tree, "Hop-by-hop Option");
244 }
245
246 static int
247 dissect_dstopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
248     return dissect_opts(pd, offset, fd, tree, "Destination Option");
249 }
250
251 void
252 dissect_ipv6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
253   proto_tree *ipv6_tree;
254   proto_item *ti;
255   guint8 nxt;
256   int advance;
257   int poffset;
258   int frag;
259
260   struct ip6_hdr ipv6;
261
262   memcpy(&ipv6, (void *) &pd[offset], sizeof(ipv6)); 
263
264   SET_ADDRESS(&pi.net_src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
265   SET_ADDRESS(&pi.src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
266   SET_ADDRESS(&pi.net_dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
267   SET_ADDRESS(&pi.dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
268
269   if (tree) {
270     /* !!! specify length */
271     ti = proto_tree_add_item(tree, proto_ipv6, NullTVB, offset, 40, FALSE);
272     ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
273
274     /* !!! warning: version also contains 4 Bit priority */
275     proto_tree_add_uint(ipv6_tree, hf_ipv6_version, NullTVB,
276                 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
277                 (ipv6.ip6_vfc >> 4) & 0x0f);
278
279
280     proto_tree_add_uint(ipv6_tree, hf_ipv6_class, NullTVB,
281                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
282                 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
283
284     /*
285      * there should be no alignment problems for ip6_flow, since it's the first
286      * guint32 in the ipv6 struct
287      */
288     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_flow, NullTVB,
289                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
290                 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)),
291                 "Flowlabel: 0x%05lx",
292                 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)));
293
294     proto_tree_add_uint(ipv6_tree, hf_ipv6_plen, NullTVB,
295                 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
296                 ntohs(ipv6.ip6_plen));
297
298     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_nxt, NullTVB,
299                 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
300                 ipv6.ip6_nxt,
301                 "Next header: %s (0x%02x)",
302                 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
303
304     proto_tree_add_uint(ipv6_tree, hf_ipv6_hlim, NullTVB,
305                 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
306                 ipv6.ip6_hlim);
307
308     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_src, NullTVB,
309                 offset + offsetof(struct ip6_hdr, ip6_src), 16,
310                 (guint8 *)&ipv6.ip6_src,
311 #ifdef INET6
312                 "Source address: %s (%s)",
313                 get_hostname6(&ipv6.ip6_src),
314 #else
315                 "Source address: %s",
316 #endif
317                 ip6_to_str(&ipv6.ip6_src));
318
319     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_dst, NullTVB,
320                 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
321                 (guint8 *)&ipv6.ip6_dst,
322 #ifdef INET6
323                 "Destination address: %s (%s)",
324                 get_hostname6(&ipv6.ip6_dst),
325 #else
326                 "Destination address: %s",
327 #endif
328                 ip6_to_str(&ipv6.ip6_dst));
329   }
330
331   /* start of the new header (could be a extension header) */
332   nxt = pd[poffset = offset + offsetof(struct ip6_hdr, ip6_nxt)];
333   offset += sizeof(struct ip6_hdr);
334   frag = 0;
335
336 again:
337     switch (nxt) {
338     case IP_PROTO_HOPOPTS:
339         advance = dissect_hopopts(pd, offset, fd, tree);
340         nxt = pd[poffset = offset];
341         offset += advance;
342         goto again;
343     case IP_PROTO_ROUTING:
344         advance = dissect_routing6(pd, offset, fd, tree);
345         nxt = pd[poffset = offset];
346         offset += advance;
347         goto again;
348     case IP_PROTO_FRAGMENT:
349         advance = dissect_frag6(pd, offset, fd, tree, &frag);
350         nxt = pd[poffset = offset];
351         offset += advance;
352         goto again;
353     case IP_PROTO_AH:
354         advance = dissect_ah_old(pd, offset, fd, tree);
355         nxt = pd[poffset = offset];
356         offset += advance;
357         goto again;
358     case IP_PROTO_DSTOPTS:
359         advance = dissect_dstopts(pd, offset, fd, tree);
360         nxt = pd[poffset = offset];
361         offset += advance;
362         goto again;
363     }
364
365 #ifdef TEST_FINALHDR
366   proto_tree_add_uint_hidden(ipv6_tree, hf_ipv6_final, NullTVB, poffset, 1, nxt);
367 #endif
368   if (frag) {
369     /* fragmented */
370     if (check_col(fd, COL_PROTOCOL))
371       col_add_str(fd, COL_PROTOCOL, "IPv6");
372     /* COL_INFO was filled in by "dissect_frag6()" */
373     dissect_data(pd, offset, fd, tree);
374   } else {
375     /* do lookup with the subdissector table */
376     if (!dissector_try_port(ip_dissector_table, nxt, pd, offset, fd, tree)) {
377       /* Unknown protocol */
378       if (check_col(fd, COL_PROTOCOL))
379         col_add_str(fd, COL_PROTOCOL, "IPv6");
380       if (check_col(fd, COL_INFO))
381         col_add_fstr(fd, COL_INFO, "%s (0x%02x)", ipprotostr(nxt), nxt);
382       dissect_data(pd, offset, fd, tree);
383     }
384   }
385 }
386
387 static void
388 dissect_ipv6_none(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
389   if (check_col(fd, COL_INFO))
390     col_add_fstr(fd, COL_INFO, "IPv6 no next header");
391
392   /* XXX - dissect the payload as padding? */
393 }
394
395 void
396 proto_register_ipv6(void)
397 {
398   static hf_register_info hf[] = {
399     { &hf_ipv6_version,
400       { "Version",              "ipv6.version",
401                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
402     { &hf_ipv6_class,
403       { "Traffic class",        "ipv6.class",
404                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
405     { &hf_ipv6_flow,
406       { "Flowlabel",            "ipv6.flow",
407                                 FT_UINT32, BASE_HEX, NULL, 0x0, "" }},
408     { &hf_ipv6_plen,
409       { "Payload length",       "ipv6.plen",
410                                 FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
411     { &hf_ipv6_nxt,
412       { "Next header",          "ipv6.nxt",
413                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
414     { &hf_ipv6_hlim,
415       { "Hop limit",            "ipv6.hlim",
416                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
417     { &hf_ipv6_src,
418       { "Source",               "ipv6.src",
419                                 FT_IPv6, BASE_NONE, NULL, 0x0,
420                                 "Source IPv6 Address" }},
421     { &hf_ipv6_dst,
422       { "Destination",          "ipv6.dst",
423                                 FT_IPv6, BASE_NONE, NULL, 0x0,
424                                 "Destination IPv6 Address" }},
425 #ifdef TEST_FINALHDR
426     { &hf_ipv6_final,
427       { "Final next header",    "ipv6.final",
428                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
429 #endif
430   };
431   static gint *ett[] = {
432     &ett_ipv6,
433   };
434
435   proto_ipv6 = proto_register_protocol("Internet Protocol Version 6", "ipv6");
436   proto_register_field_array(proto_ipv6, hf, array_length(hf));
437   proto_register_subtree_array(ett, array_length(ett));
438 }
439
440 void
441 proto_reg_handoff_ipv6(void)
442 {
443         dissector_add("ethertype", ETHERTYPE_IPv6, dissect_ipv6);
444         dissector_add("ppp.protocol", PPP_IPV6, dissect_ipv6);
445         dissector_add("ip.proto", IP_PROTO_IPV6, dissect_ipv6);
446         dissector_add("ip.proto", IP_PROTO_NONE, dissect_ipv6_none);
447 }