ctrl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 "wlantest.h"
  19. #include "wlantest_ctrl.h"
  20. static u8 * attr_get(u8 *buf, size_t buflen, enum wlantest_ctrl_attr attr,
  21. size_t *len)
  22. {
  23. u8 *pos = buf;
  24. while (pos + 8 <= buf + buflen) {
  25. enum wlantest_ctrl_attr a;
  26. size_t alen;
  27. a = WPA_GET_BE32(pos);
  28. pos += 4;
  29. alen = WPA_GET_BE32(pos);
  30. pos += 4;
  31. if (pos + alen > buf + buflen) {
  32. wpa_printf(MSG_DEBUG, "Invalid control message "
  33. "attribute");
  34. return NULL;
  35. }
  36. if (a == attr) {
  37. *len = alen;
  38. return pos;
  39. }
  40. pos += alen;
  41. }
  42. return NULL;
  43. }
  44. static u8 * attr_add_be32(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  45. u32 val)
  46. {
  47. if (pos == NULL || end - pos < 12)
  48. return NULL;
  49. WPA_PUT_BE32(pos, attr);
  50. pos += 4;
  51. WPA_PUT_BE32(pos, 4);
  52. pos += 4;
  53. WPA_PUT_BE32(pos, val);
  54. pos += 4;
  55. return pos;
  56. }
  57. static void ctrl_disconnect(struct wlantest *wt, int sock)
  58. {
  59. int i;
  60. wpa_printf(MSG_DEBUG, "Disconnect control interface connection %d",
  61. sock);
  62. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++) {
  63. if (wt->ctrl_socks[i] == sock) {
  64. close(wt->ctrl_socks[i]);
  65. eloop_unregister_read_sock(wt->ctrl_socks[i]);
  66. wt->ctrl_socks[i] = -1;
  67. break;
  68. }
  69. }
  70. }
  71. static void ctrl_send(struct wlantest *wt, int sock, const u8 *buf,
  72. size_t len)
  73. {
  74. if (send(sock, buf, len, 0) < 0) {
  75. wpa_printf(MSG_INFO, "send(ctrl): %s", strerror(errno));
  76. ctrl_disconnect(wt, sock);
  77. }
  78. }
  79. static void ctrl_send_simple(struct wlantest *wt, int sock,
  80. enum wlantest_ctrl_cmd cmd)
  81. {
  82. u8 buf[4];
  83. WPA_PUT_BE32(buf, cmd);
  84. ctrl_send(wt, sock, buf, sizeof(buf));
  85. }
  86. static void ctrl_list_bss(struct wlantest *wt, int sock)
  87. {
  88. u8 buf[WLANTEST_CTRL_MAX_RESP_LEN], *pos, *len;
  89. struct wlantest_bss *bss;
  90. pos = buf;
  91. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  92. pos += 4;
  93. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  94. pos += 4;
  95. len = pos; /* to be filled */
  96. pos += 4;
  97. dl_list_for_each(bss, &wt->bss, struct wlantest_bss, list) {
  98. if (pos + ETH_ALEN > buf + WLANTEST_CTRL_MAX_RESP_LEN)
  99. break;
  100. os_memcpy(pos, bss->bssid, ETH_ALEN);
  101. pos += ETH_ALEN;
  102. }
  103. WPA_PUT_BE32(len, pos - len - 4);
  104. ctrl_send(wt, sock, buf, pos - buf);
  105. }
  106. static void ctrl_list_sta(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
  107. {
  108. u8 buf[WLANTEST_CTRL_MAX_RESP_LEN], *pos, *len;
  109. u8 *bssid;
  110. size_t bssid_len;
  111. struct wlantest_bss *bss;
  112. struct wlantest_sta *sta;
  113. bssid = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &bssid_len);
  114. if (bssid == NULL || bssid_len != ETH_ALEN) {
  115. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  116. return;
  117. }
  118. bss = bss_get(wt, bssid);
  119. if (bss == NULL) {
  120. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  121. return;
  122. }
  123. pos = buf;
  124. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  125. pos += 4;
  126. WPA_PUT_BE32(pos, WLANTEST_ATTR_STA_ADDR);
  127. pos += 4;
  128. len = pos; /* to be filled */
  129. pos += 4;
  130. dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
  131. if (pos + ETH_ALEN > buf + WLANTEST_CTRL_MAX_RESP_LEN)
  132. break;
  133. os_memcpy(pos, sta->addr, ETH_ALEN);
  134. pos += ETH_ALEN;
  135. }
  136. WPA_PUT_BE32(len, pos - len - 4);
  137. ctrl_send(wt, sock, buf, pos - buf);
  138. }
  139. static void ctrl_flush(struct wlantest *wt, int sock)
  140. {
  141. wpa_printf(MSG_DEBUG, "Drop all collected BSS data");
  142. bss_flush(wt);
  143. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  144. }
  145. static void ctrl_clear_sta_counters(struct wlantest *wt, int sock, u8 *cmd,
  146. size_t clen)
  147. {
  148. u8 *addr;
  149. size_t addr_len;
  150. struct wlantest_bss *bss;
  151. struct wlantest_sta *sta;
  152. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &addr_len);
  153. if (addr == NULL || addr_len != ETH_ALEN) {
  154. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  155. return;
  156. }
  157. bss = bss_get(wt, addr);
  158. if (bss == NULL) {
  159. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  160. return;
  161. }
  162. addr = attr_get(cmd, clen, WLANTEST_ATTR_STA_ADDR, &addr_len);
  163. if (addr == NULL || addr_len != ETH_ALEN) {
  164. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  165. return;
  166. }
  167. sta = sta_get(bss, addr);
  168. if (sta == NULL) {
  169. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  170. return;
  171. }
  172. os_memset(sta->counters, 0, sizeof(sta->counters));
  173. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  174. }
  175. static void ctrl_clear_bss_counters(struct wlantest *wt, int sock, u8 *cmd,
  176. size_t clen)
  177. {
  178. u8 *addr;
  179. size_t addr_len;
  180. struct wlantest_bss *bss;
  181. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &addr_len);
  182. if (addr == NULL || addr_len != ETH_ALEN) {
  183. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  184. return;
  185. }
  186. bss = bss_get(wt, addr);
  187. if (bss == NULL) {
  188. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  189. return;
  190. }
  191. os_memset(bss->counters, 0, sizeof(bss->counters));
  192. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  193. }
  194. static void ctrl_get_sta_counter(struct wlantest *wt, int sock, u8 *cmd,
  195. size_t clen)
  196. {
  197. u8 *addr;
  198. size_t addr_len;
  199. struct wlantest_bss *bss;
  200. struct wlantest_sta *sta;
  201. u32 counter;
  202. u8 buf[4 + 12], *end, *pos;
  203. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &addr_len);
  204. if (addr == NULL || addr_len != ETH_ALEN) {
  205. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  206. return;
  207. }
  208. bss = bss_get(wt, addr);
  209. if (bss == NULL) {
  210. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  211. return;
  212. }
  213. addr = attr_get(cmd, clen, WLANTEST_ATTR_STA_ADDR, &addr_len);
  214. if (addr == NULL || addr_len != ETH_ALEN) {
  215. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  216. return;
  217. }
  218. sta = sta_get(bss, addr);
  219. if (sta == NULL) {
  220. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  221. return;
  222. }
  223. addr = attr_get(cmd, clen, WLANTEST_ATTR_STA_COUNTER, &addr_len);
  224. if (addr == NULL || addr_len != 4) {
  225. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  226. return;
  227. }
  228. counter = WPA_GET_BE32(addr);
  229. if (counter >= NUM_WLANTEST_STA_COUNTER) {
  230. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  231. return;
  232. }
  233. pos = buf;
  234. end = buf + sizeof(buf);
  235. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  236. pos += 4;
  237. pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
  238. sta->counters[counter]);
  239. ctrl_send(wt, sock, buf, pos - buf);
  240. }
  241. static void ctrl_get_bss_counter(struct wlantest *wt, int sock, u8 *cmd,
  242. size_t clen)
  243. {
  244. u8 *addr;
  245. size_t addr_len;
  246. struct wlantest_bss *bss;
  247. u32 counter;
  248. u8 buf[4 + 12], *end, *pos;
  249. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &addr_len);
  250. if (addr == NULL || addr_len != ETH_ALEN) {
  251. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  252. return;
  253. }
  254. bss = bss_get(wt, addr);
  255. if (bss == NULL) {
  256. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  257. return;
  258. }
  259. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSS_COUNTER, &addr_len);
  260. if (addr == NULL || addr_len != 4) {
  261. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  262. return;
  263. }
  264. counter = WPA_GET_BE32(addr);
  265. if (counter >= NUM_WLANTEST_BSS_COUNTER) {
  266. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  267. return;
  268. }
  269. pos = buf;
  270. end = buf + sizeof(buf);
  271. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  272. pos += 4;
  273. pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
  274. bss->counters[counter]);
  275. ctrl_send(wt, sock, buf, pos - buf);
  276. }
  277. static void ctrl_read(int sock, void *eloop_ctx, void *sock_ctx)
  278. {
  279. struct wlantest *wt = eloop_ctx;
  280. u8 buf[WLANTEST_CTRL_MAX_CMD_LEN];
  281. int len;
  282. enum wlantest_ctrl_cmd cmd;
  283. wpa_printf(MSG_EXCESSIVE, "New control interface message from %d",
  284. sock);
  285. len = recv(sock, buf, sizeof(buf), 0);
  286. if (len < 0) {
  287. wpa_printf(MSG_INFO, "recv(ctrl): %s", strerror(errno));
  288. ctrl_disconnect(wt, sock);
  289. return;
  290. }
  291. if (len == 0) {
  292. ctrl_disconnect(wt, sock);
  293. return;
  294. }
  295. if (len < 4) {
  296. wpa_printf(MSG_INFO, "Too short control interface command "
  297. "from %d", sock);
  298. ctrl_disconnect(wt, sock);
  299. return;
  300. }
  301. cmd = WPA_GET_BE32(buf);
  302. wpa_printf(MSG_EXCESSIVE, "Control interface command %d from %d",
  303. cmd, sock);
  304. switch (cmd) {
  305. case WLANTEST_CTRL_PING:
  306. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  307. break;
  308. case WLANTEST_CTRL_TERMINATE:
  309. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  310. eloop_terminate();
  311. break;
  312. case WLANTEST_CTRL_LIST_BSS:
  313. ctrl_list_bss(wt, sock);
  314. break;
  315. case WLANTEST_CTRL_LIST_STA:
  316. ctrl_list_sta(wt, sock, buf + 4, len - 4);
  317. break;
  318. case WLANTEST_CTRL_FLUSH:
  319. ctrl_flush(wt, sock);
  320. break;
  321. case WLANTEST_CTRL_CLEAR_STA_COUNTERS:
  322. ctrl_clear_sta_counters(wt, sock, buf + 4, len - 4);
  323. break;
  324. case WLANTEST_CTRL_CLEAR_BSS_COUNTERS:
  325. ctrl_clear_bss_counters(wt, sock, buf + 4, len - 4);
  326. break;
  327. case WLANTEST_CTRL_GET_STA_COUNTER:
  328. ctrl_get_sta_counter(wt, sock, buf + 4, len - 4);
  329. break;
  330. case WLANTEST_CTRL_GET_BSS_COUNTER:
  331. ctrl_get_bss_counter(wt, sock, buf + 4, len - 4);
  332. break;
  333. default:
  334. ctrl_send_simple(wt, sock, WLANTEST_CTRL_UNKNOWN_CMD);
  335. break;
  336. }
  337. }
  338. static void ctrl_connect(int sock, void *eloop_ctx, void *sock_ctx)
  339. {
  340. struct wlantest *wt = eloop_ctx;
  341. int conn, i;
  342. conn = accept(sock, NULL, NULL);
  343. if (conn < 0) {
  344. wpa_printf(MSG_INFO, "accept(ctrl): %s", strerror(errno));
  345. return;
  346. }
  347. wpa_printf(MSG_MSGDUMP, "New control interface connection %d", conn);
  348. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++) {
  349. if (wt->ctrl_socks[i] < 0)
  350. break;
  351. }
  352. if (i == MAX_CTRL_CONNECTIONS) {
  353. wpa_printf(MSG_INFO, "No room for new control connection");
  354. close(conn);
  355. return;
  356. }
  357. wt->ctrl_socks[i] = conn;
  358. eloop_register_read_sock(conn, ctrl_read, wt, NULL);
  359. }
  360. int ctrl_init(struct wlantest *wt)
  361. {
  362. struct sockaddr_un addr;
  363. wt->ctrl_sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  364. if (wt->ctrl_sock < 0) {
  365. wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
  366. return -1;
  367. }
  368. os_memset(&addr, 0, sizeof(addr));
  369. addr.sun_family = AF_UNIX;
  370. os_strlcpy(addr.sun_path + 1, WLANTEST_SOCK_NAME,
  371. sizeof(addr.sun_path) - 1);
  372. if (bind(wt->ctrl_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  373. wpa_printf(MSG_ERROR, "bind: %s", strerror(errno));
  374. close(wt->ctrl_sock);
  375. wt->ctrl_sock = -1;
  376. return -1;
  377. }
  378. if (listen(wt->ctrl_sock, 5) < 0) {
  379. wpa_printf(MSG_ERROR, "listen: %s", strerror(errno));
  380. close(wt->ctrl_sock);
  381. wt->ctrl_sock = -1;
  382. return -1;
  383. }
  384. if (eloop_register_read_sock(wt->ctrl_sock, ctrl_connect, wt, NULL)) {
  385. close(wt->ctrl_sock);
  386. wt->ctrl_sock = -1;
  387. return -1;
  388. }
  389. return 0;
  390. }
  391. void ctrl_deinit(struct wlantest *wt)
  392. {
  393. int i;
  394. if (wt->ctrl_sock < 0)
  395. return;
  396. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++) {
  397. if (wt->ctrl_socks[i] >= 0) {
  398. close(wt->ctrl_socks[i]);
  399. eloop_unregister_read_sock(wt->ctrl_socks[i]);
  400. wt->ctrl_socks[i] = -1;
  401. }
  402. }
  403. eloop_unregister_read_sock(wt->ctrl_sock);
  404. close(wt->ctrl_sock);
  405. wt->ctrl_sock = -1;
  406. }