ctrl_iface_unix.c 28 KB

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