wlantest.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. int 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 -1;
  119. }
  120. w = os_zalloc(sizeof(*w));
  121. if (w == NULL)
  122. return -1;
  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 -1;
  127. }
  128. w->key_len = len / 2;
  129. dl_list_add(&wt->wep, &w->list);
  130. return 0;
  131. }
  132. int main(int argc, char *argv[])
  133. {
  134. int c;
  135. const char *read_file = NULL;
  136. const char *read_wired_file = NULL;
  137. const char *write_file = NULL;
  138. const char *ifname = NULL;
  139. const char *ifname_wired = NULL;
  140. struct wlantest wt;
  141. int ctrl_iface = 0;
  142. wpa_debug_level = MSG_INFO;
  143. wpa_debug_show_keys = 1;
  144. if (os_program_init())
  145. return -1;
  146. wlantest_init(&wt);
  147. for (;;) {
  148. c = getopt(argc, argv, "cdhi:I:p:P:qr:R:w:W:");
  149. if (c < 0)
  150. break;
  151. switch (c) {
  152. case 'c':
  153. ctrl_iface = 1;
  154. break;
  155. case 'd':
  156. if (wpa_debug_level > 0)
  157. wpa_debug_level--;
  158. break;
  159. case 'h':
  160. usage();
  161. return 0;
  162. case 'i':
  163. ifname = optarg;
  164. break;
  165. case 'I':
  166. ifname_wired = optarg;
  167. break;
  168. case 'p':
  169. add_passphrase(&wt, optarg);
  170. break;
  171. case 'P':
  172. add_secret(&wt, optarg);
  173. break;
  174. case 'q':
  175. wpa_debug_level++;
  176. break;
  177. case 'r':
  178. read_file = optarg;
  179. break;
  180. case 'R':
  181. read_wired_file = optarg;
  182. break;
  183. case 'w':
  184. write_file = optarg;
  185. break;
  186. case 'W':
  187. if (add_wep(&wt, optarg) < 0)
  188. return -1;
  189. break;
  190. default:
  191. usage();
  192. return -1;
  193. }
  194. }
  195. if (ifname == NULL && ifname_wired == NULL &&
  196. read_file == NULL && read_wired_file == NULL) {
  197. usage();
  198. return 0;
  199. }
  200. if (eloop_init())
  201. return -1;
  202. if (write_file && write_pcap_init(&wt, write_file) < 0)
  203. return -1;
  204. if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
  205. return -1;
  206. if (read_file && read_cap_file(&wt, read_file) < 0)
  207. return -1;
  208. if (ifname && monitor_init(&wt, ifname) < 0)
  209. return -1;
  210. if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
  211. return -1;
  212. if (ctrl_iface && ctrl_init(&wt) < 0)
  213. return -1;
  214. eloop_register_signal_terminate(wlantest_terminate, &wt);
  215. eloop_run();
  216. wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
  217. "fcs_error=%u",
  218. wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
  219. wlantest_deinit(&wt);
  220. eloop_destroy();
  221. os_program_deinit();
  222. return 0;
  223. }