1 /* 2 * WPA Supplicant / privileged helper program 3 * Copyright (c) 2007-2009, 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 #ifdef __linux__ 11 #include <fcntl.h> 12 #endif /* __linux__ */ 13 #include <sys/un.h> 14 #include <sys/stat.h> 15 16 #include "common.h" 17 #include "eloop.h" 18 #include "common/version.h" 19 #include "drivers/driver.h" 20 #include "l2_packet/l2_packet.h" 21 #include "common/privsep_commands.h" 22 #include "common/ieee802_11_defs.h" 23 24 25 struct wpa_priv_interface { 26 struct wpa_priv_interface *next; 27 char *driver_name; 28 char *ifname; 29 char *sock_name; 30 int fd; 31 32 struct wpa_driver_ops *driver; 33 void *drv_priv; 34 struct sockaddr_un drv_addr; 35 int wpas_registered; 36 37 /* TODO: add support for multiple l2 connections */ 38 struct l2_packet_data *l2; 39 struct sockaddr_un l2_addr; 40 }; 41 42 43 static void wpa_priv_cmd_register(struct wpa_priv_interface *iface, 44 struct sockaddr_un *from) 45 { 46 if (iface->drv_priv) { 47 wpa_printf(MSG_DEBUG, "Cleaning up forgotten driver instance"); 48 if (iface->driver->deinit) 49 iface->driver->deinit(iface->drv_priv); 50 iface->drv_priv = NULL; 51 iface->wpas_registered = 0; 52 } 53 54 if (iface->l2) { 55 wpa_printf(MSG_DEBUG, "Cleaning up forgotten l2_packet " 56 "instance"); 57 l2_packet_deinit(iface->l2); 58 iface->l2 = NULL; 59 } 60 61 if (iface->driver->init == NULL) 62 return; 63 64 iface->drv_priv = iface->driver->init(iface, iface->ifname); 65 if (iface->drv_priv == NULL) { 66 wpa_printf(MSG_DEBUG, "Failed to initialize driver wrapper"); 67 return; 68 } 69 70 wpa_printf(MSG_DEBUG, "Driver wrapper '%s' initialized for interface " 71 "'%s'", iface->driver_name, iface->ifname); 72 73 os_memcpy(&iface->drv_addr, from, sizeof(iface->drv_addr)); 74 iface->wpas_registered = 1; 75 76 if (iface->driver->set_param && 77 iface->driver->set_param(iface->drv_priv, NULL) < 0) { 78 wpa_printf(MSG_ERROR, "Driver interface rejected param"); 79 } 80 } 81 82 83 static void wpa_priv_cmd_unregister(struct wpa_priv_interface *iface, 84 struct sockaddr_un *from) 85 { 86 if (iface->drv_priv) { 87 if (iface->driver->deinit) 88 iface->driver->deinit(iface->drv_priv); 89 iface->drv_priv = NULL; 90 iface->wpas_registered = 0; 91 } 92 } 93 94 95 static void wpa_priv_cmd_scan(struct wpa_priv_interface *iface, 96 char *buf, size_t len) 97 { 98 struct wpa_driver_scan_params params; 99 100 if (iface->drv_priv == NULL) 101 return; 102 103 os_memset(¶ms, 0, sizeof(params)); 104 if (len) { 105 params.ssids[0].ssid = (u8 *) buf; 106 params.ssids[0].ssid_len = len; 107 params.num_ssids = 1; 108 } 109 110 if (iface->driver->scan2) 111 iface->driver->scan2(iface->drv_priv, ¶ms); 112 } 113 114 115 static void wpa_priv_get_scan_results2(struct wpa_priv_interface *iface, 116 struct sockaddr_un *from) 117 { 118 struct wpa_scan_results *res; 119 u8 *buf = NULL, *pos, *end; 120 int val; 121 size_t i; 122 123 res = iface->driver->get_scan_results2(iface->drv_priv); 124 if (res == NULL) 125 goto fail; 126 127 buf = os_malloc(60000); 128 if (buf == NULL) 129 goto fail; 130 pos = buf; 131 end = buf + 60000; 132 val = res->num; 133 os_memcpy(pos, &val, sizeof(int)); 134 pos += sizeof(int); 135 136 for (i = 0; i < res->num; i++) { 137 struct wpa_scan_res *r = res->res[i]; 138 val = sizeof(*r) + r->ie_len; 139 if (end - pos < (int) sizeof(int) + val) 140 break; 141 os_memcpy(pos, &val, sizeof(int)); 142 pos += sizeof(int); 143 os_memcpy(pos, r, val); 144 pos += val; 145 } 146 147 sendto(iface->fd, buf, pos - buf, 0, (struct sockaddr *) from, 148 sizeof(*from)); 149 150 os_free(buf); 151 wpa_scan_results_free(res); 152 return; 153 154 fail: 155 os_free(buf); 156 wpa_scan_results_free(res); 157 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from)); 158 } 159 160 161 static void wpa_priv_cmd_get_scan_results(struct wpa_priv_interface *iface, 162 struct sockaddr_un *from) 163 { 164 if (iface->drv_priv == NULL) 165 return; 166 167 if (iface->driver->get_scan_results2) 168 wpa_priv_get_scan_results2(iface, from); 169 else 170 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, 171 sizeof(*from)); 172 } 173 174 175 static void wpa_priv_cmd_associate(struct wpa_priv_interface *iface, 176 void *buf, size_t len) 177 { 178 struct wpa_driver_associate_params params; 179 struct privsep_cmd_associate *assoc; 180 u8 *bssid; 181 int res; 182 183 if (iface->drv_priv == NULL || iface->driver->associate == NULL) 184 return; 185 186 if (len < sizeof(*assoc)) { 187 wpa_printf(MSG_DEBUG, "Invalid association request"); 188 return; 189 } 190 191 assoc = buf; 192 if (sizeof(*assoc) + assoc->wpa_ie_len > len) { 193 wpa_printf(MSG_DEBUG, "Association request overflow"); 194 return; 195 } 196 197 os_memset(¶ms, 0, sizeof(params)); 198 bssid = assoc->bssid; 199 if (bssid[0] | bssid[1] | bssid[2] | bssid[3] | bssid[4] | bssid[5]) 200 params.bssid = bssid; 201 params.ssid = assoc->ssid; 202 if (assoc->ssid_len > 32) 203 return; 204 params.ssid_len = assoc->ssid_len; 205 params.freq = assoc->freq; 206 if (assoc->wpa_ie_len) { 207 params.wpa_ie = (u8 *) (assoc + 1); 208 params.wpa_ie_len = assoc->wpa_ie_len; 209 } 210 params.pairwise_suite = assoc->pairwise_suite; 211 params.group_suite = assoc->group_suite; 212 params.key_mgmt_suite = assoc->key_mgmt_suite; 213 params.auth_alg = assoc->auth_alg; 214 params.mode = assoc->mode; 215 216 res = iface->driver->associate(iface->drv_priv, ¶ms); 217 wpa_printf(MSG_DEBUG, "drv->associate: res=%d", res); 218 } 219 220 221 static void wpa_priv_cmd_get_bssid(struct wpa_priv_interface *iface, 222 struct sockaddr_un *from) 223 { 224 u8 bssid[ETH_ALEN]; 225 226 if (iface->drv_priv == NULL) 227 goto fail; 228 229 if (iface->driver->get_bssid == NULL || 230 iface->driver->get_bssid(iface->drv_priv, bssid) < 0) 231 goto fail; 232 233 sendto(iface->fd, bssid, ETH_ALEN, 0, (struct sockaddr *) from, 234 sizeof(*from)); 235 return; 236 237 fail: 238 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from)); 239 } 240 241 242 static void wpa_priv_cmd_get_ssid(struct wpa_priv_interface *iface, 243 struct sockaddr_un *from) 244 { 245 u8 ssid[sizeof(int) + 32]; 246 int res; 247 248 if (iface->drv_priv == NULL) 249 goto fail; 250 251 if (iface->driver->get_ssid == NULL) 252 goto fail; 253 254 res = iface->driver->get_ssid(iface->drv_priv, &ssid[sizeof(int)]); 255 if (res < 0 || res > 32) 256 goto fail; 257 os_memcpy(ssid, &res, sizeof(int)); 258 259 sendto(iface->fd, ssid, sizeof(ssid), 0, (struct sockaddr *) from, 260 sizeof(*from)); 261 return; 262 263 fail: 264 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from)); 265 } 266 267 268 static void wpa_priv_cmd_set_key(struct wpa_priv_interface *iface, 269 void *buf, size_t len) 270 { 271 struct privsep_cmd_set_key *params; 272 int res; 273 274 if (iface->drv_priv == NULL || iface->driver->set_key == NULL) 275 return; 276 277 if (len != sizeof(*params)) { 278 wpa_printf(MSG_DEBUG, "Invalid set_key request"); 279 return; 280 } 281 282 params = buf; 283 284 res = iface->driver->set_key(iface->ifname, iface->drv_priv, 285 params->alg, 286 params->addr, params->key_idx, 287 params->set_tx, 288 params->seq_len ? params->seq : NULL, 289 params->seq_len, 290 params->key_len ? params->key : NULL, 291 params->key_len); 292 wpa_printf(MSG_DEBUG, "drv->set_key: res=%d", res); 293 } 294 295 296 static void wpa_priv_cmd_get_capa(struct wpa_priv_interface *iface, 297 struct sockaddr_un *from) 298 { 299 struct wpa_driver_capa capa; 300 301 if (iface->drv_priv == NULL) 302 goto fail; 303 304 if (iface->driver->get_capa == NULL || 305 iface->driver->get_capa(iface->drv_priv, &capa) < 0) 306 goto fail; 307 308 sendto(iface->fd, &capa, sizeof(capa), 0, (struct sockaddr *) from, 309 sizeof(*from)); 310 return; 311 312 fail: 313 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from)); 314 } 315 316 317 static void wpa_priv_l2_rx(void *ctx, const u8 *src_addr, const u8 *buf, 318 size_t len) 319 { 320 struct wpa_priv_interface *iface = ctx; 321 struct msghdr msg; 322 struct iovec io[2]; 323 324 io[0].iov_base = (u8 *) src_addr; 325 io[0].iov_len = ETH_ALEN; 326 io[1].iov_base = (u8 *) buf; 327 io[1].iov_len = len; 328 329 os_memset(&msg, 0, sizeof(msg)); 330 msg.msg_iov = io; 331 msg.msg_iovlen = 2; 332 msg.msg_name = &iface->l2_addr; 333 msg.msg_namelen = sizeof(iface->l2_addr); 334 335 if (sendmsg(iface->fd, &msg, 0) < 0) { 336 perror("sendmsg(l2 rx)"); 337 } 338 } 339 340 341 static void wpa_priv_cmd_l2_register(struct wpa_priv_interface *iface, 342 struct sockaddr_un *from, 343 void *buf, size_t len) 344 { 345 int *reg_cmd = buf; 346 u8 own_addr[ETH_ALEN]; 347 int res; 348 u16 proto; 349 350 if (len != 2 * sizeof(int)) { 351 wpa_printf(MSG_DEBUG, "Invalid l2_register length %lu", 352 (unsigned long) len); 353 return; 354 } 355 356 proto = reg_cmd[0]; 357 if (proto != ETH_P_EAPOL && proto != ETH_P_RSN_PREAUTH) { 358 wpa_printf(MSG_DEBUG, "Refused l2_packet connection for " 359 "ethertype 0x%x", proto); 360 return; 361 } 362 363 if (iface->l2) { 364 wpa_printf(MSG_DEBUG, "Cleaning up forgotten l2_packet " 365 "instance"); 366 l2_packet_deinit(iface->l2); 367 iface->l2 = NULL; 368 } 369 370 os_memcpy(&iface->l2_addr, from, sizeof(iface->l2_addr)); 371 372 iface->l2 = l2_packet_init(iface->ifname, NULL, proto, 373 wpa_priv_l2_rx, iface, reg_cmd[1]); 374 if (iface->l2 == NULL) { 375 wpa_printf(MSG_DEBUG, "Failed to initialize l2_packet " 376 "instance for protocol %d", proto); 377 return; 378 } 379 380 if (l2_packet_get_own_addr(iface->l2, own_addr) < 0) { 381 wpa_printf(MSG_DEBUG, "Failed to get own address from " 382 "l2_packet"); 383 l2_packet_deinit(iface->l2); 384 iface->l2 = NULL; 385 return; 386 } 387 388 res = sendto(iface->fd, own_addr, ETH_ALEN, 0, 389 (struct sockaddr *) from, sizeof(*from)); 390 wpa_printf(MSG_DEBUG, "L2 registration: res=%d", res); 391 } 392 393 394 static void wpa_priv_cmd_l2_unregister(struct wpa_priv_interface *iface, 395 struct sockaddr_un *from) 396 { 397 if (iface->l2) { 398 l2_packet_deinit(iface->l2); 399 iface->l2 = NULL; 400 } 401 } 402 403 404 static void wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface *iface, 405 struct sockaddr_un *from) 406 { 407 if (iface->l2) 408 l2_packet_notify_auth_start(iface->l2); 409 } 410 411 412 static void wpa_priv_cmd_l2_send(struct wpa_priv_interface *iface, 413 struct sockaddr_un *from, 414 void *buf, size_t len) 415 { 416 u8 *dst_addr; 417 u16 proto; 418 int res; 419 420 if (iface->l2 == NULL) 421 return; 422 423 if (len < ETH_ALEN + 2) { 424 wpa_printf(MSG_DEBUG, "Too short L2 send packet (len=%lu)", 425 (unsigned long) len); 426 return; 427 } 428 429 dst_addr = buf; 430 os_memcpy(&proto, buf + ETH_ALEN, 2); 431 432 if (proto != ETH_P_EAPOL && proto != ETH_P_RSN_PREAUTH) { 433 wpa_printf(MSG_DEBUG, "Refused l2_packet send for ethertype " 434 "0x%x", proto); 435 return; 436 } 437 438 res = l2_packet_send(iface->l2, dst_addr, proto, buf + ETH_ALEN + 2, 439 len - ETH_ALEN - 2); 440 wpa_printf(MSG_DEBUG, "L2 send: res=%d", res); 441 } 442 443 444 static void wpa_priv_cmd_set_country(struct wpa_priv_interface *iface, 445 char *buf) 446 { 447 if (iface->drv_priv == NULL || iface->driver->set_country == NULL || 448 *buf == '\0') 449 return; 450 451 iface->driver->set_country(iface->drv_priv, buf); 452 } 453 454 455 static void wpa_priv_receive(int sock, void *eloop_ctx, void *sock_ctx) 456 { 457 struct wpa_priv_interface *iface = eloop_ctx; 458 char buf[2000], *pos; 459 void *cmd_buf; 460 size_t cmd_len; 461 int res, cmd; 462 struct sockaddr_un from; 463 socklen_t fromlen = sizeof(from); 464 465 res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from, 466 &fromlen); 467 if (res < 0) { 468 perror("recvfrom"); 469 return; 470 } 471 472 if (res < (int) sizeof(int)) { 473 wpa_printf(MSG_DEBUG, "Too short command (len=%d)", res); 474 return; 475 } 476 477 os_memcpy(&cmd, buf, sizeof(int)); 478 wpa_printf(MSG_DEBUG, "Command %d for interface %s", 479 cmd, iface->ifname); 480 cmd_buf = &buf[sizeof(int)]; 481 cmd_len = res - sizeof(int); 482 483 switch (cmd) { 484 case PRIVSEP_CMD_REGISTER: 485 wpa_priv_cmd_register(iface, &from); 486 break; 487 case PRIVSEP_CMD_UNREGISTER: 488 wpa_priv_cmd_unregister(iface, &from); 489 break; 490 case PRIVSEP_CMD_SCAN: 491 wpa_priv_cmd_scan(iface, cmd_buf, cmd_len); 492 break; 493 case PRIVSEP_CMD_GET_SCAN_RESULTS: 494 wpa_priv_cmd_get_scan_results(iface, &from); 495 break; 496 case PRIVSEP_CMD_ASSOCIATE: 497 wpa_priv_cmd_associate(iface, cmd_buf, cmd_len); 498 break; 499 case PRIVSEP_CMD_GET_BSSID: 500 wpa_priv_cmd_get_bssid(iface, &from); 501 break; 502 case PRIVSEP_CMD_GET_SSID: 503 wpa_priv_cmd_get_ssid(iface, &from); 504 break; 505 case PRIVSEP_CMD_SET_KEY: 506 wpa_priv_cmd_set_key(iface, cmd_buf, cmd_len); 507 break; 508 case PRIVSEP_CMD_GET_CAPA: 509 wpa_priv_cmd_get_capa(iface, &from); 510 break; 511 case PRIVSEP_CMD_L2_REGISTER: 512 wpa_priv_cmd_l2_register(iface, &from, cmd_buf, cmd_len); 513 break; 514 case PRIVSEP_CMD_L2_UNREGISTER: 515 wpa_priv_cmd_l2_unregister(iface, &from); 516 break; 517 case PRIVSEP_CMD_L2_NOTIFY_AUTH_START: 518 wpa_priv_cmd_l2_notify_auth_start(iface, &from); 519 break; 520 case PRIVSEP_CMD_L2_SEND: 521 wpa_priv_cmd_l2_send(iface, &from, cmd_buf, cmd_len); 522 break; 523 case PRIVSEP_CMD_SET_COUNTRY: 524 pos = cmd_buf; 525 if (pos + cmd_len >= buf + sizeof(buf)) 526 break; 527 pos[cmd_len] = '\0'; 528 wpa_priv_cmd_set_country(iface, pos); 529 break; 530 } 531 } 532 533 534 static void wpa_priv_interface_deinit(struct wpa_priv_interface *iface) 535 { 536 if (iface->drv_priv && iface->driver->deinit) 537 iface->driver->deinit(iface->drv_priv); 538 539 if (iface->fd >= 0) { 540 eloop_unregister_read_sock(iface->fd); 541 close(iface->fd); 542 unlink(iface->sock_name); 543 } 544 545 if (iface->l2) 546 l2_packet_deinit(iface->l2); 547 548 os_free(iface->ifname); 549 os_free(iface->driver_name); 550 os_free(iface->sock_name); 551 os_free(iface); 552 } 553 554 555 extern struct wpa_driver_ops *wpa_drivers[]; 556 557 static struct wpa_priv_interface * 558 wpa_priv_interface_init(const char *dir, const char *params) 559 { 560 struct wpa_priv_interface *iface; 561 char *pos; 562 size_t len; 563 struct sockaddr_un addr; 564 int i; 565 566 pos = os_strchr(params, ':'); 567 if (pos == NULL) 568 return NULL; 569 570 iface = os_zalloc(sizeof(*iface)); 571 if (iface == NULL) 572 return NULL; 573 iface->fd = -1; 574 575 len = pos - params; 576 iface->driver_name = os_malloc(len + 1); 577 if (iface->driver_name == NULL) { 578 wpa_priv_interface_deinit(iface); 579 return NULL; 580 } 581 os_memcpy(iface->driver_name, params, len); 582 iface->driver_name[len] = '\0'; 583 584 for (i = 0; wpa_drivers[i]; i++) { 585 if (os_strcmp(iface->driver_name, 586 wpa_drivers[i]->name) == 0) { 587 iface->driver = wpa_drivers[i]; 588 break; 589 } 590 } 591 if (iface->driver == NULL) { 592 wpa_printf(MSG_ERROR, "Unsupported driver '%s'", 593 iface->driver_name); 594 wpa_priv_interface_deinit(iface); 595 return NULL; 596 } 597 598 pos++; 599 iface->ifname = os_strdup(pos); 600 if (iface->ifname == NULL) { 601 wpa_priv_interface_deinit(iface); 602 return NULL; 603 } 604 605 len = os_strlen(dir) + 1 + os_strlen(iface->ifname); 606 iface->sock_name = os_malloc(len + 1); 607 if (iface->sock_name == NULL) { 608 wpa_priv_interface_deinit(iface); 609 return NULL; 610 } 611 612 os_snprintf(iface->sock_name, len + 1, "%s/%s", dir, iface->ifname); 613 if (os_strlen(iface->sock_name) >= sizeof(addr.sun_path)) { 614 wpa_priv_interface_deinit(iface); 615 return NULL; 616 } 617 618 iface->fd = socket(PF_UNIX, SOCK_DGRAM, 0); 619 if (iface->fd < 0) { 620 perror("socket(PF_UNIX)"); 621 wpa_priv_interface_deinit(iface); 622 return NULL; 623 } 624 625 os_memset(&addr, 0, sizeof(addr)); 626 addr.sun_family = AF_UNIX; 627 os_strlcpy(addr.sun_path, iface->sock_name, sizeof(addr.sun_path)); 628 629 if (bind(iface->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 630 wpa_printf(MSG_DEBUG, "bind(PF_UNIX) failed: %s", 631 strerror(errno)); 632 if (connect(iface->fd, (struct sockaddr *) &addr, 633 sizeof(addr)) < 0) { 634 wpa_printf(MSG_DEBUG, "Socket exists, but does not " 635 "allow connections - assuming it was " 636 "leftover from forced program termination"); 637 if (unlink(iface->sock_name) < 0) { 638 perror("unlink[ctrl_iface]"); 639 wpa_printf(MSG_ERROR, "Could not unlink " 640 "existing ctrl_iface socket '%s'", 641 iface->sock_name); 642 goto fail; 643 } 644 if (bind(iface->fd, (struct sockaddr *) &addr, 645 sizeof(addr)) < 0) { 646 perror("wpa-priv-iface-init: bind(PF_UNIX)"); 647 goto fail; 648 } 649 wpa_printf(MSG_DEBUG, "Successfully replaced leftover " 650 "socket '%s'", iface->sock_name); 651 } else { 652 wpa_printf(MSG_INFO, "Socket exists and seems to be " 653 "in use - cannot override it"); 654 wpa_printf(MSG_INFO, "Delete '%s' manually if it is " 655 "not used anymore", iface->sock_name); 656 goto fail; 657 } 658 } 659 660 if (chmod(iface->sock_name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 661 perror("chmod"); 662 goto fail; 663 } 664 665 eloop_register_read_sock(iface->fd, wpa_priv_receive, iface, NULL); 666 667 return iface; 668 669 fail: 670 wpa_priv_interface_deinit(iface); 671 return NULL; 672 } 673 674 675 static int wpa_priv_send_event(struct wpa_priv_interface *iface, int event, 676 const void *data, size_t data_len) 677 { 678 struct msghdr msg; 679 struct iovec io[2]; 680 681 io[0].iov_base = &event; 682 io[0].iov_len = sizeof(event); 683 io[1].iov_base = (u8 *) data; 684 io[1].iov_len = data_len; 685 686 os_memset(&msg, 0, sizeof(msg)); 687 msg.msg_iov = io; 688 msg.msg_iovlen = data ? 2 : 1; 689 msg.msg_name = &iface->drv_addr; 690 msg.msg_namelen = sizeof(iface->drv_addr); 691 692 if (sendmsg(iface->fd, &msg, 0) < 0) { 693 perror("sendmsg(wpas_socket)"); 694 return -1; 695 } 696 697 return 0; 698 } 699 700 701 static void wpa_priv_send_assoc(struct wpa_priv_interface *iface, int event, 702 union wpa_event_data *data) 703 { 704 size_t buflen = 3 * sizeof(int); 705 u8 *buf, *pos; 706 int len; 707 708 if (data) { 709 buflen += data->assoc_info.req_ies_len + 710 data->assoc_info.resp_ies_len + 711 data->assoc_info.beacon_ies_len; 712 } 713 714 buf = os_malloc(buflen); 715 if (buf == NULL) 716 return; 717 718 pos = buf; 719 720 if (data && data->assoc_info.req_ies) { 721 len = data->assoc_info.req_ies_len; 722 os_memcpy(pos, &len, sizeof(int)); 723 pos += sizeof(int); 724 os_memcpy(pos, data->assoc_info.req_ies, len); 725 pos += len; 726 } else { 727 len = 0; 728 os_memcpy(pos, &len, sizeof(int)); 729 pos += sizeof(int); 730 } 731 732 if (data && data->assoc_info.resp_ies) { 733 len = data->assoc_info.resp_ies_len; 734 os_memcpy(pos, &len, sizeof(int)); 735 pos += sizeof(int); 736 os_memcpy(pos, data->assoc_info.resp_ies, len); 737 pos += len; 738 } else { 739 len = 0; 740 os_memcpy(pos, &len, sizeof(int)); 741 pos += sizeof(int); 742 } 743 744 if (data && data->assoc_info.beacon_ies) { 745 len = data->assoc_info.beacon_ies_len; 746 os_memcpy(pos, &len, sizeof(int)); 747 pos += sizeof(int); 748 os_memcpy(pos, data->assoc_info.beacon_ies, len); 749 pos += len; 750 } else { 751 len = 0; 752 os_memcpy(pos, &len, sizeof(int)); 753 pos += sizeof(int); 754 } 755 756 wpa_priv_send_event(iface, event, buf, buflen); 757 758 os_free(buf); 759 } 760 761 762 static void wpa_priv_send_interface_status(struct wpa_priv_interface *iface, 763 union wpa_event_data *data) 764 { 765 int ievent; 766 size_t len, maxlen; 767 u8 *buf; 768 char *ifname; 769 770 if (data == NULL) 771 return; 772 773 ievent = data->interface_status.ievent; 774 maxlen = sizeof(data->interface_status.ifname); 775 ifname = data->interface_status.ifname; 776 for (len = 0; len < maxlen && ifname[len]; len++) 777 ; 778 779 buf = os_malloc(sizeof(int) + len); 780 if (buf == NULL) 781 return; 782 783 os_memcpy(buf, &ievent, sizeof(int)); 784 os_memcpy(buf + sizeof(int), ifname, len); 785 786 wpa_priv_send_event(iface, PRIVSEP_EVENT_INTERFACE_STATUS, 787 buf, sizeof(int) + len); 788 789 os_free(buf); 790 791 } 792 793 794 static void wpa_priv_send_ft_response(struct wpa_priv_interface *iface, 795 union wpa_event_data *data) 796 { 797 size_t len; 798 u8 *buf, *pos; 799 800 if (data == NULL || data->ft_ies.ies == NULL) 801 return; 802 803 len = sizeof(int) + ETH_ALEN + data->ft_ies.ies_len; 804 buf = os_malloc(len); 805 if (buf == NULL) 806 return; 807 808 pos = buf; 809 os_memcpy(pos, &data->ft_ies.ft_action, sizeof(int)); 810 pos += sizeof(int); 811 os_memcpy(pos, data->ft_ies.target_ap, ETH_ALEN); 812 pos += ETH_ALEN; 813 os_memcpy(pos, data->ft_ies.ies, data->ft_ies.ies_len); 814 815 wpa_priv_send_event(iface, PRIVSEP_EVENT_FT_RESPONSE, buf, len); 816 817 os_free(buf); 818 819 } 820 821 822 void wpa_supplicant_event(void *ctx, enum wpa_event_type event, 823 union wpa_event_data *data) 824 { 825 struct wpa_priv_interface *iface = ctx; 826 827 wpa_printf(MSG_DEBUG, "%s - event=%d", __func__, event); 828 829 if (!iface->wpas_registered) { 830 wpa_printf(MSG_DEBUG, "Driver event received, but " 831 "wpa_supplicant not registered"); 832 return; 833 } 834 835 switch (event) { 836 case EVENT_ASSOC: 837 wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOC, data); 838 break; 839 case EVENT_DISASSOC: 840 wpa_priv_send_event(iface, PRIVSEP_EVENT_DISASSOC, NULL, 0); 841 break; 842 case EVENT_ASSOCINFO: 843 if (data == NULL) 844 return; 845 wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOCINFO, data); 846 break; 847 case EVENT_MICHAEL_MIC_FAILURE: 848 if (data == NULL) 849 return; 850 wpa_priv_send_event(iface, PRIVSEP_EVENT_MICHAEL_MIC_FAILURE, 851 &data->michael_mic_failure.unicast, 852 sizeof(int)); 853 break; 854 case EVENT_SCAN_RESULTS: 855 wpa_priv_send_event(iface, PRIVSEP_EVENT_SCAN_RESULTS, NULL, 856 0); 857 break; 858 case EVENT_INTERFACE_STATUS: 859 wpa_priv_send_interface_status(iface, data); 860 break; 861 case EVENT_PMKID_CANDIDATE: 862 if (data == NULL) 863 return; 864 wpa_priv_send_event(iface, PRIVSEP_EVENT_PMKID_CANDIDATE, 865 &data->pmkid_candidate, 866 sizeof(struct pmkid_candidate)); 867 break; 868 case EVENT_STKSTART: 869 if (data == NULL) 870 return; 871 wpa_priv_send_event(iface, PRIVSEP_EVENT_STKSTART, 872 &data->stkstart.peer, ETH_ALEN); 873 break; 874 case EVENT_FT_RESPONSE: 875 wpa_priv_send_ft_response(iface, data); 876 break; 877 default: 878 wpa_printf(MSG_DEBUG, "Unsupported driver event %d - TODO", 879 event); 880 break; 881 } 882 } 883 884 885 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr, 886 const u8 *buf, size_t len) 887 { 888 struct wpa_priv_interface *iface = ctx; 889 struct msghdr msg; 890 struct iovec io[3]; 891 int event = PRIVSEP_EVENT_RX_EAPOL; 892 893 wpa_printf(MSG_DEBUG, "RX EAPOL from driver"); 894 io[0].iov_base = &event; 895 io[0].iov_len = sizeof(event); 896 io[1].iov_base = (u8 *) src_addr; 897 io[1].iov_len = ETH_ALEN; 898 io[2].iov_base = (u8 *) buf; 899 io[2].iov_len = len; 900 901 os_memset(&msg, 0, sizeof(msg)); 902 msg.msg_iov = io; 903 msg.msg_iovlen = 3; 904 msg.msg_name = &iface->drv_addr; 905 msg.msg_namelen = sizeof(iface->drv_addr); 906 907 if (sendmsg(iface->fd, &msg, 0) < 0) 908 perror("sendmsg(wpas_socket)"); 909 } 910 911 912 static void wpa_priv_terminate(int sig, void *signal_ctx) 913 { 914 wpa_printf(MSG_DEBUG, "wpa_priv termination requested"); 915 eloop_terminate(); 916 } 917 918 919 static void wpa_priv_fd_workaround(void) 920 { 921 #ifdef __linux__ 922 int s, i; 923 /* When started from pcmcia-cs scripts, wpa_supplicant might start with 924 * fd 0, 1, and 2 closed. This will cause some issues because many 925 * places in wpa_supplicant are still printing out to stdout. As a 926 * workaround, make sure that fd's 0, 1, and 2 are not used for other 927 * sockets. */ 928 for (i = 0; i < 3; i++) { 929 s = open("/dev/null", O_RDWR); 930 if (s > 2) { 931 close(s); 932 break; 933 } 934 } 935 #endif /* __linux__ */ 936 } 937 938 939 static void usage(void) 940 { 941 printf("wpa_priv v" VERSION_STR "\n" 942 "Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi> and " 943 "contributors\n" 944 "\n" 945 "usage:\n" 946 " wpa_priv [-Bdd] [-P<pid file>] <driver:ifname> " 947 "[driver:ifname ...]\n"); 948 } 949 950 951 extern int wpa_debug_level; 952 953 int main(int argc, char *argv[]) 954 { 955 int c, i; 956 int ret = -1; 957 char *pid_file = NULL; 958 int daemonize = 0; 959 char *ctrl_dir = "/var/run/wpa_priv"; 960 struct wpa_priv_interface *interfaces = NULL, *iface; 961 962 if (os_program_init()) 963 return -1; 964 965 wpa_priv_fd_workaround(); 966 967 for (;;) { 968 c = getopt(argc, argv, "Bc:dP:"); 969 if (c < 0) 970 break; 971 switch (c) { 972 case 'B': 973 daemonize++; 974 break; 975 case 'c': 976 ctrl_dir = optarg; 977 break; 978 case 'd': 979 wpa_debug_level--; 980 break; 981 case 'P': 982 pid_file = os_rel2abs_path(optarg); 983 break; 984 default: 985 usage(); 986 goto out; 987 } 988 } 989 990 if (optind >= argc) { 991 usage(); 992 goto out; 993 } 994 995 wpa_printf(MSG_DEBUG, "wpa_priv control directory: '%s'", ctrl_dir); 996 997 if (eloop_init()) { 998 wpa_printf(MSG_ERROR, "Failed to initialize event loop"); 999 goto out; 1000 } 1001 1002 for (i = optind; i < argc; i++) { 1003 wpa_printf(MSG_DEBUG, "Adding driver:interface %s", argv[i]); 1004 iface = wpa_priv_interface_init(ctrl_dir, argv[i]); 1005 if (iface == NULL) 1006 goto out; 1007 iface->next = interfaces; 1008 interfaces = iface; 1009 } 1010 1011 if (daemonize && os_daemonize(pid_file)) 1012 goto out; 1013 1014 eloop_register_signal_terminate(wpa_priv_terminate, NULL); 1015 eloop_run(); 1016 1017 ret = 0; 1018 1019 out: 1020 iface = interfaces; 1021 while (iface) { 1022 struct wpa_priv_interface *prev = iface; 1023 iface = iface->next; 1024 wpa_priv_interface_deinit(prev); 1025 } 1026 1027 eloop_destroy(); 1028 1029 os_daemonize_terminate(pid_file); 1030 os_free(pid_file); 1031 os_program_deinit(); 1032 1033 return ret; 1034 } 1035