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