add2d59832e943a9fc6da441b000b8b095ec99c3
[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.3 1999/11/10 05:42:06 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
41 static int proto_vlan = -1;
42 static int hf_vlan_etype = -1;
43 static int hf_vlan_priority = -1;
44 static int hf_vlan_id = -1;
45 static int hf_vlan_cfi = -1;
46
47 void
48 dissect_vlan(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
49   proto_tree *ti, *vlan_tree = NULL;
50   guint16 tci,encap_proto;
51
52   if (!BYTES_ARE_IN_FRAME(offset, 2*sizeof(guint16))) {
53     return;
54   }
55
56   if (check_col(fd, COL_PROTOCOL))
57     col_add_str(fd, COL_PROTOCOL, "VLAN");
58
59   tci = pntohs( &pd[offset] );
60   encap_proto = pntohs( &pd[offset+2] );
61
62   if (check_col(fd, COL_INFO)) {
63     col_add_fstr(fd, COL_INFO, "PRI: %d  CFI: %d  ID: %d",
64       (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
65   }
66
67   if (tree) {
68     ti = proto_tree_add_item(tree, proto_vlan, offset, 4);
69     vlan_tree = proto_item_add_subtree(ti, ETT_VLAN);
70
71     proto_tree_add_item(vlan_tree, hf_vlan_priority, offset, 2, tci);
72     proto_tree_add_item(vlan_tree, hf_vlan_cfi, offset, 2, tci);
73     proto_tree_add_item(vlan_tree, hf_vlan_id, offset, 2, tci);
74   }
75
76   ethertype(encap_proto, offset+4, pd, fd, tree, vlan_tree, hf_vlan_etype);
77 }
78
79 void
80 proto_register_vlan(void)
81 {
82   static hf_register_info hf[] = {
83         { &hf_vlan_etype, { 
84                 "Type", "vlan.etype", FT_UINT16, BASE_HEX, 
85                 VALS(etype_vals), 0x0, "Type" }},
86         { &hf_vlan_priority, { 
87                 "Priority", "vlan.priority", FT_UINT16, BASE_BIN, 
88                 0, 0xE000, "Priority" }},
89         { &hf_vlan_cfi, { 
90                 "CFI", "vlan.cfi", FT_UINT16, BASE_BIN, 
91                 0, 0x1000, "CFI" }},
92         { &hf_vlan_id, { 
93                 "ID", "vlan.id", FT_UINT16, BASE_BIN, 
94                 0, 0x0FFF, "ID" }},
95   };
96
97   proto_vlan = proto_register_protocol("802.1q Virtual LAN", "vlan");
98   proto_register_field_array(proto_vlan, hf, array_length(hf));
99 }