18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/*
28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * WPA Supplicant - test code for pre-authentication
38d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
5c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * This software may be distributed under the terms of the BSD license.
6c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * See README for more details.
78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Not used in production version.
108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "includes.h"
138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <assert.h>
148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common.h"
168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "config.h"
178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "eapol_supp/eapol_supp_sm.h"
188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "eloop.h"
198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "rsn_supp/wpa.h"
208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "eap_peer/eap.h"
218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "wpa_supplicant_i.h"
228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "l2_packet/l2_packet.h"
238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "ctrl_iface.h"
248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "pcsc_funcs.h"
258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "rsn_supp/preauth.h"
268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "rsn_supp/pmksa_cache.h"
278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "drivers/driver.h"
288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
301d755d025b206e22b06aeb322e25a79f98ca7777Dmitry Shmidtconst struct wpa_driver_ops *const wpa_drivers[] = { NULL };
318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct preauth_test_data {
348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int auth_timed_out;
358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_deauthenticate(wpa_s, reason_code);
418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    const void *data, u16 data_len,
468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    size_t *msg_len, void **data_pos)
478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct ieee802_1x_hdr *hdr;
498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*msg_len = sizeof(*hdr) + data_len;
518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr = os_malloc(*msg_len);
528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (hdr == NULL)
538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr->version = wpa_s->conf->eapol_version;
568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr->type = type;
578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr->length = htons(data_len);
588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (data)
608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(hdr + 1, data, data_len);
618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memset(hdr + 1, 0, data_len);
638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (data_pos)
658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*data_pos = hdr + 1;
668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return (u8 *) hdr;
688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     const void *data, u16 data_len,
738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     size_t *msg_len, void **data_pos)
748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void _wpa_supplicant_set_state(void *ctx, enum wpa_states state)
808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_supplicant *wpa_s = ctx;
828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_s->wpa_state = state;
838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic enum wpa_states _wpa_supplicant_get_state(void *ctx)
878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_supplicant *wpa_s = ctx;
898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wpa_s->wpa_state;
908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  const u8 *buf, size_t len)
958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	printf("%s - not implemented\n", __func__);
978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void * wpa_supplicant_get_network_ctx(void *wpa_s)
1028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wpa_supplicant_get_ssid(wpa_s);
1048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_cancel_auth_timeout(wpa_s);
1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_supplicant_get_beacon_ie(void *wpa_s)
1148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	printf("%s - not implemented\n", __func__);
1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
1178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_supplicant_get_bssid(void *wpa_s, u8 *bssid)
1218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	printf("%s - not implemented\n", __func__);
1238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
1248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_supplicant_set_key(void *wpa_s, enum wpa_alg alg,
1288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  const u8 *addr, int key_idx, int set_tx,
1298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  const u8 *seq, size_t seq_len,
1308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  const u8 *key, size_t key_len)
1318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	printf("%s - not implemented\n", __func__);
1338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
1348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
1388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     int protection_type,
1398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     int key_type)
1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	printf("%s - not implemented\n", __func__);
1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_supplicant_add_pmkid(void *wpa_s,
1478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    const u8 *bssid, const u8 *pmkid)
1488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	printf("%s - not implemented\n", __func__);
1508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
1518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_supplicant_remove_pmkid(void *wpa_s,
1558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       const u8 *bssid, const u8 *pmkid)
1568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	printf("%s - not implemented\n", __func__);
1588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
1598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_supplicant_set_config_blob(void *ctx,
1638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   struct wpa_config_blob *blob)
1648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_supplicant *wpa_s = ctx;
1668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_config_set_blob(wpa_s->conf, blob);
1678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const struct wpa_config_blob *
1718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtwpa_supplicant_get_config_blob(void *ctx, const char *name)
1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_supplicant *wpa_s = ctx;
1748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wpa_config_get_blob(wpa_s->conf, name);
1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void test_eapol_clean(struct wpa_supplicant *wpa_s)
1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rsn_preauth_deinit(wpa_s->wpa);
1818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pmksa_candidate_free(wpa_s->wpa);
1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_sm_deinit(wpa_s->wpa);
1838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	scard_deinit(wpa_s->scard);
1848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_s->ctrl_iface) {
1858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
1868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_s->ctrl_iface = NULL;
1878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_config_free(wpa_s->conf);
1898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
1938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct preauth_test_data *p = eloop_ctx;
1958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	printf("EAPOL test timed out\n");
1968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	p->auth_timed_out = 1;
1978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_terminate();
1988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void eapol_test_poll(void *eloop_ctx, void *timeout_ctx)
2028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_supplicant *wpa_s = eloop_ctx;
2048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!rsn_preauth_in_progress(wpa_s->wpa))
2058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_terminate();
2068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else {
2078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_register_timeout(0, 100000, eapol_test_poll, eloop_ctx,
2088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       timeout_ctx);
2098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct wpa_driver_ops dummy_driver;
2148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_init_conf(struct wpa_supplicant *wpa_s, const char *ifname)
2178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct l2_packet_data *l2;
2198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_sm_ctx *ctx;
2208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&dummy_driver, 0, sizeof(dummy_driver));
2228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_s->driver = &dummy_driver;
2238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx = os_zalloc(sizeof(*ctx));
2258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	assert(ctx != NULL);
2268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->ctx = wpa_s;
2288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->msg_ctx = wpa_s;
2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->set_state = _wpa_supplicant_set_state;
2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->get_state = _wpa_supplicant_get_state;
2318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->deauthenticate = _wpa_supplicant_deauthenticate;
2328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->set_key = wpa_supplicant_set_key;
2338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
2348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->get_bssid = wpa_supplicant_get_bssid;
2358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->ether_send = wpa_ether_send;
2368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
2378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->alloc_eapol = _wpa_alloc_eapol;
2388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
2398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->add_pmkid = wpa_supplicant_add_pmkid;
2408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
2418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->set_config_blob = wpa_supplicant_set_config_blob;
2428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->get_config_blob = wpa_supplicant_get_config_blob;
2438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
2448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_s->wpa = wpa_sm_init(ctx);
2468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	assert(wpa_s->wpa != NULL);
2478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, WPA_PROTO_RSN);
2488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname, NULL);
2518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	l2 = l2_packet_init(wpa_s->ifname, NULL, ETH_P_RSN_PREAUTH, NULL,
2538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    NULL, 0);
2548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	assert(l2 != NULL);
2558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (l2_packet_get_own_addr(l2, wpa_s->own_addr)) {
2568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_WARNING, "Failed to get own L2 address\n");
2578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		exit(-1);
2588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	l2_packet_deinit(l2);
2608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void eapol_test_terminate(int sig, void *signal_ctx)
2658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_supplicant *wpa_s = signal_ctx;
2678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
2688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_terminate();
2698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint main(int argc, char *argv[])
2738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_supplicant wpa_s;
2758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = 1;
2768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 bssid[ETH_ALEN];
2778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct preauth_test_data preauth_test;
2788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_program_init())
2808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
2818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&preauth_test, 0, sizeof(preauth_test));
2838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_debug_level = 0;
2858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_debug_show_keys = 1;
2868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (argc != 4) {
2888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		printf("usage: preauth_test <conf> <target MAC address> "
2898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       "<ifname>\n");
2908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
2918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (hwaddr_aton(argv[2], bssid)) {
2948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		printf("Failed to parse target address '%s'.\n", argv[2]);
2958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
2968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (eap_register_methods()) {
2998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "Failed to register EAP methods");
3008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
3018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (eloop_init()) {
3048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
3058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
3068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&wpa_s, 0, sizeof(wpa_s));
30964f47c5c24428834677459e048420f86e3514c20Dmitry Shmidt	wpa_s.conf = wpa_config_read(argv[1], NULL);
3108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_s.conf == NULL) {
3118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		printf("Failed to parse configuration file '%s'.\n", argv[1]);
3128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
3138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_s.conf->ssid == NULL) {
3158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		printf("No networks defined.\n");
3168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
3178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_init_conf(&wpa_s, argv[3]);
3208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
3218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_s.ctrl_iface == NULL) {
3228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		printf("Failed to initialize control interface '%s'.\n"
3238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       "You may have another preauth_test process already "
3248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       "running or the file was\n"
3258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       "left by an unclean termination of preauth_test in "
3268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       "which case you will need\n"
3278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       "to manually remove this file before starting "
3288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       "preauth_test again.\n",
3298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       wpa_s.conf->ctrl_interface);
3308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
3318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
3338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
3348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (rsn_preauth_init(wpa_s.wpa, bssid, &wpa_s.conf->ssid->eap))
3368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
3378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_register_timeout(30, 0, eapol_test_timeout, &preauth_test, NULL);
3398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_register_timeout(0, 100000, eapol_test_poll, &wpa_s, NULL);
3408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
3418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
3428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_run();
3438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (preauth_test.auth_timed_out)
3458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = -2;
3468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else {
3478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = pmksa_cache_set_current(wpa_s.wpa, NULL, bssid, NULL, 0)
3488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			? 0 : -3;
3498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	test_eapol_clean(&wpa_s);
3528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eap_peer_unregister_methods();
3548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_destroy();
3568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_program_deinit();
3588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
3608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
361