wlantest.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * wlantest - IEEE 802.11 protocol monitoring and testing tool
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "wlantest.h"
  18. extern int wpa_debug_level;
  19. extern int wpa_debug_show_keys;
  20. static void wlantest_terminate(int sig, void *signal_ctx)
  21. {
  22. eloop_terminate();
  23. }
  24. static void usage(void)
  25. {
  26. printf("wlantest [-ddhqq] [-i<ifname>] [-r<pcap file>] "
  27. "[-p<passphrase>]\n"
  28. " [-I<wired ifname>] [-R<wired pcap file>] "
  29. "[-P<RADIUS shared secret>]\n"
  30. " [-w<write pcap file>]\n");
  31. }
  32. static void passphrase_deinit(struct wlantest_passphrase *p)
  33. {
  34. dl_list_del(&p->list);
  35. os_free(p);
  36. }
  37. static void secret_deinit(struct wlantest_radius_secret *r)
  38. {
  39. dl_list_del(&r->list);
  40. os_free(r);
  41. }
  42. static void wlantest_init(struct wlantest *wt)
  43. {
  44. os_memset(wt, 0, sizeof(*wt));
  45. wt->monitor_sock = -1;
  46. dl_list_init(&wt->passphrase);
  47. dl_list_init(&wt->bss);
  48. dl_list_init(&wt->secret);
  49. dl_list_init(&wt->radius);
  50. dl_list_init(&wt->pmk);
  51. }
  52. void radius_deinit(struct wlantest_radius *r)
  53. {
  54. dl_list_del(&r->list);
  55. os_free(r);
  56. }
  57. static void wlantest_deinit(struct wlantest *wt)
  58. {
  59. struct wlantest_bss *bss, *n;
  60. struct wlantest_passphrase *p, *pn;
  61. struct wlantest_radius_secret *s, *sn;
  62. struct wlantest_radius *r, *rn;
  63. struct wlantest_pmk *pmk, *np;
  64. if (wt->monitor_sock >= 0)
  65. monitor_deinit(wt);
  66. dl_list_for_each_safe(bss, n, &wt->bss, struct wlantest_bss, list)
  67. bss_deinit(bss);
  68. dl_list_for_each_safe(p, pn, &wt->passphrase,
  69. struct wlantest_passphrase, list)
  70. passphrase_deinit(p);
  71. dl_list_for_each_safe(s, sn, &wt->secret,
  72. struct wlantest_radius_secret, list)
  73. secret_deinit(s);
  74. dl_list_for_each_safe(r, rn, &wt->radius, struct wlantest_radius, list)
  75. radius_deinit(r);
  76. dl_list_for_each_safe(pmk, np, &wt->pmk, struct wlantest_pmk, list)
  77. pmk_deinit(pmk);
  78. write_pcap_deinit(wt);
  79. }
  80. static void add_passphrase(struct wlantest *wt, const char *passphrase)
  81. {
  82. struct wlantest_passphrase *p;
  83. size_t len = os_strlen(passphrase);
  84. if (len < 8 || len > 63)
  85. return;
  86. p = os_zalloc(sizeof(*p));
  87. if (p == NULL)
  88. return;
  89. os_memcpy(p->passphrase, passphrase, len);
  90. dl_list_add(&wt->passphrase, &p->list);
  91. }
  92. static void add_secret(struct wlantest *wt, const char *secret)
  93. {
  94. struct wlantest_radius_secret *s;
  95. size_t len = os_strlen(secret);
  96. if (len >= MAX_RADIUS_SECRET_LEN)
  97. return;
  98. s = os_zalloc(sizeof(*s));
  99. if (s == NULL)
  100. return;
  101. os_memcpy(s->secret, secret, len);
  102. dl_list_add(&wt->secret, &s->list);
  103. }
  104. int main(int argc, char *argv[])
  105. {
  106. int c;
  107. const char *read_file = NULL;
  108. const char *read_wired_file = NULL;
  109. const char *write_file = NULL;
  110. const char *ifname = NULL;
  111. const char *ifname_wired = NULL;
  112. struct wlantest wt;
  113. wpa_debug_level = MSG_INFO;
  114. wpa_debug_show_keys = 1;
  115. if (os_program_init())
  116. return -1;
  117. wlantest_init(&wt);
  118. for (;;) {
  119. c = getopt(argc, argv, "dhi:I:p:P:qr:R:w:");
  120. if (c < 0)
  121. break;
  122. switch (c) {
  123. case 'd':
  124. if (wpa_debug_level > 0)
  125. wpa_debug_level--;
  126. break;
  127. case 'h':
  128. usage();
  129. return 0;
  130. case 'i':
  131. ifname = optarg;
  132. break;
  133. case 'I':
  134. ifname_wired = optarg;
  135. break;
  136. case 'p':
  137. add_passphrase(&wt, optarg);
  138. break;
  139. case 'P':
  140. add_secret(&wt, optarg);
  141. break;
  142. case 'q':
  143. wpa_debug_level++;
  144. break;
  145. case 'r':
  146. read_file = optarg;
  147. break;
  148. case 'R':
  149. read_wired_file = optarg;
  150. break;
  151. case 'w':
  152. write_file = optarg;
  153. break;
  154. default:
  155. usage();
  156. return -1;
  157. }
  158. }
  159. if (ifname == NULL && ifname_wired == NULL &&
  160. read_file == NULL && read_wired_file == NULL) {
  161. usage();
  162. return 0;
  163. }
  164. if (eloop_init())
  165. return -1;
  166. if (write_file && write_pcap_init(&wt, write_file) < 0)
  167. return -1;
  168. if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
  169. return -1;
  170. if (read_file && read_cap_file(&wt, read_file) < 0)
  171. return -1;
  172. if (ifname && monitor_init(&wt, ifname) < 0)
  173. return -1;
  174. if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
  175. return -1;
  176. eloop_register_signal_terminate(wlantest_terminate, &wt);
  177. eloop_run();
  178. wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
  179. "fcs_error=%u",
  180. wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
  181. wlantest_deinit(&wt);
  182. eloop_destroy();
  183. os_program_deinit();
  184. return 0;
  185. }