ctrl_iface_unix.c 35 KB

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