ctrl_iface_unix.c 23 KB

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