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