ctrl_iface_unix.c 28 KB

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