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