ctrl_iface_unix.c 26 KB

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