Make work better the (hexdump) popup menus.
[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.19 2000/08/13 14:09:06 deniel 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(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
73   proto_tree *ti, *vlan_tree = NULL;
74   guint16 tci,encap_proto;
75   tvbuff_t *next_tvb;
76
77   OLD_CHECK_DISPLAY_AS_DATA(proto_vlan, pd, offset, fd, tree);
78
79   if (!BYTES_ARE_IN_FRAME(offset, 2*sizeof(guint16))) {
80     return;
81   }
82
83   if (check_col(fd, COL_PROTOCOL))
84     col_add_str(fd, COL_PROTOCOL, "VLAN");
85
86   tci = pntohs( &pd[offset] );
87   encap_proto = pntohs( &pd[offset+2] );
88
89   if (check_col(fd, COL_INFO)) {
90     col_add_fstr(fd, COL_INFO, "PRI: %d  CFI: %d  ID: %d",
91       (tci >> 13), ((tci >> 12) & 1), (tci & 0xFFF));
92   }
93
94   if (tree) {
95     ti = proto_tree_add_item(tree, proto_vlan, NullTVB, offset, 4, FALSE);
96     vlan_tree = proto_item_add_subtree(ti, ett_vlan);
97
98     proto_tree_add_uint(vlan_tree, hf_vlan_priority, NullTVB, offset, 2, tci);
99     proto_tree_add_uint(vlan_tree, hf_vlan_cfi, NullTVB, offset, 2, tci);
100     proto_tree_add_uint(vlan_tree, hf_vlan_id, NullTVB, offset, 2, tci);
101   }
102
103   next_tvb = tvb_create_from_top(offset+4); /* XXX - should TRY() like dissect_eth() */
104   if ( encap_proto <= IEEE_802_3_MAX_LEN) {
105     if ( pd[offset+4] == 0xff && pd[offset+5] == 0xff ) {
106       dissect_ipx(next_tvb, &pi, tree);
107     } else {
108       dissect_llc(next_tvb, &pi, tree);
109     }
110   } else {
111     ethertype(encap_proto, pi.compat_top_tvb, offset+4, &pi, tree, vlan_tree, hf_vlan_etype);
112   }
113 }
114
115 void
116 proto_register_vlan(void)
117 {
118   static hf_register_info hf[] = {
119         { &hf_vlan_etype, { 
120                 "Type", "vlan.etype", FT_UINT16, BASE_HEX, 
121                 VALS(etype_vals), 0x0, "Type" }},
122         { &hf_vlan_priority, { 
123                 "Priority", "vlan.priority", FT_UINT16, BASE_BIN, 
124                 0, 0xE000, "Priority" }},
125         { &hf_vlan_cfi, { 
126                 "CFI", "vlan.cfi", FT_UINT16, BASE_BIN, 
127                 0, 0x1000, "CFI" }},
128         { &hf_vlan_id, { 
129                 "ID", "vlan.id", FT_UINT16, BASE_BIN, 
130                 0, 0x0FFF, "ID" }},
131   };
132   static gint *ett[] = {
133         &ett_vlan,
134   };
135
136   proto_vlan = proto_register_protocol("802.1q Virtual LAN", "vlan");
137   proto_register_field_array(proto_vlan, hf, array_length(hf));
138   proto_register_subtree_array(ett, array_length(ett));
139 }
140
141 void
142 proto_reg_handoff_vlan(void)
143 {
144         old_dissector_add("ethertype", ETHERTYPE_VLAN, dissect_vlan);
145 }