preauth_test.c

Go to the documentation of this file.
00001 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <stdarg.h>
00022 #include <ctype.h>
00023 #include <string.h>
00024 #include <signal.h>
00025 #include <netinet/in.h>
00026 #include <assert.h>
00027 #include <arpa/inet.h>
00028 
00029 #include "common.h"
00030 #include "config.h"
00031 #include "eapol_sm.h"
00032 #include "eloop.h"
00033 #include "wpa.h"
00034 #include "eap.h"
00035 #include "wpa_supplicant.h"
00036 #include "wpa_supplicant_i.h"
00037 #include "l2_packet.h"
00038 #include "ctrl_iface.h"
00039 #include "pcsc_funcs.h"
00040 #include "preauth.h"
00041 
00042 
00043 extern int wpa_debug_level;
00044 extern int wpa_debug_show_keys;
00045 
00046 struct wpa_driver_ops *wpa_supplicant_drivers[] = { };
00047 
00048 
00049 struct preauth_test_data {
00050         int auth_timed_out;
00051 };
00052 
00053 
00054 static void _wpa_supplicant_req_scan(void *wpa_s, int sec, int usec)
00055 {
00056         wpa_supplicant_req_scan(wpa_s, sec, usec);
00057 }
00058 
00059 
00060 static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
00061 {
00062         wpa_supplicant_disassociate(wpa_s, reason_code);
00063 }
00064 
00065 
00066 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
00067 {
00068         wpa_supplicant_deauthenticate(wpa_s, reason_code);
00069 }
00070 
00071 
00072 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
00073                             const void *data, u16 data_len,
00074                             size_t *msg_len, void **data_pos)
00075 {
00076         struct ieee802_1x_hdr *hdr;
00077 
00078         *msg_len = sizeof(*hdr) + data_len;
00079         hdr = malloc(*msg_len);
00080         if (hdr == NULL)
00081                 return NULL;
00082 
00083         hdr->version = wpa_s->conf->eapol_version;
00084         hdr->type = type;
00085         hdr->length = htons(data_len);
00086 
00087         if (data)
00088                 memcpy(hdr + 1, data, data_len);
00089         else
00090                 memset(hdr + 1, 0, data_len);
00091 
00092         if (data_pos)
00093                 *data_pos = hdr + 1;
00094 
00095         return (u8 *) hdr;
00096 }
00097 
00098 
00099 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
00100                              const void *data, u16 data_len,
00101                              size_t *msg_len, void **data_pos)
00102 {
00103         return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
00104 }
00105 
00106 
00107 static void _wpa_supplicant_set_state(void *ctx, wpa_states state)
00108 {
00109         struct wpa_supplicant *wpa_s = ctx;
00110         wpa_s->wpa_state = state;
00111 }
00112 
00113 
00114 static wpa_states _wpa_supplicant_get_state(void *ctx)
00115 {
00116         struct wpa_supplicant *wpa_s = ctx;
00117         return wpa_s->wpa_state;
00118 }
00119 
00120 
00121 static int wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
00122                           const u8 *buf, size_t len)
00123 {
00124         printf("%s - not implemented\n", __func__);
00125         return -1;
00126 }
00127 
00128 
00129 static struct wpa_ssid * _wpa_supplicant_get_ssid(void *wpa_s)
00130 {
00131         return wpa_supplicant_get_ssid(wpa_s);
00132 }
00133 
00134 
00135 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
00136 {
00137         wpa_supplicant_cancel_auth_timeout(wpa_s);
00138 }
00139 
00140 
00141 static int wpa_supplicant_get_beacon_ie(void *wpa_s)
00142 {
00143         printf("%s - not implemented\n", __func__);
00144         return -1;
00145 }
00146 
00147 
00148 void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
00149 {
00150         printf("%s - not implemented\n", __func__);
00151 }
00152 
00153 
00154 static int wpa_supplicant_get_bssid(void *wpa_s, u8 *bssid)
00155 {
00156         printf("%s - not implemented\n", __func__);
00157         return -1;
00158 }
00159 
00160 
00161 static int wpa_supplicant_set_key(void *wpa_s, wpa_alg alg,
00162                                   const u8 *addr, int key_idx, int set_tx,
00163                                   const u8 *seq, size_t seq_len,
00164                                   const u8 *key, size_t key_len)
00165 {
00166         printf("%s - not implemented\n", __func__);
00167         return -1;
00168 }
00169 
00170 
00171 static int wpa_supplicant_add_pmkid(void *wpa_s,
00172                                     const u8 *bssid, const u8 *pmkid)
00173 {
00174         printf("%s - not implemented\n", __func__);
00175         return -1;
00176 }
00177 
00178 
00179 static int wpa_supplicant_remove_pmkid(void *wpa_s,
00180                                        const u8 *bssid, const u8 *pmkid)
00181 {
00182         printf("%s - not implemented\n", __func__);
00183         return -1;
00184 }
00185 
00186 
00187 static void wpa_supplicant_set_config_blob(void *ctx,
00188                                            struct wpa_config_blob *blob)
00189 {
00190         struct wpa_supplicant *wpa_s = ctx;
00191         wpa_config_set_blob(wpa_s->conf, blob);
00192 }
00193 
00194 
00195 static const struct wpa_config_blob *
00196 wpa_supplicant_get_config_blob(void *ctx, const char *name)
00197 {
00198         struct wpa_supplicant *wpa_s = ctx;
00199         return wpa_config_get_blob(wpa_s->conf, name);
00200 }
00201 
00202 
00203 static void test_eapol_clean(struct wpa_supplicant *wpa_s)
00204 {
00205         rsn_preauth_deinit(wpa_s->wpa);
00206         pmksa_candidate_free(wpa_s->wpa);
00207         pmksa_cache_free(wpa_s->wpa);
00208         wpa_sm_deinit(wpa_s->wpa);
00209         scard_deinit(wpa_s->scard);
00210         wpa_supplicant_ctrl_iface_deinit(wpa_s);
00211         wpa_config_free(wpa_s->conf);
00212 }
00213 
00214 
00215 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
00216 {
00217         struct preauth_test_data *p = eloop_ctx;
00218         printf("EAPOL test timed out\n");
00219         p->auth_timed_out = 1;
00220         eloop_terminate();
00221 }
00222 
00223 
00224 static void eapol_test_poll(void *eloop_ctx, void *timeout_ctx)
00225 {
00226         struct wpa_supplicant *wpa_s = eloop_ctx;
00227         if (!rsn_preauth_in_progress(wpa_s->wpa))
00228                 eloop_terminate();
00229         else {
00230                 eloop_register_timeout(0, 100000, eapol_test_poll, eloop_ctx,
00231                                        timeout_ctx);
00232         }
00233 }
00234 
00235 
00236 static struct wpa_driver_ops dummy_driver;
00237 
00238 
00239 static void wpa_init_conf(struct wpa_supplicant *wpa_s, const char *ifname)
00240 {
00241         struct l2_packet_data *l2;
00242         struct wpa_sm_ctx *ctx;
00243 
00244         memset(&dummy_driver, 0, sizeof(dummy_driver));
00245         wpa_s->driver = &dummy_driver;
00246 
00247         ctx = malloc(sizeof(*ctx));
00248         assert(ctx != NULL);
00249 
00250         memset(ctx, 0, sizeof(*ctx));
00251         ctx->ctx = wpa_s;
00252         ctx->set_state = _wpa_supplicant_set_state;
00253         ctx->get_state = _wpa_supplicant_get_state;
00254         ctx->req_scan = _wpa_supplicant_req_scan;
00255         ctx->deauthenticate = _wpa_supplicant_deauthenticate;
00256         ctx->disassociate = _wpa_supplicant_disassociate;
00257         ctx->set_key = wpa_supplicant_set_key;
00258         ctx->scan = wpa_supplicant_scan;
00259         ctx->get_ssid = _wpa_supplicant_get_ssid;
00260         ctx->get_bssid = wpa_supplicant_get_bssid;
00261         ctx->ether_send = wpa_ether_send;
00262         ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
00263         ctx->alloc_eapol = _wpa_alloc_eapol;
00264         ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
00265         ctx->add_pmkid = wpa_supplicant_add_pmkid;
00266         ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
00267         ctx->set_config_blob = wpa_supplicant_set_config_blob;
00268         ctx->get_config_blob = wpa_supplicant_get_config_blob;
00269 
00270         wpa_s->wpa = wpa_sm_init(ctx);
00271         assert(wpa_s->wpa != NULL);
00272         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, WPA_PROTO_RSN);
00273 
00274         strncpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
00275         wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname);
00276 
00277         l2 = l2_packet_init(wpa_s->ifname, NULL, ETH_P_RSN_PREAUTH, NULL,
00278                             NULL, 0);
00279         assert(l2 != NULL);
00280         if (l2_packet_get_own_addr(l2, wpa_s->own_addr)) {
00281                 wpa_printf(MSG_WARNING, "Failed to get own L2 address\n");
00282                 exit(-1);
00283         }
00284         l2_packet_deinit(l2);
00285         wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
00286 }
00287 
00288 
00289 static void eapol_test_terminate(int sig, void *eloop_ctx,
00290                                  void *signal_ctx)
00291 {
00292         struct wpa_supplicant *wpa_s = eloop_ctx;
00293         wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
00294         eloop_terminate();
00295 }
00296 
00297 
00298 int main(int argc, char *argv[])
00299 {
00300         struct wpa_supplicant wpa_s;
00301         int ret = 1;
00302         u8 bssid[ETH_ALEN];
00303         struct preauth_test_data preauth_test;
00304 
00305         memset(&preauth_test, 0, sizeof(preauth_test));
00306 
00307         wpa_debug_level = 0;
00308         wpa_debug_show_keys = 1;
00309 
00310         if (argc != 4) {
00311                 printf("usage: preauth_test <conf> <target MAC address> "
00312                        "<ifname>\n");
00313                 return -1;
00314         }
00315 
00316         if (hwaddr_aton(argv[2], bssid)) {
00317                 printf("Failed to parse target address '%s'.\n", argv[2]);
00318                 return -1;
00319         }
00320 
00321         eloop_init(&wpa_s);
00322 
00323         memset(&wpa_s, 0, sizeof(wpa_s));
00324         wpa_s.conf = wpa_config_read(argv[1]);
00325         if (wpa_s.conf == NULL) {
00326                 printf("Failed to parse configuration file '%s'.\n", argv[1]);
00327                 return -1;
00328         }
00329         if (wpa_s.conf->ssid == NULL) {
00330                 printf("No networks defined.\n");
00331                 return -1;
00332         }
00333 
00334         wpa_init_conf(&wpa_s, argv[3]);
00335         if (wpa_supplicant_ctrl_iface_init(&wpa_s)) {
00336                 printf("Failed to initialize control interface '%s'.\n"
00337                        "You may have another preauth_test process already "
00338                        "running or the file was\n"
00339                        "left by an unclean termination of preauth_test in "
00340                        "which case you will need\n"
00341                        "to manually remove this file before starting "
00342                        "preauth_test again.\n",
00343                        wpa_s.conf->ctrl_interface);
00344                 return -1;
00345         }
00346         if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
00347                 return -1;
00348 
00349         if (rsn_preauth_init(wpa_s.wpa, bssid, wpa_s.conf->ssid))
00350                 return -1;
00351 
00352         eloop_register_timeout(30, 0, eapol_test_timeout, &preauth_test, NULL);
00353         eloop_register_timeout(0, 100000, eapol_test_poll, &wpa_s, NULL);
00354         eloop_register_signal(SIGINT, eapol_test_terminate, NULL);
00355         eloop_register_signal(SIGTERM, eapol_test_terminate, NULL);
00356         eloop_register_signal(SIGHUP, eapol_test_terminate, NULL);
00357         eloop_run();
00358 
00359         if (preauth_test.auth_timed_out)
00360                 ret = -2;
00361         else {
00362                 ret = pmksa_cache_get(wpa_s.wpa, bssid, NULL) ? 0 : -3;
00363         }
00364 
00365         test_eapol_clean(&wpa_s);
00366 
00367         eloop_destroy();
00368 
00369         return ret;
00370 }
00371 

Generated on Sat May 6 21:13:38 2006 for wpa_supplicant by  doxygen 1.4.2