You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ctrl_iface_unix.c 18KB

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