From Matthijs Melchior: check whether the ring buffer timeout has
[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.14 2003/09/23 02:35:59 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 #include <glib.h>
32 #include <epan/packet.h>
33 #include "packet-ieee80211.h"
34 #include "etypes.h"
35
36 static int proto_eapol = -1;
37 static int hf_eapol_version = -1;
38 static int hf_eapol_type = -1;
39 static int hf_eapol_len = -1;
40 static int hf_eapol_keydes_type = -1;
41 static int hf_eapol_keydes_keylen = -1;
42 static int hf_eapol_keydes_replay_counter = -1;
43 static int hf_eapol_keydes_key_iv = -1;
44 static int hf_eapol_keydes_key_index_keytype = -1;
45 static int hf_eapol_keydes_key_index_indexnum = -1;
46 static int hf_eapol_keydes_key_signature = -1;
47 static int hf_eapol_keydes_key = -1;
48
49 static int hf_eapol_wpa_keydes_keyinfo = -1;
50 static int hf_eapol_wpa_keydes_nonce = -1;
51 static int hf_eapol_wpa_keydes_rsc = -1;
52 static int hf_eapol_wpa_keydes_id = -1;
53 static int hf_eapol_wpa_keydes_mic = -1;
54 static int hf_eapol_wpa_keydes_datalen = -1;
55 static int hf_eapol_wpa_keydes_data = -1;
56
57 static gint ett_eapol = -1;
58 static gint ett_eapol_keydes_data = -1;
59 static gint ett_eapol_key_index = -1;
60
61 static dissector_handle_t eap_handle;
62 static dissector_handle_t data_handle;
63
64 #define EAPOL_HDR_LEN   4
65
66 #define EAP_PACKET              0
67 #define EAPOL_START             1
68 #define EAPOL_LOGOFF            2
69 #define EAPOL_KEY               3
70 #define EAPOL_ENCAP_ASF_ALERT   4
71
72 #define EAPOL_WPA_KEY           254
73
74 static const value_string eapol_type_vals[] = {
75     { EAP_PACKET,            "EAP Packet" },
76     { EAPOL_START,           "Start" },
77     { EAPOL_LOGOFF,          "Logoff" },
78     { EAPOL_KEY,             "Key" },
79     { EAPOL_ENCAP_ASF_ALERT, "Encapsulated ASF Alert" },
80     { 0,                     NULL }
81 };
82
83 static const value_string eapol_keydes_type_vals[] = {
84         { 1, "RC4 Descriptor" },
85         { EAPOL_WPA_KEY, "EAPOL WPA key" },
86         { 0, NULL }
87 };
88
89 static const true_false_string keytype_tfs =
90         { "Unicast", "Broadcast" };
91
92 static void
93 dissect_eapol(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
94 {
95   int         offset = 0;
96   guint8      eapol_type;
97   guint8      keydesc_type;
98   guint16     eapol_len;
99   guint       len;
100   guint16     eapol_key_len, eapol_data_len;
101   guint8      key_index;
102   proto_tree *ti = NULL;
103   proto_tree *eapol_tree = NULL;
104   proto_tree *key_index_tree, *keydes_tree;
105   tvbuff_t   *next_tvb;
106
107   if (check_col(pinfo->cinfo, COL_PROTOCOL))
108     col_set_str(pinfo->cinfo, COL_PROTOCOL, "EAPOL");
109   if (check_col(pinfo->cinfo, COL_INFO))
110     col_clear(pinfo->cinfo, COL_INFO);
111
112   if (tree) {
113     ti = proto_tree_add_item(tree, proto_eapol, tvb, 0, -1, FALSE);
114     eapol_tree = proto_item_add_subtree(ti, ett_eapol);
115
116     proto_tree_add_item(eapol_tree, hf_eapol_version, tvb, offset, 1, FALSE);
117   }
118   offset++;
119
120   eapol_type = tvb_get_guint8(tvb, offset);
121   if (tree)
122     proto_tree_add_uint(eapol_tree, hf_eapol_type, tvb, offset, 1, eapol_type);
123   if (check_col(pinfo->cinfo, COL_INFO))
124     col_add_str(pinfo->cinfo, COL_INFO,
125                 val_to_str(eapol_type, eapol_type_vals, "Unknown type (0x%02X)"));
126   offset++;
127
128   eapol_len = tvb_get_ntohs(tvb, offset);
129   len = EAPOL_HDR_LEN + eapol_len;
130   set_actual_length(tvb, len);
131   if (tree) {
132     proto_item_set_len(ti, len);
133     proto_tree_add_uint(eapol_tree, hf_eapol_len, tvb, offset, 2, eapol_len);
134   }
135   offset += 2;
136
137   switch (eapol_type) {
138
139   case EAP_PACKET:
140     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
141     call_dissector(eap_handle, next_tvb, pinfo, eapol_tree);
142     break;
143
144   case EAPOL_KEY:
145     if (tree) {
146       keydesc_type = tvb_get_guint8(tvb, offset);
147       proto_tree_add_item(eapol_tree, hf_eapol_keydes_type, tvb, offset, 1, FALSE);
148       offset += 1;
149       if (keydesc_type == EAPOL_WPA_KEY) {
150         proto_tree_add_uint(eapol_tree, hf_eapol_wpa_keydes_keyinfo, tvb,
151                             offset, 2, tvb_get_ntohs(tvb, offset));
152         offset += 2;
153         proto_tree_add_uint(eapol_tree, hf_eapol_keydes_keylen, tvb, offset,
154                             2, tvb_get_ntohs(tvb, offset));
155         offset += 2;
156         proto_tree_add_item(eapol_tree, hf_eapol_keydes_replay_counter, tvb,
157                             offset, 8, FALSE);
158         offset += 8;
159         proto_tree_add_item(eapol_tree, hf_eapol_wpa_keydes_nonce, tvb, offset,
160                             32, FALSE);
161         offset += 32;
162         proto_tree_add_item(eapol_tree, hf_eapol_keydes_key_iv, tvb,
163                             offset, 16, FALSE);
164         offset += 16;
165         proto_tree_add_item(eapol_tree, hf_eapol_wpa_keydes_rsc, tvb, offset,
166                             8, FALSE);
167         offset += 8;
168         proto_tree_add_item(eapol_tree, hf_eapol_wpa_keydes_id, tvb, offset, 8,
169                             FALSE);
170         offset += 8;
171         proto_tree_add_item(eapol_tree, hf_eapol_wpa_keydes_mic, tvb, offset,
172                             16, FALSE);
173         offset += 16;
174         eapol_data_len = tvb_get_ntohs(tvb, offset);
175         proto_tree_add_uint(eapol_tree, hf_eapol_wpa_keydes_datalen, tvb,
176                             offset, 2, eapol_data_len);
177         offset += 2;
178         if (eapol_data_len != 0) {
179           ti = proto_tree_add_item(eapol_tree, hf_eapol_wpa_keydes_data,
180                 tvb, offset, eapol_data_len, FALSE);
181           keydes_tree = proto_item_add_subtree(ti, ett_eapol_keydes_data);
182           ieee_80211_add_tagged_parameters(tvb, offset, keydes_tree,
183                 eapol_data_len);
184         }
185       }
186       else {
187         eapol_key_len = tvb_get_ntohs(tvb, offset);
188         proto_tree_add_uint(eapol_tree, hf_eapol_keydes_keylen, tvb, offset, 2, eapol_key_len);
189         offset += 2;
190         proto_tree_add_item(eapol_tree, hf_eapol_keydes_replay_counter, tvb,
191                           offset, 8, FALSE);
192         offset += 8;
193         proto_tree_add_item(eapol_tree, hf_eapol_keydes_key_iv, tvb,
194                           offset, 16, FALSE);
195         offset += 16;
196         key_index = tvb_get_guint8(tvb, offset);
197         ti = proto_tree_add_text(eapol_tree, tvb, offset, 1,
198                                "Key Index: %s, index %u",
199                                (key_index & 0x80) ? "unicast" : "broadcast",
200                                key_index & 0x7F);
201         key_index_tree = proto_item_add_subtree(ti, ett_eapol_key_index);
202         proto_tree_add_boolean(eapol_tree, hf_eapol_keydes_key_index_keytype,
203                              tvb, offset, 1, key_index);
204         proto_tree_add_uint(eapol_tree, hf_eapol_keydes_key_index_indexnum,
205                              tvb, offset, 1, key_index);
206         offset += 1;
207         proto_tree_add_item(eapol_tree, hf_eapol_keydes_key_signature, tvb,
208                           offset, 16, FALSE);
209         offset += 16;
210         if (eapol_key_len != 0)
211           proto_tree_add_item(eapol_tree, hf_eapol_keydes_key, tvb, offset,
212                             eapol_key_len, FALSE);
213       }
214     }
215     break;
216
217   case EAPOL_ENCAP_ASF_ALERT:   /* XXX - is this an SNMP trap? */
218   default:
219     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
220     call_dissector(data_handle, next_tvb, pinfo, eapol_tree);
221     break;
222   }
223 }
224
225 void
226 proto_register_eapol(void)
227 {
228   static hf_register_info hf[] = {
229         { &hf_eapol_version, {
230                 "Version", "eapol.version", FT_UINT8, BASE_DEC,
231                 NULL, 0x0, "", HFILL }},
232         { &hf_eapol_type, {
233                 "Type", "eapol.type", FT_UINT8, BASE_DEC,
234                 VALS(eapol_type_vals), 0x0, "", HFILL }},
235         { &hf_eapol_len, {
236                 "Length", "eapol.len", FT_UINT16, BASE_DEC,
237                 NULL, 0x0, "Length", HFILL }},
238         { &hf_eapol_keydes_type, {
239                 "Descriptor Type", "eapol.keydes.type", FT_UINT8, BASE_DEC,
240                 VALS(eapol_keydes_type_vals), 0x0, "Key Descriptor Type", HFILL }},
241         { &hf_eapol_keydes_keylen, {
242                 "Key Length", "eapol.keydes.keylen", FT_UINT16, BASE_DEC,
243                 NULL, 0x0, "Key Length", HFILL }},
244         { &hf_eapol_keydes_replay_counter, {
245                 "Replay Counter", "eapol.keydes.replay_counter", FT_UINT64, BASE_DEC,
246                 NULL, 0x0, "Replay Counter", HFILL }},
247         { &hf_eapol_keydes_key_iv, {
248                 "Key IV", "eapol.keydes.key_iv", FT_BYTES, BASE_NONE,
249                 NULL, 0x0, "Key Initialization Vector", HFILL }},
250         { &hf_eapol_keydes_key_index_keytype, {
251                 "Key Type", "eapol.keydes.index.keytype", FT_BOOLEAN, 8,
252                 TFS(&keytype_tfs), 0x80, "Key Type (unicast/broadcast)", HFILL }},
253         { &hf_eapol_keydes_key_index_indexnum, {
254                 "Index Number", "eapol.keydes.index.indexnum", FT_UINT8, BASE_DEC,
255                 NULL, 0x7F, "Key Index number", HFILL }},
256         { &hf_eapol_keydes_key_signature, {
257                 "Key Signature", "eapol.keydes.key_signature", FT_BYTES, BASE_NONE,
258                 NULL, 0x0, "Key Signature", HFILL }},
259         { &hf_eapol_keydes_key, {
260                 "Key", "eapol.keydes.key", FT_BYTES, BASE_NONE,
261                 NULL, 0x0, "Key", HFILL }},
262
263         { &hf_eapol_wpa_keydes_keyinfo, {
264                 "Key Information", "eapol.keydes.key_info", FT_UINT16,
265                 BASE_HEX, NULL, 0x0, "WPA key info", HFILL }},
266         { &hf_eapol_wpa_keydes_nonce, {
267                 "Nonce", "eapol.keydes.nonce", FT_BYTES, BASE_NONE,
268                 NULL, 0x0, "WPA Key Nonce", HFILL }},
269         { &hf_eapol_wpa_keydes_rsc, {
270                 "WPA Key RSC", "eapol.keydes.rsc", FT_BYTES, BASE_NONE, NULL,
271                 0x0, "WPA Key Receive Sequence Counter", HFILL }},
272         { &hf_eapol_wpa_keydes_id, {
273                 "WPA Key ID", "eapol,keydes.id", FT_BYTES, BASE_NONE, NULL,
274                 0x0, "WPA Key ID", HFILL }},
275         { &hf_eapol_wpa_keydes_mic, {
276                 "WPA Key MIC", "eapol.keydes.mic", FT_BYTES, BASE_NONE, NULL,
277                 0x0, "WPA Key Message Integrity Check", HFILL }},
278         { &hf_eapol_wpa_keydes_datalen, {
279                 "WPA Key Length", "eapol.keydes.datalen", FT_UINT16, BASE_DEC,
280                 NULL, 0x0, "WPA Key Data Length", HFILL }},
281         { &hf_eapol_wpa_keydes_data, {
282                 "WPA Key", "eapol.keydes.data", FT_BYTES, BASE_NONE,
283                 NULL, 0x0, "WPA Key Data", HFILL }},
284   };
285   static gint *ett[] = {
286         &ett_eapol,
287         &ett_eapol_keydes_data,
288         &ett_eapol_key_index
289   };
290
291   proto_eapol = proto_register_protocol("802.1x Authentication", "EAPOL", "eapol");
292   proto_register_field_array(proto_eapol, hf, array_length(hf));
293   proto_register_subtree_array(ett, array_length(ett));
294 }
295
296 void
297 proto_reg_handoff_eapol(void)
298 {
299   dissector_handle_t eapol_handle;
300
301   /*
302    * Get handles for the EAP and raw data dissectors.
303    */
304   eap_handle = find_dissector("eap");
305   data_handle = find_dissector("data");
306
307   eapol_handle = create_dissector_handle(dissect_eapol, proto_eapol);
308   dissector_add("ethertype", ETHERTYPE_EAPOL, eapol_handle);
309 }