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