Do not use "match_strval()" unless you're prepared to check whether it
[metze/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.29 2001/01/09 09:59:28 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
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.
15  * 
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.
20  * 
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
36 #endif
37
38 #include <glib.h>
39 #include "packet.h"
40 #include "packet-ipx.h"
41 #include "packet-llc.h"
42 #include "etypes.h"
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 static dissector_handle_t ipx_handle;
55 static dissector_handle_t llc_handle;
56
57 void
58 capture_vlan(const u_char *pd, int offset, packet_counts *ld ) {
59   guint32 encap_proto;
60   if ( !BYTES_ARE_IN_FRAME(offset,5) ) {
61     ld->other++;
62     return; 
63   }
64   encap_proto = pntohs( &pd[offset+2] );
65   if ( encap_proto <= IEEE_802_3_MAX_LEN) {
66     if ( pd[offset+4] == 0xff && pd[offset+5] == 0xff ) {
67       capture_ipx(pd,offset+4,ld);
68     } else {
69       capture_llc(pd,offset+4,ld);
70     }
71   } else {
72     capture_ethertype(encap_proto, offset+4, pd, ld);
73   }
74 }
75
76 static void
77 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
78 {
79   proto_tree *ti;
80   guint16 tci,encap_proto;
81   volatile gboolean is_802_2;
82   tvbuff_t *volatile next_tvb;
83   tvbuff_t *volatile trailer_tvb;
84   proto_tree *volatile vlan_tree;
85   guint length_before, length;
86
87   CHECK_DISPLAY_AS_DATA(proto_vlan, tvb, pinfo, tree);
88
89   pinfo->current_proto = "VLAN";
90
91   if (check_col(pinfo->fd, COL_PROTOCOL))
92     col_set_str(pinfo->fd, COL_PROTOCOL, "VLAN");
93
94   tci = tvb_get_ntohs( tvb, 0 );
95   encap_proto = tvb_get_ntohs( tvb, 2 );
96
97   if (check_col(pinfo->fd, COL_INFO)) {
98     col_add_fstr(pinfo->fd, COL_INFO, "PRI: %d  CFI: %d  ID: %d",
99       (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
100   }
101
102   vlan_tree = NULL;
103
104   if (tree) {
105     ti = proto_tree_add_item(tree, proto_vlan, tvb, 0, 4, FALSE);
106     vlan_tree = proto_item_add_subtree(ti, ett_vlan);
107
108     proto_tree_add_uint(vlan_tree, hf_vlan_priority, tvb, 0, 2, tci);
109     proto_tree_add_uint(vlan_tree, hf_vlan_cfi, tvb, 0, 2, tci);
110     proto_tree_add_uint(vlan_tree, hf_vlan_id, tvb, 0, 2, tci);
111   }
112
113   if ( encap_proto <= IEEE_802_3_MAX_LEN) {
114     /* Give the next dissector only 'encap_proto' number of bytes */
115     proto_tree_add_uint(vlan_tree, hf_vlan_len, tvb, 2, 2, encap_proto);
116     TRY {
117        next_tvb = tvb_new_subset(tvb, 4, encap_proto, encap_proto);
118        trailer_tvb = tvb_new_subset(tvb, 4 + encap_proto, -1, -1);
119     }
120     CATCH2(BoundsError, ReportedBoundsError) {
121       /* Either:
122
123            the packet doesn't have "encap_proto" bytes worth of
124            captured data left in it - or it may not even have
125            "encap_proto" bytes worth of data in it, period -
126            so the "tvb_new_subset()" creating "next_tvb"
127            threw an exception
128
129          or
130
131            the packet has exactly "encap_proto" bytes worth of
132            captured data left in it, so the "tvb_new_subset()"
133            creating "trailer_tvb" threw an exception.
134
135          In either case, this means that all the data in the frame
136          is within the length value, so we give all the data to the
137          next protocol and have no trailer. */
138       next_tvb = tvb_new_subset(tvb, 4, -1, encap_proto);
139       trailer_tvb = NULL;
140     }
141     ENDTRY;
142
143     /* Is there an 802.2 layer? I can tell by looking at the first 2
144        bytes after the VLAN header. If they are 0xffff, then what
145        follows the VLAN header is an IPX payload, meaning no 802.2.
146        (IPX/SPX is they only thing that can be contained inside a
147        straight 802.3 packet, so presumably the same applies for
148        Ethernet VLAN packets). A non-0xffff value means that there's an
149        802.2 layer inside the VLAN layer */
150     is_802_2 = TRUE;
151     TRY {
152             if (tvb_get_ntohs(next_tvb, 2) == 0xffff) {
153               is_802_2 = FALSE;
154             }
155     }
156     CATCH2(BoundsError, ReportedBoundsError) {
157             ; /* do nothing */
158
159     }
160     ENDTRY;
161     if (is_802_2 ) {
162       /* 802.2 LLC */
163       call_dissector(llc_handle, next_tvb, pinfo, tree);
164     } else {
165       call_dissector(ipx_handle, next_tvb, pinfo, tree);
166     }
167   } else {
168     length_before = tvb_reported_length(tvb);
169     length = ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree,
170                        hf_vlan_etype) + 4;
171     if (length < length_before) {
172       /*
173        * Create a tvbuff for the padding.
174        */
175       TRY {
176         trailer_tvb = tvb_new_subset(tvb, length, -1, -1);
177       }
178       CATCH2(BoundsError, ReportedBoundsError) {
179         /* The packet doesn't have "length" bytes worth of captured
180            data left in it.  No trailer to display. */
181         trailer_tvb = NULL;
182       }
183       ENDTRY;
184     } else {
185       /* No padding. */
186       trailer_tvb = NULL;
187     }
188   }
189
190   /* If there's some bytes left over, mark them. */
191   if (trailer_tvb && tree) {
192     int trailer_length;
193     const guint8    *ptr;
194
195     trailer_length = tvb_length(trailer_tvb);
196     if (trailer_length > 0) {
197       ptr = tvb_get_ptr(trailer_tvb, 0, trailer_length);
198       proto_tree_add_bytes(vlan_tree, hf_vlan_trailer, trailer_tvb, 0,
199                            trailer_length, ptr);
200     }
201   }
202 }
203
204 void
205 proto_register_vlan(void)
206 {
207   static hf_register_info hf[] = {
208         { &hf_vlan_priority, { 
209                 "Priority", "vlan.priority", FT_UINT16, BASE_BIN, 
210                 0, 0xE000, "Priority" }},
211         { &hf_vlan_cfi, { 
212                 "CFI", "vlan.cfi", FT_UINT16, BASE_BIN, 
213                 0, 0x1000, "CFI" }},    /* XXX - Boolean? */
214         { &hf_vlan_id, { 
215                 "ID", "vlan.id", FT_UINT16, BASE_BIN, 
216                 0, 0x0FFF, "ID" }},
217         { &hf_vlan_etype, { 
218                 "Type", "vlan.etype", FT_UINT16, BASE_HEX, 
219                 VALS(etype_vals), 0x0, "Type" }},
220         { &hf_vlan_len, {
221                 "Length", "vlan.len", FT_UINT16, BASE_DEC,
222                 NULL, 0x0, "Length" }},
223         { &hf_vlan_trailer, {
224                 "Trailer", "vlan.trailer", FT_BYTES, BASE_NONE,
225                 NULL, 0x0, "VLAN Trailer" }}
226   };
227   static gint *ett[] = {
228         &ett_vlan,
229   };
230
231   proto_vlan = proto_register_protocol("802.1q Virtual LAN", "VLAN", "vlan");
232   proto_register_field_array(proto_vlan, hf, array_length(hf));
233   proto_register_subtree_array(ett, array_length(ett));
234 }
235
236 void
237 proto_reg_handoff_vlan(void)
238 {
239   /*
240    * Get handles for the IPX and LLC dissectors.
241    */
242   llc_handle = find_dissector("llc");
243   ipx_handle = find_dissector("ipx");
244
245   dissector_add("ethertype", ETHERTYPE_VLAN, dissect_vlan, proto_vlan);
246 }