Tvbuffify the 802.1Q VLAN dissector.
[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.20 2000/11/12 05:43:26 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_etype = -1;
46 static int hf_vlan_priority = -1;
47 static int hf_vlan_id = -1;
48 static int hf_vlan_cfi = -1;
49
50 static gint ett_vlan = -1;
51
52 void
53 capture_vlan(const u_char *pd, int offset, packet_counts *ld ) {
54   guint32 encap_proto;
55   if ( !BYTES_ARE_IN_FRAME(offset,5) ) {
56     ld->other++;
57     return; 
58   }
59   encap_proto = pntohs( &pd[offset+2] );
60   if ( encap_proto <= IEEE_802_3_MAX_LEN) {
61     if ( pd[offset+4] == 0xff && pd[offset+5] == 0xff ) {
62       capture_ipx(pd,offset+4,ld);
63     } else {
64       capture_llc(pd,offset+4,ld);
65     }
66   } else {
67     capture_ethertype(encap_proto, offset+4, pd, ld);
68   }
69 }
70
71 static void
72 dissect_vlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
73   proto_tree *ti, *vlan_tree = NULL;
74   guint16 tci,encap_proto;
75   tvbuff_t *next_tvb;
76
77   CHECK_DISPLAY_AS_DATA(proto_vlan, tvb, pinfo, tree);
78
79   pinfo->current_proto = "VLAN";
80
81   if (check_col(pinfo->fd, COL_PROTOCOL))
82     col_add_str(pinfo->fd, COL_PROTOCOL, "VLAN");
83
84   tci = tvb_get_ntohs( tvb, 0 );
85   encap_proto = tvb_get_ntohs( tvb, 2 );
86
87   if (check_col(pinfo->fd, COL_INFO)) {
88     col_add_fstr(pinfo->fd, COL_INFO, "PRI: %d  CFI: %d  ID: %d",
89       (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
90   }
91
92   if (tree) {
93     ti = proto_tree_add_item(tree, proto_vlan, tvb, 0, 4, FALSE);
94     vlan_tree = proto_item_add_subtree(ti, ett_vlan);
95
96     proto_tree_add_uint(vlan_tree, hf_vlan_priority, tvb, 0, 2, tci);
97     proto_tree_add_uint(vlan_tree, hf_vlan_cfi, tvb, 0, 2, tci);
98     proto_tree_add_uint(vlan_tree, hf_vlan_id, tvb, 0, 2, tci);
99   }
100
101   if ( encap_proto <= IEEE_802_3_MAX_LEN) {
102     next_tvb = tvb_new_subset(tvb, 4, -1, -1); /* XXX - should TRY() like dissect_eth() */
103     if ( tvb_get_ntohs(next_tvb, 2) == 0xffff ) {
104       dissect_ipx(next_tvb, pinfo, tree);
105     } else {
106       dissect_llc(next_tvb, pinfo, tree);
107     }
108   } else {
109     ethertype(encap_proto, tvb, 4, pinfo, tree, vlan_tree, hf_vlan_etype);
110   }
111 }
112
113 void
114 proto_register_vlan(void)
115 {
116   static hf_register_info hf[] = {
117         { &hf_vlan_etype, { 
118                 "Type", "vlan.etype", FT_UINT16, BASE_HEX, 
119                 VALS(etype_vals), 0x0, "Type" }},
120         { &hf_vlan_priority, { 
121                 "Priority", "vlan.priority", FT_UINT16, BASE_BIN, 
122                 0, 0xE000, "Priority" }},
123         { &hf_vlan_cfi, { 
124                 "CFI", "vlan.cfi", FT_UINT16, BASE_BIN, 
125                 0, 0x1000, "CFI" }},
126         { &hf_vlan_id, { 
127                 "ID", "vlan.id", FT_UINT16, BASE_BIN, 
128                 0, 0x0FFF, "ID" }},
129   };
130   static gint *ett[] = {
131         &ett_vlan,
132   };
133
134   proto_vlan = proto_register_protocol("802.1q Virtual LAN", "vlan");
135   proto_register_field_array(proto_vlan, hf, array_length(hf));
136   proto_register_subtree_array(ett, array_length(ett));
137 }
138
139 void
140 proto_reg_handoff_vlan(void)
141 {
142   dissector_add("ethertype", ETHERTYPE_VLAN, dissect_vlan);
143 }