ctrl_iface_unix.c 35 KB

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