1 /* 2 * WPA Supplicant / UNIX domain socket -based control interface 3 * Copyright (c) 2004-2005, Jouni Malinen <j@w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #include "includes.h" 16 #include <sys/un.h> 17 #include <sys/stat.h> 18 #include <grp.h> 19 #include <stddef.h> 20 21 #include "common.h" 22 #include "eloop.h" 23 #include "config.h" 24 #include "eapol_supp/eapol_supp_sm.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 wpa_ctrl_dst *next; 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 wpa_ctrl_dst *ctrl_dst; 50 }; 51 52 53 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv, 54 int level, const char *buf, 55 size_t len); 56 57 58 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv, 59 struct sockaddr_un *from, 60 socklen_t fromlen) 61 { 62 struct wpa_ctrl_dst *dst; 63 64 dst = os_zalloc(sizeof(*dst)); 65 if (dst == NULL) 66 return -1; 67 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un)); 68 dst->addrlen = fromlen; 69 dst->debug_level = MSG_INFO; 70 dst->next = priv->ctrl_dst; 71 priv->ctrl_dst = dst; 72 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached", 73 (u8 *) from->sun_path, 74 fromlen - offsetof(struct sockaddr_un, sun_path)); 75 return 0; 76 } 77 78 79 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv, 80 struct sockaddr_un *from, 81 socklen_t fromlen) 82 { 83 struct wpa_ctrl_dst *dst, *prev = NULL; 84 85 dst = priv->ctrl_dst; 86 while (dst) { 87 if (fromlen == dst->addrlen && 88 os_memcmp(from->sun_path, dst->addr.sun_path, 89 fromlen - offsetof(struct sockaddr_un, sun_path)) == 0) { 90 if (prev == NULL) 91 priv->ctrl_dst = dst->next; 92 else 93 prev->next = dst->next; 94 os_free(dst); 95 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached", 96 (u8 *) from->sun_path, 97 fromlen - offsetof(struct sockaddr_un, sun_path)); 98 return 0; 99 } 100 prev = dst; 101 dst = dst->next; 102 } 103 return -1; 104 } 105 106 107 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv, 108 struct sockaddr_un *from, 109 socklen_t fromlen, 110 char *level) 111 { 112 struct wpa_ctrl_dst *dst; 113 114 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level); 115 116 dst = priv->ctrl_dst; 117 while (dst) { 118 if (fromlen == dst->addrlen && 119 os_memcmp(from->sun_path, dst->addr.sun_path, 120 fromlen - offsetof(struct sockaddr_un, sun_path)) == 0) { 121 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor " 122 "level", (u8 *) from->sun_path, 123 fromlen - offsetof(struct sockaddr_un, sun_path)); 124 dst->debug_level = atoi(level); 125 return 0; 126 } 127 dst = dst->next; 128 } 129 130 return -1; 131 } 132 133 134 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx, 135 void *sock_ctx) 136 { 137 struct wpa_supplicant *wpa_s = eloop_ctx; 138 struct ctrl_iface_priv *priv = sock_ctx; 139 char buf[256]; 140 int res; 141 struct sockaddr_un from; 142 socklen_t fromlen = sizeof(from); 143 char *reply = NULL; 144 size_t reply_len = 0; 145 int new_attached = 0; 146 147 res = recvfrom(sock, buf, sizeof(buf) - 1, 0, 148 (struct sockaddr *) &from, &fromlen); 149 if (res < 0) { 150 perror("recvfrom(ctrl_iface)"); 151 return; 152 } 153 buf[res] = '\0'; 154 155 if (os_strcmp(buf, "ATTACH") == 0) { 156 if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen)) 157 reply_len = 1; 158 else { 159 new_attached = 1; 160 reply_len = 2; 161 } 162 } else if (os_strcmp(buf, "DETACH") == 0) { 163 if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen)) 164 reply_len = 1; 165 else 166 reply_len = 2; 167 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { 168 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen, 169 buf + 6)) 170 reply_len = 1; 171 else 172 reply_len = 2; 173 } else { 174 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf, 175 &reply_len); 176 } 177 178 if (reply) { 179 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, 180 fromlen); 181 os_free(reply); 182 } else if (reply_len == 1) { 183 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, 184 fromlen); 185 } else if (reply_len == 2) { 186 sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from, 187 fromlen); 188 } 189 190 if (new_attached) 191 eapol_sm_notify_ctrl_attached(wpa_s->eapol); 192 } 193 194 195 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s) 196 { 197 char *buf; 198 size_t len; 199 char *pbuf, *dir = NULL, *gid_str = NULL; 200 int res; 201 202 if (wpa_s->conf->ctrl_interface == NULL) 203 return NULL; 204 205 pbuf = os_strdup(wpa_s->conf->ctrl_interface); 206 if (pbuf == NULL) 207 return NULL; 208 if (os_strncmp(pbuf, "DIR=", 4) == 0) { 209 dir = pbuf + 4; 210 gid_str = os_strstr(dir, " GROUP="); 211 if (gid_str) { 212 *gid_str = '\0'; 213 gid_str += 7; 214 } 215 } else 216 dir = pbuf; 217 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 225 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname); 226 if (res < 0 || (size_t) res >= len) { 227 os_free(pbuf); 228 os_free(buf); 229 return NULL; 230 } 231 #ifdef __CYGWIN__ 232 { 233 /* Windows/WinPcap uses interface names that are not suitable 234 * as a file name - convert invalid chars to underscores */ 235 char *pos = buf; 236 while (*pos) { 237 if (*pos == '\\') 238 *pos = '_'; 239 pos++; 240 } 241 } 242 #endif /* __CYGWIN__ */ 243 os_free(pbuf); 244 return buf; 245 } 246 247 248 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, 249 const char *txt, size_t len) 250 { 251 struct wpa_supplicant *wpa_s = ctx; 252 if (wpa_s == NULL || wpa_s->ctrl_iface == NULL) 253 return; 254 wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len); 255 } 256 257 258 struct ctrl_iface_priv * 259 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s) 260 { 261 struct ctrl_iface_priv *priv; 262 struct sockaddr_un addr; 263 char *fname = NULL; 264 gid_t gid = 0; 265 int gid_set = 0; 266 char *buf, *dir = NULL, *gid_str = NULL; 267 struct group *grp; 268 char *endp; 269 270 priv = os_zalloc(sizeof(*priv)); 271 if (priv == NULL) 272 return NULL; 273 priv->wpa_s = wpa_s; 274 priv->sock = -1; 275 276 if (wpa_s->conf->ctrl_interface == NULL) 277 return priv; 278 279 buf = os_strdup(wpa_s->conf->ctrl_interface); 280 if (buf == NULL) 281 goto fail; 282 if (os_strncmp(buf, "DIR=", 4) == 0) { 283 dir = buf + 4; 284 gid_str = os_strstr(dir, " GROUP="); 285 if (gid_str) { 286 *gid_str = '\0'; 287 gid_str += 7; 288 } 289 } else { 290 dir = buf; 291 gid_str = wpa_s->conf->ctrl_interface_group; 292 } 293 294 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) { 295 if (errno == EEXIST) { 296 wpa_printf(MSG_DEBUG, "Using existing control " 297 "interface directory."); 298 } else { 299 perror("mkdir[ctrl_interface]"); 300 goto fail; 301 } 302 } 303 304 if (gid_str) { 305 grp = getgrnam(gid_str); 306 if (grp) { 307 gid = grp->gr_gid; 308 gid_set = 1; 309 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d" 310 " (from group name '%s')", 311 (int) gid, gid_str); 312 } else { 313 /* Group name not found - try to parse this as gid */ 314 gid = strtol(gid_str, &endp, 10); 315 if (*gid_str == '\0' || *endp != '\0') { 316 wpa_printf(MSG_ERROR, "CTRL: Invalid group " 317 "'%s'", gid_str); 318 goto fail; 319 } 320 gid_set = 1; 321 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d", 322 (int) gid); 323 } 324 } 325 326 if (gid_set && chown(dir, -1, gid) < 0) { 327 perror("chown[ctrl_interface]"); 328 goto fail; 329 } 330 331 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >= 332 sizeof(addr.sun_path)) { 333 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded"); 334 goto fail; 335 } 336 337 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0); 338 if (priv->sock < 0) { 339 perror("socket(PF_UNIX)"); 340 goto fail; 341 } 342 343 os_memset(&addr, 0, sizeof(addr)); 344 /* XXX #ifdef */ 345 addr.sun_len = sizeof(addr); 346 addr.sun_family = AF_UNIX; 347 fname = wpa_supplicant_ctrl_iface_path(wpa_s); 348 if (fname == NULL) 349 goto fail; 350 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); 351 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 352 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", 353 strerror(errno)); 354 if (connect(priv->sock, (struct sockaddr *) &addr, 355 sizeof(addr)) < 0) { 356 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" 357 " allow connections - assuming it was left" 358 "over from forced program termination"); 359 if (unlink(fname) < 0) { 360 perror("unlink[ctrl_iface]"); 361 wpa_printf(MSG_ERROR, "Could not unlink " 362 "existing ctrl_iface socket '%s'", 363 fname); 364 goto fail; 365 } 366 if (bind(priv->sock, (struct sockaddr *) &addr, 367 sizeof(addr)) < 0) { 368 perror("bind(PF_UNIX)"); 369 goto fail; 370 } 371 wpa_printf(MSG_DEBUG, "Successfully replaced leftover " 372 "ctrl_iface socket '%s'", fname); 373 } else { 374 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " 375 "be in use - cannot override it"); 376 wpa_printf(MSG_INFO, "Delete '%s' manually if it is " 377 "not used anymore", fname); 378 os_free(fname); 379 fname = NULL; 380 goto fail; 381 } 382 } 383 384 if (gid_set && chown(fname, -1, gid) < 0) { 385 perror("chown[ctrl_interface/ifname]"); 386 goto fail; 387 } 388 389 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { 390 perror("chmod[ctrl_interface/ifname]"); 391 goto fail; 392 } 393 os_free(fname); 394 395 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive, 396 wpa_s, priv); 397 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb); 398 399 os_free(buf); 400 return priv; 401 402 fail: 403 if (priv->sock >= 0) 404 close(priv->sock); 405 os_free(priv); 406 if (fname) { 407 unlink(fname); 408 os_free(fname); 409 } 410 os_free(buf); 411 return NULL; 412 } 413 414 415 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv) 416 { 417 struct wpa_ctrl_dst *dst, *prev; 418 419 if (priv->sock > -1) { 420 char *fname; 421 char *buf, *dir = NULL, *gid_str = NULL; 422 eloop_unregister_read_sock(priv->sock); 423 if (priv->ctrl_dst) { 424 /* 425 * Wait a second before closing the control socket if 426 * there are any attached monitors in order to allow 427 * them to receive any pending messages. 428 */ 429 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached " 430 "monitors to receive messages"); 431 os_sleep(1, 0); 432 } 433 close(priv->sock); 434 priv->sock = -1; 435 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s); 436 if (fname) { 437 unlink(fname); 438 os_free(fname); 439 } 440 441 buf = os_strdup(priv->wpa_s->conf->ctrl_interface); 442 if (buf == NULL) 443 goto free_dst; 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 454 if (rmdir(dir) < 0) { 455 if (errno == ENOTEMPTY) { 456 wpa_printf(MSG_DEBUG, "Control interface " 457 "directory not empty - leaving it " 458 "behind"); 459 } else { 460 perror("rmdir[ctrl_interface]"); 461 } 462 } 463 os_free(buf); 464 } 465 466 free_dst: 467 dst = priv->ctrl_dst; 468 while (dst) { 469 prev = dst; 470 dst = dst->next; 471 os_free(prev); 472 } 473 os_free(priv); 474 } 475 476 477 /** 478 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors 479 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init() 480 * @level: Priority level of the message 481 * @buf: Message data 482 * @len: Message length 483 * 484 * Send a packet to all monitor programs attached to the control interface. 485 */ 486 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv, 487 int level, const char *buf, 488 size_t len) 489 { 490 struct wpa_ctrl_dst *dst, *next; 491 char levelstr[10]; 492 int idx, res; 493 struct msghdr msg; 494 struct iovec io[2]; 495 496 dst = priv->ctrl_dst; 497 if (priv->sock < 0 || dst == NULL) 498 return; 499 500 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); 501 if (res < 0 || (size_t) res >= sizeof(levelstr)) 502 return; 503 io[0].iov_base = levelstr; 504 io[0].iov_len = os_strlen(levelstr); 505 io[1].iov_base = (char *) buf; 506 io[1].iov_len = len; 507 os_memset(&msg, 0, sizeof(msg)); 508 msg.msg_iov = io; 509 msg.msg_iovlen = 2; 510 511 idx = 0; 512 while (dst) { 513 next = dst->next; 514 if (level >= dst->debug_level) { 515 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send", 516 (u8 *) dst->addr.sun_path, 517 dst->addrlen - offsetof(struct sockaddr_un, sun_path)); 518 msg.msg_name = (void *) &dst->addr; 519 msg.msg_namelen = dst->addrlen; 520 if (sendmsg(priv->sock, &msg, 0) < 0) { 521 perror("sendmsg(CTRL_IFACE monitor)"); 522 dst->errors++; 523 if (dst->errors > 10) { 524 wpa_supplicant_ctrl_iface_detach( 525 priv, &dst->addr, 526 dst->addrlen); 527 } 528 } else 529 dst->errors = 0; 530 } 531 idx++; 532 dst = next; 533 } 534 } 535 536 537 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv) 538 { 539 char buf[256]; 540 int res; 541 struct sockaddr_un from; 542 socklen_t fromlen = sizeof(from); 543 544 for (;;) { 545 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to " 546 "attach", priv->wpa_s->ifname); 547 eloop_wait_for_read_sock(priv->sock); 548 549 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0, 550 (struct sockaddr *) &from, &fromlen); 551 if (res < 0) { 552 perror("recvfrom(ctrl_iface)"); 553 continue; 554 } 555 buf[res] = '\0'; 556 557 if (os_strcmp(buf, "ATTACH") == 0) { 558 /* handle ATTACH signal of first monitor interface */ 559 if (!wpa_supplicant_ctrl_iface_attach(priv, &from, 560 fromlen)) { 561 sendto(priv->sock, "OK\n", 3, 0, 562 (struct sockaddr *) &from, fromlen); 563 /* OK to continue */ 564 return; 565 } else { 566 sendto(priv->sock, "FAIL\n", 5, 0, 567 (struct sockaddr *) &from, fromlen); 568 } 569 } else { 570 /* return FAIL for all other signals */ 571 sendto(priv->sock, "FAIL\n", 5, 0, 572 (struct sockaddr *) &from, fromlen); 573 } 574 } 575 } 576 577 578 /* Global ctrl_iface */ 579 580 struct ctrl_iface_global_priv { 581 struct wpa_global *global; 582 int sock; 583 }; 584 585 586 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx, 587 void *sock_ctx) 588 { 589 struct wpa_global *global = eloop_ctx; 590 char buf[256]; 591 int res; 592 struct sockaddr_un from; 593 socklen_t fromlen = sizeof(from); 594 char *reply; 595 size_t reply_len; 596 597 res = recvfrom(sock, buf, sizeof(buf) - 1, 0, 598 (struct sockaddr *) &from, &fromlen); 599 if (res < 0) { 600 perror("recvfrom(ctrl_iface)"); 601 return; 602 } 603 buf[res] = '\0'; 604 605 reply = wpa_supplicant_global_ctrl_iface_process(global, buf, 606 &reply_len); 607 608 if (reply) { 609 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, 610 fromlen); 611 os_free(reply); 612 } else if (reply_len) { 613 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, 614 fromlen); 615 } 616 } 617 618 619 struct ctrl_iface_global_priv * 620 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global) 621 { 622 struct ctrl_iface_global_priv *priv; 623 struct sockaddr_un addr; 624 625 priv = os_zalloc(sizeof(*priv)); 626 if (priv == NULL) 627 return NULL; 628 priv->global = global; 629 priv->sock = -1; 630 631 if (global->params.ctrl_interface == NULL) 632 return priv; 633 634 wpa_printf(MSG_DEBUG, "Global control interface '%s'", 635 global->params.ctrl_interface); 636 637 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0); 638 if (priv->sock < 0) { 639 perror("socket(PF_UNIX)"); 640 goto fail; 641 } 642 643 os_memset(&addr, 0, sizeof(addr)); 644 /* XXX #ifdef */ 645 addr.sun_len = sizeof(addr); 646 addr.sun_family = AF_UNIX; 647 os_strlcpy(addr.sun_path, global->params.ctrl_interface, 648 sizeof(addr.sun_path)); 649 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 650 perror("bind(PF_UNIX)"); 651 if (connect(priv->sock, (struct sockaddr *) &addr, 652 sizeof(addr)) < 0) { 653 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" 654 " allow connections - assuming it was left" 655 "over from forced program termination"); 656 if (unlink(global->params.ctrl_interface) < 0) { 657 perror("unlink[ctrl_iface]"); 658 wpa_printf(MSG_ERROR, "Could not unlink " 659 "existing ctrl_iface socket '%s'", 660 global->params.ctrl_interface); 661 goto fail; 662 } 663 if (bind(priv->sock, (struct sockaddr *) &addr, 664 sizeof(addr)) < 0) { 665 perror("bind(PF_UNIX)"); 666 goto fail; 667 } 668 wpa_printf(MSG_DEBUG, "Successfully replaced leftover " 669 "ctrl_iface socket '%s'", 670 global->params.ctrl_interface); 671 } else { 672 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " 673 "be in use - cannot override it"); 674 wpa_printf(MSG_INFO, "Delete '%s' manually if it is " 675 "not used anymore", 676 global->params.ctrl_interface); 677 goto fail; 678 } 679 } 680 681 eloop_register_read_sock(priv->sock, 682 wpa_supplicant_global_ctrl_iface_receive, 683 global, NULL); 684 685 return priv; 686 687 fail: 688 if (priv->sock >= 0) 689 close(priv->sock); 690 os_free(priv); 691 return NULL; 692 } 693 694 695 void 696 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv) 697 { 698 if (priv->sock >= 0) { 699 eloop_unregister_read_sock(priv->sock); 700 close(priv->sock); 701 } 702 if (priv->global->params.ctrl_interface) 703 unlink(priv->global->params.ctrl_interface); 704 os_free(priv); 705 } 706