ctrl_iface_unix.c 18 KB

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