p2p-fuzzer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "p2p/p2p.h"
  13. static void debug_print(void *ctx, int level, const char *msg)
  14. {
  15. wpa_printf(level, "P2P: %s", msg);
  16. }
  17. static void find_stopped(void *ctx)
  18. {
  19. }
  20. static int start_listen(void *ctx, unsigned int freq,
  21. unsigned int duration,
  22. const struct wpabuf *probe_resp_ie)
  23. {
  24. return 0;
  25. }
  26. static void stop_listen(void *ctx)
  27. {
  28. }
  29. static void dev_found(void *ctx, const u8 *addr,
  30. const struct p2p_peer_info *info,
  31. int new_device)
  32. {
  33. }
  34. static void dev_lost(void *ctx, const u8 *dev_addr)
  35. {
  36. }
  37. static int send_action(void *ctx, unsigned int freq, const u8 *dst,
  38. const u8 *src, const u8 *bssid, const u8 *buf,
  39. size_t len, unsigned int wait_time)
  40. {
  41. return 0;
  42. }
  43. static void send_action_done(void *ctx)
  44. {
  45. }
  46. static void go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  47. {
  48. }
  49. static struct p2p_data * init_p2p(void)
  50. {
  51. struct p2p_config p2p;
  52. os_memset(&p2p, 0, sizeof(p2p));
  53. p2p.max_peers = 100;
  54. p2p.passphrase_len = 8;
  55. p2p.channels.reg_classes = 1;
  56. p2p.channels.reg_class[0].reg_class = 81;
  57. p2p.channels.reg_class[0].channel[0] = 1;
  58. p2p.channels.reg_class[0].channel[1] = 2;
  59. p2p.channels.reg_class[0].channels = 2;
  60. p2p.debug_print = debug_print;
  61. p2p.find_stopped = find_stopped;
  62. p2p.start_listen = start_listen;
  63. p2p.stop_listen = stop_listen;
  64. p2p.dev_found = dev_found;
  65. p2p.dev_lost = dev_lost;
  66. p2p.send_action = send_action;
  67. p2p.send_action_done = send_action_done;
  68. p2p.go_neg_req_rx = go_neg_req_rx;
  69. return p2p_init(&p2p);
  70. }
  71. struct arg_ctx {
  72. struct p2p_data *p2p;
  73. const char *fname;
  74. };
  75. static void test_send_proberesp(void *eloop_data, void *user_ctx)
  76. {
  77. struct arg_ctx *ctx = eloop_data;
  78. char *data;
  79. size_t len;
  80. struct os_reltime rx_time;
  81. wpa_printf(MSG_INFO, "p2p-fuzzer: Send proberesp '%s'", ctx->fname);
  82. data = os_readfile(ctx->fname, &len);
  83. if (!data) {
  84. wpa_printf(MSG_ERROR, "Could not read '%s'", ctx->fname);
  85. return;
  86. }
  87. wpa_hexdump(MSG_MSGDUMP, "fuzzer - IEs", data, len);
  88. os_memset(&rx_time, 0, sizeof(rx_time));
  89. p2p_scan_res_handler(ctx->p2p, (u8 *) "\x02\x00\x00\x00\x01\x00", 2412,
  90. &rx_time, 0, (u8 *) data, len);
  91. p2p_scan_res_handled(ctx->p2p);
  92. os_free(data);
  93. eloop_terminate();
  94. }
  95. static void test_send_action(void *eloop_data, void *user_ctx)
  96. {
  97. struct arg_ctx *ctx = eloop_data;
  98. char *data;
  99. size_t len;
  100. struct os_reltime rx_time;
  101. struct ieee80211_mgmt *mgmt;
  102. wpa_printf(MSG_INFO, "p2p-fuzzer: Send action '%s'", ctx->fname);
  103. data = os_readfile(ctx->fname, &len);
  104. if (!data) {
  105. wpa_printf(MSG_ERROR, "Could not read '%s'", ctx->fname);
  106. return;
  107. }
  108. if (len < IEEE80211_HDRLEN + 1)
  109. goto out;
  110. wpa_hexdump(MSG_MSGDUMP, "fuzzer - action", data, len);
  111. mgmt = (struct ieee80211_mgmt *) data;
  112. os_memset(&rx_time, 0, sizeof(rx_time));
  113. p2p_rx_action(ctx->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
  114. mgmt->u.action.category,
  115. (u8 *) data + IEEE80211_HDRLEN + 1,
  116. len - IEEE80211_HDRLEN - 1, 2412);
  117. out:
  118. os_free(data);
  119. eloop_terminate();
  120. }
  121. int main(int argc, char *argv[])
  122. {
  123. struct p2p_data *p2p;
  124. struct arg_ctx ctx;
  125. /* TODO: probreq and wpas_p2p_probe_req_rx() */
  126. if (argc < 3) {
  127. printf("usage: %s <proberesp|action> <file>\n", argv[0]);
  128. return -1;
  129. }
  130. if (os_program_init())
  131. return -1;
  132. wpa_debug_level = 0;
  133. wpa_debug_show_keys = 1;
  134. if (eloop_init()) {
  135. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  136. return -1;
  137. }
  138. p2p = init_p2p();
  139. if (!p2p) {
  140. wpa_printf(MSG_ERROR, "P2P init failed");
  141. return -1;
  142. }
  143. ctx.p2p = p2p;
  144. ctx.fname = argv[2];
  145. if (os_strcmp(argv[1], "proberesp") == 0) {
  146. eloop_register_timeout(0, 0, test_send_proberesp, &ctx, NULL);
  147. } else if (os_strcmp(argv[1], "action") == 0) {
  148. eloop_register_timeout(0, 0, test_send_action, &ctx, NULL);
  149. } else {
  150. wpa_printf(MSG_ERROR, "Unsupported test type '%s'", argv[1]);
  151. return -1;
  152. }
  153. wpa_printf(MSG_DEBUG, "Starting eloop");
  154. eloop_run();
  155. wpa_printf(MSG_DEBUG, "eloop done");
  156. p2p_deinit(p2p);
  157. eloop_destroy();
  158. os_program_deinit();
  159. return 0;
  160. }