0f89eba724a49b60d85450e56ab0d1052a2bbe36
[obnox/wireshark/wip.git] / packet-eap.c
1 /* packet-eap.c
2  * Routines for EAP Extensible Authentication Protocol header disassembly
3  *
4  * $Id: packet-eap.c,v 1.2 2001/11/26 04:52:49 hagbard 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 "packet.h"
39 #include "packet-ieee8023.h"
40 #include "packet-ipx.h"
41 #include "packet-llc.h"
42 #include "etypes.h"
43 #include "ppptypes.h"
44
45 static int proto_eap = -1;
46 static int hf_eap_code = -1;
47 static int hf_eap_identifier = -1;
48 static int hf_eap_len = -1;
49 static int hf_eap_type = -1;
50
51 static gint ett_eap = -1;
52
53 static dissector_handle_t data_handle;
54
55 typedef struct _e_eap {
56     guint8 eap_code;
57     guint8 eap_id;
58     guint16 eap_len;
59 } e_eap;
60
61 static const char *eap_code_name[] = { 
62     "Undefined",
63     "Request",
64     "Response",
65     "Success",
66     "Failure",
67 };
68 #define EAP_CODE_COUNT (sizeof(eap_code_name)/sizeof(eap_code_name[0]))
69
70 static const char *eap_type_name[] = { 
71     "Undefined",
72     "Identity",
73     "Nak (Response only)",
74     "MD5-Challenge",
75     "One-Time Password",
76     "Generic Token Card",
77 };
78 #define EAP_TYPE_COUNT (sizeof(eap_type_name)/sizeof(eap_type_name[0]))
79
80
81 void
82 dissect_eap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
83 {
84   e_eap       eaph;
85   guint       len;
86   proto_tree *ti;
87   proto_tree *volatile eap_tree;
88
89   if (check_col(pinfo->fd, COL_PROTOCOL))
90     col_set_str(pinfo->fd, COL_PROTOCOL, "EAP");
91   if (check_col(pinfo->fd, COL_INFO))
92     col_clear(pinfo->fd, COL_INFO);
93
94   tvb_memcpy(tvb, (guint8 *)&eaph, 0, sizeof(eaph));
95   eaph.eap_len = ntohs(eaph.eap_len);
96
97   len = eaph.eap_len;
98
99   set_actual_length(tvb, pinfo, len);
100
101   eap_tree = NULL;
102
103   if (tree) {
104     ti = proto_tree_add_item(tree, proto_eap, tvb, 0, len, FALSE);
105     eap_tree = proto_item_add_subtree(ti, ett_eap);
106
107     proto_tree_add_text(eap_tree, tvb, 0, 0, "Code: %s (%u) ", 
108                         eaph.eap_code > EAP_CODE_COUNT?
109                         "Unknown": eap_code_name[eaph.eap_code],
110                         eaph.eap_code);
111
112     proto_tree_add_uint(eap_tree, hf_eap_identifier, tvb, 1, 1, eaph.eap_id);
113     proto_tree_add_uint(eap_tree, hf_eap_len,    tvb, 2, 2, eaph.eap_len);
114
115     if (len > 4) {
116         guint8 eap_type = tvb_get_guint8(tvb, 4);
117         proto_tree_add_text(eap_tree, tvb, 4, 1, "Type: %s (%u)", 
118                             eap_type > EAP_TYPE_COUNT?
119                             "Unknown" : eap_type_name[eap_type],
120                             eap_type);
121     }
122     if (len > 5)
123       call_dissector(data_handle,tvb_new_subset(tvb, 5,-1,tvb_reported_length_remaining(tvb,5)), pinfo, tree);
124   }
125 }
126
127 void
128 proto_register_eap(void)
129 {
130   static hf_register_info hf[] = {
131         { &hf_eap_code, { 
132                 "Code", "eap.code", FT_UINT8, BASE_DEC, 
133                 NULL, 0x0, "", HFILL }},
134         { &hf_eap_identifier, {
135                 "Id", "eap.id", FT_UINT8, BASE_DEC,
136                 NULL, 0x0, "", HFILL }},
137         { &hf_eap_len, {
138                 "Length", "eap.len", FT_UINT16, BASE_DEC,
139                 NULL, 0x0, "", HFILL }},
140         { &hf_eap_type, { 
141                 "Type", "eap.type", FT_UINT8, BASE_DEC, 
142                 NULL, 0x0, "", HFILL }},
143   };
144   static gint *ett[] = {
145         &ett_eap,
146   };
147
148   proto_eap = proto_register_protocol("Extensible Authentication Protocol", 
149                                       "EAP", "eap");
150   proto_register_field_array(proto_eap, hf, array_length(hf));
151   proto_register_subtree_array(ett, array_length(ett));
152 }
153
154 void
155 proto_reg_handoff_eap(void)
156 {
157   data_handle = find_dissector("data");
158   dissector_add("ppp.protocol", PPP_EAP, dissect_eap, proto_eap);
159 }