wlantest.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * wlantest - IEEE 802.11 protocol monitoring and testing tool
  3. * Copyright (c) 2010-2011, 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>] [-f<MSK/PMK 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 int add_pmk_file(struct wlantest *wt, const char *pmk_file)
  113. {
  114. FILE *f;
  115. u8 pmk[32];
  116. char buf[300], *pos;
  117. struct wlantest_pmk *p;
  118. f = fopen(pmk_file, "r");
  119. if (f == NULL) {
  120. wpa_printf(MSG_ERROR, "Could not open '%s'", pmk_file);
  121. return -1;
  122. }
  123. while (fgets(buf, sizeof(buf), f)) {
  124. pos = buf;
  125. while (*pos && *pos != '\r' && *pos != '\n')
  126. pos++;
  127. *pos = '\0';
  128. if (pos - buf < 2 * 32)
  129. continue;
  130. if (hexstr2bin(buf, pmk, 32) < 0)
  131. continue;
  132. p = os_zalloc(sizeof(*p));
  133. if (p == NULL)
  134. break;
  135. os_memcpy(p->pmk, pmk, 32);
  136. dl_list_add(&wt->pmk, &p->list);
  137. wpa_hexdump(MSG_DEBUG, "Added PMK from file", pmk, 32);
  138. }
  139. fclose(f);
  140. return 0;
  141. }
  142. int add_wep(struct wlantest *wt, const char *key)
  143. {
  144. struct wlantest_wep *w;
  145. size_t len = os_strlen(key);
  146. if (len != 2 * 5 && len != 2 * 13) {
  147. wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
  148. return -1;
  149. }
  150. w = os_zalloc(sizeof(*w));
  151. if (w == NULL)
  152. return -1;
  153. if (hexstr2bin(key, w->key, len / 2) < 0) {
  154. os_free(w);
  155. wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
  156. return -1;
  157. }
  158. w->key_len = len / 2;
  159. dl_list_add(&wt->wep, &w->list);
  160. return 0;
  161. }
  162. int main(int argc, char *argv[])
  163. {
  164. int c;
  165. const char *read_file = NULL;
  166. const char *read_wired_file = NULL;
  167. const char *write_file = NULL;
  168. const char *ifname = NULL;
  169. const char *ifname_wired = NULL;
  170. struct wlantest wt;
  171. int ctrl_iface = 0;
  172. wpa_debug_level = MSG_INFO;
  173. wpa_debug_show_keys = 1;
  174. if (os_program_init())
  175. return -1;
  176. wlantest_init(&wt);
  177. for (;;) {
  178. c = getopt(argc, argv, "cdf:hi:I:p:P:qr:R:w:W:");
  179. if (c < 0)
  180. break;
  181. switch (c) {
  182. case 'c':
  183. ctrl_iface = 1;
  184. break;
  185. case 'd':
  186. if (wpa_debug_level > 0)
  187. wpa_debug_level--;
  188. break;
  189. case 'f':
  190. if (add_pmk_file(&wt, optarg) < 0)
  191. return -1;
  192. break;
  193. case 'h':
  194. usage();
  195. return 0;
  196. case 'i':
  197. ifname = optarg;
  198. break;
  199. case 'I':
  200. ifname_wired = optarg;
  201. break;
  202. case 'p':
  203. add_passphrase(&wt, optarg);
  204. break;
  205. case 'P':
  206. add_secret(&wt, optarg);
  207. break;
  208. case 'q':
  209. wpa_debug_level++;
  210. break;
  211. case 'r':
  212. read_file = optarg;
  213. break;
  214. case 'R':
  215. read_wired_file = optarg;
  216. break;
  217. case 'w':
  218. write_file = optarg;
  219. break;
  220. case 'W':
  221. if (add_wep(&wt, optarg) < 0)
  222. return -1;
  223. break;
  224. default:
  225. usage();
  226. return -1;
  227. }
  228. }
  229. if (ifname == NULL && ifname_wired == NULL &&
  230. read_file == NULL && read_wired_file == NULL) {
  231. usage();
  232. return 0;
  233. }
  234. if (eloop_init())
  235. return -1;
  236. if (write_file && write_pcap_init(&wt, write_file) < 0)
  237. return -1;
  238. if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
  239. return -1;
  240. if (read_file && read_cap_file(&wt, read_file) < 0)
  241. return -1;
  242. if (ifname && monitor_init(&wt, ifname) < 0)
  243. return -1;
  244. if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
  245. return -1;
  246. if (ctrl_iface && ctrl_init(&wt) < 0)
  247. return -1;
  248. eloop_register_signal_terminate(wlantest_terminate, &wt);
  249. eloop_run();
  250. wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
  251. "fcs_error=%u",
  252. wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
  253. wlantest_deinit(&wt);
  254. eloop_destroy();
  255. os_program_deinit();
  256. return 0;
  257. }