ctrl_iface_unix.c 31 KB

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