wlantest.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. dl_list_init(&wt->wep);
  56. }
  57. void radius_deinit(struct wlantest_radius *r)
  58. {
  59. dl_list_del(&r->list);
  60. os_free(r);
  61. }
  62. static void wlantest_deinit(struct wlantest *wt)
  63. {
  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. struct wlantest_wep *wep, *nw;
  69. if (wt->ctrl_sock >= 0)
  70. ctrl_deinit(wt);
  71. if (wt->monitor_sock >= 0)
  72. monitor_deinit(wt);
  73. bss_flush(wt);
  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. dl_list_for_each_safe(wep, nw, &wt->wep, struct wlantest_wep, list)
  85. os_free(wep);
  86. write_pcap_deinit(wt);
  87. }
  88. static void add_passphrase(struct wlantest *wt, const char *passphrase)
  89. {
  90. struct wlantest_passphrase *p;
  91. size_t len = os_strlen(passphrase);
  92. if (len < 8 || len > 63)
  93. return;
  94. p = os_zalloc(sizeof(*p));
  95. if (p == NULL)
  96. return;
  97. os_memcpy(p->passphrase, passphrase, len);
  98. dl_list_add(&wt->passphrase, &p->list);
  99. }
  100. static void add_secret(struct wlantest *wt, const char *secret)
  101. {
  102. struct wlantest_radius_secret *s;
  103. size_t len = os_strlen(secret);
  104. if (len >= MAX_RADIUS_SECRET_LEN)
  105. return;
  106. s = os_zalloc(sizeof(*s));
  107. if (s == NULL)
  108. return;
  109. os_memcpy(s->secret, secret, len);
  110. dl_list_add(&wt->secret, &s->list);
  111. }
  112. static void add_wep(struct wlantest *wt, const char *key)
  113. {
  114. struct wlantest_wep *w;
  115. size_t len = os_strlen(key);
  116. if (len != 2 * 5 && len != 2 * 13) {
  117. wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
  118. return;
  119. }
  120. w = os_zalloc(sizeof(*w));
  121. if (w == NULL)
  122. return;
  123. if (hexstr2bin(key, w->key, len / 2) < 0) {
  124. os_free(w);
  125. wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
  126. return;
  127. }
  128. w->key_len = len / 2;
  129. dl_list_add(&wt->wep, &w->list);
  130. }
  131. int main(int argc, char *argv[])
  132. {
  133. int c;
  134. const char *read_file = NULL;
  135. const char *read_wired_file = NULL;
  136. const char *write_file = NULL;
  137. const char *ifname = NULL;
  138. const char *ifname_wired = NULL;
  139. struct wlantest wt;
  140. int ctrl_iface = 0;
  141. wpa_debug_level = MSG_INFO;
  142. wpa_debug_show_keys = 1;
  143. if (os_program_init())
  144. return -1;
  145. wlantest_init(&wt);
  146. for (;;) {
  147. c = getopt(argc, argv, "cdhi:I:p:P:qr:R:w:W:");
  148. if (c < 0)
  149. break;
  150. switch (c) {
  151. case 'c':
  152. ctrl_iface = 1;
  153. break;
  154. case 'd':
  155. if (wpa_debug_level > 0)
  156. wpa_debug_level--;
  157. break;
  158. case 'h':
  159. usage();
  160. return 0;
  161. case 'i':
  162. ifname = optarg;
  163. break;
  164. case 'I':
  165. ifname_wired = optarg;
  166. break;
  167. case 'p':
  168. add_passphrase(&wt, optarg);
  169. break;
  170. case 'P':
  171. add_secret(&wt, optarg);
  172. break;
  173. case 'q':
  174. wpa_debug_level++;
  175. break;
  176. case 'r':
  177. read_file = optarg;
  178. break;
  179. case 'R':
  180. read_wired_file = optarg;
  181. break;
  182. case 'w':
  183. write_file = optarg;
  184. break;
  185. case 'W':
  186. add_wep(&wt, optarg);
  187. break;
  188. default:
  189. usage();
  190. return -1;
  191. }
  192. }
  193. if (ifname == NULL && ifname_wired == NULL &&
  194. read_file == NULL && read_wired_file == NULL) {
  195. usage();
  196. return 0;
  197. }
  198. if (eloop_init())
  199. return -1;
  200. if (write_file && write_pcap_init(&wt, write_file) < 0)
  201. return -1;
  202. if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
  203. return -1;
  204. if (read_file && read_cap_file(&wt, read_file) < 0)
  205. return -1;
  206. if (ifname && monitor_init(&wt, ifname) < 0)
  207. return -1;
  208. if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
  209. return -1;
  210. if (ctrl_iface && ctrl_init(&wt) < 0)
  211. return -1;
  212. eloop_register_signal_terminate(wlantest_terminate, &wt);
  213. eloop_run();
  214. wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
  215. "fcs_error=%u",
  216. wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
  217. wlantest_deinit(&wt);
  218. eloop_destroy();
  219. os_program_deinit();
  220. return 0;
  221. }