2 * Routines for null packet disassembly
4 * $Id: packet-null.c,v 1.63 2003/12/17 23:35:29 ulfl 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.
34 #include <epan/packet.h>
35 #include "packet-null.h"
36 #include <epan/atalk-utils.h>
38 #include "packet-ip.h"
39 #include "packet-ipx.h"
40 #include "packet-osi.h"
41 #include "packet-ppp.h"
45 static dissector_table_t null_dissector_table;
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;
52 static gint ett_null = -1;
54 /* Null/loopback structs and definitions */
57 static const value_string family_vals[] = {
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" },
68 static dissector_handle_t ppp_hdlc_handle;
69 static dissector_handle_t data_handle;
71 capture_null( const guchar *pd, int len, packet_counts *ld )
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
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
89 * when read on a little-endian machine and
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.
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:
104 * on a little-endian machine:
106 * a little-endian DLT_NULL header looks like
110 * a big-endian DLT_NULL header, or a DLT_LOOP header, looks
115 * an IRIX or UNICOS/mp DLT_NULL header looks like
119 * on a big-endian machine:
121 * a big-endian DLT_NULL header, or a DLT_LOOP header, looks
126 * a little-endian DLT_NULL header looks like
130 * an IRIX or UNICOS/mp DLT_NULL header looks like
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:
144 * high-order byte of a PPP protocol field
145 * low-order byte of a PPP protocol field
147 * If we treat that as a 32-bit host-byte-order value, it looks like
151 * where PPPP is a byte-swapped PPP protocol type if we read it on
152 * a little-endian machine and
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
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
173 * where EEEE is a byte-swapped Ethernet type, and if we're reading it
174 * on a big-endian machine, it looks like
178 * where EEEE is an Ethernet type.
180 * If the first 2 bytes of the header are FF 03:
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
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
190 * it can't be an IRIX or UNICOS/mp DLT_NULL header, as the
191 * resulting AF_ value with be 0x03FF.
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.
196 * Otherwise, if the upper 16 bits are non-zero, either:
198 * it's a BSD DLT_NULL or DLT_LOOP header whose AF_ value
199 * is not in our byte order;
201 * it's an IRIX or UNICOS/mp DLT_NULL header being read on
202 * a big-endian machine;
204 * it's a Linux DLT_NULL header being read on a little-endian
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.
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.
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.
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
229 * So, if the upper 16 bits are non-zero:
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;
235 * otherwise, we byte-swap it and do the next stage.
237 * If the upper 16 bits are zero, either:
239 * it's a BSD DLT_NULLor DLT_LOOP header whose AF_ value is in
242 * it's an IRIX or UNICOS/mp DLT_NULL header being read on
243 * a little-endian machine;
245 * it's a Linux DLT_NULL header being read on a big-endian
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.
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.
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 <
259 if (!BYTES_ARE_IN_FRAME(0, len, 2)) {
263 if (pd[0] == 0xFF && pd[1] == 0x03) {
267 capture_ppp_hdlc(pd, 0, len, ld);
270 * Treat it as a normal DLT_NULL header.
272 if (!BYTES_ARE_IN_FRAME(0, len, (int)sizeof(null_header))) {
276 memcpy((char *)&null_header, (const char *)&pd[0], sizeof(null_header));
278 if ((null_header & 0xFFFF0000) != 0) {
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.
285 if ((null_header & 0xFF000000) == 0 &&
286 (null_header & 0x00FF0000) < 0x00060000) {
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".
295 null_header = BSWAP32(null_header);
299 * Check for an IRIX or UNICOS/mp snoop header.
301 if ((null_header & 0x000000FF) == 0 &&
302 (null_header & 0x0000FF00) < 0x00000600) {
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
309 null_header = BSWAP16(null_header & 0xFFFF);
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?).
323 if (null_header > IEEE_802_3_MAX_LEN)
324 capture_ethertype((guint16) null_header, pd, 4, len, ld);
327 switch (null_header) {
330 capture_ip(pd, 4, len, ld);
342 dissect_null(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
350 * See comment in "capture_null()" for an explanation of what we're
353 if (tvb_get_ntohs(tvb, 0) == 0xFF03) {
357 call_dissector(ppp_hdlc_handle, tvb, pinfo, tree);
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" );
372 * Treat it as a normal DLT_NULL header.
374 tvb_memcpy(tvb, (guint8 *)&null_header, 0, sizeof(null_header));
376 if ((null_header & 0xFFFF0000) != 0) {
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.
383 if ((null_header & 0xFF000000) == 0 &&
384 (null_header & 0x00FF0000) < 0x00060000) {
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".
393 null_header = BSWAP32(null_header);
397 * Check for an IRIX or UNICOS/mp snoop header.
399 if ((null_header & 0x000000FF) == 0 &&
400 (null_header & 0x0000FF00) < 0x00000600) {
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
407 null_header = BSWAP16(null_header & 0xFFFF);
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?).
421 if (null_header > IEEE_802_3_MAX_LEN) {
423 ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, FALSE);
424 fh_tree = proto_item_add_subtree(ti, ett_null);
427 ethertype((guint16) null_header, tvb, 4, pinfo, tree, fh_tree, hf_null_etype, -1,
430 /* populate a tree in the second pane with the status of the link
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);
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);
449 proto_register_null(void)
451 static hf_register_info hf[] = {
453 /* registered here but handled in ethertype.c */
455 { "Type", "null.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
459 { "Family", "null.family", FT_UINT32, BASE_DEC, VALS(family_vals), 0x0,
462 static gint *ett[] = {
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));
470 /* subdissector code */
471 null_dissector_table = register_dissector_table("null.type",
472 "BSD AF_ type", FT_UINT32, BASE_DEC);
476 proto_reg_handoff_null(void)
478 dissector_handle_t null_handle;
481 * Get a handle for the PPP-in-HDLC-like-framing dissector.
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);