ctrl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /*
  2. * wlantest control interface
  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 <sys/un.h>
  16. #include "utils/common.h"
  17. #include "utils/eloop.h"
  18. #include "common/version.h"
  19. #include "common/ieee802_11_defs.h"
  20. #include "wlantest.h"
  21. #include "wlantest_ctrl.h"
  22. static u8 * attr_get(u8 *buf, size_t buflen, enum wlantest_ctrl_attr attr,
  23. size_t *len)
  24. {
  25. u8 *pos = buf;
  26. while (pos + 8 <= buf + buflen) {
  27. enum wlantest_ctrl_attr a;
  28. size_t alen;
  29. a = WPA_GET_BE32(pos);
  30. pos += 4;
  31. alen = WPA_GET_BE32(pos);
  32. pos += 4;
  33. if (pos + alen > buf + buflen) {
  34. wpa_printf(MSG_DEBUG, "Invalid control message "
  35. "attribute");
  36. return NULL;
  37. }
  38. if (a == attr) {
  39. *len = alen;
  40. return pos;
  41. }
  42. pos += alen;
  43. }
  44. return NULL;
  45. }
  46. static u8 * attr_get_macaddr(u8 *buf, size_t buflen,
  47. enum wlantest_ctrl_attr attr)
  48. {
  49. u8 *addr;
  50. size_t addr_len;
  51. addr = attr_get(buf, buflen, attr, &addr_len);
  52. if (addr && addr_len != ETH_ALEN)
  53. addr = NULL;
  54. return addr;
  55. }
  56. static int attr_get_int(u8 *buf, size_t buflen, enum wlantest_ctrl_attr attr)
  57. {
  58. u8 *pos;
  59. size_t len;
  60. pos = attr_get(buf, buflen, attr, &len);
  61. if (pos == NULL || len != 4)
  62. return -1;
  63. return WPA_GET_BE32(pos);
  64. }
  65. static u8 * attr_add_str(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  66. const char *str)
  67. {
  68. size_t len = os_strlen(str);
  69. if (pos == NULL || end - pos < 8 + len)
  70. return NULL;
  71. WPA_PUT_BE32(pos, attr);
  72. pos += 4;
  73. WPA_PUT_BE32(pos, len);
  74. pos += 4;
  75. os_memcpy(pos, str, len);
  76. pos += len;
  77. return pos;
  78. }
  79. static u8 * attr_add_be32(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  80. u32 val)
  81. {
  82. if (pos == NULL || end - pos < 12)
  83. return NULL;
  84. WPA_PUT_BE32(pos, attr);
  85. pos += 4;
  86. WPA_PUT_BE32(pos, 4);
  87. pos += 4;
  88. WPA_PUT_BE32(pos, val);
  89. pos += 4;
  90. return pos;
  91. }
  92. static void ctrl_disconnect(struct wlantest *wt, int sock)
  93. {
  94. int i;
  95. wpa_printf(MSG_DEBUG, "Disconnect control interface connection %d",
  96. sock);
  97. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++) {
  98. if (wt->ctrl_socks[i] == sock) {
  99. close(wt->ctrl_socks[i]);
  100. eloop_unregister_read_sock(wt->ctrl_socks[i]);
  101. wt->ctrl_socks[i] = -1;
  102. break;
  103. }
  104. }
  105. }
  106. static void ctrl_send(struct wlantest *wt, int sock, const u8 *buf,
  107. size_t len)
  108. {
  109. if (send(sock, buf, len, 0) < 0) {
  110. wpa_printf(MSG_INFO, "send(ctrl): %s", strerror(errno));
  111. ctrl_disconnect(wt, sock);
  112. }
  113. }
  114. static void ctrl_send_simple(struct wlantest *wt, int sock,
  115. enum wlantest_ctrl_cmd cmd)
  116. {
  117. u8 buf[4];
  118. WPA_PUT_BE32(buf, cmd);
  119. ctrl_send(wt, sock, buf, sizeof(buf));
  120. }
  121. static void ctrl_list_bss(struct wlantest *wt, int sock)
  122. {
  123. u8 buf[WLANTEST_CTRL_MAX_RESP_LEN], *pos, *len;
  124. struct wlantest_bss *bss;
  125. pos = buf;
  126. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  127. pos += 4;
  128. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  129. pos += 4;
  130. len = pos; /* to be filled */
  131. pos += 4;
  132. dl_list_for_each(bss, &wt->bss, struct wlantest_bss, list) {
  133. if (pos + ETH_ALEN > buf + WLANTEST_CTRL_MAX_RESP_LEN)
  134. break;
  135. os_memcpy(pos, bss->bssid, ETH_ALEN);
  136. pos += ETH_ALEN;
  137. }
  138. WPA_PUT_BE32(len, pos - len - 4);
  139. ctrl_send(wt, sock, buf, pos - buf);
  140. }
  141. static void ctrl_list_sta(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
  142. {
  143. u8 buf[WLANTEST_CTRL_MAX_RESP_LEN], *pos, *len;
  144. u8 *bssid;
  145. size_t bssid_len;
  146. struct wlantest_bss *bss;
  147. struct wlantest_sta *sta;
  148. bssid = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &bssid_len);
  149. if (bssid == NULL || bssid_len != ETH_ALEN) {
  150. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  151. return;
  152. }
  153. bss = bss_get(wt, bssid);
  154. if (bss == NULL) {
  155. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  156. return;
  157. }
  158. pos = buf;
  159. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  160. pos += 4;
  161. WPA_PUT_BE32(pos, WLANTEST_ATTR_STA_ADDR);
  162. pos += 4;
  163. len = pos; /* to be filled */
  164. pos += 4;
  165. dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
  166. if (pos + ETH_ALEN > buf + WLANTEST_CTRL_MAX_RESP_LEN)
  167. break;
  168. os_memcpy(pos, sta->addr, ETH_ALEN);
  169. pos += ETH_ALEN;
  170. }
  171. WPA_PUT_BE32(len, pos - len - 4);
  172. ctrl_send(wt, sock, buf, pos - buf);
  173. }
  174. static void ctrl_flush(struct wlantest *wt, int sock)
  175. {
  176. wpa_printf(MSG_DEBUG, "Drop all collected BSS data");
  177. bss_flush(wt);
  178. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  179. }
  180. static void ctrl_clear_sta_counters(struct wlantest *wt, int sock, u8 *cmd,
  181. size_t clen)
  182. {
  183. u8 *addr;
  184. size_t addr_len;
  185. struct wlantest_bss *bss;
  186. struct wlantest_sta *sta;
  187. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &addr_len);
  188. if (addr == NULL || addr_len != ETH_ALEN) {
  189. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  190. return;
  191. }
  192. bss = bss_get(wt, addr);
  193. if (bss == NULL) {
  194. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  195. return;
  196. }
  197. addr = attr_get(cmd, clen, WLANTEST_ATTR_STA_ADDR, &addr_len);
  198. if (addr == NULL || addr_len != ETH_ALEN) {
  199. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  200. return;
  201. }
  202. sta = sta_get(bss, addr);
  203. if (sta == NULL) {
  204. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  205. return;
  206. }
  207. os_memset(sta->counters, 0, sizeof(sta->counters));
  208. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  209. }
  210. static void ctrl_clear_bss_counters(struct wlantest *wt, int sock, u8 *cmd,
  211. size_t clen)
  212. {
  213. u8 *addr;
  214. size_t addr_len;
  215. struct wlantest_bss *bss;
  216. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &addr_len);
  217. if (addr == NULL || addr_len != ETH_ALEN) {
  218. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  219. return;
  220. }
  221. bss = bss_get(wt, addr);
  222. if (bss == NULL) {
  223. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  224. return;
  225. }
  226. os_memset(bss->counters, 0, sizeof(bss->counters));
  227. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  228. }
  229. static void ctrl_get_sta_counter(struct wlantest *wt, int sock, u8 *cmd,
  230. size_t clen)
  231. {
  232. u8 *addr;
  233. size_t addr_len;
  234. struct wlantest_bss *bss;
  235. struct wlantest_sta *sta;
  236. u32 counter;
  237. u8 buf[4 + 12], *end, *pos;
  238. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &addr_len);
  239. if (addr == NULL || addr_len != ETH_ALEN) {
  240. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  241. return;
  242. }
  243. bss = bss_get(wt, addr);
  244. if (bss == NULL) {
  245. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  246. return;
  247. }
  248. addr = attr_get(cmd, clen, WLANTEST_ATTR_STA_ADDR, &addr_len);
  249. if (addr == NULL || addr_len != ETH_ALEN) {
  250. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  251. return;
  252. }
  253. sta = sta_get(bss, addr);
  254. if (sta == NULL) {
  255. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  256. return;
  257. }
  258. addr = attr_get(cmd, clen, WLANTEST_ATTR_STA_COUNTER, &addr_len);
  259. if (addr == NULL || addr_len != 4) {
  260. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  261. return;
  262. }
  263. counter = WPA_GET_BE32(addr);
  264. if (counter >= NUM_WLANTEST_STA_COUNTER) {
  265. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  266. return;
  267. }
  268. pos = buf;
  269. end = buf + sizeof(buf);
  270. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  271. pos += 4;
  272. pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
  273. sta->counters[counter]);
  274. ctrl_send(wt, sock, buf, pos - buf);
  275. }
  276. static void ctrl_get_bss_counter(struct wlantest *wt, int sock, u8 *cmd,
  277. size_t clen)
  278. {
  279. u8 *addr;
  280. size_t addr_len;
  281. struct wlantest_bss *bss;
  282. u32 counter;
  283. u8 buf[4 + 12], *end, *pos;
  284. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &addr_len);
  285. if (addr == NULL || addr_len != ETH_ALEN) {
  286. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  287. return;
  288. }
  289. bss = bss_get(wt, addr);
  290. if (bss == NULL) {
  291. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  292. return;
  293. }
  294. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSS_COUNTER, &addr_len);
  295. if (addr == NULL || addr_len != 4) {
  296. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  297. return;
  298. }
  299. counter = WPA_GET_BE32(addr);
  300. if (counter >= NUM_WLANTEST_BSS_COUNTER) {
  301. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  302. return;
  303. }
  304. pos = buf;
  305. end = buf + sizeof(buf);
  306. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  307. pos += 4;
  308. pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
  309. bss->counters[counter]);
  310. ctrl_send(wt, sock, buf, pos - buf);
  311. }
  312. static void build_mgmt_hdr(struct ieee80211_mgmt *mgmt,
  313. struct wlantest_bss *bss, struct wlantest_sta *sta,
  314. int sender_ap, int stype)
  315. {
  316. os_memset(mgmt, 0, 24);
  317. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
  318. if (sender_ap) {
  319. if (sta)
  320. os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
  321. else
  322. os_memset(mgmt->da, 0xff, ETH_ALEN);
  323. os_memcpy(mgmt->sa, bss->bssid, ETH_ALEN);
  324. } else {
  325. os_memcpy(mgmt->da, bss->bssid, ETH_ALEN);
  326. os_memcpy(mgmt->sa, sta->addr, ETH_ALEN);
  327. }
  328. os_memcpy(mgmt->bssid, bss->bssid, ETH_ALEN);
  329. }
  330. static int ctrl_inject_auth(struct wlantest *wt, struct wlantest_bss *bss,
  331. struct wlantest_sta *sta, int sender_ap,
  332. enum wlantest_inject_protection prot)
  333. {
  334. struct ieee80211_mgmt mgmt;
  335. if (prot != WLANTEST_INJECT_NORMAL &&
  336. prot != WLANTEST_INJECT_UNPROTECTED)
  337. return -1; /* Authentication frame is never protected */
  338. if (sta == NULL)
  339. return -1; /* No broadcast Authentication frames */
  340. if (sender_ap)
  341. wpa_printf(MSG_INFO, "INJECT: Auth " MACSTR " -> " MACSTR,
  342. MAC2STR(bss->bssid), MAC2STR(sta->addr));
  343. else
  344. wpa_printf(MSG_INFO, "INJECT: Auth " MACSTR " -> " MACSTR,
  345. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  346. build_mgmt_hdr(&mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_AUTH);
  347. mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
  348. mgmt.u.auth.auth_transaction = host_to_le16(1);
  349. mgmt.u.auth.status_code = host_to_le16(WLAN_STATUS_SUCCESS);
  350. return wlantest_inject(wt, bss, sta, (u8 *) &mgmt, 24 + 6,
  351. WLANTEST_INJECT_UNPROTECTED);
  352. }
  353. static int ctrl_inject_assocreq(struct wlantest *wt, struct wlantest_bss *bss,
  354. struct wlantest_sta *sta, int sender_ap,
  355. enum wlantest_inject_protection prot)
  356. {
  357. u8 *buf;
  358. struct ieee80211_mgmt *mgmt;
  359. int ret;
  360. if (prot != WLANTEST_INJECT_NORMAL &&
  361. prot != WLANTEST_INJECT_UNPROTECTED)
  362. return -1; /* Association Request frame is never protected */
  363. if (sta == NULL)
  364. return -1; /* No broadcast Association Request frames */
  365. if (sender_ap)
  366. return -1; /* No Association Request frame sent by AP */
  367. if (sta->assocreq_ies == NULL) {
  368. wpa_printf(MSG_INFO, "INJECT: No previous (Re)Association "
  369. "Request available for " MACSTR,
  370. MAC2STR(sta->addr));
  371. return -1;
  372. }
  373. wpa_printf(MSG_INFO, "INJECT: AssocReq " MACSTR " -> " MACSTR,
  374. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  375. buf = os_malloc(sizeof(*mgmt) + sta->assocreq_ies_len);
  376. if (buf == NULL)
  377. return -1;
  378. mgmt = (struct ieee80211_mgmt *) buf;
  379. build_mgmt_hdr(mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_ASSOC_REQ);
  380. mgmt->u.assoc_req.capab_info = host_to_le16(sta->assocreq_capab_info);
  381. mgmt->u.assoc_req.listen_interval =
  382. host_to_le16(sta->assocreq_listen_int);
  383. os_memcpy(mgmt->u.assoc_req.variable, sta->assocreq_ies,
  384. sta->assocreq_ies_len);
  385. ret = wlantest_inject(wt, bss, sta, buf,
  386. 24 + 4 + sta->assocreq_ies_len,
  387. WLANTEST_INJECT_UNPROTECTED);
  388. os_free(buf);
  389. return ret;
  390. }
  391. static int ctrl_inject_reassocreq(struct wlantest *wt,
  392. struct wlantest_bss *bss,
  393. struct wlantest_sta *sta, int sender_ap,
  394. enum wlantest_inject_protection prot)
  395. {
  396. u8 *buf;
  397. struct ieee80211_mgmt *mgmt;
  398. int ret;
  399. if (prot != WLANTEST_INJECT_NORMAL &&
  400. prot != WLANTEST_INJECT_UNPROTECTED)
  401. return -1; /* Reassociation Request frame is never protected */
  402. if (sta == NULL)
  403. return -1; /* No broadcast Reassociation Request frames */
  404. if (sender_ap)
  405. return -1; /* No Reassociation Request frame sent by AP */
  406. if (sta->assocreq_ies == NULL) {
  407. wpa_printf(MSG_INFO, "INJECT: No previous (Re)Association "
  408. "Request available for " MACSTR,
  409. MAC2STR(sta->addr));
  410. return -1;
  411. }
  412. wpa_printf(MSG_INFO, "INJECT: ReassocReq " MACSTR " -> " MACSTR,
  413. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  414. buf = os_malloc(sizeof(*mgmt) + sta->assocreq_ies_len);
  415. if (buf == NULL)
  416. return -1;
  417. mgmt = (struct ieee80211_mgmt *) buf;
  418. build_mgmt_hdr(mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_REASSOC_REQ);
  419. mgmt->u.reassoc_req.capab_info =
  420. host_to_le16(sta->assocreq_capab_info);
  421. mgmt->u.reassoc_req.listen_interval =
  422. host_to_le16(sta->assocreq_listen_int);
  423. os_memcpy(mgmt->u.reassoc_req.current_ap, bss->bssid, ETH_ALEN);
  424. os_memcpy(mgmt->u.reassoc_req.variable, sta->assocreq_ies,
  425. sta->assocreq_ies_len);
  426. ret = wlantest_inject(wt, bss, sta, buf,
  427. 24 + 10 + sta->assocreq_ies_len,
  428. WLANTEST_INJECT_UNPROTECTED);
  429. os_free(buf);
  430. return ret;
  431. }
  432. static int ctrl_inject_deauth(struct wlantest *wt, struct wlantest_bss *bss,
  433. struct wlantest_sta *sta, int sender_ap,
  434. enum wlantest_inject_protection prot)
  435. {
  436. struct ieee80211_mgmt mgmt;
  437. if (sender_ap) {
  438. if (sta)
  439. wpa_printf(MSG_INFO, "INJECT: Deauth " MACSTR " -> "
  440. MACSTR,
  441. MAC2STR(bss->bssid), MAC2STR(sta->addr));
  442. else
  443. wpa_printf(MSG_INFO, "INJECT: Deauth " MACSTR
  444. " -> broadcast", MAC2STR(bss->bssid));
  445. } else
  446. wpa_printf(MSG_INFO, "INJECT: Deauth " MACSTR " -> " MACSTR,
  447. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  448. build_mgmt_hdr(&mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_DEAUTH);
  449. mgmt.u.deauth.reason_code = host_to_le16(WLAN_REASON_UNSPECIFIED);
  450. return wlantest_inject(wt, bss, sta, (u8 *) &mgmt, 24 + 2, prot);
  451. }
  452. static int ctrl_inject_disassoc(struct wlantest *wt, struct wlantest_bss *bss,
  453. struct wlantest_sta *sta, int sender_ap,
  454. enum wlantest_inject_protection prot)
  455. {
  456. struct ieee80211_mgmt mgmt;
  457. if (sender_ap) {
  458. if (sta)
  459. wpa_printf(MSG_INFO, "INJECT: Disassoc " MACSTR " -> "
  460. MACSTR,
  461. MAC2STR(bss->bssid), MAC2STR(sta->addr));
  462. else
  463. wpa_printf(MSG_INFO, "INJECT: Disassoc " MACSTR
  464. " -> broadcast", MAC2STR(bss->bssid));
  465. } else
  466. wpa_printf(MSG_INFO, "INJECT: Disassoc " MACSTR " -> " MACSTR,
  467. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  468. build_mgmt_hdr(&mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_DISASSOC);
  469. mgmt.u.disassoc.reason_code = host_to_le16(WLAN_REASON_UNSPECIFIED);
  470. return wlantest_inject(wt, bss, sta, (u8 *) &mgmt, 24 + 2, prot);
  471. }
  472. static int ctrl_inject_saqueryreq(struct wlantest *wt,
  473. struct wlantest_bss *bss,
  474. struct wlantest_sta *sta, int sender_ap,
  475. enum wlantest_inject_protection prot)
  476. {
  477. struct ieee80211_mgmt mgmt;
  478. if (sta == NULL)
  479. return -1; /* No broadcast SA Query frames */
  480. if (sender_ap)
  481. wpa_printf(MSG_INFO, "INJECT: SA Query Request " MACSTR " -> "
  482. MACSTR, MAC2STR(bss->bssid), MAC2STR(sta->addr));
  483. else
  484. wpa_printf(MSG_INFO, "INJECT: SA Query Request " MACSTR " -> "
  485. MACSTR, MAC2STR(sta->addr), MAC2STR(bss->bssid));
  486. build_mgmt_hdr(&mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_ACTION);
  487. mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
  488. mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
  489. mgmt.u.action.u.sa_query_req.trans_id[0] = 0x12;
  490. mgmt.u.action.u.sa_query_req.trans_id[1] = 0x34;
  491. os_memcpy(sender_ap ? sta->ap_sa_query_tr : sta->sta_sa_query_tr,
  492. mgmt.u.action.u.sa_query_req.trans_id,
  493. WLAN_SA_QUERY_TR_ID_LEN);
  494. return wlantest_inject(wt, bss, sta, (u8 *) &mgmt, 24 + 4, prot);
  495. }
  496. static void ctrl_inject(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
  497. {
  498. u8 *bssid, *sta_addr;
  499. struct wlantest_bss *bss;
  500. struct wlantest_sta *sta;
  501. int frame, sender_ap, prot;
  502. int ret = 0;
  503. bssid = attr_get_macaddr(cmd, clen, WLANTEST_ATTR_BSSID);
  504. sta_addr = attr_get_macaddr(cmd, clen, WLANTEST_ATTR_STA_ADDR);
  505. frame = attr_get_int(cmd, clen, WLANTEST_ATTR_INJECT_FRAME);
  506. sender_ap = attr_get_int(cmd, clen, WLANTEST_ATTR_INJECT_SENDER_AP);
  507. if (sender_ap < 0)
  508. sender_ap = 0;
  509. prot = attr_get_int(cmd, clen, WLANTEST_ATTR_INJECT_PROTECTION);
  510. if (bssid == NULL || sta_addr == NULL || frame < 0 || prot < 0) {
  511. wpa_printf(MSG_INFO, "Invalid inject command parameters");
  512. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  513. return;
  514. }
  515. bss = bss_get(wt, bssid);
  516. if (bss == NULL) {
  517. wpa_printf(MSG_INFO, "BSS not found for inject command");
  518. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  519. return;
  520. }
  521. if (is_broadcast_ether_addr(sta_addr)) {
  522. if (!sender_ap) {
  523. wpa_printf(MSG_INFO, "Invalid broadcast inject "
  524. "command without sender_ap set");
  525. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  526. return;
  527. } sta = NULL;
  528. } else {
  529. sta = sta_get(bss, sta_addr);
  530. if (sta == NULL) {
  531. wpa_printf(MSG_INFO, "Station not found for inject "
  532. "command");
  533. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  534. return;
  535. }
  536. }
  537. switch (frame) {
  538. case WLANTEST_FRAME_AUTH:
  539. ret = ctrl_inject_auth(wt, bss, sta, sender_ap, prot);
  540. break;
  541. case WLANTEST_FRAME_ASSOCREQ:
  542. ret = ctrl_inject_assocreq(wt, bss, sta, sender_ap, prot);
  543. break;
  544. case WLANTEST_FRAME_REASSOCREQ:
  545. ret = ctrl_inject_reassocreq(wt, bss, sta, sender_ap, prot);
  546. break;
  547. case WLANTEST_FRAME_DEAUTH:
  548. ret = ctrl_inject_deauth(wt, bss, sta, sender_ap, prot);
  549. break;
  550. case WLANTEST_FRAME_DISASSOC:
  551. ret = ctrl_inject_disassoc(wt, bss, sta, sender_ap, prot);
  552. break;
  553. case WLANTEST_FRAME_SAQUERYREQ:
  554. ret = ctrl_inject_saqueryreq(wt, bss, sta, sender_ap, prot);
  555. break;
  556. default:
  557. wpa_printf(MSG_INFO, "Unsupported inject command frame %d",
  558. frame);
  559. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  560. return;
  561. }
  562. if (ret)
  563. wpa_printf(MSG_INFO, "Failed to inject frame");
  564. else
  565. wpa_printf(MSG_INFO, "Frame injected successfully");
  566. ctrl_send_simple(wt, sock, ret == 0 ? WLANTEST_CTRL_SUCCESS :
  567. WLANTEST_CTRL_FAILURE);
  568. }
  569. static void ctrl_version(struct wlantest *wt, int sock)
  570. {
  571. u8 buf[WLANTEST_CTRL_MAX_RESP_LEN], *pos;
  572. pos = buf;
  573. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  574. pos += 4;
  575. pos = attr_add_str(pos, buf + sizeof(buf), WLANTEST_ATTR_VERSION,
  576. VERSION_STR);
  577. ctrl_send(wt, sock, buf, pos - buf);
  578. }
  579. static void ctrl_add_passphrase(struct wlantest *wt, int sock, u8 *cmd,
  580. size_t clen)
  581. {
  582. u8 *passphrase;
  583. size_t len;
  584. struct wlantest_passphrase *p, *pa;
  585. u8 *bssid;
  586. passphrase = attr_get(cmd, clen, WLANTEST_ATTR_PASSPHRASE, &len);
  587. if (passphrase == NULL || len < 8 || len > 63) {
  588. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  589. return;
  590. }
  591. p = os_zalloc(sizeof(*p));
  592. if (p == NULL) {
  593. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  594. return;
  595. }
  596. os_memcpy(p->passphrase, passphrase, len);
  597. wpa_printf(MSG_INFO, "Add passphrase '%s'", p->passphrase);
  598. bssid = attr_get_macaddr(cmd, clen, WLANTEST_ATTR_BSSID);
  599. if (bssid) {
  600. os_memcpy(p->bssid, bssid, ETH_ALEN);
  601. wpa_printf(MSG_INFO, "Limit passphrase for BSSID " MACSTR,
  602. MAC2STR(p->bssid));
  603. }
  604. dl_list_for_each(pa, &wt->passphrase, struct wlantest_passphrase, list)
  605. {
  606. if (os_strcmp(p->passphrase, pa->passphrase) == 0 &&
  607. os_memcmp(p->bssid, pa->bssid, ETH_ALEN) == 0) {
  608. wpa_printf(MSG_INFO, "Passphrase was already known");
  609. os_free(p);
  610. p = NULL;
  611. break;
  612. }
  613. }
  614. if (p)
  615. dl_list_add(&wt->passphrase, &p->list);
  616. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  617. }
  618. static void ctrl_read(int sock, void *eloop_ctx, void *sock_ctx)
  619. {
  620. struct wlantest *wt = eloop_ctx;
  621. u8 buf[WLANTEST_CTRL_MAX_CMD_LEN];
  622. int len;
  623. enum wlantest_ctrl_cmd cmd;
  624. wpa_printf(MSG_EXCESSIVE, "New control interface message from %d",
  625. sock);
  626. len = recv(sock, buf, sizeof(buf), 0);
  627. if (len < 0) {
  628. wpa_printf(MSG_INFO, "recv(ctrl): %s", strerror(errno));
  629. ctrl_disconnect(wt, sock);
  630. return;
  631. }
  632. if (len == 0) {
  633. ctrl_disconnect(wt, sock);
  634. return;
  635. }
  636. if (len < 4) {
  637. wpa_printf(MSG_INFO, "Too short control interface command "
  638. "from %d", sock);
  639. ctrl_disconnect(wt, sock);
  640. return;
  641. }
  642. cmd = WPA_GET_BE32(buf);
  643. wpa_printf(MSG_EXCESSIVE, "Control interface command %d from %d",
  644. cmd, sock);
  645. switch (cmd) {
  646. case WLANTEST_CTRL_PING:
  647. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  648. break;
  649. case WLANTEST_CTRL_TERMINATE:
  650. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  651. eloop_terminate();
  652. break;
  653. case WLANTEST_CTRL_LIST_BSS:
  654. ctrl_list_bss(wt, sock);
  655. break;
  656. case WLANTEST_CTRL_LIST_STA:
  657. ctrl_list_sta(wt, sock, buf + 4, len - 4);
  658. break;
  659. case WLANTEST_CTRL_FLUSH:
  660. ctrl_flush(wt, sock);
  661. break;
  662. case WLANTEST_CTRL_CLEAR_STA_COUNTERS:
  663. ctrl_clear_sta_counters(wt, sock, buf + 4, len - 4);
  664. break;
  665. case WLANTEST_CTRL_CLEAR_BSS_COUNTERS:
  666. ctrl_clear_bss_counters(wt, sock, buf + 4, len - 4);
  667. break;
  668. case WLANTEST_CTRL_GET_STA_COUNTER:
  669. ctrl_get_sta_counter(wt, sock, buf + 4, len - 4);
  670. break;
  671. case WLANTEST_CTRL_GET_BSS_COUNTER:
  672. ctrl_get_bss_counter(wt, sock, buf + 4, len - 4);
  673. break;
  674. case WLANTEST_CTRL_INJECT:
  675. ctrl_inject(wt, sock, buf + 4, len - 4);
  676. break;
  677. case WLANTEST_CTRL_VERSION:
  678. ctrl_version(wt, sock);
  679. break;
  680. case WLANTEST_CTRL_ADD_PASSPHRASE:
  681. ctrl_add_passphrase(wt, sock, buf + 4, len - 4);
  682. break;
  683. default:
  684. ctrl_send_simple(wt, sock, WLANTEST_CTRL_UNKNOWN_CMD);
  685. break;
  686. }
  687. }
  688. static void ctrl_connect(int sock, void *eloop_ctx, void *sock_ctx)
  689. {
  690. struct wlantest *wt = eloop_ctx;
  691. int conn, i;
  692. conn = accept(sock, NULL, NULL);
  693. if (conn < 0) {
  694. wpa_printf(MSG_INFO, "accept(ctrl): %s", strerror(errno));
  695. return;
  696. }
  697. wpa_printf(MSG_MSGDUMP, "New control interface connection %d", conn);
  698. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++) {
  699. if (wt->ctrl_socks[i] < 0)
  700. break;
  701. }
  702. if (i == MAX_CTRL_CONNECTIONS) {
  703. wpa_printf(MSG_INFO, "No room for new control connection");
  704. close(conn);
  705. return;
  706. }
  707. wt->ctrl_socks[i] = conn;
  708. eloop_register_read_sock(conn, ctrl_read, wt, NULL);
  709. }
  710. int ctrl_init(struct wlantest *wt)
  711. {
  712. struct sockaddr_un addr;
  713. wt->ctrl_sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  714. if (wt->ctrl_sock < 0) {
  715. wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
  716. return -1;
  717. }
  718. os_memset(&addr, 0, sizeof(addr));
  719. addr.sun_family = AF_UNIX;
  720. os_strlcpy(addr.sun_path + 1, WLANTEST_SOCK_NAME,
  721. sizeof(addr.sun_path) - 1);
  722. if (bind(wt->ctrl_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  723. wpa_printf(MSG_ERROR, "bind: %s", strerror(errno));
  724. close(wt->ctrl_sock);
  725. wt->ctrl_sock = -1;
  726. return -1;
  727. }
  728. if (listen(wt->ctrl_sock, 5) < 0) {
  729. wpa_printf(MSG_ERROR, "listen: %s", strerror(errno));
  730. close(wt->ctrl_sock);
  731. wt->ctrl_sock = -1;
  732. return -1;
  733. }
  734. if (eloop_register_read_sock(wt->ctrl_sock, ctrl_connect, wt, NULL)) {
  735. close(wt->ctrl_sock);
  736. wt->ctrl_sock = -1;
  737. return -1;
  738. }
  739. return 0;
  740. }
  741. void ctrl_deinit(struct wlantest *wt)
  742. {
  743. int i;
  744. if (wt->ctrl_sock < 0)
  745. return;
  746. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++) {
  747. if (wt->ctrl_socks[i] >= 0) {
  748. close(wt->ctrl_socks[i]);
  749. eloop_unregister_read_sock(wt->ctrl_socks[i]);
  750. wt->ctrl_socks[i] = -1;
  751. }
  752. }
  753. eloop_unregister_read_sock(wt->ctrl_sock);
  754. close(wt->ctrl_sock);
  755. wt->ctrl_sock = -1;
  756. }