2 * Routines for VLAN 802.1Q ethernet header disassembly
4 * $Id: packet-vlan.c,v 1.25 2000/11/19 08:54:10 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;
55 capture_vlan(const u_char *pd, int offset, packet_counts *ld ) {
57 if ( !BYTES_ARE_IN_FRAME(offset,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 ) {
64 capture_ipx(pd,offset+4,ld);
66 capture_llc(pd,offset+4,ld);
69 capture_ethertype(encap_proto, offset+4, pd, ld);
74 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
77 guint16 tci,encap_proto;
78 volatile gboolean is_802_2;
79 tvbuff_t *volatile next_tvb;
80 tvbuff_t *volatile trailer_tvb;
81 proto_tree *volatile vlan_tree;
82 guint length_before, length;
84 CHECK_DISPLAY_AS_DATA(proto_vlan, tvb, pinfo, tree);
86 pinfo->current_proto = "VLAN";
88 if (check_col(pinfo->fd, COL_PROTOCOL))
89 col_set_str(pinfo->fd, COL_PROTOCOL, "VLAN");
91 tci = tvb_get_ntohs( tvb, 0 );
92 encap_proto = tvb_get_ntohs( tvb, 2 );
94 if (check_col(pinfo->fd, COL_INFO)) {
95 col_add_fstr(pinfo->fd, COL_INFO, "PRI: %d CFI: %d ID: %d",
96 (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
102 ti = proto_tree_add_item(tree, proto_vlan, tvb, 0, 4, FALSE);
103 vlan_tree = proto_item_add_subtree(ti, ett_vlan);
105 proto_tree_add_uint(vlan_tree, hf_vlan_priority, tvb, 0, 2, tci);
106 proto_tree_add_uint(vlan_tree, hf_vlan_cfi, tvb, 0, 2, tci);
107 proto_tree_add_uint(vlan_tree, hf_vlan_id, tvb, 0, 2, tci);
110 if ( encap_proto <= IEEE_802_3_MAX_LEN) {
111 /* Give the next dissector only 'encap_proto' number of bytes */
112 proto_tree_add_uint(vlan_tree, hf_vlan_len, tvb, 2, 2, encap_proto);
114 next_tvb = tvb_new_subset(tvb, 4, encap_proto, encap_proto);
115 trailer_tvb = tvb_new_subset(tvb, 4 + encap_proto, -1, -1);
117 CATCH2(BoundsError, ReportedBoundsError) {
120 the packet doesn't have "encap_proto" bytes worth of
121 captured data left in it - or it may not even have
122 "encap_proto" bytes worth of data in it, period -
123 so the "tvb_new_subset()" creating "next_tvb"
128 the packet has exactly "encap_proto" bytes worth of
129 captured data left in it, so the "tvb_new_subset()"
130 creating "trailer_tvb" threw an exception.
132 In either case, this means that all the data in the frame
133 is within the length value, so we give all the data to the
134 next protocol and have no trailer. */
135 next_tvb = tvb_new_subset(tvb, 4, -1, encap_proto);
140 /* Is there an 802.2 layer? I can tell by looking at the first 2
141 bytes after the VLAN header. If they are 0xffff, then what
142 follows the VLAN header is an IPX payload, meaning no 802.2.
143 (IPX/SPX is they only thing that can be contained inside a
144 straight 802.3 packet, so presumably the same applies for
145 Ethernet VLAN packets). A non-0xffff value means that there's an
146 802.2 layer inside the VLAN layer */
149 if (tvb_get_ntohs(next_tvb, 2) == 0xffff) {
153 CATCH2(BoundsError, ReportedBoundsError) {
160 dissect_llc(next_tvb, pinfo, tree);
162 dissect_ipx(next_tvb, pinfo, tree);
165 length_before = tvb_reported_length(tvb);
166 length = ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree,
168 if (length < length_before) {
170 * Create a tvbuff for the padding.
173 trailer_tvb = tvb_new_subset(tvb, length, -1, -1);
175 CATCH2(BoundsError, ReportedBoundsError) {
176 /* The packet doesn't have "length" bytes worth of captured
177 data left in it. No trailer to display. */
187 /* If there's some bytes left over, mark them. */
188 if (trailer_tvb && tree) {
192 trailer_length = tvb_length(trailer_tvb);
193 if (trailer_length > 0) {
194 ptr = tvb_get_ptr(trailer_tvb, 0, trailer_length);
195 proto_tree_add_bytes(vlan_tree, hf_vlan_trailer, trailer_tvb, 0,
196 trailer_length, ptr);
202 proto_register_vlan(void)
204 static hf_register_info hf[] = {
205 { &hf_vlan_priority, {
206 "Priority", "vlan.priority", FT_UINT16, BASE_BIN,
207 0, 0xE000, "Priority" }},
209 "CFI", "vlan.cfi", FT_UINT16, BASE_BIN,
210 0, 0x1000, "CFI" }}, /* XXX - Boolean? */
212 "ID", "vlan.id", FT_UINT16, BASE_BIN,
215 "Type", "vlan.etype", FT_UINT16, BASE_HEX,
216 VALS(etype_vals), 0x0, "Type" }},
218 "Length", "vlan.len", FT_UINT16, BASE_DEC,
219 NULL, 0x0, "Length" }},
220 { &hf_vlan_trailer, {
221 "Trailer", "vlan.trailer", FT_BYTES, BASE_NONE,
222 NULL, 0x0, "VLAN Trailer" }}
224 static gint *ett[] = {
228 proto_vlan = proto_register_protocol("802.1q Virtual LAN", "vlan");
229 proto_register_field_array(proto_vlan, hf, array_length(hf));
230 proto_register_subtree_array(ett, array_length(ett));
234 proto_reg_handoff_vlan(void)
236 dissector_add("ethertype", ETHERTYPE_VLAN, dissect_vlan);