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