There's no need to keep a "FILE *" for the file being printed to in a
[obnox/wireshark/wip.git] / packet-ethertype.c
1 /* ethertype.c
2  * Routines for calling the right protocol for the ethertype.
3  *
4  * $Id: packet-ethertype.c,v 1.41 2003/11/27 21:09:42 guy Exp $
5  *
6  * Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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-ip.h"
35 #include "packet-ipv6.h"
36 #include "packet-ipx.h"
37 #include "packet-vlan.h"
38 #include "packet-vines.h"
39 #include "etypes.h"
40 #include "ppptypes.h"
41
42 static dissector_table_t ethertype_dissector_table;
43
44 static dissector_handle_t data_handle;
45
46 const value_string etype_vals[] = {
47     {ETHERTYPE_IP,              "IP"                            },
48     {ETHERTYPE_IPv6,            "IPv6"                          },
49     {ETHERTYPE_X25L3,           "X.25 Layer 3"                  },
50     {ETHERTYPE_ARP,             "ARP"                           },
51     {ETHERTYPE_REVARP,          "RARP"                          },
52     {ETHERTYPE_DEC_LB,          "DEC LanBridge"                 },
53     {ETHERTYPE_ATALK,           "Appletalk"                     },
54     {ETHERTYPE_SNA,             "SNA-over-Ethernet"             },
55     {ETHERTYPE_AARP,            "AARP"                          },
56     {ETHERTYPE_IPX,             "Netware IPX/SPX"               },
57     {ETHERTYPE_VINES_IP,        "Vines IP"                      },
58     {ETHERTYPE_VINES_ECHO,      "Vines Echo"                    },
59     {ETHERTYPE_TRAIN,           "Netmon Train"                  },
60     {ETHERTYPE_LOOP,            "Loopback"                      }, /* Ethernet Loopback */
61     {ETHERTYPE_WCP,             "Wellfleet Compression Protocol" },
62     {ETHERTYPE_ISMP,            "Cabletron Interswitch Message Protocol" },
63     {ETHERTYPE_ISMP_TBFLOOD,    "Cabletron SFVLAN 1.8 Tag-Based Flood" },
64                                 /* for ISMP, see RFC 2641, RFC 2642, RFC 2643 */
65     {ETHERTYPE_PPPOED,          "PPPoE Discovery"               },
66     {ETHERTYPE_PPPOES,          "PPPoE Session"                 },
67     {ETHERTYPE_INTEL_ANS,       "Intel ANS probe"               },
68     {ETHERTYPE_MS_NLB_HEARTBEAT,        "MS NLB heartbeat"      },
69     {ETHERTYPE_VLAN,            "802.1Q Virtual LAN"            },
70     {ETHERTYPE_EAPOL,           "802.1X Authentication"         },
71     {ETHERTYPE_MPLS,            "MPLS label switched packet"    },
72     {ETHERTYPE_MPLS_MULTI,      "MPLS multicast label switched packet" },
73     {ETHERTYPE_3C_NBP_DGRAM,    "3Com NBP Datagram"             },
74     {ETHERTYPE_DEC,             "DEC proto"                     },
75     {ETHERTYPE_DNA_DL,          "DEC DNA Dump/Load"             },
76     {ETHERTYPE_DNA_RC,          "DEC DNA Remote Console"        },
77     {ETHERTYPE_DNA_RT,          "DEC DNA Routing"               },
78     {ETHERTYPE_LAT,             "DEC LAT"                       },
79     {ETHERTYPE_DEC_DIAG,        "DEC Diagnostics"               },
80     {ETHERTYPE_DEC_CUST,        "DEC Customer use"              },
81     {ETHERTYPE_DEC_SCA,         "DEC LAVC/SCA"                  },
82     {ETHERTYPE_ETHBRIDGE,       "Transparent Ethernet bridging" },
83     {ETHERTYPE_CGMP,            "Cisco Group Management Protocol" },
84     {ETHERTYPE_SLOW_PROTOCOLS,  "Slow Protocols"                },
85     {ETHERTYPE_RTNET,           "RTNET Protocol"                },
86     {ETHERTYPE_RTCFG,           "RTCFG Protocol"                },
87     {ETHERTYPE_PROFINET,        "PROFInet"                },
88
89     /*
90      * NDISWAN on Windows translates Ethernet frames from higher-level
91      * protocols into PPP frames to hand to the PPP driver, and translates
92      * PPP frames from the PPP driver to hand to the higher-level protocols.
93      *
94      * Apparently the PPP driver, on at least some versions of Windows,
95      * passes frames for internal-to-PPP protocols up through NDISWAN;
96      * the protocol type field appears to be passed through unchanged
97      * (unlike what's done with, for example, the protocol type field
98      * for IP, which is mapped from its PPP value to its Ethernet value).
99      *
100      * This means that we may see, on Ethernet captures, frames for
101      * protocols internal to PPP, so we list as "Ethernet" protocol
102      * types the PPP protocol types we've seen.
103      */
104     {PPP_IPCP,                  "PPP IP Control Protocol" },
105     {PPP_LCP,                   "PPP Link Control Protocol" },
106     {PPP_PAP,                   "PPP Password Authentication Protocol" },
107     {PPP_CCP,                   "PPP Compression Control Protocol" },
108     {0,                         NULL                            } };
109
110 static void add_dix_trailer(proto_tree *fh_tree, int trailer_id, tvbuff_t *tvb,
111     tvbuff_t *next_tvb, int offset_after_etype, guint length_before,
112     gint fcs_len);
113
114 void
115 capture_ethertype(guint16 etype, const guchar *pd, int offset, int len,
116                   packet_counts *ld)
117 {
118   switch (etype) {
119     case ETHERTYPE_ARP:
120       ld->arp++;
121       break;
122     case ETHERTYPE_IP:
123       capture_ip(pd, offset, len, ld);
124       break;
125     case ETHERTYPE_IPv6:
126       capture_ipv6(pd, offset, len, ld);
127       break;
128     case ETHERTYPE_IPX:
129       capture_ipx(ld);
130       break;
131     case ETHERTYPE_VLAN:
132       capture_vlan(pd, offset, len, ld);
133       break;
134     case ETHERTYPE_VINES_IP:
135     case ETHERTYPE_VINES_ECHO:
136       capture_vines(ld);
137       break;
138     default:
139       ld->other++;
140       break;
141   }
142 }
143
144 void
145 ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_etype,
146                 packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
147                 int etype_id, int trailer_id, int fcs_len)
148 {
149         char                    *description;
150         tvbuff_t                *next_tvb;
151         guint                   length_before;
152         volatile gboolean       dissector_found;
153         const char              *saved_proto, *exception_proto;
154
155         /* Add the Ethernet type to the protocol tree */
156         if (tree) {
157                 proto_tree_add_uint(fh_tree, etype_id, tvb,
158                     offset_after_etype - 2, 2, etype);
159         }
160
161         /* Tvbuff for the payload after the Ethernet type. */
162         next_tvb = tvb_new_subset(tvb, offset_after_etype, -1, -1);
163
164         pinfo->ethertype = etype;
165
166         /* Remember how much data there is in it. */
167         length_before = tvb_reported_length(next_tvb);
168
169         /* Look for sub-dissector, and call it if found.
170            Catch BoundsError and ReportedBoundsError, so that if the
171            reported length of "next_tvb" was reduced by some dissector
172            before an exception was thrown, we can still put in an item
173            for the trailer. */
174         saved_proto = pinfo->current_proto;
175         TRY {
176                 dissector_found = dissector_try_port(ethertype_dissector_table,
177                     etype, next_tvb, pinfo, tree);
178         }
179         CATCH2(BoundsError, ReportedBoundsError) {
180                 /* Well, somebody threw an exception; that means that a
181                    dissector was found, so we don't need to dissect
182                    the payload as data or update the protocol or info
183                    columns. */
184                 dissector_found = TRUE;
185
186                 /* Save the protocol for which the exception was
187                    thrown, and restore pinfo->current_proto to the
188                    one for us, so if we throw an exception adding
189                    the trailer (e.g., if we get an FCS when capturing,
190                    but the snapshot length cut off the FCS), the
191                    exception will reflect the appropriate protocol. */
192                 exception_proto = pinfo->current_proto;
193                 pinfo->current_proto = saved_proto;
194
195                 /* Add the trailer, if appropriate. */
196                 add_dix_trailer(fh_tree, trailer_id, tvb, next_tvb,
197                     offset_after_etype, length_before, fcs_len);
198
199                 /* Put back the protocol for which the exception is
200                    thrown, and rethrow the exception, so the
201                    appropriate indication for the exception can be
202                    put into the tree. */
203                 pinfo->current_proto = exception_proto;
204                 RETHROW;
205
206                 /* XXX - RETHROW shouldn't return. */
207                 g_assert_not_reached();
208         }
209         ENDTRY;
210
211         if (!dissector_found) {
212                 /* No sub-dissector found.
213                    Label rest of packet as "Data" */
214                 call_dissector(data_handle,next_tvb, pinfo, tree);
215
216                 /* Label protocol */
217                 switch (etype) {
218
219                 case ETHERTYPE_LOOP:
220                         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
221                                 col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "LOOP");
222                         }
223                         break;
224
225                 default:
226                         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
227                                 col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "0x%04x",
228                                     etype);
229                         }
230                         break;
231                 }
232                 if (check_col(pinfo->cinfo, COL_INFO)) {
233                         description = match_strval(etype, etype_vals);
234                         if (description) {
235                                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
236                                     description);
237                         }
238                 }
239         }
240
241         add_dix_trailer(fh_tree, trailer_id, tvb, next_tvb, offset_after_etype,
242             length_before, fcs_len);
243 }
244
245 static void
246 add_dix_trailer(proto_tree *fh_tree, int trailer_id, tvbuff_t *tvb,
247     tvbuff_t *next_tvb, int offset_after_etype, guint length_before,
248     gint fcs_len)
249 {
250         guint           length;
251         tvbuff_t        *volatile trailer_tvb;
252
253         if (fh_tree == NULL)
254                 return; /* we're not building a protocol tree */
255
256         if (trailer_id == -1)
257                 return; /* our caller doesn't care about trailers */
258
259         /* OK, how much is there in that tvbuff now? */
260         length = tvb_reported_length(next_tvb);
261
262         /* If there's less than there was before, what's left is
263            a trailer. */
264         if (length < length_before) {
265                 /*
266                  * Create a tvbuff for the padding.
267                  */
268                 TRY {
269                         trailer_tvb = tvb_new_subset(tvb,
270                             offset_after_etype + length, -1, -1);
271                 }
272                 CATCH2(BoundsError, ReportedBoundsError) {
273                         /* The packet doesn't have "length" bytes worth of
274                            captured data left in it.  No trailer to display. */
275                         trailer_tvb = NULL;
276                 }
277                 ENDTRY;
278         } else
279                 trailer_tvb = NULL;     /* no trailer */
280
281         add_ethernet_trailer(fh_tree, trailer_id, tvb, trailer_tvb, fcs_len);
282 }
283
284 void
285 proto_register_ethertype(void)
286 {
287         /* subdissector code */
288         ethertype_dissector_table = register_dissector_table("ethertype",
289             "Ethertype", FT_UINT16, BASE_HEX);
290 }
291
292 void
293 proto_reg_handoff_ethertype(void)
294 {
295         data_handle = find_dissector("data");
296 }