ctrl_iface_unix.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * WPA Supplicant / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include <sys/un.h>
  10. #include <sys/stat.h>
  11. #include <grp.h>
  12. #include <stddef.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #ifdef ANDROID
  16. #include <cutils/sockets.h>
  17. #endif /* ANDROID */
  18. #include "utils/common.h"
  19. #include "utils/eloop.h"
  20. #include "utils/list.h"
  21. #include "eapol_supp/eapol_supp_sm.h"
  22. #include "config.h"
  23. #include "wpa_supplicant_i.h"
  24. #include "ctrl_iface.h"
  25. /* Per-interface ctrl_iface */
  26. /**
  27. * struct wpa_ctrl_dst - Internal data structure of control interface monitors
  28. *
  29. * This structure is used to store information about registered control
  30. * interface monitors into struct wpa_supplicant. This data is private to
  31. * ctrl_iface_unix.c and should not be touched directly from other files.
  32. */
  33. struct wpa_ctrl_dst {
  34. struct dl_list list;
  35. struct sockaddr_un addr;
  36. socklen_t addrlen;
  37. int debug_level;
  38. int errors;
  39. };
  40. struct ctrl_iface_priv {
  41. struct wpa_supplicant *wpa_s;
  42. int sock;
  43. struct dl_list ctrl_dst;
  44. };
  45. struct ctrl_iface_global_priv {
  46. struct wpa_global *global;
  47. int sock;
  48. struct dl_list ctrl_dst;
  49. };
  50. static void wpa_supplicant_ctrl_iface_send(const char *ifname, int sock,
  51. struct dl_list *ctrl_dst,
  52. int level, const char *buf,
  53. size_t len);
  54. static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
  55. struct sockaddr_un *from,
  56. socklen_t fromlen)
  57. {
  58. struct wpa_ctrl_dst *dst;
  59. dst = os_zalloc(sizeof(*dst));
  60. if (dst == NULL)
  61. return -1;
  62. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  63. dst->addrlen = fromlen;
  64. dst->debug_level = MSG_INFO;
  65. dl_list_add(ctrl_dst, &dst->list);
  66. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  67. (u8 *) from->sun_path,
  68. fromlen - offsetof(struct sockaddr_un, sun_path));
  69. return 0;
  70. }
  71. static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
  72. struct sockaddr_un *from,
  73. socklen_t fromlen)
  74. {
  75. struct wpa_ctrl_dst *dst;
  76. dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
  77. if (fromlen == dst->addrlen &&
  78. os_memcmp(from->sun_path, dst->addr.sun_path,
  79. fromlen - offsetof(struct sockaddr_un, sun_path))
  80. == 0) {
  81. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  82. (u8 *) from->sun_path,
  83. fromlen -
  84. offsetof(struct sockaddr_un, sun_path));
  85. dl_list_del(&dst->list);
  86. os_free(dst);
  87. return 0;
  88. }
  89. }
  90. return -1;
  91. }
  92. static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
  93. struct sockaddr_un *from,
  94. socklen_t fromlen,
  95. char *level)
  96. {
  97. struct wpa_ctrl_dst *dst;
  98. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  99. dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
  100. if (fromlen == dst->addrlen &&
  101. os_memcmp(from->sun_path, dst->addr.sun_path,
  102. fromlen - offsetof(struct sockaddr_un, sun_path))
  103. == 0) {
  104. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  105. "level", (u8 *) from->sun_path,
  106. fromlen -
  107. offsetof(struct sockaddr_un, sun_path));
  108. dst->debug_level = atoi(level);
  109. return 0;
  110. }
  111. }
  112. return -1;
  113. }
  114. static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
  115. void *sock_ctx)
  116. {
  117. struct wpa_supplicant *wpa_s = eloop_ctx;
  118. struct ctrl_iface_priv *priv = sock_ctx;
  119. char buf[4096];
  120. int res;
  121. struct sockaddr_un from;
  122. socklen_t fromlen = sizeof(from);
  123. char *reply = NULL;
  124. size_t reply_len = 0;
  125. int new_attached = 0;
  126. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  127. (struct sockaddr *) &from, &fromlen);
  128. if (res < 0) {
  129. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  130. strerror(errno));
  131. return;
  132. }
  133. buf[res] = '\0';
  134. if (os_strcmp(buf, "ATTACH") == 0) {
  135. if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
  136. fromlen))
  137. reply_len = 1;
  138. else {
  139. new_attached = 1;
  140. reply_len = 2;
  141. }
  142. } else if (os_strcmp(buf, "DETACH") == 0) {
  143. if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
  144. fromlen))
  145. reply_len = 1;
  146. else
  147. reply_len = 2;
  148. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  149. if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
  150. buf + 6))
  151. reply_len = 1;
  152. else
  153. reply_len = 2;
  154. } else {
  155. reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
  156. &reply_len);
  157. }
  158. if (reply) {
  159. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  160. fromlen) < 0) {
  161. wpa_dbg(wpa_s, MSG_DEBUG,
  162. "ctrl_iface sendto failed: %s",
  163. strerror(errno));
  164. }
  165. os_free(reply);
  166. } else if (reply_len == 1) {
  167. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  168. fromlen) < 0) {
  169. wpa_dbg(wpa_s, MSG_DEBUG,
  170. "ctrl_iface sendto failed: %s",
  171. strerror(errno));
  172. }
  173. } else if (reply_len == 2) {
  174. if (sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
  175. fromlen) < 0) {
  176. wpa_dbg(wpa_s, MSG_DEBUG,
  177. "ctrl_iface sendto failed: %s",
  178. strerror(errno));
  179. }
  180. }
  181. if (new_attached)
  182. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  183. }
  184. static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
  185. {
  186. char *buf;
  187. size_t len;
  188. char *pbuf, *dir = NULL, *gid_str = NULL;
  189. int res;
  190. if (wpa_s->conf->ctrl_interface == NULL)
  191. return NULL;
  192. pbuf = os_strdup(wpa_s->conf->ctrl_interface);
  193. if (pbuf == NULL)
  194. return NULL;
  195. if (os_strncmp(pbuf, "DIR=", 4) == 0) {
  196. dir = pbuf + 4;
  197. gid_str = os_strstr(dir, " GROUP=");
  198. if (gid_str) {
  199. *gid_str = '\0';
  200. gid_str += 7;
  201. }
  202. } else
  203. dir = pbuf;
  204. len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
  205. buf = os_malloc(len);
  206. if (buf == NULL) {
  207. os_free(pbuf);
  208. return NULL;
  209. }
  210. res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
  211. if (res < 0 || (size_t) res >= len) {
  212. os_free(pbuf);
  213. os_free(buf);
  214. return NULL;
  215. }
  216. #ifdef __CYGWIN__
  217. {
  218. /* Windows/WinPcap uses interface names that are not suitable
  219. * as a file name - convert invalid chars to underscores */
  220. char *pos = buf;
  221. while (*pos) {
  222. if (*pos == '\\')
  223. *pos = '_';
  224. pos++;
  225. }
  226. }
  227. #endif /* __CYGWIN__ */
  228. os_free(pbuf);
  229. return buf;
  230. }
  231. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, int global,
  232. const char *txt, size_t len)
  233. {
  234. struct wpa_supplicant *wpa_s = ctx;
  235. if (wpa_s == NULL)
  236. return;
  237. if (global != 2 && wpa_s->global->ctrl_iface) {
  238. struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
  239. if (!dl_list_empty(&priv->ctrl_dst)) {
  240. wpa_supplicant_ctrl_iface_send(global ? NULL :
  241. wpa_s->ifname,
  242. priv->sock,
  243. &priv->ctrl_dst,
  244. level, txt, len);
  245. }
  246. }
  247. if (wpa_s->ctrl_iface == NULL)
  248. return;
  249. wpa_supplicant_ctrl_iface_send(NULL, wpa_s->ctrl_iface->sock,
  250. &wpa_s->ctrl_iface->ctrl_dst,
  251. level, txt, len);
  252. }
  253. struct ctrl_iface_priv *
  254. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  255. {
  256. struct ctrl_iface_priv *priv;
  257. struct sockaddr_un addr;
  258. char *fname = NULL;
  259. gid_t gid = 0;
  260. int gid_set = 0;
  261. char *buf, *dir = NULL, *gid_str = NULL;
  262. struct group *grp;
  263. char *endp;
  264. int flags;
  265. priv = os_zalloc(sizeof(*priv));
  266. if (priv == NULL)
  267. return NULL;
  268. dl_list_init(&priv->ctrl_dst);
  269. priv->wpa_s = wpa_s;
  270. priv->sock = -1;
  271. if (wpa_s->conf->ctrl_interface == NULL)
  272. return priv;
  273. buf = os_strdup(wpa_s->conf->ctrl_interface);
  274. if (buf == NULL)
  275. goto fail;
  276. #ifdef ANDROID
  277. os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
  278. wpa_s->conf->ctrl_interface);
  279. priv->sock = android_get_control_socket(addr.sun_path);
  280. if (priv->sock >= 0)
  281. goto havesock;
  282. #endif /* ANDROID */
  283. if (os_strncmp(buf, "DIR=", 4) == 0) {
  284. dir = buf + 4;
  285. gid_str = os_strstr(dir, " GROUP=");
  286. if (gid_str) {
  287. *gid_str = '\0';
  288. gid_str += 7;
  289. }
  290. } else {
  291. dir = buf;
  292. gid_str = wpa_s->conf->ctrl_interface_group;
  293. }
  294. if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
  295. if (errno == EEXIST) {
  296. wpa_printf(MSG_DEBUG, "Using existing control "
  297. "interface directory.");
  298. } else {
  299. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
  300. dir, strerror(errno));
  301. goto fail;
  302. }
  303. }
  304. #ifdef ANDROID
  305. /*
  306. * wpa_supplicant is started from /init.*.rc on Android and that seems
  307. * to be using umask 0077 which would leave the control interface
  308. * directory without group access. This breaks things since Wi-Fi
  309. * framework assumes that this directory can be accessed by other
  310. * applications in the wifi group. Fix this by adding group access even
  311. * if umask value would prevent this.
  312. */
  313. if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
  314. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  315. strerror(errno));
  316. /* Try to continue anyway */
  317. }
  318. #endif /* ANDROID */
  319. if (gid_str) {
  320. grp = getgrnam(gid_str);
  321. if (grp) {
  322. gid = grp->gr_gid;
  323. gid_set = 1;
  324. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  325. " (from group name '%s')",
  326. (int) gid, gid_str);
  327. } else {
  328. /* Group name not found - try to parse this as gid */
  329. gid = strtol(gid_str, &endp, 10);
  330. if (*gid_str == '\0' || *endp != '\0') {
  331. wpa_printf(MSG_ERROR, "CTRL: Invalid group "
  332. "'%s'", gid_str);
  333. goto fail;
  334. }
  335. gid_set = 1;
  336. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  337. (int) gid);
  338. }
  339. }
  340. if (gid_set && chown(dir, -1, gid) < 0) {
  341. wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
  342. dir, (int) gid, strerror(errno));
  343. goto fail;
  344. }
  345. /* Make sure the group can enter and read the directory */
  346. if (gid_set &&
  347. chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
  348. wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
  349. strerror(errno));
  350. goto fail;
  351. }
  352. if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
  353. sizeof(addr.sun_path)) {
  354. wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
  355. goto fail;
  356. }
  357. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  358. if (priv->sock < 0) {
  359. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  360. goto fail;
  361. }
  362. os_memset(&addr, 0, sizeof(addr));
  363. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  364. addr.sun_len = sizeof(addr);
  365. #endif /* __FreeBSD__ */
  366. addr.sun_family = AF_UNIX;
  367. fname = wpa_supplicant_ctrl_iface_path(wpa_s);
  368. if (fname == NULL)
  369. goto fail;
  370. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  371. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  372. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  373. strerror(errno));
  374. if (connect(priv->sock, (struct sockaddr *) &addr,
  375. sizeof(addr)) < 0) {
  376. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  377. " allow connections - assuming it was left"
  378. "over from forced program termination");
  379. if (unlink(fname) < 0) {
  380. wpa_printf(MSG_ERROR,
  381. "Could not unlink existing ctrl_iface socket '%s': %s",
  382. fname, strerror(errno));
  383. goto fail;
  384. }
  385. if (bind(priv->sock, (struct sockaddr *) &addr,
  386. sizeof(addr)) < 0) {
  387. wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
  388. strerror(errno));
  389. goto fail;
  390. }
  391. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  392. "ctrl_iface socket '%s'", fname);
  393. } else {
  394. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  395. "be in use - cannot override it");
  396. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  397. "not used anymore", fname);
  398. os_free(fname);
  399. fname = NULL;
  400. goto fail;
  401. }
  402. }
  403. if (gid_set && chown(fname, -1, gid) < 0) {
  404. wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
  405. fname, (int) gid, strerror(errno));
  406. goto fail;
  407. }
  408. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  409. wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
  410. fname, strerror(errno));
  411. goto fail;
  412. }
  413. os_free(fname);
  414. #ifdef ANDROID
  415. havesock:
  416. #endif /* ANDROID */
  417. /*
  418. * Make socket non-blocking so that we don't hang forever if
  419. * target dies unexpectedly.
  420. */
  421. flags = fcntl(priv->sock, F_GETFL);
  422. if (flags >= 0) {
  423. flags |= O_NONBLOCK;
  424. if (fcntl(priv->sock, F_SETFL, flags) < 0) {
  425. wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
  426. strerror(errno));
  427. /* Not fatal, continue on.*/
  428. }
  429. }
  430. eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
  431. wpa_s, priv);
  432. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  433. os_free(buf);
  434. return priv;
  435. fail:
  436. if (priv->sock >= 0)
  437. close(priv->sock);
  438. os_free(priv);
  439. if (fname) {
  440. unlink(fname);
  441. os_free(fname);
  442. }
  443. os_free(buf);
  444. return NULL;
  445. }
  446. void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
  447. {
  448. struct wpa_ctrl_dst *dst, *prev;
  449. if (priv->sock > -1) {
  450. char *fname;
  451. char *buf, *dir = NULL, *gid_str = NULL;
  452. eloop_unregister_read_sock(priv->sock);
  453. if (!dl_list_empty(&priv->ctrl_dst)) {
  454. /*
  455. * Wait before closing the control socket if
  456. * there are any attached monitors in order to allow
  457. * them to receive any pending messages.
  458. */
  459. wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
  460. "monitors to receive messages");
  461. os_sleep(0, 100000);
  462. }
  463. close(priv->sock);
  464. priv->sock = -1;
  465. fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
  466. if (fname) {
  467. unlink(fname);
  468. os_free(fname);
  469. }
  470. buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
  471. if (buf == NULL)
  472. goto free_dst;
  473. if (os_strncmp(buf, "DIR=", 4) == 0) {
  474. dir = buf + 4;
  475. gid_str = os_strstr(dir, " GROUP=");
  476. if (gid_str) {
  477. *gid_str = '\0';
  478. gid_str += 7;
  479. }
  480. } else
  481. dir = buf;
  482. if (rmdir(dir) < 0) {
  483. if (errno == ENOTEMPTY) {
  484. wpa_printf(MSG_DEBUG, "Control interface "
  485. "directory not empty - leaving it "
  486. "behind");
  487. } else {
  488. wpa_printf(MSG_ERROR,
  489. "rmdir[ctrl_interface=%s]: %s",
  490. dir, strerror(errno));
  491. }
  492. }
  493. os_free(buf);
  494. }
  495. free_dst:
  496. dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
  497. list)
  498. os_free(dst);
  499. os_free(priv);
  500. }
  501. /**
  502. * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
  503. * @ifname: Interface name for global control socket or %NULL
  504. * @sock: Local socket fd
  505. * @ctrl_dst: List of attached listeners
  506. * @level: Priority level of the message
  507. * @buf: Message data
  508. * @len: Message length
  509. *
  510. * Send a packet to all monitor programs attached to the control interface.
  511. */
  512. static void wpa_supplicant_ctrl_iface_send(const char *ifname, int sock,
  513. struct dl_list *ctrl_dst,
  514. int level, const char *buf,
  515. size_t len)
  516. {
  517. struct wpa_ctrl_dst *dst, *next;
  518. char levelstr[10];
  519. int idx, res;
  520. struct msghdr msg;
  521. struct iovec io[5];
  522. if (sock < 0 || dl_list_empty(ctrl_dst))
  523. return;
  524. res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  525. if (res < 0 || (size_t) res >= sizeof(levelstr))
  526. return;
  527. idx = 0;
  528. if (ifname) {
  529. io[idx].iov_base = "IFNAME=";
  530. io[idx].iov_len = 7;
  531. idx++;
  532. io[idx].iov_base = (char *) ifname;
  533. io[idx].iov_len = os_strlen(ifname);
  534. idx++;
  535. io[idx].iov_base = " ";
  536. io[idx].iov_len = 1;
  537. idx++;
  538. }
  539. io[idx].iov_base = levelstr;
  540. io[idx].iov_len = os_strlen(levelstr);
  541. idx++;
  542. io[idx].iov_base = (char *) buf;
  543. io[idx].iov_len = len;
  544. idx++;
  545. os_memset(&msg, 0, sizeof(msg));
  546. msg.msg_iov = io;
  547. msg.msg_iovlen = idx;
  548. idx = 0;
  549. dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
  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 = (void *) &dst->addr;
  555. msg.msg_namelen = dst->addrlen;
  556. if (sendmsg(sock, &msg, MSG_DONTWAIT) < 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 > 1000 ||
  563. (_errno != ENOBUFS && dst->errors > 10) ||
  564. _errno == ENOENT) {
  565. wpa_supplicant_ctrl_iface_detach(
  566. ctrl_dst, &dst->addr,
  567. dst->addrlen);
  568. }
  569. } else
  570. dst->errors = 0;
  571. }
  572. idx++;
  573. }
  574. }
  575. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  576. {
  577. char buf[256];
  578. int res;
  579. struct sockaddr_un from;
  580. socklen_t fromlen = sizeof(from);
  581. for (;;) {
  582. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
  583. "attach", priv->wpa_s->ifname);
  584. eloop_wait_for_read_sock(priv->sock);
  585. res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
  586. (struct sockaddr *) &from, &fromlen);
  587. if (res < 0) {
  588. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  589. strerror(errno));
  590. continue;
  591. }
  592. buf[res] = '\0';
  593. if (os_strcmp(buf, "ATTACH") == 0) {
  594. /* handle ATTACH signal of first monitor interface */
  595. if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
  596. &from, fromlen)) {
  597. if (sendto(priv->sock, "OK\n", 3, 0,
  598. (struct sockaddr *) &from, fromlen) <
  599. 0) {
  600. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  601. strerror(errno));
  602. }
  603. /* OK to continue */
  604. return;
  605. } else {
  606. if (sendto(priv->sock, "FAIL\n", 5, 0,
  607. (struct sockaddr *) &from, fromlen) <
  608. 0) {
  609. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  610. strerror(errno));
  611. }
  612. }
  613. } else {
  614. /* return FAIL for all other signals */
  615. if (sendto(priv->sock, "FAIL\n", 5, 0,
  616. (struct sockaddr *) &from, fromlen) < 0) {
  617. wpa_printf(MSG_DEBUG,
  618. "ctrl_iface sendto failed: %s",
  619. strerror(errno));
  620. }
  621. }
  622. }
  623. }
  624. /* Global ctrl_iface */
  625. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  626. void *sock_ctx)
  627. {
  628. struct wpa_global *global = eloop_ctx;
  629. struct ctrl_iface_global_priv *priv = sock_ctx;
  630. char buf[256];
  631. int res;
  632. struct sockaddr_un from;
  633. socklen_t fromlen = sizeof(from);
  634. char *reply = NULL;
  635. size_t reply_len;
  636. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  637. (struct sockaddr *) &from, &fromlen);
  638. if (res < 0) {
  639. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  640. strerror(errno));
  641. return;
  642. }
  643. buf[res] = '\0';
  644. if (os_strcmp(buf, "ATTACH") == 0) {
  645. if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
  646. fromlen))
  647. reply_len = 1;
  648. else
  649. reply_len = 2;
  650. } else if (os_strcmp(buf, "DETACH") == 0) {
  651. if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
  652. fromlen))
  653. reply_len = 1;
  654. else
  655. reply_len = 2;
  656. } else {
  657. reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
  658. &reply_len);
  659. }
  660. if (reply) {
  661. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  662. fromlen) < 0) {
  663. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  664. strerror(errno));
  665. }
  666. os_free(reply);
  667. } else if (reply_len == 1) {
  668. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  669. fromlen) < 0) {
  670. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  671. strerror(errno));
  672. }
  673. } else if (reply_len == 2) {
  674. if (sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
  675. fromlen) < 0) {
  676. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  677. strerror(errno));
  678. }
  679. }
  680. }
  681. struct ctrl_iface_global_priv *
  682. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  683. {
  684. struct ctrl_iface_global_priv *priv;
  685. struct sockaddr_un addr;
  686. const char *ctrl = global->params.ctrl_interface;
  687. int flags;
  688. priv = os_zalloc(sizeof(*priv));
  689. if (priv == NULL)
  690. return NULL;
  691. dl_list_init(&priv->ctrl_dst);
  692. priv->global = global;
  693. priv->sock = -1;
  694. if (ctrl == NULL)
  695. return priv;
  696. wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
  697. #ifdef ANDROID
  698. if (os_strncmp(ctrl, "@android:", 9) == 0) {
  699. priv->sock = android_get_control_socket(ctrl + 9);
  700. if (priv->sock < 0) {
  701. wpa_printf(MSG_ERROR, "Failed to open Android control "
  702. "socket '%s'", ctrl + 9);
  703. goto fail;
  704. }
  705. wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
  706. ctrl + 9);
  707. goto havesock;
  708. }
  709. if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
  710. /*
  711. * Backwards compatibility - try to open an Android control
  712. * socket and if that fails, assume this was a UNIX domain
  713. * socket instead.
  714. */
  715. priv->sock = android_get_control_socket(ctrl);
  716. if (priv->sock >= 0) {
  717. wpa_printf(MSG_DEBUG,
  718. "Using Android control socket '%s'",
  719. ctrl);
  720. goto havesock;
  721. }
  722. }
  723. #endif /* ANDROID */
  724. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  725. if (priv->sock < 0) {
  726. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  727. goto fail;
  728. }
  729. os_memset(&addr, 0, sizeof(addr));
  730. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  731. addr.sun_len = sizeof(addr);
  732. #endif /* __FreeBSD__ */
  733. addr.sun_family = AF_UNIX;
  734. if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
  735. addr.sun_path[0] = '\0';
  736. os_strlcpy(addr.sun_path + 1, ctrl + 10,
  737. sizeof(addr.sun_path) - 1);
  738. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
  739. 0) {
  740. wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
  741. "bind(PF_UNIX;%s) failed: %s",
  742. ctrl, strerror(errno));
  743. goto fail;
  744. }
  745. wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
  746. ctrl + 10);
  747. goto havesock;
  748. }
  749. os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
  750. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  751. wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
  752. ctrl, strerror(errno));
  753. if (connect(priv->sock, (struct sockaddr *) &addr,
  754. sizeof(addr)) < 0) {
  755. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  756. " allow connections - assuming it was left"
  757. "over from forced program termination");
  758. if (unlink(ctrl) < 0) {
  759. wpa_printf(MSG_ERROR,
  760. "Could not unlink existing ctrl_iface socket '%s': %s",
  761. ctrl, strerror(errno));
  762. goto fail;
  763. }
  764. if (bind(priv->sock, (struct sockaddr *) &addr,
  765. sizeof(addr)) < 0) {
  766. wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
  767. ctrl, strerror(errno));
  768. goto fail;
  769. }
  770. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  771. "ctrl_iface socket '%s'",
  772. ctrl);
  773. } else {
  774. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  775. "be in use - cannot override it");
  776. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  777. "not used anymore",
  778. ctrl);
  779. goto fail;
  780. }
  781. }
  782. wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
  783. if (global->params.ctrl_interface_group) {
  784. char *gid_str = global->params.ctrl_interface_group;
  785. gid_t gid = 0;
  786. struct group *grp;
  787. char *endp;
  788. grp = getgrnam(gid_str);
  789. if (grp) {
  790. gid = grp->gr_gid;
  791. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  792. " (from group name '%s')",
  793. (int) gid, gid_str);
  794. } else {
  795. /* Group name not found - try to parse this as gid */
  796. gid = strtol(gid_str, &endp, 10);
  797. if (*gid_str == '\0' || *endp != '\0') {
  798. wpa_printf(MSG_ERROR, "CTRL: Invalid group "
  799. "'%s'", gid_str);
  800. goto fail;
  801. }
  802. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  803. (int) gid);
  804. }
  805. if (chown(ctrl, -1, gid) < 0) {
  806. wpa_printf(MSG_ERROR,
  807. "chown[global_ctrl_interface=%s,gid=%d]: %s",
  808. ctrl, (int) gid, strerror(errno));
  809. goto fail;
  810. }
  811. if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
  812. wpa_printf(MSG_ERROR,
  813. "chmod[global_ctrl_interface=%s]: %s",
  814. ctrl, strerror(errno));
  815. goto fail;
  816. }
  817. } else {
  818. chmod(ctrl, S_IRWXU);
  819. }
  820. havesock:
  821. /*
  822. * Make socket non-blocking so that we don't hang forever if
  823. * target dies unexpectedly.
  824. */
  825. flags = fcntl(priv->sock, F_GETFL);
  826. if (flags >= 0) {
  827. flags |= O_NONBLOCK;
  828. if (fcntl(priv->sock, F_SETFL, flags) < 0) {
  829. wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
  830. strerror(errno));
  831. /* Not fatal, continue on.*/
  832. }
  833. }
  834. eloop_register_read_sock(priv->sock,
  835. wpa_supplicant_global_ctrl_iface_receive,
  836. global, priv);
  837. return priv;
  838. fail:
  839. if (priv->sock >= 0)
  840. close(priv->sock);
  841. os_free(priv);
  842. return NULL;
  843. }
  844. void
  845. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  846. {
  847. struct wpa_ctrl_dst *dst, *prev;
  848. if (priv->sock >= 0) {
  849. eloop_unregister_read_sock(priv->sock);
  850. close(priv->sock);
  851. }
  852. if (priv->global->params.ctrl_interface)
  853. unlink(priv->global->params.ctrl_interface);
  854. dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
  855. list)
  856. os_free(dst);
  857. os_free(priv);
  858. }