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