New tap for tethereal: io statistics that provides frames/bytes counts for frames...
[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.12 2002/08/28 21:00:13 jmayer 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-ieee8023.h"
34 #include "packet-ipx.h"
35 #include "packet-llc.h"
36 #include "etypes.h"
37
38 static int proto_eapol = -1;
39 static int hf_eapol_version = -1;
40 static int hf_eapol_type = -1;
41 static int hf_eapol_len = -1;
42 static int hf_eapol_keydes_type = -1;
43 static int hf_eapol_keydes_keylen = -1;
44 static int hf_eapol_keydes_replay_counter = -1;
45 static int hf_eapol_keydes_key_iv = -1;
46 static int hf_eapol_keydes_key_index_keytype = -1;
47 static int hf_eapol_keydes_key_index_indexnum = -1;
48 static int hf_eapol_keydes_key_signature = -1;
49 static int hf_eapol_keydes_key = -1;
50
51 static gint ett_eapol = -1;
52 static gint ett_eapol_key_index = -1;
53
54 static dissector_handle_t eap_handle;
55 static dissector_handle_t data_handle;
56
57 #define EAPOL_HDR_LEN   4
58
59 #define EAP_PACKET              0
60 #define EAPOL_START             1
61 #define EAPOL_LOGOFF            2
62 #define EAPOL_KEY               3
63 #define EAPOL_ENCAP_ASF_ALERT   4
64
65 static const value_string eapol_type_vals[] = {
66     { EAP_PACKET,            "EAP Packet" },
67     { EAPOL_START,           "Start" },
68     { EAPOL_LOGOFF,          "Logoff" },
69     { EAPOL_KEY,             "Key" },
70     { EAPOL_ENCAP_ASF_ALERT, "Encapsulated ASF Alert" },
71     { 0,                     NULL }
72 };
73
74 static const value_string eapol_keydes_type_vals[] = {
75         { 1, "RC4 Descriptor" },
76         { 0, NULL }
77 };
78
79 static const true_false_string keytype_tfs =
80         { "Unicast", "Broadcast" };
81
82 static void
83 dissect_eapol(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
84 {
85   int         offset = 0;
86   guint8      eapol_type;
87   guint16     eapol_len;
88   guint       len;
89   guint16     eapol_key_len;
90   guint8      key_index;
91   proto_tree *ti = NULL;
92   proto_tree *eapol_tree = NULL;
93   proto_tree *key_index_tree;
94   tvbuff_t   *next_tvb;
95
96   if (check_col(pinfo->cinfo, COL_PROTOCOL))
97     col_set_str(pinfo->cinfo, COL_PROTOCOL, "EAPOL");
98   if (check_col(pinfo->cinfo, COL_INFO))
99     col_clear(pinfo->cinfo, COL_INFO);
100
101   if (tree) {
102     ti = proto_tree_add_item(tree, proto_eapol, tvb, 0, -1, FALSE);
103     eapol_tree = proto_item_add_subtree(ti, ett_eapol);
104
105     proto_tree_add_item(eapol_tree, hf_eapol_version, tvb, offset, 1, FALSE);
106   }
107   offset++;
108
109   eapol_type = tvb_get_guint8(tvb, offset);
110   if (tree)
111     proto_tree_add_uint(eapol_tree, hf_eapol_type, tvb, offset, 1, eapol_type);
112   if (check_col(pinfo->cinfo, COL_INFO))
113     col_add_str(pinfo->cinfo, COL_INFO,
114                 val_to_str(eapol_type, eapol_type_vals, "Unknown type (0x%02X)"));
115   offset++;
116
117   eapol_len = tvb_get_ntohs(tvb, offset);
118   len = EAPOL_HDR_LEN + eapol_len;
119   set_actual_length(tvb, len);
120   if (tree) {
121     proto_item_set_len(ti, len);
122     proto_tree_add_uint(eapol_tree, hf_eapol_len, tvb, offset, 2, eapol_len);
123   }
124   offset += 2;
125
126   switch (eapol_type) {
127
128   case EAP_PACKET:
129     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
130     call_dissector(eap_handle, next_tvb, pinfo, eapol_tree);
131     break;
132
133   case EAPOL_KEY:
134     if (tree) {
135       proto_tree_add_item(eapol_tree, hf_eapol_keydes_type, tvb, offset, 1, FALSE);
136       offset += 1;
137       eapol_key_len = tvb_get_ntohs(tvb, offset);
138       proto_tree_add_uint(eapol_tree, hf_eapol_keydes_keylen, tvb, offset, 2, eapol_key_len);
139       offset += 2;
140       proto_tree_add_item(eapol_tree, hf_eapol_keydes_replay_counter, tvb,
141                           offset, 8, FALSE);
142       offset += 8;
143       proto_tree_add_item(eapol_tree, hf_eapol_keydes_key_iv, tvb,
144                           offset, 16, FALSE);
145       offset += 16;
146       key_index = tvb_get_guint8(tvb, offset);
147       ti = proto_tree_add_text(eapol_tree, tvb, offset, 1,
148                                "Key Index: %s, index %u",
149                                (key_index & 0x80) ? "unicast" : "broadcast",
150                                key_index & 0x7F);
151       key_index_tree = proto_item_add_subtree(ti, ett_eapol_key_index);
152       proto_tree_add_boolean(eapol_tree, hf_eapol_keydes_key_index_keytype,
153                              tvb, offset, 1, key_index);
154       proto_tree_add_uint(eapol_tree, hf_eapol_keydes_key_index_indexnum,
155                              tvb, offset, 1, key_index);
156       offset += 1;
157       proto_tree_add_item(eapol_tree, hf_eapol_keydes_key_signature, tvb,
158                           offset, 16, FALSE);
159       offset += 16;
160       if (eapol_key_len != 0)
161         proto_tree_add_item(eapol_tree, hf_eapol_keydes_key, tvb, offset,
162                             eapol_key_len, FALSE);
163     }
164     break;
165
166   case EAPOL_ENCAP_ASF_ALERT:   /* XXX - is this an SNMP trap? */
167   default:
168     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
169     call_dissector(data_handle, next_tvb, pinfo, eapol_tree);
170     break;
171   }
172 }
173
174 void
175 proto_register_eapol(void)
176 {
177   static hf_register_info hf[] = {
178         { &hf_eapol_version, {
179                 "Version", "eapol.version", FT_UINT8, BASE_DEC,
180                 NULL, 0x0, "", HFILL }},
181         { &hf_eapol_type, {
182                 "Type", "eapol.type", FT_UINT8, BASE_DEC,
183                 VALS(eapol_type_vals), 0x0, "", HFILL }},
184         { &hf_eapol_len, {
185                 "Length", "eapol.len", FT_UINT16, BASE_DEC,
186                 NULL, 0x0, "Length", HFILL }},
187         { &hf_eapol_keydes_type, {
188                 "Descriptor Type", "eapol.keydes.type", FT_UINT8, BASE_DEC,
189                 VALS(eapol_keydes_type_vals), 0x0, "Key Descriptor Type", HFILL }},
190         { &hf_eapol_keydes_keylen, {
191                 "Key Length", "eapol.keydes.keylen", FT_UINT16, BASE_DEC,
192                 NULL, 0x0, "Key Length", HFILL }},
193         { &hf_eapol_keydes_replay_counter, {
194                 "Replay Counter", "eapol.keydes.replay_counter", FT_UINT64, BASE_DEC,
195                 NULL, 0x0, "Replay Counter", HFILL }},
196         { &hf_eapol_keydes_key_iv, {
197                 "Key IV", "eapol.keydes.key_iv", FT_BYTES, BASE_NONE,
198                 NULL, 0x0, "Key Initialization Vector", HFILL }},
199         { &hf_eapol_keydes_key_index_keytype, {
200                 "Key Type", "eapol.keydes.index.keytype", FT_BOOLEAN, 8,
201                 TFS(&keytype_tfs), 0x80, "Key Type (unicast/broadcast)", HFILL }},
202         { &hf_eapol_keydes_key_index_indexnum, {
203                 "Index Number", "eapol.keydes.index.indexnum", FT_UINT8, BASE_DEC,
204                 NULL, 0x7F, "Key Index number", HFILL }},
205         { &hf_eapol_keydes_key_signature, {
206                 "Key Signature", "eapol.keydes.key_signature", FT_BYTES, BASE_NONE,
207                 NULL, 0x0, "Key Signature", HFILL }},
208         { &hf_eapol_keydes_key, {
209                 "Key", "eapol.keydes.key", FT_BYTES, BASE_NONE,
210                 NULL, 0x0, "Key", HFILL }},
211   };
212   static gint *ett[] = {
213         &ett_eapol,
214         &ett_eapol_key_index
215   };
216
217   proto_eapol = proto_register_protocol("802.1x Authentication", "EAPOL", "eapol");
218   proto_register_field_array(proto_eapol, hf, array_length(hf));
219   proto_register_subtree_array(ett, array_length(ett));
220 }
221
222 void
223 proto_reg_handoff_eapol(void)
224 {
225   dissector_handle_t eapol_handle;
226
227   /*
228    * Get handles for the EAP and raw data dissectors.
229    */
230   eap_handle = find_dissector("eap");
231   data_handle = find_dissector("data");
232
233   eapol_handle = create_dissector_handle(dissect_eapol, proto_eapol);
234   dissector_add("ethertype", ETHERTYPE_EAPOL, eapol_handle);
235 }