ctrl.c 33 KB

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