Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * WPA Supplicant - test code for pre-authentication
      3  * Copyright (c) 2003-2007, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  *
      8  * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
      9  * Not used in production version.
     10  */
     11 
     12 #include "includes.h"
     13 #include <assert.h>
     14 
     15 #include "common.h"
     16 #include "config.h"
     17 #include "eapol_supp/eapol_supp_sm.h"
     18 #include "eloop.h"
     19 #include "rsn_supp/wpa.h"
     20 #include "eap_peer/eap.h"
     21 #include "wpa_supplicant_i.h"
     22 #include "l2_packet/l2_packet.h"
     23 #include "ctrl_iface.h"
     24 #include "pcsc_funcs.h"
     25 #include "rsn_supp/preauth.h"
     26 #include "rsn_supp/pmksa_cache.h"
     27 #include "drivers/driver.h"
     28 
     29 
     30 extern int wpa_debug_level;
     31 extern int wpa_debug_show_keys;
     32 
     33 struct wpa_driver_ops *wpa_drivers[] = { NULL };
     34 
     35 
     36 struct preauth_test_data {
     37 	int auth_timed_out;
     38 };
     39 
     40 
     41 static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
     42 {
     43 	wpa_supplicant_disassociate(wpa_s, reason_code);
     44 }
     45 
     46 
     47 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
     48 {
     49 	wpa_supplicant_deauthenticate(wpa_s, reason_code);
     50 }
     51 
     52 
     53 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
     54 			    const void *data, u16 data_len,
     55 			    size_t *msg_len, void **data_pos)
     56 {
     57 	struct ieee802_1x_hdr *hdr;
     58 
     59 	*msg_len = sizeof(*hdr) + data_len;
     60 	hdr = os_malloc(*msg_len);
     61 	if (hdr == NULL)
     62 		return NULL;
     63 
     64 	hdr->version = wpa_s->conf->eapol_version;
     65 	hdr->type = type;
     66 	hdr->length = htons(data_len);
     67 
     68 	if (data)
     69 		os_memcpy(hdr + 1, data, data_len);
     70 	else
     71 		os_memset(hdr + 1, 0, data_len);
     72 
     73 	if (data_pos)
     74 		*data_pos = hdr + 1;
     75 
     76 	return (u8 *) hdr;
     77 }
     78 
     79 
     80 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
     81 			     const void *data, u16 data_len,
     82 			     size_t *msg_len, void **data_pos)
     83 {
     84 	return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
     85 }
     86 
     87 
     88 static void _wpa_supplicant_set_state(void *ctx, enum wpa_states state)
     89 {
     90 	struct wpa_supplicant *wpa_s = ctx;
     91 	wpa_s->wpa_state = state;
     92 }
     93 
     94 
     95 static enum wpa_states _wpa_supplicant_get_state(void *ctx)
     96 {
     97 	struct wpa_supplicant *wpa_s = ctx;
     98 	return wpa_s->wpa_state;
     99 }
    100 
    101 
    102 static int wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
    103 			  const u8 *buf, size_t len)
    104 {
    105 	printf("%s - not implemented\n", __func__);
    106 	return -1;
    107 }
    108 
    109 
    110 static void * wpa_supplicant_get_network_ctx(void *wpa_s)
    111 {
    112 	return wpa_supplicant_get_ssid(wpa_s);
    113 }
    114 
    115 
    116 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
    117 {
    118 	wpa_supplicant_cancel_auth_timeout(wpa_s);
    119 }
    120 
    121 
    122 static int wpa_supplicant_get_beacon_ie(void *wpa_s)
    123 {
    124 	printf("%s - not implemented\n", __func__);
    125 	return -1;
    126 }
    127 
    128 
    129 static int wpa_supplicant_get_bssid(void *wpa_s, u8 *bssid)
    130 {
    131 	printf("%s - not implemented\n", __func__);
    132 	return -1;
    133 }
    134 
    135 
    136 static int wpa_supplicant_set_key(void *wpa_s, enum wpa_alg alg,
    137 				  const u8 *addr, int key_idx, int set_tx,
    138 				  const u8 *seq, size_t seq_len,
    139 				  const u8 *key, size_t key_len)
    140 {
    141 	printf("%s - not implemented\n", __func__);
    142 	return -1;
    143 }
    144 
    145 
    146 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
    147 					     int protection_type,
    148 					     int key_type)
    149 {
    150 	printf("%s - not implemented\n", __func__);
    151 	return -1;
    152 }
    153 
    154 
    155 static int wpa_supplicant_add_pmkid(void *wpa_s,
    156 				    const u8 *bssid, const u8 *pmkid)
    157 {
    158 	printf("%s - not implemented\n", __func__);
    159 	return -1;
    160 }
    161 
    162 
    163 static int wpa_supplicant_remove_pmkid(void *wpa_s,
    164 				       const u8 *bssid, const u8 *pmkid)
    165 {
    166 	printf("%s - not implemented\n", __func__);
    167 	return -1;
    168 }
    169 
    170 
    171 static void wpa_supplicant_set_config_blob(void *ctx,
    172 					   struct wpa_config_blob *blob)
    173 {
    174 	struct wpa_supplicant *wpa_s = ctx;
    175 	wpa_config_set_blob(wpa_s->conf, blob);
    176 }
    177 
    178 
    179 static const struct wpa_config_blob *
    180 wpa_supplicant_get_config_blob(void *ctx, const char *name)
    181 {
    182 	struct wpa_supplicant *wpa_s = ctx;
    183 	return wpa_config_get_blob(wpa_s->conf, name);
    184 }
    185 
    186 
    187 static void test_eapol_clean(struct wpa_supplicant *wpa_s)
    188 {
    189 	rsn_preauth_deinit(wpa_s->wpa);
    190 	pmksa_candidate_free(wpa_s->wpa);
    191 	wpa_sm_deinit(wpa_s->wpa);
    192 	scard_deinit(wpa_s->scard);
    193 	if (wpa_s->ctrl_iface) {
    194 		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
    195 		wpa_s->ctrl_iface = NULL;
    196 	}
    197 	wpa_config_free(wpa_s->conf);
    198 }
    199 
    200 
    201 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
    202 {
    203 	struct preauth_test_data *p = eloop_ctx;
    204 	printf("EAPOL test timed out\n");
    205 	p->auth_timed_out = 1;
    206 	eloop_terminate();
    207 }
    208 
    209 
    210 static void eapol_test_poll(void *eloop_ctx, void *timeout_ctx)
    211 {
    212 	struct wpa_supplicant *wpa_s = eloop_ctx;
    213 	if (!rsn_preauth_in_progress(wpa_s->wpa))
    214 		eloop_terminate();
    215 	else {
    216 		eloop_register_timeout(0, 100000, eapol_test_poll, eloop_ctx,
    217 				       timeout_ctx);
    218 	}
    219 }
    220 
    221 
    222 static struct wpa_driver_ops dummy_driver;
    223 
    224 
    225 static void wpa_init_conf(struct wpa_supplicant *wpa_s, const char *ifname)
    226 {
    227 	struct l2_packet_data *l2;
    228 	struct wpa_sm_ctx *ctx;
    229 
    230 	os_memset(&dummy_driver, 0, sizeof(dummy_driver));
    231 	wpa_s->driver = &dummy_driver;
    232 
    233 	ctx = os_zalloc(sizeof(*ctx));
    234 	assert(ctx != NULL);
    235 
    236 	ctx->ctx = wpa_s;
    237 	ctx->msg_ctx = wpa_s;
    238 	ctx->set_state = _wpa_supplicant_set_state;
    239 	ctx->get_state = _wpa_supplicant_get_state;
    240 	ctx->deauthenticate = _wpa_supplicant_deauthenticate;
    241 	ctx->disassociate = _wpa_supplicant_disassociate;
    242 	ctx->set_key = wpa_supplicant_set_key;
    243 	ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
    244 	ctx->get_bssid = wpa_supplicant_get_bssid;
    245 	ctx->ether_send = wpa_ether_send;
    246 	ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
    247 	ctx->alloc_eapol = _wpa_alloc_eapol;
    248 	ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
    249 	ctx->add_pmkid = wpa_supplicant_add_pmkid;
    250 	ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
    251 	ctx->set_config_blob = wpa_supplicant_set_config_blob;
    252 	ctx->get_config_blob = wpa_supplicant_get_config_blob;
    253 	ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
    254 
    255 	wpa_s->wpa = wpa_sm_init(ctx);
    256 	assert(wpa_s->wpa != NULL);
    257 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, WPA_PROTO_RSN);
    258 
    259 	os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
    260 	wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname, NULL);
    261 
    262 	l2 = l2_packet_init(wpa_s->ifname, NULL, ETH_P_RSN_PREAUTH, NULL,
    263 			    NULL, 0);
    264 	assert(l2 != NULL);
    265 	if (l2_packet_get_own_addr(l2, wpa_s->own_addr)) {
    266 		wpa_printf(MSG_WARNING, "Failed to get own L2 address\n");
    267 		exit(-1);
    268 	}
    269 	l2_packet_deinit(l2);
    270 	wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
    271 }
    272 
    273 
    274 static void eapol_test_terminate(int sig, void *signal_ctx)
    275 {
    276 	struct wpa_supplicant *wpa_s = signal_ctx;
    277 	wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
    278 	eloop_terminate();
    279 }
    280 
    281 
    282 int main(int argc, char *argv[])
    283 {
    284 	struct wpa_supplicant wpa_s;
    285 	int ret = 1;
    286 	u8 bssid[ETH_ALEN];
    287 	struct preauth_test_data preauth_test;
    288 
    289 	if (os_program_init())
    290 		return -1;
    291 
    292 	os_memset(&preauth_test, 0, sizeof(preauth_test));
    293 
    294 	wpa_debug_level = 0;
    295 	wpa_debug_show_keys = 1;
    296 
    297 	if (argc != 4) {
    298 		printf("usage: preauth_test <conf> <target MAC address> "
    299 		       "<ifname>\n");
    300 		return -1;
    301 	}
    302 
    303 	if (hwaddr_aton(argv[2], bssid)) {
    304 		printf("Failed to parse target address '%s'.\n", argv[2]);
    305 		return -1;
    306 	}
    307 
    308 	if (eap_register_methods()) {
    309 		wpa_printf(MSG_ERROR, "Failed to register EAP methods");
    310 		return -1;
    311 	}
    312 
    313 	if (eloop_init()) {
    314 		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
    315 		return -1;
    316 	}
    317 
    318 	os_memset(&wpa_s, 0, sizeof(wpa_s));
    319 	wpa_s.conf = wpa_config_read(argv[1]);
    320 	if (wpa_s.conf == NULL) {
    321 		printf("Failed to parse configuration file '%s'.\n", argv[1]);
    322 		return -1;
    323 	}
    324 	if (wpa_s.conf->ssid == NULL) {
    325 		printf("No networks defined.\n");
    326 		return -1;
    327 	}
    328 
    329 	wpa_init_conf(&wpa_s, argv[3]);
    330 	wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
    331 	if (wpa_s.ctrl_iface == NULL) {
    332 		printf("Failed to initialize control interface '%s'.\n"
    333 		       "You may have another preauth_test process already "
    334 		       "running or the file was\n"
    335 		       "left by an unclean termination of preauth_test in "
    336 		       "which case you will need\n"
    337 		       "to manually remove this file before starting "
    338 		       "preauth_test again.\n",
    339 		       wpa_s.conf->ctrl_interface);
    340 		return -1;
    341 	}
    342 	if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
    343 		return -1;
    344 
    345 	if (rsn_preauth_init(wpa_s.wpa, bssid, &wpa_s.conf->ssid->eap))
    346 		return -1;
    347 
    348 	eloop_register_timeout(30, 0, eapol_test_timeout, &preauth_test, NULL);
    349 	eloop_register_timeout(0, 100000, eapol_test_poll, &wpa_s, NULL);
    350 	eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
    351 	eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
    352 	eloop_run();
    353 
    354 	if (preauth_test.auth_timed_out)
    355 		ret = -2;
    356 	else {
    357 		ret = pmksa_cache_set_current(wpa_s.wpa, NULL, bssid, NULL, 0)
    358 			? 0 : -3;
    359 	}
    360 
    361 	test_eapol_clean(&wpa_s);
    362 
    363 	eap_peer_unregister_methods();
    364 
    365 	eloop_destroy();
    366 
    367 	os_program_deinit();
    368 
    369 	return ret;
    370 }
    371