Add HP Switch Protocol SAP value
[obnox/wireshark/wip.git] / epan / dissectors / packet-vlan.c
1 /* packet-vlan.c
2  * Routines for VLAN 802.1Q ethernet header disassembly
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 <epan/etypes.h>
36 #include <epan/prefs.h>
37
38 void proto_reg_handoff_vlan(void);
39
40 static unsigned int old_q_in_q_ethertype;
41 static unsigned int q_in_q_ethertype = 0x9100;
42
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;
50
51 static gint ett_vlan = -1;
52
53 void
54 capture_vlan(const guchar *pd, int offset, int len, packet_counts *ld ) {
55   guint16 encap_proto;
56   if ( !BYTES_ARE_IN_FRAME(offset,len,5) ) {
57     ld->other++;
58     return;
59   }
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 ) {
63       capture_ipx(ld);
64     } else {
65       capture_llc(pd,offset+4,len,ld);
66     }
67   } else {
68     capture_ethertype(encap_proto, pd, offset+4, len, ld);
69   }
70 }
71
72 static void
73 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
74 {
75   proto_tree *ti;
76   guint16 tci,encap_proto;
77   volatile gboolean is_802_2;
78   proto_tree *volatile vlan_tree;
79
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);
84
85   tci = tvb_get_ntohs( tvb, 0 );
86
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));
90   }
91   if ( check_col(pinfo->cinfo, COL_8021Q_VLAN_ID)) {
92       col_add_fstr(pinfo->cinfo, COL_8021Q_VLAN_ID, "%u", (tci & 0xFFF));
93   }
94
95   vlan_tree = NULL;
96
97   if (tree) {
98     ti = proto_tree_add_item(tree, proto_vlan, tvb, 0, 4, FALSE);
99     vlan_tree = proto_item_add_subtree(ti, ett_vlan);
100
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);
104   }
105
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 */
115     is_802_2 = TRUE;
116     TRY {
117             if (tvb_get_ntohs(tvb, 4) == 0xffff) {
118               is_802_2 = FALSE;
119             }
120     }
121     CATCH2(BoundsError, ReportedBoundsError) {
122             ; /* do nothing */
123
124     }
125     ENDTRY;
126
127     dissect_802_3(encap_proto, is_802_2, tvb, 4, pinfo, tree, vlan_tree,
128                   hf_vlan_len, hf_vlan_trailer, 0);
129   } else {
130     ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree,
131                        hf_vlan_etype, hf_vlan_trailer, 0);
132   }
133 }
134
135 void
136 proto_register_vlan(void)
137 {
138   static hf_register_info hf[] = {
139         { &hf_vlan_priority, {
140                 "Priority", "vlan.priority", FT_UINT16, BASE_DEC,
141                 0, 0xE000, "Priority", HFILL }},
142         { &hf_vlan_cfi, {
143                 "CFI", "vlan.cfi", FT_UINT16, BASE_DEC,
144                 0, 0x1000, "CFI", HFILL }},     /* XXX - Boolean? */
145         { &hf_vlan_id, {
146                 "ID", "vlan.id", FT_UINT16, BASE_DEC,
147                 0, 0x0FFF, "ID", HFILL }},
148         { &hf_vlan_etype, {
149                 "Type", "vlan.etype", FT_UINT16, BASE_HEX,
150                 VALS(etype_vals), 0x0, "Type", HFILL }},
151         { &hf_vlan_len, {
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 }}
157   };
158   static gint *ett[] = {
159         &ett_vlan
160   };
161   module_t *vlan_module;
162
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));
166
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);
172 }
173
174 void
175 proto_reg_handoff_vlan(void)
176 {
177   static gboolean prefs_initialized = FALSE;
178   static dissector_handle_t vlan_handle;
179
180   if (!prefs_initialized)
181   {
182     vlan_handle = create_dissector_handle(dissect_vlan, proto_vlan);
183     dissector_add("ethertype", ETHERTYPE_VLAN, vlan_handle);
184     prefs_initialized = TRUE;
185   }
186   else
187   {
188     dissector_delete("ethertype", old_q_in_q_ethertype, vlan_handle);
189   }
190
191   old_q_in_q_ethertype = q_in_q_ethertype;
192
193   dissector_add("ethertype", q_in_q_ethertype, vlan_handle);
194 }