wlantest.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * wlantest - IEEE 802.11 protocol monitoring and testing tool
  3. * Copyright (c) 2010-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "wlantest.h"
  12. static void wlantest_terminate(int sig, void *signal_ctx)
  13. {
  14. eloop_terminate();
  15. }
  16. static void usage(void)
  17. {
  18. printf("wlantest [-cddhqqFNt] [-i<ifname>] [-r<pcap file>] "
  19. "[-p<passphrase>]\n"
  20. " [-I<wired ifname>] [-R<wired pcap file>] "
  21. "[-P<RADIUS shared secret>]\n"
  22. " [-n<write pcapng file>]\n"
  23. " [-w<write pcap file>] [-f<MSK/PMK file>]\n"
  24. " [-L<log file>] [-T<PTK file>]\n");
  25. }
  26. static void passphrase_deinit(struct wlantest_passphrase *p)
  27. {
  28. dl_list_del(&p->list);
  29. os_free(p);
  30. }
  31. static void secret_deinit(struct wlantest_radius_secret *r)
  32. {
  33. dl_list_del(&r->list);
  34. os_free(r);
  35. }
  36. static void wlantest_init(struct wlantest *wt)
  37. {
  38. int i;
  39. os_memset(wt, 0, sizeof(*wt));
  40. wt->monitor_sock = -1;
  41. wt->ctrl_sock = -1;
  42. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++)
  43. wt->ctrl_socks[i] = -1;
  44. dl_list_init(&wt->passphrase);
  45. dl_list_init(&wt->bss);
  46. dl_list_init(&wt->secret);
  47. dl_list_init(&wt->radius);
  48. dl_list_init(&wt->pmk);
  49. dl_list_init(&wt->ptk);
  50. dl_list_init(&wt->wep);
  51. }
  52. void radius_deinit(struct wlantest_radius *r)
  53. {
  54. dl_list_del(&r->list);
  55. os_free(r);
  56. }
  57. static void ptk_deinit(struct wlantest_ptk *ptk)
  58. {
  59. dl_list_del(&ptk->list);
  60. os_free(ptk);
  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_ptk *ptk, *npt;
  69. struct wlantest_wep *wep, *nw;
  70. if (wt->ctrl_sock >= 0)
  71. ctrl_deinit(wt);
  72. if (wt->monitor_sock >= 0)
  73. monitor_deinit(wt);
  74. bss_flush(wt);
  75. dl_list_for_each_safe(p, pn, &wt->passphrase,
  76. struct wlantest_passphrase, list)
  77. passphrase_deinit(p);
  78. dl_list_for_each_safe(s, sn, &wt->secret,
  79. struct wlantest_radius_secret, list)
  80. secret_deinit(s);
  81. dl_list_for_each_safe(r, rn, &wt->radius, struct wlantest_radius, list)
  82. radius_deinit(r);
  83. dl_list_for_each_safe(pmk, np, &wt->pmk, struct wlantest_pmk, list)
  84. pmk_deinit(pmk);
  85. dl_list_for_each_safe(ptk, npt, &wt->ptk, struct wlantest_ptk, list)
  86. ptk_deinit(ptk);
  87. dl_list_for_each_safe(wep, nw, &wt->wep, struct wlantest_wep, list)
  88. os_free(wep);
  89. write_pcap_deinit(wt);
  90. write_pcapng_deinit(wt);
  91. clear_notes(wt);
  92. os_free(wt->decrypted);
  93. wt->decrypted = NULL;
  94. }
  95. static void add_passphrase(struct wlantest *wt, const char *passphrase)
  96. {
  97. struct wlantest_passphrase *p;
  98. size_t len = os_strlen(passphrase);
  99. if (len < 8 || len > 63)
  100. return;
  101. p = os_zalloc(sizeof(*p));
  102. if (p == NULL)
  103. return;
  104. os_memcpy(p->passphrase, passphrase, len);
  105. dl_list_add(&wt->passphrase, &p->list);
  106. }
  107. static void add_secret(struct wlantest *wt, const char *secret)
  108. {
  109. struct wlantest_radius_secret *s;
  110. size_t len = os_strlen(secret);
  111. if (len >= MAX_RADIUS_SECRET_LEN)
  112. return;
  113. s = os_zalloc(sizeof(*s));
  114. if (s == NULL)
  115. return;
  116. os_memcpy(s->secret, secret, len);
  117. dl_list_add(&wt->secret, &s->list);
  118. }
  119. static int add_pmk_file(struct wlantest *wt, const char *pmk_file)
  120. {
  121. FILE *f;
  122. u8 pmk[32];
  123. char buf[300], *pos;
  124. struct wlantest_pmk *p;
  125. f = fopen(pmk_file, "r");
  126. if (f == NULL) {
  127. wpa_printf(MSG_ERROR, "Could not open '%s'", pmk_file);
  128. return -1;
  129. }
  130. while (fgets(buf, sizeof(buf), f)) {
  131. pos = buf;
  132. while (*pos && *pos != '\r' && *pos != '\n')
  133. pos++;
  134. *pos = '\0';
  135. if (pos - buf < 2 * 32)
  136. continue;
  137. if (hexstr2bin(buf, pmk, 32) < 0)
  138. continue;
  139. p = os_zalloc(sizeof(*p));
  140. if (p == NULL)
  141. break;
  142. os_memcpy(p->pmk, pmk, 32);
  143. dl_list_add(&wt->pmk, &p->list);
  144. wpa_hexdump(MSG_DEBUG, "Added PMK from file", pmk, 32);
  145. /* For FT, the send half of MSK is used */
  146. if (hexstr2bin(&buf[64], pmk, 32) < 0)
  147. continue;
  148. p = os_zalloc(sizeof(*p));
  149. if (p == NULL)
  150. break;
  151. os_memcpy(p->pmk, pmk, 32);
  152. dl_list_add(&wt->pmk, &p->list);
  153. wpa_hexdump(MSG_DEBUG, "Added PMK from file (2nd half of MSK)",
  154. pmk, 32);
  155. }
  156. fclose(f);
  157. return 0;
  158. }
  159. static int add_ptk_file(struct wlantest *wt, const char *ptk_file)
  160. {
  161. FILE *f;
  162. u8 ptk[64];
  163. size_t ptk_len;
  164. char buf[300], *pos;
  165. struct wlantest_ptk *p;
  166. f = fopen(ptk_file, "r");
  167. if (f == NULL) {
  168. wpa_printf(MSG_ERROR, "Could not open '%s'", ptk_file);
  169. return -1;
  170. }
  171. while (fgets(buf, sizeof(buf), f)) {
  172. pos = buf;
  173. while (*pos && *pos != '\r' && *pos != '\n')
  174. pos++;
  175. *pos = '\0';
  176. ptk_len = pos - buf;
  177. if (ptk_len & 1)
  178. continue;
  179. ptk_len /= 2;
  180. if (ptk_len != 16 && ptk_len != 32 &&
  181. ptk_len != 48 && ptk_len != 64)
  182. continue;
  183. if (hexstr2bin(buf, ptk, ptk_len) < 0)
  184. continue;
  185. p = os_zalloc(sizeof(*p));
  186. if (p == NULL)
  187. break;
  188. if (ptk_len < 48) {
  189. os_memcpy(p->ptk.tk, ptk, ptk_len);
  190. p->ptk.tk_len = ptk_len;
  191. p->ptk_len = 32 + ptk_len;
  192. } else {
  193. os_memcpy(p->ptk.kck, ptk, 16);
  194. p->ptk.kck_len = 16;
  195. os_memcpy(p->ptk.kek, ptk + 16, 16);
  196. p->ptk.kek_len = 16;
  197. os_memcpy(p->ptk.tk, ptk + 32, ptk_len - 32);
  198. p->ptk.tk_len = ptk_len - 32;
  199. p->ptk_len = ptk_len;
  200. }
  201. dl_list_add(&wt->ptk, &p->list);
  202. wpa_hexdump(MSG_DEBUG, "Added PTK from file", ptk, ptk_len);
  203. }
  204. fclose(f);
  205. return 0;
  206. }
  207. int add_wep(struct wlantest *wt, const char *key)
  208. {
  209. struct wlantest_wep *w;
  210. size_t len = os_strlen(key);
  211. if (len != 2 * 5 && len != 2 * 13) {
  212. wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
  213. return -1;
  214. }
  215. w = os_zalloc(sizeof(*w));
  216. if (w == NULL)
  217. return -1;
  218. if (hexstr2bin(key, w->key, len / 2) < 0) {
  219. os_free(w);
  220. wpa_printf(MSG_INFO, "Invalid WEP key '%s'", key);
  221. return -1;
  222. }
  223. w->key_len = len / 2;
  224. dl_list_add(&wt->wep, &w->list);
  225. return 0;
  226. }
  227. void add_note(struct wlantest *wt, int level, const char *fmt, ...)
  228. {
  229. va_list ap;
  230. size_t len = 1000;
  231. int wlen;
  232. if (wt->num_notes == MAX_NOTES)
  233. return;
  234. wt->notes[wt->num_notes] = os_malloc(len);
  235. if (wt->notes[wt->num_notes] == NULL)
  236. return;
  237. va_start(ap, fmt);
  238. wlen = vsnprintf(wt->notes[wt->num_notes], len, fmt, ap);
  239. va_end(ap);
  240. if (wlen < 0) {
  241. os_free(wt->notes[wt->num_notes]);
  242. wt->notes[wt->num_notes] = NULL;
  243. return;
  244. }
  245. if (wlen >= len)
  246. wt->notes[wt->num_notes][len - 1] = '\0';
  247. wpa_printf(level, "%s", wt->notes[wt->num_notes]);
  248. wt->num_notes++;
  249. }
  250. void clear_notes(struct wlantest *wt)
  251. {
  252. size_t i;
  253. for (i = 0; i < wt->num_notes; i++) {
  254. os_free(wt->notes[i]);
  255. wt->notes[i] = NULL;
  256. }
  257. wt->num_notes = 0;
  258. }
  259. size_t notes_len(struct wlantest *wt, size_t hdrlen)
  260. {
  261. size_t i;
  262. size_t len = wt->num_notes * hdrlen;
  263. for (i = 0; i < wt->num_notes; i++)
  264. len += os_strlen(wt->notes[i]);
  265. return len;
  266. }
  267. int wlantest_relog(struct wlantest *wt)
  268. {
  269. int ret = 0;
  270. wpa_printf(MSG_INFO, "Re-open log/capture files");
  271. if (wpa_debug_reopen_file())
  272. ret = -1;
  273. if (wt->write_file) {
  274. write_pcap_deinit(wt);
  275. if (write_pcap_init(wt, wt->write_file) < 0)
  276. ret = -1;
  277. }
  278. if (wt->pcapng_file) {
  279. write_pcapng_deinit(wt);
  280. if (write_pcapng_init(wt, wt->pcapng_file) < 0)
  281. ret = -1;
  282. }
  283. return ret;
  284. }
  285. int main(int argc, char *argv[])
  286. {
  287. int c;
  288. const char *read_file = NULL;
  289. const char *read_wired_file = NULL;
  290. const char *ifname = NULL;
  291. const char *ifname_wired = NULL;
  292. const char *logfile = NULL;
  293. struct wlantest wt;
  294. int ctrl_iface = 0;
  295. wpa_debug_level = MSG_INFO;
  296. wpa_debug_show_keys = 1;
  297. if (os_program_init())
  298. return -1;
  299. wlantest_init(&wt);
  300. for (;;) {
  301. c = getopt(argc, argv, "cdf:Fhi:I:L:n:Np:P:qr:R:tT:w:W:");
  302. if (c < 0)
  303. break;
  304. switch (c) {
  305. case 'c':
  306. ctrl_iface = 1;
  307. break;
  308. case 'd':
  309. if (wpa_debug_level > 0)
  310. wpa_debug_level--;
  311. break;
  312. case 'f':
  313. if (add_pmk_file(&wt, optarg) < 0)
  314. return -1;
  315. break;
  316. case 'F':
  317. wt.assume_fcs = 1;
  318. break;
  319. case 'h':
  320. usage();
  321. return 0;
  322. case 'i':
  323. ifname = optarg;
  324. break;
  325. case 'I':
  326. ifname_wired = optarg;
  327. break;
  328. case 'L':
  329. logfile = optarg;
  330. break;
  331. case 'n':
  332. wt.pcapng_file = optarg;
  333. break;
  334. case 'N':
  335. wt.pcap_no_buffer = 1;
  336. break;
  337. case 'p':
  338. add_passphrase(&wt, optarg);
  339. break;
  340. case 'P':
  341. add_secret(&wt, optarg);
  342. break;
  343. case 'q':
  344. wpa_debug_level++;
  345. break;
  346. case 'r':
  347. read_file = optarg;
  348. break;
  349. case 'R':
  350. read_wired_file = optarg;
  351. break;
  352. case 't':
  353. wpa_debug_timestamp = 1;
  354. break;
  355. case 'T':
  356. if (add_ptk_file(&wt, optarg) < 0)
  357. return -1;
  358. break;
  359. case 'w':
  360. wt.write_file = optarg;
  361. break;
  362. case 'W':
  363. if (add_wep(&wt, optarg) < 0)
  364. return -1;
  365. break;
  366. default:
  367. usage();
  368. return -1;
  369. }
  370. }
  371. if (ifname == NULL && ifname_wired == NULL &&
  372. read_file == NULL && read_wired_file == NULL) {
  373. usage();
  374. return 0;
  375. }
  376. if (eloop_init())
  377. return -1;
  378. if (logfile)
  379. wpa_debug_open_file(logfile);
  380. if (wt.write_file && write_pcap_init(&wt, wt.write_file) < 0)
  381. return -1;
  382. if (wt.pcapng_file && write_pcapng_init(&wt, wt.pcapng_file) < 0)
  383. return -1;
  384. if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
  385. return -1;
  386. if (read_file && read_cap_file(&wt, read_file) < 0)
  387. return -1;
  388. if (ifname && monitor_init(&wt, ifname) < 0)
  389. return -1;
  390. if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
  391. return -1;
  392. if (ctrl_iface && ctrl_init(&wt) < 0)
  393. return -1;
  394. eloop_register_signal_terminate(wlantest_terminate, &wt);
  395. eloop_run();
  396. wpa_printf(MSG_INFO, "Processed: rx_mgmt=%u rx_ctrl=%u rx_data=%u "
  397. "fcs_error=%u",
  398. wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
  399. wlantest_deinit(&wt);
  400. wpa_debug_close_file();
  401. eloop_destroy();
  402. os_program_deinit();
  403. return 0;
  404. }