Include files from the "epan" directory and subdirectories thereof with
[obnox/wireshark/wip.git] / packet-eapol.c
1 /* packet-eapol.c
2  * Routines for EAPOL 802.1X authentication header disassembly
3  *
4  * $Id: packet-eapol.c,v 1.5 2002/01/21 07:36:34 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32
33 #ifdef HAVE_NETINET_IN_H
34 # include <netinet/in.h>
35 #endif
36
37 #include <glib.h>
38 #include <epan/packet.h>
39 #include "packet-ieee8023.h"
40 #include "packet-ipx.h"
41 #include "packet-llc.h"
42 #include "etypes.h"
43
44 static int proto_eapol = -1;
45 static int hf_eapol_version = -1;
46 static int hf_eapol_type = -1;
47 static int hf_eapol_len = -1;
48
49 static gint ett_eapol = -1;
50
51 static dissector_handle_t data_handle;
52
53 typedef struct _e_eapol
54 {
55     guint8 eapol_ver;
56     guint8 eapol_type;
57     guint16 eapol_len;
58 } e_eapol;
59
60 static const char *eapol_type_name[] = { 
61     "EAP",
62     "Start",
63     "Logoff",
64     "Key",
65     "Encapsulated ASF Alert"
66 };
67 #define EAPOL_TYPE_COUNT (sizeof(eapol_type_name)/sizeof(eapol_type_name[0]))
68
69 extern void dissect_eap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
70
71 static void
72 dissect_eapol(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
73 {
74   e_eapol     eapolh;
75   guint       len;
76   proto_tree *ti;
77   proto_tree *volatile eapol_tree;
78   tvbuff_t   *next_tvb;
79
80   if (check_col(pinfo->cinfo, COL_PROTOCOL))
81     col_set_str(pinfo->cinfo, COL_PROTOCOL, "EAPOL");
82   if (check_col(pinfo->cinfo, COL_INFO))
83     col_clear(pinfo->cinfo, COL_INFO);
84
85   tvb_memcpy(tvb, (guint8 *)&eapolh, 0, sizeof(eapolh));
86   eapolh.eapol_len = ntohs(eapolh.eapol_len);
87
88   len = sizeof(eapolh) + eapolh.eapol_len;
89
90   set_actual_length(tvb, pinfo, len);
91
92   eapol_tree = NULL;
93
94   if (tree) {
95     ti = proto_tree_add_item(tree, proto_eapol, tvb, 0, len, FALSE);
96     eapol_tree = proto_item_add_subtree(ti, ett_eapol);
97
98     proto_tree_add_uint(eapol_tree, hf_eapol_version, tvb, 0, 1, eapolh.eapol_ver);
99     proto_tree_add_text(eapol_tree, tvb, 1, 1, "Type: %s (%u)", 
100                         eapolh.eapol_type > EAPOL_TYPE_COUNT?
101                         "Unknown" : eapol_type_name[eapolh.eapol_type],
102                         eapolh.eapol_type);
103     proto_tree_add_uint(eapol_tree, hf_eapol_len,    tvb, 2, 2, eapolh.eapol_len);
104   }
105
106   next_tvb = tvb_new_subset(tvb, 4, -1, -1);
107
108   if (eapolh.eapol_type == 0 && next_tvb != NULL) 
109       dissect_eap(next_tvb, pinfo, eapol_tree);
110   else
111       call_dissector(data_handle,tvb_new_subset(tvb, 4,-1,tvb_reported_length_remaining(tvb,4)), pinfo, tree);
112 }
113
114 void
115 proto_register_eapol(void)
116 {
117   static hf_register_info hf[] = {
118         { &hf_eapol_version, { 
119                 "Version", "eapol.version", FT_UINT8, BASE_DEC, 
120                 NULL, 0x0, "", HFILL }},
121         { &hf_eapol_type, { 
122                 "Type", "eapol.type", FT_UINT8, BASE_DEC, 
123                 0, 0x0, "", HFILL }},
124         { &hf_eapol_len, {
125                 "Length", "eapol.len", FT_UINT16, BASE_DEC,
126                 NULL, 0x0, "Length", HFILL }},
127   };
128   static gint *ett[] = {
129         &ett_eapol,
130   };
131
132   proto_eapol = proto_register_protocol("802.1x Authentication", "EAPOL", "eapol");
133   proto_register_field_array(proto_eapol, hf, array_length(hf));
134   proto_register_subtree_array(ett, array_length(ett));
135 }
136
137 void
138 proto_reg_handoff_eapol(void)
139 {
140   dissector_handle_t eapol_handle;
141
142   data_handle = find_dissector("data");
143   eapol_handle = create_dissector_handle(dissect_eapol, proto_eapol);
144   dissector_add("ethertype", ETHERTYPE_EAPOL, eapol_handle);
145 }