2 * Routines for ethernet packet disassembly
4 * $Id: packet-eth.c,v 1.18 1999/08/24 06:10:05 guy 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 */
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;
48 #define ETH_HEADER_SIZE 14
50 /* These are the Netware-ish names for the different Ethernet frame types.
51 EthernetII: The ethernet with a Type field instead of a length field
52 Ethernet802.2: An 802.3 header followed by an 802.3 header
53 Ethernet802.3: A raw 802.3 packet. IPX/SPX can be the only payload.
54 There's not 802.2 hdr in this.
55 EthernetSNAP: Basically 802.2, just with 802.2SNAP. For our purposes,
56 there's no difference between 802.2 and 802.2SNAP, since we just
57 pass it down to dissect_llc(). -- Gilbert
60 #define ETHERNET_802_2 1
61 #define ETHERNET_802_3 2
62 #define ETHERNET_SNAP 3
65 capture_eth(const u_char *pd, guint32 cap_len, packet_counts *ld) {
67 int offset = ETH_HEADER_SIZE;
68 int ethhdr_type; /* the type of ethernet frame */
70 etype = (pd[12] << 8) | pd[13];
72 /* either ethernet802.3 or ethernet802.2 */
73 if (etype <= IEEE_802_3_MAX_LEN) {
75 /* Is there an 802.2 layer? I can tell by looking at the first 2
76 bytes after the 802.3 header. If they are 0xffff, then what
77 follows the 802.3 header is an IPX payload, meaning no 802.2.
78 (IPX/SPX is they only thing that can be contained inside a
79 straight 802.3 packet). A non-0xffff value means that there's an
80 802.2 layer inside the 802.3 layer */
81 if (pd[14] == 0xff && pd[15] == 0xff) {
82 ethhdr_type = ETHERNET_802_3;
85 ethhdr_type = ETHERNET_802_2;
88 ethhdr_type = ETHERNET_II;
91 switch (ethhdr_type) {
93 ld->other++; /* IPX */
96 capture_llc(pd, offset, cap_len, ld);
99 capture_ethertype(etype, offset, pd, cap_len, ld);
105 dissect_eth(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
106 guint16 etype, length;
107 proto_tree *fh_tree = NULL;
109 int ethhdr_type; /* the type of ethernet frame */
111 if (check_col(fd, COL_RES_DL_DST))
112 col_add_str(fd, COL_RES_DL_DST, get_ether_name((u_char *)&pd[offset+0]));
113 if (check_col(fd, COL_RES_DL_SRC))
114 col_add_str(fd, COL_RES_DL_SRC, get_ether_name((u_char *)&pd[offset+6]));
115 if (check_col(fd, COL_UNRES_DL_DST))
116 col_add_str(fd, COL_UNRES_DL_DST, ether_to_str((u_char *)&pd[offset+0]));
117 if (check_col(fd, COL_UNRES_DL_SRC))
118 col_add_str(fd, COL_UNRES_DL_SRC, ether_to_str((u_char *)&pd[offset+6]));
119 if (check_col(fd, COL_PROTOCOL))
120 col_add_str(fd, COL_PROTOCOL, "Ethernet");
122 etype = (pd[offset+12] << 8) | pd[offset+13];
124 /* either ethernet802.3 or ethernet802.2 */
125 if (etype <= IEEE_802_3_MAX_LEN) {
128 /* Is there an 802.2 layer? I can tell by looking at the first 2
129 bytes after the 802.3 header. If they are 0xffff, then what
130 follows the 802.3 header is an IPX payload, meaning no 802.2.
131 (IPX/SPX is they only thing that can be contained inside a
132 straight 802.3 packet). A non-0xffff value means that there's an
133 802.2 layer inside the 802.3 layer */
134 if (pd[offset+14] == 0xff && pd[offset+15] == 0xff) {
135 ethhdr_type = ETHERNET_802_3;
138 ethhdr_type = ETHERNET_802_2;
141 if (check_col(fd, COL_INFO)) {
142 col_add_fstr(fd, COL_INFO, "IEEE 802.3 %s",
143 (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));
147 ti = proto_tree_add_item_format(tree, proto_eth, 0, ETH_HEADER_SIZE,
148 NULL, "IEEE 802.3 %s", (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));
150 fh_tree = proto_item_add_subtree(ti, ETT_IEEE8023);
152 proto_tree_add_item(fh_tree, hf_eth_dst, 0, 6, &pd[offset+0]);
153 proto_tree_add_item(fh_tree, hf_eth_src, 6, 6, &pd[offset+6]);
154 proto_tree_add_item(fh_tree, hf_eth_len, 12, 2, length);
156 /* Convert the LLC length from the 802.3 header to a total
157 length, by adding in the Ethernet header size, and set
158 the payload and captured-payload lengths to the minima
159 of the total length and the frame lengths. */
160 length += ETH_HEADER_SIZE;
163 if (pi.captured_len > length)
164 pi.captured_len = length;
168 ethhdr_type = ETHERNET_II;
169 if (check_col(fd, COL_INFO))
170 col_add_str(fd, COL_INFO, "Ethernet II");
173 ti = proto_tree_add_item_format(tree, proto_eth, 0, ETH_HEADER_SIZE,
174 NULL, "Ethernet II");
176 fh_tree = proto_item_add_subtree(ti, ETT_ETHER2);
178 proto_tree_add_item_format(fh_tree, hf_eth_dst, 0, 6, &pd[offset+0],
179 "Destination: %s (%s)", ether_to_str((guint8 *) &pd[offset+0]),
180 get_ether_name((u_char *) &pd[offset+0]));
182 proto_tree_add_item_format(fh_tree, hf_eth_src, 6, 6, &pd[offset+6],
183 "Source: %s (%s)", ether_to_str((guint8 *) &pd[offset+6]),
184 get_ether_name((u_char *) &pd[offset+6]));
187 offset += ETH_HEADER_SIZE;
189 switch (ethhdr_type) {
191 dissect_ipx(pd, offset, fd, tree);
194 dissect_llc(pd, offset, fd, tree);
197 ethertype(etype, offset, pd, fd, tree, fh_tree, hf_eth_type);
203 proto_register_eth(void)
205 static hf_register_info hf[] = {
208 { "Destination", "eth.dst", FT_ETHER, NULL }},
211 { "Source", "eth.src", FT_ETHER, NULL }},
214 { "Length", "eth.len", FT_UINT16, NULL }},
216 /* registered here but handled in ethertype.c */
218 { "Type", "eth.type", FT_VALS_UINT16, VALS(etype_vals) }}
221 proto_eth = proto_register_protocol ("Ethernet", "eth" );
222 proto_register_field_array(proto_eth, hf, array_length(hf));