proto_registrar_get_nth(hfinfo->id) == hfinfo, so use the latter rather
[obnox/wireshark/wip.git] / packet-eapol.c
1 /* packet-eapol.c
2  * Routines for EAPOL 802.1X authentication header disassembly
3  * (From IEEE Draft P802.1X/D11; is there a later draft, or a
4  * final standard?  If so, check it.)
5  *
6  * $Id: packet-eapol.c,v 1.9 2002/03/11 08:47:46 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #ifdef HAVE_NETINET_IN_H
36 # include <netinet/in.h>
37 #endif
38
39 #include <glib.h>
40 #include <epan/packet.h>
41 #include "packet-ieee8023.h"
42 #include "packet-ipx.h"
43 #include "packet-llc.h"
44 #include "etypes.h"
45
46 static int proto_eapol = -1;
47 static int hf_eapol_version = -1;
48 static int hf_eapol_type = -1;
49 static int hf_eapol_len = -1;
50 static int hf_eapol_keydes_type = -1;
51 static int hf_eapol_keydes_keylen = -1;
52 static int hf_eapol_keydes_replay_counter = -1;
53 static int hf_eapol_keydes_key_iv = -1;
54 static int hf_eapol_keydes_key_index_keytype = -1;
55 static int hf_eapol_keydes_key_index_indexnum = -1;
56 static int hf_eapol_keydes_key_signature = -1;
57 static int hf_eapol_keydes_key = -1;
58
59 static gint ett_eapol = -1;
60 static gint ett_eapol_key_index = -1;
61
62 static dissector_handle_t eap_handle;
63 static dissector_handle_t data_handle;
64
65 #define EAPOL_HDR_LEN   4
66
67 #define EAP_PACKET              0
68 #define EAPOL_START             1
69 #define EAPOL_LOGOFF            2
70 #define EAPOL_KEY               3
71 #define EAPOL_ENCAP_ASF_ALERT   4
72
73 static const value_string eapol_type_vals[] = { 
74     { EAP_PACKET,            "EAP Packet" },
75     { EAPOL_START,           "Start" },
76     { EAPOL_LOGOFF,          "Logoff" },
77     { EAPOL_KEY,             "Key" },
78     { EAPOL_ENCAP_ASF_ALERT, "Encapsulated ASF Alert" },
79     { 0,                     NULL }
80 };
81
82 static const value_string eapol_keydes_type_vals[] = {
83         { 1, "RC4 Descriptor" },
84         { 0, NULL }
85 };
86
87 static const true_false_string keytype_tfs =
88         { "Unicast", "Broadcast" };
89
90 static void
91 dissect_eapol(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
92 {
93   int         offset = 0;
94   guint8      eapol_ver;
95   guint8      eapol_type;
96   guint16     eapol_len;
97   guint       len;
98   guint16     eapol_key_len;
99   guint8      key_index;
100   proto_tree *ti = NULL;
101   proto_tree *eapol_tree = NULL;
102   proto_tree *key_index_tree;
103   tvbuff_t   *next_tvb;
104
105   if (check_col(pinfo->cinfo, COL_PROTOCOL))
106     col_set_str(pinfo->cinfo, COL_PROTOCOL, "EAPOL");
107   if (check_col(pinfo->cinfo, COL_INFO))
108     col_clear(pinfo->cinfo, COL_INFO);
109
110   if (tree) {
111     ti = proto_tree_add_item(tree, proto_eapol, tvb, 0, -1, FALSE);
112     eapol_tree = proto_item_add_subtree(ti, ett_eapol);
113
114     proto_tree_add_item(eapol_tree, hf_eapol_version, tvb, offset, 1, FALSE);
115   }
116   offset++;
117
118   eapol_type = tvb_get_guint8(tvb, offset);
119   if (tree)
120     proto_tree_add_uint(eapol_tree, hf_eapol_type, tvb, offset, 1, eapol_type);
121   if (check_col(pinfo->cinfo, COL_INFO))
122     col_add_str(pinfo->cinfo, COL_INFO,
123                 val_to_str(eapol_type, eapol_type_vals, "Unknown type (0x%02X)"));
124   offset++;
125
126   eapol_len = tvb_get_ntohs(tvb, offset);
127   len = EAPOL_HDR_LEN + eapol_len;
128   set_actual_length(tvb, len);
129   if (tree) {
130     proto_item_set_len(ti, len);
131     proto_tree_add_uint(eapol_tree, hf_eapol_len, tvb, offset, 2, eapol_len);
132   }
133   offset += 2;
134
135   switch (eapol_type) {
136
137   case EAP_PACKET:
138     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
139     call_dissector(eap_handle, next_tvb, pinfo, eapol_tree);
140     break;
141
142   case EAPOL_KEY:
143     if (tree) {
144       proto_tree_add_item(eapol_tree, hf_eapol_keydes_type, tvb, offset, 1, FALSE);
145       offset += 1;
146       eapol_key_len = tvb_get_ntohs(tvb, offset);
147       proto_tree_add_uint(eapol_tree, hf_eapol_keydes_keylen, tvb, offset, 2, eapol_key_len);
148       offset += 2;
149       proto_tree_add_item(eapol_tree, hf_eapol_keydes_replay_counter, tvb,
150                           offset, 8, FALSE);
151       offset += 8;
152       proto_tree_add_item(eapol_tree, hf_eapol_keydes_key_iv, tvb,
153                           offset, 16, FALSE);
154       offset += 16;
155       key_index = tvb_get_guint8(tvb, offset);
156       ti = proto_tree_add_text(eapol_tree, tvb, offset, 1,
157                                "Key Index: %s, index %u",
158                                (key_index & 0x80) ? "unicast" : "broadcast",
159                                key_index & 0x7F);
160       key_index_tree = proto_item_add_subtree(ti, ett_eapol_key_index);
161       proto_tree_add_boolean(eapol_tree, hf_eapol_keydes_key_index_keytype,
162                              tvb, offset, 1, key_index);
163       proto_tree_add_uint(eapol_tree, hf_eapol_keydes_key_index_indexnum,
164                              tvb, offset, 1, key_index);
165       offset += 1;
166       proto_tree_add_item(eapol_tree, hf_eapol_keydes_key_signature, tvb,
167                           offset, 16, FALSE);
168       offset += 16;
169       if (eapol_key_len != 0)
170         proto_tree_add_item(eapol_tree, hf_eapol_keydes_key, tvb, offset,
171                             eapol_key_len, FALSE);
172     }
173     break;
174
175   case EAPOL_ENCAP_ASF_ALERT:   /* XXX - is this an SNMP trap? */
176   default:
177     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
178     call_dissector(data_handle, next_tvb, pinfo, eapol_tree);
179     break;
180   }
181 }
182
183 void
184 proto_register_eapol(void)
185 {
186   static hf_register_info hf[] = {
187         { &hf_eapol_version, { 
188                 "Version", "eapol.version", FT_UINT8, BASE_DEC, 
189                 NULL, 0x0, "", HFILL }},
190         { &hf_eapol_type, { 
191                 "Type", "eapol.type", FT_UINT8, BASE_DEC, 
192                 VALS(eapol_type_vals), 0x0, "", HFILL }},
193         { &hf_eapol_len, {
194                 "Length", "eapol.len", FT_UINT16, BASE_DEC,
195                 NULL, 0x0, "Length", HFILL }},
196         { &hf_eapol_keydes_type, {
197                 "Descriptor Type", "eapol.keydes.type", FT_UINT8, BASE_DEC,
198                 VALS(eapol_keydes_type_vals), 0x0, "Key Descriptor Type", HFILL }},
199         { &hf_eapol_keydes_keylen, {
200                 "Key Length", "eapol.keydes.keylen", FT_UINT16, BASE_DEC,
201                 NULL, 0x0, "Key Length", HFILL }},
202         { &hf_eapol_keydes_replay_counter, {
203                 "Replay Counter", "eapol.keydes.replay_counter", FT_UINT64, BASE_DEC,
204                 NULL, 0x0, "Replay Counter", HFILL }},
205         { &hf_eapol_keydes_key_iv, {
206                 "Key IV", "eapol.keydes.key_iv", FT_BYTES, BASE_NONE,
207                 NULL, 0x0, "Key Initialization Vector", HFILL }},
208         { &hf_eapol_keydes_key_index_keytype, {
209                 "Key Type", "eapol.keydes.index.keytype", FT_BOOLEAN, 8,
210                 TFS(&keytype_tfs), 0x80, "Key Type (unicast/broadcast)", HFILL }},
211         { &hf_eapol_keydes_key_index_indexnum, {
212                 "Index Number", "eapol.keydes.index.indexnum", FT_UINT8, BASE_DEC,
213                 NULL, 0x7F, "Key Index number", HFILL }},
214         { &hf_eapol_keydes_key_signature, {
215                 "Key Signature", "eapol.keydes.key_signature", FT_BYTES, BASE_NONE,
216                 NULL, 0x0, "Key Signature", HFILL }},
217         { &hf_eapol_keydes_key, {
218                 "Key", "eapol.keydes.key", FT_BYTES, BASE_NONE,
219                 NULL, 0x0, "Key", HFILL }},
220   };
221   static gint *ett[] = {
222         &ett_eapol,
223         &ett_eapol_key_index
224   };
225
226   proto_eapol = proto_register_protocol("802.1x Authentication", "EAPOL", "eapol");
227   proto_register_field_array(proto_eapol, hf, array_length(hf));
228   proto_register_subtree_array(ett, array_length(ett));
229 }
230
231 void
232 proto_reg_handoff_eapol(void)
233 {
234   dissector_handle_t eapol_handle;
235
236   /*
237    * Get handles for the EAP and raw data dissectors.
238    */
239   eap_handle = find_dissector("eap");
240   data_handle = find_dissector("data");
241
242   eapol_handle = create_dissector_handle(dissect_eapol, proto_eapol);
243   dissector_add("ethertype", ETHERTYPE_EAPOL, eapol_handle);
244 }