ctrl_iface_unix.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. /*
  2. * WPA Supplicant / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2013, 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. #include <unistd.h>
  14. #include <fcntl.h>
  15. #ifdef ANDROID
  16. #include <cutils/sockets.h>
  17. #endif /* ANDROID */
  18. #include "utils/common.h"
  19. #include "utils/eloop.h"
  20. #include "utils/list.h"
  21. #include "eapol_supp/eapol_supp_sm.h"
  22. #include "config.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 dl_list list;
  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 dl_list ctrl_dst;
  44. };
  45. struct ctrl_iface_global_priv {
  46. struct wpa_global *global;
  47. int sock;
  48. struct dl_list ctrl_dst;
  49. };
  50. static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
  51. const char *ifname, int sock,
  52. struct dl_list *ctrl_dst,
  53. int level, const char *buf,
  54. size_t len,
  55. struct ctrl_iface_priv *priv,
  56. struct ctrl_iface_global_priv *gp);
  57. static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
  58. struct ctrl_iface_priv *priv);
  59. static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
  60. struct ctrl_iface_global_priv *priv);
  61. static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
  62. struct sockaddr_un *from,
  63. socklen_t fromlen)
  64. {
  65. struct wpa_ctrl_dst *dst;
  66. dst = os_zalloc(sizeof(*dst));
  67. if (dst == NULL)
  68. return -1;
  69. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  70. dst->addrlen = fromlen;
  71. dst->debug_level = MSG_INFO;
  72. dl_list_add(ctrl_dst, &dst->list);
  73. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  74. (u8 *) from->sun_path,
  75. fromlen - offsetof(struct sockaddr_un, sun_path));
  76. return 0;
  77. }
  78. static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
  79. struct sockaddr_un *from,
  80. socklen_t fromlen)
  81. {
  82. struct wpa_ctrl_dst *dst;
  83. dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
  84. if (fromlen == dst->addrlen &&
  85. os_memcmp(from->sun_path, dst->addr.sun_path,
  86. fromlen - offsetof(struct sockaddr_un, sun_path))
  87. == 0) {
  88. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  89. (u8 *) from->sun_path,
  90. fromlen -
  91. offsetof(struct sockaddr_un, sun_path));
  92. dl_list_del(&dst->list);
  93. os_free(dst);
  94. return 0;
  95. }
  96. }
  97. return -1;
  98. }
  99. static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
  100. struct sockaddr_un *from,
  101. socklen_t fromlen,
  102. char *level)
  103. {
  104. struct wpa_ctrl_dst *dst;
  105. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  106. dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
  107. if (fromlen == dst->addrlen &&
  108. os_memcmp(from->sun_path, dst->addr.sun_path,
  109. fromlen - offsetof(struct sockaddr_un, sun_path))
  110. == 0) {
  111. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  112. "level", (u8 *) from->sun_path,
  113. fromlen -
  114. offsetof(struct sockaddr_un, sun_path));
  115. dst->debug_level = atoi(level);
  116. return 0;
  117. }
  118. }
  119. return -1;
  120. }
  121. static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
  122. void *sock_ctx)
  123. {
  124. struct wpa_supplicant *wpa_s = eloop_ctx;
  125. struct ctrl_iface_priv *priv = sock_ctx;
  126. char buf[4096];
  127. int res;
  128. struct sockaddr_un from;
  129. socklen_t fromlen = sizeof(from);
  130. char *reply = NULL, *reply_buf = NULL;
  131. size_t reply_len = 0;
  132. int new_attached = 0;
  133. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  134. (struct sockaddr *) &from, &fromlen);
  135. if (res < 0) {
  136. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  137. strerror(errno));
  138. return;
  139. }
  140. buf[res] = '\0';
  141. if (os_strcmp(buf, "ATTACH") == 0) {
  142. if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
  143. fromlen))
  144. reply_len = 1;
  145. else {
  146. new_attached = 1;
  147. reply_len = 2;
  148. }
  149. } else if (os_strcmp(buf, "DETACH") == 0) {
  150. if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
  151. fromlen))
  152. reply_len = 1;
  153. else
  154. reply_len = 2;
  155. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  156. if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
  157. buf + 6))
  158. reply_len = 1;
  159. else
  160. reply_len = 2;
  161. } else {
  162. reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
  163. &reply_len);
  164. reply = reply_buf;
  165. }
  166. if (!reply && reply_len == 1) {
  167. reply = "FAIL\n";
  168. reply_len = 5;
  169. } else if (!reply && reply_len == 2) {
  170. reply = "OK\n";
  171. reply_len = 3;
  172. }
  173. if (reply) {
  174. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  175. fromlen) < 0) {
  176. int _errno = errno;
  177. wpa_dbg(wpa_s, MSG_DEBUG,
  178. "ctrl_iface sendto failed: %d - %s",
  179. _errno, strerror(_errno));
  180. if (_errno == ENOBUFS || _errno == EAGAIN) {
  181. /*
  182. * The socket send buffer could be full. This
  183. * may happen if client programs are not
  184. * receiving their pending messages. Close and
  185. * reopen the socket as a workaround to avoid
  186. * getting stuck being unable to send any new
  187. * responses.
  188. */
  189. sock = wpas_ctrl_iface_reinit(wpa_s, priv);
  190. if (sock < 0) {
  191. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket");
  192. }
  193. }
  194. if (new_attached) {
  195. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching");
  196. new_attached = 0;
  197. wpa_supplicant_ctrl_iface_detach(
  198. &priv->ctrl_dst, &from, fromlen);
  199. }
  200. }
  201. }
  202. os_free(reply_buf);
  203. if (new_attached)
  204. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  205. }
  206. static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
  207. {
  208. char *buf;
  209. size_t len;
  210. char *pbuf, *dir = NULL, *gid_str = NULL;
  211. int res;
  212. if (wpa_s->conf->ctrl_interface == NULL)
  213. return NULL;
  214. pbuf = os_strdup(wpa_s->conf->ctrl_interface);
  215. if (pbuf == NULL)
  216. return NULL;
  217. if (os_strncmp(pbuf, "DIR=", 4) == 0) {
  218. dir = pbuf + 4;
  219. gid_str = os_strstr(dir, " GROUP=");
  220. if (gid_str) {
  221. *gid_str = '\0';
  222. gid_str += 7;
  223. }
  224. } else
  225. dir = pbuf;
  226. len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
  227. buf = os_malloc(len);
  228. if (buf == NULL) {
  229. os_free(pbuf);
  230. return NULL;
  231. }
  232. res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
  233. if (res < 0 || (size_t) res >= len) {
  234. os_free(pbuf);
  235. os_free(buf);
  236. return NULL;
  237. }
  238. #ifdef __CYGWIN__
  239. {
  240. /* Windows/WinPcap uses interface names that are not suitable
  241. * as a file name - convert invalid chars to underscores */
  242. char *pos = buf;
  243. while (*pos) {
  244. if (*pos == '\\')
  245. *pos = '_';
  246. pos++;
  247. }
  248. }
  249. #endif /* __CYGWIN__ */
  250. os_free(pbuf);
  251. return buf;
  252. }
  253. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, int global,
  254. const char *txt, size_t len)
  255. {
  256. struct wpa_supplicant *wpa_s = ctx;
  257. if (wpa_s == NULL)
  258. return;
  259. if (global != 2 && wpa_s->global->ctrl_iface) {
  260. struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
  261. if (!dl_list_empty(&priv->ctrl_dst)) {
  262. wpa_supplicant_ctrl_iface_send(wpa_s, global ? NULL :
  263. wpa_s->ifname,
  264. priv->sock,
  265. &priv->ctrl_dst,
  266. level, txt, len, NULL,
  267. priv);
  268. }
  269. }
  270. if (wpa_s->ctrl_iface == NULL)
  271. return;
  272. wpa_supplicant_ctrl_iface_send(wpa_s, NULL, wpa_s->ctrl_iface->sock,
  273. &wpa_s->ctrl_iface->ctrl_dst,
  274. level, txt, len, wpa_s->ctrl_iface,
  275. NULL);
  276. }
  277. static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
  278. struct ctrl_iface_priv *priv)
  279. {
  280. struct sockaddr_un addr;
  281. char *fname = NULL;
  282. gid_t gid = 0;
  283. int gid_set = 0;
  284. char *buf, *dir = NULL, *gid_str = NULL;
  285. struct group *grp;
  286. char *endp;
  287. int flags;
  288. buf = os_strdup(wpa_s->conf->ctrl_interface);
  289. if (buf == NULL)
  290. goto fail;
  291. #ifdef ANDROID
  292. os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
  293. wpa_s->conf->ctrl_interface);
  294. priv->sock = android_get_control_socket(addr.sun_path);
  295. if (priv->sock >= 0)
  296. goto havesock;
  297. #endif /* ANDROID */
  298. if (os_strncmp(buf, "DIR=", 4) == 0) {
  299. dir = buf + 4;
  300. gid_str = os_strstr(dir, " GROUP=");
  301. if (gid_str) {
  302. *gid_str = '\0';
  303. gid_str += 7;
  304. }
  305. } else {
  306. dir = buf;
  307. gid_str = wpa_s->conf->ctrl_interface_group;
  308. }
  309. if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
  310. if (errno == EEXIST) {
  311. wpa_printf(MSG_DEBUG, "Using existing control "
  312. "interface directory.");
  313. } else {
  314. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
  315. dir, strerror(errno));
  316. goto fail;
  317. }
  318. }
  319. #ifdef ANDROID
  320. /*
  321. * wpa_supplicant is started from /init.*.rc on Android and that seems
  322. * to be using umask 0077 which would leave the control interface
  323. * directory without group access. This breaks things since Wi-Fi
  324. * framework assumes that this directory can be accessed by other
  325. * applications in the wifi group. Fix this by adding group access even
  326. * if umask value would prevent this.
  327. */
  328. if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
  329. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  330. strerror(errno));
  331. /* Try to continue anyway */
  332. }
  333. #endif /* ANDROID */
  334. if (gid_str) {
  335. grp = getgrnam(gid_str);
  336. if (grp) {
  337. gid = grp->gr_gid;
  338. gid_set = 1;
  339. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  340. " (from group name '%s')",
  341. (int) gid, gid_str);
  342. } else {
  343. /* Group name not found - try to parse this as gid */
  344. gid = strtol(gid_str, &endp, 10);
  345. if (*gid_str == '\0' || *endp != '\0') {
  346. wpa_printf(MSG_ERROR, "CTRL: Invalid group "
  347. "'%s'", gid_str);
  348. goto fail;
  349. }
  350. gid_set = 1;
  351. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  352. (int) gid);
  353. }
  354. }
  355. if (gid_set && chown(dir, -1, gid) < 0) {
  356. wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
  357. dir, (int) gid, strerror(errno));
  358. goto fail;
  359. }
  360. /* Make sure the group can enter and read the directory */
  361. if (gid_set &&
  362. chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
  363. wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
  364. strerror(errno));
  365. goto fail;
  366. }
  367. if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
  368. sizeof(addr.sun_path)) {
  369. wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
  370. goto fail;
  371. }
  372. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  373. if (priv->sock < 0) {
  374. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  375. goto fail;
  376. }
  377. os_memset(&addr, 0, sizeof(addr));
  378. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  379. addr.sun_len = sizeof(addr);
  380. #endif /* __FreeBSD__ */
  381. addr.sun_family = AF_UNIX;
  382. fname = wpa_supplicant_ctrl_iface_path(wpa_s);
  383. if (fname == NULL)
  384. goto fail;
  385. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  386. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  387. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  388. strerror(errno));
  389. if (connect(priv->sock, (struct sockaddr *) &addr,
  390. sizeof(addr)) < 0) {
  391. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  392. " allow connections - assuming it was left"
  393. "over from forced program termination");
  394. if (unlink(fname) < 0) {
  395. wpa_printf(MSG_ERROR,
  396. "Could not unlink existing ctrl_iface socket '%s': %s",
  397. fname, strerror(errno));
  398. goto fail;
  399. }
  400. if (bind(priv->sock, (struct sockaddr *) &addr,
  401. sizeof(addr)) < 0) {
  402. wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
  403. strerror(errno));
  404. goto fail;
  405. }
  406. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  407. "ctrl_iface socket '%s'", fname);
  408. } else {
  409. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  410. "be in use - cannot override it");
  411. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  412. "not used anymore", fname);
  413. os_free(fname);
  414. fname = NULL;
  415. goto fail;
  416. }
  417. }
  418. if (gid_set && chown(fname, -1, gid) < 0) {
  419. wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
  420. fname, (int) gid, strerror(errno));
  421. goto fail;
  422. }
  423. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  424. wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
  425. fname, strerror(errno));
  426. goto fail;
  427. }
  428. os_free(fname);
  429. #ifdef ANDROID
  430. havesock:
  431. #endif /* ANDROID */
  432. /*
  433. * Make socket non-blocking so that we don't hang forever if
  434. * target dies unexpectedly.
  435. */
  436. flags = fcntl(priv->sock, F_GETFL);
  437. if (flags >= 0) {
  438. flags |= O_NONBLOCK;
  439. if (fcntl(priv->sock, F_SETFL, flags) < 0) {
  440. wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
  441. strerror(errno));
  442. /* Not fatal, continue on.*/
  443. }
  444. }
  445. eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
  446. wpa_s, priv);
  447. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  448. os_free(buf);
  449. return 0;
  450. fail:
  451. if (priv->sock >= 0) {
  452. close(priv->sock);
  453. priv->sock = -1;
  454. }
  455. if (fname) {
  456. unlink(fname);
  457. os_free(fname);
  458. }
  459. os_free(buf);
  460. return -1;
  461. }
  462. struct ctrl_iface_priv *
  463. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  464. {
  465. struct ctrl_iface_priv *priv;
  466. priv = os_zalloc(sizeof(*priv));
  467. if (priv == NULL)
  468. return NULL;
  469. dl_list_init(&priv->ctrl_dst);
  470. priv->wpa_s = wpa_s;
  471. priv->sock = -1;
  472. if (wpa_s->conf->ctrl_interface == NULL)
  473. return priv;
  474. if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) {
  475. os_free(priv);
  476. return NULL;
  477. }
  478. return priv;
  479. }
  480. static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
  481. struct ctrl_iface_priv *priv)
  482. {
  483. int res;
  484. if (priv->sock <= 0)
  485. return -1;
  486. eloop_unregister_read_sock(priv->sock);
  487. close(priv->sock);
  488. priv->sock = -1;
  489. res = wpas_ctrl_iface_open_sock(wpa_s, priv);
  490. if (res < 0)
  491. return -1;
  492. return priv->sock;
  493. }
  494. void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
  495. {
  496. struct wpa_ctrl_dst *dst, *prev;
  497. if (priv->sock > -1) {
  498. char *fname;
  499. char *buf, *dir = NULL, *gid_str = NULL;
  500. eloop_unregister_read_sock(priv->sock);
  501. if (!dl_list_empty(&priv->ctrl_dst)) {
  502. /*
  503. * Wait before closing the control socket if
  504. * there are any attached monitors in order to allow
  505. * them to receive any pending messages.
  506. */
  507. wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
  508. "monitors to receive messages");
  509. os_sleep(0, 100000);
  510. }
  511. close(priv->sock);
  512. priv->sock = -1;
  513. fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
  514. if (fname) {
  515. unlink(fname);
  516. os_free(fname);
  517. }
  518. if (priv->wpa_s->conf->ctrl_interface == NULL)
  519. goto free_dst;
  520. buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
  521. if (buf == NULL)
  522. goto free_dst;
  523. if (os_strncmp(buf, "DIR=", 4) == 0) {
  524. dir = buf + 4;
  525. gid_str = os_strstr(dir, " GROUP=");
  526. if (gid_str) {
  527. *gid_str = '\0';
  528. gid_str += 7;
  529. }
  530. } else
  531. dir = buf;
  532. if (rmdir(dir) < 0) {
  533. if (errno == ENOTEMPTY) {
  534. wpa_printf(MSG_DEBUG, "Control interface "
  535. "directory not empty - leaving it "
  536. "behind");
  537. } else {
  538. wpa_printf(MSG_ERROR,
  539. "rmdir[ctrl_interface=%s]: %s",
  540. dir, strerror(errno));
  541. }
  542. }
  543. os_free(buf);
  544. }
  545. free_dst:
  546. dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
  547. list)
  548. os_free(dst);
  549. os_free(priv);
  550. }
  551. /**
  552. * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
  553. * @ifname: Interface name for global control socket or %NULL
  554. * @sock: Local socket fd
  555. * @ctrl_dst: List of attached listeners
  556. * @level: Priority level of the message
  557. * @buf: Message data
  558. * @len: Message length
  559. *
  560. * Send a packet to all monitor programs attached to the control interface.
  561. */
  562. static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
  563. const char *ifname, int sock,
  564. struct dl_list *ctrl_dst,
  565. int level, const char *buf,
  566. size_t len,
  567. struct ctrl_iface_priv *priv,
  568. struct ctrl_iface_global_priv *gp)
  569. {
  570. struct wpa_ctrl_dst *dst, *next;
  571. char levelstr[10];
  572. int idx, res;
  573. struct msghdr msg;
  574. struct iovec io[5];
  575. if (sock < 0 || dl_list_empty(ctrl_dst))
  576. return;
  577. res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  578. if (res < 0 || (size_t) res >= sizeof(levelstr))
  579. return;
  580. idx = 0;
  581. if (ifname) {
  582. io[idx].iov_base = "IFNAME=";
  583. io[idx].iov_len = 7;
  584. idx++;
  585. io[idx].iov_base = (char *) ifname;
  586. io[idx].iov_len = os_strlen(ifname);
  587. idx++;
  588. io[idx].iov_base = " ";
  589. io[idx].iov_len = 1;
  590. idx++;
  591. }
  592. io[idx].iov_base = levelstr;
  593. io[idx].iov_len = os_strlen(levelstr);
  594. idx++;
  595. io[idx].iov_base = (char *) buf;
  596. io[idx].iov_len = len;
  597. idx++;
  598. os_memset(&msg, 0, sizeof(msg));
  599. msg.msg_iov = io;
  600. msg.msg_iovlen = idx;
  601. idx = -1;
  602. dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
  603. int _errno;
  604. idx++;
  605. if (level < dst->debug_level)
  606. continue;
  607. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  608. (u8 *) dst->addr.sun_path, dst->addrlen -
  609. offsetof(struct sockaddr_un, sun_path));
  610. msg.msg_name = (void *) &dst->addr;
  611. msg.msg_namelen = dst->addrlen;
  612. if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
  613. dst->errors = 0;
  614. idx++;
  615. continue;
  616. }
  617. _errno = errno;
  618. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: %d - %s",
  619. idx, errno, strerror(errno));
  620. dst->errors++;
  621. if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
  622. wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor that cannot receive messages");
  623. wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
  624. dst->addrlen);
  625. }
  626. if (_errno == ENOBUFS || _errno == EAGAIN) {
  627. /*
  628. * The socket send buffer could be full. This may happen
  629. * if client programs are not receiving their pending
  630. * messages. Close and reopen the socket as a workaround
  631. * to avoid getting stuck being unable to send any new
  632. * responses.
  633. */
  634. if (priv)
  635. sock = wpas_ctrl_iface_reinit(wpa_s, priv);
  636. else if (gp)
  637. sock = wpas_ctrl_iface_global_reinit(
  638. wpa_s->global, gp);
  639. else
  640. break;
  641. if (sock < 0) {
  642. wpa_dbg(wpa_s, MSG_DEBUG,
  643. "Failed to reinitialize ctrl_iface socket");
  644. }
  645. }
  646. }
  647. }
  648. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  649. {
  650. char buf[256];
  651. int res;
  652. struct sockaddr_un from;
  653. socklen_t fromlen = sizeof(from);
  654. for (;;) {
  655. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
  656. "attach", priv->wpa_s->ifname);
  657. eloop_wait_for_read_sock(priv->sock);
  658. res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
  659. (struct sockaddr *) &from, &fromlen);
  660. if (res < 0) {
  661. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  662. strerror(errno));
  663. continue;
  664. }
  665. buf[res] = '\0';
  666. if (os_strcmp(buf, "ATTACH") == 0) {
  667. /* handle ATTACH signal of first monitor interface */
  668. if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
  669. &from, fromlen)) {
  670. if (sendto(priv->sock, "OK\n", 3, 0,
  671. (struct sockaddr *) &from, fromlen) <
  672. 0) {
  673. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  674. strerror(errno));
  675. }
  676. /* OK to continue */
  677. return;
  678. } else {
  679. if (sendto(priv->sock, "FAIL\n", 5, 0,
  680. (struct sockaddr *) &from, fromlen) <
  681. 0) {
  682. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  683. strerror(errno));
  684. }
  685. }
  686. } else {
  687. /* return FAIL for all other signals */
  688. if (sendto(priv->sock, "FAIL\n", 5, 0,
  689. (struct sockaddr *) &from, fromlen) < 0) {
  690. wpa_printf(MSG_DEBUG,
  691. "ctrl_iface sendto failed: %s",
  692. strerror(errno));
  693. }
  694. }
  695. }
  696. }
  697. /* Global ctrl_iface */
  698. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  699. void *sock_ctx)
  700. {
  701. struct wpa_global *global = eloop_ctx;
  702. struct ctrl_iface_global_priv *priv = sock_ctx;
  703. char buf[256];
  704. int res;
  705. struct sockaddr_un from;
  706. socklen_t fromlen = sizeof(from);
  707. char *reply = NULL, *reply_buf = NULL;
  708. size_t reply_len;
  709. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  710. (struct sockaddr *) &from, &fromlen);
  711. if (res < 0) {
  712. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  713. strerror(errno));
  714. return;
  715. }
  716. buf[res] = '\0';
  717. if (os_strcmp(buf, "ATTACH") == 0) {
  718. if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
  719. fromlen))
  720. reply_len = 1;
  721. else
  722. reply_len = 2;
  723. } else if (os_strcmp(buf, "DETACH") == 0) {
  724. if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
  725. fromlen))
  726. reply_len = 1;
  727. else
  728. reply_len = 2;
  729. } else {
  730. reply_buf = wpa_supplicant_global_ctrl_iface_process(
  731. global, buf, &reply_len);
  732. reply = reply_buf;
  733. }
  734. if (!reply && reply_len == 1) {
  735. reply = "FAIL\n";
  736. reply_len = 5;
  737. } else if (!reply && reply_len == 2) {
  738. reply = "OK\n";
  739. reply_len = 3;
  740. }
  741. if (reply) {
  742. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  743. fromlen) < 0) {
  744. wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
  745. strerror(errno));
  746. }
  747. }
  748. os_free(reply_buf);
  749. }
  750. static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
  751. struct ctrl_iface_global_priv *priv)
  752. {
  753. struct sockaddr_un addr;
  754. const char *ctrl = global->params.ctrl_interface;
  755. int flags;
  756. wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
  757. #ifdef ANDROID
  758. if (os_strncmp(ctrl, "@android:", 9) == 0) {
  759. priv->sock = android_get_control_socket(ctrl + 9);
  760. if (priv->sock < 0) {
  761. wpa_printf(MSG_ERROR, "Failed to open Android control "
  762. "socket '%s'", ctrl + 9);
  763. goto fail;
  764. }
  765. wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
  766. ctrl + 9);
  767. goto havesock;
  768. }
  769. if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
  770. /*
  771. * Backwards compatibility - try to open an Android control
  772. * socket and if that fails, assume this was a UNIX domain
  773. * socket instead.
  774. */
  775. priv->sock = android_get_control_socket(ctrl);
  776. if (priv->sock >= 0) {
  777. wpa_printf(MSG_DEBUG,
  778. "Using Android control socket '%s'",
  779. ctrl);
  780. goto havesock;
  781. }
  782. }
  783. #endif /* ANDROID */
  784. priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  785. if (priv->sock < 0) {
  786. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  787. goto fail;
  788. }
  789. os_memset(&addr, 0, sizeof(addr));
  790. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  791. addr.sun_len = sizeof(addr);
  792. #endif /* __FreeBSD__ */
  793. addr.sun_family = AF_UNIX;
  794. if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
  795. addr.sun_path[0] = '\0';
  796. os_strlcpy(addr.sun_path + 1, ctrl + 10,
  797. sizeof(addr.sun_path) - 1);
  798. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
  799. 0) {
  800. wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
  801. "bind(PF_UNIX;%s) failed: %s",
  802. ctrl, strerror(errno));
  803. goto fail;
  804. }
  805. wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
  806. ctrl + 10);
  807. goto havesock;
  808. }
  809. os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
  810. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  811. wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
  812. ctrl, strerror(errno));
  813. if (connect(priv->sock, (struct sockaddr *) &addr,
  814. sizeof(addr)) < 0) {
  815. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  816. " allow connections - assuming it was left"
  817. "over from forced program termination");
  818. if (unlink(ctrl) < 0) {
  819. wpa_printf(MSG_ERROR,
  820. "Could not unlink existing ctrl_iface socket '%s': %s",
  821. ctrl, strerror(errno));
  822. goto fail;
  823. }
  824. if (bind(priv->sock, (struct sockaddr *) &addr,
  825. sizeof(addr)) < 0) {
  826. wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
  827. ctrl, strerror(errno));
  828. goto fail;
  829. }
  830. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  831. "ctrl_iface socket '%s'",
  832. ctrl);
  833. } else {
  834. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  835. "be in use - cannot override it");
  836. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  837. "not used anymore",
  838. ctrl);
  839. goto fail;
  840. }
  841. }
  842. wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
  843. if (global->params.ctrl_interface_group) {
  844. char *gid_str = global->params.ctrl_interface_group;
  845. gid_t gid = 0;
  846. struct group *grp;
  847. char *endp;
  848. grp = getgrnam(gid_str);
  849. if (grp) {
  850. gid = grp->gr_gid;
  851. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  852. " (from group name '%s')",
  853. (int) gid, gid_str);
  854. } else {
  855. /* Group name not found - try to parse this as gid */
  856. gid = strtol(gid_str, &endp, 10);
  857. if (*gid_str == '\0' || *endp != '\0') {
  858. wpa_printf(MSG_ERROR, "CTRL: Invalid group "
  859. "'%s'", gid_str);
  860. goto fail;
  861. }
  862. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  863. (int) gid);
  864. }
  865. if (chown(ctrl, -1, gid) < 0) {
  866. wpa_printf(MSG_ERROR,
  867. "chown[global_ctrl_interface=%s,gid=%d]: %s",
  868. ctrl, (int) gid, strerror(errno));
  869. goto fail;
  870. }
  871. if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
  872. wpa_printf(MSG_ERROR,
  873. "chmod[global_ctrl_interface=%s]: %s",
  874. ctrl, strerror(errno));
  875. goto fail;
  876. }
  877. } else {
  878. chmod(ctrl, S_IRWXU);
  879. }
  880. havesock:
  881. /*
  882. * Make socket non-blocking so that we don't hang forever if
  883. * target dies unexpectedly.
  884. */
  885. flags = fcntl(priv->sock, F_GETFL);
  886. if (flags >= 0) {
  887. flags |= O_NONBLOCK;
  888. if (fcntl(priv->sock, F_SETFL, flags) < 0) {
  889. wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
  890. strerror(errno));
  891. /* Not fatal, continue on.*/
  892. }
  893. }
  894. eloop_register_read_sock(priv->sock,
  895. wpa_supplicant_global_ctrl_iface_receive,
  896. global, priv);
  897. return 0;
  898. fail:
  899. if (priv->sock >= 0) {
  900. close(priv->sock);
  901. priv->sock = -1;
  902. }
  903. return -1;
  904. }
  905. struct ctrl_iface_global_priv *
  906. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  907. {
  908. struct ctrl_iface_global_priv *priv;
  909. priv = os_zalloc(sizeof(*priv));
  910. if (priv == NULL)
  911. return NULL;
  912. dl_list_init(&priv->ctrl_dst);
  913. priv->global = global;
  914. priv->sock = -1;
  915. if (global->params.ctrl_interface == NULL)
  916. return priv;
  917. if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
  918. os_free(priv);
  919. return NULL;
  920. }
  921. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  922. return priv;
  923. }
  924. static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
  925. struct ctrl_iface_global_priv *priv)
  926. {
  927. int res;
  928. if (priv->sock <= 0)
  929. return -1;
  930. eloop_unregister_read_sock(priv->sock);
  931. close(priv->sock);
  932. priv->sock = -1;
  933. res = wpas_global_ctrl_iface_open_sock(global, priv);
  934. if (res < 0)
  935. return -1;
  936. return priv->sock;
  937. }
  938. void
  939. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  940. {
  941. struct wpa_ctrl_dst *dst, *prev;
  942. if (priv->sock >= 0) {
  943. eloop_unregister_read_sock(priv->sock);
  944. close(priv->sock);
  945. }
  946. if (priv->global->params.ctrl_interface)
  947. unlink(priv->global->params.ctrl_interface);
  948. dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
  949. list)
  950. os_free(dst);
  951. os_free(priv);
  952. }