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