ctrl.c 29 KB

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