From Didier Gautheron:
[obnox/wireshark/wip.git] / epan / dissectors / packet-null.c
1 /* packet-null.c
2  * Routines for null packet disassembly
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  *
9  * This file created by Mike Hall <mlh@io.com>
10  * Copyright 1998
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32
33 #include <string.h>
34 #include <epan/packet.h>
35 #include "packet-null.h"
36 #include <epan/atalk-utils.h>
37 #include <epan/prefs.h>
38 #include "packet-ip.h"
39 #include "packet-ipv6.h"
40 #include "packet-ipx.h"
41 #include "packet-osi.h"
42 #include "packet-ppp.h"
43 #include <epan/etypes.h>
44 #include <epan/aftypes.h>
45
46 static dissector_table_t null_dissector_table;
47 static dissector_table_t ethertype_dissector_table;
48
49 /* protocols and header fields */
50 static int proto_null = -1;
51 static int hf_null_etype = -1;
52 static int hf_null_family = -1;
53
54 static gint ett_null = -1;
55
56 /* Null/loopback structs and definitions */
57
58 /* Family values. */
59 static const value_string family_vals[] = {
60     {BSD_AF_INET,          "IP"             },
61     {BSD_AF_ISO,           "OSI"            },
62     {BSD_AF_APPLETALK,     "Appletalk"      },
63     {BSD_AF_IPX,           "Netware IPX/SPX"},
64     {BSD_AF_INET6_BSD,     "IPv6"           },
65     {BSD_AF_INET6_FREEBSD, "IPv6"           },
66     {BSD_AF_INET6_DARWIN,  "IPv6"           },
67     {0,                    NULL             }
68 };
69
70 static dissector_handle_t ppp_hdlc_handle;
71 static dissector_handle_t data_handle;
72 void
73 capture_null( const guchar *pd, int len, packet_counts *ld )
74 {
75   guint32 null_header;
76
77   /*
78    * BSD drivers that use DLT_NULL - including the FreeBSD 3.2 ISDN-for-BSD
79    * drivers, as well as the 4.4-Lite and FreeBSD loopback drivers -
80    * stuff the AF_ value for the protocol, in *host* byte order, in the
81    * first four bytes.  (BSD drivers that use DLT_LOOP, such as recent
82    * OpenBSD loopback drivers, stuff it in *network* byte order in the
83    * first four bytes.)
84    *
85    * However, the IRIX and UNICOS/mp snoop socket mechanism supplies,
86    * on loopback devices, a 4-byte header that has a 2 byte (big-endian)
87    * AF_ value and 2 bytes of 0, so it's
88    *
89    *    0000AAAA
90    *
91    * when read on a little-endian machine and
92    *
93    *    AAAA0000
94    *
95    * when read on a big-endian machine.  The current CVS version of libpcap
96    * compensates for this by converting it to standard 4-byte format before
97    * processing the packet, but snoop captures from IRIX or UNICOS/mp
98    * have the 2-byte+2-byte header, as might tcpdump or libpcap captures
99    * with older versions of libpcap.
100    *
101    * AF_ values are small integers, and probably fit in 8 bits (current
102    * values on the BSDs do), and have their upper 24 bits zero.
103    * This means that, in practice, if you look at the header as a 32-bit
104    * integer in host byte order:
105    *
106    *    on a little-endian machine:
107    *
108    *            a little-endian DLT_NULL header looks like
109    *
110    *                    000000AA
111    *
112    *            a big-endian DLT_NULL header, or a DLT_LOOP header, looks
113    *            like
114    *
115    *                    AA000000
116    *
117    *            an IRIX or UNICOS/mp DLT_NULL header looks like
118    *
119    *                    0000AA00
120    *
121    *    on a big-endian machine:
122    *
123    *            a big-endian DLT_NULL header, or a DLT_LOOP header, looks
124    *            like
125    *
126    *                    000000AA
127    *
128    *            a little-endian DLT_NULL header looks like
129    *
130    *                    AA000000
131    *
132    *            an IRIX or UNICOS/mp DLT_NULL header looks like
133    *
134    *                    00AA0000
135    *
136    * However, according to Gerald Combs, a FreeBSD ISDN PPP dump that
137    * Andreas Klemm sent to wireshark-dev has a packet type of DLT_NULL,
138    * and the family bits look like PPP's protocol field.  (Was this an
139    * older, or different, ISDN driver?)  Looking at what appears to be
140    * that capture file, it appears that it's using PPP in HDLC framing,
141    * RFC 1549, wherein the first two octets of the frame are 0xFF
142    * (address) and 0x03 (control), so the header bytes are, in order:
143    *
144    *    0xFF
145    *    0x03
146    *    high-order byte of a PPP protocol field
147    *    low-order byte of a PPP protocol field
148    *
149    * If we treat that as a 32-bit host-byte-order value, it looks like
150    *
151    *    PPPP03FF
152    *
153    * where PPPP is a byte-swapped PPP protocol type if we read it on
154    * a little-endian machine and
155    *
156    *    FF03PPPP
157    *
158    * where PPPP is a PPP protocol type if we read it on a big-endian
159    * machine.  0x0000 does not appear to be a valid PPP protocol type
160    * value, so at least one of those hex digits is guaranteed not to
161    * be 0.
162    *
163    * Old versions of libpcap for Linux used DLT_NULL for loopback devices,
164    * but not any other devices.  (Current versions use DLT_EN10MB for it.)
165    * The Linux loopback driver puts an *Ethernet* header at the beginning
166    * of loopback packets, with fake source and destination addresses and
167    * the appropriate Ethernet type value; however, those older versions of
168    * libpcap for Linux compensated for this by skipping the source and
169    * destination MAC addresses, replacing them with 2 bytes of 0.
170    * This means that if we're reading the capture on a little-endian
171    * machine, the header, treated as a 32-bit integer, looks like
172    *
173    *    EEEE0000
174    *
175    * where EEEE is a byte-swapped Ethernet type, and if we're reading it
176    * on a big-endian machine, it looks like
177    *
178    *    0000EEEE
179    *
180    * where EEEE is an Ethernet type.
181    *
182    * If the first 2 bytes of the header are FF 03:
183    *
184    *    it can't be a big-endian BSD DLT_NULL header, or a DLT_LOOP
185    *    header, as AF_ values are small so the first 2 bytes of the
186    *    header would be 0;
187    *
188    *    it can't be a little-endian BSD DLT_NULL header, as the
189    *    resulting AF_ value would be >= 0x03FF, which is too big
190    *    for an AF_ value;
191    *
192    *    it can't be an IRIX or UNICOS/mp DLT_NULL header, as the
193    *    resulting AF_ value with be 0x03FF.
194    *
195    * So the first thing we do is check the first two bytes of the
196    * header; if it's FF 03, we treat the packet as a PPP frame.
197    *
198    * Otherwise, if the upper 16 bits are non-zero, either:
199    *
200    *    it's a BSD DLT_NULL or DLT_LOOP header whose AF_ value
201    *    is not in our byte order;
202    *
203    *    it's an IRIX or UNICOS/mp DLT_NULL header being read on
204    *    a big-endian machine;
205    *
206    *    it's a Linux DLT_NULL header being read on a little-endian
207    *    machine.
208    *
209    * In all those cases except for the IRIX or UNICOS/mp DLT_NULL header,
210    * we should byte-swap it (if it's a Linux DLT_NULL header, that'll
211    * put the Ethernet type in the right byte order).  In the case
212    * of the IRIX or UNICOS/mp DLT_NULL header, we should just get
213    * the upper 16 bits as an AF_ value.
214    *
215    * If it's a BSD DLT_NULL or DLT_LOOP header whose AF_ value is not
216    * in our byte order, then the upper 2 hex digits would be non-zero
217    * and the next 2 hex digits down would be zero, as AF_ values fit in
218    * 8 bits, and the upper 2 hex digits are the *lower* 8 bits of the value.
219    *
220    * If it's an IRIX or UNICOS/mp DLT_NULL header, the upper 2 hex digits
221    * would be zero and the next 2 hex digits down would be non-zero, as
222    * the upper 16 bits are a big-endian AF_ value.  Furthermore, the
223    * next 2 hex digits down are likely to be < 0x60, as 0x60 is 96,
224    * and, so far, we're far from requiring AF_ values that high.
225    *
226    * If it's a Linux DLT_NULL header, the third hex digit from the top
227    * will be >= 6, as Ethernet types are >= 1536, or 0x0600, and
228    * it's byte-swapped, so the second 2 hex digits from the top are
229    * >= 0x60.
230    *
231    * So, if the upper 16 bits are non-zero:
232    *
233    *    if the upper 2 hex digits are 0 and the next 2 hex digits are
234    *    in the range 0x00-0x5F, we treat it as a big-endian IRIX or
235    *    UNICOS/mp DLT_NULL header;
236    *
237    *    otherwise, we byte-swap it and do the next stage.
238    *
239    * If the upper 16 bits are zero, either:
240    *
241    *    it's a BSD DLT_NULLor DLT_LOOP header whose AF_ value is in
242    *    our byte order;
243    *
244    *    it's an IRIX or UNICOS/mp DLT_NULL header being read on
245    *    a little-endian machine;
246    *
247    *    it's a Linux DLT_NULL header being read on a big-endian
248    *    machine.
249    *
250    * In all of those cases except for the IRIX or UNICOS/mp DLT_NULL header,
251    * we should *not* byte-swap it.  In the case of the IRIX or UNICOS/mp
252    * DLT_NULL header, we should extract the AF_ value and byte-swap it.
253    *
254    * If it's a BSD DLT_NULL or DLT_LOOP header whose AF_ value is
255    * in our byte order, the upper 6 hex digits would all be zero.
256    *
257    * If it's an IRIX or UNICOS/mp DLT_NULL header, the upper 4 hex
258    * digits would be zero and the next 2 hex digits would not be zero.
259    * Furthermore, the third hex digit from the bottom would be < 
260    */
261   if (!BYTES_ARE_IN_FRAME(0, len, 2)) {
262     ld->other++;
263     return;
264   }
265   if (pd[0] == 0xFF && pd[1] == 0x03) {
266     /*
267      * Hand it to PPP.
268      */
269     capture_ppp_hdlc(pd, 0, len, ld);
270   } else {
271     /*
272      * Treat it as a normal DLT_NULL header.
273      */
274     if (!BYTES_ARE_IN_FRAME(0, len, (int)sizeof(null_header))) {
275       ld->other++;
276       return;
277     }
278     memcpy((char *)&null_header, (const char *)&pd[0], sizeof(null_header));
279
280     if ((null_header & 0xFFFF0000) != 0) {
281       /*
282        * It is possible that the AF_ type was only a 16 bit value.
283        * IRIX and UNICOS/mp loopback snoop use a 4 byte header with
284        * AF_ type in the first 2 bytes!
285        * BSD AF_ types will always have the upper 8 bits as 0.
286        */
287       if ((null_header & 0xFF000000) == 0 &&
288           (null_header & 0x00FF0000) < 0x00060000) {
289         /*
290          * Looks like a IRIX or UNICOS/mp loopback header, in the
291          * correct byte order.  Set the null header value to the
292          * AF_ type, which is in the upper 16 bits of "null_header".
293          */
294         null_header >>= 16;
295       } else {
296         /* Byte-swap it. */
297         null_header = BSWAP32(null_header);
298       }
299     } else {
300       /*
301        * Check for an IRIX or UNICOS/mp snoop header.
302        */
303       if ((null_header & 0x000000FF) == 0 &&
304           (null_header & 0x0000FF00) < 0x00000600) {
305         /*
306          * Looks like a IRIX or UNICOS/mp loopback header, in the
307          * wrong byte order.  Set the null header value to the AF_
308          * type; that's in the lower 16 bits of "null_header", but
309          * is byte-swapped.
310          */
311         null_header = BSWAP16(null_header & 0xFFFF);
312       }
313     }
314
315     /*
316      * The null header value must be greater than the IEEE 802.3 maximum
317      * frame length to be a valid Ethernet type; if it is, hand it
318      * to "capture_ethertype()", otherwise treat it as a BSD AF_type (we
319      * wire in the values of the BSD AF_ types, because the values
320      * in the file will be BSD values, and the OS on which
321      * we're building this might not have the same values or
322      * might not have them defined at all; XXX - what if different
323      * BSD derivatives have different values?).
324      */
325     if (null_header > IEEE_802_3_MAX_LEN)
326       capture_ethertype((guint16) null_header, pd, 4, len, ld);
327     else {
328
329       switch (null_header) {
330
331       case BSD_AF_INET:
332         capture_ip(pd, 4, len, ld);
333         break;
334
335       case BSD_AF_INET6_BSD:
336       case BSD_AF_INET6_FREEBSD:
337       case BSD_AF_INET6_DARWIN:
338         capture_ipv6(pd, 4, len, ld);
339         break;
340
341       default:
342         ld->other++;
343         break;
344       }
345     }
346   }
347 }
348
349 static void
350 dissect_null(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
351 {
352   guint32       null_header;
353   proto_tree    *fh_tree;
354   proto_item    *ti;
355   tvbuff_t      *next_tvb;
356
357   /*
358    * See comment in "capture_null()" for an explanation of what we're
359    * doing.
360    */
361   if (tvb_get_ntohs(tvb, 0) == 0xFF03) {
362     /*
363      * Hand it to PPP.
364      */
365     call_dissector(ppp_hdlc_handle, tvb, pinfo, tree);
366   } else {
367
368     /* load the top pane info. This should be overwritten by
369        the next protocol in the stack */
370     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "N/A");
371     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "N/A");
372     col_set_str(pinfo->cinfo, COL_PROTOCOL, "N/A");
373     col_set_str(pinfo->cinfo, COL_INFO, "Null/Loopback");
374
375     /*
376      * Treat it as a normal DLT_NULL header.
377      */
378     tvb_memcpy(tvb, (guint8 *)&null_header, 0, sizeof(null_header));
379
380     if ((null_header & 0xFFFF0000) != 0) {
381       /*
382        * It is possible that the AF_ type was only a 16 bit value.
383        * IRIX and UNICOS/mp loopback snoop use a 4 byte header with
384        * AF_ type in the first 2 bytes!
385        * BSD AF_ types will always have the upper 8 bits as 0.
386        */
387       if ((null_header & 0xFF000000) == 0 &&
388           (null_header & 0x00FF0000) < 0x00060000) {
389         /*
390          * Looks like a IRIX or UNICOS/mp loopback header, in the
391          * correct byte order.  Set the null header value to the
392          * AF_ type, which is in the upper 16 bits of "null_header".
393          */
394         null_header >>= 16;
395       } else {
396         /* Byte-swap it. */
397         null_header = BSWAP32(null_header);
398       }
399     } else {
400       /*
401        * Check for an IRIX or UNICOS/mp snoop header.
402        */
403       if ((null_header & 0x000000FF) == 0 &&
404           (null_header & 0x0000FF00) < 0x00000600) {
405         /*
406          * Looks like a IRIX or UNICOS/mp loopback header, in the
407          * wrong byte order.  Set the null header value to the AF_
408          * type; that's in the lower 16 bits of "null_header", but
409          * is byte-swapped.
410          */
411         null_header = BSWAP16(null_header & 0xFFFF);
412       }
413     }
414
415     /*
416      * The null header value must be greater than the IEEE 802.3 maximum
417      * frame length to be a valid Ethernet type; if it is, dissect it
418      * as one, otherwise treat it as a BSD AF_type (we wire in the values
419      * of the BSD AF_ types, because the values in the file will be BSD
420      * values, and the OS on which we're building this might not have the
421      * same values or might not have them defined at all; XXX - what if
422      * different BSD derivatives have different values?).
423      */
424     if (null_header > IEEE_802_3_MAX_LEN) {
425       if (tree) {
426         ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, FALSE);
427         fh_tree = proto_item_add_subtree(ti, ett_null);
428         proto_tree_add_uint(fh_tree, hf_null_etype, tvb, 0, 4,
429           (guint16) null_header);
430       }
431
432       next_tvb = tvb_new_subset_remaining(tvb, 4);
433       if (!dissector_try_port(ethertype_dissector_table,
434             (guint16) null_header, next_tvb, pinfo, tree))
435         call_dissector(data_handle, next_tvb, pinfo, tree);
436     } else {
437       /* populate a tree in the second pane with the status of the link
438          layer (ie none) */
439       if (tree) {
440         ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, FALSE);
441         fh_tree = proto_item_add_subtree(ti, ett_null);
442         proto_tree_add_uint(fh_tree, hf_null_family, tvb, 0, 4, null_header);
443       }
444
445       next_tvb = tvb_new_subset_remaining(tvb, 4);
446       if (!dissector_try_port(null_dissector_table, null_header,
447             next_tvb, pinfo, tree)) {
448         /* No sub-dissector found.  Label rest of packet as "Data" */
449         call_dissector(data_handle,next_tvb, pinfo, tree);
450       }
451     }
452   }
453 }
454
455 void
456 proto_register_null(void)
457 {
458         static hf_register_info hf[] = {
459
460                 /* registered here but handled in ethertype.c */
461                 { &hf_null_etype,
462                 { "Type",               "null.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
463                         NULL, HFILL }},
464
465                 { &hf_null_family,
466                 { "Family",             "null.family",  FT_UINT32, BASE_DEC, VALS(family_vals), 0x0,
467                         NULL, HFILL }}
468         };
469         static gint *ett[] = {
470                 &ett_null,
471         };
472
473         proto_null = proto_register_protocol("Null/Loopback", "Null", "null");
474         proto_register_field_array(proto_null, hf, array_length(hf));
475         proto_register_subtree_array(ett, array_length(ett));
476
477         /* subdissector code */
478         null_dissector_table = register_dissector_table("null.type",
479            "BSD AF_ type", FT_UINT32, BASE_DEC);
480 }
481
482 void
483 proto_reg_handoff_null(void)
484 {
485         dissector_handle_t null_handle;
486
487         /*
488          * Get a handle for the PPP-in-HDLC-like-framing dissector and
489          * the "I don't know what this is" dissector.
490          */
491         ppp_hdlc_handle = find_dissector("ppp_hdlc");
492         data_handle = find_dissector("data");
493
494         ethertype_dissector_table = find_dissector_table("ethertype");
495
496         null_handle = create_dissector_handle(dissect_null, proto_null);
497         dissector_add("wtap_encap", WTAP_ENCAP_NULL, null_handle);
498 }