ctrl_iface_unix.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /*
  2. * WPA Supplicant / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include <sys/un.h>
  10. #include <sys/stat.h>
  11. #include <grp.h>
  12. #include <stddef.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #ifdef ANDROID
  16. #include <cutils/sockets.h>
  17. #endif /* ANDROID */
  18. #include "utils/common.h"
  19. #include "utils/eloop.h"
  20. #include "utils/list.h"
  21. #include "eapol_supp/eapol_supp_sm.h"
  22. #include "config.h"
  23. #include "wpa_supplicant_i.h"
  24. #include "ctrl_iface.h"
  25. /* Per-interface ctrl_iface */
  26. /**
  27. * struct wpa_ctrl_dst - Internal data structure of control interface monitors
  28. *
  29. * This structure is used to store information about registered control
  30. * interface monitors into struct wpa_supplicant. This data is private to
  31. * ctrl_iface_unix.c and should not be touched directly from other files.
  32. */
  33. struct wpa_ctrl_dst {
  34. struct dl_list list;
  35. struct sockaddr_un addr;
  36. socklen_t addrlen;
  37. int debug_level;
  38. int errors;
  39. };
  40. struct ctrl_iface_priv {
  41. struct wpa_supplicant *wpa_s;
  42. int sock;
  43. struct dl_list ctrl_dst;
  44. };
  45. struct ctrl_iface_global_priv {
  46. struct wpa_global *global;
  47. int sock;
  48. struct dl_list ctrl_dst;
  49. };
  50. static void wpa_supplicant_ctrl_iface_send(const char *ifname, int sock,
  51. struct dl_list *ctrl_dst,
  52. int level, const char *buf,
  53. size_t len);
  54. static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
  55. struct sockaddr_un *from,
  56. socklen_t fromlen)
  57. {
  58. struct wpa_ctrl_dst *dst;
  59. dst = os_zalloc(sizeof(*dst));
  60. if (dst == NULL)
  61. return -1;
  62. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  63. dst->addrlen = fromlen;
  64. dst->debug_level = MSG_INFO;
  65. dl_list_add(ctrl_dst, &dst->list);
  66. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  67. (u8 *) from->sun_path,
  68. fromlen - offsetof(struct sockaddr_un, sun_path));
  69. return 0;
  70. }
  71. static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
  72. struct sockaddr_un *from,
  73. socklen_t fromlen)
  74. {
  75. struct wpa_ctrl_dst *dst;
  76. dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
  77. if (fromlen == dst->addrlen &&
  78. os_memcmp(from->sun_path, dst->addr.sun_path,
  79. fromlen - offsetof(struct sockaddr_un, sun_path))
  80. == 0) {
  81. dl_list_del(&dst->list);
  82. os_free(dst);
  83. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  84. (u8 *) from->sun_path,
  85. fromlen -
  86. offsetof(struct sockaddr_un, sun_path));
  87. return 0;
  88. }
  89. }
  90. return -1;
  91. }
  92. static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
  93. struct sockaddr_un *from,
  94. socklen_t fromlen,
  95. char *level)
  96. {
  97. struct wpa_ctrl_dst *dst;
  98. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  99. dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
  100. if (fromlen == dst->addrlen &&
  101. os_memcmp(from->sun_path, dst->addr.sun_path,
  102. fromlen - offsetof(struct sockaddr_un, sun_path))
  103. == 0) {
  104. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  105. "level", (u8 *) from->sun_path,
  106. fromlen -
  107. offsetof(struct sockaddr_un, sun_path));
  108. dst->debug_level = atoi(level);
  109. return 0;
  110. }
  111. }
  112. return -1;
  113. }
  114. static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
  115. void *sock_ctx)
  116. {
  117. struct wpa_supplicant *wpa_s = eloop_ctx;
  118. struct ctrl_iface_priv *priv = sock_ctx;
  119. char buf[4096];
  120. int res;
  121. struct sockaddr_un from;
  122. socklen_t fromlen = sizeof(from);
  123. char *reply = NULL;
  124. size_t reply_len = 0;
  125. int new_attached = 0;
  126. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  127. (struct sockaddr *) &from, &fromlen);
  128. if (res < 0) {
  129. perror("recvfrom(ctrl_iface)");
  130. return;
  131. }
  132. buf[res] = '\0';
  133. if (os_strcmp(buf, "ATTACH") == 0) {
  134. if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
  135. fromlen))
  136. reply_len = 1;
  137. else {
  138. new_attached = 1;
  139. reply_len = 2;
  140. }
  141. } else if (os_strcmp(buf, "DETACH") == 0) {
  142. if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
  143. fromlen))
  144. reply_len = 1;
  145. else
  146. reply_len = 2;
  147. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  148. if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
  149. buf + 6))
  150. reply_len = 1;
  151. else
  152. reply_len = 2;
  153. } else {
  154. reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
  155. &reply_len);
  156. }
  157. if (reply) {
  158. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  159. fromlen);
  160. os_free(reply);
  161. } else if (reply_len == 1) {
  162. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  163. fromlen);
  164. } else if (reply_len == 2) {
  165. sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
  166. fromlen);
  167. }
  168. if (new_attached)
  169. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  170. }
  171. static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
  172. {
  173. char *buf;
  174. size_t len;
  175. char *pbuf, *dir = NULL, *gid_str = NULL;
  176. int res;
  177. if (wpa_s->conf->ctrl_interface == NULL)
  178. return NULL;
  179. pbuf = os_strdup(wpa_s->conf->ctrl_interface);
  180. if (pbuf == NULL)
  181. return NULL;
  182. if (os_strncmp(pbuf, "DIR=", 4) == 0) {
  183. dir = pbuf + 4;
  184. gid_str = os_strstr(dir, " GROUP=");
  185. if (gid_str) {
  186. *gid_str = '\0';
  187. gid_str += 7;
  188. }
  189. } else
  190. dir = pbuf;
  191. len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
  192. buf = os_malloc(len);
  193. if (buf == NULL) {
  194. os_free(pbuf);
  195. return NULL;
  196. }
  197. res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
  198. if (res < 0 || (size_t) res >= len) {
  199. os_free(pbuf);
  200. os_free(buf);
  201. return NULL;
  202. }
  203. #ifdef __CYGWIN__
  204. {
  205. /* Windows/WinPcap uses interface names that are not suitable
  206. * as a file name - convert invalid chars to underscores */
  207. char *pos = buf;
  208. while (*pos) {
  209. if (*pos == '\\')
  210. *pos = '_';
  211. pos++;
  212. }
  213. }
  214. #endif /* __CYGWIN__ */
  215. os_free(pbuf);
  216. return buf;
  217. }
  218. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
  219. const char *txt, size_t len)
  220. {
  221. struct wpa_supplicant *wpa_s = ctx;
  222. if (wpa_s == NULL)
  223. return;
  224. if (wpa_s->global->ctrl_iface) {
  225. struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
  226. if (!dl_list_empty(&priv->ctrl_dst)) {
  227. wpa_supplicant_ctrl_iface_send(wpa_s->ifname,
  228. priv->sock,
  229. &priv->ctrl_dst,
  230. level, txt, len);
  231. }
  232. }
  233. if (wpa_s->ctrl_iface == NULL)
  234. return;
  235. wpa_supplicant_ctrl_iface_send(NULL, wpa_s->ctrl_iface->sock,
  236. &wpa_s->ctrl_iface->ctrl_dst,
  237. level, txt, len);
  238. }
  239. struct ctrl_iface_priv *
  240. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  241. {
  242. struct ctrl_iface_priv *priv;
  243. struct sockaddr_un addr;
  244. char *fname = NULL;
  245. gid_t gid = 0;
  246. int gid_set = 0;
  247. char *buf, *dir = NULL, *gid_str = NULL;
  248. struct group *grp;
  249. char *endp;
  250. int flags;
  251. priv = os_zalloc(sizeof(*priv));
  252. if (priv == NULL)
  253. return NULL;
  254. dl_list_init(&priv->ctrl_dst);
  255. priv->wpa_s = wpa_s;
  256. priv->sock = -1;
  257. if (wpa_s->conf->ctrl_interface == NULL)
  258. return priv;
  259. buf = os_strdup(wpa_s->conf->ctrl_interface);
  260. if (buf == NULL)
  261. goto fail;
  262. #ifdef ANDROID
  263. os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
  264. wpa_s->conf->ctrl_interface);
  265. priv->sock = android_get_control_socket(addr.sun_path);
  266. if (priv->sock >= 0)
  267. goto havesock;
  268. #endif /* ANDROID */
  269. if (os_strncmp(buf, "DIR=", 4) == 0) {
  270. dir = buf + 4;
  271. gid_str = os_strstr(dir, " GROUP=");
  272. if (gid_str) {
  273. *gid_str = '\0';
  274. gid_str += 7;
  275. }
  276. } else {
  277. dir = buf;
  278. gid_str = wpa_s->conf->ctrl_interface_group;
  279. }
  280. if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
  281. if (errno == EEXIST) {
  282. wpa_printf(MSG_DEBUG, "Using existing control "
  283. "interface directory.");
  284. } else {
  285. perror("mkdir[ctrl_interface]");
  286. goto fail;
  287. }
  288. }
  289. #ifdef ANDROID
  290. /*
  291. * wpa_supplicant is started from /init.*.rc on Android and that seems
  292. * to be using umask 0077 which would leave the control interface
  293. * directory without group access. This breaks things since Wi-Fi
  294. * framework assumes that this directory can be accessed by other
  295. * applications in the wifi group. Fix this by adding group access even
  296. * if umask value would prevent this.
  297. */
  298. if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
  299. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  300. strerror(errno));
  301. /* Try to continue anyway */
  302. }
  303. #endif /* ANDROID */
  304. if (gid_str) {
  305. grp = getgrnam(gid_str);
  306. if (grp) {
  307. gid = grp->gr_gid;
  308. gid_set = 1;
  309. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  310. " (from group name '%s')",
  311. (int) gid, gid_str);
  312. } else {
  313. /* Group name not found - try to parse this as gid */
  314. gid = strtol(gid_str, &endp, 10);
  315. if (*gid_str == '\0' || *endp != '\0') {
  316. wpa_printf(MSG_ERROR, "CTRL: Invalid group "
  317. "'%s'", gid_str);
  318. goto fail;
  319. }
  320. gid_set = 1;
  321. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  322. (int) gid);
  323. }
  324. }
  325. if (gid_set && chown(dir, -1, gid) < 0) {
  326. perror("chown[ctrl_interface]");
  327. goto fail;
  328. }
  329. /* Make sure the group can enter and read the directory */
  330. if (gid_set &&
  331. chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
  332. wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
  333. strerror(errno));
  334. goto fail;
  335. }
  336. if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
  337. sizeof(addr.sun_path)) {
  338. wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
  339. goto fail;
  340. }
  341. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  342. if (priv->sock < 0) {
  343. perror("socket(PF_UNIX)");
  344. goto fail;
  345. }
  346. os_memset(&addr, 0, sizeof(addr));
  347. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  348. addr.sun_len = sizeof(addr);
  349. #endif /* __FreeBSD__ */
  350. addr.sun_family = AF_UNIX;
  351. fname = wpa_supplicant_ctrl_iface_path(wpa_s);
  352. if (fname == NULL)
  353. goto fail;
  354. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  355. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  356. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  357. strerror(errno));
  358. if (connect(priv->sock, (struct sockaddr *) &addr,
  359. sizeof(addr)) < 0) {
  360. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  361. " allow connections - assuming it was left"
  362. "over from forced program termination");
  363. if (unlink(fname) < 0) {
  364. perror("unlink[ctrl_iface]");
  365. wpa_printf(MSG_ERROR, "Could not unlink "
  366. "existing ctrl_iface socket '%s'",
  367. fname);
  368. goto fail;
  369. }
  370. if (bind(priv->sock, (struct sockaddr *) &addr,
  371. sizeof(addr)) < 0) {
  372. perror("supp-ctrl-iface-init: bind(PF_UNIX)");
  373. goto fail;
  374. }
  375. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  376. "ctrl_iface socket '%s'", fname);
  377. } else {
  378. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  379. "be in use - cannot override it");
  380. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  381. "not used anymore", fname);
  382. os_free(fname);
  383. fname = NULL;
  384. goto fail;
  385. }
  386. }
  387. if (gid_set && chown(fname, -1, gid) < 0) {
  388. perror("chown[ctrl_interface/ifname]");
  389. goto fail;
  390. }
  391. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  392. perror("chmod[ctrl_interface/ifname]");
  393. goto fail;
  394. }
  395. os_free(fname);
  396. #ifdef ANDROID
  397. havesock:
  398. #endif /* ANDROID */
  399. /*
  400. * Make socket non-blocking so that we don't hang forever if
  401. * target dies unexpectedly.
  402. */
  403. flags = fcntl(priv->sock, F_GETFL);
  404. if (flags >= 0) {
  405. flags |= O_NONBLOCK;
  406. if (fcntl(priv->sock, F_SETFL, flags) < 0) {
  407. perror("fcntl(ctrl, O_NONBLOCK)");
  408. /* Not fatal, continue on.*/
  409. }
  410. }
  411. eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
  412. wpa_s, priv);
  413. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  414. os_free(buf);
  415. return priv;
  416. fail:
  417. if (priv->sock >= 0)
  418. close(priv->sock);
  419. os_free(priv);
  420. if (fname) {
  421. unlink(fname);
  422. os_free(fname);
  423. }
  424. os_free(buf);
  425. return NULL;
  426. }
  427. void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
  428. {
  429. struct wpa_ctrl_dst *dst, *prev;
  430. if (priv->sock > -1) {
  431. char *fname;
  432. char *buf, *dir = NULL, *gid_str = NULL;
  433. eloop_unregister_read_sock(priv->sock);
  434. if (!dl_list_empty(&priv->ctrl_dst)) {
  435. /*
  436. * Wait a second before closing the control socket if
  437. * there are any attached monitors in order to allow
  438. * them to receive any pending messages.
  439. */
  440. wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
  441. "monitors to receive messages");
  442. os_sleep(1, 0);
  443. }
  444. close(priv->sock);
  445. priv->sock = -1;
  446. fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
  447. if (fname) {
  448. unlink(fname);
  449. os_free(fname);
  450. }
  451. buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
  452. if (buf == NULL)
  453. goto free_dst;
  454. if (os_strncmp(buf, "DIR=", 4) == 0) {
  455. dir = buf + 4;
  456. gid_str = os_strstr(dir, " GROUP=");
  457. if (gid_str) {
  458. *gid_str = '\0';
  459. gid_str += 7;
  460. }
  461. } else
  462. dir = buf;
  463. if (rmdir(dir) < 0) {
  464. if (errno == ENOTEMPTY) {
  465. wpa_printf(MSG_DEBUG, "Control interface "
  466. "directory not empty - leaving it "
  467. "behind");
  468. } else {
  469. perror("rmdir[ctrl_interface]");
  470. }
  471. }
  472. os_free(buf);
  473. }
  474. free_dst:
  475. dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
  476. list)
  477. os_free(dst);
  478. os_free(priv);
  479. }
  480. /**
  481. * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
  482. * @ifname: Interface name for global control socket or %NULL
  483. * @sock: Local socket fd
  484. * @ctrl_dst: List of attached listeners
  485. * @level: Priority level of the message
  486. * @buf: Message data
  487. * @len: Message length
  488. *
  489. * Send a packet to all monitor programs attached to the control interface.
  490. */
  491. static void wpa_supplicant_ctrl_iface_send(const char *ifname, int sock,
  492. struct dl_list *ctrl_dst,
  493. int level, const char *buf,
  494. size_t len)
  495. {
  496. struct wpa_ctrl_dst *dst, *next;
  497. char levelstr[10];
  498. int idx, res;
  499. struct msghdr msg;
  500. struct iovec io[5];
  501. if (sock < 0 || dl_list_empty(ctrl_dst))
  502. return;
  503. res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  504. if (res < 0 || (size_t) res >= sizeof(levelstr))
  505. return;
  506. idx = 0;
  507. if (ifname) {
  508. io[idx].iov_base = "IFNAME=";
  509. io[idx].iov_len = 7;
  510. idx++;
  511. io[idx].iov_base = (char *) ifname;
  512. io[idx].iov_len = os_strlen(ifname);
  513. idx++;
  514. io[idx].iov_base = " ";
  515. io[idx].iov_len = 1;
  516. idx++;
  517. }
  518. io[idx].iov_base = levelstr;
  519. io[idx].iov_len = os_strlen(levelstr);
  520. idx++;
  521. io[idx].iov_base = (char *) buf;
  522. io[idx].iov_len = len;
  523. idx++;
  524. os_memset(&msg, 0, sizeof(msg));
  525. msg.msg_iov = io;
  526. msg.msg_iovlen = idx;
  527. idx = 0;
  528. dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
  529. if (level >= dst->debug_level) {
  530. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  531. (u8 *) dst->addr.sun_path, dst->addrlen -
  532. offsetof(struct sockaddr_un, sun_path));
  533. msg.msg_name = (void *) &dst->addr;
  534. msg.msg_namelen = dst->addrlen;
  535. if (sendmsg(sock, &msg, 0) < 0) {
  536. int _errno = errno;
  537. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  538. "%d - %s",
  539. idx, errno, strerror(errno));
  540. dst->errors++;
  541. if (dst->errors > 1000 ||
  542. (_errno != ENOBUFS && dst->errors > 10) ||
  543. _errno == ENOENT) {
  544. wpa_supplicant_ctrl_iface_detach(
  545. ctrl_dst, &dst->addr,
  546. dst->addrlen);
  547. }
  548. } else
  549. dst->errors = 0;
  550. }
  551. idx++;
  552. }
  553. }
  554. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  555. {
  556. char buf[256];
  557. int res;
  558. struct sockaddr_un from;
  559. socklen_t fromlen = sizeof(from);
  560. for (;;) {
  561. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
  562. "attach", priv->wpa_s->ifname);
  563. eloop_wait_for_read_sock(priv->sock);
  564. res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
  565. (struct sockaddr *) &from, &fromlen);
  566. if (res < 0) {
  567. perror("recvfrom(ctrl_iface)");
  568. continue;
  569. }
  570. buf[res] = '\0';
  571. if (os_strcmp(buf, "ATTACH") == 0) {
  572. /* handle ATTACH signal of first monitor interface */
  573. if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
  574. &from, fromlen)) {
  575. sendto(priv->sock, "OK\n", 3, 0,
  576. (struct sockaddr *) &from, fromlen);
  577. /* OK to continue */
  578. return;
  579. } else {
  580. sendto(priv->sock, "FAIL\n", 5, 0,
  581. (struct sockaddr *) &from, fromlen);
  582. }
  583. } else {
  584. /* return FAIL for all other signals */
  585. sendto(priv->sock, "FAIL\n", 5, 0,
  586. (struct sockaddr *) &from, fromlen);
  587. }
  588. }
  589. }
  590. /* Global ctrl_iface */
  591. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  592. void *sock_ctx)
  593. {
  594. struct wpa_global *global = eloop_ctx;
  595. struct ctrl_iface_global_priv *priv = sock_ctx;
  596. char buf[256];
  597. int res;
  598. struct sockaddr_un from;
  599. socklen_t fromlen = sizeof(from);
  600. char *reply = NULL;
  601. size_t reply_len;
  602. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  603. (struct sockaddr *) &from, &fromlen);
  604. if (res < 0) {
  605. perror("recvfrom(ctrl_iface)");
  606. return;
  607. }
  608. buf[res] = '\0';
  609. if (os_strcmp(buf, "ATTACH") == 0) {
  610. if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
  611. fromlen))
  612. reply_len = 1;
  613. else
  614. reply_len = 2;
  615. } else if (os_strcmp(buf, "DETACH") == 0) {
  616. if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
  617. fromlen))
  618. reply_len = 1;
  619. else
  620. reply_len = 2;
  621. } else {
  622. reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
  623. &reply_len);
  624. }
  625. if (reply) {
  626. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  627. fromlen);
  628. os_free(reply);
  629. } else if (reply_len == 1) {
  630. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  631. fromlen);
  632. } else if (reply_len == 2) {
  633. sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from, fromlen);
  634. }
  635. }
  636. struct ctrl_iface_global_priv *
  637. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  638. {
  639. struct ctrl_iface_global_priv *priv;
  640. struct sockaddr_un addr;
  641. const char *ctrl = global->params.ctrl_interface;
  642. priv = os_zalloc(sizeof(*priv));
  643. if (priv == NULL)
  644. return NULL;
  645. dl_list_init(&priv->ctrl_dst);
  646. priv->global = global;
  647. priv->sock = -1;
  648. if (ctrl == NULL)
  649. return priv;
  650. wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
  651. #ifdef ANDROID
  652. if (os_strncmp(ctrl, "@android:", 9) == 0) {
  653. priv->sock = android_get_control_socket(ctrl + 9);
  654. if (priv->sock < 0) {
  655. wpa_printf(MSG_ERROR, "Failed to open Android control "
  656. "socket '%s'", ctrl + 9);
  657. goto fail;
  658. }
  659. wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
  660. ctrl + 9);
  661. goto havesock;
  662. }
  663. if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
  664. /*
  665. * Backwards compatibility - try to open an Android control
  666. * socket and if that fails, assume this was a UNIX domain
  667. * socket instead.
  668. */
  669. priv->sock = android_get_control_socket(ctrl);
  670. if (priv->sock >= 0) {
  671. wpa_printf(MSG_DEBUG,
  672. "Using Android control socket '%s'",
  673. ctrl);
  674. goto havesock;
  675. }
  676. }
  677. #endif /* ANDROID */
  678. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  679. if (priv->sock < 0) {
  680. perror("socket(PF_UNIX)");
  681. goto fail;
  682. }
  683. os_memset(&addr, 0, sizeof(addr));
  684. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  685. addr.sun_len = sizeof(addr);
  686. #endif /* __FreeBSD__ */
  687. addr.sun_family = AF_UNIX;
  688. if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
  689. addr.sun_path[0] = '\0';
  690. os_strlcpy(addr.sun_path + 1, ctrl + 10,
  691. sizeof(addr.sun_path) - 1);
  692. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
  693. 0) {
  694. wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
  695. "bind(PF_UNIX) failed: %s", strerror(errno));
  696. goto fail;
  697. }
  698. wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
  699. ctrl + 10);
  700. goto havesock;
  701. }
  702. os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
  703. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  704. perror("supp-global-ctrl-iface-init (will try fixup): "
  705. "bind(PF_UNIX)");
  706. if (connect(priv->sock, (struct sockaddr *) &addr,
  707. sizeof(addr)) < 0) {
  708. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  709. " allow connections - assuming it was left"
  710. "over from forced program termination");
  711. if (unlink(ctrl) < 0) {
  712. perror("unlink[ctrl_iface]");
  713. wpa_printf(MSG_ERROR, "Could not unlink "
  714. "existing ctrl_iface socket '%s'",
  715. ctrl);
  716. goto fail;
  717. }
  718. if (bind(priv->sock, (struct sockaddr *) &addr,
  719. sizeof(addr)) < 0) {
  720. perror("supp-glb-iface-init: bind(PF_UNIX)");
  721. goto fail;
  722. }
  723. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  724. "ctrl_iface socket '%s'",
  725. ctrl);
  726. } else {
  727. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  728. "be in use - cannot override it");
  729. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  730. "not used anymore",
  731. ctrl);
  732. goto fail;
  733. }
  734. }
  735. wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
  736. if (global->params.ctrl_interface_group) {
  737. char *gid_str = global->params.ctrl_interface_group;
  738. gid_t gid = 0;
  739. struct group *grp;
  740. char *endp;
  741. grp = getgrnam(gid_str);
  742. if (grp) {
  743. gid = grp->gr_gid;
  744. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  745. " (from group name '%s')",
  746. (int) gid, gid_str);
  747. } else {
  748. /* Group name not found - try to parse this as gid */
  749. gid = strtol(gid_str, &endp, 10);
  750. if (*gid_str == '\0' || *endp != '\0') {
  751. wpa_printf(MSG_ERROR, "CTRL: Invalid group "
  752. "'%s'", gid_str);
  753. goto fail;
  754. }
  755. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  756. (int) gid);
  757. }
  758. if (chown(ctrl, -1, gid) < 0) {
  759. perror("chown[global_ctrl_interface/ifname]");
  760. goto fail;
  761. }
  762. if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
  763. perror("chmod[global_ctrl_interface/ifname]");
  764. goto fail;
  765. }
  766. } else {
  767. chmod(ctrl, S_IRWXU);
  768. }
  769. havesock:
  770. eloop_register_read_sock(priv->sock,
  771. wpa_supplicant_global_ctrl_iface_receive,
  772. global, priv);
  773. return priv;
  774. fail:
  775. if (priv->sock >= 0)
  776. close(priv->sock);
  777. os_free(priv);
  778. return NULL;
  779. }
  780. void
  781. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  782. {
  783. struct wpa_ctrl_dst *dst, *prev;
  784. if (priv->sock >= 0) {
  785. eloop_unregister_read_sock(priv->sock);
  786. close(priv->sock);
  787. }
  788. if (priv->global->params.ctrl_interface)
  789. unlink(priv->global->params.ctrl_interface);
  790. dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
  791. list)
  792. os_free(dst);
  793. os_free(priv);
  794. }