ctrl_iface_unix.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. if (priv->wpa_s->conf->ctrl_interface == NULL)
  519. goto free_dst;
  520. buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
  521. if (buf == NULL)
  522. goto free_dst;
  523. if (os_strncmp(buf, "DIR=", 4) == 0) {
  524. dir = buf + 4;
  525. gid_str = os_strstr(dir, " GROUP=");
  526. if (gid_str) {
  527. *gid_str = '\0';
  528. gid_str += 7;
  529. }
  530. } else
  531. dir = buf;
  532. if (rmdir(dir) < 0) {
  533. if (errno == ENOTEMPTY) {
  534. wpa_printf(MSG_DEBUG, "Control interface "
  535. "directory not empty - leaving it "
  536. "behind");
  537. } else {
  538. wpa_printf(MSG_ERROR,
  539. "rmdir[ctrl_interface=%s]: %s",
  540. dir, strerror(errno));
  541. }
  542. }
  543. os_free(buf);
  544. }
  545. free_dst:
  546. dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
  547. list)
  548. os_free(dst);
  549. os_free(priv);
  550. }
  551. /**
  552. * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
  553. * @ifname: Interface name for global control socket or %NULL
  554. * @sock: Local socket fd
  555. * @ctrl_dst: List of attached listeners
  556. * @level: Priority level of the message
  557. * @buf: Message data
  558. * @len: Message length
  559. *
  560. * Send a packet to all monitor programs attached to the control interface.
  561. */
  562. static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
  563. const char *ifname, int sock,
  564. struct dl_list *ctrl_dst,
  565. int level, const char *buf,
  566. size_t len,
  567. struct ctrl_iface_priv *priv,
  568. struct ctrl_iface_global_priv *gp)
  569. {
  570. struct wpa_ctrl_dst *dst, *next;
  571. char levelstr[10];
  572. int idx, res;
  573. struct msghdr msg;
  574. struct iovec io[5];
  575. if (sock < 0 || dl_list_empty(ctrl_dst))
  576. return;
  577. res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  578. if (res < 0 || (size_t) res >= sizeof(levelstr))
  579. return;
  580. idx = 0;
  581. if (ifname) {
  582. io[idx].iov_base = "IFNAME=";
  583. io[idx].iov_len = 7;
  584. idx++;
  585. io[idx].iov_base = (char *) ifname;
  586. io[idx].iov_len = os_strlen(ifname);
  587. idx++;
  588. io[idx].iov_base = " ";
  589. io[idx].iov_len = 1;
  590. idx++;
  591. }
  592. io[idx].iov_base = levelstr;
  593. io[idx].iov_len = os_strlen(levelstr);
  594. idx++;
  595. io[idx].iov_base = (char *) buf;
  596. io[idx].iov_len = len;
  597. idx++;
  598. os_memset(&msg, 0, sizeof(msg));
  599. msg.msg_iov = io;
  600. msg.msg_iovlen = idx;
  601. idx = -1;
  602. dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
  603. int _errno;
  604. idx++;
  605. if (level < dst->debug_level)
  606. continue;
  607. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  608. (u8 *) dst->addr.sun_path, dst->addrlen -
  609. offsetof(struct sockaddr_un, sun_path));
  610. msg.msg_name = (void *) &dst->addr;
  611. msg.msg_namelen = dst->addrlen;
  612. if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
  613. dst->errors = 0;
  614. idx++;
  615. continue;
  616. }
  617. _errno = errno;
  618. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: %d - %s",
  619. idx, errno, strerror(errno));
  620. dst->errors++;
  621. if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
  622. wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor that cannot receive messages");
  623. wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
  624. dst->addrlen);
  625. }
  626. if (_errno == ENOBUFS || _errno == EAGAIN) {
  627. /*
  628. * The socket send buffer could be full. This may happen
  629. * if client programs are not receiving their pending
  630. * messages. Close and reopen the socket as a workaround
  631. * to avoid getting stuck being unable to send any new
  632. * responses.
  633. */
  634. if (priv)
  635. sock = wpas_ctrl_iface_reinit(wpa_s, priv);
  636. else if (gp)
  637. sock = wpas_ctrl_iface_global_reinit(
  638. wpa_s->global, gp);
  639. else
  640. break;
  641. if (sock < 0) {
  642. wpa_dbg(wpa_s, MSG_DEBUG,
  643. "Failed to reinitialize ctrl_iface socket");
  644. break;
  645. }
  646. }
  647. }
  648. }
  649. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  650. {
  651. char buf[256];
  652. int res;
  653. struct sockaddr_un from;
  654. socklen_t fromlen = sizeof(from);
  655. for (;;) {
  656. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
  657. "attach", priv->wpa_s->ifname);
  658. eloop_wait_for_read_sock(priv->sock);
  659. res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
  660. (struct sockaddr *) &from, &fromlen);
  661. if (res < 0) {
  662. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  663. strerror(errno));
  664. continue;
  665. }
  666. buf[res] = '\0';
  667. if (os_strcmp(buf, "ATTACH") == 0) {
  668. /* handle ATTACH signal of first monitor interface */
  669. if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
  670. &from, fromlen)) {
  671. if (sendto(priv->sock, "OK\n", 3, 0,
  672. (struct sockaddr *) &from, fromlen) <
  673. 0) {
  674. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  675. strerror(errno));
  676. }
  677. /* OK to continue */
  678. return;
  679. } else {
  680. if (sendto(priv->sock, "FAIL\n", 5, 0,
  681. (struct sockaddr *) &from, fromlen) <
  682. 0) {
  683. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  684. strerror(errno));
  685. }
  686. }
  687. } else {
  688. /* return FAIL for all other signals */
  689. if (sendto(priv->sock, "FAIL\n", 5, 0,
  690. (struct sockaddr *) &from, fromlen) < 0) {
  691. wpa_printf(MSG_DEBUG,
  692. "ctrl_iface sendto failed: %s",
  693. strerror(errno));
  694. }
  695. }
  696. }
  697. }
  698. /* Global ctrl_iface */
  699. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  700. void *sock_ctx)
  701. {
  702. struct wpa_global *global = eloop_ctx;
  703. struct ctrl_iface_global_priv *priv = sock_ctx;
  704. char buf[256];
  705. int res;
  706. struct sockaddr_un from;
  707. socklen_t fromlen = sizeof(from);
  708. char *reply = NULL, *reply_buf = NULL;
  709. size_t reply_len;
  710. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  711. (struct sockaddr *) &from, &fromlen);
  712. if (res < 0) {
  713. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  714. strerror(errno));
  715. return;
  716. }
  717. buf[res] = '\0';
  718. if (os_strcmp(buf, "ATTACH") == 0) {
  719. if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
  720. fromlen))
  721. reply_len = 1;
  722. else
  723. reply_len = 2;
  724. } else if (os_strcmp(buf, "DETACH") == 0) {
  725. if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
  726. fromlen))
  727. reply_len = 1;
  728. else
  729. reply_len = 2;
  730. } else {
  731. reply_buf = wpa_supplicant_global_ctrl_iface_process(
  732. global, buf, &reply_len);
  733. reply = reply_buf;
  734. }
  735. if (!reply && reply_len == 1) {
  736. reply = "FAIL\n";
  737. reply_len = 5;
  738. } else if (!reply && reply_len == 2) {
  739. reply = "OK\n";
  740. reply_len = 3;
  741. }
  742. if (reply) {
  743. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  744. fromlen) < 0) {
  745. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  746. strerror(errno));
  747. }
  748. }
  749. os_free(reply_buf);
  750. }
  751. static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
  752. struct ctrl_iface_global_priv *priv)
  753. {
  754. struct sockaddr_un addr;
  755. const char *ctrl = global->params.ctrl_interface;
  756. int flags;
  757. wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
  758. #ifdef ANDROID
  759. if (os_strncmp(ctrl, "@android:", 9) == 0) {
  760. priv->sock = android_get_control_socket(ctrl + 9);
  761. if (priv->sock < 0) {
  762. wpa_printf(MSG_ERROR, "Failed to open Android control "
  763. "socket '%s'", ctrl + 9);
  764. goto fail;
  765. }
  766. wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
  767. ctrl + 9);
  768. goto havesock;
  769. }
  770. if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
  771. /*
  772. * Backwards compatibility - try to open an Android control
  773. * socket and if that fails, assume this was a UNIX domain
  774. * socket instead.
  775. */
  776. priv->sock = android_get_control_socket(ctrl);
  777. if (priv->sock >= 0) {
  778. wpa_printf(MSG_DEBUG,
  779. "Using Android control socket '%s'",
  780. ctrl);
  781. goto havesock;
  782. }
  783. }
  784. #endif /* ANDROID */
  785. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  786. if (priv->sock < 0) {
  787. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  788. goto fail;
  789. }
  790. os_memset(&addr, 0, sizeof(addr));
  791. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  792. addr.sun_len = sizeof(addr);
  793. #endif /* __FreeBSD__ */
  794. addr.sun_family = AF_UNIX;
  795. if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
  796. addr.sun_path[0] = '\0';
  797. os_strlcpy(addr.sun_path + 1, ctrl + 10,
  798. sizeof(addr.sun_path) - 1);
  799. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
  800. 0) {
  801. wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
  802. "bind(PF_UNIX;%s) failed: %s",
  803. ctrl, strerror(errno));
  804. goto fail;
  805. }
  806. wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
  807. ctrl + 10);
  808. goto havesock;
  809. }
  810. os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
  811. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  812. wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
  813. ctrl, strerror(errno));
  814. if (connect(priv->sock, (struct sockaddr *) &addr,
  815. sizeof(addr)) < 0) {
  816. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  817. " allow connections - assuming it was left"
  818. "over from forced program termination");
  819. if (unlink(ctrl) < 0) {
  820. wpa_printf(MSG_ERROR,
  821. "Could not unlink existing ctrl_iface socket '%s': %s",
  822. ctrl, strerror(errno));
  823. goto fail;
  824. }
  825. if (bind(priv->sock, (struct sockaddr *) &addr,
  826. sizeof(addr)) < 0) {
  827. wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
  828. ctrl, strerror(errno));
  829. goto fail;
  830. }
  831. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  832. "ctrl_iface socket '%s'",
  833. ctrl);
  834. } else {
  835. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  836. "be in use - cannot override it");
  837. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  838. "not used anymore",
  839. ctrl);
  840. goto fail;
  841. }
  842. }
  843. wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
  844. if (global->params.ctrl_interface_group) {
  845. char *gid_str = global->params.ctrl_interface_group;
  846. gid_t gid = 0;
  847. struct group *grp;
  848. char *endp;
  849. grp = getgrnam(gid_str);
  850. if (grp) {
  851. gid = grp->gr_gid;
  852. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  853. " (from group name '%s')",
  854. (int) gid, gid_str);
  855. } else {
  856. /* Group name not found - try to parse this as gid */
  857. gid = strtol(gid_str, &endp, 10);
  858. if (*gid_str == '\0' || *endp != '\0') {
  859. wpa_printf(MSG_ERROR, "CTRL: Invalid group "
  860. "'%s'", gid_str);
  861. goto fail;
  862. }
  863. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  864. (int) gid);
  865. }
  866. if (chown(ctrl, -1, gid) < 0) {
  867. wpa_printf(MSG_ERROR,
  868. "chown[global_ctrl_interface=%s,gid=%d]: %s",
  869. ctrl, (int) gid, strerror(errno));
  870. goto fail;
  871. }
  872. if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
  873. wpa_printf(MSG_ERROR,
  874. "chmod[global_ctrl_interface=%s]: %s",
  875. ctrl, strerror(errno));
  876. goto fail;
  877. }
  878. } else {
  879. chmod(ctrl, S_IRWXU);
  880. }
  881. havesock:
  882. /*
  883. * Make socket non-blocking so that we don't hang forever if
  884. * target dies unexpectedly.
  885. */
  886. flags = fcntl(priv->sock, F_GETFL);
  887. if (flags >= 0) {
  888. flags |= O_NONBLOCK;
  889. if (fcntl(priv->sock, F_SETFL, flags) < 0) {
  890. wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
  891. strerror(errno));
  892. /* Not fatal, continue on.*/
  893. }
  894. }
  895. eloop_register_read_sock(priv->sock,
  896. wpa_supplicant_global_ctrl_iface_receive,
  897. global, priv);
  898. return 0;
  899. fail:
  900. if (priv->sock >= 0) {
  901. close(priv->sock);
  902. priv->sock = -1;
  903. }
  904. return -1;
  905. }
  906. struct ctrl_iface_global_priv *
  907. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  908. {
  909. struct ctrl_iface_global_priv *priv;
  910. priv = os_zalloc(sizeof(*priv));
  911. if (priv == NULL)
  912. return NULL;
  913. dl_list_init(&priv->ctrl_dst);
  914. priv->global = global;
  915. priv->sock = -1;
  916. if (global->params.ctrl_interface == NULL)
  917. return priv;
  918. if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
  919. os_free(priv);
  920. return NULL;
  921. }
  922. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  923. return priv;
  924. }
  925. static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
  926. struct ctrl_iface_global_priv *priv)
  927. {
  928. int res;
  929. if (priv->sock <= 0)
  930. return -1;
  931. eloop_unregister_read_sock(priv->sock);
  932. close(priv->sock);
  933. priv->sock = -1;
  934. res = wpas_global_ctrl_iface_open_sock(global, priv);
  935. if (res < 0)
  936. return -1;
  937. return priv->sock;
  938. }
  939. void
  940. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  941. {
  942. struct wpa_ctrl_dst *dst, *prev;
  943. if (priv->sock >= 0) {
  944. eloop_unregister_read_sock(priv->sock);
  945. close(priv->sock);
  946. }
  947. if (priv->global->params.ctrl_interface)
  948. unlink(priv->global->params.ctrl_interface);
  949. dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
  950. list)
  951. os_free(dst);
  952. os_free(priv);
  953. }