#if 0 out the stuff to set the reported length, as it'd throw an
[obnox/wireshark/wip.git] / packet-vlan.c
1 /* packet-vlan.c
2  * Routines for VLAN 802.1Q ethernet header disassembly
3  *
4  * $Id: packet-vlan.c,v 1.43 2003/10/01 07:11:45 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
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 "etypes.h"
36
37 static int proto_vlan = -1;
38 static int hf_vlan_priority = -1;
39 static int hf_vlan_cfi = -1;
40 static int hf_vlan_id = -1;
41 static int hf_vlan_etype = -1;
42 static int hf_vlan_len = -1;
43 static int hf_vlan_trailer = -1;
44
45 static gint ett_vlan = -1;
46
47 void
48 capture_vlan(const guchar *pd, int offset, int len, packet_counts *ld ) {
49   guint16 encap_proto;
50   if ( !BYTES_ARE_IN_FRAME(offset,len,5) ) {
51     ld->other++;
52     return;
53   }
54   encap_proto = pntohs( &pd[offset+2] );
55   if ( encap_proto <= IEEE_802_3_MAX_LEN) {
56     if ( pd[offset+4] == 0xff && pd[offset+5] == 0xff ) {
57       capture_ipx(ld);
58     } else {
59       capture_llc(pd,offset+4,len,ld);
60     }
61   } else {
62     capture_ethertype(encap_proto, pd, offset+4, len, ld);
63   }
64 }
65
66 static void
67 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
68 {
69   proto_tree *ti;
70   guint16 tci,encap_proto;
71   volatile gboolean is_802_2;
72   proto_tree *volatile vlan_tree;
73
74   if (check_col(pinfo->cinfo, COL_PROTOCOL))
75     col_set_str(pinfo->cinfo, COL_PROTOCOL, "VLAN");
76   if (check_col(pinfo->cinfo, COL_INFO))
77     col_clear(pinfo->cinfo, COL_INFO);
78
79   tci = tvb_get_ntohs( tvb, 0 );
80
81   if (check_col(pinfo->cinfo, COL_INFO)) {
82     col_add_fstr(pinfo->cinfo, COL_INFO, "PRI: %d  CFI: %d  ID: %d",
83       (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
84   }
85
86   vlan_tree = NULL;
87
88   if (tree) {
89     ti = proto_tree_add_item(tree, proto_vlan, tvb, 0, 4, FALSE);
90     vlan_tree = proto_item_add_subtree(ti, ett_vlan);
91
92     proto_tree_add_uint(vlan_tree, hf_vlan_priority, tvb, 0, 2, tci);
93     proto_tree_add_uint(vlan_tree, hf_vlan_cfi, tvb, 0, 2, tci);
94     proto_tree_add_uint(vlan_tree, hf_vlan_id, tvb, 0, 2, tci);
95   }
96
97   encap_proto = tvb_get_ntohs(tvb, 2);
98   if (encap_proto <= IEEE_802_3_MAX_LEN) {
99     /* Is there an 802.2 layer? I can tell by looking at the first 2
100        bytes after the VLAN header. If they are 0xffff, then what
101        follows the VLAN header is an IPX payload, meaning no 802.2.
102        (IPX/SPX is they only thing that can be contained inside a
103        straight 802.3 packet, so presumably the same applies for
104        Ethernet VLAN packets). A non-0xffff value means that there's an
105        802.2 layer inside the VLAN layer */
106     is_802_2 = TRUE;
107     TRY {
108             if (tvb_get_ntohs(tvb, 4) == 0xffff) {
109               is_802_2 = FALSE;
110             }
111     }
112     CATCH2(BoundsError, ReportedBoundsError) {
113             ; /* do nothing */
114
115     }
116     ENDTRY;
117
118     dissect_802_3(encap_proto, is_802_2, tvb, 4, pinfo, tree, vlan_tree,
119                   hf_vlan_len, hf_vlan_trailer, 0);
120   } else {
121     ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree,
122                        hf_vlan_etype, hf_vlan_trailer, 0);
123   }
124 }
125
126 void
127 proto_register_vlan(void)
128 {
129   static hf_register_info hf[] = {
130         { &hf_vlan_priority, {
131                 "Priority", "vlan.priority", FT_UINT16, BASE_DEC,
132                 0, 0xE000, "Priority", HFILL }},
133         { &hf_vlan_cfi, {
134                 "CFI", "vlan.cfi", FT_UINT16, BASE_DEC,
135                 0, 0x1000, "CFI", HFILL }},     /* XXX - Boolean? */
136         { &hf_vlan_id, {
137                 "ID", "vlan.id", FT_UINT16, BASE_DEC,
138                 0, 0x0FFF, "ID", HFILL }},
139         { &hf_vlan_etype, {
140                 "Type", "vlan.etype", FT_UINT16, BASE_HEX,
141                 VALS(etype_vals), 0x0, "Type", HFILL }},
142         { &hf_vlan_len, {
143                 "Length", "vlan.len", FT_UINT16, BASE_DEC,
144                 NULL, 0x0, "Length", HFILL }},
145         { &hf_vlan_trailer, {
146                 "Trailer", "vlan.trailer", FT_BYTES, BASE_NONE,
147                 NULL, 0x0, "VLAN Trailer", HFILL }}
148   };
149   static gint *ett[] = {
150         &ett_vlan,
151   };
152
153   proto_vlan = proto_register_protocol("802.1q Virtual LAN", "VLAN", "vlan");
154   proto_register_field_array(proto_vlan, hf, array_length(hf));
155   proto_register_subtree_array(ett, array_length(ett));
156 }
157
158 void
159 proto_reg_handoff_vlan(void)
160 {
161   dissector_handle_t vlan_handle;
162
163   vlan_handle = create_dissector_handle(dissect_vlan, proto_vlan);
164   dissector_add("ethertype", ETHERTYPE_VLAN, vlan_handle);
165 }