| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 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, int *scheduled) | 
 | 56 | { | 
 | 57 | 	*scheduled = 0; | 
 | 58 | 	return 0; | 
 | 59 | } | 
 | 60 |  | 
 | 61 |  | 
 | 62 | static void send_action_done(void *ctx) | 
 | 63 | { | 
 | 64 | } | 
 | 65 |  | 
 | 66 |  | 
 | 67 | static void go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id, | 
 | 68 | 			  u8 go_intent) | 
 | 69 | { | 
 | 70 | } | 
 | 71 |  | 
 | 72 |  | 
 | 73 | static struct p2p_data * init_p2p(void) | 
 | 74 | { | 
 | 75 | 	struct p2p_config p2p; | 
 | 76 |  | 
 | 77 | 	os_memset(&p2p, 0, sizeof(p2p)); | 
 | 78 | 	p2p.max_peers = 100; | 
 | 79 | 	p2p.passphrase_len = 8; | 
 | 80 | 	p2p.channels.reg_classes = 1; | 
 | 81 | 	p2p.channels.reg_class[0].reg_class = 81; | 
 | 82 | 	p2p.channels.reg_class[0].channel[0] = 1; | 
 | 83 | 	p2p.channels.reg_class[0].channel[1] = 2; | 
 | 84 | 	p2p.channels.reg_class[0].channels = 2; | 
 | 85 | 	p2p.debug_print = debug_print; | 
 | 86 | 	p2p.find_stopped = find_stopped; | 
 | 87 | 	p2p.start_listen = start_listen; | 
 | 88 | 	p2p.stop_listen = stop_listen; | 
 | 89 | 	p2p.dev_found = dev_found; | 
 | 90 | 	p2p.dev_lost = dev_lost; | 
 | 91 | 	p2p.send_action = send_action; | 
 | 92 | 	p2p.send_action_done = send_action_done; | 
 | 93 | 	p2p.go_neg_req_rx = go_neg_req_rx; | 
 | 94 |  | 
 | 95 | 	return p2p_init(&p2p); | 
 | 96 | } | 
 | 97 |  | 
 | 98 |  | 
 | 99 | struct arg_ctx { | 
 | 100 | 	struct p2p_data *p2p; | 
 | 101 | 	const char *fname; | 
 | 102 | }; | 
 | 103 |  | 
 | 104 |  | 
 | 105 | static void test_send_proberesp(void *eloop_data, void *user_ctx) | 
 | 106 | { | 
 | 107 | 	struct arg_ctx *ctx = eloop_data; | 
 | 108 | 	char *data; | 
 | 109 | 	size_t len; | 
 | 110 | 	struct os_reltime rx_time; | 
 | 111 |  | 
 | 112 | 	wpa_printf(MSG_INFO, "p2p-fuzzer: Send proberesp '%s'", ctx->fname); | 
 | 113 |  | 
 | 114 | 	data = os_readfile(ctx->fname, &len); | 
 | 115 | 	if (!data) { | 
 | 116 | 		wpa_printf(MSG_ERROR, "Could not read '%s'", ctx->fname); | 
 | 117 | 		return; | 
 | 118 | 	} | 
 | 119 |  | 
 | 120 | 	wpa_hexdump(MSG_MSGDUMP, "fuzzer - IEs", data, len); | 
 | 121 |  | 
 | 122 | 	os_memset(&rx_time, 0, sizeof(rx_time)); | 
 | 123 | 	p2p_scan_res_handler(ctx->p2p, (u8 *) "\x02\x00\x00\x00\x01\x00", 2412, | 
 | 124 | 			     &rx_time, 0, (u8 *) data, len); | 
 | 125 | 	p2p_scan_res_handled(ctx->p2p); | 
 | 126 |  | 
 | 127 | 	os_free(data); | 
 | 128 | 	eloop_terminate(); | 
 | 129 | } | 
 | 130 |  | 
 | 131 |  | 
 | 132 | static void test_send_action(void *eloop_data, void *user_ctx) | 
 | 133 | { | 
 | 134 | 	struct arg_ctx *ctx = eloop_data; | 
 | 135 | 	char *data; | 
 | 136 | 	size_t len; | 
 | 137 | 	struct os_reltime rx_time; | 
 | 138 | 	struct ieee80211_mgmt *mgmt; | 
 | 139 |  | 
 | 140 | 	wpa_printf(MSG_INFO, "p2p-fuzzer: Send action '%s'", ctx->fname); | 
 | 141 |  | 
 | 142 | 	data = os_readfile(ctx->fname, &len); | 
 | 143 | 	if (!data) { | 
 | 144 | 		wpa_printf(MSG_ERROR, "Could not read '%s'", ctx->fname); | 
 | 145 | 		return; | 
 | 146 | 	} | 
 | 147 | 	if (len < IEEE80211_HDRLEN + 1) | 
 | 148 | 		goto out; | 
 | 149 |  | 
 | 150 | 	wpa_hexdump(MSG_MSGDUMP, "fuzzer - action", data, len); | 
 | 151 |  | 
 | 152 | 	mgmt = (struct ieee80211_mgmt *) data; | 
 | 153 | 	os_memset(&rx_time, 0, sizeof(rx_time)); | 
 | 154 | 	p2p_rx_action(ctx->p2p, mgmt->da, mgmt->sa, mgmt->bssid, | 
 | 155 | 		      mgmt->u.action.category, | 
 | 156 | 		      (u8 *) data + IEEE80211_HDRLEN + 1, | 
 | 157 | 		      len - IEEE80211_HDRLEN - 1, 2412); | 
 | 158 |  | 
 | 159 | out: | 
 | 160 | 	os_free(data); | 
 | 161 | 	eloop_terminate(); | 
 | 162 | } | 
 | 163 |  | 
 | 164 |  | 
 | 165 | int main(int argc, char *argv[]) | 
 | 166 | { | 
 | 167 | 	struct p2p_data *p2p; | 
 | 168 | 	struct arg_ctx ctx; | 
 | 169 |  | 
 | 170 | 	/* TODO: probreq and wpas_p2p_probe_req_rx() */ | 
 | 171 |  | 
 | 172 | 	if (argc < 3) { | 
 | 173 | 		printf("usage: %s <proberesp|action> <file>\n", argv[0]); | 
 | 174 | 		return -1; | 
 | 175 | 	} | 
 | 176 |  | 
 | 177 | 	if (os_program_init()) | 
 | 178 | 		return -1; | 
 | 179 |  | 
 | 180 | 	wpa_debug_level = 0; | 
 | 181 | 	wpa_debug_show_keys = 1; | 
 | 182 |  | 
 | 183 | 	if (eloop_init()) { | 
 | 184 | 		wpa_printf(MSG_ERROR, "Failed to initialize event loop"); | 
 | 185 | 		return -1; | 
 | 186 | 	} | 
 | 187 |  | 
 | 188 | 	p2p = init_p2p(); | 
 | 189 | 	if (!p2p) { | 
 | 190 | 		wpa_printf(MSG_ERROR, "P2P init failed"); | 
 | 191 | 		return -1; | 
 | 192 | 	} | 
 | 193 |  | 
 | 194 | 	ctx.p2p = p2p; | 
 | 195 | 	ctx.fname = argv[2]; | 
 | 196 |  | 
 | 197 | 	if (os_strcmp(argv[1], "proberesp") == 0) { | 
 | 198 | 		eloop_register_timeout(0, 0, test_send_proberesp, &ctx, NULL); | 
 | 199 | 	} else if (os_strcmp(argv[1], "action") == 0) { | 
 | 200 | 		eloop_register_timeout(0, 0, test_send_action, &ctx, NULL); | 
 | 201 | 	} else { | 
 | 202 | 		wpa_printf(MSG_ERROR, "Unsupported test type '%s'", argv[1]); | 
 | 203 | 		return -1; | 
 | 204 | 	} | 
 | 205 |  | 
 | 206 | 	wpa_printf(MSG_DEBUG, "Starting eloop"); | 
 | 207 | 	eloop_run(); | 
 | 208 | 	wpa_printf(MSG_DEBUG, "eloop done"); | 
 | 209 |  | 
 | 210 | 	p2p_deinit(p2p); | 
 | 211 | 	eloop_destroy(); | 
 | 212 | 	os_program_deinit(); | 
 | 213 |  | 
 | 214 | 	return 0; | 
 | 215 | } |