Fix a URL.
[obnox/wireshark/wip.git] / packet-null.c
1 /* packet-null.c
2  * Routines for null packet disassembly
3  *
4  * $Id: packet-null.c,v 1.60 2003/10/01 07:11:44 guy 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    * appear to stuff the AF_ value for the protocol, in *host* byte
79    * order, in the first four bytes.
80    *
81    * However, according to Gerald Combs, a FreeBSD ISDN PPP dump that
82    * Andreas Klemm sent to ethereal-dev has a packet type of DLT_NULL,
83    * and the family bits look like PPP's protocol field.  (Was this an
84    * older, or different, ISDN driver?)  Looking at what appears to be
85    * that capture file, it appears that it's using PPP in HDLC framing,
86    * RFC 1549, wherein the first two octets of the frame are 0xFF
87    * (address) and 0x03 (control), so the header bytes are, in order:
88    *
89    *    0xFF
90    *    0x03
91    *    high-order byte of a PPP protocol field
92    *    low-order byte of a PPP protocol field
93    *
94    * when reading it on a little-endian machine; that means it's
95    * PPPP03FF, where PPPP is a byte-swapped PPP protocol field.
96    *
97    * "libpcap" for Linux uses DLT_NULL only for the loopback device.
98    * The loopback driver in Linux 2.0.36, at least, puts an *Ethernet*
99    * header at the beginning of loopback packets; however, "libpcap"
100    * for Linux compensates for this by skipping the source and
101    * destination MAC addresses, replacing them with 2 bytes of 0.
102    * This means that if we're reading the capture on a little-endian
103    * machine, the header, treated as a 32-bit integer, looks like
104    *
105    *    EEEEEEEEEEEEEEEE0000000000000000
106    *
107    * where "EEEEEEEEEEEEEEEE" is the Ethernet type, and if we're reading
108    * it on a big-endian machine, it looks like
109    *
110    *    0000000000000000EEEEEEEEEEEEEEEE
111    *
112    * The Ethernet type might or might not be byte-swapped; I haven't
113    * bothered thinking about that yet.
114    *
115    * AF_ values are (relatively) small integers, and shouldn't have their
116    * upper 16 bits zero; Ethernet types have to fit in 16 bits and
117    * thus must have their upper 16 bits zero.  Therefore, if the upper
118    * 16 bits of the field aren't zero, it's in the wrong byte order.
119    *
120    * Ethernet types are bigger than 1536, and AF_ values are smaller
121    * than 1536, so we needn't worry about one being mistaken for
122    * the other.  (There may be a problem if the 16-bit Ethernet
123    * type is byte-swapped as a 16-bit quantity, but if when treated
124    * as a 32-bit quantity its upper 16 bits are zero, but I'll think
125    * about that one later.)
126    *
127    * As for the PPP protocol field values:
128    *
129    * 0x0000 does not appear to be a valid PPP protocol field value,
130    * so the upper 16 bits will be non-zero, and we'll byte swap it.
131    * It'll then be
132    *
133    *    0xFF03PPPP
134    *
135    * where PPPP is a non-byte-swapped PPP protocol field; we'll
136    * check for the upper 16 bits of the byte-swapped field being
137    * non-zero and, if so, assume the lower 16 bits are a PPP
138    * protocol field (AF_ and Ethernet protocol fields should leave
139    * the upper 16 bits zero - unless somebody stuff something else
140    * there; see below).
141    *
142    * So, to compensate for this mess, we:
143    *
144    *    check if the first two octets are 0xFF and 0x03 and, if so,
145    *    treat it as a PPP frame;
146    *
147    *    otherwise, byte-swap the value if its upper 16 bits aren't zero,
148    *    and compare the lower 16 bits of the value against Ethernet
149    *    and AF_ types.
150    *
151    * If, as implied by an earlier version of the "e_nullhdr" structure,
152    * the family is only 16 bits, and there are "next" and "len" fields
153    * before it, that all goes completely to hell.  (Note that, for
154    * the BSD header, we could byte-swap it if the capture was written
155    * on a machine with the opposite byte-order to ours - the "libpcap"
156    * header lets us determine that - but it's more of a mess for Linux,
157    * given that the effect of inserting the two 0 bytes depends only
158    * on the byte order of the machine reading the file.)
159    */
160   if (!BYTES_ARE_IN_FRAME(0, len, 2)) {
161     ld->other++;
162     return;
163   }
164   if (pd[0] == 0xFF && pd[1] == 0x03) {
165     /*
166      * Hand it to PPP.
167      */
168     capture_ppp_hdlc(pd, 0, len, ld);
169   } else {
170     /*
171      * Treat it as a normal DLT_NULL header.
172      */
173     if (!BYTES_ARE_IN_FRAME(0, len, (int)sizeof(null_header))) {
174       ld->other++;
175       return;
176     }
177     memcpy((char *)&null_header, (const char *)&pd[0], sizeof(null_header));
178
179     if ((null_header & 0xFFFF0000) != 0) {
180       /* Byte-swap it. */
181       null_header = BSWAP32(null_header);
182     }
183
184     /*
185      * The null header value must be greater than the IEEE 802.3 maximum
186      * frame length to be a valid Ethernet type; if it is, hand it
187      * to "ethertype()", otherwise treat it as a BSD AF_type (we wire
188      * in the values of the BSD AF_ types, because the values
189      * in the file will be BSD values, and the OS on which
190      * we're building this might not have the same values or
191      * might not have them defined at all; XXX - what if different
192      * BSD derivatives have different values?).
193      */
194     if (null_header > IEEE_802_3_MAX_LEN)
195       capture_ethertype(null_header, pd, 4, len, ld);
196     else {
197       switch (null_header) {
198
199       case BSD_AF_INET:
200         capture_ip(pd, 4, len, ld);
201         break;
202
203       default:
204         ld->other++;
205         break;
206       }
207     }
208   }
209 }
210
211 static void
212 dissect_null(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
213 {
214   guint32       null_header;
215   proto_tree    *fh_tree;
216   proto_item    *ti;
217   tvbuff_t      *next_tvb;
218
219   /*
220    * See comment in "capture_null()" for an explanation of what we're
221    * doing.
222    */
223   if (tvb_get_ntohs(tvb, 0) == 0xFF03) {
224     /*
225      * Hand it to PPP.
226      */
227     call_dissector(ppp_hdlc_handle, tvb, pinfo, tree);
228   } else {
229
230     /* load the top pane info. This should be overwritten by
231        the next protocol in the stack */
232     if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
233       col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "N/A" );
234     if(check_col(pinfo->cinfo, COL_RES_DL_DST))
235       col_set_str(pinfo->cinfo, COL_RES_DL_DST, "N/A" );
236     if(check_col(pinfo->cinfo, COL_PROTOCOL))
237       col_set_str(pinfo->cinfo, COL_PROTOCOL, "N/A" );
238     if(check_col(pinfo->cinfo, COL_INFO))
239       col_set_str(pinfo->cinfo, COL_INFO, "Null/Loopback" );
240
241     /*
242      * Treat it as a normal DLT_NULL header.
243      */
244     tvb_memcpy(tvb, (guint8 *)&null_header, 0, sizeof(null_header));
245
246     if ((null_header & 0xFFFF0000) != 0) {
247       /* Byte-swap it. */
248       null_header = BSWAP32(null_header);
249     }
250
251     /*
252      * The null header value must be greater than the IEEE 802.3 maximum
253      * frame length to be a valid Ethernet type; if it is, hand it
254      * to "ethertype()", otherwise treat it as a BSD AF_type (we wire
255      * in the values of the BSD AF_ types, because the values
256      * in the file will be BSD values, and the OS on which
257      * we're building this might not have the same values or
258      * might not have them defined at all; XXX - what if different
259      * BSD derivatives have different values?).
260      */
261     if (null_header > IEEE_802_3_MAX_LEN) {
262       if (tree) {
263         ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, FALSE);
264         fh_tree = proto_item_add_subtree(ti, ett_null);
265       } else
266         fh_tree = NULL;
267       ethertype(null_header, tvb, 4, pinfo, tree, fh_tree, hf_null_etype, -1,
268         0);
269     } else {
270       /* populate a tree in the second pane with the status of the link
271          layer (ie none) */
272       if (tree) {
273         ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, FALSE);
274         fh_tree = proto_item_add_subtree(ti, ett_null);
275         proto_tree_add_uint(fh_tree, hf_null_family, tvb, 0, 4, null_header);
276       }
277
278       next_tvb = tvb_new_subset(tvb, 4, -1, -1);
279       if (!dissector_try_port(null_dissector_table, null_header,
280             next_tvb, pinfo, tree)) {
281         /* No sub-dissector found.  Label rest of packet as "Data" */
282         call_dissector(data_handle,next_tvb, pinfo, tree);
283       }
284     }
285   }
286 }
287
288 void
289 proto_register_null(void)
290 {
291         static hf_register_info hf[] = {
292
293                 /* registered here but handled in ethertype.c */
294                 { &hf_null_etype,
295                 { "Type",               "null.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
296                         "", HFILL }},
297
298                 { &hf_null_family,
299                 { "Family",             "null.family",  FT_UINT32, BASE_DEC, VALS(family_vals), 0x0,
300                         "", HFILL }}
301         };
302         static gint *ett[] = {
303                 &ett_null,
304         };
305
306         proto_null = proto_register_protocol("Null/Loopback", "Null", "null");
307         proto_register_field_array(proto_null, hf, array_length(hf));
308         proto_register_subtree_array(ett, array_length(ett));
309
310         /* subdissector code */
311         null_dissector_table = register_dissector_table("null.type",
312            "BSD AF_ type", FT_UINT32, BASE_DEC);
313 }
314
315 void
316 proto_reg_handoff_null(void)
317 {
318         dissector_handle_t null_handle;
319
320         /*
321          * Get a handle for the PPP-in-HDLC-like-framing dissector.
322          */
323         ppp_hdlc_handle = find_dissector("ppp_hdlc");
324         data_handle = find_dissector("data");
325         null_handle = create_dissector_handle(dissect_null, proto_null);
326         dissector_add("wtap_encap", WTAP_ENCAP_NULL, null_handle);
327 }