From Benjamin Roch:
[obnox/wireshark/wip.git] / epan / dissectors / packet-ethertype.c
1 /* packet-ethertype.c
2  * Routines for calling the right protocol for the ethertype.
3  *
4  * $Id$
5  *
6  * Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include "packet-eth.h"
34 #include "packet-frame.h"
35 #include "packet-ip.h"
36 #include "packet-ipv6.h"
37 #include "packet-ipx.h"
38 #include "packet-vlan.h"
39 #include "packet-ieee8021ah.h"
40 #include "packet-vines.h"
41 #include <epan/etypes.h>
42 #include <epan/ppptypes.h>
43
44 static dissector_table_t ethertype_dissector_table;
45
46 static dissector_handle_t data_handle;
47
48 const value_string etype_vals[] = {
49     {ETHERTYPE_IP,                                              "IP"                            },
50     {ETHERTYPE_IPv6,                                    "IPv6"                          },
51     {ETHERTYPE_WLCCP,                                   "Cisco Wireless Lan Context Control Protocol"           },
52     {ETHERTYPE_CENTRINO_PROMISC,                "IEEE 802.11 (Centrino promiscuous)" },
53     {ETHERTYPE_XNS_IDP,                                 "XNS Internet Datagram Protocol" },
54     {ETHERTYPE_X25L3,                                   "X.25 Layer 3"                  },
55     {ETHERTYPE_ARP,                                             "ARP"                           },
56     {ETHERTYPE_WOL,                                             "Wake on LAN"                   },
57     {ETHERTYPE_WMX_M2M,                                 "WiMax Mac-to-Mac"              },
58     {ETHERTYPE_EPL_V1,                                  "EPL_V1"                                },
59     {ETHERTYPE_REVARP,                                  "RARP"                          },
60     {ETHERTYPE_DEC_LB,                                  "DEC LanBridge"                 },
61     {ETHERTYPE_ATALK,                                   "Appletalk"                     },
62     {ETHERTYPE_SNA,                                             "SNA-over-Ethernet"             },
63     {ETHERTYPE_AARP,                                    "AARP"                          },
64     {ETHERTYPE_IPX,                                             "Netware IPX/SPX"               },
65     {ETHERTYPE_VINES_IP,                                "Vines IP"                      },
66     {ETHERTYPE_VINES_ECHO,                              "Vines Echo"                    },
67     {ETHERTYPE_TRAIN,                                   "Netmon Train"                  },
68     {ETHERTYPE_LOOP,                                    "Loopback"                      }, /* Ethernet Loopback */
69     {ETHERTYPE_FOUNDRY,                                 "Foundry proprietary"           },
70     {ETHERTYPE_WCP,                                             "Wellfleet Compression Protocol" },
71     {ETHERTYPE_STP,                                             "Spanning Tree Protocol"        },
72     {ETHERTYPE_ISMP,                                    "Cabletron Interswitch Message Protocol" },
73     {ETHERTYPE_ISMP_TBFLOOD,                    "Cabletron SFVLAN 1.8 Tag-Based Flood" },
74                                 /* for ISMP, see RFC 2641, RFC 2642, RFC 2643 */
75     {ETHERTYPE_PPPOED,                                  "PPPoE Discovery"               },
76     {ETHERTYPE_PPPOES,                                  "PPPoE Session"                 },
77     {ETHERTYPE_INTEL_ANS,                               "Intel ANS probe"               },
78     {ETHERTYPE_MS_NLB_HEARTBEAT,                "MS NLB heartbeat"      },
79     {ETHERTYPE_HOMEPLUG,                                "Homeplug"                      },
80     {ETHERTYPE_VLAN,                                    "802.1Q Virtual LAN"            },
81     {ETHERTYPE_IEEE_802_1AD,                    "802.1ad Provider Bridge (Q-in-Q)"},
82     {ETHERTYPE_IEEE_802_1AH,                    "802.1ah Provider Backbone Bridge (mac-in-mac)"},
83     {ETHERTYPE_EAPOL,                                   "802.1X Authentication"         },
84     {ETHERTYPE_RSN_PREAUTH,                             "802.11i Pre-Authentication"    },
85     {ETHERTYPE_MPLS,                                    "MPLS label switched packet"    },
86     {ETHERTYPE_MPLS_MULTI,                              "MPLS multicast label switched packet" },
87     {ETHERTYPE_3C_NBP_DGRAM,                    "3Com NBP Datagram"             },
88     {ETHERTYPE_DEC,                                             "DEC proto"                     },
89     {ETHERTYPE_DNA_DL,                                  "DEC DNA Dump/Load"             },
90     {ETHERTYPE_DNA_RC,                                  "DEC DNA Remote Console"        },
91     {ETHERTYPE_DNA_RT,                                  "DEC DNA Routing"               },
92     {ETHERTYPE_LAT,                                             "DEC LAT"                       },
93     {ETHERTYPE_DEC_DIAG,                                "DEC Diagnostics"               },
94     {ETHERTYPE_DEC_CUST,                                "DEC Customer use"              },
95     {ETHERTYPE_DEC_SCA,                                 "DEC LAVC/SCA"                  },
96     {ETHERTYPE_DEC_LAST,                                "DEC LAST"                      },
97     {ETHERTYPE_ETHBRIDGE,                               "Transparent Ethernet bridging" },
98     {ETHERTYPE_CGMP,                                    "Cisco Group Management Protocol" },
99     {ETHERTYPE_MAC_CONTROL,                             "MAC Control"                   },
100     {ETHERTYPE_SLOW_PROTOCOLS,                  "Slow Protocols"                },
101     {ETHERTYPE_RTMAC,                                   "Real-Time Media Access Control" },
102     {ETHERTYPE_RTCFG,                                   "Real-Time Configuration Protocol" },
103     {ETHERTYPE_CDMA2000_A10_UBS,                "CDMA2000 A10 Unstructured byte stream" },
104     {ETHERTYPE_PROFINET,                                "PROFINET"                      },
105     {ETHERTYPE_AOE,                                             "ATA over Ethernet"             },
106     {ETHERTYPE_ECATF,                                   "EtherCAT frame" },
107     {ETHERTYPE_TELKONET,                                "Telkonet powerline"    },
108     {ETHERTYPE_EPL_V2,                                  "ETHERNET Powerlink v2"                 },
109     {ETHERTYPE_CSM_ENCAPS,                              "CSM_ENCAPS Protocol"             },
110     {ETHERTYPE_IEEE802_OUI_EXTENDED,    "IEEE 802a OUI Extended Ethertype" },
111     {ETHERTYPE_IEC61850_GOOSE,                  "IEC 61850/GOOSE" },
112     {ETHERTYPE_IEC61850_GSE,                    "IEC 61850/GSE management services" },
113     {ETHERTYPE_IEC61850_SV,                             "IEC 61850/SV (Sampled Value Transmission" },
114     {ETHERTYPE_TIPC,                                    "Transparent Inter Process Communication" },
115     {ETHERTYPE_LLDP,                                    "802.1 Link Layer Discovery Protocol (LLDP)" },
116     {ETHERTYPE_3GPP2,                                   "CDMA2000 A10 3GPP2 Packet" },
117     {ETHERTYPE_TTE_PCF,                                 "TTEthernet Protocol Control Frame" },
118     {ETHERTYPE_LLTD,                                    "Link Layer Topology Discovery (LLTD)" },
119     {ETHERTYPE_WSMP,                                    "(WAVE) Short Message Protocol (WSM)" },
120     {ETHERTYPE_COBRANET,                                "Cirrus Cobranet Packet" },
121     {ETHERTYPE_NSRP,                                    "Juniper Netscreen Redundant Protocol" },
122     /*
123      * NDISWAN on Windows translates Ethernet frames from higher-level
124      * protocols into PPP frames to hand to the PPP driver, and translates
125      * PPP frames from the PPP driver to hand to the higher-level protocols.
126      *
127      * Apparently the PPP driver, on at least some versions of Windows,
128      * passes frames for internal-to-PPP protocols up through NDISWAN;
129      * the protocol type field appears to be passed through unchanged
130      * (unlike what's done with, for example, the protocol type field
131      * for IP, which is mapped from its PPP value to its Ethernet value).
132      *
133      * This means that we may see, on Ethernet captures, frames for
134      * protocols internal to PPP, so we list as "Ethernet" protocol
135      * types the PPP protocol types we've seen.
136      */
137     {PPP_IPCP,                                  "PPP IP Control Protocol" },
138     {PPP_LCP,                                   "PPP Link Control Protocol" },
139     {PPP_PAP,                                   "PPP Password Authentication Protocol" },
140     {PPP_CCP,                                   "PPP Compression Control Protocol" },
141     {ETHERTYPE_LLT,                             "Veritas Low Latency Transport (not officially registered)"},
142     {ETHERTYPE_CFM,                             "IEEE 802.1ag Connectivity Fault Management (CFM) protocol" },
143     {ETHERTYPE_FCOE,                    "Fibre Channel over Ethernet" },
144     {ETHERTYPE_FIP,                             "FCoE Initialization Protocol" },
145     {ETHERTYPE_PTP,                             "PTPv2 over Ethernet (IEEE1588)"},
146     {ETHERTYPE_PRP,                             "Parallel Redundancy Protocol (IEC62439 Chapter 6)"},
147     {0,                                         NULL } };
148
149 static void add_dix_trailer(packet_info *pinfo, proto_tree *fh_tree,
150     int trailer_id, tvbuff_t *tvb, tvbuff_t *next_tvb, int offset_after_etype,
151     guint length_before, gint fcs_len);
152
153 void
154 capture_ethertype(guint16 etype, const guchar *pd, int offset, int len,
155                   packet_counts *ld)
156 {
157   switch (etype) {
158     case ETHERTYPE_ARP:
159       ld->arp++;
160       break;
161     case ETHERTYPE_IP:
162       capture_ip(pd, offset, len, ld);
163       break;
164     case ETHERTYPE_IPv6:
165       capture_ipv6(pd, offset, len, ld);
166       break;
167     case ETHERTYPE_IPX:
168       capture_ipx(ld);
169       break;
170     case ETHERTYPE_VLAN:
171       capture_vlan(pd, offset, len, ld);
172       break;
173     case ETHERTYPE_IEEE_802_1AD:
174     case ETHERTYPE_IEEE_802_1AH:
175       capture_ieee8021ah(pd, offset, len, ld);
176       break;
177     case ETHERTYPE_VINES_IP:
178     case ETHERTYPE_VINES_ECHO:
179       capture_vines(ld);
180       break;
181     default:
182       ld->other++;
183       break;
184   }
185 }
186
187 void
188 ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_etype,
189                 packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
190                 int etype_id, int trailer_id, int fcs_len)
191 {
192         const char              *description;
193         tvbuff_t                *volatile next_tvb;
194         guint                   length_before;
195         gint                    captured_length, reported_length;
196         volatile gboolean       dissector_found = FALSE;
197         const char              *volatile saved_proto;
198
199         /* Add the Ethernet type to the protocol tree */
200         if (tree) {
201                 proto_tree_add_uint(fh_tree, etype_id, tvb,
202                     offset_after_etype - 2, 2, etype);
203         }
204
205         /* Get the captured length and reported length of the data
206            after the Ethernet type. */
207         captured_length = tvb_length_remaining(tvb, offset_after_etype);
208         reported_length = tvb_reported_length_remaining(tvb,
209             offset_after_etype);
210
211         /* Remember how much data there is after the Ethernet type,
212            including any trailer and FCS. */
213         length_before = reported_length;
214
215         /* Construct a tvbuff for the payload after the Ethernet type.
216            If the FCS length is positive, remove the FCS.
217            (If it's zero, there's no FCS; if it's negative,
218            we don't know whether there's an FCS, so we'll
219            guess based on the length of the trailer.) */
220         if (fcs_len > 0) {
221                 if (captured_length >= 0 && reported_length >= 0) {
222                         if (reported_length >= fcs_len)
223                                 reported_length -= fcs_len;
224                         if (captured_length > reported_length)
225                                 captured_length = reported_length;
226                 }
227         }
228         next_tvb = tvb_new_subset(tvb, offset_after_etype, captured_length,
229             reported_length);
230
231         pinfo->ethertype = etype;
232
233         /* Look for sub-dissector, and call it if found.
234            Catch exceptions, so that if the reported length of "next_tvb"
235            was reduced by some dissector before an exception was thrown,
236            we can still put in an item for the trailer. */
237         saved_proto = pinfo->current_proto;
238         TRY {
239                 dissector_found = dissector_try_port(ethertype_dissector_table,
240                     etype, next_tvb, pinfo, tree);
241         }
242         CATCH(BoundsError) {
243                 /* Somebody threw BoundsError, which means that:
244
245                      1) a dissector was found, so we don't need to
246                         dissect the payload as data or update the
247                         protocol or info columns;
248
249                      2) dissecting the payload found that the packet was
250                         cut off by a snapshot length before the end of
251                         the payload.  The trailer comes after the payload,
252                         so *all* of the trailer is cut off, and we'll
253                         just get another BoundsError if we add the trailer.
254
255                    Therefore, we just rethrow the exception so it gets
256                    reported; we don't dissect the trailer or do anything
257                    else. */
258                  RETHROW;
259         }
260         CATCH(OutOfMemoryError) {
261                  RETHROW;
262         }
263         CATCH_ALL {
264                 /* Somebody threw an exception other than BoundsError, which
265                    means that a dissector was found, so we don't need to
266                    dissect the payload as data or update the protocol or info
267                    columns.  We just show the exception and then drive on
268                    to show the trailer, after noting that a dissector was
269                    found and restoring the protocol value that was in effect
270                    before we called the subdissector. */
271                 show_exception(next_tvb, pinfo, tree, EXCEPT_CODE, GET_MESSAGE);
272                 dissector_found = TRUE;
273                 pinfo->current_proto = saved_proto;
274         }
275         ENDTRY;
276
277         if (!dissector_found) {
278                 /* No sub-dissector found.
279                    Label rest of packet as "Data" */
280                 call_dissector(data_handle,next_tvb, pinfo, tree);
281
282                 /* Label protocol */
283                 if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
284                         col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "0x%04x",
285                             etype);
286                 }
287                 if (check_col(pinfo->cinfo, COL_INFO)) {
288                         description = match_strval(etype, etype_vals);
289                         if (description) {
290                                 col_add_str(pinfo->cinfo, COL_INFO, 
291                                     description);
292                         }
293                 }
294         }
295
296         add_dix_trailer(pinfo, fh_tree, trailer_id, tvb, next_tvb, offset_after_etype,
297             length_before, fcs_len);
298 }
299
300 static void
301 add_dix_trailer(packet_info *pinfo, proto_tree *fh_tree, int trailer_id,
302     tvbuff_t *tvb, tvbuff_t *next_tvb, int offset_after_etype,
303     guint length_before, gint fcs_len)
304 {
305         guint           length;
306         tvbuff_t        *trailer_tvb;
307
308         if (fh_tree == NULL)
309                 return; /* we're not building a protocol tree */
310
311         if (trailer_id == -1)
312                 return; /* our caller doesn't care about trailers */
313
314         /* OK, how much is there in that tvbuff now? */
315         length = tvb_reported_length(next_tvb);
316
317         /* If there's less than there was before, what's left is
318            a trailer. */
319         if (length < length_before) {
320                 /*
321                  * Is any of the padding present in the tvbuff?
322                  */
323                 if (tvb_offset_exists(tvb, offset_after_etype + length)) {
324                         /*
325                          * Yes - create a tvbuff for the padding.
326                          */
327                         trailer_tvb = tvb_new_subset(tvb,
328                             offset_after_etype + length, -1, -1);
329                 } else {
330                         /*
331                          * No - don't bother showing the trailer.
332                          * XXX - show a Short Frame indication?
333                          */
334                         trailer_tvb = NULL;
335                 }
336         } else
337                 trailer_tvb = NULL;     /* no trailer */
338
339         add_ethernet_trailer(pinfo, fh_tree, trailer_id, tvb, trailer_tvb, fcs_len);
340 }
341
342 void
343 proto_register_ethertype(void)
344 {
345         /* subdissector code */
346         ethertype_dissector_table = register_dissector_table("ethertype",
347             "Ethertype", FT_UINT16, BASE_HEX);
348 }
349
350 void
351 proto_reg_handoff_ethertype(void)
352 {
353         data_handle = find_dissector("data");
354 }