wlantest.c 9.1 KB

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