wlantest.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 [-cddhqq] [-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. int i;
  45. os_memset(wt, 0, sizeof(*wt));
  46. wt->monitor_sock = -1;
  47. wt->ctrl_sock = -1;
  48. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++)
  49. wt->ctrl_socks[i] = -1;
  50. dl_list_init(&wt->passphrase);
  51. dl_list_init(&wt->bss);
  52. dl_list_init(&wt->secret);
  53. dl_list_init(&wt->radius);
  54. dl_list_init(&wt->pmk);
  55. }
  56. void radius_deinit(struct wlantest_radius *r)
  57. {
  58. dl_list_del(&r->list);
  59. os_free(r);
  60. }
  61. static void wlantest_deinit(struct wlantest *wt)
  62. {
  63. struct wlantest_bss *bss, *n;
  64. struct wlantest_passphrase *p, *pn;
  65. struct wlantest_radius_secret *s, *sn;
  66. struct wlantest_radius *r, *rn;
  67. struct wlantest_pmk *pmk, *np;
  68. if (wt->ctrl_sock >= 0)
  69. ctrl_deinit(wt);
  70. if (wt->monitor_sock >= 0)
  71. monitor_deinit(wt);
  72. dl_list_for_each_safe(bss, n, &wt->bss, struct wlantest_bss, list)
  73. bss_deinit(bss);
  74. dl_list_for_each_safe(p, pn, &wt->passphrase,
  75. struct wlantest_passphrase, list)
  76. passphrase_deinit(p);
  77. dl_list_for_each_safe(s, sn, &wt->secret,
  78. struct wlantest_radius_secret, list)
  79. secret_deinit(s);
  80. dl_list_for_each_safe(r, rn, &wt->radius, struct wlantest_radius, list)
  81. radius_deinit(r);
  82. dl_list_for_each_safe(pmk, np, &wt->pmk, struct wlantest_pmk, list)
  83. pmk_deinit(pmk);
  84. write_pcap_deinit(wt);
  85. }
  86. static void add_passphrase(struct wlantest *wt, const char *passphrase)
  87. {
  88. struct wlantest_passphrase *p;
  89. size_t len = os_strlen(passphrase);
  90. if (len < 8 || len > 63)
  91. return;
  92. p = os_zalloc(sizeof(*p));
  93. if (p == NULL)
  94. return;
  95. os_memcpy(p->passphrase, passphrase, len);
  96. dl_list_add(&wt->passphrase, &p->list);
  97. }
  98. static void add_secret(struct wlantest *wt, const char *secret)
  99. {
  100. struct wlantest_radius_secret *s;
  101. size_t len = os_strlen(secret);
  102. if (len >= MAX_RADIUS_SECRET_LEN)
  103. return;
  104. s = os_zalloc(sizeof(*s));
  105. if (s == NULL)
  106. return;
  107. os_memcpy(s->secret, secret, len);
  108. dl_list_add(&wt->secret, &s->list);
  109. }
  110. int main(int argc, char *argv[])
  111. {
  112. int c;
  113. const char *read_file = NULL;
  114. const char *read_wired_file = NULL;
  115. const char *write_file = NULL;
  116. const char *ifname = NULL;
  117. const char *ifname_wired = NULL;
  118. struct wlantest wt;
  119. int ctrl_iface = 0;
  120. wpa_debug_level = MSG_INFO;
  121. wpa_debug_show_keys = 1;
  122. if (os_program_init())
  123. return -1;
  124. wlantest_init(&wt);
  125. for (;;) {
  126. c = getopt(argc, argv, "cdhi:I:p:P:qr:R:w:");
  127. if (c < 0)
  128. break;
  129. switch (c) {
  130. case 'c':
  131. ctrl_iface = 1;
  132. break;
  133. case 'd':
  134. if (wpa_debug_level > 0)
  135. wpa_debug_level--;
  136. break;
  137. case 'h':
  138. usage();
  139. return 0;
  140. case 'i':
  141. ifname = optarg;
  142. break;
  143. case 'I':
  144. ifname_wired = optarg;
  145. break;
  146. case 'p':
  147. add_passphrase(&wt, optarg);
  148. break;
  149. case 'P':
  150. add_secret(&wt, optarg);
  151. break;
  152. case 'q':
  153. wpa_debug_level++;
  154. break;
  155. case 'r':
  156. read_file = optarg;
  157. break;
  158. case 'R':
  159. read_wired_file = optarg;
  160. break;
  161. case 'w':
  162. write_file = optarg;
  163. break;
  164. default:
  165. usage();
  166. return -1;
  167. }
  168. }
  169. if (ifname == NULL && ifname_wired == NULL &&
  170. read_file == NULL && read_wired_file == NULL) {
  171. usage();
  172. return 0;
  173. }
  174. if (eloop_init())
  175. return -1;
  176. if (write_file && write_pcap_init(&wt, write_file) < 0)
  177. return -1;
  178. if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
  179. return -1;
  180. if (read_file && read_cap_file(&wt, read_file) < 0)
  181. return -1;
  182. if (ifname && monitor_init(&wt, ifname) < 0)
  183. return -1;
  184. if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
  185. return -1;
  186. if (ctrl_iface && ctrl_init(&wt) < 0)
  187. return -1;
  188. eloop_register_signal_terminate(wlantest_terminate, &wt);
  189. eloop_run();
  190. wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
  191. "fcs_error=%u",
  192. wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
  193. wlantest_deinit(&wt);
  194. eloop_destroy();
  195. os_program_deinit();
  196. return 0;
  197. }