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