Clean up the descriptions of reassembly preferences.
[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.38 2002/01/21 07:36:44 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 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32
33 #ifdef HAVE_NETINET_IN_H
34 # include <netinet/in.h>
35 #endif
36
37 #include <glib.h>
38 #include <epan/packet.h>
39 #include "packet-ieee8023.h"
40 #include "packet-ipx.h"
41 #include "packet-llc.h"
42 #include "packet-vlan.h"
43 #include "etypes.h"
44
45 static int proto_vlan = -1;
46 static int hf_vlan_priority = -1;
47 static int hf_vlan_cfi = -1;
48 static int hf_vlan_id = -1;
49 static int hf_vlan_etype = -1;
50 static int hf_vlan_len = -1;
51 static int hf_vlan_trailer = -1;
52
53 static gint ett_vlan = -1;
54
55 void
56 capture_vlan(const u_char *pd, int offset, int len, packet_counts *ld ) {
57   guint16 encap_proto;
58   if ( !BYTES_ARE_IN_FRAME(offset,len,5) ) {
59     ld->other++;
60     return; 
61   }
62   encap_proto = pntohs( &pd[offset+2] );
63   if ( encap_proto <= IEEE_802_3_MAX_LEN) {
64     if ( pd[offset+4] == 0xff && pd[offset+5] == 0xff ) {
65       capture_ipx(pd,offset+4,len,ld);
66     } else {
67       capture_llc(pd,offset+4,len,ld);
68     }
69   } else {
70     capture_ethertype(encap_proto, pd, offset+4, len, ld);
71   }
72 }
73
74 static void
75 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
76 {
77   proto_tree *ti;
78   guint16 tci,encap_proto;
79   volatile gboolean is_802_2;
80   proto_tree *volatile vlan_tree;
81
82   if (check_col(pinfo->cinfo, COL_PROTOCOL))
83     col_set_str(pinfo->cinfo, COL_PROTOCOL, "VLAN");
84   if (check_col(pinfo->cinfo, COL_INFO))
85     col_clear(pinfo->cinfo, COL_INFO);
86
87   tci = tvb_get_ntohs( tvb, 0 );
88
89   if (check_col(pinfo->cinfo, COL_INFO)) {
90     col_add_fstr(pinfo->cinfo, COL_INFO, "PRI: %d  CFI: %d  ID: %d",
91       (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
92   }
93
94   vlan_tree = NULL;
95
96   if (tree) {
97     ti = proto_tree_add_item(tree, proto_vlan, tvb, 0, 4, FALSE);
98     vlan_tree = proto_item_add_subtree(ti, ett_vlan);
99
100     proto_tree_add_uint(vlan_tree, hf_vlan_priority, tvb, 0, 2, tci);
101     proto_tree_add_uint(vlan_tree, hf_vlan_cfi, tvb, 0, 2, tci);
102     proto_tree_add_uint(vlan_tree, hf_vlan_id, tvb, 0, 2, tci);
103   }
104
105   encap_proto = tvb_get_ntohs(tvb, 2);
106   if (encap_proto <= IEEE_802_3_MAX_LEN) {
107     /* Is there an 802.2 layer? I can tell by looking at the first 2
108        bytes after the VLAN header. If they are 0xffff, then what
109        follows the VLAN header is an IPX payload, meaning no 802.2.
110        (IPX/SPX is they only thing that can be contained inside a
111        straight 802.3 packet, so presumably the same applies for
112        Ethernet VLAN packets). A non-0xffff value means that there's an
113        802.2 layer inside the VLAN layer */
114     is_802_2 = TRUE;
115     TRY {
116             if (tvb_get_ntohs(tvb, 4) == 0xffff) {
117               is_802_2 = FALSE;
118             }
119     }
120     CATCH2(BoundsError, ReportedBoundsError) {
121             ; /* do nothing */
122
123     }
124     ENDTRY;
125
126     dissect_802_3(encap_proto, is_802_2, tvb, 4, pinfo, tree, vlan_tree,
127                   hf_vlan_len, hf_vlan_trailer);
128   } else {
129     ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree,
130                        hf_vlan_etype, hf_vlan_trailer);
131   }
132 }
133
134 void
135 proto_register_vlan(void)
136 {
137   static hf_register_info hf[] = {
138         { &hf_vlan_priority, { 
139                 "Priority", "vlan.priority", FT_UINT16, BASE_BIN, 
140                 0, 0xE000, "Priority", HFILL }},
141         { &hf_vlan_cfi, { 
142                 "CFI", "vlan.cfi", FT_UINT16, BASE_BIN, 
143                 0, 0x1000, "CFI", HFILL }},     /* XXX - Boolean? */
144         { &hf_vlan_id, { 
145                 "ID", "vlan.id", FT_UINT16, BASE_BIN, 
146                 0, 0x0FFF, "ID", HFILL }},
147         { &hf_vlan_etype, { 
148                 "Type", "vlan.etype", FT_UINT16, BASE_HEX, 
149                 VALS(etype_vals), 0x0, "Type", HFILL }},
150         { &hf_vlan_len, {
151                 "Length", "vlan.len", FT_UINT16, BASE_DEC,
152                 NULL, 0x0, "Length", HFILL }},
153         { &hf_vlan_trailer, {
154                 "Trailer", "vlan.trailer", FT_BYTES, BASE_NONE,
155                 NULL, 0x0, "VLAN Trailer", HFILL }}
156   };
157   static gint *ett[] = {
158         &ett_vlan,
159   };
160
161   proto_vlan = proto_register_protocol("802.1q Virtual LAN", "VLAN", "vlan");
162   proto_register_field_array(proto_vlan, hf, array_length(hf));
163   proto_register_subtree_array(ett, array_length(ett));
164 }
165
166 void
167 proto_reg_handoff_vlan(void)
168 {
169   dissector_handle_t vlan_handle;
170
171   vlan_handle = create_dissector_handle(dissect_vlan, proto_vlan);
172   dissector_add("ethertype", ETHERTYPE_VLAN, vlan_handle);
173 }