ctrl_iface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2009, 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. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include <sys/un.h>
  17. #include <sys/stat.h>
  18. #include <stddef.h>
  19. #include "utils/common.h"
  20. #include "utils/eloop.h"
  21. #include "common/ieee802_11_defs.h"
  22. #include "drivers/driver.h"
  23. #include "radius/radius_client.h"
  24. #include "ap/hostapd.h"
  25. #include "ap/ap_config.h"
  26. #include "ap/ieee802_1x.h"
  27. #include "ap/wpa_auth.h"
  28. #include "ap/ieee802_11.h"
  29. #include "ap/sta_info.h"
  30. #include "ap/accounting.h"
  31. #include "ap/wps_hostapd.h"
  32. #include "ap/ctrl_iface_ap.h"
  33. #include "ctrl_iface.h"
  34. struct wpa_ctrl_dst {
  35. struct wpa_ctrl_dst *next;
  36. struct sockaddr_un addr;
  37. socklen_t addrlen;
  38. int debug_level;
  39. int errors;
  40. };
  41. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  42. const char *buf, size_t len);
  43. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  44. struct sockaddr_un *from,
  45. socklen_t fromlen)
  46. {
  47. struct wpa_ctrl_dst *dst;
  48. dst = os_zalloc(sizeof(*dst));
  49. if (dst == NULL)
  50. return -1;
  51. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  52. dst->addrlen = fromlen;
  53. dst->debug_level = MSG_INFO;
  54. dst->next = hapd->ctrl_dst;
  55. hapd->ctrl_dst = dst;
  56. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  57. (u8 *) from->sun_path,
  58. fromlen - offsetof(struct sockaddr_un, sun_path));
  59. return 0;
  60. }
  61. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  62. struct sockaddr_un *from,
  63. socklen_t fromlen)
  64. {
  65. struct wpa_ctrl_dst *dst, *prev = NULL;
  66. dst = hapd->ctrl_dst;
  67. while (dst) {
  68. if (fromlen == dst->addrlen &&
  69. os_memcmp(from->sun_path, dst->addr.sun_path,
  70. fromlen - offsetof(struct sockaddr_un, sun_path))
  71. == 0) {
  72. if (prev == NULL)
  73. hapd->ctrl_dst = dst->next;
  74. else
  75. prev->next = dst->next;
  76. os_free(dst);
  77. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  78. (u8 *) from->sun_path,
  79. fromlen -
  80. offsetof(struct sockaddr_un, sun_path));
  81. return 0;
  82. }
  83. prev = dst;
  84. dst = dst->next;
  85. }
  86. return -1;
  87. }
  88. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  89. struct sockaddr_un *from,
  90. socklen_t fromlen,
  91. char *level)
  92. {
  93. struct wpa_ctrl_dst *dst;
  94. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  95. dst = hapd->ctrl_dst;
  96. while (dst) {
  97. if (fromlen == dst->addrlen &&
  98. os_memcmp(from->sun_path, dst->addr.sun_path,
  99. fromlen - offsetof(struct sockaddr_un, sun_path))
  100. == 0) {
  101. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  102. "level", (u8 *) from->sun_path, fromlen -
  103. offsetof(struct sockaddr_un, sun_path));
  104. dst->debug_level = atoi(level);
  105. return 0;
  106. }
  107. dst = dst->next;
  108. }
  109. return -1;
  110. }
  111. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  112. const char *txtaddr)
  113. {
  114. u8 addr[ETH_ALEN];
  115. struct sta_info *sta;
  116. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  117. if (hwaddr_aton(txtaddr, addr))
  118. return -1;
  119. sta = ap_get_sta(hapd, addr);
  120. if (sta)
  121. return 0;
  122. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  123. "notification", MAC2STR(addr));
  124. sta = ap_sta_add(hapd, addr);
  125. if (sta == NULL)
  126. return -1;
  127. hostapd_new_assoc_sta(hapd, sta, 0);
  128. return 0;
  129. }
  130. static int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
  131. const char *txtaddr)
  132. {
  133. u8 addr[ETH_ALEN];
  134. struct sta_info *sta;
  135. const char *pos;
  136. wpa_printf(MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s", txtaddr);
  137. if (hwaddr_aton(txtaddr, addr))
  138. return -1;
  139. pos = os_strstr(txtaddr, " test=");
  140. if (pos) {
  141. struct ieee80211_mgmt mgmt;
  142. int encrypt;
  143. if (hapd->driver->send_frame == NULL)
  144. return -1;
  145. pos += 6;
  146. encrypt = atoi(pos);
  147. os_memset(&mgmt, 0, sizeof(mgmt));
  148. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  149. WLAN_FC_STYPE_DEAUTH);
  150. os_memcpy(mgmt.da, addr, ETH_ALEN);
  151. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  152. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  153. mgmt.u.deauth.reason_code =
  154. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  155. if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
  156. IEEE80211_HDRLEN +
  157. sizeof(mgmt.u.deauth),
  158. encrypt) < 0)
  159. return -1;
  160. return 0;
  161. }
  162. hapd->drv.sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  163. sta = ap_get_sta(hapd, addr);
  164. if (sta)
  165. ap_sta_deauthenticate(hapd, sta,
  166. WLAN_REASON_PREV_AUTH_NOT_VALID);
  167. return 0;
  168. }
  169. static int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
  170. const char *txtaddr)
  171. {
  172. u8 addr[ETH_ALEN];
  173. struct sta_info *sta;
  174. const char *pos;
  175. wpa_printf(MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s", txtaddr);
  176. if (hwaddr_aton(txtaddr, addr))
  177. return -1;
  178. pos = os_strstr(txtaddr, " test=");
  179. if (pos) {
  180. struct ieee80211_mgmt mgmt;
  181. int encrypt;
  182. if (hapd->driver->send_frame == NULL)
  183. return -1;
  184. pos += 6;
  185. encrypt = atoi(pos);
  186. os_memset(&mgmt, 0, sizeof(mgmt));
  187. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  188. WLAN_FC_STYPE_DISASSOC);
  189. os_memcpy(mgmt.da, addr, ETH_ALEN);
  190. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  191. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  192. mgmt.u.disassoc.reason_code =
  193. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  194. if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
  195. IEEE80211_HDRLEN +
  196. sizeof(mgmt.u.deauth),
  197. encrypt) < 0)
  198. return -1;
  199. return 0;
  200. }
  201. hapd->drv.sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  202. sta = ap_get_sta(hapd, addr);
  203. if (sta)
  204. ap_sta_disassociate(hapd, sta,
  205. WLAN_REASON_PREV_AUTH_NOT_VALID);
  206. return 0;
  207. }
  208. #ifdef CONFIG_IEEE80211W
  209. #ifdef NEED_AP_MLME
  210. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  211. const char *txtaddr)
  212. {
  213. u8 addr[ETH_ALEN];
  214. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  215. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  216. if (hwaddr_aton(txtaddr, addr) ||
  217. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  218. return -1;
  219. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  220. return 0;
  221. }
  222. #endif /* NEED_AP_MLME */
  223. #endif /* CONFIG_IEEE80211W */
  224. #ifdef CONFIG_WPS
  225. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  226. {
  227. char *pin = os_strchr(txt, ' ');
  228. char *timeout_txt;
  229. int timeout;
  230. if (pin == NULL)
  231. return -1;
  232. *pin++ = '\0';
  233. timeout_txt = os_strchr(pin, ' ');
  234. if (timeout_txt) {
  235. *timeout_txt++ = '\0';
  236. timeout = atoi(timeout_txt);
  237. } else
  238. timeout = 0;
  239. return hostapd_wps_add_pin(hapd, txt, pin, timeout);
  240. }
  241. #ifdef CONFIG_WPS_OOB
  242. static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
  243. {
  244. char *path, *method, *name;
  245. path = os_strchr(txt, ' ');
  246. if (path == NULL)
  247. return -1;
  248. *path++ = '\0';
  249. method = os_strchr(path, ' ');
  250. if (method == NULL)
  251. return -1;
  252. *method++ = '\0';
  253. name = os_strchr(method, ' ');
  254. if (name != NULL)
  255. *name++ = '\0';
  256. return hostapd_wps_start_oob(hapd, txt, path, method, name);
  257. }
  258. #endif /* CONFIG_WPS_OOB */
  259. #endif /* CONFIG_WPS */
  260. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  261. void *sock_ctx)
  262. {
  263. struct hostapd_data *hapd = eloop_ctx;
  264. char buf[256];
  265. int res;
  266. struct sockaddr_un from;
  267. socklen_t fromlen = sizeof(from);
  268. char *reply;
  269. const int reply_size = 4096;
  270. int reply_len;
  271. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  272. (struct sockaddr *) &from, &fromlen);
  273. if (res < 0) {
  274. perror("recvfrom(ctrl_iface)");
  275. return;
  276. }
  277. buf[res] = '\0';
  278. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
  279. reply = os_malloc(reply_size);
  280. if (reply == NULL) {
  281. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  282. fromlen);
  283. return;
  284. }
  285. os_memcpy(reply, "OK\n", 3);
  286. reply_len = 3;
  287. if (os_strcmp(buf, "PING") == 0) {
  288. os_memcpy(reply, "PONG\n", 5);
  289. reply_len = 5;
  290. } else if (os_strcmp(buf, "MIB") == 0) {
  291. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  292. if (reply_len >= 0) {
  293. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  294. reply_size - reply_len);
  295. if (res < 0)
  296. reply_len = -1;
  297. else
  298. reply_len += res;
  299. }
  300. if (reply_len >= 0) {
  301. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  302. reply_size - reply_len);
  303. if (res < 0)
  304. reply_len = -1;
  305. else
  306. reply_len += res;
  307. }
  308. #ifndef CONFIG_NO_RADIUS
  309. if (reply_len >= 0) {
  310. res = radius_client_get_mib(hapd->radius,
  311. reply + reply_len,
  312. reply_size - reply_len);
  313. if (res < 0)
  314. reply_len = -1;
  315. else
  316. reply_len += res;
  317. }
  318. #endif /* CONFIG_NO_RADIUS */
  319. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  320. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  321. reply_size);
  322. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  323. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  324. reply_size);
  325. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  326. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  327. reply_size);
  328. } else if (os_strcmp(buf, "ATTACH") == 0) {
  329. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  330. reply_len = -1;
  331. } else if (os_strcmp(buf, "DETACH") == 0) {
  332. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  333. reply_len = -1;
  334. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  335. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  336. buf + 6))
  337. reply_len = -1;
  338. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  339. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  340. reply_len = -1;
  341. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  342. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  343. reply_len = -1;
  344. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  345. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  346. reply_len = -1;
  347. #ifdef CONFIG_IEEE80211W
  348. #ifdef NEED_AP_MLME
  349. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  350. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  351. reply_len = -1;
  352. #endif /* NEED_AP_MLME */
  353. #endif /* CONFIG_IEEE80211W */
  354. #ifdef CONFIG_WPS
  355. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  356. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  357. reply_len = -1;
  358. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  359. if (hostapd_wps_button_pushed(hapd))
  360. reply_len = -1;
  361. #ifdef CONFIG_WPS_OOB
  362. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  363. if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
  364. reply_len = -1;
  365. #endif /* CONFIG_WPS_OOB */
  366. #endif /* CONFIG_WPS */
  367. } else {
  368. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  369. reply_len = 16;
  370. }
  371. if (reply_len < 0) {
  372. os_memcpy(reply, "FAIL\n", 5);
  373. reply_len = 5;
  374. }
  375. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  376. os_free(reply);
  377. }
  378. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  379. {
  380. char *buf;
  381. size_t len;
  382. if (hapd->conf->ctrl_interface == NULL)
  383. return NULL;
  384. len = os_strlen(hapd->conf->ctrl_interface) +
  385. os_strlen(hapd->conf->iface) + 2;
  386. buf = os_malloc(len);
  387. if (buf == NULL)
  388. return NULL;
  389. os_snprintf(buf, len, "%s/%s",
  390. hapd->conf->ctrl_interface, hapd->conf->iface);
  391. buf[len - 1] = '\0';
  392. return buf;
  393. }
  394. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  395. const char *txt, size_t len)
  396. {
  397. struct hostapd_data *hapd = ctx;
  398. if (hapd == NULL)
  399. return;
  400. hostapd_ctrl_iface_send(hapd, level, txt, len);
  401. }
  402. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  403. {
  404. struct sockaddr_un addr;
  405. int s = -1;
  406. char *fname = NULL;
  407. hapd->ctrl_sock = -1;
  408. if (hapd->conf->ctrl_interface == NULL)
  409. return 0;
  410. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  411. if (errno == EEXIST) {
  412. wpa_printf(MSG_DEBUG, "Using existing control "
  413. "interface directory.");
  414. } else {
  415. perror("mkdir[ctrl_interface]");
  416. goto fail;
  417. }
  418. }
  419. if (hapd->conf->ctrl_interface_gid_set &&
  420. chown(hapd->conf->ctrl_interface, 0,
  421. hapd->conf->ctrl_interface_gid) < 0) {
  422. perror("chown[ctrl_interface]");
  423. return -1;
  424. }
  425. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  426. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  427. goto fail;
  428. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  429. if (s < 0) {
  430. perror("socket(PF_UNIX)");
  431. goto fail;
  432. }
  433. os_memset(&addr, 0, sizeof(addr));
  434. #ifdef __FreeBSD__
  435. addr.sun_len = sizeof(addr);
  436. #endif /* __FreeBSD__ */
  437. addr.sun_family = AF_UNIX;
  438. fname = hostapd_ctrl_iface_path(hapd);
  439. if (fname == NULL)
  440. goto fail;
  441. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  442. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  443. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  444. strerror(errno));
  445. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  446. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  447. " allow connections - assuming it was left"
  448. "over from forced program termination");
  449. if (unlink(fname) < 0) {
  450. perror("unlink[ctrl_iface]");
  451. wpa_printf(MSG_ERROR, "Could not unlink "
  452. "existing ctrl_iface socket '%s'",
  453. fname);
  454. goto fail;
  455. }
  456. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  457. 0) {
  458. perror("bind(PF_UNIX)");
  459. goto fail;
  460. }
  461. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  462. "ctrl_iface socket '%s'", fname);
  463. } else {
  464. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  465. "be in use - cannot override it");
  466. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  467. "not used anymore", fname);
  468. os_free(fname);
  469. fname = NULL;
  470. goto fail;
  471. }
  472. }
  473. if (hapd->conf->ctrl_interface_gid_set &&
  474. chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
  475. perror("chown[ctrl_interface/ifname]");
  476. goto fail;
  477. }
  478. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  479. perror("chmod[ctrl_interface/ifname]");
  480. goto fail;
  481. }
  482. os_free(fname);
  483. hapd->ctrl_sock = s;
  484. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  485. NULL);
  486. hapd->msg_ctx = hapd;
  487. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  488. return 0;
  489. fail:
  490. if (s >= 0)
  491. close(s);
  492. if (fname) {
  493. unlink(fname);
  494. os_free(fname);
  495. }
  496. return -1;
  497. }
  498. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  499. {
  500. struct wpa_ctrl_dst *dst, *prev;
  501. if (hapd->ctrl_sock > -1) {
  502. char *fname;
  503. eloop_unregister_read_sock(hapd->ctrl_sock);
  504. close(hapd->ctrl_sock);
  505. hapd->ctrl_sock = -1;
  506. fname = hostapd_ctrl_iface_path(hapd);
  507. if (fname)
  508. unlink(fname);
  509. os_free(fname);
  510. if (hapd->conf->ctrl_interface &&
  511. rmdir(hapd->conf->ctrl_interface) < 0) {
  512. if (errno == ENOTEMPTY) {
  513. wpa_printf(MSG_DEBUG, "Control interface "
  514. "directory not empty - leaving it "
  515. "behind");
  516. } else {
  517. perror("rmdir[ctrl_interface]");
  518. }
  519. }
  520. }
  521. dst = hapd->ctrl_dst;
  522. while (dst) {
  523. prev = dst;
  524. dst = dst->next;
  525. os_free(prev);
  526. }
  527. }
  528. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  529. const char *buf, size_t len)
  530. {
  531. struct wpa_ctrl_dst *dst, *next;
  532. struct msghdr msg;
  533. int idx;
  534. struct iovec io[2];
  535. char levelstr[10];
  536. dst = hapd->ctrl_dst;
  537. if (hapd->ctrl_sock < 0 || dst == NULL)
  538. return;
  539. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  540. io[0].iov_base = levelstr;
  541. io[0].iov_len = os_strlen(levelstr);
  542. io[1].iov_base = (char *) buf;
  543. io[1].iov_len = len;
  544. os_memset(&msg, 0, sizeof(msg));
  545. msg.msg_iov = io;
  546. msg.msg_iovlen = 2;
  547. idx = 0;
  548. while (dst) {
  549. next = dst->next;
  550. if (level >= dst->debug_level) {
  551. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  552. (u8 *) dst->addr.sun_path, dst->addrlen -
  553. offsetof(struct sockaddr_un, sun_path));
  554. msg.msg_name = &dst->addr;
  555. msg.msg_namelen = dst->addrlen;
  556. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  557. int _errno = errno;
  558. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  559. "%d - %s",
  560. idx, errno, strerror(errno));
  561. dst->errors++;
  562. if (dst->errors > 10 || _errno == ENOENT) {
  563. hostapd_ctrl_iface_detach(
  564. hapd, &dst->addr,
  565. dst->addrlen);
  566. }
  567. } else
  568. dst->errors = 0;
  569. }
  570. idx++;
  571. dst = next;
  572. }
  573. }
  574. #endif /* CONFIG_NATIVE_WINDOWS */