ctrl_iface_unix.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. #include "utils/common.h"
  20. #include "utils/eloop.h"
  21. #include "utils/list.h"
  22. #include "eapol_supp/eapol_supp_sm.h"
  23. #include "config.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "ctrl_iface.h"
  26. /* Per-interface ctrl_iface */
  27. /**
  28. * struct wpa_ctrl_dst - Internal data structure of control interface monitors
  29. *
  30. * This structure is used to store information about registered control
  31. * interface monitors into struct wpa_supplicant. This data is private to
  32. * ctrl_iface_unix.c and should not be touched directly from other files.
  33. */
  34. struct wpa_ctrl_dst {
  35. struct dl_list list;
  36. struct sockaddr_un addr;
  37. socklen_t addrlen;
  38. int debug_level;
  39. int errors;
  40. };
  41. struct ctrl_iface_priv {
  42. struct wpa_supplicant *wpa_s;
  43. int sock;
  44. struct dl_list ctrl_dst;
  45. };
  46. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  47. int level, const char *buf,
  48. size_t len);
  49. static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
  50. struct sockaddr_un *from,
  51. socklen_t fromlen)
  52. {
  53. struct wpa_ctrl_dst *dst;
  54. dst = os_zalloc(sizeof(*dst));
  55. if (dst == NULL)
  56. return -1;
  57. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  58. dst->addrlen = fromlen;
  59. dst->debug_level = MSG_INFO;
  60. dl_list_add(&priv->ctrl_dst, &dst->list);
  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;
  71. dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
  72. if (fromlen == dst->addrlen &&
  73. os_memcmp(from->sun_path, dst->addr.sun_path,
  74. fromlen - offsetof(struct sockaddr_un, sun_path))
  75. == 0) {
  76. dl_list_del(&dst->list);
  77. os_free(dst);
  78. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  79. (u8 *) from->sun_path,
  80. fromlen -
  81. offsetof(struct sockaddr_un, sun_path));
  82. return 0;
  83. }
  84. }
  85. return -1;
  86. }
  87. static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
  88. struct sockaddr_un *from,
  89. socklen_t fromlen,
  90. char *level)
  91. {
  92. struct wpa_ctrl_dst *dst;
  93. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  94. dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
  95. if (fromlen == dst->addrlen &&
  96. os_memcmp(from->sun_path, dst->addr.sun_path,
  97. fromlen - offsetof(struct sockaddr_un, sun_path))
  98. == 0) {
  99. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  100. "level", (u8 *) from->sun_path,
  101. fromlen -
  102. offsetof(struct sockaddr_un, sun_path));
  103. dst->debug_level = atoi(level);
  104. return 0;
  105. }
  106. }
  107. return -1;
  108. }
  109. static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
  110. void *sock_ctx)
  111. {
  112. struct wpa_supplicant *wpa_s = eloop_ctx;
  113. struct ctrl_iface_priv *priv = sock_ctx;
  114. char buf[4096];
  115. int res;
  116. struct sockaddr_un from;
  117. socklen_t fromlen = sizeof(from);
  118. char *reply = NULL;
  119. size_t reply_len = 0;
  120. int new_attached = 0;
  121. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  122. (struct sockaddr *) &from, &fromlen);
  123. if (res < 0) {
  124. perror("recvfrom(ctrl_iface)");
  125. return;
  126. }
  127. buf[res] = '\0';
  128. if (os_strcmp(buf, "ATTACH") == 0) {
  129. if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
  130. reply_len = 1;
  131. else {
  132. new_attached = 1;
  133. reply_len = 2;
  134. }
  135. } else if (os_strcmp(buf, "DETACH") == 0) {
  136. if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
  137. reply_len = 1;
  138. else
  139. reply_len = 2;
  140. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  141. if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
  142. buf + 6))
  143. reply_len = 1;
  144. else
  145. reply_len = 2;
  146. } else {
  147. reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
  148. &reply_len);
  149. }
  150. if (reply) {
  151. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  152. fromlen);
  153. os_free(reply);
  154. } else if (reply_len == 1) {
  155. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  156. fromlen);
  157. } else if (reply_len == 2) {
  158. sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
  159. fromlen);
  160. }
  161. if (new_attached)
  162. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  163. }
  164. static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
  165. {
  166. char *buf;
  167. size_t len;
  168. char *pbuf, *dir = NULL, *gid_str = NULL;
  169. int res;
  170. if (wpa_s->conf->ctrl_interface == NULL)
  171. return NULL;
  172. pbuf = os_strdup(wpa_s->conf->ctrl_interface);
  173. if (pbuf == NULL)
  174. return NULL;
  175. if (os_strncmp(pbuf, "DIR=", 4) == 0) {
  176. dir = pbuf + 4;
  177. gid_str = os_strstr(dir, " GROUP=");
  178. if (gid_str) {
  179. *gid_str = '\0';
  180. gid_str += 7;
  181. }
  182. } else
  183. dir = pbuf;
  184. len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
  185. buf = os_malloc(len);
  186. if (buf == NULL) {
  187. os_free(pbuf);
  188. return NULL;
  189. }
  190. res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
  191. if (res < 0 || (size_t) res >= len) {
  192. os_free(pbuf);
  193. os_free(buf);
  194. return NULL;
  195. }
  196. #ifdef __CYGWIN__
  197. {
  198. /* Windows/WinPcap uses interface names that are not suitable
  199. * as a file name - convert invalid chars to underscores */
  200. char *pos = buf;
  201. while (*pos) {
  202. if (*pos == '\\')
  203. *pos = '_';
  204. pos++;
  205. }
  206. }
  207. #endif /* __CYGWIN__ */
  208. os_free(pbuf);
  209. return buf;
  210. }
  211. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
  212. const char *txt, size_t len)
  213. {
  214. struct wpa_supplicant *wpa_s = ctx;
  215. if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
  216. return;
  217. wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
  218. }
  219. struct ctrl_iface_priv *
  220. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  221. {
  222. struct ctrl_iface_priv *priv;
  223. struct sockaddr_un addr;
  224. char *fname = NULL;
  225. gid_t gid = 0;
  226. int gid_set = 0;
  227. char *buf, *dir = NULL, *gid_str = NULL;
  228. struct group *grp;
  229. char *endp;
  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. if (os_strncmp(buf, "DIR=", 4) == 0) {
  242. dir = buf + 4;
  243. gid_str = os_strstr(dir, " GROUP=");
  244. if (gid_str) {
  245. *gid_str = '\0';
  246. gid_str += 7;
  247. }
  248. } else {
  249. dir = buf;
  250. gid_str = wpa_s->conf->ctrl_interface_group;
  251. }
  252. if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
  253. if (errno == EEXIST) {
  254. wpa_printf(MSG_DEBUG, "Using existing control "
  255. "interface directory.");
  256. } else {
  257. perror("mkdir[ctrl_interface]");
  258. goto fail;
  259. }
  260. }
  261. if (gid_str) {
  262. grp = getgrnam(gid_str);
  263. if (grp) {
  264. gid = grp->gr_gid;
  265. gid_set = 1;
  266. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  267. " (from group name '%s')",
  268. (int) gid, gid_str);
  269. } else {
  270. /* Group name not found - try to parse this as gid */
  271. gid = strtol(gid_str, &endp, 10);
  272. if (*gid_str == '\0' || *endp != '\0') {
  273. wpa_printf(MSG_ERROR, "CTRL: Invalid group "
  274. "'%s'", gid_str);
  275. goto fail;
  276. }
  277. gid_set = 1;
  278. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  279. (int) gid);
  280. }
  281. }
  282. if (gid_set && chown(dir, -1, gid) < 0) {
  283. perror("chown[ctrl_interface]");
  284. goto fail;
  285. }
  286. /* Make sure the group can enter and read the directory */
  287. if (gid_set &&
  288. chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
  289. wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
  290. strerror(errno));
  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. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  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 (!dl_list_empty(&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. dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
  418. list)
  419. os_free(dst);
  420. os_free(priv);
  421. }
  422. /**
  423. * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
  424. * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
  425. * @level: Priority level of the message
  426. * @buf: Message data
  427. * @len: Message length
  428. *
  429. * Send a packet to all monitor programs attached to the control interface.
  430. */
  431. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  432. int level, const char *buf,
  433. size_t len)
  434. {
  435. struct wpa_ctrl_dst *dst, *next;
  436. char levelstr[10];
  437. int idx, res;
  438. struct msghdr msg;
  439. struct iovec io[2];
  440. if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
  441. return;
  442. res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  443. if (res < 0 || (size_t) res >= sizeof(levelstr))
  444. return;
  445. io[0].iov_base = levelstr;
  446. io[0].iov_len = os_strlen(levelstr);
  447. io[1].iov_base = (char *) buf;
  448. io[1].iov_len = len;
  449. os_memset(&msg, 0, sizeof(msg));
  450. msg.msg_iov = io;
  451. msg.msg_iovlen = 2;
  452. idx = 0;
  453. dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
  454. list) {
  455. if (level >= dst->debug_level) {
  456. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  457. (u8 *) dst->addr.sun_path, dst->addrlen -
  458. offsetof(struct sockaddr_un, sun_path));
  459. msg.msg_name = (void *) &dst->addr;
  460. msg.msg_namelen = dst->addrlen;
  461. if (sendmsg(priv->sock, &msg, 0) < 0) {
  462. int _errno = errno;
  463. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  464. "%d - %s",
  465. idx, errno, strerror(errno));
  466. dst->errors++;
  467. if (dst->errors > 1000 ||
  468. (_errno != ENOBUFS && dst->errors > 10) ||
  469. _errno == ENOENT) {
  470. wpa_supplicant_ctrl_iface_detach(
  471. priv, &dst->addr,
  472. dst->addrlen);
  473. }
  474. } else
  475. dst->errors = 0;
  476. }
  477. idx++;
  478. }
  479. }
  480. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  481. {
  482. char buf[256];
  483. int res;
  484. struct sockaddr_un from;
  485. socklen_t fromlen = sizeof(from);
  486. for (;;) {
  487. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
  488. "attach", priv->wpa_s->ifname);
  489. eloop_wait_for_read_sock(priv->sock);
  490. res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
  491. (struct sockaddr *) &from, &fromlen);
  492. if (res < 0) {
  493. perror("recvfrom(ctrl_iface)");
  494. continue;
  495. }
  496. buf[res] = '\0';
  497. if (os_strcmp(buf, "ATTACH") == 0) {
  498. /* handle ATTACH signal of first monitor interface */
  499. if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
  500. fromlen)) {
  501. sendto(priv->sock, "OK\n", 3, 0,
  502. (struct sockaddr *) &from, fromlen);
  503. /* OK to continue */
  504. return;
  505. } else {
  506. sendto(priv->sock, "FAIL\n", 5, 0,
  507. (struct sockaddr *) &from, fromlen);
  508. }
  509. } else {
  510. /* return FAIL for all other signals */
  511. sendto(priv->sock, "FAIL\n", 5, 0,
  512. (struct sockaddr *) &from, fromlen);
  513. }
  514. }
  515. }
  516. /* Global ctrl_iface */
  517. struct ctrl_iface_global_priv {
  518. struct wpa_global *global;
  519. int sock;
  520. };
  521. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  522. void *sock_ctx)
  523. {
  524. struct wpa_global *global = eloop_ctx;
  525. char buf[256];
  526. int res;
  527. struct sockaddr_un from;
  528. socklen_t fromlen = sizeof(from);
  529. char *reply;
  530. size_t reply_len;
  531. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  532. (struct sockaddr *) &from, &fromlen);
  533. if (res < 0) {
  534. perror("recvfrom(ctrl_iface)");
  535. return;
  536. }
  537. buf[res] = '\0';
  538. reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
  539. &reply_len);
  540. if (reply) {
  541. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  542. fromlen);
  543. os_free(reply);
  544. } else if (reply_len) {
  545. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  546. fromlen);
  547. }
  548. }
  549. struct ctrl_iface_global_priv *
  550. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  551. {
  552. struct ctrl_iface_global_priv *priv;
  553. struct sockaddr_un addr;
  554. priv = os_zalloc(sizeof(*priv));
  555. if (priv == NULL)
  556. return NULL;
  557. priv->global = global;
  558. priv->sock = -1;
  559. if (global->params.ctrl_interface == NULL)
  560. return priv;
  561. wpa_printf(MSG_DEBUG, "Global control interface '%s'",
  562. global->params.ctrl_interface);
  563. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  564. if (priv->sock < 0) {
  565. perror("socket(PF_UNIX)");
  566. goto fail;
  567. }
  568. os_memset(&addr, 0, sizeof(addr));
  569. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  570. addr.sun_len = sizeof(addr);
  571. #endif /* __FreeBSD__ */
  572. addr.sun_family = AF_UNIX;
  573. os_strlcpy(addr.sun_path, global->params.ctrl_interface,
  574. sizeof(addr.sun_path));
  575. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  576. perror("bind(PF_UNIX)");
  577. if (connect(priv->sock, (struct sockaddr *) &addr,
  578. sizeof(addr)) < 0) {
  579. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  580. " allow connections - assuming it was left"
  581. "over from forced program termination");
  582. if (unlink(global->params.ctrl_interface) < 0) {
  583. perror("unlink[ctrl_iface]");
  584. wpa_printf(MSG_ERROR, "Could not unlink "
  585. "existing ctrl_iface socket '%s'",
  586. global->params.ctrl_interface);
  587. goto fail;
  588. }
  589. if (bind(priv->sock, (struct sockaddr *) &addr,
  590. sizeof(addr)) < 0) {
  591. perror("bind(PF_UNIX)");
  592. goto fail;
  593. }
  594. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  595. "ctrl_iface socket '%s'",
  596. global->params.ctrl_interface);
  597. } else {
  598. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  599. "be in use - cannot override it");
  600. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  601. "not used anymore",
  602. global->params.ctrl_interface);
  603. goto fail;
  604. }
  605. }
  606. eloop_register_read_sock(priv->sock,
  607. wpa_supplicant_global_ctrl_iface_receive,
  608. global, NULL);
  609. return priv;
  610. fail:
  611. if (priv->sock >= 0)
  612. close(priv->sock);
  613. os_free(priv);
  614. return NULL;
  615. }
  616. void
  617. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  618. {
  619. if (priv->sock >= 0) {
  620. eloop_unregister_read_sock(priv->sock);
  621. close(priv->sock);
  622. }
  623. if (priv->global->params.ctrl_interface)
  624. unlink(priv->global->params.ctrl_interface);
  625. os_free(priv);
  626. }