ctrl.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. /*
  2. * wlantest control interface
  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 <sys/un.h>
  10. #include "utils/common.h"
  11. #include "utils/eloop.h"
  12. #include "common/defs.h"
  13. #include "common/version.h"
  14. #include "common/ieee802_11_defs.h"
  15. #include "wlantest.h"
  16. #include "wlantest_ctrl.h"
  17. static u8 * attr_get(u8 *buf, size_t buflen, enum wlantest_ctrl_attr attr,
  18. size_t *len)
  19. {
  20. u8 *pos = buf;
  21. while (pos + 8 <= buf + buflen) {
  22. enum wlantest_ctrl_attr a;
  23. size_t alen;
  24. a = WPA_GET_BE32(pos);
  25. pos += 4;
  26. alen = WPA_GET_BE32(pos);
  27. pos += 4;
  28. if (pos + alen > buf + buflen) {
  29. wpa_printf(MSG_DEBUG, "Invalid control message "
  30. "attribute");
  31. return NULL;
  32. }
  33. if (a == attr) {
  34. *len = alen;
  35. return pos;
  36. }
  37. pos += alen;
  38. }
  39. return NULL;
  40. }
  41. static u8 * attr_get_macaddr(u8 *buf, size_t buflen,
  42. enum wlantest_ctrl_attr attr)
  43. {
  44. u8 *addr;
  45. size_t addr_len;
  46. addr = attr_get(buf, buflen, attr, &addr_len);
  47. if (addr && addr_len != ETH_ALEN)
  48. addr = NULL;
  49. return addr;
  50. }
  51. static int attr_get_int(u8 *buf, size_t buflen, enum wlantest_ctrl_attr attr)
  52. {
  53. u8 *pos;
  54. size_t len;
  55. pos = attr_get(buf, buflen, attr, &len);
  56. if (pos == NULL || len != 4)
  57. return -1;
  58. return WPA_GET_BE32(pos);
  59. }
  60. static u8 * attr_add_str(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  61. const char *str)
  62. {
  63. size_t len = os_strlen(str);
  64. if (pos == NULL || end - pos < 8 + len)
  65. return NULL;
  66. WPA_PUT_BE32(pos, attr);
  67. pos += 4;
  68. WPA_PUT_BE32(pos, len);
  69. pos += 4;
  70. os_memcpy(pos, str, len);
  71. pos += len;
  72. return pos;
  73. }
  74. static u8 * attr_add_be32(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  75. u32 val)
  76. {
  77. if (pos == NULL || end - pos < 12)
  78. return NULL;
  79. WPA_PUT_BE32(pos, attr);
  80. pos += 4;
  81. WPA_PUT_BE32(pos, 4);
  82. pos += 4;
  83. WPA_PUT_BE32(pos, val);
  84. pos += 4;
  85. return pos;
  86. }
  87. static void ctrl_disconnect(struct wlantest *wt, int sock)
  88. {
  89. int i;
  90. wpa_printf(MSG_DEBUG, "Disconnect control interface connection %d",
  91. sock);
  92. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++) {
  93. if (wt->ctrl_socks[i] == sock) {
  94. close(wt->ctrl_socks[i]);
  95. eloop_unregister_read_sock(wt->ctrl_socks[i]);
  96. wt->ctrl_socks[i] = -1;
  97. break;
  98. }
  99. }
  100. }
  101. static void ctrl_send(struct wlantest *wt, int sock, const u8 *buf,
  102. size_t len)
  103. {
  104. if (send(sock, buf, len, 0) < 0) {
  105. wpa_printf(MSG_INFO, "send(ctrl): %s", strerror(errno));
  106. ctrl_disconnect(wt, sock);
  107. }
  108. }
  109. static void ctrl_send_simple(struct wlantest *wt, int sock,
  110. enum wlantest_ctrl_cmd cmd)
  111. {
  112. u8 buf[4];
  113. WPA_PUT_BE32(buf, cmd);
  114. ctrl_send(wt, sock, buf, sizeof(buf));
  115. }
  116. static struct wlantest_bss * ctrl_get_bss(struct wlantest *wt, int sock,
  117. u8 *cmd, size_t clen)
  118. {
  119. struct wlantest_bss *bss;
  120. u8 *pos;
  121. size_t len;
  122. pos = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &len);
  123. if (pos == NULL || len != ETH_ALEN) {
  124. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  125. return NULL;
  126. }
  127. bss = bss_find(wt, pos);
  128. if (bss == NULL) {
  129. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  130. return NULL;
  131. }
  132. return bss;
  133. }
  134. static struct wlantest_sta * ctrl_get_sta(struct wlantest *wt, int sock,
  135. u8 *cmd, size_t clen,
  136. struct wlantest_bss *bss)
  137. {
  138. struct wlantest_sta *sta;
  139. u8 *pos;
  140. size_t len;
  141. if (bss == NULL)
  142. return NULL;
  143. pos = attr_get(cmd, clen, WLANTEST_ATTR_STA_ADDR, &len);
  144. if (pos == NULL || len != ETH_ALEN) {
  145. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  146. return NULL;
  147. }
  148. sta = sta_find(bss, pos);
  149. if (sta == NULL) {
  150. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  151. return NULL;
  152. }
  153. return sta;
  154. }
  155. static struct wlantest_sta * ctrl_get_sta2(struct wlantest *wt, int sock,
  156. u8 *cmd, size_t clen,
  157. struct wlantest_bss *bss)
  158. {
  159. struct wlantest_sta *sta;
  160. u8 *pos;
  161. size_t len;
  162. if (bss == NULL)
  163. return NULL;
  164. pos = attr_get(cmd, clen, WLANTEST_ATTR_STA2_ADDR, &len);
  165. if (pos == NULL || len != ETH_ALEN) {
  166. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  167. return NULL;
  168. }
  169. sta = sta_find(bss, pos);
  170. if (sta == NULL) {
  171. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  172. return NULL;
  173. }
  174. return sta;
  175. }
  176. static void ctrl_list_bss(struct wlantest *wt, int sock)
  177. {
  178. u8 buf[WLANTEST_CTRL_MAX_RESP_LEN], *pos, *len;
  179. struct wlantest_bss *bss;
  180. pos = buf;
  181. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  182. pos += 4;
  183. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  184. pos += 4;
  185. len = pos; /* to be filled */
  186. pos += 4;
  187. dl_list_for_each(bss, &wt->bss, struct wlantest_bss, list) {
  188. if (pos + ETH_ALEN > buf + WLANTEST_CTRL_MAX_RESP_LEN)
  189. break;
  190. os_memcpy(pos, bss->bssid, ETH_ALEN);
  191. pos += ETH_ALEN;
  192. }
  193. WPA_PUT_BE32(len, pos - len - 4);
  194. ctrl_send(wt, sock, buf, pos - buf);
  195. }
  196. static void ctrl_list_sta(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
  197. {
  198. u8 buf[WLANTEST_CTRL_MAX_RESP_LEN], *pos, *len;
  199. struct wlantest_bss *bss;
  200. struct wlantest_sta *sta;
  201. bss = ctrl_get_bss(wt, sock, cmd, clen);
  202. if (bss == NULL)
  203. return;
  204. pos = buf;
  205. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  206. pos += 4;
  207. WPA_PUT_BE32(pos, WLANTEST_ATTR_STA_ADDR);
  208. pos += 4;
  209. len = pos; /* to be filled */
  210. pos += 4;
  211. dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
  212. if (pos + ETH_ALEN > buf + WLANTEST_CTRL_MAX_RESP_LEN)
  213. break;
  214. os_memcpy(pos, sta->addr, ETH_ALEN);
  215. pos += ETH_ALEN;
  216. }
  217. WPA_PUT_BE32(len, pos - len - 4);
  218. ctrl_send(wt, sock, buf, pos - buf);
  219. }
  220. static void ctrl_flush(struct wlantest *wt, int sock)
  221. {
  222. wpa_printf(MSG_DEBUG, "Drop all collected BSS data");
  223. bss_flush(wt);
  224. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  225. }
  226. static void ctrl_clear_sta_counters(struct wlantest *wt, int sock, u8 *cmd,
  227. size_t clen)
  228. {
  229. struct wlantest_bss *bss;
  230. struct wlantest_sta *sta;
  231. bss = ctrl_get_bss(wt, sock, cmd, clen);
  232. sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
  233. if (sta == NULL) {
  234. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  235. return;
  236. }
  237. os_memset(sta->counters, 0, sizeof(sta->counters));
  238. os_memset(sta->tx_tid, 0, sizeof(sta->tx_tid));
  239. os_memset(sta->rx_tid, 0, sizeof(sta->rx_tid));
  240. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  241. }
  242. static void ctrl_clear_bss_counters(struct wlantest *wt, int sock, u8 *cmd,
  243. size_t clen)
  244. {
  245. struct wlantest_bss *bss;
  246. bss = ctrl_get_bss(wt, sock, cmd, clen);
  247. if (bss == NULL) {
  248. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  249. return;
  250. }
  251. os_memset(bss->counters, 0, sizeof(bss->counters));
  252. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  253. }
  254. static void ctrl_clear_tdls_counters(struct wlantest *wt, int sock, u8 *cmd,
  255. size_t clen)
  256. {
  257. struct wlantest_bss *bss;
  258. struct wlantest_sta *sta;
  259. struct wlantest_sta *sta2;
  260. struct wlantest_tdls *tdls;
  261. bss = ctrl_get_bss(wt, sock, cmd, clen);
  262. sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
  263. sta2 = ctrl_get_sta2(wt, sock, cmd, clen, bss);
  264. if (sta == NULL || sta2 == NULL) {
  265. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  266. return;
  267. }
  268. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
  269. if ((tdls->init == sta && tdls->resp == sta2) ||
  270. (tdls->init == sta2 && tdls->resp == sta))
  271. os_memset(tdls->counters, 0, sizeof(tdls->counters));
  272. }
  273. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  274. }
  275. static void ctrl_get_sta_counter(struct wlantest *wt, int sock, u8 *cmd,
  276. size_t clen)
  277. {
  278. u8 *addr;
  279. size_t addr_len;
  280. struct wlantest_bss *bss;
  281. struct wlantest_sta *sta;
  282. u32 counter;
  283. u8 buf[4 + 12], *end, *pos;
  284. bss = ctrl_get_bss(wt, sock, cmd, clen);
  285. sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
  286. if (sta == NULL)
  287. return;
  288. addr = attr_get(cmd, clen, WLANTEST_ATTR_STA_COUNTER, &addr_len);
  289. if (addr == NULL || addr_len != 4) {
  290. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  291. return;
  292. }
  293. counter = WPA_GET_BE32(addr);
  294. if (counter >= NUM_WLANTEST_STA_COUNTER) {
  295. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  296. return;
  297. }
  298. pos = buf;
  299. end = buf + sizeof(buf);
  300. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  301. pos += 4;
  302. pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
  303. sta->counters[counter]);
  304. ctrl_send(wt, sock, buf, pos - buf);
  305. }
  306. static void ctrl_get_bss_counter(struct wlantest *wt, int sock, u8 *cmd,
  307. size_t clen)
  308. {
  309. u8 *addr;
  310. size_t addr_len;
  311. struct wlantest_bss *bss;
  312. u32 counter;
  313. u8 buf[4 + 12], *end, *pos;
  314. bss = ctrl_get_bss(wt, sock, cmd, clen);
  315. if (bss == NULL)
  316. return;
  317. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSS_COUNTER, &addr_len);
  318. if (addr == NULL || addr_len != 4) {
  319. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  320. return;
  321. }
  322. counter = WPA_GET_BE32(addr);
  323. if (counter >= NUM_WLANTEST_BSS_COUNTER) {
  324. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  325. return;
  326. }
  327. pos = buf;
  328. end = buf + sizeof(buf);
  329. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  330. pos += 4;
  331. pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
  332. bss->counters[counter]);
  333. ctrl_send(wt, sock, buf, pos - buf);
  334. }
  335. static void ctrl_get_tdls_counter(struct wlantest *wt, int sock, u8 *cmd,
  336. size_t clen)
  337. {
  338. u8 *addr;
  339. size_t addr_len;
  340. struct wlantest_bss *bss;
  341. struct wlantest_sta *sta;
  342. struct wlantest_sta *sta2;
  343. struct wlantest_tdls *tdls;
  344. u32 counter;
  345. u8 buf[4 + 12], *end, *pos;
  346. int found = 0;
  347. bss = ctrl_get_bss(wt, sock, cmd, clen);
  348. sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
  349. sta2 = ctrl_get_sta2(wt, sock, cmd, clen, bss);
  350. if (sta == NULL || sta2 == NULL) {
  351. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  352. return;
  353. }
  354. addr = attr_get(cmd, clen, WLANTEST_ATTR_TDLS_COUNTER, &addr_len);
  355. if (addr == NULL || addr_len != 4) {
  356. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  357. return;
  358. }
  359. counter = WPA_GET_BE32(addr);
  360. if (counter >= NUM_WLANTEST_TDLS_COUNTER) {
  361. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  362. return;
  363. }
  364. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
  365. if (tdls->init == sta && tdls->resp == sta2) {
  366. found = 1;
  367. break;
  368. }
  369. }
  370. if (!found) {
  371. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  372. return;
  373. }
  374. pos = buf;
  375. end = buf + sizeof(buf);
  376. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  377. pos += 4;
  378. pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
  379. tdls->counters[counter]);
  380. ctrl_send(wt, sock, buf, pos - buf);
  381. }
  382. static void build_mgmt_hdr(struct ieee80211_mgmt *mgmt,
  383. struct wlantest_bss *bss, struct wlantest_sta *sta,
  384. int sender_ap, int stype)
  385. {
  386. os_memset(mgmt, 0, 24);
  387. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
  388. if (sender_ap) {
  389. if (sta)
  390. os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
  391. else
  392. os_memset(mgmt->da, 0xff, ETH_ALEN);
  393. os_memcpy(mgmt->sa, bss->bssid, ETH_ALEN);
  394. } else {
  395. os_memcpy(mgmt->da, bss->bssid, ETH_ALEN);
  396. os_memcpy(mgmt->sa, sta->addr, ETH_ALEN);
  397. }
  398. os_memcpy(mgmt->bssid, bss->bssid, ETH_ALEN);
  399. }
  400. static int ctrl_inject_auth(struct wlantest *wt, struct wlantest_bss *bss,
  401. struct wlantest_sta *sta, int sender_ap,
  402. enum wlantest_inject_protection prot)
  403. {
  404. struct ieee80211_mgmt mgmt;
  405. if (prot != WLANTEST_INJECT_NORMAL &&
  406. prot != WLANTEST_INJECT_UNPROTECTED)
  407. return -1; /* Authentication frame is never protected */
  408. if (sta == NULL)
  409. return -1; /* No broadcast Authentication frames */
  410. if (sender_ap)
  411. wpa_printf(MSG_INFO, "INJECT: Auth " MACSTR " -> " MACSTR,
  412. MAC2STR(bss->bssid), MAC2STR(sta->addr));
  413. else
  414. wpa_printf(MSG_INFO, "INJECT: Auth " MACSTR " -> " MACSTR,
  415. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  416. build_mgmt_hdr(&mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_AUTH);
  417. mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
  418. mgmt.u.auth.auth_transaction = host_to_le16(1);
  419. mgmt.u.auth.status_code = host_to_le16(WLAN_STATUS_SUCCESS);
  420. return wlantest_inject(wt, bss, sta, (u8 *) &mgmt, 24 + 6,
  421. WLANTEST_INJECT_UNPROTECTED);
  422. }
  423. static int ctrl_inject_assocreq(struct wlantest *wt, struct wlantest_bss *bss,
  424. struct wlantest_sta *sta, int sender_ap,
  425. enum wlantest_inject_protection prot)
  426. {
  427. u8 *buf;
  428. struct ieee80211_mgmt *mgmt;
  429. int ret;
  430. if (prot != WLANTEST_INJECT_NORMAL &&
  431. prot != WLANTEST_INJECT_UNPROTECTED)
  432. return -1; /* Association Request frame is never protected */
  433. if (sta == NULL)
  434. return -1; /* No broadcast Association Request frames */
  435. if (sender_ap)
  436. return -1; /* No Association Request frame sent by AP */
  437. if (sta->assocreq_ies == NULL) {
  438. wpa_printf(MSG_INFO, "INJECT: No previous (Re)Association "
  439. "Request available for " MACSTR,
  440. MAC2STR(sta->addr));
  441. return -1;
  442. }
  443. wpa_printf(MSG_INFO, "INJECT: AssocReq " MACSTR " -> " MACSTR,
  444. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  445. buf = os_malloc(sizeof(*mgmt) + sta->assocreq_ies_len);
  446. if (buf == NULL)
  447. return -1;
  448. mgmt = (struct ieee80211_mgmt *) buf;
  449. build_mgmt_hdr(mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_ASSOC_REQ);
  450. mgmt->u.assoc_req.capab_info = host_to_le16(sta->assocreq_capab_info);
  451. mgmt->u.assoc_req.listen_interval =
  452. host_to_le16(sta->assocreq_listen_int);
  453. os_memcpy(mgmt->u.assoc_req.variable, sta->assocreq_ies,
  454. sta->assocreq_ies_len);
  455. ret = wlantest_inject(wt, bss, sta, buf,
  456. 24 + 4 + sta->assocreq_ies_len,
  457. WLANTEST_INJECT_UNPROTECTED);
  458. os_free(buf);
  459. return ret;
  460. }
  461. static int ctrl_inject_reassocreq(struct wlantest *wt,
  462. struct wlantest_bss *bss,
  463. struct wlantest_sta *sta, int sender_ap,
  464. enum wlantest_inject_protection prot)
  465. {
  466. u8 *buf;
  467. struct ieee80211_mgmt *mgmt;
  468. int ret;
  469. if (prot != WLANTEST_INJECT_NORMAL &&
  470. prot != WLANTEST_INJECT_UNPROTECTED)
  471. return -1; /* Reassociation Request frame is never protected */
  472. if (sta == NULL)
  473. return -1; /* No broadcast Reassociation Request frames */
  474. if (sender_ap)
  475. return -1; /* No Reassociation Request frame sent by AP */
  476. if (sta->assocreq_ies == NULL) {
  477. wpa_printf(MSG_INFO, "INJECT: No previous (Re)Association "
  478. "Request available for " MACSTR,
  479. MAC2STR(sta->addr));
  480. return -1;
  481. }
  482. wpa_printf(MSG_INFO, "INJECT: ReassocReq " MACSTR " -> " MACSTR,
  483. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  484. buf = os_malloc(sizeof(*mgmt) + sta->assocreq_ies_len);
  485. if (buf == NULL)
  486. return -1;
  487. mgmt = (struct ieee80211_mgmt *) buf;
  488. build_mgmt_hdr(mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_REASSOC_REQ);
  489. mgmt->u.reassoc_req.capab_info =
  490. host_to_le16(sta->assocreq_capab_info);
  491. mgmt->u.reassoc_req.listen_interval =
  492. host_to_le16(sta->assocreq_listen_int);
  493. os_memcpy(mgmt->u.reassoc_req.current_ap, bss->bssid, ETH_ALEN);
  494. os_memcpy(mgmt->u.reassoc_req.variable, sta->assocreq_ies,
  495. sta->assocreq_ies_len);
  496. ret = wlantest_inject(wt, bss, sta, buf,
  497. 24 + 10 + sta->assocreq_ies_len,
  498. WLANTEST_INJECT_UNPROTECTED);
  499. os_free(buf);
  500. return ret;
  501. }
  502. static int ctrl_inject_deauth(struct wlantest *wt, struct wlantest_bss *bss,
  503. struct wlantest_sta *sta, int sender_ap,
  504. enum wlantest_inject_protection prot)
  505. {
  506. struct ieee80211_mgmt mgmt;
  507. if (sender_ap) {
  508. if (sta)
  509. wpa_printf(MSG_INFO, "INJECT: Deauth " MACSTR " -> "
  510. MACSTR,
  511. MAC2STR(bss->bssid), MAC2STR(sta->addr));
  512. else
  513. wpa_printf(MSG_INFO, "INJECT: Deauth " MACSTR
  514. " -> broadcast", MAC2STR(bss->bssid));
  515. } else
  516. wpa_printf(MSG_INFO, "INJECT: Deauth " MACSTR " -> " MACSTR,
  517. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  518. build_mgmt_hdr(&mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_DEAUTH);
  519. mgmt.u.deauth.reason_code = host_to_le16(WLAN_REASON_UNSPECIFIED);
  520. return wlantest_inject(wt, bss, sta, (u8 *) &mgmt, 24 + 2, prot);
  521. }
  522. static int ctrl_inject_disassoc(struct wlantest *wt, struct wlantest_bss *bss,
  523. struct wlantest_sta *sta, int sender_ap,
  524. enum wlantest_inject_protection prot)
  525. {
  526. struct ieee80211_mgmt mgmt;
  527. if (sender_ap) {
  528. if (sta)
  529. wpa_printf(MSG_INFO, "INJECT: Disassoc " MACSTR " -> "
  530. MACSTR,
  531. MAC2STR(bss->bssid), MAC2STR(sta->addr));
  532. else
  533. wpa_printf(MSG_INFO, "INJECT: Disassoc " MACSTR
  534. " -> broadcast", MAC2STR(bss->bssid));
  535. } else
  536. wpa_printf(MSG_INFO, "INJECT: Disassoc " MACSTR " -> " MACSTR,
  537. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  538. build_mgmt_hdr(&mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_DISASSOC);
  539. mgmt.u.disassoc.reason_code = host_to_le16(WLAN_REASON_UNSPECIFIED);
  540. return wlantest_inject(wt, bss, sta, (u8 *) &mgmt, 24 + 2, prot);
  541. }
  542. static int ctrl_inject_saqueryreq(struct wlantest *wt,
  543. struct wlantest_bss *bss,
  544. struct wlantest_sta *sta, int sender_ap,
  545. enum wlantest_inject_protection prot)
  546. {
  547. struct ieee80211_mgmt mgmt;
  548. if (sta == NULL)
  549. return -1; /* No broadcast SA Query frames */
  550. if (sender_ap)
  551. wpa_printf(MSG_INFO, "INJECT: SA Query Request " MACSTR " -> "
  552. MACSTR, MAC2STR(bss->bssid), MAC2STR(sta->addr));
  553. else
  554. wpa_printf(MSG_INFO, "INJECT: SA Query Request " MACSTR " -> "
  555. MACSTR, MAC2STR(sta->addr), MAC2STR(bss->bssid));
  556. build_mgmt_hdr(&mgmt, bss, sta, sender_ap, WLAN_FC_STYPE_ACTION);
  557. mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
  558. mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
  559. mgmt.u.action.u.sa_query_req.trans_id[0] = 0x12;
  560. mgmt.u.action.u.sa_query_req.trans_id[1] = 0x34;
  561. os_memcpy(sender_ap ? sta->ap_sa_query_tr : sta->sta_sa_query_tr,
  562. mgmt.u.action.u.sa_query_req.trans_id,
  563. WLAN_SA_QUERY_TR_ID_LEN);
  564. return wlantest_inject(wt, bss, sta, (u8 *) &mgmt, 24 + 4, prot);
  565. }
  566. static void ctrl_inject(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
  567. {
  568. u8 *bssid, *sta_addr;
  569. struct wlantest_bss *bss;
  570. struct wlantest_sta *sta;
  571. int frame, sender_ap, prot;
  572. int ret = 0;
  573. bssid = attr_get_macaddr(cmd, clen, WLANTEST_ATTR_BSSID);
  574. sta_addr = attr_get_macaddr(cmd, clen, WLANTEST_ATTR_STA_ADDR);
  575. frame = attr_get_int(cmd, clen, WLANTEST_ATTR_INJECT_FRAME);
  576. sender_ap = attr_get_int(cmd, clen, WLANTEST_ATTR_INJECT_SENDER_AP);
  577. if (sender_ap < 0)
  578. sender_ap = 0;
  579. prot = attr_get_int(cmd, clen, WLANTEST_ATTR_INJECT_PROTECTION);
  580. if (bssid == NULL || sta_addr == NULL || frame < 0 || prot < 0) {
  581. wpa_printf(MSG_INFO, "Invalid inject command parameters");
  582. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  583. return;
  584. }
  585. bss = bss_find(wt, bssid);
  586. if (bss == NULL) {
  587. wpa_printf(MSG_INFO, "BSS not found for inject command");
  588. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  589. return;
  590. }
  591. if (is_broadcast_ether_addr(sta_addr)) {
  592. if (!sender_ap) {
  593. wpa_printf(MSG_INFO, "Invalid broadcast inject "
  594. "command without sender_ap set");
  595. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  596. return;
  597. } sta = NULL;
  598. } else {
  599. sta = sta_find(bss, sta_addr);
  600. if (sta == NULL) {
  601. wpa_printf(MSG_INFO, "Station not found for inject "
  602. "command");
  603. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  604. return;
  605. }
  606. }
  607. switch (frame) {
  608. case WLANTEST_FRAME_AUTH:
  609. ret = ctrl_inject_auth(wt, bss, sta, sender_ap, prot);
  610. break;
  611. case WLANTEST_FRAME_ASSOCREQ:
  612. ret = ctrl_inject_assocreq(wt, bss, sta, sender_ap, prot);
  613. break;
  614. case WLANTEST_FRAME_REASSOCREQ:
  615. ret = ctrl_inject_reassocreq(wt, bss, sta, sender_ap, prot);
  616. break;
  617. case WLANTEST_FRAME_DEAUTH:
  618. ret = ctrl_inject_deauth(wt, bss, sta, sender_ap, prot);
  619. break;
  620. case WLANTEST_FRAME_DISASSOC:
  621. ret = ctrl_inject_disassoc(wt, bss, sta, sender_ap, prot);
  622. break;
  623. case WLANTEST_FRAME_SAQUERYREQ:
  624. ret = ctrl_inject_saqueryreq(wt, bss, sta, sender_ap, prot);
  625. break;
  626. default:
  627. wpa_printf(MSG_INFO, "Unsupported inject command frame %d",
  628. frame);
  629. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  630. return;
  631. }
  632. if (ret)
  633. wpa_printf(MSG_INFO, "Failed to inject frame");
  634. else
  635. wpa_printf(MSG_INFO, "Frame injected successfully");
  636. ctrl_send_simple(wt, sock, ret == 0 ? WLANTEST_CTRL_SUCCESS :
  637. WLANTEST_CTRL_FAILURE);
  638. }
  639. static void ctrl_version(struct wlantest *wt, int sock)
  640. {
  641. u8 buf[WLANTEST_CTRL_MAX_RESP_LEN], *pos;
  642. pos = buf;
  643. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  644. pos += 4;
  645. pos = attr_add_str(pos, buf + sizeof(buf), WLANTEST_ATTR_VERSION,
  646. VERSION_STR);
  647. ctrl_send(wt, sock, buf, pos - buf);
  648. }
  649. static void ctrl_add_passphrase(struct wlantest *wt, int sock, u8 *cmd,
  650. size_t clen)
  651. {
  652. u8 *passphrase;
  653. size_t len;
  654. struct wlantest_passphrase *p, *pa;
  655. u8 *bssid;
  656. passphrase = attr_get(cmd, clen, WLANTEST_ATTR_PASSPHRASE, &len);
  657. if (passphrase == NULL) {
  658. u8 *wepkey;
  659. char *key;
  660. enum wlantest_ctrl_cmd res;
  661. wepkey = attr_get(cmd, clen, WLANTEST_ATTR_WEPKEY, &len);
  662. if (wepkey == NULL) {
  663. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  664. return;
  665. }
  666. key = os_zalloc(len + 1);
  667. if (key == NULL) {
  668. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  669. return;
  670. }
  671. os_memcpy(key, wepkey, len);
  672. if (add_wep(wt, key) < 0)
  673. res = WLANTEST_CTRL_FAILURE;
  674. else
  675. res = WLANTEST_CTRL_SUCCESS;
  676. os_free(key);
  677. ctrl_send_simple(wt, sock, res);
  678. return;
  679. }
  680. if (len < 8 || len > 63) {
  681. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  682. return;
  683. }
  684. p = os_zalloc(sizeof(*p));
  685. if (p == NULL) {
  686. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  687. return;
  688. }
  689. os_memcpy(p->passphrase, passphrase, len);
  690. wpa_printf(MSG_INFO, "Add passphrase '%s'", p->passphrase);
  691. bssid = attr_get_macaddr(cmd, clen, WLANTEST_ATTR_BSSID);
  692. if (bssid) {
  693. os_memcpy(p->bssid, bssid, ETH_ALEN);
  694. wpa_printf(MSG_INFO, "Limit passphrase for BSSID " MACSTR,
  695. MAC2STR(p->bssid));
  696. }
  697. dl_list_for_each(pa, &wt->passphrase, struct wlantest_passphrase, list)
  698. {
  699. if (os_strcmp(p->passphrase, pa->passphrase) == 0 &&
  700. os_memcmp(p->bssid, pa->bssid, ETH_ALEN) == 0) {
  701. wpa_printf(MSG_INFO, "Passphrase was already known");
  702. os_free(p);
  703. p = NULL;
  704. break;
  705. }
  706. }
  707. if (p) {
  708. struct wlantest_bss *bss;
  709. dl_list_add(&wt->passphrase, &p->list);
  710. dl_list_for_each(bss, &wt->bss, struct wlantest_bss, list) {
  711. if (bssid &&
  712. os_memcmp(p->bssid, bss->bssid, ETH_ALEN) != 0)
  713. continue;
  714. bss_add_pmk_from_passphrase(bss, p->passphrase);
  715. }
  716. }
  717. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  718. }
  719. static void info_print_proto(char *buf, size_t len, int proto)
  720. {
  721. char *pos, *end;
  722. if (proto == 0) {
  723. os_snprintf(buf, len, "OPEN");
  724. return;
  725. }
  726. pos = buf;
  727. end = buf + len;
  728. if (proto & WPA_PROTO_WPA)
  729. pos += os_snprintf(pos, end - pos, "%sWPA",
  730. pos == buf ? "" : " ");
  731. if (proto & WPA_PROTO_RSN)
  732. pos += os_snprintf(pos, end - pos, "%sWPA2",
  733. pos == buf ? "" : " ");
  734. }
  735. static void info_print_cipher(char *buf, size_t len, int cipher)
  736. {
  737. char *pos, *end;
  738. if (cipher == 0) {
  739. os_snprintf(buf, len, "N/A");
  740. return;
  741. }
  742. pos = buf;
  743. end = buf + len;
  744. if (cipher & WPA_CIPHER_NONE)
  745. pos += os_snprintf(pos, end - pos, "%sNONE",
  746. pos == buf ? "" : " ");
  747. if (cipher & WPA_CIPHER_WEP40)
  748. pos += os_snprintf(pos, end - pos, "%sWEP40",
  749. pos == buf ? "" : " ");
  750. if (cipher & WPA_CIPHER_WEP104)
  751. pos += os_snprintf(pos, end - pos, "%sWEP104",
  752. pos == buf ? "" : " ");
  753. if (cipher & WPA_CIPHER_TKIP)
  754. pos += os_snprintf(pos, end - pos, "%sTKIP",
  755. pos == buf ? "" : " ");
  756. if (cipher & WPA_CIPHER_CCMP)
  757. pos += os_snprintf(pos, end - pos, "%sCCMP",
  758. pos == buf ? "" : " ");
  759. if (cipher & WPA_CIPHER_AES_128_CMAC)
  760. pos += os_snprintf(pos, end - pos, "%sBIP",
  761. pos == buf ? "" : " ");
  762. if (cipher & WPA_CIPHER_BIP_GMAC_128)
  763. pos += os_snprintf(pos, end - pos, "%sBIP-GMAC-128",
  764. pos == buf ? "" : " ");
  765. if (cipher & WPA_CIPHER_BIP_GMAC_256)
  766. pos += os_snprintf(pos, end - pos, "%sBIP-GMAC-256",
  767. pos == buf ? "" : " ");
  768. if (cipher & WPA_CIPHER_BIP_CMAC_256)
  769. pos += os_snprintf(pos, end - pos, "%sBIP-CMAC-256",
  770. pos == buf ? "" : " ");
  771. }
  772. static void info_print_key_mgmt(char *buf, size_t len, int key_mgmt)
  773. {
  774. char *pos, *end;
  775. if (key_mgmt == 0) {
  776. os_snprintf(buf, len, "N/A");
  777. return;
  778. }
  779. pos = buf;
  780. end = buf + len;
  781. if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  782. pos += os_snprintf(pos, end - pos, "%sEAP",
  783. pos == buf ? "" : " ");
  784. if (key_mgmt & WPA_KEY_MGMT_PSK)
  785. pos += os_snprintf(pos, end - pos, "%sPSK",
  786. pos == buf ? "" : " ");
  787. if (key_mgmt & WPA_KEY_MGMT_WPA_NONE)
  788. pos += os_snprintf(pos, end - pos, "%sWPA-NONE",
  789. pos == buf ? "" : " ");
  790. if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  791. pos += os_snprintf(pos, end - pos, "%sFT-EAP",
  792. pos == buf ? "" : " ");
  793. if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
  794. pos += os_snprintf(pos, end - pos, "%sFT-PSK",
  795. pos == buf ? "" : " ");
  796. if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  797. pos += os_snprintf(pos, end - pos, "%sEAP-SHA256",
  798. pos == buf ? "" : " ");
  799. if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  800. pos += os_snprintf(pos, end - pos, "%sPSK-SHA256",
  801. pos == buf ? "" : " ");
  802. if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
  803. pos += os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
  804. pos == buf ? "" : " ");
  805. if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  806. pos += os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
  807. pos == buf ? "" : " ");
  808. }
  809. static void info_print_rsn_capab(char *buf, size_t len, int capab)
  810. {
  811. char *pos, *end;
  812. pos = buf;
  813. end = buf + len;
  814. if (capab & WPA_CAPABILITY_PREAUTH)
  815. pos += os_snprintf(pos, end - pos, "%sPREAUTH",
  816. pos == buf ? "" : " ");
  817. if (capab & WPA_CAPABILITY_NO_PAIRWISE)
  818. pos += os_snprintf(pos, end - pos, "%sNO_PAIRWISE",
  819. pos == buf ? "" : " ");
  820. if (capab & WPA_CAPABILITY_MFPR)
  821. pos += os_snprintf(pos, end - pos, "%sMFPR",
  822. pos == buf ? "" : " ");
  823. if (capab & WPA_CAPABILITY_MFPC)
  824. pos += os_snprintf(pos, end - pos, "%sMFPC",
  825. pos == buf ? "" : " ");
  826. if (capab & WPA_CAPABILITY_PEERKEY_ENABLED)
  827. pos += os_snprintf(pos, end - pos, "%sPEERKEY",
  828. pos == buf ? "" : " ");
  829. }
  830. static void info_print_state(char *buf, size_t len, int state)
  831. {
  832. switch (state) {
  833. case STATE1:
  834. os_strlcpy(buf, "NOT-AUTH", len);
  835. break;
  836. case STATE2:
  837. os_strlcpy(buf, "AUTH", len);
  838. break;
  839. case STATE3:
  840. os_strlcpy(buf, "AUTH+ASSOC", len);
  841. break;
  842. }
  843. }
  844. static void info_print_gtk(char *buf, size_t len, struct wlantest_sta *sta)
  845. {
  846. size_t pos;
  847. pos = os_snprintf(buf, len, "IDX=%d,GTK=", sta->gtk_idx);
  848. wpa_snprintf_hex(buf + pos, len - pos, sta->gtk, sta->gtk_len);
  849. }
  850. static void ctrl_info_sta(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
  851. {
  852. u8 *addr;
  853. size_t addr_len;
  854. struct wlantest_bss *bss;
  855. struct wlantest_sta *sta;
  856. enum wlantest_sta_info info;
  857. u8 buf[4 + 108], *end, *pos;
  858. char resp[100];
  859. bss = ctrl_get_bss(wt, sock, cmd, clen);
  860. sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
  861. if (sta == NULL)
  862. return;
  863. addr = attr_get(cmd, clen, WLANTEST_ATTR_STA_INFO, &addr_len);
  864. if (addr == NULL || addr_len != 4) {
  865. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  866. return;
  867. }
  868. info = WPA_GET_BE32(addr);
  869. resp[0] = '\0';
  870. switch (info) {
  871. case WLANTEST_STA_INFO_PROTO:
  872. info_print_proto(resp, sizeof(resp), sta->proto);
  873. break;
  874. case WLANTEST_STA_INFO_PAIRWISE:
  875. info_print_cipher(resp, sizeof(resp), sta->pairwise_cipher);
  876. break;
  877. case WLANTEST_STA_INFO_KEY_MGMT:
  878. info_print_key_mgmt(resp, sizeof(resp), sta->key_mgmt);
  879. break;
  880. case WLANTEST_STA_INFO_RSN_CAPAB:
  881. info_print_rsn_capab(resp, sizeof(resp), sta->rsn_capab);
  882. break;
  883. case WLANTEST_STA_INFO_STATE:
  884. info_print_state(resp, sizeof(resp), sta->state);
  885. break;
  886. case WLANTEST_STA_INFO_GTK:
  887. info_print_gtk(resp, sizeof(resp), sta);
  888. break;
  889. default:
  890. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  891. return;
  892. }
  893. pos = buf;
  894. end = buf + sizeof(buf);
  895. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  896. pos += 4;
  897. pos = attr_add_str(pos, end, WLANTEST_ATTR_INFO, resp);
  898. ctrl_send(wt, sock, buf, pos - buf);
  899. }
  900. static void ctrl_info_bss(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
  901. {
  902. u8 *addr;
  903. size_t addr_len;
  904. struct wlantest_bss *bss;
  905. enum wlantest_bss_info info;
  906. u8 buf[4 + 108], *end, *pos;
  907. char resp[100];
  908. bss = ctrl_get_bss(wt, sock, cmd, clen);
  909. if (bss == NULL)
  910. return;
  911. addr = attr_get(cmd, clen, WLANTEST_ATTR_BSS_INFO, &addr_len);
  912. if (addr == NULL || addr_len != 4) {
  913. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  914. return;
  915. }
  916. info = WPA_GET_BE32(addr);
  917. resp[0] = '\0';
  918. switch (info) {
  919. case WLANTEST_BSS_INFO_PROTO:
  920. info_print_proto(resp, sizeof(resp), bss->proto);
  921. break;
  922. case WLANTEST_BSS_INFO_PAIRWISE:
  923. info_print_cipher(resp, sizeof(resp), bss->pairwise_cipher);
  924. break;
  925. case WLANTEST_BSS_INFO_GROUP:
  926. info_print_cipher(resp, sizeof(resp), bss->group_cipher);
  927. break;
  928. case WLANTEST_BSS_INFO_GROUP_MGMT:
  929. info_print_cipher(resp, sizeof(resp), bss->mgmt_group_cipher);
  930. break;
  931. case WLANTEST_BSS_INFO_KEY_MGMT:
  932. info_print_key_mgmt(resp, sizeof(resp), bss->key_mgmt);
  933. break;
  934. case WLANTEST_BSS_INFO_RSN_CAPAB:
  935. info_print_rsn_capab(resp, sizeof(resp), bss->rsn_capab);
  936. break;
  937. default:
  938. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  939. return;
  940. }
  941. pos = buf;
  942. end = buf + sizeof(buf);
  943. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  944. pos += 4;
  945. pos = attr_add_str(pos, end, WLANTEST_ATTR_INFO, resp);
  946. ctrl_send(wt, sock, buf, pos - buf);
  947. }
  948. static void ctrl_send_(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
  949. {
  950. struct wlantest_bss *bss;
  951. struct wlantest_sta *sta;
  952. u8 *bssid, *sta_addr;
  953. int prot;
  954. u8 *frame;
  955. size_t frame_len;
  956. int ret = 0;
  957. struct ieee80211_hdr *hdr;
  958. u16 fc;
  959. frame = attr_get(cmd, clen, WLANTEST_ATTR_FRAME, &frame_len);
  960. prot = attr_get_int(cmd, clen, WLANTEST_ATTR_INJECT_PROTECTION);
  961. if (frame == NULL || frame_len < 24 || prot < 0) {
  962. wpa_printf(MSG_INFO, "Invalid send command parameters");
  963. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  964. return;
  965. }
  966. hdr = (struct ieee80211_hdr *) frame;
  967. fc = le_to_host16(hdr->frame_control);
  968. switch (WLAN_FC_GET_TYPE(fc)) {
  969. case WLAN_FC_TYPE_MGMT:
  970. bssid = hdr->addr3;
  971. if (os_memcmp(hdr->addr2, hdr->addr3, ETH_ALEN) == 0)
  972. sta_addr = hdr->addr1;
  973. else
  974. sta_addr = hdr->addr2;
  975. break;
  976. case WLAN_FC_TYPE_DATA:
  977. switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
  978. case 0:
  979. bssid = hdr->addr3;
  980. sta_addr = hdr->addr2;
  981. break;
  982. case WLAN_FC_TODS:
  983. bssid = hdr->addr1;
  984. sta_addr = hdr->addr2;
  985. break;
  986. case WLAN_FC_FROMDS:
  987. bssid = hdr->addr2;
  988. sta_addr = hdr->addr1;
  989. break;
  990. default:
  991. wpa_printf(MSG_INFO, "Unsupported inject frame");
  992. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  993. return;
  994. }
  995. break;
  996. default:
  997. wpa_printf(MSG_INFO, "Unsupported inject frame");
  998. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  999. return;
  1000. }
  1001. bss = bss_find(wt, bssid);
  1002. if (bss == NULL && prot != WLANTEST_INJECT_UNPROTECTED) {
  1003. wpa_printf(MSG_INFO, "Unknown BSSID");
  1004. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  1005. return;
  1006. }
  1007. if (bss)
  1008. sta = sta_find(bss, sta_addr);
  1009. else
  1010. sta = NULL;
  1011. if (sta == NULL && prot != WLANTEST_INJECT_UNPROTECTED) {
  1012. wpa_printf(MSG_INFO, "Unknown STA address");
  1013. ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
  1014. return;
  1015. }
  1016. ret = wlantest_inject(wt, bss, sta, frame, frame_len, prot);
  1017. if (ret)
  1018. wpa_printf(MSG_INFO, "Failed to inject frame");
  1019. else
  1020. wpa_printf(MSG_INFO, "Frame injected successfully");
  1021. ctrl_send_simple(wt, sock, ret == 0 ? WLANTEST_CTRL_SUCCESS :
  1022. WLANTEST_CTRL_FAILURE);
  1023. }
  1024. static void ctrl_relog(struct wlantest *wt, int sock)
  1025. {
  1026. int res = wlantest_relog(wt);
  1027. ctrl_send_simple(wt, sock, res ? WLANTEST_CTRL_FAILURE :
  1028. WLANTEST_CTRL_SUCCESS);
  1029. }
  1030. static void ctrl_get_tx_tid(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
  1031. {
  1032. u8 *addr;
  1033. size_t addr_len;
  1034. struct wlantest_bss *bss;
  1035. struct wlantest_sta *sta;
  1036. u32 counter;
  1037. u8 buf[4 + 12], *end, *pos;
  1038. bss = ctrl_get_bss(wt, sock, cmd, clen);
  1039. sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
  1040. if (sta == NULL)
  1041. return;
  1042. addr = attr_get(cmd, clen, WLANTEST_ATTR_TID, &addr_len);
  1043. if (addr == NULL || addr_len != 4) {
  1044. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  1045. return;
  1046. }
  1047. counter = WPA_GET_BE32(addr);
  1048. if (counter >= 16 + 1) {
  1049. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  1050. return;
  1051. }
  1052. pos = buf;
  1053. end = buf + sizeof(buf);
  1054. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  1055. pos += 4;
  1056. pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
  1057. sta->tx_tid[counter]);
  1058. ctrl_send(wt, sock, buf, pos - buf);
  1059. }
  1060. static void ctrl_get_rx_tid(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
  1061. {
  1062. u8 *addr;
  1063. size_t addr_len;
  1064. struct wlantest_bss *bss;
  1065. struct wlantest_sta *sta;
  1066. u32 counter;
  1067. u8 buf[4 + 12], *end, *pos;
  1068. bss = ctrl_get_bss(wt, sock, cmd, clen);
  1069. sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
  1070. if (sta == NULL)
  1071. return;
  1072. addr = attr_get(cmd, clen, WLANTEST_ATTR_TID, &addr_len);
  1073. if (addr == NULL || addr_len != 4) {
  1074. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  1075. return;
  1076. }
  1077. counter = WPA_GET_BE32(addr);
  1078. if (counter >= 16 + 1) {
  1079. ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
  1080. return;
  1081. }
  1082. pos = buf;
  1083. end = buf + sizeof(buf);
  1084. WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
  1085. pos += 4;
  1086. pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
  1087. sta->rx_tid[counter]);
  1088. ctrl_send(wt, sock, buf, pos - buf);
  1089. }
  1090. static void ctrl_read(int sock, void *eloop_ctx, void *sock_ctx)
  1091. {
  1092. struct wlantest *wt = eloop_ctx;
  1093. u8 buf[WLANTEST_CTRL_MAX_CMD_LEN];
  1094. int len;
  1095. enum wlantest_ctrl_cmd cmd;
  1096. wpa_printf(MSG_EXCESSIVE, "New control interface message from %d",
  1097. sock);
  1098. len = recv(sock, buf, sizeof(buf), 0);
  1099. if (len < 0) {
  1100. wpa_printf(MSG_INFO, "recv(ctrl): %s", strerror(errno));
  1101. ctrl_disconnect(wt, sock);
  1102. return;
  1103. }
  1104. if (len == 0) {
  1105. ctrl_disconnect(wt, sock);
  1106. return;
  1107. }
  1108. if (len < 4) {
  1109. wpa_printf(MSG_INFO, "Too short control interface command "
  1110. "from %d", sock);
  1111. ctrl_disconnect(wt, sock);
  1112. return;
  1113. }
  1114. cmd = WPA_GET_BE32(buf);
  1115. wpa_printf(MSG_EXCESSIVE, "Control interface command %d from %d",
  1116. cmd, sock);
  1117. switch (cmd) {
  1118. case WLANTEST_CTRL_PING:
  1119. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  1120. break;
  1121. case WLANTEST_CTRL_TERMINATE:
  1122. ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
  1123. eloop_terminate();
  1124. break;
  1125. case WLANTEST_CTRL_LIST_BSS:
  1126. ctrl_list_bss(wt, sock);
  1127. break;
  1128. case WLANTEST_CTRL_LIST_STA:
  1129. ctrl_list_sta(wt, sock, buf + 4, len - 4);
  1130. break;
  1131. case WLANTEST_CTRL_FLUSH:
  1132. ctrl_flush(wt, sock);
  1133. break;
  1134. case WLANTEST_CTRL_CLEAR_STA_COUNTERS:
  1135. ctrl_clear_sta_counters(wt, sock, buf + 4, len - 4);
  1136. break;
  1137. case WLANTEST_CTRL_CLEAR_BSS_COUNTERS:
  1138. ctrl_clear_bss_counters(wt, sock, buf + 4, len - 4);
  1139. break;
  1140. case WLANTEST_CTRL_CLEAR_TDLS_COUNTERS:
  1141. ctrl_clear_tdls_counters(wt, sock, buf + 4, len - 4);
  1142. break;
  1143. case WLANTEST_CTRL_GET_STA_COUNTER:
  1144. ctrl_get_sta_counter(wt, sock, buf + 4, len - 4);
  1145. break;
  1146. case WLANTEST_CTRL_GET_BSS_COUNTER:
  1147. ctrl_get_bss_counter(wt, sock, buf + 4, len - 4);
  1148. break;
  1149. case WLANTEST_CTRL_GET_TDLS_COUNTER:
  1150. ctrl_get_tdls_counter(wt, sock, buf + 4, len - 4);
  1151. break;
  1152. case WLANTEST_CTRL_INJECT:
  1153. ctrl_inject(wt, sock, buf + 4, len - 4);
  1154. break;
  1155. case WLANTEST_CTRL_VERSION:
  1156. ctrl_version(wt, sock);
  1157. break;
  1158. case WLANTEST_CTRL_ADD_PASSPHRASE:
  1159. ctrl_add_passphrase(wt, sock, buf + 4, len - 4);
  1160. break;
  1161. case WLANTEST_CTRL_INFO_STA:
  1162. ctrl_info_sta(wt, sock, buf + 4, len - 4);
  1163. break;
  1164. case WLANTEST_CTRL_INFO_BSS:
  1165. ctrl_info_bss(wt, sock, buf + 4, len - 4);
  1166. break;
  1167. case WLANTEST_CTRL_SEND:
  1168. ctrl_send_(wt, sock, buf + 4, len - 4);
  1169. break;
  1170. case WLANTEST_CTRL_RELOG:
  1171. ctrl_relog(wt, sock);
  1172. break;
  1173. case WLANTEST_CTRL_GET_TX_TID:
  1174. ctrl_get_tx_tid(wt, sock, buf + 4, len - 4);
  1175. break;
  1176. case WLANTEST_CTRL_GET_RX_TID:
  1177. ctrl_get_rx_tid(wt, sock, buf + 4, len - 4);
  1178. break;
  1179. default:
  1180. ctrl_send_simple(wt, sock, WLANTEST_CTRL_UNKNOWN_CMD);
  1181. break;
  1182. }
  1183. }
  1184. static void ctrl_connect(int sock, void *eloop_ctx, void *sock_ctx)
  1185. {
  1186. struct wlantest *wt = eloop_ctx;
  1187. int conn, i;
  1188. conn = accept(sock, NULL, NULL);
  1189. if (conn < 0) {
  1190. wpa_printf(MSG_INFO, "accept(ctrl): %s", strerror(errno));
  1191. return;
  1192. }
  1193. wpa_printf(MSG_MSGDUMP, "New control interface connection %d", conn);
  1194. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++) {
  1195. if (wt->ctrl_socks[i] < 0)
  1196. break;
  1197. }
  1198. if (i == MAX_CTRL_CONNECTIONS) {
  1199. wpa_printf(MSG_INFO, "No room for new control connection");
  1200. close(conn);
  1201. return;
  1202. }
  1203. wt->ctrl_socks[i] = conn;
  1204. eloop_register_read_sock(conn, ctrl_read, wt, NULL);
  1205. }
  1206. int ctrl_init(struct wlantest *wt)
  1207. {
  1208. struct sockaddr_un addr;
  1209. wt->ctrl_sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  1210. if (wt->ctrl_sock < 0) {
  1211. wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
  1212. return -1;
  1213. }
  1214. os_memset(&addr, 0, sizeof(addr));
  1215. addr.sun_family = AF_UNIX;
  1216. os_strlcpy(addr.sun_path + 1, WLANTEST_SOCK_NAME,
  1217. sizeof(addr.sun_path) - 1);
  1218. if (bind(wt->ctrl_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1219. wpa_printf(MSG_ERROR, "bind: %s", strerror(errno));
  1220. close(wt->ctrl_sock);
  1221. wt->ctrl_sock = -1;
  1222. return -1;
  1223. }
  1224. if (listen(wt->ctrl_sock, 5) < 0) {
  1225. wpa_printf(MSG_ERROR, "listen: %s", strerror(errno));
  1226. close(wt->ctrl_sock);
  1227. wt->ctrl_sock = -1;
  1228. return -1;
  1229. }
  1230. if (eloop_register_read_sock(wt->ctrl_sock, ctrl_connect, wt, NULL)) {
  1231. close(wt->ctrl_sock);
  1232. wt->ctrl_sock = -1;
  1233. return -1;
  1234. }
  1235. return 0;
  1236. }
  1237. void ctrl_deinit(struct wlantest *wt)
  1238. {
  1239. int i;
  1240. if (wt->ctrl_sock < 0)
  1241. return;
  1242. for (i = 0; i < MAX_CTRL_CONNECTIONS; i++) {
  1243. if (wt->ctrl_socks[i] >= 0) {
  1244. close(wt->ctrl_socks[i]);
  1245. eloop_unregister_read_sock(wt->ctrl_socks[i]);
  1246. wt->ctrl_socks[i] = -1;
  1247. }
  1248. }
  1249. eloop_unregister_read_sock(wt->ctrl_sock);
  1250. close(wt->ctrl_sock);
  1251. wt->ctrl_sock = -1;
  1252. }