2 * Routines for null packet disassembly
4 * $Id: packet-null.c,v 1.18 2000/01/23 08:55:35 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
9 * This file created and 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>
44 extern const value_string etype_vals[];
46 /* protocols and header fields */
47 static int proto_null = -1;
48 static int hf_null_etype = -1;
49 static int hf_null_family = -1;
51 static gint ett_null = -1;
53 /* Null/loopback structs and definitions */
55 /* Macro to byte-swap 32-bit quantities. */
57 ((((x)&0xFF000000)>>24) | \
58 (((x)&0x00FF0000)>>8) | \
59 (((x)&0x0000FF00)<<8) | \
60 (((x)&0x000000FF)<<24))
65 #define BSD_AF_APPLETALK 16
67 #define BSD_AF_INET6_BSD 24 /* OpenBSD (and probably NetBSD), BSD/OS */
68 #define BSD_AF_INET6_FREEBSD 28
71 static const value_string family_vals[] = {
74 {BSD_AF_APPLETALK, "Appletalk" },
75 {BSD_AF_IPX, "Netware IPX/SPX"},
76 {BSD_AF_INET6_BSD, "IPv6" },
77 {BSD_AF_INET6_FREEBSD, "IPv6" },
82 capture_null( const u_char *pd, packet_counts *ld )
87 * BSD drivers that use DLT_NULL - including the FreeBSD 3.2 ISDN-for-BSD
88 * drivers, as well as the 4.4-Lite and FreeBSD loopback drivers -
89 * appear to stuff the AF_ value for the protocol, in *host* byte
90 * order, in the first four bytes.
92 * However, according to Gerald Combs, a FreeBSD ISDN PPP dump that
93 * Andreas Klemm sent to ethereal-dev has a packet type of DLT_NULL,
94 * and the family bits look like PPP's protocol field. (Was this an
95 * older, or different, ISDN driver?) Looking at what appears to be
96 * that capture file, it appears that it's using PPP in HDLC framing,
97 * RFC 1549, wherein the first two octets of the frame are 0xFF
98 * (address) and 0x03 (control), so the header bytes are, in order:
102 * high-order byte of a PPP protocol field
103 * low-order byte of a PPP protocol field
105 * when reading it on a little-endian machine; that means it's
106 * PPPP03FF, where PPPP is a byte-swapped PPP protocol field.
108 * "libpcap" for Linux uses DLT_NULL only for the loopback device.
109 * The loopback driver in Linux 2.0.36, at least, puts an *Ethernet*
110 * header at the beginning of loopback packets; however, "libpcap"
111 * for Linux compensates for this by skipping the source and
112 * destination MAC addresses, replacing them with 2 bytes of 0.
113 * This means that if we're reading the capture on a little-endian
114 * machine, the header, treated as a 32-bit integer, looks like
116 * EEEEEEEEEEEEEEEE0000000000000000
118 * where "EEEEEEEEEEEEEEEE" is the Ethernet type, and if we're reading
119 * it on a big-endian machine, it looks like
121 * 0000000000000000EEEEEEEEEEEEEEEE
123 * The Ethernet type might or might not be byte-swapped; I haven't
124 * bothered thinking about that yet.
126 * AF_ values are (relatively) small integers, and shouldn't have their
127 * upper 16 bits zero; Ethernet types have to fit in 16 bits and
128 * thus must have their upper 16 bits zero. Therefore, if the upper
129 * 16 bits of the field aren't zero, it's in the wrong byte order.
131 * Ethernet types are bigger than 1536, and AF_ values are smaller
132 * than 1536, so we needn't worry about one being mistaken for
133 * the other. (There may be a problem if the 16-bit Ethernet
134 * type is byte-swapped as a 16-bit quantity, but if when treated
135 * as a 32-bit quantity its upper 16 bits are zero, but I'll think
136 * about that one later.)
138 * As for the PPP protocol field values:
140 * 0x0000 does not appear to be a valid PPP protocol field value,
141 * so the upper 16 bits will be non-zero, and we'll byte swap it.
146 * where PPPP is a non-byte-swapped PPP protocol field; we'll
147 * check for the upper 16 bits of the byte-swapped field being
148 * non-zero and, if so, assume the lower 16 bits are a PPP
149 * protocol field (AF_ and Ethernet protocol fields should leave
150 * the upper 16 bits zero - unless somebody stuff something else
153 * So, to compensate for this mess, we:
155 * check if the first two octets are 0xFF and 0x03 and, if so,
156 * treat it as a PPP frame;
158 * otherwise, byte-swap the value if its upper 16 bits aren't zero,
159 * and compare the lower 16 bits of the value against Ethernet
162 * If, as implied by an earlier version of the "e_nullhdr" structure,
163 * the family is only 16 bits, and there are "next" and "len" fields
164 * before it, that all goes completely to hell. (Note that, for
165 * the BSD header, we could byte-swap it if the capture was written
166 * on a machine with the opposite byte-order to ours - the "libpcap"
167 * header lets us determine that - but it's more of a mess for Linux,
168 * given that the effect of inserting the two 0 bytes depends only
169 * on the byte order of the machine reading the file.)
171 if (pd[0] == 0xFF && pd[1] == 0x03) {
178 * Treat it as a normal DLT_NULL header.
180 memcpy((char *)&null_header, (char *)&pd[0], sizeof(null_header));
182 if ((null_header & 0xFFFF0000) != 0) {
184 null_header = BSWAP32(null_header);
188 * The null header value must be greater than the IEEE 802.3 maximum
189 * frame length to be a valid Ethernet type; if it is, hand it
190 * to "ethertype()", otherwise treat it as a BSD AF_type (we wire
191 * in the values of the BSD AF_ types, because the values
192 * in the file will be BSD values, and the OS on which
193 * we're building this might not have the same values or
194 * might not have them defined at all; XXX - what if different
195 * BSD derivatives have different values?).
197 if (null_header > IEEE_802_3_MAX_LEN)
198 capture_ethertype(null_header, 4, pd, ld);
200 switch (null_header) {
203 capture_ip(pd, 4, ld);
215 dissect_null( const u_char *pd, frame_data *fd, proto_tree *tree )
222 * See comment in "capture_null()" for an explanation of what we're
225 if (pd[0] == 0xFF && pd[1] == 0x03) {
229 dissect_ppp(pd, fd, tree);
232 * Treat it as a normal DLT_NULL header.
234 memcpy((char *)&null_header, (char *)&pd[0], sizeof(null_header));
236 if ((null_header & 0xFFFF0000) != 0) {
238 null_header = BSWAP32(null_header);
241 /* load the top pane info. This should be overwritten by
242 the next protocol in the stack */
243 if(check_col(fd, COL_RES_DL_SRC))
244 col_add_str(fd, COL_RES_DL_SRC, "N/A" );
245 if(check_col(fd, COL_RES_DL_DST))
246 col_add_str(fd, COL_RES_DL_DST, "N/A" );
247 if(check_col(fd, COL_PROTOCOL))
248 col_add_str(fd, COL_PROTOCOL, "N/A" );
249 if(check_col(fd, COL_INFO))
250 col_add_str(fd, COL_INFO, "Null/Loopback" );
253 * The null header value must be greater than the IEEE 802.3 maximum
254 * frame length to be a valid Ethernet type; if it is, hand it
255 * to "ethertype()", otherwise treat it as a BSD AF_type (we wire
256 * in the values of the BSD AF_ types, because the values
257 * in the file will be BSD values, and the OS on which
258 * we're building this might not have the same values or
259 * might not have them defined at all; XXX - what if different
260 * BSD derivatives have different values?).
262 if (null_header > IEEE_802_3_MAX_LEN) {
264 ti = proto_tree_add_item(tree, proto_null, 0, 4, NULL);
265 fh_tree = proto_item_add_subtree(ti, ett_null);
268 ethertype(null_header, 4, pd, fd, tree, fh_tree, hf_null_etype);
270 /* populate a tree in the second pane with the status of the link
273 ti = proto_tree_add_item(tree, proto_null, 0, 4, NULL);
274 fh_tree = proto_item_add_subtree(ti, ett_null);
275 proto_tree_add_item(fh_tree, hf_null_family, 0, 4, null_header);
278 switch (null_header) {
281 dissect_ip(pd, 4, fd, tree);
284 case BSD_AF_APPLETALK:
285 dissect_ddp(pd, 4, fd, tree);
289 dissect_ipx(pd, 4, fd, tree);
293 dissect_osi(pd, 4, fd, tree);
296 case BSD_AF_INET6_BSD:
297 case BSD_AF_INET6_FREEBSD:
298 dissect_ipv6(pd, 4, fd, tree);
302 dissect_data(pd, 4, fd, tree);
310 proto_register_null(void)
312 static hf_register_info hf[] = {
314 /* registered here but handled in ethertype.c */
316 { "Type", "null.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
320 { "Family", "null.family", FT_UINT32, BASE_HEX, VALS(family_vals), 0x0,
323 static gint *ett[] = {
327 proto_null = proto_register_protocol ("Null/Loopback", "null" );
328 proto_register_field_array(proto_null, hf, array_length(hf));
329 proto_register_subtree_array(ett, array_length(ett));