2 * Routines for null packet disassembly
4 * $Id: packet-null.c,v 1.52 2002/01/21 07:36:38 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
9 * This file created by Mike Hall <mlh@io.com>
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.
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.
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.
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
42 #include <epan/packet.h>
43 #include "packet-null.h"
44 #include <epan/atalk-utils.h>
45 #include "packet-ip.h"
46 #include "packet-ipx.h"
47 #include "packet-osi.h"
48 #include "packet-ppp.h"
52 static dissector_table_t null_dissector_table;
54 /* protocols and header fields */
55 static int proto_null = -1;
56 static int hf_null_etype = -1;
57 static int hf_null_family = -1;
59 static gint ett_null = -1;
61 /* Null/loopback structs and definitions */
64 static const value_string family_vals[] = {
67 {BSD_AF_APPLETALK, "Appletalk" },
68 {BSD_AF_IPX, "Netware IPX/SPX"},
69 {BSD_AF_INET6_BSD, "IPv6" },
70 {BSD_AF_INET6_FREEBSD, "IPv6" },
74 static dissector_handle_t ppp_hdlc_handle;
75 static dissector_handle_t data_handle;
77 capture_null( const u_char *pd, int len, packet_counts *ld )
82 * BSD drivers that use DLT_NULL - including the FreeBSD 3.2 ISDN-for-BSD
83 * drivers, as well as the 4.4-Lite and FreeBSD loopback drivers -
84 * appear to stuff the AF_ value for the protocol, in *host* byte
85 * order, in the first four bytes.
87 * However, according to Gerald Combs, a FreeBSD ISDN PPP dump that
88 * Andreas Klemm sent to ethereal-dev has a packet type of DLT_NULL,
89 * and the family bits look like PPP's protocol field. (Was this an
90 * older, or different, ISDN driver?) Looking at what appears to be
91 * that capture file, it appears that it's using PPP in HDLC framing,
92 * RFC 1549, wherein the first two octets of the frame are 0xFF
93 * (address) and 0x03 (control), so the header bytes are, in order:
97 * high-order byte of a PPP protocol field
98 * low-order byte of a PPP protocol field
100 * when reading it on a little-endian machine; that means it's
101 * PPPP03FF, where PPPP is a byte-swapped PPP protocol field.
103 * "libpcap" for Linux uses DLT_NULL only for the loopback device.
104 * The loopback driver in Linux 2.0.36, at least, puts an *Ethernet*
105 * header at the beginning of loopback packets; however, "libpcap"
106 * for Linux compensates for this by skipping the source and
107 * destination MAC addresses, replacing them with 2 bytes of 0.
108 * This means that if we're reading the capture on a little-endian
109 * machine, the header, treated as a 32-bit integer, looks like
111 * EEEEEEEEEEEEEEEE0000000000000000
113 * where "EEEEEEEEEEEEEEEE" is the Ethernet type, and if we're reading
114 * it on a big-endian machine, it looks like
116 * 0000000000000000EEEEEEEEEEEEEEEE
118 * The Ethernet type might or might not be byte-swapped; I haven't
119 * bothered thinking about that yet.
121 * AF_ values are (relatively) small integers, and shouldn't have their
122 * upper 16 bits zero; Ethernet types have to fit in 16 bits and
123 * thus must have their upper 16 bits zero. Therefore, if the upper
124 * 16 bits of the field aren't zero, it's in the wrong byte order.
126 * Ethernet types are bigger than 1536, and AF_ values are smaller
127 * than 1536, so we needn't worry about one being mistaken for
128 * the other. (There may be a problem if the 16-bit Ethernet
129 * type is byte-swapped as a 16-bit quantity, but if when treated
130 * as a 32-bit quantity its upper 16 bits are zero, but I'll think
131 * about that one later.)
133 * As for the PPP protocol field values:
135 * 0x0000 does not appear to be a valid PPP protocol field value,
136 * so the upper 16 bits will be non-zero, and we'll byte swap it.
141 * where PPPP is a non-byte-swapped PPP protocol field; we'll
142 * check for the upper 16 bits of the byte-swapped field being
143 * non-zero and, if so, assume the lower 16 bits are a PPP
144 * protocol field (AF_ and Ethernet protocol fields should leave
145 * the upper 16 bits zero - unless somebody stuff something else
148 * So, to compensate for this mess, we:
150 * check if the first two octets are 0xFF and 0x03 and, if so,
151 * treat it as a PPP frame;
153 * otherwise, byte-swap the value if its upper 16 bits aren't zero,
154 * and compare the lower 16 bits of the value against Ethernet
157 * If, as implied by an earlier version of the "e_nullhdr" structure,
158 * the family is only 16 bits, and there are "next" and "len" fields
159 * before it, that all goes completely to hell. (Note that, for
160 * the BSD header, we could byte-swap it if the capture was written
161 * on a machine with the opposite byte-order to ours - the "libpcap"
162 * header lets us determine that - but it's more of a mess for Linux,
163 * given that the effect of inserting the two 0 bytes depends only
164 * on the byte order of the machine reading the file.)
166 if (!BYTES_ARE_IN_FRAME(0, len, 2)) {
170 if (pd[0] == 0xFF && pd[1] == 0x03) {
174 capture_ppp_hdlc(pd, 0, len, ld);
177 * Treat it as a normal DLT_NULL header.
179 if (!BYTES_ARE_IN_FRAME(0, len, (int)sizeof(null_header))) {
183 memcpy((char *)&null_header, (char *)&pd[0], sizeof(null_header));
185 if ((null_header & 0xFFFF0000) != 0) {
187 null_header = BSWAP32(null_header);
191 * The null header value must be greater than the IEEE 802.3 maximum
192 * frame length to be a valid Ethernet type; if it is, hand it
193 * to "ethertype()", otherwise treat it as a BSD AF_type (we wire
194 * in the values of the BSD AF_ types, because the values
195 * in the file will be BSD values, and the OS on which
196 * we're building this might not have the same values or
197 * might not have them defined at all; XXX - what if different
198 * BSD derivatives have different values?).
200 if (null_header > IEEE_802_3_MAX_LEN)
201 capture_ethertype(null_header, pd, 4, len, ld);
203 switch (null_header) {
206 capture_ip(pd, 4, len, ld);
218 dissect_null(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
226 * See comment in "capture_null()" for an explanation of what we're
229 if (tvb_get_ntohs(tvb, 0) == 0xFF03) {
233 call_dissector(ppp_hdlc_handle, tvb, pinfo, tree);
236 /* load the top pane info. This should be overwritten by
237 the next protocol in the stack */
238 if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
239 col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "N/A" );
240 if(check_col(pinfo->cinfo, COL_RES_DL_DST))
241 col_set_str(pinfo->cinfo, COL_RES_DL_DST, "N/A" );
242 if(check_col(pinfo->cinfo, COL_PROTOCOL))
243 col_set_str(pinfo->cinfo, COL_PROTOCOL, "N/A" );
244 if(check_col(pinfo->cinfo, COL_INFO))
245 col_set_str(pinfo->cinfo, COL_INFO, "Null/Loopback" );
248 * Treat it as a normal DLT_NULL header.
250 tvb_memcpy(tvb, (guint8 *)&null_header, 0, sizeof(null_header));
252 if ((null_header & 0xFFFF0000) != 0) {
254 null_header = BSWAP32(null_header);
258 * The null header value must be greater than the IEEE 802.3 maximum
259 * frame length to be a valid Ethernet type; if it is, hand it
260 * to "ethertype()", otherwise treat it as a BSD AF_type (we wire
261 * in the values of the BSD AF_ types, because the values
262 * in the file will be BSD values, and the OS on which
263 * we're building this might not have the same values or
264 * might not have them defined at all; XXX - what if different
265 * BSD derivatives have different values?).
267 if (null_header > IEEE_802_3_MAX_LEN) {
269 ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, FALSE);
270 fh_tree = proto_item_add_subtree(ti, ett_null);
273 ethertype(null_header, tvb, 4, pinfo, tree, fh_tree, hf_null_etype, -1);
275 /* populate a tree in the second pane with the status of the link
278 ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, FALSE);
279 fh_tree = proto_item_add_subtree(ti, ett_null);
280 proto_tree_add_uint(fh_tree, hf_null_family, tvb, 0, 4, null_header);
283 next_tvb = tvb_new_subset(tvb, 4, -1, -1);
284 if (!dissector_try_port(null_dissector_table, null_header,
285 next_tvb, pinfo, tree)) {
286 /* No sub-dissector found. Label rest of packet as "Data" */
287 call_dissector(data_handle,next_tvb, pinfo, tree);
294 proto_register_null(void)
296 static hf_register_info hf[] = {
298 /* registered here but handled in ethertype.c */
300 { "Type", "null.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
304 { "Family", "null.family", FT_UINT32, BASE_HEX, VALS(family_vals), 0x0,
307 static gint *ett[] = {
311 proto_null = proto_register_protocol("Null/Loopback", "Null", "null");
312 proto_register_field_array(proto_null, hf, array_length(hf));
313 proto_register_subtree_array(ett, array_length(ett));
315 /* subdissector code */
316 null_dissector_table = register_dissector_table("null.type",
317 "BSD AF_ type", FT_UINT32, BASE_DEC);
321 proto_reg_handoff_null(void)
323 dissector_handle_t null_handle;
326 * Get a handle for the PPP-in-HDLC-like-framing dissector.
328 ppp_hdlc_handle = find_dissector("ppp_hdlc");
329 data_handle = find_dissector("data");
330 null_handle = create_dissector_handle(dissect_null, proto_null);
331 dissector_add("wtap_encap", WTAP_ENCAP_NULL, null_handle);