af24e3bb8873fdfa187b6be2c4a870bdd9bdec79
[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.12 1999/07/15 15:32:40 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 int proto_eth = -1;
43 int hf_eth_dst = -1;
44 int hf_eth_dst_vendor = -1;
45 int hf_eth_src = -1;
46 int hf_eth_src_vendor = -1;
47 int hf_eth_len = -1;
48 int hf_eth_type = -1;
49
50 #define IEEE_802_3_MAX_LEN 1500
51
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
60 */
61 #define ETHERNET_II     0
62 #define ETHERNET_802_2  1
63 #define ETHERNET_802_3  2
64 #define ETHERNET_SNAP   3
65
66 void
67 capture_eth(const u_char *pd, guint32 cap_len, packet_counts *ld) {
68   guint16 etype;
69   int        offset = 14;
70   int           ethhdr_type;    /* the type of ethernet frame */
71   
72   etype = (pd[12] << 8) | pd[13];
73
74         /* either ethernet802.3 or ethernet802.2 */
75   if (etype <= IEEE_802_3_MAX_LEN) {
76
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;
85     }
86     else {
87       ethhdr_type = ETHERNET_802_2;
88     }
89   } else {
90     ethhdr_type = ETHERNET_II;
91   }
92
93   switch (ethhdr_type) {
94     case ETHERNET_802_3:
95       ld->other++;      /* IPX */
96       break;
97     case ETHERNET_802_2:
98       capture_llc(pd, offset, cap_len, ld);
99       break;
100     case ETHERNET_II:
101       capture_ethertype(etype, offset, pd, cap_len, ld);
102       break;
103   }
104 }
105
106 void
107 dissect_eth(const u_char *pd, frame_data *fd, proto_tree *tree) {
108   guint16    etype, length;
109   int        offset = 14;
110   proto_tree *fh_tree = NULL;
111   proto_item *ti;
112   int           ethhdr_type;    /* the type of ethernet frame */
113
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");
126
127   etype = (pd[12] << 8) | pd[13];
128
129         /* either ethernet802.3 or ethernet802.2 */
130   if (etype <= IEEE_802_3_MAX_LEN) {
131     length = etype;
132
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;
141     }
142     else {
143       ethhdr_type = ETHERNET_802_2;
144     }
145
146     if (check_col(fd, COL_INFO))
147       col_add_str(fd, COL_INFO, "802.3");
148     if (tree) {
149
150         ti = proto_tree_add_item_format(tree, proto_eth, 0, offset,
151                 NULL, "IEEE 802.3 %s", (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));
152
153         fh_tree = proto_item_add_subtree(ti, ETT_IEEE8023);
154
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);
160     }
161
162   } else {
163     ethhdr_type = ETHERNET_II;
164     if (tree) {
165
166         ti = proto_tree_add_item_format(tree, proto_eth, 0, 14, NULL,
167                 "Ethernet II");
168
169         fh_tree = proto_item_add_subtree(ti, ETT_ETHER2);
170
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]));
174
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]));
178
179     }
180   }
181
182   switch (ethhdr_type) {
183     case ETHERNET_802_3:
184       dissect_ipx(pd, offset, fd, tree);
185       break;
186     case ETHERNET_802_2:
187       dissect_llc(pd, offset, fd, tree);
188       break;
189     case ETHERNET_II:
190       ethertype(etype, offset, pd, fd, tree, fh_tree, hf_eth_type);
191       break;
192   }
193 }
194
195 void
196 proto_register_eth(void)
197 {
198         static hf_register_info hf[] = {
199
200                 { &hf_eth_dst,
201                 { "Destination",        "eth.dst", FT_ETHER, NULL }},
202
203                 { &hf_eth_src,
204                 { "Source",             "eth.src", FT_ETHER, NULL }},
205
206                 { &hf_eth_dst_vendor,
207                 { "Destination Hardware Vendor", "eth.dst_vendor", FT_ETHER, NULL }},
208
209                 { &hf_eth_src_vendor,
210                 { "Source Hardware Vendor", "eth.src_vendor", FT_ETHER, NULL }},
211
212                 { &hf_eth_len,
213                 { "Length",             "eth.len", FT_UINT16, NULL }},
214
215                 /* registered here but handled in ethertype.c */
216                 { &hf_eth_type,
217                 { "Type",               "eth.type", FT_VALS_UINT16, VALS(etype_vals) }}
218         };
219
220         proto_eth = proto_register_protocol ("Ethernet", "eth" );
221         proto_register_field_array(proto_eth, hf, array_length(hf));
222 }