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