2 * Routines for null packet disassembly
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <wsutil/pint.h>
34 #include <epan/packet.h>
35 #include "packet-null.h"
36 #include <epan/atalk-utils.h>
37 #include <epan/prefs.h>
38 #include "packet-ip.h"
39 #include "packet-ipv6.h"
40 #include "packet-ipx.h"
41 #include "packet-osi.h"
42 #include "packet-ppp.h"
43 #include <epan/etypes.h>
44 #include <epan/aftypes.h>
46 static dissector_table_t null_dissector_table
;
47 static dissector_table_t ethertype_dissector_table
;
49 /* protocols and header fields */
50 static int proto_null
= -1;
51 static int hf_null_etype
= -1;
52 static int hf_null_family
= -1;
54 static gint ett_null
= -1;
56 /* Null/loopback structs and definitions */
59 static const value_string family_vals
[] = {
62 {BSD_AF_APPLETALK
, "Appletalk" },
63 {BSD_AF_IPX
, "Netware IPX/SPX"},
64 {BSD_AF_INET6_BSD
, "IPv6" },
65 {BSD_AF_INET6_FREEBSD
, "IPv6" },
66 {BSD_AF_INET6_DARWIN
, "IPv6" },
70 static dissector_handle_t ppp_hdlc_handle
;
71 static dissector_handle_t data_handle
;
73 capture_null( const guchar
*pd
, int len
, packet_counts
*ld
)
78 * BSD drivers that use DLT_NULL - including the FreeBSD 3.2 ISDN-for-BSD
79 * drivers, as well as the 4.4-Lite and FreeBSD loopback drivers -
80 * stuff the AF_ value for the protocol, in *host* byte order, in the
81 * first four bytes. (BSD drivers that use DLT_LOOP, such as recent
82 * OpenBSD loopback drivers, stuff it in *network* byte order in the
85 * However, the IRIX and UNICOS/mp snoop socket mechanism supplies,
86 * on loopback devices, a 4-byte header that has a 2 byte (big-endian)
87 * AF_ value and 2 bytes of 0, so it's
91 * when read on a little-endian machine and
95 * when read on a big-endian machine. The current CVS version of libpcap
96 * compensates for this by converting it to standard 4-byte format before
97 * processing the packet, but snoop captures from IRIX or UNICOS/mp
98 * have the 2-byte+2-byte header, as might tcpdump or libpcap captures
99 * with older versions of libpcap.
101 * AF_ values are small integers, and probably fit in 8 bits (current
102 * values on the BSDs do), and have their upper 24 bits zero.
103 * This means that, in practice, if you look at the header as a 32-bit
104 * integer in host byte order:
106 * on a little-endian machine:
108 * a little-endian DLT_NULL header looks like
112 * a big-endian DLT_NULL header, or a DLT_LOOP header, looks
117 * an IRIX or UNICOS/mp DLT_NULL header looks like
121 * on a big-endian machine:
123 * a big-endian DLT_NULL header, or a DLT_LOOP header, looks
128 * a little-endian DLT_NULL header looks like
132 * an IRIX or UNICOS/mp DLT_NULL header looks like
136 * However, according to Gerald Combs, a FreeBSD ISDN PPP dump that
137 * Andreas Klemm sent to wireshark-dev has a packet type of DLT_NULL,
138 * and the family bits look like PPP's protocol field. (Was this an
139 * older, or different, ISDN driver?) Looking at what appears to be
140 * that capture file, it appears that it's using PPP in HDLC framing,
141 * RFC 1549, wherein the first two octets of the frame are 0xFF
142 * (address) and 0x03 (control), so the header bytes are, in order:
146 * high-order byte of a PPP protocol field
147 * low-order byte of a PPP protocol field
149 * If we treat that as a 32-bit host-byte-order value, it looks like
153 * where PPPP is a byte-swapped PPP protocol type if we read it on
154 * a little-endian machine and
158 * where PPPP is a PPP protocol type if we read it on a big-endian
159 * machine. 0x0000 does not appear to be a valid PPP protocol type
160 * value, so at least one of those hex digits is guaranteed not to
163 * Old versions of libpcap for Linux used DLT_NULL for loopback devices,
164 * but not any other devices. (Current versions use DLT_EN10MB for it.)
165 * The Linux loopback driver puts an *Ethernet* header at the beginning
166 * of loopback packets, with fake source and destination addresses and
167 * the appropriate Ethernet type value; however, those older versions of
168 * libpcap for Linux compensated for this by skipping the source and
169 * destination MAC addresses, replacing them with 2 bytes of 0.
170 * This means that if we're reading the capture on a little-endian
171 * machine, the header, treated as a 32-bit integer, looks like
175 * where EEEE is a byte-swapped Ethernet type, and if we're reading it
176 * on a big-endian machine, it looks like
180 * where EEEE is an Ethernet type.
182 * If the first 2 bytes of the header are FF 03:
184 * it can't be a big-endian BSD DLT_NULL header, or a DLT_LOOP
185 * header, as AF_ values are small so the first 2 bytes of the
188 * it can't be a little-endian BSD DLT_NULL header, as the
189 * resulting AF_ value would be >= 0x03FF, which is too big
192 * it can't be an IRIX or UNICOS/mp DLT_NULL header, as the
193 * resulting AF_ value with be 0x03FF.
195 * So the first thing we do is check the first two bytes of the
196 * header; if it's FF 03, we treat the packet as a PPP frame.
198 * Otherwise, if the upper 16 bits are non-zero, either:
200 * it's a BSD DLT_NULL or DLT_LOOP header whose AF_ value
201 * is not in our byte order;
203 * it's an IRIX or UNICOS/mp DLT_NULL header being read on
204 * a big-endian machine;
206 * it's a Linux DLT_NULL header being read on a little-endian
209 * In all those cases except for the IRIX or UNICOS/mp DLT_NULL header,
210 * we should byte-swap it (if it's a Linux DLT_NULL header, that'll
211 * put the Ethernet type in the right byte order). In the case
212 * of the IRIX or UNICOS/mp DLT_NULL header, we should just get
213 * the upper 16 bits as an AF_ value.
215 * If it's a BSD DLT_NULL or DLT_LOOP header whose AF_ value is not
216 * in our byte order, then the upper 2 hex digits would be non-zero
217 * and the next 2 hex digits down would be zero, as AF_ values fit in
218 * 8 bits, and the upper 2 hex digits are the *lower* 8 bits of the value.
220 * If it's an IRIX or UNICOS/mp DLT_NULL header, the upper 2 hex digits
221 * would be zero and the next 2 hex digits down would be non-zero, as
222 * the upper 16 bits are a big-endian AF_ value. Furthermore, the
223 * next 2 hex digits down are likely to be < 0x60, as 0x60 is 96,
224 * and, so far, we're far from requiring AF_ values that high.
226 * If it's a Linux DLT_NULL header, the third hex digit from the top
227 * will be >= 6, as Ethernet types are >= 1536, or 0x0600, and
228 * it's byte-swapped, so the second 2 hex digits from the top are
231 * So, if the upper 16 bits are non-zero:
233 * if the upper 2 hex digits are 0 and the next 2 hex digits are
234 * in the range 0x00-0x5F, we treat it as a big-endian IRIX or
235 * UNICOS/mp DLT_NULL header;
237 * otherwise, we byte-swap it and do the next stage.
239 * If the upper 16 bits are zero, either:
241 * it's a BSD DLT_NULLor DLT_LOOP header whose AF_ value is in
244 * it's an IRIX or UNICOS/mp DLT_NULL header being read on
245 * a little-endian machine;
247 * it's a Linux DLT_NULL header being read on a big-endian
250 * In all of those cases except for the IRIX or UNICOS/mp DLT_NULL header,
251 * we should *not* byte-swap it. In the case of the IRIX or UNICOS/mp
252 * DLT_NULL header, we should extract the AF_ value and byte-swap it.
254 * If it's a BSD DLT_NULL or DLT_LOOP header whose AF_ value is
255 * in our byte order, the upper 6 hex digits would all be zero.
257 * If it's an IRIX or UNICOS/mp DLT_NULL header, the upper 4 hex
258 * digits would be zero and the next 2 hex digits would not be zero.
259 * Furthermore, the third hex digit from the bottom would be <
261 if (!BYTES_ARE_IN_FRAME(0, len
, 2)) {
265 if (pd
[0] == 0xFF && pd
[1] == 0x03) {
269 capture_ppp_hdlc(pd
, 0, len
, ld
);
272 * Treat it as a normal DLT_NULL header.
274 if (!BYTES_ARE_IN_FRAME(0, len
, (int)sizeof(null_header
))) {
278 memcpy((char *)&null_header
, (const char *)&pd
[0], sizeof(null_header
));
280 if ((null_header
& 0xFFFF0000) != 0) {
282 * It is possible that the AF_ type was only a 16 bit value.
283 * IRIX and UNICOS/mp loopback snoop use a 4 byte header with
284 * AF_ type in the first 2 bytes!
285 * BSD AF_ types will always have the upper 8 bits as 0.
287 if ((null_header
& 0xFF000000) == 0 &&
288 (null_header
& 0x00FF0000) < 0x00060000) {
290 * Looks like a IRIX or UNICOS/mp loopback header, in the
291 * correct byte order. Set the null header value to the
292 * AF_ type, which is in the upper 16 bits of "null_header".
297 null_header
= BSWAP32(null_header
);
301 * Check for an IRIX or UNICOS/mp snoop header.
303 if ((null_header
& 0x000000FF) == 0 &&
304 (null_header
& 0x0000FF00) < 0x00000600) {
306 * Looks like a IRIX or UNICOS/mp loopback header, in the
307 * wrong byte order. Set the null header value to the AF_
308 * type; that's in the lower 16 bits of "null_header", but
311 null_header
= BSWAP16(null_header
& 0xFFFF);
316 * The null header value must be greater than the IEEE 802.3 maximum
317 * frame length to be a valid Ethernet type; if it is, hand it
318 * to "capture_ethertype()", otherwise treat it as a BSD AF_type (we
319 * wire in the values of the BSD AF_ types, because the values
320 * in the file will be BSD values, and the OS on which
321 * we're building this might not have the same values or
322 * might not have them defined at all; XXX - what if different
323 * BSD derivatives have different values?).
325 if (null_header
> IEEE_802_3_MAX_LEN
)
326 capture_ethertype((guint16
) null_header
, pd
, 4, len
, ld
);
329 switch (null_header
) {
332 capture_ip(pd
, 4, len
, ld
);
335 case BSD_AF_INET6_BSD
:
336 case BSD_AF_INET6_FREEBSD
:
337 case BSD_AF_INET6_DARWIN
:
338 capture_ipv6(pd
, 4, len
, ld
);
350 dissect_null(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
)
358 * See comment in "capture_null()" for an explanation of what we're
361 if (tvb_get_ntohs(tvb
, 0) == 0xFF03) {
365 call_dissector(ppp_hdlc_handle
, tvb
, pinfo
, tree
);
368 /* load the top pane info. This should be overwritten by
369 the next protocol in the stack */
370 col_set_str(pinfo
->cinfo
, COL_RES_DL_SRC
, "N/A");
371 col_set_str(pinfo
->cinfo
, COL_RES_DL_DST
, "N/A");
372 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "N/A");
373 col_set_str(pinfo
->cinfo
, COL_INFO
, "Null/Loopback");
376 * Treat it as a normal DLT_NULL header.
378 tvb_memcpy(tvb
, (guint8
*)&null_header
, 0, sizeof(null_header
));
380 if ((null_header
& 0xFFFF0000) != 0) {
382 * It is possible that the AF_ type was only a 16 bit value.
383 * IRIX and UNICOS/mp loopback snoop use a 4 byte header with
384 * AF_ type in the first 2 bytes!
385 * BSD AF_ types will always have the upper 8 bits as 0.
387 if ((null_header
& 0xFF000000) == 0 &&
388 (null_header
& 0x00FF0000) < 0x00060000) {
390 * Looks like a IRIX or UNICOS/mp loopback header, in the
391 * correct byte order. Set the null header value to the
392 * AF_ type, which is in the upper 16 bits of "null_header".
397 null_header
= BSWAP32(null_header
);
401 * Check for an IRIX or UNICOS/mp snoop header.
403 if ((null_header
& 0x000000FF) == 0 &&
404 (null_header
& 0x0000FF00) < 0x00000600) {
406 * Looks like a IRIX or UNICOS/mp loopback header, in the
407 * wrong byte order. Set the null header value to the AF_
408 * type; that's in the lower 16 bits of "null_header", but
411 null_header
= BSWAP16(null_header
& 0xFFFF);
416 * The null header value must be greater than the IEEE 802.3 maximum
417 * frame length to be a valid Ethernet type; if it is, dissect it
418 * as one, otherwise treat it as a BSD AF_type (we wire in the values
419 * of the BSD AF_ types, because the values in the file will be BSD
420 * values, and the OS on which we're building this might not have the
421 * same values or might not have them defined at all; XXX - what if
422 * different BSD derivatives have different values?).
424 if (null_header
> IEEE_802_3_MAX_LEN
) {
426 ti
= proto_tree_add_item(tree
, proto_null
, tvb
, 0, 4, ENC_NA
);
427 fh_tree
= proto_item_add_subtree(ti
, ett_null
);
428 proto_tree_add_uint(fh_tree
, hf_null_etype
, tvb
, 0, 4,
429 (guint16
) null_header
);
432 next_tvb
= tvb_new_subset_remaining(tvb
, 4);
433 if (!dissector_try_uint(ethertype_dissector_table
,
434 (guint16
) null_header
, next_tvb
, pinfo
, tree
))
435 call_dissector(data_handle
, next_tvb
, pinfo
, tree
);
437 /* populate a tree in the second pane with the status of the link
440 ti
= proto_tree_add_item(tree
, proto_null
, tvb
, 0, 4, ENC_NA
);
441 fh_tree
= proto_item_add_subtree(ti
, ett_null
);
442 proto_tree_add_uint(fh_tree
, hf_null_family
, tvb
, 0, 4, null_header
);
445 next_tvb
= tvb_new_subset_remaining(tvb
, 4);
446 if (!dissector_try_uint(null_dissector_table
, null_header
,
447 next_tvb
, pinfo
, tree
)) {
448 /* No sub-dissector found. Label rest of packet as "Data" */
449 call_dissector(data_handle
,next_tvb
, pinfo
, tree
);
456 proto_register_null(void)
458 static hf_register_info hf
[] = {
460 /* registered here but handled in ethertype.c */
462 { "Type", "null.type", FT_UINT16
, BASE_HEX
, VALS(etype_vals
), 0x0,
466 { "Family", "null.family", FT_UINT32
, BASE_DEC
, VALS(family_vals
), 0x0,
469 static gint
*ett
[] = {
473 proto_null
= proto_register_protocol("Null/Loopback", "Null", "null");
474 proto_register_field_array(proto_null
, hf
, array_length(hf
));
475 proto_register_subtree_array(ett
, array_length(ett
));
477 /* subdissector code */
478 null_dissector_table
= register_dissector_table("null.type",
479 "Null type", FT_UINT32
, BASE_DEC
);
483 proto_reg_handoff_null(void)
485 dissector_handle_t null_handle
;
488 * Get a handle for the PPP-in-HDLC-like-framing dissector and
489 * the "I don't know what this is" dissector.
491 ppp_hdlc_handle
= find_dissector("ppp_hdlc");
492 data_handle
= find_dissector("data");
494 ethertype_dissector_table
= find_dissector_table("ethertype");
496 null_handle
= create_dissector_handle(dissect_null
, proto_null
);
497 dissector_add_uint("wtap_encap", WTAP_ENCAP_NULL
, null_handle
);