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