Move the notes on nettl support above the notes on libpcap; the notes on
[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.27 2000/02/15 21:02:21 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 "packet.h"
46 #include "packet-icmpv6.h"
47 #include "packet-ip.h"
48 #include "packet-ipsec.h"
49 #include "packet-ipv6.h"
50 #include "packet-pim.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, offset, len,
94             "Routing Header, Type %d", rt.ip6r_type);
95         rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
96
97         proto_tree_add_text(rthdr_tree,
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,
101             offset + offsetof(struct ip6_rthdr, ip6r_len), 1,
102             "Length: %d (%d bytes)", rt.ip6r_len, len);
103         proto_tree_add_text(rthdr_tree,
104             offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
105             "Type: %d", rt.ip6r_type, len);
106         proto_tree_add_text(rthdr_tree,
107             offset + offsetof(struct ip6_rthdr, ip6r_segleft), 1,
108             "Segments left: %d", rt.ip6r_segleft, len);
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,
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     struct ip6_frag frag;
142     int len;
143
144     memcpy(&frag, (void *) &pd[offset], sizeof(frag));
145     len = sizeof(frag);
146
147     if (check_col(fd, COL_INFO)) {
148         col_add_fstr(fd, COL_INFO,
149             "IPv6 fragment (nxt=%s (0x%02x) off=0x%04x id=0x%x)",
150             ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
151             (frag.ip6f_offlg >> 3) & 0x1fff, frag.ip6f_ident);
152     }
153     return len;
154 }
155
156 static int
157 dissect_opts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
158     char *optname) {
159     struct ip6_ext ext;
160     int len;
161     proto_tree *dstopt_tree;
162         proto_item *ti;
163     u_char *p;
164     static const value_string rtalertvals[] = {
165         { IP6OPT_RTALERT_MLD, "MLD" },
166         { IP6OPT_RTALERT_RSVP, "RSVP" },
167         { 0, NULL },
168     };
169
170     memcpy(&ext, (void *) &pd[offset], sizeof(ext)); 
171     len = (ext.ip6e_len + 1) << 3;
172
173     if (tree) {
174         /* !!! specify length */
175         ti = proto_tree_add_text(tree, offset, len,
176             "%s Header", optname);
177         dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
178
179         proto_tree_add_text(dstopt_tree,
180             offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
181             "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
182         proto_tree_add_text(dstopt_tree,
183             offset + offsetof(struct ip6_ext, ip6e_len), 1,
184             "Length: %d (%d bytes)", ext.ip6e_len, len);
185
186         p = (u_char *)(pd + offset + 2);
187         while (p < pd + offset + len) {
188             switch (p[0]) {
189             case IP6OPT_PAD1:
190                 proto_tree_add_text(dstopt_tree, p - pd, 1,
191                     "Pad1");
192                 p++;
193                 break;
194             case IP6OPT_PADN:
195                 proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
196                     "PadN: %d bytes", p[1] + 2);
197                 p += p[1];
198                 p += 2;
199                 break;
200             case IP6OPT_JUMBO:
201                 if (p[1] == 4) {
202                     proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
203                         "Jumbo payload: %u (%d bytes)",
204                         pntohl(&p[2]), p[1] + 2);
205                 } else {
206                     proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
207                         "Jumbo payload: Invalid length (%d bytes)",
208                         p[1] + 2);
209                 }
210                 p += p[1];
211                 p += 2;
212                 break;
213             case IP6OPT_RTALERT:
214               {
215                 char *rta;
216
217                 if (p[1] == 2) {
218                     rta = val_to_str(pntohs(&p[2]), rtalertvals,
219                                 "Unknown");
220                 } else
221                     rta = "Invalid length";
222                 ti = proto_tree_add_text(dstopt_tree, p - pd, p[1] + 2,
223                     "Router alert: %s (%d bytes)", rta, p[1] + 2);
224                 p += p[1];
225                 p += 2;
226                 break;
227               }
228             default:
229                 p = (u_char *)(pd + offset + len);
230                 break;
231             }
232         }
233
234         /* decode... */
235     }
236
237     return len;
238 }
239
240 static int
241 dissect_hopopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
242     return dissect_opts(pd, offset, fd, tree, "Hop-by-hop Option");
243 }
244
245 static int
246 dissect_dstopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
247     return dissect_opts(pd, offset, fd, tree, "Destination Option");
248 }
249
250 void
251 dissect_ipv6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
252   proto_tree *ipv6_tree;
253   proto_item *ti;
254   guint8 nxt;
255   int advance;
256   int poffset;
257
258   struct ip6_hdr ipv6;
259
260   memcpy(&ipv6, (void *) &pd[offset], sizeof(ipv6)); 
261
262   SET_ADDRESS(&pi.net_src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
263   SET_ADDRESS(&pi.src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
264   SET_ADDRESS(&pi.net_dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
265   SET_ADDRESS(&pi.dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
266
267   if (check_col(fd, COL_PROTOCOL))
268     col_add_str(fd, COL_PROTOCOL, "IPv6");
269
270   if (tree) {
271     /* !!! specify length */
272     ti = proto_tree_add_item(tree, proto_ipv6, offset, 40, NULL);
273     ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
274
275     /* !!! warning: version also contains 4 Bit priority */
276     proto_tree_add_item(ipv6_tree, hf_ipv6_version,
277                 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
278                 (ipv6.ip6_vfc >> 4) & 0x0f);
279
280
281     proto_tree_add_item(ipv6_tree, hf_ipv6_class,
282                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
283                 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
284
285     /*
286      * there should be no alignment problems for ip6_flow, since it's the first
287      * guint32 in the ipv6 struct
288      */
289     proto_tree_add_item_format(ipv6_tree, hf_ipv6_flow,
290                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
291                 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)),
292                 "Flowlabel: 0x%05lx",
293                 (unsigned long)(ntohl(ipv6.ip6_flow & IPV6_FLOWLABEL_MASK)));
294
295     proto_tree_add_item(ipv6_tree, hf_ipv6_plen,
296                 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
297                 ntohs(ipv6.ip6_plen));
298
299     proto_tree_add_item_format(ipv6_tree, hf_ipv6_nxt,
300                 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
301                 ipv6.ip6_nxt,
302                 "Next header: %s (0x%02x)",
303                 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
304
305     proto_tree_add_item(ipv6_tree, hf_ipv6_hlim,
306                 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
307                 ipv6.ip6_hlim);
308
309     proto_tree_add_item_format(ipv6_tree, hf_ipv6_src,
310                 offset + offsetof(struct ip6_hdr, ip6_src), 16,
311                 &ipv6.ip6_src,
312 #ifdef INET6
313                 "Source address: %s (%s)",
314                 get_hostname6(&ipv6.ip6_src),
315 #else
316                 "Source address: %s",
317 #endif
318                 ip6_to_str(&ipv6.ip6_src));
319
320     proto_tree_add_item_format(ipv6_tree, hf_ipv6_dst,
321                 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
322                 &ipv6.ip6_dst,
323 #ifdef INET6
324                 "Destination address: %s (%s)",
325                 get_hostname6(&ipv6.ip6_dst),
326 #else
327                 "Destination address: %s",
328 #endif
329                 ip6_to_str(&ipv6.ip6_dst));
330   }
331
332   /* start of the new header (could be a extension header) */
333   nxt = pd[poffset = offset + offsetof(struct ip6_hdr, ip6_nxt)];
334   offset += sizeof(struct ip6_hdr);
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_IPIP:
344         dissect_ip(pd, offset, fd, tree);
345         break;
346     case IP_PROTO_ROUTING:
347         advance = dissect_routing6(pd, offset, fd, tree);
348         nxt = pd[poffset = offset];
349         offset += advance;
350         goto again;
351     case IP_PROTO_FRAGMENT:
352         advance = dissect_frag6(pd, offset, fd, tree);
353         nxt = pd[poffset = offset];
354         offset += advance;
355         goto again;
356     case IP_PROTO_ICMPV6:
357 #ifdef TEST_FINALHDR
358         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
359 #endif
360         dissect_icmpv6(pd, offset, fd, tree);
361         break;
362     case IP_PROTO_NONE:
363 #ifdef TEST_FINALHDR
364         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
365 #endif
366         if (check_col(fd, COL_INFO)) {
367             col_add_fstr(fd, COL_INFO, "IPv6 no next header");
368         }
369         break;
370     case IP_PROTO_AH:
371         advance = dissect_ah(pd, offset, fd, tree);
372         nxt = pd[poffset = offset];
373         offset += advance;
374         goto again;
375     case IP_PROTO_ESP:
376         dissect_esp(pd, offset, fd, tree);
377         break;
378     case IP_PROTO_DSTOPTS:
379         advance = dissect_dstopts(pd, offset, fd, tree);
380         nxt = pd[poffset = offset];
381         offset += advance;
382         goto again;
383     case IP_PROTO_TCP:
384 #ifdef TEST_FINALHDR
385         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
386 #endif
387         dissect_tcp(pd, offset, fd, tree);
388         break;
389     case IP_PROTO_UDP:
390 #ifdef TEST_FINALHDR
391         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
392 #endif
393         dissect_udp(pd, offset, fd, tree);
394         break;
395     case IP_PROTO_PIM:
396 #ifdef TEST_FINALHDR
397         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
398 #endif
399         dissect_pim(pd, offset, fd, tree);
400         break;
401     case IP_PROTO_IPCOMP:
402         dissect_ipcomp(pd, offset, fd, tree);
403         break;
404     default:
405 #ifdef TEST_FINALHDR
406         proto_tree_add_item_hidden(ipv6_tree, hf_ipv6_final, poffset, 1, nxt);
407 #endif
408         if (check_col(fd, COL_INFO)) {
409             col_add_fstr(fd, COL_INFO, "%s (0x%02x)",
410                 ipprotostr(nxt), nxt);
411         }
412         dissect_data(pd, offset, fd, tree);
413         break;
414     }
415 }
416
417 void
418 proto_register_ipv6(void)
419 {
420   static hf_register_info hf[] = {
421     { &hf_ipv6_version,
422       { "Version",              "ipv6.version",
423                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
424     { &hf_ipv6_class,
425       { "Traffic class",        "ipv6.class",
426                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
427     { &hf_ipv6_flow,
428       { "Flowlabel",            "ipv6.flow",
429                                 FT_UINT32, BASE_HEX, NULL, 0x0, "" }},
430     { &hf_ipv6_plen,
431       { "Payload length",       "ipv6.plen",
432                                 FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
433     { &hf_ipv6_nxt,
434       { "Next header",          "ipv6.nxt",
435                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
436     { &hf_ipv6_hlim,
437       { "Hop limit",            "ipv6.hlim",
438                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
439     { &hf_ipv6_src,
440       { "Source",               "ipv6.src",
441                                 FT_IPv6, BASE_NONE, NULL, 0x0,
442                                 "Source IPv6 Address" }},
443     { &hf_ipv6_dst,
444       { "Destination",          "ipv6.dst",
445                                 FT_IPv6, BASE_NONE, NULL, 0x0,
446                                 "Destination IPv6 Address" }},
447 #ifdef TEST_FINALHDR
448     { &hf_ipv6_final,
449       { "Final next header",    "ipv6.final",
450                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
451 #endif
452   };
453   static gint *ett[] = {
454     &ett_ipv6,
455   };
456
457   proto_ipv6 = proto_register_protocol("Internet Protocol Version 6", "ipv6");
458   proto_register_field_array(proto_ipv6, hf, array_length(hf));
459   proto_register_subtree_array(ett, array_length(ett));
460 }