Update from Kojak to dissect ICQ login packets and text messages.
[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.21 1999/10/22 07:17:31 guy 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 #define ETH_HEADER_SIZE 14
49
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
58 */
59 #define ETHERNET_II     0
60 #define ETHERNET_802_2  1
61 #define ETHERNET_802_3  2
62 #define ETHERNET_SNAP   3
63
64 void
65 capture_eth(const u_char *pd, guint32 cap_len, packet_counts *ld) {
66   guint16 etype;
67   int     offset = ETH_HEADER_SIZE;
68   int     ethhdr_type;  /* the type of ethernet frame */
69
70   if (cap_len < ETH_HEADER_SIZE) {
71     ld->other++;
72     return;
73   }
74   
75   etype = (pd[12] << 8) | pd[13];
76
77         /* either ethernet802.3 or ethernet802.2 */
78   if (etype <= IEEE_802_3_MAX_LEN) {
79
80   /* Is there an 802.2 layer? I can tell by looking at the first 2
81      bytes after the 802.3 header. If they are 0xffff, then what
82      follows the 802.3 header is an IPX payload, meaning no 802.2.
83      (IPX/SPX is they only thing that can be contained inside a
84      straight 802.3 packet). A non-0xffff value means that there's an
85      802.2 layer inside the 802.3 layer */
86     if (pd[14] == 0xff && pd[15] == 0xff) {
87       ethhdr_type = ETHERNET_802_3;
88     }
89     else {
90       ethhdr_type = ETHERNET_802_2;
91     }
92   } else {
93     ethhdr_type = ETHERNET_II;
94   }
95
96   switch (ethhdr_type) {
97     case ETHERNET_802_3:
98       ld->other++;      /* IPX */
99       break;
100     case ETHERNET_802_2:
101       capture_llc(pd, offset, cap_len, ld);
102       break;
103     case ETHERNET_II:
104       capture_ethertype(etype, offset, pd, cap_len, ld);
105       break;
106   }
107 }
108
109 void
110 dissect_eth(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
111   guint16    etype, length;
112   proto_tree *fh_tree = NULL;
113   proto_item *ti;
114   int        ethhdr_type;       /* the type of ethernet frame */
115   
116   if (fd->cap_len < ETH_HEADER_SIZE) {
117     dissect_data(pd, offset, fd, tree);
118     return;
119   }
120
121   SET_ADDRESS(&pi.dl_src, AT_ETHER, 6, &pd[offset+6]);
122   SET_ADDRESS(&pi.src, AT_ETHER, 6, &pd[offset+6]);
123   SET_ADDRESS(&pi.dl_dst, AT_ETHER, 6, &pd[offset+0]);
124   SET_ADDRESS(&pi.dst, AT_ETHER, 6, &pd[offset+0]);
125
126   if (check_col(fd, COL_PROTOCOL))
127     col_add_str(fd, COL_PROTOCOL, "Ethernet");
128
129   etype = (pd[offset+12] << 8) | pd[offset+13];
130
131         /* either ethernet802.3 or ethernet802.2 */
132   if (etype <= IEEE_802_3_MAX_LEN) {
133     length = etype;
134
135     /* Is there an 802.2 layer? I can tell by looking at the first 2
136        bytes after the 802.3 header. If they are 0xffff, then what
137        follows the 802.3 header is an IPX payload, meaning no 802.2.
138        (IPX/SPX is they only thing that can be contained inside a
139        straight 802.3 packet). A non-0xffff value means that there's an
140        802.2 layer inside the 802.3 layer */
141     if (pd[offset+14] == 0xff && pd[offset+15] == 0xff) {
142       ethhdr_type = ETHERNET_802_3;
143     }
144     else {
145       ethhdr_type = ETHERNET_802_2;
146     }
147
148     if (check_col(fd, COL_INFO)) {
149       col_add_fstr(fd, COL_INFO, "IEEE 802.3 %s",
150                 (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));
151     }
152     if (tree) {
153
154         ti = proto_tree_add_item_format(tree, proto_eth, 0, ETH_HEADER_SIZE,
155                 NULL, "IEEE 802.3 %s", (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));
156
157         fh_tree = proto_item_add_subtree(ti, ETT_IEEE8023);
158
159         proto_tree_add_item(fh_tree, hf_eth_dst, 0, 6, &pd[offset+0]);
160         proto_tree_add_item(fh_tree, hf_eth_src, 6, 6, &pd[offset+6]);
161         proto_tree_add_item(fh_tree, hf_eth_len, 12, 2, length);
162
163         /* Convert the LLC length from the 802.3 header to a total
164            length, by adding in the Ethernet header size, and set
165            the payload and captured-payload lengths to the minima
166            of the total length and the frame lengths. */
167         length += ETH_HEADER_SIZE;
168         if (pi.len > length)
169           pi.len = length;
170         if (pi.captured_len > length)
171           pi.captured_len = length;
172     }
173
174   } else {
175     ethhdr_type = ETHERNET_II;
176     if (check_col(fd, COL_INFO))
177       col_add_str(fd, COL_INFO, "Ethernet II");
178     if (tree) {
179
180         ti = proto_tree_add_item_format(tree, proto_eth, 0, ETH_HEADER_SIZE,
181                 NULL, "Ethernet II");
182
183         fh_tree = proto_item_add_subtree(ti, ETT_ETHER2);
184
185         proto_tree_add_item_format(fh_tree, hf_eth_dst, 0, 6, &pd[offset+0],
186                 "Destination: %s (%s)", ether_to_str((guint8 *) &pd[offset+0]),
187                 get_ether_name((u_char *) &pd[offset+0]));
188
189         proto_tree_add_item_format(fh_tree, hf_eth_src, 6, 6, &pd[offset+6],
190                 "Source: %s (%s)", ether_to_str((guint8 *) &pd[offset+6]),
191                 get_ether_name((u_char *) &pd[offset+6]));
192     }
193   }
194   offset += ETH_HEADER_SIZE;
195
196   switch (ethhdr_type) {
197     case ETHERNET_802_3:
198       dissect_ipx(pd, offset, fd, tree);
199       break;
200     case ETHERNET_802_2:
201       dissect_llc(pd, offset, fd, tree);
202       break;
203     case ETHERNET_II:
204       ethertype(etype, offset, pd, fd, tree, fh_tree, hf_eth_type);
205       break;
206   }
207 }
208
209 void
210 proto_register_eth(void)
211 {
212         static hf_register_info hf[] = {
213
214                 { &hf_eth_dst,
215                 { "Destination",        "eth.dst", FT_ETHER, BASE_NONE, NULL, 0x0,
216                         "Destination Hardware Address" }},
217
218                 { &hf_eth_src,
219                 { "Source",             "eth.src", FT_ETHER, BASE_NONE, NULL, 0x0,
220                         "Source Hardware Address" }},
221
222                 { &hf_eth_len,
223                 { "Length",             "eth.len", FT_UINT16, BASE_DEC, NULL, 0x0,
224                         "" }},
225
226                 /* registered here but handled in ethertype.c */
227                 { &hf_eth_type,
228                 { "Type",               "eth.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
229                         "" }}
230         };
231
232         proto_eth = proto_register_protocol ("Ethernet", "eth" );
233         proto_register_field_array(proto_eth, hf, array_length(hf));
234 }