Make the Zebra dissector, and a routine it uses, static, as they're not
[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.49 2001/01/03 06:55:29 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 "aftypes.h"
48 #include "packet.h"
49 #include "packet-ip.h"
50 #include "packet-ipsec.h"
51 #include "packet-ipv6.h"
52 #include "packet-tcp.h"
53 #include "packet-udp.h"
54 #include "resolv.h"
55
56 /*
57  * NOTE: ipv6.nxt is not very useful as we will have chained header.
58  * now testing ipv6.final, but it raises SEGV.
59 #define TEST_FINALHDR
60  */
61
62 static int proto_ipv6 = -1;
63 static int hf_ipv6_version = -1;
64 static int hf_ipv6_class = -1;
65 static int hf_ipv6_flow = -1;
66 static int hf_ipv6_plen = -1;
67 static int hf_ipv6_nxt = -1;
68 static int hf_ipv6_hlim = -1;
69 static int hf_ipv6_src = -1;
70 static int hf_ipv6_dst = -1;
71 static int hf_ipv6_addr = -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, NullTVB, 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, NullTVB,
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, NullTVB,
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, NullTVB,
106             offset + offsetof(struct ip6_rthdr, ip6r_type), 1,
107             "Type: %u", rt.ip6r_type);
108         proto_tree_add_text(rthdr_tree, NullTVB,
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, NullTVB,
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     proto_item *ti;
147     proto_tree *rthdr_tree;
148
149     memcpy(&frag, (void *) &pd[offset], sizeof(frag));
150     len = sizeof(frag);
151     frag.ip6f_offlg = ntohs(frag.ip6f_offlg);
152     *fragstart = frag.ip6f_offlg & IP6F_OFF_MASK;
153     if (check_col(fd, COL_INFO)) {
154         col_add_fstr(fd, COL_INFO,
155             "IPv6 fragment (nxt=%s (0x%02x) off=%u id=0x%x)",
156             ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt,
157             *fragstart, frag.ip6f_ident);
158     }
159     if (tree) {
160            ti = proto_tree_add_text(tree, NullTVB, offset, len,
161                            "Fragmention Header");
162            rthdr_tree = proto_item_add_subtree(ti, ett_ipv6);
163
164            proto_tree_add_text(rthdr_tree, NullTVB,
165                          offset + offsetof(struct ip6_frag, ip6f_nxt), 1,
166                          "Next header: %s (0x%02x)",
167                          ipprotostr(frag.ip6f_nxt), frag.ip6f_nxt);
168
169         #if 0
170            proto_tree_add_text(rthdr_tree, NullTVB,
171                          offset + offsetof(struct ip6_frag, ip6f_reserved), 1,
172                          "Reserved: %u",
173                          frag.ip6f_reserved);
174         #endif
175
176            proto_tree_add_text(rthdr_tree, NullTVB,
177                          offset + offsetof(struct ip6_frag, ip6f_offlg), 2,
178                          "Offset: %u",
179                          frag.ip6f_offlg & IP6F_OFF_MASK);
180
181            proto_tree_add_text(rthdr_tree, NullTVB,
182                          offset + offsetof(struct ip6_frag, ip6f_offlg), 2,
183                          "More fragments: %s",
184                                 frag.ip6f_offlg & IP6F_MORE_FRAG ?
185                                 "Yes" : "No");
186
187            proto_tree_add_text(rthdr_tree, NullTVB,
188                          offset + offsetof(struct ip6_frag, ip6f_ident), 4,
189                          "Identification: 0x%08x",
190                          frag.ip6f_ident);
191     }
192     return len;
193 }
194
195 static int
196 dissect_opts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
197     char *optname) {
198     struct ip6_ext ext;
199     int len;
200     proto_tree *dstopt_tree;
201         proto_item *ti;
202     u_char *p;
203     static const value_string rtalertvals[] = {
204         { IP6OPT_RTALERT_MLD, "MLD" },
205         { IP6OPT_RTALERT_RSVP, "RSVP" },
206         { 0, NULL },
207     };
208
209     memcpy(&ext, (void *) &pd[offset], sizeof(ext)); 
210     len = (ext.ip6e_len + 1) << 3;
211
212     if (tree) {
213         /* !!! specify length */
214         ti = proto_tree_add_text(tree, NullTVB, offset, len,
215             "%s Header", optname);
216         dstopt_tree = proto_item_add_subtree(ti, ett_ipv6);
217
218         proto_tree_add_text(dstopt_tree, NullTVB,
219             offset + offsetof(struct ip6_ext, ip6e_nxt), 1,
220             "Next header: %s (0x%02x)", ipprotostr(ext.ip6e_nxt), ext.ip6e_nxt);
221         proto_tree_add_text(dstopt_tree, NullTVB,
222             offset + offsetof(struct ip6_ext, ip6e_len), 1,
223             "Length: %u (%d bytes)", ext.ip6e_len, len);
224
225         p = (u_char *)(pd + offset + 2);
226         while (p < pd + offset + len) {
227             switch (p[0]) {
228             case IP6OPT_PAD1:
229                 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, 1,
230                     "Pad1");
231                 p++;
232                 break;
233             case IP6OPT_PADN:
234                 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
235                     "PadN: %u bytes", p[1] + 2);
236                 p += p[1];
237                 p += 2;
238                 break;
239             case IP6OPT_JUMBO:
240                 if (p[1] == 4) {
241                     proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
242                         "Jumbo payload: %u (%u bytes)",
243                         pntohl(&p[2]), p[1] + 2);
244                 } else {
245                     proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
246                         "Jumbo payload: Invalid length (%u bytes)",
247                         p[1] + 2);
248                 }
249                 p += p[1];
250                 p += 2;
251                 break;
252             case IP6OPT_RTALERT:
253               {
254                 char *rta;
255
256                 if (p[1] == 2) {
257                     rta = val_to_str(pntohs(&p[2]), rtalertvals,
258                                 "Unknown");
259                 } else
260                     rta = "Invalid length";
261                 ti = proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
262                     "Router alert: %s (%u bytes)", rta, p[1] + 2);
263                 p += p[1];
264                 p += 2;
265                 break;
266               }
267             case IP6OPT_BINDING_UPDATE:
268                 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
269                     "Mobile IPv6 Binding Update");
270                 p += p[1];
271                 p += 2;
272                 break;
273             case IP6OPT_BINDING_ACK:
274                 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
275                     "Mobile IPv6 Binding Acknowledgement");
276                 p += p[1];
277                 p += 2;
278                 break;
279             case IP6OPT_BINDING_REQ:
280                 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
281                     "Mobile IPv6 Binding Request");
282                 p += p[1];
283                 p += 2;
284                 break;
285             case IP6OPT_HOME_ADDRESS:
286                 proto_tree_add_text(dstopt_tree, NullTVB, p - pd, p[1] + 2,
287                     "Mobile IPv6 Home Address");
288                 p += p[1];
289                 p += 2;
290                 break;
291             case IP6OPT_EID:
292                 p += p[1];
293                 p += 2;
294                 break;
295             default:
296                 p = (u_char *)(pd + offset + len);
297                 break;
298             }
299         }
300
301         /* decode... */
302     }
303
304     return len;
305 }
306
307 static int
308 dissect_hopopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
309     return dissect_opts(pd, offset, fd, tree, "Hop-by-hop Option");
310 }
311
312 static int
313 dissect_dstopts(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
314     return dissect_opts(pd, offset, fd, tree, "Destination Option");
315 }
316
317 void
318 dissect_ipv6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
319   proto_tree *ipv6_tree;
320   proto_item *ti;
321   guint8 nxt;
322   int advance;
323   int poffset;
324   int frag;
325
326   struct ip6_hdr ipv6;
327
328   OLD_CHECK_DISPLAY_AS_DATA(proto_ipv6, pd, offset, fd, tree);
329
330   memcpy(&ipv6, (void *) &pd[offset], sizeof(ipv6)); 
331
332   pi.ipproto = ipv6.ip6_nxt; /* XXX make work TCP follow (ipproto = 6) */
333
334   SET_ADDRESS(&pi.net_src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
335   SET_ADDRESS(&pi.src, AT_IPv6, 16, &pd[offset + IP6H_SRC]);
336   SET_ADDRESS(&pi.net_dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
337   SET_ADDRESS(&pi.dst, AT_IPv6, 16, &pd[offset + IP6H_DST]);
338
339   if (tree) {
340     /* !!! specify length */
341     ti = proto_tree_add_item(tree, proto_ipv6, NullTVB, offset, 40, FALSE);
342     ipv6_tree = proto_item_add_subtree(ti, ett_ipv6);
343
344     /* !!! warning: version also contains 4 Bit priority */
345     proto_tree_add_uint(ipv6_tree, hf_ipv6_version, NullTVB,
346                 offset + offsetof(struct ip6_hdr, ip6_vfc), 1,
347                 (ipv6.ip6_vfc >> 4) & 0x0f);
348
349
350     proto_tree_add_uint(ipv6_tree, hf_ipv6_class, NullTVB,
351                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
352                 (guint8)((ntohl(ipv6.ip6_flow) >> 20) & 0xff));
353
354     /*
355      * there should be no alignment problems for ip6_flow, since it's the first
356      * guint32 in the ipv6 struct
357      */
358     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_flow, NullTVB,
359                 offset + offsetof(struct ip6_hdr, ip6_flow), 4,
360                 (unsigned long)(ntohl(ipv6.ip6_flow) & IPV6_FLOWLABEL_MASK),
361                 "Flowlabel: 0x%05lx",
362                 (unsigned long)(ntohl(ipv6.ip6_flow) & IPV6_FLOWLABEL_MASK));
363
364     proto_tree_add_uint(ipv6_tree, hf_ipv6_plen, NullTVB,
365                 offset + offsetof(struct ip6_hdr, ip6_plen), 2,
366                 ntohs(ipv6.ip6_plen));
367
368     proto_tree_add_uint_format(ipv6_tree, hf_ipv6_nxt, NullTVB,
369                 offset + offsetof(struct ip6_hdr, ip6_nxt), 1,
370                 ipv6.ip6_nxt,
371                 "Next header: %s (0x%02x)",
372                 ipprotostr(ipv6.ip6_nxt), ipv6.ip6_nxt);
373
374     proto_tree_add_uint(ipv6_tree, hf_ipv6_hlim, NullTVB,
375                 offset + offsetof(struct ip6_hdr, ip6_hlim), 1,
376                 ipv6.ip6_hlim);
377
378     proto_tree_add_ipv6_hidden(ipv6_tree, hf_ipv6_addr, NullTVB, 
379                                offset + offsetof(struct ip6_hdr, ip6_src), 16,
380                                ipv6.ip6_src.s6_addr8);
381     proto_tree_add_ipv6_hidden(ipv6_tree, hf_ipv6_addr, NullTVB, 
382                                offset + offsetof(struct ip6_hdr, ip6_dst), 16,
383                                ipv6.ip6_dst.s6_addr8);
384
385     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_src, NullTVB,
386                 offset + offsetof(struct ip6_hdr, ip6_src), 16,
387                 (guint8 *)&ipv6.ip6_src,
388 #ifdef INET6
389                 "Source address: %s (%s)",
390                 get_hostname6(&ipv6.ip6_src),
391 #else
392                 "Source address: %s",
393 #endif
394                 ip6_to_str(&ipv6.ip6_src));
395
396     proto_tree_add_ipv6_format(ipv6_tree, hf_ipv6_dst, NullTVB,
397                 offset + offsetof(struct ip6_hdr, ip6_dst), 16,
398                 (guint8 *)&ipv6.ip6_dst,
399 #ifdef INET6
400                 "Destination address: %s (%s)",
401                 get_hostname6(&ipv6.ip6_dst),
402 #else
403                 "Destination address: %s",
404 #endif
405                 ip6_to_str(&ipv6.ip6_dst));
406   }
407
408   /* start of the new header (could be a extension header) */
409   nxt = pd[poffset = offset + offsetof(struct ip6_hdr, ip6_nxt)];
410   offset += sizeof(struct ip6_hdr);
411   frag = 0;
412
413   /* Start out assuming this isn't fragmented. */
414   pi.fragmented = FALSE;
415
416 again:
417     switch (nxt) {
418     case IP_PROTO_HOPOPTS:
419         advance = dissect_hopopts(pd, offset, fd, tree);
420         nxt = pd[poffset = offset];
421         offset += advance;
422         goto again;
423     case IP_PROTO_ROUTING:
424         advance = dissect_routing6(pd, offset, fd, tree);
425         nxt = pd[poffset = offset];
426         offset += advance;
427         goto again;
428     case IP_PROTO_FRAGMENT:
429         pi.fragmented = TRUE;
430         advance = dissect_frag6(pd, offset, fd, tree, &frag);
431         nxt = pd[poffset = offset];
432         offset += advance;
433         goto again;
434     case IP_PROTO_AH:
435         advance = dissect_ah_old(pd, offset, fd, tree);
436         nxt = pd[poffset = offset];
437         offset += advance;
438         goto again;
439     case IP_PROTO_DSTOPTS:
440         advance = dissect_dstopts(pd, offset, fd, tree);
441         nxt = pd[poffset = offset];
442         offset += advance;
443         goto again;
444     }
445
446 #ifdef TEST_FINALHDR
447   proto_tree_add_uint_hidden(ipv6_tree, hf_ipv6_final, NullTVB, poffset, 1, nxt);
448 #endif
449   if (frag) {
450     /* fragmented */
451     if (check_col(fd, COL_PROTOCOL))
452       col_set_str(fd, COL_PROTOCOL, "IPv6");
453     /* COL_INFO was filled in by "dissect_frag6()" */
454     old_dissect_data(pd, offset, fd, tree);
455   } else {
456     /* do lookup with the subdissector table */
457     if (!old_dissector_try_port(ip_dissector_table, nxt, pd, offset, fd, tree)) {
458       /* Unknown protocol */
459       if (check_col(fd, COL_PROTOCOL))
460         col_set_str(fd, COL_PROTOCOL, "IPv6");
461       if (check_col(fd, COL_INFO))
462         col_add_fstr(fd, COL_INFO, "%s (0x%02x)", ipprotostr(nxt), nxt);
463       old_dissect_data(pd, offset, fd, tree);
464     }
465   }
466 }
467
468 static void
469 dissect_ipv6_none(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
470   if (check_col(fd, COL_INFO))
471     col_add_fstr(fd, COL_INFO, "IPv6 no next header");
472
473   /* XXX - dissect the payload as padding? */
474 }
475
476 void
477 proto_register_ipv6(void)
478 {
479   static hf_register_info hf[] = {
480     { &hf_ipv6_version,
481       { "Version",              "ipv6.version",
482                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
483     { &hf_ipv6_class,
484       { "Traffic class",        "ipv6.class",
485                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
486     { &hf_ipv6_flow,
487       { "Flowlabel",            "ipv6.flow",
488                                 FT_UINT32, BASE_HEX, NULL, 0x0, "" }},
489     { &hf_ipv6_plen,
490       { "Payload length",       "ipv6.plen",
491                                 FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
492     { &hf_ipv6_nxt,
493       { "Next header",          "ipv6.nxt",
494                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
495     { &hf_ipv6_hlim,
496       { "Hop limit",            "ipv6.hlim",
497                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
498     { &hf_ipv6_src,
499       { "Source",               "ipv6.src",
500                                 FT_IPv6, BASE_NONE, NULL, 0x0,
501                                 "Source IPv6 Address" }},
502     { &hf_ipv6_dst,
503       { "Destination",          "ipv6.dst",
504                                 FT_IPv6, BASE_NONE, NULL, 0x0,
505                                 "Destination IPv6 Address" }},
506     { &hf_ipv6_addr,
507       { "Address",              "ipv6.addr",
508                                 FT_IPv6, BASE_NONE, NULL, 0x0,
509                                 "Source or Destination IPv6 Address" }},
510 #ifdef TEST_FINALHDR
511     { &hf_ipv6_final,
512       { "Final next header",    "ipv6.final",
513                                 FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
514 #endif
515   };
516   static gint *ett[] = {
517     &ett_ipv6,
518   };
519
520   proto_ipv6 = proto_register_protocol("Internet Protocol Version 6",
521                                        "IPv6", "ipv6");
522   proto_register_field_array(proto_ipv6, hf, array_length(hf));
523   proto_register_subtree_array(ett, array_length(ett));
524 }
525
526 void
527 proto_reg_handoff_ipv6(void)
528 {
529         old_dissector_add("ethertype", ETHERTYPE_IPv6, dissect_ipv6);
530         old_dissector_add("ppp.protocol", PPP_IPV6, dissect_ipv6);
531         old_dissector_add("ip.proto", IP_PROTO_IPV6, dissect_ipv6);
532         old_dissector_add("null.type", BSD_AF_INET6_BSD, dissect_ipv6);
533         old_dissector_add("null.type", BSD_AF_INET6_FREEBSD, dissect_ipv6);
534         old_dissector_add("ip.proto", IP_PROTO_NONE, dissect_ipv6_none);
535 }