Updates from Ed Warnicke.
[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.9 2000/11/18 10:38:24 guy Exp $
5  *
6  * Gilbert Ramirez <gram@xiexie.org>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #include <glib.h>
37 #include "packet.h"
38 #include "packet-ip.h"
39 #include "packet-ipx.h"
40 #include "packet-vlan.h"
41 #include "packet-vines.h"
42 #include "etypes.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_PPPOED,          "PPPoE Discovery"               }, 
60     {ETHERTYPE_PPPOES,          "PPPoE Session"                 }, 
61     {ETHERTYPE_VLAN,            "802.1Q Virtual LAN"            },
62     {ETHERTYPE_MPLS,            "MPLS label switched packet"    },
63     {ETHERTYPE_MPLS_MULTI,      "MPLS multicast label switched packet" },
64     {ETHERTYPE_3C_NBP_DGRAM,    "3Com NBP Datagram"             },
65     {ETHERTYPE_DEC,             "DEC proto"                     },
66     {ETHERTYPE_DNA_DL,          "DEC DNA Dump/Load"             },
67     {ETHERTYPE_DNA_RC,          "DEC DNA Remote Console"        },
68     {ETHERTYPE_DNA_RT,          "DEC DNA Routing"               },
69     {ETHERTYPE_LAT,             "DEC LAT"                       },
70     {ETHERTYPE_DEC_DIAG,        "DEC Diagnostics"               },
71     {ETHERTYPE_DEC_CUST,        "DEC Customer use"              },
72     {ETHERTYPE_DEC_SCA,         "DEC LAVC/SCA"                  },
73     {0,                         NULL                            } };
74
75 void
76 capture_ethertype(guint16 etype, int offset,
77                 const u_char *pd, packet_counts *ld)
78 {
79   switch (etype) {
80     case ETHERTYPE_IP:
81       capture_ip(pd, offset, ld);
82       break;
83     case ETHERTYPE_IPX:
84       capture_ipx(pd, offset, ld);
85       break;
86     case ETHERTYPE_VLAN:
87       capture_vlan(pd, offset, ld);
88       break;
89     case ETHERTYPE_VINES:
90       capture_vines(pd, offset, ld);
91       break;
92     default:
93       ld->other++;
94       break;
95   }
96 }
97
98 guint
99 ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_etype, packet_info *pinfo,
100                 proto_tree *tree, proto_tree *fh_tree, int item_id)
101 {
102         char            *description;
103         tvbuff_t        *next_tvb;
104         
105         /* Add to proto_tree */
106         if (tree) {
107                 proto_tree_add_uint(fh_tree, item_id, tvb, offset_after_etype - 2, 2, etype);
108         }
109
110         /* Tvbuff for the payload after the Ethernet type. */
111         next_tvb = tvb_new_subset(tvb, offset_after_etype, -1, -1);
112
113         /* Look for sub-dissector */
114         if (!dissector_try_port(ethertype_dissector_table, etype,
115             next_tvb, pinfo, tree)) {
116                 /* No sub-dissector found.
117                    Label rest of packet as "Data" */
118                 dissect_data(next_tvb, 0, pinfo, tree);
119
120                 /* Label protocol */
121                 switch(etype) {
122
123                 case ETHERTYPE_LOOP:
124                         if (check_col(pinfo->fd, COL_PROTOCOL)) {
125                                 col_add_fstr(pinfo->fd, COL_PROTOCOL, "LOOP");
126                         }
127                         break;
128                 default:
129                         if (check_col(pinfo->fd, COL_PROTOCOL)) {
130                                 col_add_fstr(pinfo->fd, COL_PROTOCOL, "0x%04x", etype);
131                         }
132                         break;
133                 }
134                 if (check_col(pinfo->fd, COL_INFO)) {
135                         description = match_strval(etype, etype_vals);
136                         if (description) {
137                                 col_add_fstr(pinfo->fd, COL_INFO, "%s", description);
138                         }
139                 }
140         }
141
142         /* Return the length of that tvbuff; the subdissector may have
143            reduced the length to a value specified by a length field
144            in its header, meaning what remains is padding. */
145         return tvb_reported_length(next_tvb);
146 }
147
148
149 void
150 proto_register_ethertype(void)
151 {
152         /* subdissector code */
153         ethertype_dissector_table = register_dissector_table("ethertype");
154 }