Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / wnm-fuzzer / wnm-fuzzer.c
1 /*
2  * wpa_supplicant - WNM fuzzer
3  * Copyright (c) 2015, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "rsn_supp/wpa.h"
15 #include "rsn_supp/wpa_i.h"
16 #include "../../wpa_supplicant/wpa_supplicant_i.h"
17 #include "../../wpa_supplicant/bss.h"
18 #include "../../wpa_supplicant/wnm_sta.h"
19
20
21 struct arg_ctx {
22         const char *fname;
23         struct wpa_supplicant wpa_s;
24         struct wpa_bss bss;
25         struct wpa_driver_ops driver;
26         struct wpa_sm wpa;
27 };
28
29
30 static void test_send_wnm(void *eloop_data, void *user_ctx)
31 {
32         struct arg_ctx *ctx = eloop_data;
33         char *data;
34         size_t len;
35         struct ieee80211_mgmt *mgmt;
36
37         wpa_printf(MSG_INFO, "wnm-fuzzer: Send '%s'", ctx->fname);
38
39         data = os_readfile(ctx->fname, &len);
40         if (!data) {
41                 wpa_printf(MSG_ERROR, "Could not read '%s'", ctx->fname);
42                 goto out;
43         }
44
45         wpa_hexdump(MSG_MSGDUMP, "fuzzer - WNM", data, len);
46
47         mgmt = (struct ieee80211_mgmt *) data;
48         ieee802_11_rx_wnm_action(&ctx->wpa_s, mgmt, len);
49
50 out:
51         os_free(data);
52         eloop_terminate();
53 }
54
55
56 static int init_wpa(struct arg_ctx *ctx)
57 {
58         ctx->wpa_s.wpa_state = WPA_COMPLETED;
59         os_memcpy(ctx->wpa_s.bssid, "\x02\x00\x00\x00\x03\x00", ETH_ALEN);
60         ctx->wpa_s.current_bss = &ctx->bss;
61         ctx->wpa_s.driver = &ctx->driver;
62         ctx->wpa_s.wpa = &ctx->wpa;
63
64         return 0;
65 }
66
67
68 int main(int argc, char *argv[])
69 {
70         struct arg_ctx ctx;
71         int ret = -1;
72
73         if (argc < 2) {
74                 printf("usage: %s <file>\n", argv[0]);
75                 return -1;
76         }
77
78         if (os_program_init())
79                 return -1;
80
81         wpa_debug_level = 0;
82         wpa_debug_show_keys = 1;
83
84         if (eloop_init()) {
85                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
86                 return -1;
87         }
88
89         os_memset(&ctx, 0, sizeof(ctx));
90         ctx.fname = argv[1];
91         if (init_wpa(&ctx))
92                 goto fail;
93
94         eloop_register_timeout(0, 0, test_send_wnm, &ctx, NULL);
95
96         wpa_printf(MSG_DEBUG, "Starting eloop");
97         eloop_run();
98         wpa_printf(MSG_DEBUG, "eloop done");
99
100         ret = 0;
101 fail:
102         eloop_destroy();
103         os_program_deinit();
104
105         return ret;
106 }