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 old_q_in_q_ethertype;
41 static unsigned int q_in_q_ethertype = 0x9100;
43 static int proto_vlan = -1;
44 static int hf_vlan_priority = -1;
45 static int hf_vlan_cfi = -1;
46 static int hf_vlan_id = -1;
47 static int hf_vlan_etype = -1;
48 static int hf_vlan_len = -1;
49 static int hf_vlan_trailer = -1;
51 static gint ett_vlan = -1;
54 capture_vlan(const guchar *pd, int offset, int len, packet_counts *ld ) {
56 if ( !BYTES_ARE_IN_FRAME(offset,len,5) ) {
60 encap_proto = pntohs( &pd[offset+2] );
61 if ( encap_proto <= IEEE_802_3_MAX_LEN) {
62 if ( pd[offset+4] == 0xff && pd[offset+5] == 0xff ) {
65 capture_llc(pd,offset+4,len,ld);
68 capture_ethertype(encap_proto, pd, offset+4, len, ld);
73 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
76 guint16 tci,encap_proto;
77 volatile gboolean is_802_2;
78 proto_tree *volatile vlan_tree;
80 if (check_col(pinfo->cinfo, COL_PROTOCOL))
81 col_set_str(pinfo->cinfo, COL_PROTOCOL, "VLAN");
82 if (check_col(pinfo->cinfo, COL_INFO))
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: %d CFI: %d ID: %d",
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);
99 vlan_tree = proto_item_add_subtree(ti, ett_vlan);
101 proto_tree_add_uint(vlan_tree, hf_vlan_priority, tvb, 0, 2, tci);
102 proto_tree_add_uint(vlan_tree, hf_vlan_cfi, tvb, 0, 2, tci);
103 proto_tree_add_uint(vlan_tree, hf_vlan_id, tvb, 0, 2, tci);
106 encap_proto = tvb_get_ntohs(tvb, 2);
107 if (encap_proto <= IEEE_802_3_MAX_LEN) {
108 /* Is there an 802.2 layer? I can tell by looking at the first 2
109 bytes after the VLAN header. If they are 0xffff, then what
110 follows the VLAN header is an IPX payload, meaning no 802.2.
111 (IPX/SPX is they only thing that can be contained inside a
112 straight 802.3 packet, so presumably the same applies for
113 Ethernet VLAN packets). A non-0xffff value means that there's an
114 802.2 layer inside the VLAN layer */
117 if (tvb_get_ntohs(tvb, 4) == 0xffff) {
121 CATCH2(BoundsError, ReportedBoundsError) {
127 dissect_802_3(encap_proto, is_802_2, tvb, 4, pinfo, tree, vlan_tree,
128 hf_vlan_len, hf_vlan_trailer, 0);
130 ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree,
131 hf_vlan_etype, hf_vlan_trailer, 0);
136 proto_register_vlan(void)
138 static hf_register_info hf[] = {
139 { &hf_vlan_priority, {
140 "Priority", "vlan.priority", FT_UINT16, BASE_DEC,
141 0, 0xE000, "Priority", HFILL }},
143 "CFI", "vlan.cfi", FT_UINT16, BASE_DEC,
144 0, 0x1000, "CFI", HFILL }}, /* XXX - Boolean? */
146 "ID", "vlan.id", FT_UINT16, BASE_DEC,
147 0, 0x0FFF, "ID", HFILL }},
149 "Type", "vlan.etype", FT_UINT16, BASE_HEX,
150 VALS(etype_vals), 0x0, "Type", HFILL }},
152 "Length", "vlan.len", FT_UINT16, BASE_DEC,
153 NULL, 0x0, "Length", HFILL }},
154 { &hf_vlan_trailer, {
155 "Trailer", "vlan.trailer", FT_BYTES, BASE_NONE,
156 NULL, 0x0, "VLAN Trailer", HFILL }}
158 static gint *ett[] = {
161 module_t *vlan_module;
163 proto_vlan = proto_register_protocol("802.1Q Virtual LAN", "VLAN", "vlan");
164 proto_register_field_array(proto_vlan, hf, array_length(hf));
165 proto_register_subtree_array(ett, array_length(ett));
167 vlan_module = prefs_register_protocol(proto_vlan, proto_reg_handoff_vlan);
168 prefs_register_uint_preference(vlan_module, "qinq_ethertype",
169 "802.1QinQ Ethertype",
170 "The Ethertype used to indicate 802.1QinQ VLAN in VLAN tunneling.",
171 16, &q_in_q_ethertype);
175 proto_reg_handoff_vlan(void)
177 static gboolean prefs_initialized = FALSE;
178 static dissector_handle_t vlan_handle;
180 if (!prefs_initialized)
182 vlan_handle = create_dissector_handle(dissect_vlan, proto_vlan);
183 dissector_add("ethertype", ETHERTYPE_VLAN, vlan_handle);
184 prefs_initialized = TRUE;
188 dissector_delete("ethertype", old_q_in_q_ethertype, vlan_handle);
191 old_q_in_q_ethertype = q_in_q_ethertype;
193 dissector_add("ethertype", q_in_q_ethertype, vlan_handle);