From Didier Gautheron:
[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 q_in_q_ethertype = 0x9100;
41
42 static gboolean vlan_summary_in_tree = TRUE;
43
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;
51
52 static gint ett_vlan = -1;
53
54 void
55 capture_vlan(const guchar *pd, int offset, int len, packet_counts *ld ) {
56   guint16 encap_proto;
57   if ( !BYTES_ARE_IN_FRAME(offset,len,5) ) {
58     ld->other++;
59     return;
60   }
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(ld);
65     } else {
66       capture_llc(pd,offset+4,len,ld);
67     }
68   } else {
69     capture_ethertype(encap_proto, pd, offset+4, len, ld);
70   }
71 }
72
73 static void
74 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
75 {
76   proto_tree *ti;
77   guint16 tci;
78   volatile guint16 encap_proto;
79   volatile gboolean is_802_2;
80   proto_tree *volatile vlan_tree;
81
82   col_set_str(pinfo->cinfo, COL_PROTOCOL, "VLAN");
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: %u  CFI: %u  ID: %u",
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
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));
103     }
104
105     vlan_tree = proto_item_add_subtree(ti, ett_vlan);
106
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);
110   }
111
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 */
121     is_802_2 = TRUE;
122     TRY {
123             if (tvb_get_ntohs(tvb, 4) == 0xffff) {
124               is_802_2 = FALSE;
125             }
126     }
127     CATCH2(BoundsError, ReportedBoundsError) {
128             ; /* do nothing */
129
130     }
131     ENDTRY;
132
133     dissect_802_3(encap_proto, is_802_2, tvb, 4, pinfo, tree, vlan_tree,
134                   hf_vlan_len, hf_vlan_trailer, 0);
135   } else {
136     ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree,
137                        hf_vlan_etype, hf_vlan_trailer, 0);
138   }
139 }
140
141 void
142 proto_register_vlan(void)
143 {
144   static hf_register_info hf[] = {
145         { &hf_vlan_priority, {
146                 "Priority", "vlan.priority", FT_UINT16, BASE_DEC,
147                 NULL, 0xE000, "User Priority", HFILL }},
148         { &hf_vlan_cfi, {
149                 "CFI", "vlan.cfi", FT_UINT16, BASE_DEC,
150                 NULL, 0x1000, "Canonical Format Identifier", HFILL }},
151         { &hf_vlan_id, {
152                 "ID", "vlan.id", FT_UINT16, BASE_DEC,
153                 NULL, 0x0FFF, "VLAN ID", HFILL }},
154         { &hf_vlan_etype, {
155                 "Type", "vlan.etype", FT_UINT16, BASE_HEX,
156                 VALS(etype_vals), 0x0, "Ethertype", HFILL }},
157         { &hf_vlan_len, {
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 }}
163   };
164   static gint *ett[] = {
165         &ett_vlan
166   };
167   module_t *vlan_module;
168
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));
172
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);
182
183 }
184
185 void
186 proto_reg_handoff_vlan(void)
187 {
188   static gboolean prefs_initialized = FALSE;
189   static dissector_handle_t vlan_handle;
190   static unsigned int old_q_in_q_ethertype;
191
192   if (!prefs_initialized)
193   {
194     vlan_handle = create_dissector_handle(dissect_vlan, proto_vlan);
195     dissector_add("ethertype", ETHERTYPE_VLAN, vlan_handle);
196     prefs_initialized = TRUE;
197   }
198   else
199   {
200     dissector_delete("ethertype", old_q_in_q_ethertype, vlan_handle);
201   }
202
203   old_q_in_q_ethertype = q_in_q_ethertype;
204
205   dissector_add("ethertype", q_in_q_ethertype, vlan_handle);
206 }