Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / p2p-fuzzer / p2p-fuzzer.c
1 /*
2  * wpa_supplicant - P2P 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 "p2p/p2p.h"
15
16
17 static void debug_print(void *ctx, int level, const char *msg)
18 {
19         wpa_printf(level, "P2P: %s", msg);
20 }
21
22
23 static void find_stopped(void *ctx)
24 {
25 }
26
27
28 static int start_listen(void *ctx, unsigned int freq,
29                         unsigned int duration,
30                         const struct wpabuf *probe_resp_ie)
31 {
32         return 0;
33 }
34
35
36 static void stop_listen(void *ctx)
37 {
38 }
39
40
41 static void dev_found(void *ctx, const u8 *addr,
42                       const struct p2p_peer_info *info,
43                       int new_device)
44 {
45 }
46
47
48 static void dev_lost(void *ctx, const u8 *dev_addr)
49 {
50 }
51
52
53 static int send_action(void *ctx, unsigned int freq, const u8 *dst,
54                        const u8 *src, const u8 *bssid, const u8 *buf,
55                        size_t len, unsigned int wait_time)
56 {
57         return 0;
58 }
59
60
61 static void send_action_done(void *ctx)
62 {
63 }
64
65
66 static void go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
67 {
68 }
69
70
71 static struct p2p_data * init_p2p(void)
72 {
73         struct p2p_config p2p;
74
75         os_memset(&p2p, 0, sizeof(p2p));
76         p2p.max_peers = 100;
77         p2p.passphrase_len = 8;
78         p2p.channels.reg_classes = 1;
79         p2p.channels.reg_class[0].reg_class = 81;
80         p2p.channels.reg_class[0].channel[0] = 1;
81         p2p.channels.reg_class[0].channel[1] = 2;
82         p2p.channels.reg_class[0].channels = 2;
83         p2p.debug_print = debug_print;
84         p2p.find_stopped = find_stopped;
85         p2p.start_listen = start_listen;
86         p2p.stop_listen = stop_listen;
87         p2p.dev_found = dev_found;
88         p2p.dev_lost = dev_lost;
89         p2p.send_action = send_action;
90         p2p.send_action_done = send_action_done;
91         p2p.go_neg_req_rx = go_neg_req_rx;
92
93         return p2p_init(&p2p);
94 }
95
96
97 struct arg_ctx {
98         struct p2p_data *p2p;
99         const char *fname;
100 };
101
102
103 static void test_send_proberesp(void *eloop_data, void *user_ctx)
104 {
105         struct arg_ctx *ctx = eloop_data;
106         char *data;
107         size_t len;
108         struct os_reltime rx_time;
109
110         wpa_printf(MSG_INFO, "p2p-fuzzer: Send proberesp '%s'", ctx->fname);
111
112         data = os_readfile(ctx->fname, &len);
113         if (!data) {
114                 wpa_printf(MSG_ERROR, "Could not read '%s'", ctx->fname);
115                 return;
116         }
117
118         wpa_hexdump(MSG_MSGDUMP, "fuzzer - IEs", data, len);
119
120         os_memset(&rx_time, 0, sizeof(rx_time));
121         p2p_scan_res_handler(ctx->p2p, (u8 *) "\x02\x00\x00\x00\x01\x00", 2412,
122                              &rx_time, 0, (u8 *) data, len);
123         p2p_scan_res_handled(ctx->p2p);
124
125         os_free(data);
126         eloop_terminate();
127 }
128
129
130 static void test_send_action(void *eloop_data, void *user_ctx)
131 {
132         struct arg_ctx *ctx = eloop_data;
133         char *data;
134         size_t len;
135         struct os_reltime rx_time;
136         struct ieee80211_mgmt *mgmt;
137
138         wpa_printf(MSG_INFO, "p2p-fuzzer: Send action '%s'", ctx->fname);
139
140         data = os_readfile(ctx->fname, &len);
141         if (!data) {
142                 wpa_printf(MSG_ERROR, "Could not read '%s'", ctx->fname);
143                 return;
144         }
145         if (len < IEEE80211_HDRLEN + 1)
146                 goto out;
147
148         wpa_hexdump(MSG_MSGDUMP, "fuzzer - action", data, len);
149
150         mgmt = (struct ieee80211_mgmt *) data;
151         os_memset(&rx_time, 0, sizeof(rx_time));
152         p2p_rx_action(ctx->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
153                       mgmt->u.action.category,
154                       (u8 *) data + IEEE80211_HDRLEN + 1,
155                       len - IEEE80211_HDRLEN - 1, 2412);
156
157 out:
158         os_free(data);
159         eloop_terminate();
160 }
161
162
163 int main(int argc, char *argv[])
164 {
165         struct p2p_data *p2p;
166         struct arg_ctx ctx;
167
168         /* TODO: probreq and wpas_p2p_probe_req_rx() */
169
170         if (argc < 3) {
171                 printf("usage: %s <proberesp|action> <file>\n", argv[0]);
172                 return -1;
173         }
174
175         if (os_program_init())
176                 return -1;
177
178         wpa_debug_level = 0;
179         wpa_debug_show_keys = 1;
180
181         if (eloop_init()) {
182                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
183                 return -1;
184         }
185
186         p2p = init_p2p();
187         if (!p2p) {
188                 wpa_printf(MSG_ERROR, "P2P init failed");
189                 return -1;
190         }
191
192         ctx.p2p = p2p;
193         ctx.fname = argv[2];
194
195         if (os_strcmp(argv[1], "proberesp") == 0) {
196                 eloop_register_timeout(0, 0, test_send_proberesp, &ctx, NULL);
197         } else if (os_strcmp(argv[1], "action") == 0) {
198                 eloop_register_timeout(0, 0, test_send_action, &ctx, NULL);
199         } else {
200                 wpa_printf(MSG_ERROR, "Unsupported test type '%s'", argv[1]);
201                 return -1;
202         }
203
204         wpa_printf(MSG_DEBUG, "Starting eloop");
205         eloop_run();
206         wpa_printf(MSG_DEBUG, "eloop done");
207
208         p2p_deinit(p2p);
209         eloop_destroy();
210         os_program_deinit();
211
212         return 0;
213 }