Register an "ip.proto" dissector table for IPv4, and have dissectors for
[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.34 2000/04/16 22:46:20 guy 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-icmpv6.h"
49 #include "packet-ip.h"
50 #include "packet-ipsec.h"
51 #include "packet-ipv6.h"
52 #include "packet-pim.h"
53 #include "packet-tcp.h"
54 #include "packet-udp.h"
55 #include "resolv.h"
56
57 /*
58  * NOTE: ipv6.nxt is not very useful as we will have chained header.
59  * now testing ipv6.final, but it raises SEGV.
60 #define TEST_FINALHDR
61  */
62
63 static int proto_ipv6 = -1;
64 static int hf_ipv6_version = -1;
65 static int hf_ipv6_class = -1;
66 static int hf_ipv6_flow = -1;
67 static int hf_ipv6_plen = -1;
68 static int hf_ipv6_nxt = -1;
69 static int hf_ipv6_hlim = -1;
70 static int hf_ipv6_src = -1;
71 static int hf_ipv6_dst = -1;
72 #ifdef TEST_FINALHDR
73 static int hf_ipv6_final = -1;
74 #endif
75
76 static gint ett_ipv6 = -1;
77
78 #ifndef offsetof
79 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
80 #endif
81
82 static int
83 dissect_routing6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
84     struct ip6_rthdr rt;
85     int len;
86     proto_tree *rthdr_tree;
87         proto_item *ti;
88     char buf[sizeof(struct ip6_rthdr0) + sizeof(struct e_in6_addr) * 23];
89
90     memcpy(&rt, (void *) &pd[offset], sizeof(rt));
91     len = (rt.ip6r_len + 1) << 3;
92
93     if (tree) {
94         /* !!! specify length */
95         ti = proto_tree_add_text(tree, offset, len,
96             "Routing Header, Type %u", rt.ip6r_type);
97         rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
98
99         proto_tree_add_text(rthdr_tree,
100             offset + offsetof(struct ip6_rthdr, ip6r_nxt), 1,
101             "Next header: %s (0x%02x)", ipprotostr(rt.ip6r_nxt), rt.ip6r_nxt);
102         proto_tree_add_text(rthdr_tree,
103             offset + offsetof(struct ip6_rthdr, ip6r_len), 1,
104             "Length: %u (%d bytes)", rt.ip6r_len, len);
105         proto_tree_add_text(rthdr_tree,
106             offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
107             "Type: %u", rt.ip6r_type);
108         proto_tree_add_text(rthdr_tree,
109             offset + offsetof(struct ip6_rthdr, ip6r_segleft), 1,
110             "Segments left: %u", rt.ip6r_segleft);
111
112         if (rt.ip6r_type == 0 && len <= sizeof(buf)) {
113             struct e_in6_addr *a;
114             int n;
115             struct ip6_rthdr0 *rt0;
116
117             memcpy(buf, (void *) &pd[offset], len);
118             rt0 = (struct ip6_rthdr0 *)buf;
119             for (a = rt0->ip6r0_addr, n = 0;
120                  a < (struct e_in6_addr *)(buf + len);
121                  a++, n++) {
122                 proto_tree_add_text(rthdr_tree,
123                     offset + offsetof(struct ip6_rthdr0, ip6r0_addr) + n * sizeof(struct e_in6_addr),
124                     sizeof(struct e_in6_addr),
125 #ifdef INET6
126                     "address %d: %s (%s)",
127                     n, get_hostname6(a), ip6_to_str(a)
128 #else
129                     "address %d: %s", n, ip6_to_str(a)
130 #endif
131                     );
132             }
133         }
134   
135         /* decode... */
136     }
137
138     return len;
139 }
140
141 static int
142 dissect_frag6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, 
143     int *fragstart) {
144     struct ip6_frag frag;
145     int len;
146
147     memcpy(&frag, (void *) &pd[offset], sizeof(frag));
148     len = sizeof(frag);
149     *fragstart = ntohs(frag.ip6f_offlg) & 0xfff8;
150     if (check_col(fd, COL_INFO)) {
151         col_add_fstr(fd, COL_INFO,
152             "IPv6 fragment (nxt=%s (0x%02x) off=0x%04x id=0x%x)",
153             ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
154             *fragstart, frag.ip6f_ident);
155     }
156     return len;
157 }
158
159 static int
160 dissect_opts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
161     char *optname) {
162     struct ip6_ext ext;
163     int len;
164     proto_tree *dstopt_tree;
165         proto_item *ti;
166     u_char *p;
167     static const value_string rtalertvals[] = {
168         { IP6OPT_RTALERT_MLD, "MLD" },
169         { IP6OPT_RTALERT_RSVP, "RSVP" },
170         { 0, NULL },
171     };
172
173     memcpy(&ext, (void *) &pd[offset], sizeof(ext)); 
174     len = (ext.ip6e_len + 1) << 3;
175
176     if (tree) {
177         /* !!! specify length */
178         ti = proto_tree_add_text(tree, offset, len,
179             "%s Header", optname);
180         dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
181
182         proto_tree_add_text(dstopt_tree,
183             offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
184             "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
185         proto_tree_add_text(dstopt_tree,
186             offset + offsetof(struct ip6_ext, ip6e_len), 1,
187             "Length: %u (%d bytes)", ext.ip6e_len, len);
188
189         p = (u_char *)(pd + offset + 2);
190         while (p < pd + offset + len) {
191             switch (p[0]) {
192             case IP6OPT_PAD1:
193                 proto_tree_add_text(dstopt_tree, p - pd, 1,
194                     "Pad1");
195                 p++;
196                 break;
197             case IP6OPT_PADN:
198                 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
199                     "PadN: %u bytes", p[1] + 2);
200                 p += p[1];
201                 p += 2;
202                 break;
203             case IP6OPT_JUMBO:
204                 if (p[1] == 4) {
205                     proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
206                         "Jumbo payload: %u (%u bytes)",
207                         pntohl(&p[2]), p[1] + 2);
208                 } else {
209                     proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
210                         "Jumbo payload: Invalid length (%u bytes)",
211                         p[1] + 2);
212                 }
213                 p += p[1];
214                 p += 2;
215                 break;
216             case IP6OPT_RTALERT:
217               {
218                 char *rta;
219
220                 if (p[1] == 2) {
221                     rta = val_to_str(pntohs(&p[2]), rtalertvals,
222                                 "Unknown");
223                 } else
224                     rta = "Invalid length";
225                 ti = proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
226                     "Router alert: %s (%u bytes)", rta, p[1] + 2);
227                 p += p[1];
228                 p += 2;
229                 break;
230               }
231             default:
232                 p = (u_char *)(pd + offset + len);
233                 break;
234             }
235         }
236
237         /* decode... */
238     }
239
240     return len;
241 }
242
243 static int
244 dissect_hopopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
245     return dissect_opts(pd, offset, fd, tree, "Hop-by-hop Option");
246 }
247
248 static int
249 dissect_dstopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
250     return dissect_opts(pd, offset, fd, tree, "Destination Option");
251 }
252
253 void
254 dissect_ipv6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
255   proto_tree *ipv6_tree;
256   proto_item *ti;
257   guint8 nxt;
258   int advance;
259   int poffset;
260   int frag;
261
262   struct ip6_hdr ipv6;
263
264   memcpy(&ipv6, (void *) &pd[offset], sizeof(ipv6)); 
265
266   SET_ADDRESS(&pi.net_src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
267   SET_ADDRESS(&pi.src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
268   SET_ADDRESS(&pi.net_dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
269   SET_ADDRESS(&pi.dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
270
271   if (check_col(fd, COL_PROTOCOL))
272     col_add_str(fd, COL_PROTOCOL, "IPv6");
273
274   if (tree) {
275     /* !!! specify length */
276     ti = proto_tree_add_item(tree, proto_ipv6, offset, 40, NULL);
277     ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
278
279     /* !!! warning: version also contains 4 Bit priority */
280     proto_tree_add_item(ipv6_tree, hf_ipv6_version,
281                 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
282                 (ipv6.ip6_vfc >> 4) & 0x0f);
283
284
285     proto_tree_add_item(ipv6_tree, hf_ipv6_class,
286                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
287                 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
288
289     /*
290      * there should be no alignment problems for ip6_flow, since it's the first
291      * guint32 in the ipv6 struct
292      */
293     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_flow,
294                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
295                 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)),
296                 "Flowlabel: 0x%05lx",
297                 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)));
298
299     proto_tree_add_item(ipv6_tree, hf_ipv6_plen,
300                 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
301                 ntohs(ipv6.ip6_plen));
302
303     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_nxt,
304                 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
305                 ipv6.ip6_nxt,
306                 "Next header: %s (0x%02x)",
307                 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
308
309     proto_tree_add_item(ipv6_tree, hf_ipv6_hlim,
310                 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
311                 ipv6.ip6_hlim);
312
313     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_src,
314                 offset + offsetof(struct ip6_hdr, ip6_src), 16,
315                 (guint8 *)&ipv6.ip6_src,
316 #ifdef INET6
317                 "Source address: %s (%s)",
318                 get_hostname6(&ipv6.ip6_src),
319 #else
320                 "Source address: %s",
321 #endif
322                 ip6_to_str(&ipv6.ip6_src));
323
324     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_dst,
325                 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
326                 (guint8 *)&ipv6.ip6_dst,
327 #ifdef INET6
328                 "Destination address: %s (%s)",
329                 get_hostname6(&ipv6.ip6_dst),
330 #else
331                 "Destination address: %s",
332 #endif
333                 ip6_to_str(&ipv6.ip6_dst));
334   }
335
336   /* start of the new header (could be a extension header) */
337   nxt = pd[poffset = offset + offsetof(struct ip6_hdr, ip6_nxt)];
338   offset += sizeof(struct ip6_hdr);
339   frag = 0;
340
341 again:
342     switch (nxt) {
343     case IP_PROTO_HOPOPTS:
344         advance = dissect_hopopts(pd, offset, fd, tree);
345         nxt = pd[poffset = offset];
346         offset += advance;
347         goto again;
348     case IP_PROTO_IPIP:
349         dissect_ip(pd, offset, fd, tree);
350         break;
351     case IP_PROTO_ROUTING:
352         advance = dissect_routing6(pd, offset, fd, tree);
353         nxt = pd[poffset = offset];
354         offset += advance;
355         goto again;
356     case IP_PROTO_FRAGMENT:
357         advance = dissect_frag6(pd, offset, fd, tree, &frag);
358         nxt = pd[poffset = offset];
359         offset += advance;
360         goto again;
361     case IP_PROTO_ICMPV6:
362 #ifdef TEST_FINALHDR
363         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
364 #endif
365         if (!frag)
366                 dissect_icmpv6(pd, offset, fd, tree);
367         else
368                 dissect_data(pd, offset, fd, tree);
369         break;
370     case IP_PROTO_NONE:
371 #ifdef TEST_FINALHDR
372         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
373 #endif
374         if (check_col(fd, COL_INFO)) {
375             col_add_fstr(fd, COL_INFO, "IPv6 no next header");
376         }
377         break;
378     case IP_PROTO_AH:
379         advance = dissect_ah(pd, offset, fd, tree);
380         nxt = pd[poffset = offset];
381         offset += advance;
382         goto again;
383     case IP_PROTO_ESP:
384         dissect_esp(pd, offset, fd, tree);
385         break;
386     case IP_PROTO_DSTOPTS:
387         advance = dissect_dstopts(pd, offset, fd, tree);
388         nxt = pd[poffset = offset];
389         offset += advance;
390         goto again;
391     case IP_PROTO_TCP:
392 #ifdef TEST_FINALHDR
393         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
394 #endif
395         if (!frag)
396                 dissect_tcp(pd, offset, fd, tree);
397         else
398                 dissect_data(pd, offset, fd, tree);
399         break;
400     case IP_PROTO_UDP:
401 #ifdef TEST_FINALHDR
402         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
403 #endif
404         if (!frag)
405                 dissect_udp(pd, offset, fd, tree);
406         else
407                 dissect_data(pd, offset, fd, tree);
408         break;
409     case IP_PROTO_PIM:
410 #ifdef TEST_FINALHDR
411         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
412 #endif
413         dissect_pim(pd, offset, fd, tree);
414         break;
415     case IP_PROTO_IPCOMP:
416         dissect_ipcomp(pd, offset, fd, tree);
417         break;
418     default:
419 #ifdef TEST_FINALHDR
420         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
421 #endif
422         if (check_col(fd, COL_INFO)) {
423             col_add_fstr(fd, COL_INFO, "%s (0x%02x)",
424                 ipprotostr(nxt), nxt);
425         }
426         dissect_data(pd, offset, fd, tree);
427         break;
428     }
429 }
430
431 void
432 proto_register_ipv6(void)
433 {
434   static hf_register_info hf[] = {
435     { &hf_ipv6_version,
436       { "Version",              "ipv6.version",
437                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
438     { &hf_ipv6_class,
439       { "Traffic class",        "ipv6.class",
440                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
441     { &hf_ipv6_flow,
442       { "Flowlabel",            "ipv6.flow",
443                                 FT_UINT32, BASE_HEX, NULL, 0x0, "" }},
444     { &hf_ipv6_plen,
445       { "Payload length",       "ipv6.plen",
446                                 FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
447     { &hf_ipv6_nxt,
448       { "Next header",          "ipv6.nxt",
449                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
450     { &hf_ipv6_hlim,
451       { "Hop limit",            "ipv6.hlim",
452                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
453     { &hf_ipv6_src,
454       { "Source",               "ipv6.src",
455                                 FT_IPv6, BASE_NONE, NULL, 0x0,
456                                 "Source IPv6 Address" }},
457     { &hf_ipv6_dst,
458       { "Destination",          "ipv6.dst",
459                                 FT_IPv6, BASE_NONE, NULL, 0x0,
460                                 "Destination IPv6 Address" }},
461 #ifdef TEST_FINALHDR
462     { &hf_ipv6_final,
463       { "Final next header",    "ipv6.final",
464                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
465 #endif
466   };
467   static gint *ett[] = {
468     &ett_ipv6,
469   };
470
471   proto_ipv6 = proto_register_protocol("Internet Protocol Version 6", "ipv6");
472   proto_register_field_array(proto_ipv6, hf, array_length(hf));
473   proto_register_subtree_array(ett, array_length(ett));
474 }
475
476 void
477 proto_reg_handoff_ipv6(void)
478 {
479         dissector_add("ethertype", ETHERTYPE_IPv6, dissect_ipv6);
480         dissector_add("ppp.protocol", PPP_IPV6, dissect_ipv6);
481         dissector_add("ip.proto", IP_PROTO_IPV6, dissect_ipv6);
482 }