2 * Routines for VLAN 802.1Q ethernet header disassembly
4 * $Id: packet-vlan.c,v 1.28 2001/01/09 06:31:44 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
40 #include "packet-ipx.h"
41 #include "packet-llc.h"
44 static int proto_vlan = -1;
45 static int hf_vlan_priority = -1;
46 static int hf_vlan_cfi = -1;
47 static int hf_vlan_id = -1;
48 static int hf_vlan_etype = -1;
49 static int hf_vlan_len = -1;
50 static int hf_vlan_trailer = -1;
52 static gint ett_vlan = -1;
54 static dissector_handle_t llc_handle;
57 capture_vlan(const u_char *pd, int offset, packet_counts *ld ) {
59 if ( !BYTES_ARE_IN_FRAME(offset,5) ) {
63 encap_proto = pntohs( &pd[offset+2] );
64 if ( encap_proto <= IEEE_802_3_MAX_LEN) {
65 if ( pd[offset+4] == 0xff && pd[offset+5] == 0xff ) {
66 capture_ipx(pd,offset+4,ld);
68 capture_llc(pd,offset+4,ld);
71 capture_ethertype(encap_proto, offset+4, pd, ld);
76 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
79 guint16 tci,encap_proto;
80 volatile gboolean is_802_2;
81 tvbuff_t *volatile next_tvb;
82 tvbuff_t *volatile trailer_tvb;
83 proto_tree *volatile vlan_tree;
84 guint length_before, length;
86 CHECK_DISPLAY_AS_DATA(proto_vlan, tvb, pinfo, tree);
88 pinfo->current_proto = "VLAN";
90 if (check_col(pinfo->fd, COL_PROTOCOL))
91 col_set_str(pinfo->fd, COL_PROTOCOL, "VLAN");
93 tci = tvb_get_ntohs( tvb, 0 );
94 encap_proto = tvb_get_ntohs( tvb, 2 );
96 if (check_col(pinfo->fd, COL_INFO)) {
97 col_add_fstr(pinfo->fd, COL_INFO, "PRI: %d CFI: %d ID: %d",
98 (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
104 ti = proto_tree_add_item(tree, proto_vlan, tvb, 0, 4, FALSE);
105 vlan_tree = proto_item_add_subtree(ti, ett_vlan);
107 proto_tree_add_uint(vlan_tree, hf_vlan_priority, tvb, 0, 2, tci);
108 proto_tree_add_uint(vlan_tree, hf_vlan_cfi, tvb, 0, 2, tci);
109 proto_tree_add_uint(vlan_tree, hf_vlan_id, tvb, 0, 2, tci);
112 if ( encap_proto <= IEEE_802_3_MAX_LEN) {
113 /* Give the next dissector only 'encap_proto' number of bytes */
114 proto_tree_add_uint(vlan_tree, hf_vlan_len, tvb, 2, 2, encap_proto);
116 next_tvb = tvb_new_subset(tvb, 4, encap_proto, encap_proto);
117 trailer_tvb = tvb_new_subset(tvb, 4 + encap_proto, -1, -1);
119 CATCH2(BoundsError, ReportedBoundsError) {
122 the packet doesn't have "encap_proto" bytes worth of
123 captured data left in it - or it may not even have
124 "encap_proto" bytes worth of data in it, period -
125 so the "tvb_new_subset()" creating "next_tvb"
130 the packet has exactly "encap_proto" bytes worth of
131 captured data left in it, so the "tvb_new_subset()"
132 creating "trailer_tvb" threw an exception.
134 In either case, this means that all the data in the frame
135 is within the length value, so we give all the data to the
136 next protocol and have no trailer. */
137 next_tvb = tvb_new_subset(tvb, 4, -1, encap_proto);
142 /* Is there an 802.2 layer? I can tell by looking at the first 2
143 bytes after the VLAN header. If they are 0xffff, then what
144 follows the VLAN header is an IPX payload, meaning no 802.2.
145 (IPX/SPX is they only thing that can be contained inside a
146 straight 802.3 packet, so presumably the same applies for
147 Ethernet VLAN packets). A non-0xffff value means that there's an
148 802.2 layer inside the VLAN layer */
151 if (tvb_get_ntohs(next_tvb, 2) == 0xffff) {
155 CATCH2(BoundsError, ReportedBoundsError) {
162 call_dissector(llc_handle, next_tvb, pinfo, tree);
164 dissect_ipx(next_tvb, pinfo, tree);
167 length_before = tvb_reported_length(tvb);
168 length = ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree,
170 if (length < length_before) {
172 * Create a tvbuff for the padding.
175 trailer_tvb = tvb_new_subset(tvb, length, -1, -1);
177 CATCH2(BoundsError, ReportedBoundsError) {
178 /* The packet doesn't have "length" bytes worth of captured
179 data left in it. No trailer to display. */
189 /* If there's some bytes left over, mark them. */
190 if (trailer_tvb && tree) {
194 trailer_length = tvb_length(trailer_tvb);
195 if (trailer_length > 0) {
196 ptr = tvb_get_ptr(trailer_tvb, 0, trailer_length);
197 proto_tree_add_bytes(vlan_tree, hf_vlan_trailer, trailer_tvb, 0,
198 trailer_length, ptr);
204 proto_register_vlan(void)
206 static hf_register_info hf[] = {
207 { &hf_vlan_priority, {
208 "Priority", "vlan.priority", FT_UINT16, BASE_BIN,
209 0, 0xE000, "Priority" }},
211 "CFI", "vlan.cfi", FT_UINT16, BASE_BIN,
212 0, 0x1000, "CFI" }}, /* XXX - Boolean? */
214 "ID", "vlan.id", FT_UINT16, BASE_BIN,
217 "Type", "vlan.etype", FT_UINT16, BASE_HEX,
218 VALS(etype_vals), 0x0, "Type" }},
220 "Length", "vlan.len", FT_UINT16, BASE_DEC,
221 NULL, 0x0, "Length" }},
222 { &hf_vlan_trailer, {
223 "Trailer", "vlan.trailer", FT_BYTES, BASE_NONE,
224 NULL, 0x0, "VLAN Trailer" }}
226 static gint *ett[] = {
230 proto_vlan = proto_register_protocol("802.1q Virtual LAN", "VLAN", "vlan");
231 proto_register_field_array(proto_vlan, hf, array_length(hf));
232 proto_register_subtree_array(ett, array_length(ett));
236 proto_reg_handoff_vlan(void)
239 * Get a handle for the LLC dissector.
241 llc_handle = find_dissector("llc");
243 dissector_add("ethertype", ETHERTYPE_VLAN, dissect_vlan, proto_vlan);