2 * WPA Supplicant - test code for pre-authentication
3 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
14 * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
15 * Not used in production version.
23 #include "eapol_supp/eapol_supp_sm.h"
26 #include "eap_peer/eap.h"
27 #include "wpa_supplicant_i.h"
28 #include "l2_packet/l2_packet.h"
29 #include "ctrl_iface.h"
30 #include "pcsc_funcs.h"
32 #include "pmksa_cache.h"
35 extern int wpa_debug_level
;
36 extern int wpa_debug_show_keys
;
38 struct wpa_driver_ops
*wpa_supplicant_drivers
[] = { NULL
};
41 struct preauth_test_data
{
46 static void _wpa_supplicant_disassociate(void *wpa_s
, int reason_code
)
48 wpa_supplicant_disassociate(wpa_s
, reason_code
);
52 static void _wpa_supplicant_deauthenticate(void *wpa_s
, int reason_code
)
54 wpa_supplicant_deauthenticate(wpa_s
, reason_code
);
58 static u8
* wpa_alloc_eapol(const struct wpa_supplicant
*wpa_s
, u8 type
,
59 const void *data
, u16 data_len
,
60 size_t *msg_len
, void **data_pos
)
62 struct ieee802_1x_hdr
*hdr
;
64 *msg_len
= sizeof(*hdr
) + data_len
;
65 hdr
= os_malloc(*msg_len
);
69 hdr
->version
= wpa_s
->conf
->eapol_version
;
71 hdr
->length
= htons(data_len
);
74 os_memcpy(hdr
+ 1, data
, data_len
);
76 os_memset(hdr
+ 1, 0, data_len
);
85 static u8
* _wpa_alloc_eapol(void *wpa_s
, u8 type
,
86 const void *data
, u16 data_len
,
87 size_t *msg_len
, void **data_pos
)
89 return wpa_alloc_eapol(wpa_s
, type
, data
, data_len
, msg_len
, data_pos
);
93 static void _wpa_supplicant_set_state(void *ctx
, wpa_states state
)
95 struct wpa_supplicant
*wpa_s
= ctx
;
96 wpa_s
->wpa_state
= state
;
100 static wpa_states
_wpa_supplicant_get_state(void *ctx
)
102 struct wpa_supplicant
*wpa_s
= ctx
;
103 return wpa_s
->wpa_state
;
107 static int wpa_ether_send(void *wpa_s
, const u8
*dest
, u16 proto
,
108 const u8
*buf
, size_t len
)
110 printf("%s - not implemented\n", __func__
);
115 static void * wpa_supplicant_get_network_ctx(void *wpa_s
)
117 return wpa_supplicant_get_ssid(wpa_s
);
121 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s
)
123 wpa_supplicant_cancel_auth_timeout(wpa_s
);
127 static int wpa_supplicant_get_beacon_ie(void *wpa_s
)
129 printf("%s - not implemented\n", __func__
);
134 static int wpa_supplicant_get_bssid(void *wpa_s
, u8
*bssid
)
136 printf("%s - not implemented\n", __func__
);
141 static int wpa_supplicant_set_key(void *wpa_s
, wpa_alg alg
,
142 const u8
*addr
, int key_idx
, int set_tx
,
143 const u8
*seq
, size_t seq_len
,
144 const u8
*key
, size_t key_len
)
146 printf("%s - not implemented\n", __func__
);
151 static int wpa_supplicant_mlme_setprotection(void *wpa_s
, const u8
*addr
,
155 printf("%s - not implemented\n", __func__
);
160 static int wpa_supplicant_add_pmkid(void *wpa_s
,
161 const u8
*bssid
, const u8
*pmkid
)
163 printf("%s - not implemented\n", __func__
);
168 static int wpa_supplicant_remove_pmkid(void *wpa_s
,
169 const u8
*bssid
, const u8
*pmkid
)
171 printf("%s - not implemented\n", __func__
);
176 static void wpa_supplicant_set_config_blob(void *ctx
,
177 struct wpa_config_blob
*blob
)
179 struct wpa_supplicant
*wpa_s
= ctx
;
180 wpa_config_set_blob(wpa_s
->conf
, blob
);
184 static const struct wpa_config_blob
*
185 wpa_supplicant_get_config_blob(void *ctx
, const char *name
)
187 struct wpa_supplicant
*wpa_s
= ctx
;
188 return wpa_config_get_blob(wpa_s
->conf
, name
);
192 static void test_eapol_clean(struct wpa_supplicant
*wpa_s
)
194 rsn_preauth_deinit(wpa_s
->wpa
);
195 pmksa_candidate_free(wpa_s
->wpa
);
196 wpa_sm_deinit(wpa_s
->wpa
);
197 scard_deinit(wpa_s
->scard
);
198 if (wpa_s
->ctrl_iface
) {
199 wpa_supplicant_ctrl_iface_deinit(wpa_s
->ctrl_iface
);
200 wpa_s
->ctrl_iface
= NULL
;
202 wpa_config_free(wpa_s
->conf
);
206 static void eapol_test_timeout(void *eloop_ctx
, void *timeout_ctx
)
208 struct preauth_test_data
*p
= eloop_ctx
;
209 printf("EAPOL test timed out\n");
210 p
->auth_timed_out
= 1;
215 static void eapol_test_poll(void *eloop_ctx
, void *timeout_ctx
)
217 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
218 if (!rsn_preauth_in_progress(wpa_s
->wpa
))
221 eloop_register_timeout(0, 100000, eapol_test_poll
, eloop_ctx
,
227 static struct wpa_driver_ops dummy_driver
;
230 static void wpa_init_conf(struct wpa_supplicant
*wpa_s
, const char *ifname
)
232 struct l2_packet_data
*l2
;
233 struct wpa_sm_ctx
*ctx
;
235 os_memset(&dummy_driver
, 0, sizeof(dummy_driver
));
236 wpa_s
->driver
= &dummy_driver
;
238 ctx
= os_zalloc(sizeof(*ctx
));
242 ctx
->set_state
= _wpa_supplicant_set_state
;
243 ctx
->get_state
= _wpa_supplicant_get_state
;
244 ctx
->deauthenticate
= _wpa_supplicant_deauthenticate
;
245 ctx
->disassociate
= _wpa_supplicant_disassociate
;
246 ctx
->set_key
= wpa_supplicant_set_key
;
247 ctx
->get_network_ctx
= wpa_supplicant_get_network_ctx
;
248 ctx
->get_bssid
= wpa_supplicant_get_bssid
;
249 ctx
->ether_send
= wpa_ether_send
;
250 ctx
->get_beacon_ie
= wpa_supplicant_get_beacon_ie
;
251 ctx
->alloc_eapol
= _wpa_alloc_eapol
;
252 ctx
->cancel_auth_timeout
= _wpa_supplicant_cancel_auth_timeout
;
253 ctx
->add_pmkid
= wpa_supplicant_add_pmkid
;
254 ctx
->remove_pmkid
= wpa_supplicant_remove_pmkid
;
255 ctx
->set_config_blob
= wpa_supplicant_set_config_blob
;
256 ctx
->get_config_blob
= wpa_supplicant_get_config_blob
;
257 ctx
->mlme_setprotection
= wpa_supplicant_mlme_setprotection
;
259 wpa_s
->wpa
= wpa_sm_init(ctx
);
260 assert(wpa_s
->wpa
!= NULL
);
261 wpa_sm_set_param(wpa_s
->wpa
, WPA_PARAM_PROTO
, WPA_PROTO_RSN
);
263 os_strlcpy(wpa_s
->ifname
, ifname
, sizeof(wpa_s
->ifname
));
264 wpa_sm_set_ifname(wpa_s
->wpa
, wpa_s
->ifname
, NULL
);
266 l2
= l2_packet_init(wpa_s
->ifname
, NULL
, ETH_P_RSN_PREAUTH
, NULL
,
269 if (l2_packet_get_own_addr(l2
, wpa_s
->own_addr
)) {
270 wpa_printf(MSG_WARNING
, "Failed to get own L2 address\n");
273 l2_packet_deinit(l2
);
274 wpa_sm_set_own_addr(wpa_s
->wpa
, wpa_s
->own_addr
);
278 static void eapol_test_terminate(int sig
, void *eloop_ctx
,
281 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
282 wpa_msg(wpa_s
, MSG_INFO
, "Signal %d received - terminating", sig
);
287 int main(int argc
, char *argv
[])
289 struct wpa_supplicant wpa_s
;
292 struct preauth_test_data preauth_test
;
294 if (os_program_init())
297 os_memset(&preauth_test
, 0, sizeof(preauth_test
));
300 wpa_debug_show_keys
= 1;
303 printf("usage: preauth_test <conf> <target MAC address> "
308 if (hwaddr_aton(argv
[2], bssid
)) {
309 printf("Failed to parse target address '%s'.\n", argv
[2]);
313 if (eap_peer_register_methods()) {
314 wpa_printf(MSG_ERROR
, "Failed to register EAP methods");
318 if (eloop_init(&wpa_s
)) {
319 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
323 os_memset(&wpa_s
, 0, sizeof(wpa_s
));
324 wpa_s
.conf
= wpa_config_read(argv
[1]);
325 if (wpa_s
.conf
== NULL
) {
326 printf("Failed to parse configuration file '%s'.\n", argv
[1]);
329 if (wpa_s
.conf
->ssid
== NULL
) {
330 printf("No networks defined.\n");
334 wpa_init_conf(&wpa_s
, argv
[3]);
335 wpa_s
.ctrl_iface
= wpa_supplicant_ctrl_iface_init(&wpa_s
);
336 if (wpa_s
.ctrl_iface
== NULL
) {
337 printf("Failed to initialize control interface '%s'.\n"
338 "You may have another preauth_test process already "
339 "running or the file was\n"
340 "left by an unclean termination of preauth_test in "
341 "which case you will need\n"
342 "to manually remove this file before starting "
343 "preauth_test again.\n",
344 wpa_s
.conf
->ctrl_interface
);
347 if (wpa_supplicant_scard_init(&wpa_s
, wpa_s
.conf
->ssid
))
350 if (rsn_preauth_init(wpa_s
.wpa
, bssid
, &wpa_s
.conf
->ssid
->eap
))
353 eloop_register_timeout(30, 0, eapol_test_timeout
, &preauth_test
, NULL
);
354 eloop_register_timeout(0, 100000, eapol_test_poll
, &wpa_s
, NULL
);
355 eloop_register_signal_terminate(eapol_test_terminate
, NULL
);
356 eloop_register_signal_reconfig(eapol_test_terminate
, NULL
);
359 if (preauth_test
.auth_timed_out
)
362 ret
= pmksa_cache_set_current(wpa_s
.wpa
, NULL
, bssid
, NULL
, 0)
366 test_eapol_clean(&wpa_s
);
368 eap_peer_unregister_methods();