ctrl_iface_unix.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * WPA Supplicant / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2005, 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. #include "common.h"
  20. #include "eloop.h"
  21. #include "config.h"
  22. #include "eapol_supp/eapol_supp_sm.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 wpa_ctrl_dst *next;
  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 wpa_ctrl_dst *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. dst->next = priv->ctrl_dst;
  60. priv->ctrl_dst = dst;
  61. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  62. (u8 *) from->sun_path,
  63. fromlen - offsetof(struct sockaddr_un, sun_path));
  64. return 0;
  65. }
  66. static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
  67. struct sockaddr_un *from,
  68. socklen_t fromlen)
  69. {
  70. struct wpa_ctrl_dst *dst, *prev = NULL;
  71. dst = priv->ctrl_dst;
  72. while (dst) {
  73. if (fromlen == dst->addrlen &&
  74. os_memcmp(from->sun_path, dst->addr.sun_path,
  75. fromlen - offsetof(struct sockaddr_un, sun_path))
  76. == 0) {
  77. if (prev == NULL)
  78. priv->ctrl_dst = dst->next;
  79. else
  80. prev->next = dst->next;
  81. os_free(dst);
  82. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  83. (u8 *) from->sun_path,
  84. fromlen -
  85. offsetof(struct sockaddr_un, sun_path));
  86. return 0;
  87. }
  88. prev = dst;
  89. dst = dst->next;
  90. }
  91. return -1;
  92. }
  93. static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
  94. struct sockaddr_un *from,
  95. socklen_t fromlen,
  96. char *level)
  97. {
  98. struct wpa_ctrl_dst *dst;
  99. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  100. dst = priv->ctrl_dst;
  101. while (dst) {
  102. if (fromlen == dst->addrlen &&
  103. os_memcmp(from->sun_path, dst->addr.sun_path,
  104. fromlen - offsetof(struct sockaddr_un, sun_path))
  105. == 0) {
  106. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  107. "level", (u8 *) from->sun_path,
  108. fromlen -
  109. offsetof(struct sockaddr_un, sun_path));
  110. dst->debug_level = atoi(level);
  111. return 0;
  112. }
  113. dst = dst->next;
  114. }
  115. return -1;
  116. }
  117. static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
  118. void *sock_ctx)
  119. {
  120. struct wpa_supplicant *wpa_s = eloop_ctx;
  121. struct ctrl_iface_priv *priv = sock_ctx;
  122. char buf[256];
  123. int res;
  124. struct sockaddr_un from;
  125. socklen_t fromlen = sizeof(from);
  126. char *reply = NULL;
  127. size_t reply_len = 0;
  128. int new_attached = 0;
  129. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  130. (struct sockaddr *) &from, &fromlen);
  131. if (res < 0) {
  132. perror("recvfrom(ctrl_iface)");
  133. return;
  134. }
  135. buf[res] = '\0';
  136. if (os_strcmp(buf, "ATTACH") == 0) {
  137. if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
  138. reply_len = 1;
  139. else {
  140. new_attached = 1;
  141. reply_len = 2;
  142. }
  143. } else if (os_strcmp(buf, "DETACH") == 0) {
  144. if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
  145. reply_len = 1;
  146. else
  147. reply_len = 2;
  148. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  149. if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
  150. buf + 6))
  151. reply_len = 1;
  152. else
  153. reply_len = 2;
  154. } else {
  155. reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
  156. &reply_len);
  157. }
  158. if (reply) {
  159. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  160. fromlen);
  161. os_free(reply);
  162. } else if (reply_len == 1) {
  163. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  164. fromlen);
  165. } else if (reply_len == 2) {
  166. sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
  167. fromlen);
  168. }
  169. if (new_attached)
  170. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  171. }
  172. static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
  173. {
  174. char *buf;
  175. size_t len;
  176. char *pbuf, *dir = NULL, *gid_str = NULL;
  177. int res;
  178. if (wpa_s->conf->ctrl_interface == NULL)
  179. return NULL;
  180. pbuf = os_strdup(wpa_s->conf->ctrl_interface);
  181. if (pbuf == NULL)
  182. return NULL;
  183. if (os_strncmp(pbuf, "DIR=", 4) == 0) {
  184. dir = pbuf + 4;
  185. gid_str = os_strstr(dir, " GROUP=");
  186. if (gid_str) {
  187. *gid_str = '\0';
  188. gid_str += 7;
  189. }
  190. } else
  191. dir = pbuf;
  192. len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
  193. buf = os_malloc(len);
  194. if (buf == NULL) {
  195. os_free(pbuf);
  196. return NULL;
  197. }
  198. res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
  199. if (res < 0 || (size_t) res >= len) {
  200. os_free(pbuf);
  201. os_free(buf);
  202. return NULL;
  203. }
  204. #ifdef __CYGWIN__
  205. {
  206. /* Windows/WinPcap uses interface names that are not suitable
  207. * as a file name - convert invalid chars to underscores */
  208. char *pos = buf;
  209. while (*pos) {
  210. if (*pos == '\\')
  211. *pos = '_';
  212. pos++;
  213. }
  214. }
  215. #endif /* __CYGWIN__ */
  216. os_free(pbuf);
  217. return buf;
  218. }
  219. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
  220. const char *txt, size_t len)
  221. {
  222. struct wpa_supplicant *wpa_s = ctx;
  223. if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
  224. return;
  225. wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
  226. }
  227. struct ctrl_iface_priv *
  228. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  229. {
  230. struct ctrl_iface_priv *priv;
  231. struct sockaddr_un addr;
  232. char *fname = NULL;
  233. gid_t gid = 0;
  234. int gid_set = 0;
  235. char *buf, *dir = NULL, *gid_str = NULL;
  236. struct group *grp;
  237. char *endp;
  238. priv = os_zalloc(sizeof(*priv));
  239. if (priv == NULL)
  240. return NULL;
  241. priv->wpa_s = wpa_s;
  242. priv->sock = -1;
  243. if (wpa_s->conf->ctrl_interface == NULL)
  244. return priv;
  245. buf = os_strdup(wpa_s->conf->ctrl_interface);
  246. if (buf == NULL)
  247. goto fail;
  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. #ifdef __FreeBSD__
  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("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. 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 (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. dst = priv->ctrl_dst;
  425. while (dst) {
  426. prev = dst;
  427. dst = dst->next;
  428. os_free(prev);
  429. }
  430. os_free(priv);
  431. }
  432. /**
  433. * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
  434. * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
  435. * @level: Priority level of the message
  436. * @buf: Message data
  437. * @len: Message length
  438. *
  439. * Send a packet to all monitor programs attached to the control interface.
  440. */
  441. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  442. int level, const char *buf,
  443. size_t len)
  444. {
  445. struct wpa_ctrl_dst *dst, *next;
  446. char levelstr[10];
  447. int idx, res;
  448. struct msghdr msg;
  449. struct iovec io[2];
  450. dst = priv->ctrl_dst;
  451. if (priv->sock < 0 || dst == NULL)
  452. return;
  453. res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  454. if (res < 0 || (size_t) res >= sizeof(levelstr))
  455. return;
  456. io[0].iov_base = levelstr;
  457. io[0].iov_len = os_strlen(levelstr);
  458. io[1].iov_base = (char *) buf;
  459. io[1].iov_len = len;
  460. os_memset(&msg, 0, sizeof(msg));
  461. msg.msg_iov = io;
  462. msg.msg_iovlen = 2;
  463. idx = 0;
  464. while (dst) {
  465. next = dst->next;
  466. if (level >= dst->debug_level) {
  467. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  468. (u8 *) dst->addr.sun_path, dst->addrlen -
  469. offsetof(struct sockaddr_un, sun_path));
  470. msg.msg_name = (void *) &dst->addr;
  471. msg.msg_namelen = dst->addrlen;
  472. if (sendmsg(priv->sock, &msg, 0) < 0) {
  473. int _errno = errno;
  474. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  475. "%d - %s",
  476. idx, errno, strerror(errno));
  477. dst->errors++;
  478. if (dst->errors > 10 || _errno == ENOENT) {
  479. wpa_supplicant_ctrl_iface_detach(
  480. priv, &dst->addr,
  481. dst->addrlen);
  482. }
  483. } else
  484. dst->errors = 0;
  485. }
  486. idx++;
  487. dst = next;
  488. }
  489. }
  490. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  491. {
  492. char buf[256];
  493. int res;
  494. struct sockaddr_un from;
  495. socklen_t fromlen = sizeof(from);
  496. for (;;) {
  497. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
  498. "attach", priv->wpa_s->ifname);
  499. eloop_wait_for_read_sock(priv->sock);
  500. res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
  501. (struct sockaddr *) &from, &fromlen);
  502. if (res < 0) {
  503. perror("recvfrom(ctrl_iface)");
  504. continue;
  505. }
  506. buf[res] = '\0';
  507. if (os_strcmp(buf, "ATTACH") == 0) {
  508. /* handle ATTACH signal of first monitor interface */
  509. if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
  510. fromlen)) {
  511. sendto(priv->sock, "OK\n", 3, 0,
  512. (struct sockaddr *) &from, fromlen);
  513. /* OK to continue */
  514. return;
  515. } else {
  516. sendto(priv->sock, "FAIL\n", 5, 0,
  517. (struct sockaddr *) &from, fromlen);
  518. }
  519. } else {
  520. /* return FAIL for all other signals */
  521. sendto(priv->sock, "FAIL\n", 5, 0,
  522. (struct sockaddr *) &from, fromlen);
  523. }
  524. }
  525. }
  526. /* Global ctrl_iface */
  527. struct ctrl_iface_global_priv {
  528. struct wpa_global *global;
  529. int sock;
  530. };
  531. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  532. void *sock_ctx)
  533. {
  534. struct wpa_global *global = eloop_ctx;
  535. char buf[256];
  536. int res;
  537. struct sockaddr_un from;
  538. socklen_t fromlen = sizeof(from);
  539. char *reply;
  540. size_t reply_len;
  541. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  542. (struct sockaddr *) &from, &fromlen);
  543. if (res < 0) {
  544. perror("recvfrom(ctrl_iface)");
  545. return;
  546. }
  547. buf[res] = '\0';
  548. reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
  549. &reply_len);
  550. if (reply) {
  551. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  552. fromlen);
  553. os_free(reply);
  554. } else if (reply_len) {
  555. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  556. fromlen);
  557. }
  558. }
  559. struct ctrl_iface_global_priv *
  560. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  561. {
  562. struct ctrl_iface_global_priv *priv;
  563. struct sockaddr_un addr;
  564. priv = os_zalloc(sizeof(*priv));
  565. if (priv == NULL)
  566. return NULL;
  567. priv->global = global;
  568. priv->sock = -1;
  569. if (global->params.ctrl_interface == NULL)
  570. return priv;
  571. wpa_printf(MSG_DEBUG, "Global control interface '%s'",
  572. global->params.ctrl_interface);
  573. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  574. if (priv->sock < 0) {
  575. perror("socket(PF_UNIX)");
  576. goto fail;
  577. }
  578. os_memset(&addr, 0, sizeof(addr));
  579. #ifdef __FreeBSD__
  580. addr.sun_len = sizeof(addr);
  581. #endif /* __FreeBSD__ */
  582. addr.sun_family = AF_UNIX;
  583. os_strlcpy(addr.sun_path, global->params.ctrl_interface,
  584. sizeof(addr.sun_path));
  585. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  586. perror("bind(PF_UNIX)");
  587. if (connect(priv->sock, (struct sockaddr *) &addr,
  588. sizeof(addr)) < 0) {
  589. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  590. " allow connections - assuming it was left"
  591. "over from forced program termination");
  592. if (unlink(global->params.ctrl_interface) < 0) {
  593. perror("unlink[ctrl_iface]");
  594. wpa_printf(MSG_ERROR, "Could not unlink "
  595. "existing ctrl_iface socket '%s'",
  596. global->params.ctrl_interface);
  597. goto fail;
  598. }
  599. if (bind(priv->sock, (struct sockaddr *) &addr,
  600. sizeof(addr)) < 0) {
  601. perror("bind(PF_UNIX)");
  602. goto fail;
  603. }
  604. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  605. "ctrl_iface socket '%s'",
  606. global->params.ctrl_interface);
  607. } else {
  608. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  609. "be in use - cannot override it");
  610. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  611. "not used anymore",
  612. global->params.ctrl_interface);
  613. goto fail;
  614. }
  615. }
  616. eloop_register_read_sock(priv->sock,
  617. wpa_supplicant_global_ctrl_iface_receive,
  618. global, NULL);
  619. return priv;
  620. fail:
  621. if (priv->sock >= 0)
  622. close(priv->sock);
  623. os_free(priv);
  624. return NULL;
  625. }
  626. void
  627. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  628. {
  629. if (priv->sock >= 0) {
  630. eloop_unregister_read_sock(priv->sock);
  631. close(priv->sock);
  632. }
  633. if (priv->global->params.ctrl_interface)
  634. unlink(priv->global->params.ctrl_interface);
  635. os_free(priv);
  636. }