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