bgp route refresh/MP capability option.
[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.50 2000/11/29 05:16:15 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 #include "packet-eth.h"
39 #include "packet-ipx.h"
40 #include "packet-isl.h"
41 #include "packet-llc.h"
42
43 extern const value_string etype_vals[];
44
45 /* protocols and header fields */
46 static int proto_eth = -1;
47 static int hf_eth_dst = -1;
48 static int hf_eth_src = -1;
49 static int hf_eth_len = -1;
50 static int hf_eth_type = -1;
51 static int hf_eth_addr = -1;
52 static int hf_eth_trailer = -1;
53
54 static gint ett_ieee8023 = -1;
55 static gint ett_ether2 = -1;
56
57 #define ETH_HEADER_SIZE 14
58
59 /* These are the Netware-ish names for the different Ethernet frame types.
60         EthernetII: The ethernet with a Type field instead of a length field
61         Ethernet802.2: An 802.3 header followed by an 802.2 header
62         Ethernet802.3: A raw 802.3 packet. IPX/SPX can be the only payload.
63                         There's not 802.2 hdr in this.
64         EthernetSNAP: Basically 802.2, just with 802.2SNAP. For our purposes,
65                 there's no difference between 802.2 and 802.2SNAP, since we just
66                 pass it down to dissect_llc(). -- Gilbert
67 */
68 #define ETHERNET_II     0
69 #define ETHERNET_802_2  1
70 #define ETHERNET_802_3  2
71 #define ETHERNET_SNAP   3
72
73 void
74 capture_eth(const u_char *pd, int offset, packet_counts *ld)
75 {
76   guint16    etype, length;
77   int     ethhdr_type;  /* the type of ethernet frame */
78
79   if (!BYTES_ARE_IN_FRAME(offset, ETH_HEADER_SIZE)) {
80     ld->other++;
81     return;
82   }
83   
84   etype = pntohs(&pd[offset+12]);
85
86         /* either ethernet802.3 or ethernet802.2 */
87   if (etype <= IEEE_802_3_MAX_LEN) {
88     length = etype;
89
90     /* Is there an 802.2 layer? I can tell by looking at the first 2
91        bytes after the 802.3 header. If they are 0xffff, then what
92        follows the 802.3 header is an IPX payload, meaning no 802.2.
93        (IPX/SPX is they only thing that can be contained inside a
94        straight 802.3 packet). A non-0xffff value means that there's an
95        802.2 layer inside the 802.3 layer */
96     if (pd[offset+14] == 0xff && pd[offset+15] == 0xff) {
97       ethhdr_type = ETHERNET_802_3;
98     }
99     else {
100       ethhdr_type = ETHERNET_802_2;
101     }
102
103     /* Oh, yuck.  Cisco ISL frames require special interpretation of the
104        destination address field; fortunately, they can be recognized by
105        checking the first 5 octets of the destination address, which are
106        01-00-0C-00-00 for ISL frames. */
107     if (pd[offset] == 0x01 && pd[offset+1] == 0x00 && pd[offset+2] == 0x0C
108         && pd[offset+3] == 0x00 && pd[offset+4] == 0x00) {
109       capture_isl(pd, offset, ld);
110       return;
111     }
112
113     /* Convert the LLC length from the 802.3 header to a total
114        frame length, by adding in the size of any data that preceded
115        the Ethernet header, and adding in the Ethernet header size,
116        and set the payload and captured-payload lengths to the minima
117        of the total length and the frame lengths. */
118     length += offset + ETH_HEADER_SIZE;
119     if (pi.len > length)
120       pi.len = length;
121     if (pi.captured_len > length)
122       pi.captured_len = length;
123   } else {
124     ethhdr_type = ETHERNET_II;
125   }
126   offset += ETH_HEADER_SIZE;
127
128   switch (ethhdr_type) {
129     case ETHERNET_802_3:
130       capture_ipx(pd, offset, ld);
131       break;
132     case ETHERNET_802_2:
133       capture_llc(pd, offset, ld);
134       break;
135     case ETHERNET_II:
136       capture_ethertype(etype, offset, pd, ld);
137       break;
138   }
139 }
140
141 void
142 dissect_eth(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
143 {
144   int                   orig_captured_len;
145   proto_item            *ti;
146   guint8                *dst, *src;
147   const guint8          *pd;
148
149   volatile guint16      etype;
150   volatile int          ethhdr_type;    /* the type of Ethernet frame */
151   volatile int          eth_offset;
152   volatile guint16      length;
153   tvbuff_t              *volatile next_tvb;
154   tvbuff_t              *volatile trailer_tvb;
155   proto_tree            *volatile fh_tree;
156   guint                 length_before;
157
158   CHECK_DISPLAY_AS_DATA(proto_eth, tvb, pinfo, tree);
159
160   tvb_compat(tvb, &pd, (int*)&eth_offset);
161
162   pinfo->current_proto = "Ethernet";
163   orig_captured_len = pinfo->captured_len;
164
165   if (check_col(pinfo->fd, COL_PROTOCOL))
166     col_set_str(pinfo->fd, COL_PROTOCOL, "Ethernet");
167
168   src = tvb_get_ptr(tvb, 6, 6);
169   dst = tvb_get_ptr(tvb, 0, 6);
170   SET_ADDRESS(&pinfo->dl_src,   AT_ETHER, 6, src);
171   SET_ADDRESS(&pinfo->src,      AT_ETHER, 6, src);
172   SET_ADDRESS(&pinfo->dl_dst,   AT_ETHER, 6, dst);
173   SET_ADDRESS(&pinfo->dst,      AT_ETHER, 6, dst);
174
175   etype = tvb_get_ntohs(tvb, 12);
176
177         /* either ethernet802.3 or ethernet802.2 */
178   if (etype <= IEEE_802_3_MAX_LEN) {
179     length = etype;
180
181     /* Is there an 802.2 layer? I can tell by looking at the first 2
182        bytes after the 802.3 header. If they are 0xffff, then what
183        follows the 802.3 header is an IPX payload, meaning no 802.2.
184        (IPX/SPX is they only thing that can be contained inside a
185        straight 802.3 packet). A non-0xffff value means that there's an
186        802.2 layer inside the 802.3 layer */
187     ethhdr_type = ETHERNET_802_2;
188     TRY {
189             if (tvb_get_ntohs(tvb, 14) == 0xffff) {
190               ethhdr_type = ETHERNET_802_3;
191             }
192     }
193     CATCH2(BoundsError, ReportedBoundsError) {
194             ; /* do nothing */
195
196     }
197     ENDTRY;
198
199     /* Oh, yuck.  Cisco ISL frames require special interpretation of the
200        destination address field; fortunately, they can be recognized by
201        checking the first 5 octets of the destination address, which are
202        01-00-0C-00-00 for ISL frames. */
203     if (        tvb_get_guint8(tvb, 0) == 0x01 &&
204                 tvb_get_guint8(tvb, 1) == 0x00 &&
205                 tvb_get_guint8(tvb, 2) == 0x0C &&
206                 tvb_get_guint8(tvb, 3) == 0x00 &&
207                 tvb_get_guint8(tvb, 4) == 0x00 ) {
208       dissect_isl(pd, eth_offset, pinfo->fd, tree);
209       return;
210     }
211
212     if (check_col(pinfo->fd, COL_INFO)) {
213       col_add_fstr(pinfo->fd, COL_INFO, "IEEE 802.3 %s",
214                 (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));
215     }
216     if (tree) {
217
218         ti = proto_tree_add_protocol_format(tree, proto_eth, tvb, 0, ETH_HEADER_SIZE,
219                 "IEEE 802.3 %s", (ethhdr_type == ETHERNET_802_3 ? "Raw " : ""));
220
221         fh_tree = proto_item_add_subtree(ti, ett_ieee8023);
222
223         proto_tree_add_ether(fh_tree, hf_eth_dst, tvb, 0, 6, dst);
224         proto_tree_add_ether(fh_tree, hf_eth_src, tvb, 6, 6, src);
225
226 /* add items for eth.addr filter */
227         proto_tree_add_ether_hidden(fh_tree, hf_eth_addr, tvb, 0, 6, dst);
228         proto_tree_add_ether_hidden(fh_tree, hf_eth_addr, tvb, 6, 6, src);
229
230         proto_tree_add_uint(fh_tree, hf_eth_len, tvb, 12, 2, length);
231     }
232
233     /* Convert the LLC length from the 802.3 header to a total
234        frame length, by adding in the size of any data that preceded
235        the Ethernet header, and adding in the Ethernet header size,
236        and set the payload and captured-payload lengths to the minima
237        of the total length and the frame lengths. */
238     length += eth_offset + ETH_HEADER_SIZE;
239     if (pinfo->len > length)
240       pinfo->len = length;
241     if (pinfo->captured_len > length)
242       pinfo->captured_len = length;
243   } else {
244     ethhdr_type = ETHERNET_II;
245     if (check_col(pinfo->fd, COL_INFO))
246       col_set_str(pinfo->fd, COL_INFO, "Ethernet II");
247     if (tree) {
248
249         ti = proto_tree_add_protocol_format(tree, proto_eth, tvb, 0, ETH_HEADER_SIZE,
250                 "Ethernet II");
251
252         fh_tree = proto_item_add_subtree(ti, ett_ether2);
253
254         proto_tree_add_ether(fh_tree, hf_eth_dst, tvb, 0, 6, dst);
255         proto_tree_add_ether(fh_tree, hf_eth_src, tvb, 6, 6, src);
256 /* add items for eth.addr filter */
257         proto_tree_add_ether_hidden(fh_tree, hf_eth_addr, tvb, 0, 6, dst);
258         proto_tree_add_ether_hidden(fh_tree, hf_eth_addr, tvb, 6, 6, src);
259     }
260   }
261   eth_offset += ETH_HEADER_SIZE;
262
263   if (etype <= IEEE_802_3_MAX_LEN) {
264           /* Give the next dissector only 'length' number of bytes */
265           TRY {
266             next_tvb = tvb_new_subset(tvb, ETH_HEADER_SIZE, etype, etype);
267             trailer_tvb = tvb_new_subset(tvb, ETH_HEADER_SIZE + etype, -1, -1);
268           }
269           CATCH2(BoundsError, ReportedBoundsError) {
270             /* Either:
271
272                  the packet doesn't have "etype" bytes worth of
273                  captured data left in it - or it may not even have
274                  "etype" bytes worth of data in it, period -
275                  so the "tvb_new_subset()" creating "next_tvb"
276                  threw an exception
277
278                or
279
280                  the packet has exactly "etype" bytes worth of
281                  captured data left in it, so the "tvb_new_subset()"
282                  creating "trailer_tvb" threw an exception.
283
284               In either case, this means that all the data in the frame
285               is within the length value, so we give all the data to the
286               next protocol and have no trailer. */
287             next_tvb = tvb_new_subset(tvb, ETH_HEADER_SIZE, -1, etype);
288             trailer_tvb = NULL;
289           }
290           ENDTRY;
291   }
292   else {
293      next_tvb = NULL;   /* "ethertype()" will create the next tvb for us */
294      trailer_tvb = NULL;        /* we don't know how big the trailer is */
295   }
296
297   switch (ethhdr_type) {
298     case ETHERNET_802_3:
299       dissect_ipx(next_tvb, pinfo, tree);
300       break;
301     case ETHERNET_802_2:
302       dissect_llc(next_tvb, pinfo, tree);
303       break;
304     case ETHERNET_II:
305       length_before = tvb_reported_length(tvb);
306       length = ethertype(etype, tvb, ETH_HEADER_SIZE, pinfo, tree, fh_tree,
307           hf_eth_type) + ETH_HEADER_SIZE;
308       if (length < length_before) {
309         /*
310          * Create a tvbuff for the padding.
311          */
312         TRY {
313           trailer_tvb = tvb_new_subset(tvb, length, -1, -1);
314         }
315         CATCH2(BoundsError, ReportedBoundsError) {
316           /* The packet doesn't have "length" bytes worth of captured
317              data left in it.  No trailer to display. */
318           trailer_tvb = NULL;
319         }
320         ENDTRY;
321       }
322       break;
323   }
324
325   /* If there's some bytes left over, mark them. */
326   if (trailer_tvb && tree) {
327           guint           trailer_length;
328
329           trailer_length = tvb_length(trailer_tvb);
330           if (trailer_length != 0) {
331                   proto_tree_add_item(fh_tree, hf_eth_trailer, trailer_tvb, 0,
332                           trailer_length, FALSE);
333           }
334   }
335
336 }
337
338 void
339 proto_register_eth(void)
340 {
341         static hf_register_info hf[] = {
342
343                 { &hf_eth_dst,
344                 { "Destination",        "eth.dst", FT_ETHER, BASE_NONE, NULL, 0x0,
345                         "Destination Hardware Address" }},
346
347                 { &hf_eth_src,
348                 { "Source",             "eth.src", FT_ETHER, BASE_NONE, NULL, 0x0,
349                         "Source Hardware Address" }},
350
351                 { &hf_eth_len,
352                 { "Length",             "eth.len", FT_UINT16, BASE_DEC, NULL, 0x0,
353                         "" }},
354
355                 /* registered here but handled in ethertype.c */
356                 { &hf_eth_type,
357                 { "Type",               "eth.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
358                         "" }},
359                 { &hf_eth_addr,
360                 { "Source or Destination Address", "eth.addr", FT_ETHER, BASE_NONE, NULL, 0x0,
361                         "Source or Destination Hardware Address" }},
362
363                 { &hf_eth_trailer,
364                 { "Trailer", "eth.trailer", FT_BYTES, BASE_NONE, NULL, 0x0,
365                         "Ethernet Trailer or Checksum" }},
366
367         };
368         static gint *ett[] = {
369                 &ett_ieee8023,
370                 &ett_ether2,
371         };
372
373         proto_eth = proto_register_protocol ("Ethernet", "eth" );
374         proto_register_field_array(proto_eth, hf, array_length(hf));
375         proto_register_subtree_array(ett, array_length(ett));
376
377         register_dissector("eth", dissect_eth);
378 }
379
380 void
381 proto_reg_handoff_eth(void)
382 {
383         dissector_add("wtap_encap", WTAP_ENCAP_ETHERNET, dissect_eth);
384 }