ctrl_iface_unix.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
  294. sizeof(addr.sun_path)) {
  295. wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
  296. goto fail;
  297. }
  298. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  299. if (priv->sock < 0) {
  300. perror("socket(PF_UNIX)");
  301. goto fail;
  302. }
  303. os_memset(&addr, 0, sizeof(addr));
  304. #ifdef __FreeBSD__
  305. addr.sun_len = sizeof(addr);
  306. #endif /* __FreeBSD__ */
  307. addr.sun_family = AF_UNIX;
  308. fname = wpa_supplicant_ctrl_iface_path(wpa_s);
  309. if (fname == NULL)
  310. goto fail;
  311. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  312. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  313. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  314. strerror(errno));
  315. if (connect(priv->sock, (struct sockaddr *) &addr,
  316. sizeof(addr)) < 0) {
  317. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  318. " allow connections - assuming it was left"
  319. "over from forced program termination");
  320. if (unlink(fname) < 0) {
  321. perror("unlink[ctrl_iface]");
  322. wpa_printf(MSG_ERROR, "Could not unlink "
  323. "existing ctrl_iface socket '%s'",
  324. fname);
  325. goto fail;
  326. }
  327. if (bind(priv->sock, (struct sockaddr *) &addr,
  328. sizeof(addr)) < 0) {
  329. perror("bind(PF_UNIX)");
  330. goto fail;
  331. }
  332. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  333. "ctrl_iface socket '%s'", fname);
  334. } else {
  335. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  336. "be in use - cannot override it");
  337. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  338. "not used anymore", fname);
  339. os_free(fname);
  340. fname = NULL;
  341. goto fail;
  342. }
  343. }
  344. if (gid_set && chown(fname, -1, gid) < 0) {
  345. perror("chown[ctrl_interface/ifname]");
  346. goto fail;
  347. }
  348. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  349. perror("chmod[ctrl_interface/ifname]");
  350. goto fail;
  351. }
  352. os_free(fname);
  353. eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
  354. wpa_s, priv);
  355. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  356. os_free(buf);
  357. return priv;
  358. fail:
  359. if (priv->sock >= 0)
  360. close(priv->sock);
  361. os_free(priv);
  362. if (fname) {
  363. unlink(fname);
  364. os_free(fname);
  365. }
  366. os_free(buf);
  367. return NULL;
  368. }
  369. void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
  370. {
  371. struct wpa_ctrl_dst *dst, *prev;
  372. if (priv->sock > -1) {
  373. char *fname;
  374. char *buf, *dir = NULL, *gid_str = NULL;
  375. eloop_unregister_read_sock(priv->sock);
  376. if (priv->ctrl_dst) {
  377. /*
  378. * Wait a second before closing the control socket if
  379. * there are any attached monitors in order to allow
  380. * them to receive any pending messages.
  381. */
  382. wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
  383. "monitors to receive messages");
  384. os_sleep(1, 0);
  385. }
  386. close(priv->sock);
  387. priv->sock = -1;
  388. fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
  389. if (fname) {
  390. unlink(fname);
  391. os_free(fname);
  392. }
  393. buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
  394. if (buf == NULL)
  395. goto free_dst;
  396. if (os_strncmp(buf, "DIR=", 4) == 0) {
  397. dir = buf + 4;
  398. gid_str = os_strstr(dir, " GROUP=");
  399. if (gid_str) {
  400. *gid_str = '\0';
  401. gid_str += 7;
  402. }
  403. } else
  404. dir = buf;
  405. if (rmdir(dir) < 0) {
  406. if (errno == ENOTEMPTY) {
  407. wpa_printf(MSG_DEBUG, "Control interface "
  408. "directory not empty - leaving it "
  409. "behind");
  410. } else {
  411. perror("rmdir[ctrl_interface]");
  412. }
  413. }
  414. os_free(buf);
  415. }
  416. free_dst:
  417. dst = priv->ctrl_dst;
  418. while (dst) {
  419. prev = dst;
  420. dst = dst->next;
  421. os_free(prev);
  422. }
  423. os_free(priv);
  424. }
  425. /**
  426. * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
  427. * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
  428. * @level: Priority level of the message
  429. * @buf: Message data
  430. * @len: Message length
  431. *
  432. * Send a packet to all monitor programs attached to the control interface.
  433. */
  434. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  435. int level, const char *buf,
  436. size_t len)
  437. {
  438. struct wpa_ctrl_dst *dst, *next;
  439. char levelstr[10];
  440. int idx, res;
  441. struct msghdr msg;
  442. struct iovec io[2];
  443. dst = priv->ctrl_dst;
  444. if (priv->sock < 0 || dst == NULL)
  445. return;
  446. res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  447. if (res < 0 || (size_t) res >= sizeof(levelstr))
  448. return;
  449. io[0].iov_base = levelstr;
  450. io[0].iov_len = os_strlen(levelstr);
  451. io[1].iov_base = (char *) buf;
  452. io[1].iov_len = len;
  453. os_memset(&msg, 0, sizeof(msg));
  454. msg.msg_iov = io;
  455. msg.msg_iovlen = 2;
  456. idx = 0;
  457. while (dst) {
  458. next = dst->next;
  459. if (level >= dst->debug_level) {
  460. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  461. (u8 *) dst->addr.sun_path, dst->addrlen -
  462. offsetof(struct sockaddr_un, sun_path));
  463. msg.msg_name = (void *) &dst->addr;
  464. msg.msg_namelen = dst->addrlen;
  465. if (sendmsg(priv->sock, &msg, 0) < 0) {
  466. int _errno = errno;
  467. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  468. "%d - %s",
  469. idx, errno, strerror(errno));
  470. dst->errors++;
  471. if (dst->errors > 10 || _errno == ENOENT) {
  472. wpa_supplicant_ctrl_iface_detach(
  473. priv, &dst->addr,
  474. dst->addrlen);
  475. }
  476. } else
  477. dst->errors = 0;
  478. }
  479. idx++;
  480. dst = next;
  481. }
  482. }
  483. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  484. {
  485. char buf[256];
  486. int res;
  487. struct sockaddr_un from;
  488. socklen_t fromlen = sizeof(from);
  489. for (;;) {
  490. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
  491. "attach", priv->wpa_s->ifname);
  492. eloop_wait_for_read_sock(priv->sock);
  493. res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
  494. (struct sockaddr *) &from, &fromlen);
  495. if (res < 0) {
  496. perror("recvfrom(ctrl_iface)");
  497. continue;
  498. }
  499. buf[res] = '\0';
  500. if (os_strcmp(buf, "ATTACH") == 0) {
  501. /* handle ATTACH signal of first monitor interface */
  502. if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
  503. fromlen)) {
  504. sendto(priv->sock, "OK\n", 3, 0,
  505. (struct sockaddr *) &from, fromlen);
  506. /* OK to continue */
  507. return;
  508. } else {
  509. sendto(priv->sock, "FAIL\n", 5, 0,
  510. (struct sockaddr *) &from, fromlen);
  511. }
  512. } else {
  513. /* return FAIL for all other signals */
  514. sendto(priv->sock, "FAIL\n", 5, 0,
  515. (struct sockaddr *) &from, fromlen);
  516. }
  517. }
  518. }
  519. /* Global ctrl_iface */
  520. struct ctrl_iface_global_priv {
  521. struct wpa_global *global;
  522. int sock;
  523. };
  524. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  525. void *sock_ctx)
  526. {
  527. struct wpa_global *global = eloop_ctx;
  528. char buf[256];
  529. int res;
  530. struct sockaddr_un from;
  531. socklen_t fromlen = sizeof(from);
  532. char *reply;
  533. size_t reply_len;
  534. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  535. (struct sockaddr *) &from, &fromlen);
  536. if (res < 0) {
  537. perror("recvfrom(ctrl_iface)");
  538. return;
  539. }
  540. buf[res] = '\0';
  541. reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
  542. &reply_len);
  543. if (reply) {
  544. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  545. fromlen);
  546. os_free(reply);
  547. } else if (reply_len) {
  548. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  549. fromlen);
  550. }
  551. }
  552. struct ctrl_iface_global_priv *
  553. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  554. {
  555. struct ctrl_iface_global_priv *priv;
  556. struct sockaddr_un addr;
  557. priv = os_zalloc(sizeof(*priv));
  558. if (priv == NULL)
  559. return NULL;
  560. priv->global = global;
  561. priv->sock = -1;
  562. if (global->params.ctrl_interface == NULL)
  563. return priv;
  564. wpa_printf(MSG_DEBUG, "Global control interface '%s'",
  565. global->params.ctrl_interface);
  566. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  567. if (priv->sock < 0) {
  568. perror("socket(PF_UNIX)");
  569. goto fail;
  570. }
  571. os_memset(&addr, 0, sizeof(addr));
  572. #ifdef __FreeBSD__
  573. addr.sun_len = sizeof(addr);
  574. #endif /* __FreeBSD__ */
  575. addr.sun_family = AF_UNIX;
  576. os_strlcpy(addr.sun_path, global->params.ctrl_interface,
  577. sizeof(addr.sun_path));
  578. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  579. perror("bind(PF_UNIX)");
  580. if (connect(priv->sock, (struct sockaddr *) &addr,
  581. sizeof(addr)) < 0) {
  582. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  583. " allow connections - assuming it was left"
  584. "over from forced program termination");
  585. if (unlink(global->params.ctrl_interface) < 0) {
  586. perror("unlink[ctrl_iface]");
  587. wpa_printf(MSG_ERROR, "Could not unlink "
  588. "existing ctrl_iface socket '%s'",
  589. global->params.ctrl_interface);
  590. goto fail;
  591. }
  592. if (bind(priv->sock, (struct sockaddr *) &addr,
  593. sizeof(addr)) < 0) {
  594. perror("bind(PF_UNIX)");
  595. goto fail;
  596. }
  597. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  598. "ctrl_iface socket '%s'",
  599. global->params.ctrl_interface);
  600. } else {
  601. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  602. "be in use - cannot override it");
  603. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  604. "not used anymore",
  605. global->params.ctrl_interface);
  606. goto fail;
  607. }
  608. }
  609. eloop_register_read_sock(priv->sock,
  610. wpa_supplicant_global_ctrl_iface_receive,
  611. global, NULL);
  612. return priv;
  613. fail:
  614. if (priv->sock >= 0)
  615. close(priv->sock);
  616. os_free(priv);
  617. return NULL;
  618. }
  619. void
  620. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  621. {
  622. if (priv->sock >= 0) {
  623. eloop_unregister_read_sock(priv->sock);
  624. close(priv->sock);
  625. }
  626. if (priv->global->params.ctrl_interface)
  627. unlink(priv->global->params.ctrl_interface);
  628. os_free(priv);
  629. }