Move the routine to get a list of the network interfaces on the system
[obnox/wireshark/wip.git] / packet-eth.c
1 /* packet-eth.c
2  * Routines for ethernet packet disassembly
3  *
4  * $Id: packet-eth.c,v 1.25 1999/11/30 23:56:35 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <glib.h>
35 #include "packet.h"
36 #include "etypes.h"
37 #include "resolv.h"
38
39 extern const value_string etype_vals[];
40
41 /* protocols and header fields */
42 static int proto_eth = -1;
43 static int hf_eth_dst = -1;
44 static int hf_eth_src = -1;
45 static int hf_eth_len = -1;
46 static int hf_eth_type = -1;
47
48 static gint ett_ieee8023 = -1;
49 static gint ett_ether2 = -1;
50
51 #define ETH_HEADER_SIZE 14
52
53 /* These are the Netware-ish names for the different Ethernet frame types.
54         EthernetII: The ethernet with a Type field instead of a length field
55         Ethernet802.2: An 802.3 header followed by an 802.3 header
56         Ethernet802.3: A raw 802.3 packet. IPX/SPX can be the only payload.
57                         There's not 802.2 hdr in this.
58         EthernetSNAP: Basically 802.2, just with 802.2SNAP. For our purposes,
59                 there's no difference between 802.2 and 802.2SNAP, since we just
60                 pass it down to dissect_llc(). -- Gilbert
61 */
62 #define ETHERNET_II     0
63 #define ETHERNET_802_2  1
64 #define ETHERNET_802_3  2
65 #define ETHERNET_SNAP   3
66
67 void
68 capture_eth(const u_char *pd, guint32 cap_len, packet_counts *ld) {
69   guint16 etype;
70   int     offset = ETH_HEADER_SIZE;
71   int     ethhdr_type;  /* the type of ethernet frame */
72
73   if (cap_len < ETH_HEADER_SIZE) {
74     ld->other++;
75     return;
76   }
77   
78   etype = (pd[12] << 8) | pd[13];
79
80         /* either ethernet802.3 or ethernet802.2 */
81   if (etype <= IEEE_802_3_MAX_LEN) {
82
83   /* Is there an 802.2 layer? I can tell by looking at the first 2
84      bytes after the 802.3 header. If they are 0xffff, then what
85      follows the 802.3 header is an IPX payload, meaning no 802.2.
86      (IPX/SPX is they only thing that can be contained inside a
87      straight 802.3 packet). A non-0xffff value means that there's an
88      802.2 layer inside the 802.3 layer */
89     if (pd[14] == 0xff && pd[15] == 0xff) {
90       ethhdr_type = ETHERNET_802_3;
91     }
92     else {
93       ethhdr_type = ETHERNET_802_2;
94     }
95   } else {
96     ethhdr_type = ETHERNET_II;
97   }
98
99   switch (ethhdr_type) {
100     case ETHERNET_802_3:
101       capture_ipx(pd, offset, cap_len, ld);
102       break;
103     case ETHERNET_802_2:
104       capture_llc(pd, offset, cap_len, ld);
105       break;
106     case ETHERNET_II:
107       capture_ethertype(etype, offset, pd, cap_len, ld);
108       break;
109   }
110 }
111
112 void
113 dissect_eth(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
114   guint16    etype, length;
115   proto_tree *fh_tree = NULL;
116   proto_item *ti;
117   int        ethhdr_type;       /* the type of ethernet frame */
118   
119   if (fd->cap_len < ETH_HEADER_SIZE) {
120     dissect_data(pd, offset, fd, tree);
121     return;
122   }
123
124   SET_ADDRESS(&pi.dl_src, AT_ETHER, 6, &pd[offset+6]);
125   SET_ADDRESS(&pi.src, AT_ETHER, 6, &pd[offset+6]);
126   SET_ADDRESS(&pi.dl_dst, AT_ETHER, 6, &pd[offset+0]);
127   SET_ADDRESS(&pi.dst, AT_ETHER, 6, &pd[offset+0]);
128
129   if (check_col(fd, COL_PROTOCOL))
130     col_add_str(fd, COL_PROTOCOL, "Ethernet");
131
132   etype = pntohs(&pd[offset+12]);
133
134         /* either ethernet802.3 or ethernet802.2 */
135   if (etype <= IEEE_802_3_MAX_LEN) {
136     length = etype;
137
138     /* Is there an 802.2 layer? I can tell by looking at the first 2
139        bytes after the 802.3 header. If they are 0xffff, then what
140        follows the 802.3 header is an IPX payload, meaning no 802.2.
141        (IPX/SPX is they only thing that can be contained inside a
142        straight 802.3 packet). A non-0xffff value means that there's an
143        802.2 layer inside the 802.3 layer */
144     if (pd[offset+14] == 0xff && pd[offset+15] == 0xff) {
145       ethhdr_type = ETHERNET_802_3;
146     }
147     else {
148       ethhdr_type = ETHERNET_802_2;
149     }
150
151     if (check_col(fd, COL_INFO)) {
152       col_add_fstr(fd, COL_INFO, "IEEE 802.3 %s",
153                 (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));
154     }
155     if (tree) {
156
157         ti = proto_tree_add_item_format(tree, proto_eth, offset, ETH_HEADER_SIZE,
158                 NULL, "IEEE 802.3 %s", (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));
159
160         fh_tree = proto_item_add_subtree(ti, ett_ieee8023);
161
162         proto_tree_add_item(fh_tree, hf_eth_dst, offset+0, 6, &pd[offset+0]);
163         proto_tree_add_item(fh_tree, hf_eth_src, offset+6, 6, &pd[offset+6]);
164         proto_tree_add_item(fh_tree, hf_eth_len, offset+12, 2, length);
165
166         /* Convert the LLC length from the 802.3 header to a total
167            length, by adding in the Ethernet header size, and set
168            the payload and captured-payload lengths to the minima
169            of the total length and the frame lengths. */
170         length += ETH_HEADER_SIZE;
171         if (pi.len > length)
172           pi.len = length;
173         if (pi.captured_len > length)
174           pi.captured_len = length;
175     }
176
177   } else {
178     ethhdr_type = ETHERNET_II;
179     if (check_col(fd, COL_INFO))
180       col_add_str(fd, COL_INFO, "Ethernet II");
181     if (tree) {
182
183         ti = proto_tree_add_item_format(tree, proto_eth, offset, ETH_HEADER_SIZE,
184                 NULL, "Ethernet II");
185
186         fh_tree = proto_item_add_subtree(ti, ett_ether2);
187
188         proto_tree_add_item(fh_tree, hf_eth_dst, offset+0, 6, &pd[offset+0]);
189         proto_tree_add_item(fh_tree, hf_eth_src, offset+6, 6, &pd[offset+6]);
190     }
191   }
192   offset += ETH_HEADER_SIZE;
193
194   switch (ethhdr_type) {
195     case ETHERNET_802_3:
196       dissect_ipx(pd, offset, fd, tree);
197       break;
198     case ETHERNET_802_2:
199       dissect_llc(pd, offset, fd, tree);
200       break;
201     case ETHERNET_II:
202       ethertype(etype, offset, pd, fd, tree, fh_tree, hf_eth_type);
203       break;
204   }
205 }
206
207 void
208 proto_register_eth(void)
209 {
210         static hf_register_info hf[] = {
211
212                 { &hf_eth_dst,
213                 { "Destination",        "eth.dst", FT_ETHER, BASE_NONE, NULL, 0x0,
214                         "Destination Hardware Address" }},
215
216                 { &hf_eth_src,
217                 { "Source",             "eth.src", FT_ETHER, BASE_NONE, NULL, 0x0,
218                         "Source Hardware Address" }},
219
220                 { &hf_eth_len,
221                 { "Length",             "eth.len", FT_UINT16, BASE_DEC, NULL, 0x0,
222                         "" }},
223
224                 /* registered here but handled in ethertype.c */
225                 { &hf_eth_type,
226                 { "Type",               "eth.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
227                         "" }}
228         };
229         static gint *ett[] = {
230                 &ett_ieee8023,
231                 &ett_ether2,
232         };
233
234         proto_eth = proto_register_protocol ("Ethernet", "eth" );
235         proto_register_field_array(proto_eth, hf, array_length(hf));
236         proto_register_subtree_array(ett, array_length(ett));
237 }