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