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