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