ctrl_iface_unix.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * WPA Supplicant / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2009, 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. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  46. int level, const char *buf,
  47. size_t len);
  48. static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
  49. struct sockaddr_un *from,
  50. socklen_t fromlen)
  51. {
  52. struct wpa_ctrl_dst *dst;
  53. dst = os_zalloc(sizeof(*dst));
  54. if (dst == NULL)
  55. return -1;
  56. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  57. dst->addrlen = fromlen;
  58. dst->debug_level = MSG_INFO;
  59. dl_list_add(&priv->ctrl_dst, &dst->list);
  60. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  61. (u8 *) from->sun_path,
  62. fromlen - offsetof(struct sockaddr_un, sun_path));
  63. return 0;
  64. }
  65. static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
  66. struct sockaddr_un *from,
  67. socklen_t fromlen)
  68. {
  69. struct wpa_ctrl_dst *dst;
  70. dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
  71. if (fromlen == dst->addrlen &&
  72. os_memcmp(from->sun_path, dst->addr.sun_path,
  73. fromlen - offsetof(struct sockaddr_un, sun_path))
  74. == 0) {
  75. dl_list_del(&dst->list);
  76. os_free(dst);
  77. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  78. (u8 *) from->sun_path,
  79. fromlen -
  80. offsetof(struct sockaddr_un, sun_path));
  81. return 0;
  82. }
  83. }
  84. return -1;
  85. }
  86. static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
  87. struct sockaddr_un *from,
  88. socklen_t fromlen,
  89. char *level)
  90. {
  91. struct wpa_ctrl_dst *dst;
  92. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  93. dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
  94. if (fromlen == dst->addrlen &&
  95. os_memcmp(from->sun_path, dst->addr.sun_path,
  96. fromlen - offsetof(struct sockaddr_un, sun_path))
  97. == 0) {
  98. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  99. "level", (u8 *) from->sun_path,
  100. fromlen -
  101. offsetof(struct sockaddr_un, sun_path));
  102. dst->debug_level = atoi(level);
  103. return 0;
  104. }
  105. }
  106. return -1;
  107. }
  108. static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
  109. void *sock_ctx)
  110. {
  111. struct wpa_supplicant *wpa_s = eloop_ctx;
  112. struct ctrl_iface_priv *priv = sock_ctx;
  113. char buf[4096];
  114. int res;
  115. struct sockaddr_un from;
  116. socklen_t fromlen = sizeof(from);
  117. char *reply = NULL;
  118. size_t reply_len = 0;
  119. int new_attached = 0;
  120. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  121. (struct sockaddr *) &from, &fromlen);
  122. if (res < 0) {
  123. perror("recvfrom(ctrl_iface)");
  124. return;
  125. }
  126. buf[res] = '\0';
  127. if (os_strcmp(buf, "ATTACH") == 0) {
  128. if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
  129. reply_len = 1;
  130. else {
  131. new_attached = 1;
  132. reply_len = 2;
  133. }
  134. } else if (os_strcmp(buf, "DETACH") == 0) {
  135. if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
  136. reply_len = 1;
  137. else
  138. reply_len = 2;
  139. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  140. if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
  141. buf + 6))
  142. reply_len = 1;
  143. else
  144. reply_len = 2;
  145. } else {
  146. reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
  147. &reply_len);
  148. }
  149. if (reply) {
  150. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  151. fromlen);
  152. os_free(reply);
  153. } else if (reply_len == 1) {
  154. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  155. fromlen);
  156. } else if (reply_len == 2) {
  157. sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
  158. fromlen);
  159. }
  160. if (new_attached)
  161. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  162. }
  163. static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
  164. {
  165. char *buf;
  166. size_t len;
  167. char *pbuf, *dir = NULL, *gid_str = NULL;
  168. int res;
  169. if (wpa_s->conf->ctrl_interface == NULL)
  170. return NULL;
  171. pbuf = os_strdup(wpa_s->conf->ctrl_interface);
  172. if (pbuf == NULL)
  173. return NULL;
  174. if (os_strncmp(pbuf, "DIR=", 4) == 0) {
  175. dir = pbuf + 4;
  176. gid_str = os_strstr(dir, " GROUP=");
  177. if (gid_str) {
  178. *gid_str = '\0';
  179. gid_str += 7;
  180. }
  181. } else
  182. dir = pbuf;
  183. len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
  184. buf = os_malloc(len);
  185. if (buf == NULL) {
  186. os_free(pbuf);
  187. return NULL;
  188. }
  189. res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
  190. if (res < 0 || (size_t) res >= len) {
  191. os_free(pbuf);
  192. os_free(buf);
  193. return NULL;
  194. }
  195. #ifdef __CYGWIN__
  196. {
  197. /* Windows/WinPcap uses interface names that are not suitable
  198. * as a file name - convert invalid chars to underscores */
  199. char *pos = buf;
  200. while (*pos) {
  201. if (*pos == '\\')
  202. *pos = '_';
  203. pos++;
  204. }
  205. }
  206. #endif /* __CYGWIN__ */
  207. os_free(pbuf);
  208. return buf;
  209. }
  210. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
  211. const char *txt, size_t len)
  212. {
  213. struct wpa_supplicant *wpa_s = ctx;
  214. if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
  215. return;
  216. wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
  217. }
  218. struct ctrl_iface_priv *
  219. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  220. {
  221. struct ctrl_iface_priv *priv;
  222. struct sockaddr_un addr;
  223. char *fname = NULL;
  224. gid_t gid = 0;
  225. int gid_set = 0;
  226. char *buf, *dir = NULL, *gid_str = NULL;
  227. struct group *grp;
  228. char *endp;
  229. int flags;
  230. priv = os_zalloc(sizeof(*priv));
  231. if (priv == NULL)
  232. return NULL;
  233. dl_list_init(&priv->ctrl_dst);
  234. priv->wpa_s = wpa_s;
  235. priv->sock = -1;
  236. if (wpa_s->conf->ctrl_interface == NULL)
  237. return priv;
  238. buf = os_strdup(wpa_s->conf->ctrl_interface);
  239. if (buf == NULL)
  240. goto fail;
  241. #ifdef ANDROID
  242. os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
  243. wpa_s->conf->ctrl_interface);
  244. priv->sock = android_get_control_socket(addr.sun_path);
  245. if (priv->sock >= 0)
  246. goto havesock;
  247. #endif /* ANDROID */
  248. if (os_strncmp(buf, "DIR=", 4) == 0) {
  249. dir = buf + 4;
  250. gid_str = os_strstr(dir, " GROUP=");
  251. if (gid_str) {
  252. *gid_str = '\0';
  253. gid_str += 7;
  254. }
  255. } else {
  256. dir = buf;
  257. gid_str = wpa_s->conf->ctrl_interface_group;
  258. }
  259. if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
  260. if (errno == EEXIST) {
  261. wpa_printf(MSG_DEBUG, "Using existing control "
  262. "interface directory.");
  263. } else {
  264. perror("mkdir[ctrl_interface]");
  265. goto fail;
  266. }
  267. }
  268. if (gid_str) {
  269. grp = getgrnam(gid_str);
  270. if (grp) {
  271. gid = grp->gr_gid;
  272. gid_set = 1;
  273. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  274. " (from group name '%s')",
  275. (int) gid, gid_str);
  276. } else {
  277. /* Group name not found - try to parse this as gid */
  278. gid = strtol(gid_str, &endp, 10);
  279. if (*gid_str == '\0' || *endp != '\0') {
  280. wpa_printf(MSG_ERROR, "CTRL: Invalid group "
  281. "'%s'", gid_str);
  282. goto fail;
  283. }
  284. gid_set = 1;
  285. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  286. (int) gid);
  287. }
  288. }
  289. if (gid_set && chown(dir, -1, gid) < 0) {
  290. perror("chown[ctrl_interface]");
  291. goto fail;
  292. }
  293. /* Make sure the group can enter and read the directory */
  294. if (gid_set &&
  295. chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
  296. wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
  297. strerror(errno));
  298. goto fail;
  299. }
  300. if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
  301. sizeof(addr.sun_path)) {
  302. wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
  303. goto fail;
  304. }
  305. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  306. if (priv->sock < 0) {
  307. perror("socket(PF_UNIX)");
  308. goto fail;
  309. }
  310. os_memset(&addr, 0, sizeof(addr));
  311. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  312. addr.sun_len = sizeof(addr);
  313. #endif /* __FreeBSD__ */
  314. addr.sun_family = AF_UNIX;
  315. fname = wpa_supplicant_ctrl_iface_path(wpa_s);
  316. if (fname == NULL)
  317. goto fail;
  318. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  319. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  320. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  321. strerror(errno));
  322. if (connect(priv->sock, (struct sockaddr *) &addr,
  323. sizeof(addr)) < 0) {
  324. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  325. " allow connections - assuming it was left"
  326. "over from forced program termination");
  327. if (unlink(fname) < 0) {
  328. perror("unlink[ctrl_iface]");
  329. wpa_printf(MSG_ERROR, "Could not unlink "
  330. "existing ctrl_iface socket '%s'",
  331. fname);
  332. goto fail;
  333. }
  334. if (bind(priv->sock, (struct sockaddr *) &addr,
  335. sizeof(addr)) < 0) {
  336. perror("supp-ctrl-iface-init: bind(PF_UNIX)");
  337. goto fail;
  338. }
  339. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  340. "ctrl_iface socket '%s'", fname);
  341. } else {
  342. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  343. "be in use - cannot override it");
  344. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  345. "not used anymore", fname);
  346. os_free(fname);
  347. fname = NULL;
  348. goto fail;
  349. }
  350. }
  351. if (gid_set && chown(fname, -1, gid) < 0) {
  352. perror("chown[ctrl_interface/ifname]");
  353. goto fail;
  354. }
  355. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  356. perror("chmod[ctrl_interface/ifname]");
  357. goto fail;
  358. }
  359. os_free(fname);
  360. #ifdef ANDROID
  361. havesock:
  362. #endif /* ANDROID */
  363. /*
  364. * Make socket non-blocking so that we don't hang forever if
  365. * target dies unexpectedly.
  366. */
  367. flags = fcntl(priv->sock, F_GETFL);
  368. if (flags >= 0) {
  369. flags |= O_NONBLOCK;
  370. if (fcntl(priv->sock, F_SETFL, flags) < 0) {
  371. perror("fcntl(ctrl, O_NONBLOCK)");
  372. /* Not fatal, continue on.*/
  373. }
  374. }
  375. eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
  376. wpa_s, priv);
  377. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  378. os_free(buf);
  379. return priv;
  380. fail:
  381. if (priv->sock >= 0)
  382. close(priv->sock);
  383. os_free(priv);
  384. if (fname) {
  385. unlink(fname);
  386. os_free(fname);
  387. }
  388. os_free(buf);
  389. return NULL;
  390. }
  391. void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
  392. {
  393. struct wpa_ctrl_dst *dst, *prev;
  394. if (priv->sock > -1) {
  395. char *fname;
  396. char *buf, *dir = NULL, *gid_str = NULL;
  397. eloop_unregister_read_sock(priv->sock);
  398. if (!dl_list_empty(&priv->ctrl_dst)) {
  399. /*
  400. * Wait a second before closing the control socket if
  401. * there are any attached monitors in order to allow
  402. * them to receive any pending messages.
  403. */
  404. wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
  405. "monitors to receive messages");
  406. os_sleep(1, 0);
  407. }
  408. close(priv->sock);
  409. priv->sock = -1;
  410. fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
  411. if (fname) {
  412. unlink(fname);
  413. os_free(fname);
  414. }
  415. buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
  416. if (buf == NULL)
  417. goto free_dst;
  418. if (os_strncmp(buf, "DIR=", 4) == 0) {
  419. dir = buf + 4;
  420. gid_str = os_strstr(dir, " GROUP=");
  421. if (gid_str) {
  422. *gid_str = '\0';
  423. gid_str += 7;
  424. }
  425. } else
  426. dir = buf;
  427. if (rmdir(dir) < 0) {
  428. if (errno == ENOTEMPTY) {
  429. wpa_printf(MSG_DEBUG, "Control interface "
  430. "directory not empty - leaving it "
  431. "behind");
  432. } else {
  433. perror("rmdir[ctrl_interface]");
  434. }
  435. }
  436. os_free(buf);
  437. }
  438. free_dst:
  439. dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
  440. list)
  441. os_free(dst);
  442. os_free(priv);
  443. }
  444. /**
  445. * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
  446. * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
  447. * @level: Priority level of the message
  448. * @buf: Message data
  449. * @len: Message length
  450. *
  451. * Send a packet to all monitor programs attached to the control interface.
  452. */
  453. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  454. int level, const char *buf,
  455. size_t len)
  456. {
  457. struct wpa_ctrl_dst *dst, *next;
  458. char levelstr[10];
  459. int idx, res;
  460. struct msghdr msg;
  461. struct iovec io[2];
  462. if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
  463. return;
  464. res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  465. if (res < 0 || (size_t) res >= sizeof(levelstr))
  466. return;
  467. io[0].iov_base = levelstr;
  468. io[0].iov_len = os_strlen(levelstr);
  469. io[1].iov_base = (char *) buf;
  470. io[1].iov_len = len;
  471. os_memset(&msg, 0, sizeof(msg));
  472. msg.msg_iov = io;
  473. msg.msg_iovlen = 2;
  474. idx = 0;
  475. dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
  476. list) {
  477. if (level >= dst->debug_level) {
  478. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  479. (u8 *) dst->addr.sun_path, dst->addrlen -
  480. offsetof(struct sockaddr_un, sun_path));
  481. msg.msg_name = (void *) &dst->addr;
  482. msg.msg_namelen = dst->addrlen;
  483. if (sendmsg(priv->sock, &msg, 0) < 0) {
  484. int _errno = errno;
  485. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  486. "%d - %s",
  487. idx, errno, strerror(errno));
  488. dst->errors++;
  489. if (dst->errors > 1000 ||
  490. (_errno != ENOBUFS && dst->errors > 10) ||
  491. _errno == ENOENT) {
  492. wpa_supplicant_ctrl_iface_detach(
  493. priv, &dst->addr,
  494. dst->addrlen);
  495. }
  496. } else
  497. dst->errors = 0;
  498. }
  499. idx++;
  500. }
  501. }
  502. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  503. {
  504. char buf[256];
  505. int res;
  506. struct sockaddr_un from;
  507. socklen_t fromlen = sizeof(from);
  508. for (;;) {
  509. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
  510. "attach", priv->wpa_s->ifname);
  511. eloop_wait_for_read_sock(priv->sock);
  512. res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
  513. (struct sockaddr *) &from, &fromlen);
  514. if (res < 0) {
  515. perror("recvfrom(ctrl_iface)");
  516. continue;
  517. }
  518. buf[res] = '\0';
  519. if (os_strcmp(buf, "ATTACH") == 0) {
  520. /* handle ATTACH signal of first monitor interface */
  521. if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
  522. fromlen)) {
  523. sendto(priv->sock, "OK\n", 3, 0,
  524. (struct sockaddr *) &from, fromlen);
  525. /* OK to continue */
  526. return;
  527. } else {
  528. sendto(priv->sock, "FAIL\n", 5, 0,
  529. (struct sockaddr *) &from, fromlen);
  530. }
  531. } else {
  532. /* return FAIL for all other signals */
  533. sendto(priv->sock, "FAIL\n", 5, 0,
  534. (struct sockaddr *) &from, fromlen);
  535. }
  536. }
  537. }
  538. /* Global ctrl_iface */
  539. struct ctrl_iface_global_priv {
  540. struct wpa_global *global;
  541. int sock;
  542. };
  543. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  544. void *sock_ctx)
  545. {
  546. struct wpa_global *global = eloop_ctx;
  547. char buf[256];
  548. int res;
  549. struct sockaddr_un from;
  550. socklen_t fromlen = sizeof(from);
  551. char *reply;
  552. size_t reply_len;
  553. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  554. (struct sockaddr *) &from, &fromlen);
  555. if (res < 0) {
  556. perror("recvfrom(ctrl_iface)");
  557. return;
  558. }
  559. buf[res] = '\0';
  560. reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
  561. &reply_len);
  562. if (reply) {
  563. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  564. fromlen);
  565. os_free(reply);
  566. } else if (reply_len) {
  567. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  568. fromlen);
  569. }
  570. }
  571. struct ctrl_iface_global_priv *
  572. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  573. {
  574. struct ctrl_iface_global_priv *priv;
  575. struct sockaddr_un addr;
  576. priv = os_zalloc(sizeof(*priv));
  577. if (priv == NULL)
  578. return NULL;
  579. priv->global = global;
  580. priv->sock = -1;
  581. if (global->params.ctrl_interface == NULL)
  582. return priv;
  583. #ifdef ANDROID
  584. priv->sock = android_get_control_socket(global->params.ctrl_interface);
  585. if (priv->sock >= 0)
  586. goto havesock;
  587. #endif /* ANDROID */
  588. wpa_printf(MSG_DEBUG, "Global control interface '%s'",
  589. global->params.ctrl_interface);
  590. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  591. if (priv->sock < 0) {
  592. perror("socket(PF_UNIX)");
  593. goto fail;
  594. }
  595. os_memset(&addr, 0, sizeof(addr));
  596. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  597. addr.sun_len = sizeof(addr);
  598. #endif /* __FreeBSD__ */
  599. addr.sun_family = AF_UNIX;
  600. os_strlcpy(addr.sun_path, global->params.ctrl_interface,
  601. sizeof(addr.sun_path));
  602. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  603. perror("supp-global-ctrl-iface-init (will try fixup): "
  604. "bind(PF_UNIX)");
  605. if (connect(priv->sock, (struct sockaddr *) &addr,
  606. sizeof(addr)) < 0) {
  607. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  608. " allow connections - assuming it was left"
  609. "over from forced program termination");
  610. if (unlink(global->params.ctrl_interface) < 0) {
  611. perror("unlink[ctrl_iface]");
  612. wpa_printf(MSG_ERROR, "Could not unlink "
  613. "existing ctrl_iface socket '%s'",
  614. global->params.ctrl_interface);
  615. goto fail;
  616. }
  617. if (bind(priv->sock, (struct sockaddr *) &addr,
  618. sizeof(addr)) < 0) {
  619. perror("supp-glb-iface-init: bind(PF_UNIX)");
  620. goto fail;
  621. }
  622. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  623. "ctrl_iface socket '%s'",
  624. global->params.ctrl_interface);
  625. } else {
  626. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  627. "be in use - cannot override it");
  628. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  629. "not used anymore",
  630. global->params.ctrl_interface);
  631. goto fail;
  632. }
  633. }
  634. #ifdef ANDROID
  635. havesock:
  636. #endif /* ANDROID */
  637. eloop_register_read_sock(priv->sock,
  638. wpa_supplicant_global_ctrl_iface_receive,
  639. global, NULL);
  640. return priv;
  641. fail:
  642. if (priv->sock >= 0)
  643. close(priv->sock);
  644. os_free(priv);
  645. return NULL;
  646. }
  647. void
  648. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  649. {
  650. if (priv->sock >= 0) {
  651. eloop_unregister_read_sock(priv->sock);
  652. close(priv->sock);
  653. }
  654. if (priv->global->params.ctrl_interface)
  655. unlink(priv->global->params.ctrl_interface);
  656. os_free(priv);
  657. }