2 * Routines for ethernet packet disassembly
4 * $Id: packet-eth.c,v 1.12 1999/07/15 15:32:40 gram Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
39 extern const value_string etype_vals[];
41 /* protocols and header fields */
44 int hf_eth_dst_vendor = -1;
46 int hf_eth_src_vendor = -1;
50 #define IEEE_802_3_MAX_LEN 1500
52 /* These are the Netware-ish names for the different Ethernet frame types.
53 EthernetII: The ethernet with a Type field instead of a length field
54 Ethernet802.2: An 802.3 header followed by an 802.3 header
55 Ethernet802.3: A raw 802.3 packet. IPX/SPX can be the only payload.
56 There's not 802.2 hdr in this.
57 EthernetSNAP: Basically 802.2, just with 802.2SNAP. For our purposes,
58 there's no difference between 802.2 and 802.2SNAP, since we just
59 pass it down to dissect_llc(). -- Gilbert
62 #define ETHERNET_802_2 1
63 #define ETHERNET_802_3 2
64 #define ETHERNET_SNAP 3
67 capture_eth(const u_char *pd, guint32 cap_len, packet_counts *ld) {
70 int ethhdr_type; /* the type of ethernet frame */
72 etype = (pd[12] << 8) | pd[13];
74 /* either ethernet802.3 or ethernet802.2 */
75 if (etype <= IEEE_802_3_MAX_LEN) {
77 /* Is there an 802.2 layer? I can tell by looking at the first 2
78 bytes after the 802.3 header. If they are 0xffff, then what
79 follows the 802.3 header is an IPX payload, meaning no 802.2.
80 (IPX/SPX is they only thing that can be contained inside a
81 straight 802.3 packet). A non-0xffff value means that there's an
82 802.2 layer inside the 802.3 layer */
83 if (pd[14] == 0xff && pd[15] == 0xff) {
84 ethhdr_type = ETHERNET_802_3;
87 ethhdr_type = ETHERNET_802_2;
90 ethhdr_type = ETHERNET_II;
93 switch (ethhdr_type) {
95 ld->other++; /* IPX */
98 capture_llc(pd, offset, cap_len, ld);
101 capture_ethertype(etype, offset, pd, cap_len, ld);
107 dissect_eth(const u_char *pd, frame_data *fd, proto_tree *tree) {
108 guint16 etype, length;
110 proto_tree *fh_tree = NULL;
112 int ethhdr_type; /* the type of ethernet frame */
114 if (check_col(fd, COL_RES_DL_DST))
115 col_add_str(fd, COL_RES_DL_DST, get_ether_name((u_char *)&pd[0]));
116 if (check_col(fd, COL_RES_DL_SRC))
117 col_add_str(fd, COL_RES_DL_SRC, get_ether_name((u_char *)&pd[6]));
118 if (check_col(fd, COL_UNRES_DL_DST))
119 col_add_str(fd, COL_UNRES_DL_DST, ether_to_str((u_char *)&pd[0]));
120 if (check_col(fd, COL_UNRES_DL_SRC))
121 col_add_str(fd, COL_UNRES_DL_SRC, ether_to_str((u_char *)&pd[6]));
122 if (check_col(fd, COL_PROTOCOL))
123 col_add_str(fd, COL_PROTOCOL, "N/A");
124 if (check_col(fd, COL_INFO))
125 col_add_str(fd, COL_INFO, "Ethernet II");
127 etype = (pd[12] << 8) | pd[13];
129 /* either ethernet802.3 or ethernet802.2 */
130 if (etype <= IEEE_802_3_MAX_LEN) {
133 /* Is there an 802.2 layer? I can tell by looking at the first 2
134 bytes after the 802.3 header. If they are 0xffff, then what
135 follows the 802.3 header is an IPX payload, meaning no 802.2.
136 (IPX/SPX is they only thing that can be contained inside a
137 straight 802.3 packet). A non-0xffff value means that there's an
138 802.2 layer inside the 802.3 layer */
139 if (pd[14] == 0xff && pd[15] == 0xff) {
140 ethhdr_type = ETHERNET_802_3;
143 ethhdr_type = ETHERNET_802_2;
146 if (check_col(fd, COL_INFO))
147 col_add_str(fd, COL_INFO, "802.3");
150 ti = proto_tree_add_item_format(tree, proto_eth, 0, offset,
151 NULL, "IEEE 802.3 %s", (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));
153 fh_tree = proto_item_add_subtree(ti, ETT_IEEE8023);
155 proto_tree_add_item(fh_tree, hf_eth_dst, 0, 6, &pd[0]);
156 proto_tree_add_item_hidden(fh_tree, hf_eth_dst_vendor, 0, 3, &pd[0]);
157 proto_tree_add_item(fh_tree, hf_eth_src, 6, 6, &pd[6]);
158 proto_tree_add_item_hidden(fh_tree, hf_eth_src_vendor, 6, 3, &pd[6]);
159 proto_tree_add_item(fh_tree, hf_eth_len, 12, 2, length);
163 ethhdr_type = ETHERNET_II;
166 ti = proto_tree_add_item_format(tree, proto_eth, 0, 14, NULL,
169 fh_tree = proto_item_add_subtree(ti, ETT_ETHER2);
171 proto_tree_add_item_format(fh_tree, hf_eth_dst, 0, 6, &pd[0],
172 "Destination: %s (%s)", ether_to_str((guint8 *) &pd[0]),
173 get_ether_name((u_char *) &pd[0]));
175 proto_tree_add_item_format(fh_tree, hf_eth_src, 6, 6, &pd[6],
176 "Source: %s (%s)", ether_to_str((guint8 *) &pd[6]),
177 get_ether_name((u_char *) &pd[6]));
182 switch (ethhdr_type) {
184 dissect_ipx(pd, offset, fd, tree);
187 dissect_llc(pd, offset, fd, tree);
190 ethertype(etype, offset, pd, fd, tree, fh_tree, hf_eth_type);
196 proto_register_eth(void)
198 static hf_register_info hf[] = {
201 { "Destination", "eth.dst", FT_ETHER, NULL }},
204 { "Source", "eth.src", FT_ETHER, NULL }},
206 { &hf_eth_dst_vendor,
207 { "Destination Hardware Vendor", "eth.dst_vendor", FT_ETHER, NULL }},
209 { &hf_eth_src_vendor,
210 { "Source Hardware Vendor", "eth.src_vendor", FT_ETHER, NULL }},
213 { "Length", "eth.len", FT_UINT16, NULL }},
215 /* registered here but handled in ethertype.c */
217 { "Type", "eth.type", FT_VALS_UINT16, VALS(etype_vals) }}
220 proto_eth = proto_register_protocol ("Ethernet", "eth" );
221 proto_register_field_array(proto_eth, hf, array_length(hf));