2 * Routines for VLAN 802.1Q ethernet header disassembly
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include <epan/packet.h>
31 #include "packet-ieee8023.h"
32 #include "packet-ipx.h"
33 #include "packet-llc.h"
34 #include "packet-vlan.h"
35 #include <epan/etypes.h>
36 #include <epan/prefs.h>
38 void proto_reg_handoff_vlan(void);
40 static unsigned int q_in_q_ethertype = 0x9100;
42 static gboolean vlan_summary_in_tree = TRUE;
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;
55 capture_vlan(const guchar *pd, int offset, int len, packet_counts *ld ) {
57 if ( !BYTES_ARE_IN_FRAME(offset,len,5) ) {
61 encap_proto = pntohs( &pd[offset+2] );
62 if ( encap_proto <= IEEE_802_3_MAX_LEN) {
63 if ( pd[offset+4] == 0xff && pd[offset+5] == 0xff ) {
66 capture_llc(pd,offset+4,len,ld);
69 capture_ethertype(encap_proto, pd, offset+4, len, ld);
74 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
78 volatile guint16 encap_proto;
79 volatile gboolean is_802_2;
80 proto_tree *volatile vlan_tree;
82 col_set_str(pinfo->cinfo, COL_PROTOCOL, "VLAN");
83 col_clear(pinfo->cinfo, COL_INFO);
85 tci = tvb_get_ntohs( tvb, 0 );
87 if (check_col(pinfo->cinfo, COL_INFO)) {
88 col_add_fstr(pinfo->cinfo, COL_INFO, "PRI: %u CFI: %u ID: %u",
89 (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
91 if ( check_col(pinfo->cinfo, COL_8021Q_VLAN_ID)) {
92 col_add_fstr(pinfo->cinfo, COL_8021Q_VLAN_ID, "%u", (tci & 0xFFF));
98 ti = proto_tree_add_item(tree, proto_vlan, tvb, 0, 4, FALSE);
100 if (vlan_summary_in_tree) {
101 proto_item_append_text(ti, ", PRI: %u, CFI: %u, ID: %u",
102 (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
105 vlan_tree = proto_item_add_subtree(ti, ett_vlan);
107 proto_tree_add_item(vlan_tree, hf_vlan_priority, tvb, 0, 2, FALSE);
108 proto_tree_add_item(vlan_tree, hf_vlan_cfi, tvb, 0, 2, FALSE);
109 proto_tree_add_item(vlan_tree, hf_vlan_id, tvb, 0, 2, FALSE);
112 encap_proto = tvb_get_ntohs(tvb, 2);
113 if (encap_proto <= IEEE_802_3_MAX_LEN) {
114 /* Is there an 802.2 layer? I can tell by looking at the first 2
115 bytes after the VLAN header. If they are 0xffff, then what
116 follows the VLAN header is an IPX payload, meaning no 802.2.
117 (IPX/SPX is they only thing that can be contained inside a
118 straight 802.3 packet, so presumably the same applies for
119 Ethernet VLAN packets). A non-0xffff value means that there's an
120 802.2 layer inside the VLAN layer */
123 if (tvb_get_ntohs(tvb, 4) == 0xffff) {
127 CATCH2(BoundsError, ReportedBoundsError) {
133 dissect_802_3(encap_proto, is_802_2, tvb, 4, pinfo, tree, vlan_tree,
134 hf_vlan_len, hf_vlan_trailer, 0);
136 ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree,
137 hf_vlan_etype, hf_vlan_trailer, 0);
142 proto_register_vlan(void)
144 static hf_register_info hf[] = {
145 { &hf_vlan_priority, {
146 "Priority", "vlan.priority", FT_UINT16, BASE_DEC,
147 NULL, 0xE000, "User Priority", HFILL }},
149 "CFI", "vlan.cfi", FT_UINT16, BASE_DEC,
150 NULL, 0x1000, "Canonical Format Identifier", HFILL }},
152 "ID", "vlan.id", FT_UINT16, BASE_DEC,
153 NULL, 0x0FFF, "VLAN ID", HFILL }},
155 "Type", "vlan.etype", FT_UINT16, BASE_HEX,
156 VALS(etype_vals), 0x0, "Ethertype", HFILL }},
158 "Length", "vlan.len", FT_UINT16, BASE_DEC,
159 NULL, 0x0, NULL, HFILL }},
160 { &hf_vlan_trailer, {
161 "Trailer", "vlan.trailer", FT_BYTES, BASE_NONE,
162 NULL, 0x0, "VLAN Trailer", HFILL }}
164 static gint *ett[] = {
167 module_t *vlan_module;
169 proto_vlan = proto_register_protocol("802.1Q Virtual LAN", "VLAN", "vlan");
170 proto_register_field_array(proto_vlan, hf, array_length(hf));
171 proto_register_subtree_array(ett, array_length(ett));
173 vlan_module = prefs_register_protocol(proto_vlan, proto_reg_handoff_vlan);
174 prefs_register_bool_preference(vlan_module, "summary_in_tree",
175 "Show vlan summary in protocol tree",
176 "Whether the vlan summary line should be shown in the protocol tree",
177 &vlan_summary_in_tree);
178 prefs_register_uint_preference(vlan_module, "qinq_ethertype",
179 "802.1QinQ Ethertype (in hex)",
180 "The (hexadecimal) Ethertype used to indicate 802.1QinQ VLAN in VLAN tunneling.",
181 16, &q_in_q_ethertype);
186 proto_reg_handoff_vlan(void)
188 static gboolean prefs_initialized = FALSE;
189 static dissector_handle_t vlan_handle;
190 static unsigned int old_q_in_q_ethertype;
192 if (!prefs_initialized)
194 vlan_handle = create_dissector_handle(dissect_vlan, proto_vlan);
195 dissector_add("ethertype", ETHERTYPE_VLAN, vlan_handle);
196 prefs_initialized = TRUE;
200 dissector_delete("ethertype", old_q_in_q_ethertype, vlan_handle);
203 old_q_in_q_ethertype = q_in_q_ethertype;
205 dissector_add("ethertype", q_in_q_ethertype, vlan_handle);