2 * Routines for VLAN 802.1Q ethernet header disassembly
4 * $Id: packet-vlan.c,v 1.29 2001/01/09 09:59:28 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 ipx_handle;
55 static dissector_handle_t llc_handle;
58 capture_vlan(const u_char *pd, int offset, packet_counts *ld ) {
60 if ( !BYTES_ARE_IN_FRAME(offset,5) ) {
64 encap_proto = pntohs( &pd[offset+2] );
65 if ( encap_proto <= IEEE_802_3_MAX_LEN) {
66 if ( pd[offset+4] == 0xff && pd[offset+5] == 0xff ) {
67 capture_ipx(pd,offset+4,ld);
69 capture_llc(pd,offset+4,ld);
72 capture_ethertype(encap_proto, offset+4, pd, ld);
77 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
80 guint16 tci,encap_proto;
81 volatile gboolean is_802_2;
82 tvbuff_t *volatile next_tvb;
83 tvbuff_t *volatile trailer_tvb;
84 proto_tree *volatile vlan_tree;
85 guint length_before, length;
87 CHECK_DISPLAY_AS_DATA(proto_vlan, tvb, pinfo, tree);
89 pinfo->current_proto = "VLAN";
91 if (check_col(pinfo->fd, COL_PROTOCOL))
92 col_set_str(pinfo->fd, COL_PROTOCOL, "VLAN");
94 tci = tvb_get_ntohs( tvb, 0 );
95 encap_proto = tvb_get_ntohs( tvb, 2 );
97 if (check_col(pinfo->fd, COL_INFO)) {
98 col_add_fstr(pinfo->fd, COL_INFO, "PRI: %d CFI: %d ID: %d",
99 (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
105 ti = proto_tree_add_item(tree, proto_vlan, tvb, 0, 4, FALSE);
106 vlan_tree = proto_item_add_subtree(ti, ett_vlan);
108 proto_tree_add_uint(vlan_tree, hf_vlan_priority, tvb, 0, 2, tci);
109 proto_tree_add_uint(vlan_tree, hf_vlan_cfi, tvb, 0, 2, tci);
110 proto_tree_add_uint(vlan_tree, hf_vlan_id, tvb, 0, 2, tci);
113 if ( encap_proto <= IEEE_802_3_MAX_LEN) {
114 /* Give the next dissector only 'encap_proto' number of bytes */
115 proto_tree_add_uint(vlan_tree, hf_vlan_len, tvb, 2, 2, encap_proto);
117 next_tvb = tvb_new_subset(tvb, 4, encap_proto, encap_proto);
118 trailer_tvb = tvb_new_subset(tvb, 4 + encap_proto, -1, -1);
120 CATCH2(BoundsError, ReportedBoundsError) {
123 the packet doesn't have "encap_proto" bytes worth of
124 captured data left in it - or it may not even have
125 "encap_proto" bytes worth of data in it, period -
126 so the "tvb_new_subset()" creating "next_tvb"
131 the packet has exactly "encap_proto" bytes worth of
132 captured data left in it, so the "tvb_new_subset()"
133 creating "trailer_tvb" threw an exception.
135 In either case, this means that all the data in the frame
136 is within the length value, so we give all the data to the
137 next protocol and have no trailer. */
138 next_tvb = tvb_new_subset(tvb, 4, -1, encap_proto);
143 /* Is there an 802.2 layer? I can tell by looking at the first 2
144 bytes after the VLAN header. If they are 0xffff, then what
145 follows the VLAN header is an IPX payload, meaning no 802.2.
146 (IPX/SPX is they only thing that can be contained inside a
147 straight 802.3 packet, so presumably the same applies for
148 Ethernet VLAN packets). A non-0xffff value means that there's an
149 802.2 layer inside the VLAN layer */
152 if (tvb_get_ntohs(next_tvb, 2) == 0xffff) {
156 CATCH2(BoundsError, ReportedBoundsError) {
163 call_dissector(llc_handle, next_tvb, pinfo, tree);
165 call_dissector(ipx_handle, next_tvb, pinfo, tree);
168 length_before = tvb_reported_length(tvb);
169 length = ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree,
171 if (length < length_before) {
173 * Create a tvbuff for the padding.
176 trailer_tvb = tvb_new_subset(tvb, length, -1, -1);
178 CATCH2(BoundsError, ReportedBoundsError) {
179 /* The packet doesn't have "length" bytes worth of captured
180 data left in it. No trailer to display. */
190 /* If there's some bytes left over, mark them. */
191 if (trailer_tvb && tree) {
195 trailer_length = tvb_length(trailer_tvb);
196 if (trailer_length > 0) {
197 ptr = tvb_get_ptr(trailer_tvb, 0, trailer_length);
198 proto_tree_add_bytes(vlan_tree, hf_vlan_trailer, trailer_tvb, 0,
199 trailer_length, ptr);
205 proto_register_vlan(void)
207 static hf_register_info hf[] = {
208 { &hf_vlan_priority, {
209 "Priority", "vlan.priority", FT_UINT16, BASE_BIN,
210 0, 0xE000, "Priority" }},
212 "CFI", "vlan.cfi", FT_UINT16, BASE_BIN,
213 0, 0x1000, "CFI" }}, /* XXX - Boolean? */
215 "ID", "vlan.id", FT_UINT16, BASE_BIN,
218 "Type", "vlan.etype", FT_UINT16, BASE_HEX,
219 VALS(etype_vals), 0x0, "Type" }},
221 "Length", "vlan.len", FT_UINT16, BASE_DEC,
222 NULL, 0x0, "Length" }},
223 { &hf_vlan_trailer, {
224 "Trailer", "vlan.trailer", FT_BYTES, BASE_NONE,
225 NULL, 0x0, "VLAN Trailer" }}
227 static gint *ett[] = {
231 proto_vlan = proto_register_protocol("802.1q Virtual LAN", "VLAN", "vlan");
232 proto_register_field_array(proto_vlan, hf, array_length(hf));
233 proto_register_subtree_array(ett, array_length(ett));
237 proto_reg_handoff_vlan(void)
240 * Get handles for the IPX and LLC dissectors.
242 llc_handle = find_dissector("llc");
243 ipx_handle = find_dissector("ipx");
245 dissector_add("ethertype", ETHERTYPE_VLAN, dissect_vlan, proto_vlan);