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