1 /* 2 * Testing driver interface for a simulated network driver 3 * Copyright (c) 2004-2010, Jouni Malinen <j (at) 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 /* Make sure we get winsock2.h for Windows build to get sockaddr_storage */ 10 #include "build_config.h" 11 #ifdef CONFIG_NATIVE_WINDOWS 12 #include <winsock2.h> 13 #endif /* CONFIG_NATIVE_WINDOWS */ 14 15 #include "utils/includes.h" 16 17 #ifndef CONFIG_NATIVE_WINDOWS 18 #include <sys/un.h> 19 #include <dirent.h> 20 #include <sys/stat.h> 21 #define DRIVER_TEST_UNIX 22 #endif /* CONFIG_NATIVE_WINDOWS */ 23 24 #include "utils/common.h" 25 #include "utils/eloop.h" 26 #include "utils/list.h" 27 #include "utils/trace.h" 28 #include "common/ieee802_11_defs.h" 29 #include "crypto/sha1.h" 30 #include "l2_packet/l2_packet.h" 31 #include "p2p/p2p.h" 32 #include "wps/wps.h" 33 #include "driver.h" 34 35 36 struct test_client_socket { 37 struct test_client_socket *next; 38 u8 addr[ETH_ALEN]; 39 struct sockaddr_un un; 40 socklen_t unlen; 41 struct test_driver_bss *bss; 42 }; 43 44 struct test_driver_bss { 45 struct wpa_driver_test_data *drv; 46 struct dl_list list; 47 void *bss_ctx; 48 char ifname[IFNAMSIZ]; 49 u8 bssid[ETH_ALEN]; 50 u8 *ie; 51 size_t ielen; 52 u8 *wps_beacon_ie; 53 size_t wps_beacon_ie_len; 54 u8 *wps_probe_resp_ie; 55 size_t wps_probe_resp_ie_len; 56 u8 ssid[32]; 57 size_t ssid_len; 58 int privacy; 59 }; 60 61 struct wpa_driver_test_global { 62 int bss_add_used; 63 u8 req_addr[ETH_ALEN]; 64 }; 65 66 struct wpa_driver_test_data { 67 struct wpa_driver_test_global *global; 68 void *ctx; 69 WPA_TRACE_REF(ctx); 70 u8 own_addr[ETH_ALEN]; 71 int test_socket; 72 #ifdef DRIVER_TEST_UNIX 73 struct sockaddr_un hostapd_addr; 74 #endif /* DRIVER_TEST_UNIX */ 75 int hostapd_addr_set; 76 struct sockaddr_in hostapd_addr_udp; 77 int hostapd_addr_udp_set; 78 char *own_socket_path; 79 char *test_dir; 80 #define MAX_SCAN_RESULTS 30 81 struct wpa_scan_res *scanres[MAX_SCAN_RESULTS]; 82 size_t num_scanres; 83 int use_associnfo; 84 u8 assoc_wpa_ie[80]; 85 size_t assoc_wpa_ie_len; 86 int associated; 87 u8 *probe_req_ie; 88 size_t probe_req_ie_len; 89 u8 probe_req_ssid[32]; 90 size_t probe_req_ssid_len; 91 int ibss; 92 int ap; 93 94 struct test_client_socket *cli; 95 struct dl_list bss; 96 int udp_port; 97 98 int alloc_iface_idx; 99 100 int probe_req_report; 101 unsigned int remain_on_channel_freq; 102 unsigned int remain_on_channel_duration; 103 104 int current_freq; 105 106 struct p2p_data *p2p; 107 unsigned int off_channel_freq; 108 struct wpabuf *pending_action_tx; 109 u8 pending_action_src[ETH_ALEN]; 110 u8 pending_action_dst[ETH_ALEN]; 111 u8 pending_action_bssid[ETH_ALEN]; 112 unsigned int pending_action_freq; 113 unsigned int pending_action_no_cck; 114 unsigned int pending_listen_freq; 115 unsigned int pending_listen_duration; 116 int pending_p2p_scan; 117 struct sockaddr *probe_from; 118 socklen_t probe_from_len; 119 }; 120 121 122 static void wpa_driver_test_deinit(void *priv); 123 static int wpa_driver_test_attach(struct wpa_driver_test_data *drv, 124 const char *dir, int ap); 125 static void wpa_driver_test_close_test_socket( 126 struct wpa_driver_test_data *drv); 127 static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx); 128 static int wpa_driver_test_init_p2p(struct wpa_driver_test_data *drv); 129 130 131 static void test_driver_free_bss(struct test_driver_bss *bss) 132 { 133 os_free(bss->ie); 134 os_free(bss->wps_beacon_ie); 135 os_free(bss->wps_probe_resp_ie); 136 os_free(bss); 137 } 138 139 140 static void test_driver_free_bsses(struct wpa_driver_test_data *drv) 141 { 142 struct test_driver_bss *bss, *tmp; 143 144 dl_list_for_each_safe(bss, tmp, &drv->bss, struct test_driver_bss, 145 list) { 146 dl_list_del(&bss->list); 147 test_driver_free_bss(bss); 148 } 149 } 150 151 152 static struct test_client_socket * 153 test_driver_get_cli(struct wpa_driver_test_data *drv, struct sockaddr_un *from, 154 socklen_t fromlen) 155 { 156 struct test_client_socket *cli = drv->cli; 157 158 while (cli) { 159 if (cli->unlen == fromlen && 160 strncmp(cli->un.sun_path, from->sun_path, 161 fromlen - sizeof(cli->un.sun_family)) == 0) 162 return cli; 163 cli = cli->next; 164 } 165 166 return NULL; 167 } 168 169 170 static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data, 171 size_t data_len, int encrypt, 172 const u8 *own_addr, u32 flags) 173 { 174 struct test_driver_bss *dbss = priv; 175 struct wpa_driver_test_data *drv = dbss->drv; 176 struct test_client_socket *cli; 177 struct msghdr msg; 178 struct iovec io[3]; 179 struct l2_ethhdr eth; 180 181 if (drv->test_socket < 0) 182 return -1; 183 184 cli = drv->cli; 185 while (cli) { 186 if (memcmp(cli->addr, addr, ETH_ALEN) == 0) 187 break; 188 cli = cli->next; 189 } 190 191 if (!cli) { 192 wpa_printf(MSG_DEBUG, "%s: no destination client entry", 193 __func__); 194 return -1; 195 } 196 197 memcpy(eth.h_dest, addr, ETH_ALEN); 198 memcpy(eth.h_source, own_addr, ETH_ALEN); 199 eth.h_proto = host_to_be16(ETH_P_EAPOL); 200 201 io[0].iov_base = "EAPOL "; 202 io[0].iov_len = 6; 203 io[1].iov_base = ð 204 io[1].iov_len = sizeof(eth); 205 io[2].iov_base = (u8 *) data; 206 io[2].iov_len = data_len; 207 208 memset(&msg, 0, sizeof(msg)); 209 msg.msg_iov = io; 210 msg.msg_iovlen = 3; 211 msg.msg_name = &cli->un; 212 msg.msg_namelen = cli->unlen; 213 return sendmsg(drv->test_socket, &msg, 0); 214 } 215 216 217 static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src, 218 u16 proto, const u8 *data, size_t data_len) 219 { 220 struct test_driver_bss *dbss = priv; 221 struct wpa_driver_test_data *drv = dbss->drv; 222 struct msghdr msg; 223 struct iovec io[3]; 224 struct l2_ethhdr eth; 225 char desttxt[30]; 226 struct sockaddr_un addr; 227 struct dirent *dent; 228 DIR *dir; 229 int ret = 0, broadcast = 0, count = 0; 230 231 if (drv->test_socket < 0 || drv->test_dir == NULL) { 232 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d " 233 "test_dir=%p)", 234 __func__, drv->test_socket, drv->test_dir); 235 return -1; 236 } 237 238 broadcast = memcmp(dst, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0; 239 snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dst)); 240 241 memcpy(eth.h_dest, dst, ETH_ALEN); 242 memcpy(eth.h_source, src, ETH_ALEN); 243 eth.h_proto = host_to_be16(proto); 244 245 io[0].iov_base = "ETHER "; 246 io[0].iov_len = 6; 247 io[1].iov_base = ð 248 io[1].iov_len = sizeof(eth); 249 io[2].iov_base = (u8 *) data; 250 io[2].iov_len = data_len; 251 252 memset(&msg, 0, sizeof(msg)); 253 msg.msg_iov = io; 254 msg.msg_iovlen = 3; 255 256 dir = opendir(drv->test_dir); 257 if (dir == NULL) { 258 perror("test_driver: opendir"); 259 return -1; 260 } 261 while ((dent = readdir(dir))) { 262 #ifdef _DIRENT_HAVE_D_TYPE 263 /* Skip the file if it is not a socket. Also accept 264 * DT_UNKNOWN (0) in case the C library or underlying file 265 * system does not support d_type. */ 266 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN) 267 continue; 268 #endif /* _DIRENT_HAVE_D_TYPE */ 269 if (strcmp(dent->d_name, ".") == 0 || 270 strcmp(dent->d_name, "..") == 0) 271 continue; 272 273 memset(&addr, 0, sizeof(addr)); 274 addr.sun_family = AF_UNIX; 275 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", 276 drv->test_dir, dent->d_name); 277 278 if (strcmp(addr.sun_path, drv->own_socket_path) == 0) 279 continue; 280 if (!broadcast && strstr(dent->d_name, desttxt) == NULL) 281 continue; 282 283 wpa_printf(MSG_DEBUG, "%s: Send ether frame to %s", 284 __func__, dent->d_name); 285 286 msg.msg_name = &addr; 287 msg.msg_namelen = sizeof(addr); 288 ret = sendmsg(drv->test_socket, &msg, 0); 289 if (ret < 0) 290 perror("driver_test: sendmsg"); 291 count++; 292 } 293 closedir(dir); 294 295 if (!broadcast && count == 0) { 296 wpa_printf(MSG_DEBUG, "%s: Destination " MACSTR " not found", 297 __func__, MAC2STR(dst)); 298 return -1; 299 } 300 301 return ret; 302 } 303 304 305 static int wpa_driver_test_send_mlme(void *priv, const u8 *data, 306 size_t data_len, int noack) 307 { 308 struct test_driver_bss *dbss = priv; 309 struct wpa_driver_test_data *drv = dbss->drv; 310 struct msghdr msg; 311 struct iovec io[2]; 312 const u8 *dest; 313 struct sockaddr_un addr; 314 struct dirent *dent; 315 DIR *dir; 316 int broadcast; 317 int ret = 0; 318 struct ieee80211_hdr *hdr; 319 u16 fc; 320 char cmd[50]; 321 int freq; 322 #ifdef HOSTAPD 323 char desttxt[30]; 324 #endif /* HOSTAPD */ 325 union wpa_event_data event; 326 327 wpa_hexdump(MSG_MSGDUMP, "test_send_mlme", data, data_len); 328 if (drv->test_socket < 0 || data_len < 10) { 329 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d len=%lu" 330 " test_dir=%p)", 331 __func__, drv->test_socket, 332 (unsigned long) data_len, 333 drv->test_dir); 334 return -1; 335 } 336 337 dest = data + 4; 338 broadcast = os_memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0; 339 340 #ifdef HOSTAPD 341 snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dest)); 342 #endif /* HOSTAPD */ 343 344 if (drv->remain_on_channel_freq) 345 freq = drv->remain_on_channel_freq; 346 else 347 freq = drv->current_freq; 348 wpa_printf(MSG_DEBUG, "test_driver(%s): MLME TX on freq %d MHz", 349 dbss->ifname, freq); 350 os_snprintf(cmd, sizeof(cmd), "MLME freq=%d ", freq); 351 io[0].iov_base = cmd; 352 io[0].iov_len = os_strlen(cmd); 353 io[1].iov_base = (void *) data; 354 io[1].iov_len = data_len; 355 356 os_memset(&msg, 0, sizeof(msg)); 357 msg.msg_iov = io; 358 msg.msg_iovlen = 2; 359 360 #ifdef HOSTAPD 361 if (drv->test_dir == NULL) { 362 wpa_printf(MSG_DEBUG, "%s: test_dir == NULL", __func__); 363 return -1; 364 } 365 366 dir = opendir(drv->test_dir); 367 if (dir == NULL) { 368 perror("test_driver: opendir"); 369 return -1; 370 } 371 while ((dent = readdir(dir))) { 372 #ifdef _DIRENT_HAVE_D_TYPE 373 /* Skip the file if it is not a socket. Also accept 374 * DT_UNKNOWN (0) in case the C library or underlying file 375 * system does not support d_type. */ 376 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN) 377 continue; 378 #endif /* _DIRENT_HAVE_D_TYPE */ 379 if (os_strcmp(dent->d_name, ".") == 0 || 380 os_strcmp(dent->d_name, "..") == 0) 381 continue; 382 383 os_memset(&addr, 0, sizeof(addr)); 384 addr.sun_family = AF_UNIX; 385 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", 386 drv->test_dir, dent->d_name); 387 388 if (os_strcmp(addr.sun_path, drv->own_socket_path) == 0) 389 continue; 390 if (!broadcast && os_strstr(dent->d_name, desttxt) == NULL) 391 continue; 392 393 wpa_printf(MSG_DEBUG, "%s: Send management frame to %s", 394 __func__, dent->d_name); 395 396 msg.msg_name = &addr; 397 msg.msg_namelen = sizeof(addr); 398 ret = sendmsg(drv->test_socket, &msg, 0); 399 if (ret < 0) 400 perror("driver_test: sendmsg(test_socket)"); 401 } 402 closedir(dir); 403 #else /* HOSTAPD */ 404 405 if (os_memcmp(dest, dbss->bssid, ETH_ALEN) == 0 || 406 drv->test_dir == NULL) { 407 if (drv->hostapd_addr_udp_set) { 408 msg.msg_name = &drv->hostapd_addr_udp; 409 msg.msg_namelen = sizeof(drv->hostapd_addr_udp); 410 } else { 411 #ifdef DRIVER_TEST_UNIX 412 msg.msg_name = &drv->hostapd_addr; 413 msg.msg_namelen = sizeof(drv->hostapd_addr); 414 #endif /* DRIVER_TEST_UNIX */ 415 } 416 } else if (broadcast) { 417 dir = opendir(drv->test_dir); 418 if (dir == NULL) 419 return -1; 420 while ((dent = readdir(dir))) { 421 #ifdef _DIRENT_HAVE_D_TYPE 422 /* Skip the file if it is not a socket. 423 * Also accept DT_UNKNOWN (0) in case 424 * the C library or underlying file 425 * system does not support d_type. */ 426 if (dent->d_type != DT_SOCK && 427 dent->d_type != DT_UNKNOWN) 428 continue; 429 #endif /* _DIRENT_HAVE_D_TYPE */ 430 if (os_strcmp(dent->d_name, ".") == 0 || 431 os_strcmp(dent->d_name, "..") == 0) 432 continue; 433 wpa_printf(MSG_DEBUG, "%s: Send broadcast MLME to %s", 434 __func__, dent->d_name); 435 os_memset(&addr, 0, sizeof(addr)); 436 addr.sun_family = AF_UNIX; 437 os_snprintf(addr.sun_path, sizeof(addr.sun_path), 438 "%s/%s", drv->test_dir, dent->d_name); 439 440 msg.msg_name = &addr; 441 msg.msg_namelen = sizeof(addr); 442 443 ret = sendmsg(drv->test_socket, &msg, 0); 444 if (ret < 0) 445 perror("driver_test: sendmsg(test_socket)"); 446 } 447 closedir(dir); 448 return ret; 449 } else { 450 struct stat st; 451 os_memset(&addr, 0, sizeof(addr)); 452 addr.sun_family = AF_UNIX; 453 os_snprintf(addr.sun_path, sizeof(addr.sun_path), 454 "%s/AP-" MACSTR, drv->test_dir, MAC2STR(dest)); 455 if (stat(addr.sun_path, &st) < 0) { 456 os_snprintf(addr.sun_path, sizeof(addr.sun_path), 457 "%s/STA-" MACSTR, 458 drv->test_dir, MAC2STR(dest)); 459 } 460 msg.msg_name = &addr; 461 msg.msg_namelen = sizeof(addr); 462 } 463 464 if (sendmsg(drv->test_socket, &msg, 0) < 0) { 465 perror("sendmsg(test_socket)"); 466 return -1; 467 } 468 #endif /* HOSTAPD */ 469 470 hdr = (struct ieee80211_hdr *) data; 471 fc = le_to_host16(hdr->frame_control); 472 473 os_memset(&event, 0, sizeof(event)); 474 event.tx_status.type = WLAN_FC_GET_TYPE(fc); 475 event.tx_status.stype = WLAN_FC_GET_STYPE(fc); 476 event.tx_status.dst = hdr->addr1; 477 event.tx_status.data = data; 478 event.tx_status.data_len = data_len; 479 event.tx_status.ack = ret >= 0; 480 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event); 481 482 #ifdef CONFIG_P2P 483 if (drv->p2p && 484 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && 485 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) { 486 if (drv->pending_action_tx == NULL) { 487 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - " 488 "no pending operation"); 489 return ret; 490 } 491 492 if (os_memcmp(hdr->addr1, drv->pending_action_dst, ETH_ALEN) != 493 0) { 494 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - " 495 "unknown destination address"); 496 return ret; 497 } 498 499 wpabuf_free(drv->pending_action_tx); 500 drv->pending_action_tx = NULL; 501 502 p2p_send_action_cb(drv->p2p, drv->pending_action_freq, 503 drv->pending_action_dst, 504 drv->pending_action_src, 505 drv->pending_action_bssid, 506 ret >= 0); 507 } 508 #endif /* CONFIG_P2P */ 509 510 return ret; 511 } 512 513 514 static void test_driver_scan(struct wpa_driver_test_data *drv, 515 struct sockaddr_un *from, socklen_t fromlen, 516 char *data) 517 { 518 char buf[512], *pos, *end; 519 int ret; 520 struct test_driver_bss *bss; 521 u8 sa[ETH_ALEN]; 522 u8 ie[512]; 523 size_t ielen; 524 union wpa_event_data event; 525 526 /* data: optional [ ' ' | STA-addr | ' ' | IEs(hex) ] */ 527 528 wpa_printf(MSG_DEBUG, "test_driver: SCAN"); 529 530 if (*data) { 531 if (*data != ' ' || 532 hwaddr_aton(data + 1, sa)) { 533 wpa_printf(MSG_DEBUG, "test_driver: Unexpected SCAN " 534 "command format"); 535 return; 536 } 537 538 data += 18; 539 while (*data == ' ') 540 data++; 541 ielen = os_strlen(data) / 2; 542 if (ielen > sizeof(ie)) 543 ielen = sizeof(ie); 544 if (hexstr2bin(data, ie, ielen) < 0) 545 ielen = 0; 546 547 wpa_printf(MSG_DEBUG, "test_driver: Scan from " MACSTR, 548 MAC2STR(sa)); 549 wpa_hexdump(MSG_MSGDUMP, "test_driver: scan IEs", ie, ielen); 550 551 os_memset(&event, 0, sizeof(event)); 552 event.rx_probe_req.sa = sa; 553 event.rx_probe_req.ie = ie; 554 event.rx_probe_req.ie_len = ielen; 555 wpa_supplicant_event(drv->ctx, EVENT_RX_PROBE_REQ, &event); 556 #ifdef CONFIG_P2P 557 if (drv->p2p) 558 p2p_probe_req_rx(drv->p2p, sa, NULL, NULL, ie, ielen); 559 #endif /* CONFIG_P2P */ 560 } 561 562 dl_list_for_each(bss, &drv->bss, struct test_driver_bss, list) { 563 pos = buf; 564 end = buf + sizeof(buf); 565 566 /* reply: SCANRESP BSSID SSID IEs */ 567 ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ", 568 MAC2STR(bss->bssid)); 569 if (ret < 0 || ret >= end - pos) 570 return; 571 pos += ret; 572 pos += wpa_snprintf_hex(pos, end - pos, 573 bss->ssid, bss->ssid_len); 574 ret = snprintf(pos, end - pos, " "); 575 if (ret < 0 || ret >= end - pos) 576 return; 577 pos += ret; 578 pos += wpa_snprintf_hex(pos, end - pos, bss->ie, bss->ielen); 579 pos += wpa_snprintf_hex(pos, end - pos, bss->wps_probe_resp_ie, 580 bss->wps_probe_resp_ie_len); 581 582 if (bss->privacy) { 583 ret = snprintf(pos, end - pos, " PRIVACY"); 584 if (ret < 0 || ret >= end - pos) 585 return; 586 pos += ret; 587 } 588 589 sendto(drv->test_socket, buf, pos - buf, 0, 590 (struct sockaddr *) from, fromlen); 591 } 592 } 593 594 595 static void test_driver_assoc(struct wpa_driver_test_data *drv, 596 struct sockaddr_un *from, socklen_t fromlen, 597 char *data) 598 { 599 struct test_client_socket *cli; 600 u8 ie[256], ssid[32]; 601 size_t ielen, ssid_len = 0; 602 char *pos, *pos2, cmd[50]; 603 struct test_driver_bss *bss, *tmp; 604 605 /* data: STA-addr SSID(hex) IEs(hex) */ 606 607 cli = os_zalloc(sizeof(*cli)); 608 if (cli == NULL) 609 return; 610 611 if (hwaddr_aton(data, cli->addr)) { 612 printf("test_socket: Invalid MAC address '%s' in ASSOC\n", 613 data); 614 os_free(cli); 615 return; 616 } 617 pos = data + 17; 618 while (*pos == ' ') 619 pos++; 620 pos2 = strchr(pos, ' '); 621 ielen = 0; 622 if (pos2) { 623 ssid_len = (pos2 - pos) / 2; 624 if (hexstr2bin(pos, ssid, ssid_len) < 0) { 625 wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__); 626 os_free(cli); 627 return; 628 } 629 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID", 630 ssid, ssid_len); 631 632 pos = pos2 + 1; 633 ielen = strlen(pos) / 2; 634 if (ielen > sizeof(ie)) 635 ielen = sizeof(ie); 636 if (hexstr2bin(pos, ie, ielen) < 0) 637 ielen = 0; 638 } 639 640 bss = NULL; 641 dl_list_for_each(tmp, &drv->bss, struct test_driver_bss, list) { 642 if (tmp->ssid_len == ssid_len && 643 os_memcmp(tmp->ssid, ssid, ssid_len) == 0) { 644 bss = tmp; 645 break; 646 } 647 } 648 if (bss == NULL) { 649 wpa_printf(MSG_DEBUG, "%s: No matching SSID found from " 650 "configured BSSes", __func__); 651 os_free(cli); 652 return; 653 } 654 655 cli->bss = bss; 656 memcpy(&cli->un, from, sizeof(cli->un)); 657 cli->unlen = fromlen; 658 cli->next = drv->cli; 659 drv->cli = cli; 660 wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path", 661 (const u8 *) cli->un.sun_path, 662 cli->unlen - sizeof(cli->un.sun_family)); 663 664 snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0", 665 MAC2STR(bss->bssid)); 666 sendto(drv->test_socket, cmd, strlen(cmd), 0, 667 (struct sockaddr *) from, fromlen); 668 669 drv_event_assoc(bss->bss_ctx, cli->addr, ie, ielen, 0); 670 } 671 672 673 static void test_driver_disassoc(struct wpa_driver_test_data *drv, 674 struct sockaddr_un *from, socklen_t fromlen) 675 { 676 struct test_client_socket *cli; 677 678 cli = test_driver_get_cli(drv, from, fromlen); 679 if (!cli) 680 return; 681 682 drv_event_disassoc(drv->ctx, cli->addr); 683 } 684 685 686 static void test_driver_eapol(struct wpa_driver_test_data *drv, 687 struct sockaddr_un *from, socklen_t fromlen, 688 u8 *data, size_t datalen) 689 { 690 #ifdef HOSTAPD 691 struct test_client_socket *cli; 692 #endif /* HOSTAPD */ 693 const u8 *src = NULL; 694 695 if (datalen > 14) { 696 /* Skip Ethernet header */ 697 src = data + ETH_ALEN; 698 wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src=" 699 MACSTR " proto=%04x", 700 MAC2STR(data), MAC2STR(src), 701 WPA_GET_BE16(data + 2 * ETH_ALEN)); 702 data += 14; 703 datalen -= 14; 704 } 705 706 #ifdef HOSTAPD 707 cli = test_driver_get_cli(drv, from, fromlen); 708 if (cli) { 709 drv_event_eapol_rx(cli->bss->bss_ctx, cli->addr, data, 710 datalen); 711 } else { 712 wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown " 713 "client"); 714 } 715 #else /* HOSTAPD */ 716 if (src) 717 drv_event_eapol_rx(drv->ctx, src, data, datalen); 718 #endif /* HOSTAPD */ 719 } 720 721 722 static void test_driver_ether(struct wpa_driver_test_data *drv, 723 struct sockaddr_un *from, socklen_t fromlen, 724 u8 *data, size_t datalen) 725 { 726 struct l2_ethhdr *eth; 727 728 if (datalen < sizeof(*eth)) 729 return; 730 731 eth = (struct l2_ethhdr *) data; 732 wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src=" 733 MACSTR " proto=%04x", 734 MAC2STR(eth->h_dest), MAC2STR(eth->h_source), 735 be_to_host16(eth->h_proto)); 736 737 #ifdef CONFIG_IEEE80211R 738 if (be_to_host16(eth->h_proto) == ETH_P_RRB) { 739 union wpa_event_data ev; 740 os_memset(&ev, 0, sizeof(ev)); 741 ev.ft_rrb_rx.src = eth->h_source; 742 ev.ft_rrb_rx.data = data + sizeof(*eth); 743 ev.ft_rrb_rx.data_len = datalen - sizeof(*eth); 744 } 745 #endif /* CONFIG_IEEE80211R */ 746 } 747 748 749 static void test_driver_mlme(struct wpa_driver_test_data *drv, 750 struct sockaddr_un *from, socklen_t fromlen, 751 u8 *data, size_t datalen) 752 { 753 struct ieee80211_hdr *hdr; 754 u16 fc; 755 union wpa_event_data event; 756 int freq = 0, own_freq; 757 struct test_driver_bss *bss; 758 759 bss = dl_list_first(&drv->bss, struct test_driver_bss, list); 760 761 if (datalen > 6 && os_memcmp(data, "freq=", 5) == 0) { 762 size_t pos; 763 for (pos = 5; pos < datalen; pos++) { 764 if (data[pos] == ' ') 765 break; 766 } 767 if (pos < datalen) { 768 freq = atoi((const char *) &data[5]); 769 wpa_printf(MSG_DEBUG, "test_driver(%s): MLME RX on " 770 "freq %d MHz", bss->ifname, freq); 771 pos++; 772 data += pos; 773 datalen -= pos; 774 } 775 } 776 777 if (drv->remain_on_channel_freq) 778 own_freq = drv->remain_on_channel_freq; 779 else 780 own_freq = drv->current_freq; 781 782 if (freq && own_freq && freq != own_freq) { 783 wpa_printf(MSG_DEBUG, "test_driver(%s): Ignore MLME RX on " 784 "another frequency %d MHz (own %d MHz)", 785 bss->ifname, freq, own_freq); 786 return; 787 } 788 789 hdr = (struct ieee80211_hdr *) data; 790 791 if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) { 792 struct test_client_socket *cli; 793 cli = os_zalloc(sizeof(*cli)); 794 if (cli == NULL) 795 return; 796 wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR, 797 MAC2STR(hdr->addr2)); 798 memcpy(cli->addr, hdr->addr2, ETH_ALEN); 799 memcpy(&cli->un, from, sizeof(cli->un)); 800 cli->unlen = fromlen; 801 cli->next = drv->cli; 802 drv->cli = cli; 803 } 804 805 wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame", 806 data, datalen); 807 fc = le_to_host16(hdr->frame_control); 808 if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) { 809 wpa_printf(MSG_ERROR, "%s: received non-mgmt frame", 810 __func__); 811 return; 812 } 813 814 os_memset(&event, 0, sizeof(event)); 815 event.rx_mgmt.frame = data; 816 event.rx_mgmt.frame_len = datalen; 817 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event); 818 } 819 820 821 static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx) 822 { 823 struct wpa_driver_test_data *drv = eloop_ctx; 824 char buf[2000]; 825 int res; 826 struct sockaddr_un from; 827 socklen_t fromlen = sizeof(from); 828 829 res = recvfrom(sock, buf, sizeof(buf) - 1, 0, 830 (struct sockaddr *) &from, &fromlen); 831 if (res < 0) { 832 perror("recvfrom(test_socket)"); 833 return; 834 } 835 buf[res] = '\0'; 836 837 wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res); 838 839 if (strncmp(buf, "SCAN", 4) == 0) { 840 test_driver_scan(drv, &from, fromlen, buf + 4); 841 } else if (strncmp(buf, "ASSOC ", 6) == 0) { 842 test_driver_assoc(drv, &from, fromlen, buf + 6); 843 } else if (strcmp(buf, "DISASSOC") == 0) { 844 test_driver_disassoc(drv, &from, fromlen); 845 } else if (strncmp(buf, "EAPOL ", 6) == 0) { 846 test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6, 847 res - 6); 848 } else if (strncmp(buf, "ETHER ", 6) == 0) { 849 test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6, 850 res - 6); 851 } else if (strncmp(buf, "MLME ", 5) == 0) { 852 test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5); 853 } else { 854 wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command", 855 (u8 *) buf, res); 856 } 857 } 858 859 860 static int test_driver_set_generic_elem(void *priv, 861 const u8 *elem, size_t elem_len) 862 { 863 struct test_driver_bss *bss = priv; 864 865 os_free(bss->ie); 866 867 if (elem == NULL) { 868 bss->ie = NULL; 869 bss->ielen = 0; 870 return 0; 871 } 872 873 bss->ie = os_malloc(elem_len); 874 if (bss->ie == NULL) { 875 bss->ielen = 0; 876 return -1; 877 } 878 879 memcpy(bss->ie, elem, elem_len); 880 bss->ielen = elem_len; 881 return 0; 882 } 883 884 885 static int test_driver_set_ap_wps_ie(void *priv, const struct wpabuf *beacon, 886 const struct wpabuf *proberesp, 887 const struct wpabuf *assocresp) 888 { 889 struct test_driver_bss *bss = priv; 890 891 if (beacon == NULL) 892 wpa_printf(MSG_DEBUG, "test_driver: Clear Beacon WPS IE"); 893 else 894 wpa_hexdump_buf(MSG_DEBUG, "test_driver: Beacon WPS IE", 895 beacon); 896 897 os_free(bss->wps_beacon_ie); 898 899 if (beacon == NULL) { 900 bss->wps_beacon_ie = NULL; 901 bss->wps_beacon_ie_len = 0; 902 } else { 903 bss->wps_beacon_ie = os_malloc(wpabuf_len(beacon)); 904 if (bss->wps_beacon_ie == NULL) { 905 bss->wps_beacon_ie_len = 0; 906 return -1; 907 } 908 909 os_memcpy(bss->wps_beacon_ie, wpabuf_head(beacon), 910 wpabuf_len(beacon)); 911 bss->wps_beacon_ie_len = wpabuf_len(beacon); 912 } 913 914 if (proberesp == NULL) 915 wpa_printf(MSG_DEBUG, "test_driver: Clear Probe Response WPS " 916 "IE"); 917 else 918 wpa_hexdump_buf(MSG_DEBUG, "test_driver: Probe Response WPS " 919 "IE", proberesp); 920 921 os_free(bss->wps_probe_resp_ie); 922 923 if (proberesp == NULL) { 924 bss->wps_probe_resp_ie = NULL; 925 bss->wps_probe_resp_ie_len = 0; 926 } else { 927 bss->wps_probe_resp_ie = os_malloc(wpabuf_len(proberesp)); 928 if (bss->wps_probe_resp_ie == NULL) { 929 bss->wps_probe_resp_ie_len = 0; 930 return -1; 931 } 932 933 os_memcpy(bss->wps_probe_resp_ie, wpabuf_head(proberesp), 934 wpabuf_len(proberesp)); 935 bss->wps_probe_resp_ie_len = wpabuf_len(proberesp); 936 } 937 938 return 0; 939 } 940 941 942 static int test_driver_sta_deauth(void *priv, const u8 *own_addr, 943 const u8 *addr, int reason) 944 { 945 struct test_driver_bss *dbss = priv; 946 struct wpa_driver_test_data *drv = dbss->drv; 947 struct test_client_socket *cli; 948 949 if (drv->test_socket < 0) 950 return -1; 951 952 cli = drv->cli; 953 while (cli) { 954 if (memcmp(cli->addr, addr, ETH_ALEN) == 0) 955 break; 956 cli = cli->next; 957 } 958 959 if (!cli) 960 return -1; 961 962 return sendto(drv->test_socket, "DEAUTH", 6, 0, 963 (struct sockaddr *) &cli->un, cli->unlen); 964 } 965 966 967 static int test_driver_sta_disassoc(void *priv, const u8 *own_addr, 968 const u8 *addr, int reason) 969 { 970 struct test_driver_bss *dbss = priv; 971 struct wpa_driver_test_data *drv = dbss->drv; 972 struct test_client_socket *cli; 973 974 if (drv->test_socket < 0) 975 return -1; 976 977 cli = drv->cli; 978 while (cli) { 979 if (memcmp(cli->addr, addr, ETH_ALEN) == 0) 980 break; 981 cli = cli->next; 982 } 983 984 if (!cli) 985 return -1; 986 987 return sendto(drv->test_socket, "DISASSOC", 8, 0, 988 (struct sockaddr *) &cli->un, cli->unlen); 989 } 990 991 992 static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid, 993 void *bss_ctx, void **drv_priv) 994 { 995 struct test_driver_bss *dbss = priv; 996 struct wpa_driver_test_data *drv = dbss->drv; 997 struct test_driver_bss *bss; 998 999 wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")", 1000 __func__, ifname, MAC2STR(bssid)); 1001 1002 bss = os_zalloc(sizeof(*bss)); 1003 if (bss == NULL) 1004 return -1; 1005 1006 bss->bss_ctx = bss_ctx; 1007 bss->drv = drv; 1008 os_strlcpy(bss->ifname, ifname, IFNAMSIZ); 1009 os_memcpy(bss->bssid, bssid, ETH_ALEN); 1010 1011 dl_list_add(&drv->bss, &bss->list); 1012 if (drv->global) { 1013 drv->global->bss_add_used = 1; 1014 os_memcpy(drv->global->req_addr, bssid, ETH_ALEN); 1015 } 1016 1017 if (drv_priv) 1018 *drv_priv = bss; 1019 1020 return 0; 1021 } 1022 1023 1024 static int test_driver_bss_remove(void *priv, const char *ifname) 1025 { 1026 struct test_driver_bss *dbss = priv; 1027 struct wpa_driver_test_data *drv = dbss->drv; 1028 struct test_driver_bss *bss; 1029 struct test_client_socket *cli, *prev_c; 1030 1031 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname); 1032 1033 dl_list_for_each(bss, &drv->bss, struct test_driver_bss, list) { 1034 if (strcmp(bss->ifname, ifname) != 0) 1035 continue; 1036 1037 for (prev_c = NULL, cli = drv->cli; cli; 1038 prev_c = cli, cli = cli->next) { 1039 if (cli->bss != bss) 1040 continue; 1041 if (prev_c) 1042 prev_c->next = cli->next; 1043 else 1044 drv->cli = cli->next; 1045 os_free(cli); 1046 break; 1047 } 1048 1049 dl_list_del(&bss->list); 1050 test_driver_free_bss(bss); 1051 return 0; 1052 } 1053 1054 return -1; 1055 } 1056 1057 1058 static int test_driver_if_add(void *priv, enum wpa_driver_if_type type, 1059 const char *ifname, const u8 *addr, 1060 void *bss_ctx, void **drv_priv, 1061 char *force_ifname, u8 *if_addr, 1062 const char *bridge) 1063 { 1064 struct test_driver_bss *dbss = priv; 1065 struct wpa_driver_test_data *drv = dbss->drv; 1066 1067 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s bss_ctx=%p)", 1068 __func__, type, ifname, bss_ctx); 1069 if (addr) 1070 os_memcpy(if_addr, addr, ETH_ALEN); 1071 else { 1072 drv->alloc_iface_idx++; 1073 if_addr[0] = 0x02; /* locally administered */ 1074 sha1_prf(drv->own_addr, ETH_ALEN, 1075 "hostapd test addr generation", 1076 (const u8 *) &drv->alloc_iface_idx, 1077 sizeof(drv->alloc_iface_idx), 1078 if_addr + 1, ETH_ALEN - 1); 1079 } 1080 if (type == WPA_IF_AP_BSS || type == WPA_IF_P2P_GO || 1081 type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP) 1082 return test_driver_bss_add(priv, ifname, if_addr, bss_ctx, 1083 drv_priv); 1084 return 0; 1085 } 1086 1087 1088 static int test_driver_if_remove(void *priv, enum wpa_driver_if_type type, 1089 const char *ifname) 1090 { 1091 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname); 1092 if (type == WPA_IF_AP_BSS || type == WPA_IF_P2P_GO || 1093 type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP) 1094 return test_driver_bss_remove(priv, ifname); 1095 return 0; 1096 } 1097 1098 1099 static int test_driver_set_ssid(void *priv, const u8 *buf, int len) 1100 { 1101 struct test_driver_bss *bss = priv; 1102 1103 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, bss->ifname); 1104 if (len < 0) 1105 return -1; 1106 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len); 1107 1108 if ((size_t) len > sizeof(bss->ssid)) 1109 return -1; 1110 1111 os_memcpy(bss->ssid, buf, len); 1112 bss->ssid_len = len; 1113 1114 return 0; 1115 } 1116 1117 1118 static int test_driver_set_privacy(void *priv, int enabled) 1119 { 1120 struct test_driver_bss *dbss = priv; 1121 1122 wpa_printf(MSG_DEBUG, "%s(enabled=%d)", __func__, enabled); 1123 dbss->privacy = enabled; 1124 1125 return 0; 1126 } 1127 1128 1129 static int test_driver_set_sta_vlan(void *priv, const u8 *addr, 1130 const char *ifname, int vlan_id) 1131 { 1132 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)", 1133 __func__, MAC2STR(addr), ifname, vlan_id); 1134 return 0; 1135 } 1136 1137 1138 static int test_driver_sta_add(void *priv, 1139 struct hostapd_sta_add_params *params) 1140 { 1141 struct test_driver_bss *bss = priv; 1142 struct wpa_driver_test_data *drv = bss->drv; 1143 struct test_client_socket *cli; 1144 1145 wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d " 1146 "capability=0x%x listen_interval=%d)", 1147 __func__, bss->ifname, MAC2STR(params->addr), params->aid, 1148 params->capability, params->listen_interval); 1149 wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates", 1150 params->supp_rates, params->supp_rates_len); 1151 1152 cli = drv->cli; 1153 while (cli) { 1154 if (os_memcmp(cli->addr, params->addr, ETH_ALEN) == 0) 1155 break; 1156 cli = cli->next; 1157 } 1158 if (!cli) { 1159 wpa_printf(MSG_DEBUG, "%s: no matching client entry", 1160 __func__); 1161 return -1; 1162 } 1163 1164 cli->bss = bss; 1165 1166 return 0; 1167 } 1168 1169 1170 static struct wpa_driver_test_data * test_alloc_data(void *ctx, 1171 const char *ifname) 1172 { 1173 struct wpa_driver_test_data *drv; 1174 struct test_driver_bss *bss; 1175 1176 drv = os_zalloc(sizeof(struct wpa_driver_test_data)); 1177 if (drv == NULL) { 1178 wpa_printf(MSG_ERROR, "Could not allocate memory for test " 1179 "driver data"); 1180 return NULL; 1181 } 1182 1183 bss = os_zalloc(sizeof(struct test_driver_bss)); 1184 if (bss == NULL) { 1185 os_free(drv); 1186 return NULL; 1187 } 1188 1189 drv->ctx = ctx; 1190 wpa_trace_add_ref(drv, ctx, ctx); 1191 dl_list_init(&drv->bss); 1192 dl_list_add(&drv->bss, &bss->list); 1193 os_strlcpy(bss->ifname, ifname, IFNAMSIZ); 1194 bss->bss_ctx = ctx; 1195 bss->drv = drv; 1196 1197 /* Generate a MAC address to help testing with multiple STAs */ 1198 drv->own_addr[0] = 0x02; /* locally administered */ 1199 sha1_prf((const u8 *) ifname, os_strlen(ifname), 1200 "test mac addr generation", 1201 NULL, 0, drv->own_addr + 1, ETH_ALEN - 1); 1202 1203 return drv; 1204 } 1205 1206 1207 static void * test_driver_init(struct hostapd_data *hapd, 1208 struct wpa_init_params *params) 1209 { 1210 struct wpa_driver_test_data *drv; 1211 struct sockaddr_un addr_un; 1212 struct sockaddr_in addr_in; 1213 struct sockaddr *addr; 1214 socklen_t alen; 1215 struct test_driver_bss *bss; 1216 1217 drv = test_alloc_data(hapd, params->ifname); 1218 if (drv == NULL) 1219 return NULL; 1220 drv->ap = 1; 1221 bss = dl_list_first(&drv->bss, struct test_driver_bss, list); 1222 drv->global = params->global_priv; 1223 1224 bss->bss_ctx = hapd; 1225 os_memcpy(bss->bssid, drv->own_addr, ETH_ALEN); 1226 os_memcpy(params->own_addr, drv->own_addr, ETH_ALEN); 1227 1228 if (params->test_socket) { 1229 if (os_strlen(params->test_socket) >= 1230 sizeof(addr_un.sun_path)) { 1231 printf("Too long test_socket path\n"); 1232 wpa_driver_test_deinit(bss); 1233 return NULL; 1234 } 1235 if (strncmp(params->test_socket, "DIR:", 4) == 0) { 1236 size_t len = strlen(params->test_socket) + 30; 1237 drv->test_dir = os_strdup(params->test_socket + 4); 1238 drv->own_socket_path = os_malloc(len); 1239 if (drv->own_socket_path) { 1240 snprintf(drv->own_socket_path, len, 1241 "%s/AP-" MACSTR, 1242 params->test_socket + 4, 1243 MAC2STR(params->own_addr)); 1244 } 1245 } else if (strncmp(params->test_socket, "UDP:", 4) == 0) { 1246 drv->udp_port = atoi(params->test_socket + 4); 1247 } else { 1248 drv->own_socket_path = os_strdup(params->test_socket); 1249 } 1250 if (drv->own_socket_path == NULL && drv->udp_port == 0) { 1251 wpa_driver_test_deinit(bss); 1252 return NULL; 1253 } 1254 1255 drv->test_socket = socket(drv->udp_port ? PF_INET : PF_UNIX, 1256 SOCK_DGRAM, 0); 1257 if (drv->test_socket < 0) { 1258 perror("socket"); 1259 wpa_driver_test_deinit(bss); 1260 return NULL; 1261 } 1262 1263 if (drv->udp_port) { 1264 os_memset(&addr_in, 0, sizeof(addr_in)); 1265 addr_in.sin_family = AF_INET; 1266 addr_in.sin_port = htons(drv->udp_port); 1267 addr = (struct sockaddr *) &addr_in; 1268 alen = sizeof(addr_in); 1269 } else { 1270 os_memset(&addr_un, 0, sizeof(addr_un)); 1271 addr_un.sun_family = AF_UNIX; 1272 os_strlcpy(addr_un.sun_path, drv->own_socket_path, 1273 sizeof(addr_un.sun_path)); 1274 addr = (struct sockaddr *) &addr_un; 1275 alen = sizeof(addr_un); 1276 } 1277 if (bind(drv->test_socket, addr, alen) < 0) { 1278 perror("bind(PF_UNIX)"); 1279 close(drv->test_socket); 1280 if (drv->own_socket_path) 1281 unlink(drv->own_socket_path); 1282 wpa_driver_test_deinit(bss); 1283 return NULL; 1284 } 1285 eloop_register_read_sock(drv->test_socket, 1286 test_driver_receive_unix, drv, NULL); 1287 } else 1288 drv->test_socket = -1; 1289 1290 return bss; 1291 } 1292 1293 1294 static void wpa_driver_test_poll(void *eloop_ctx, void *timeout_ctx) 1295 { 1296 struct wpa_driver_test_data *drv = eloop_ctx; 1297 1298 #ifdef DRIVER_TEST_UNIX 1299 if (drv->associated && drv->hostapd_addr_set) { 1300 struct stat st; 1301 if (stat(drv->hostapd_addr.sun_path, &st) < 0) { 1302 wpa_printf(MSG_DEBUG, "%s: lost connection to AP: %s", 1303 __func__, strerror(errno)); 1304 drv->associated = 0; 1305 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL); 1306 } 1307 } 1308 #endif /* DRIVER_TEST_UNIX */ 1309 1310 eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL); 1311 } 1312 1313 1314 static void wpa_driver_test_scan_timeout(void *eloop_ctx, void *timeout_ctx) 1315 { 1316 struct wpa_driver_test_data *drv = eloop_ctx; 1317 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results"); 1318 if (drv->pending_p2p_scan && drv->p2p) { 1319 #ifdef CONFIG_P2P 1320 size_t i; 1321 for (i = 0; i < drv->num_scanres; i++) { 1322 struct wpa_scan_res *bss = drv->scanres[i]; 1323 if (p2p_scan_res_handler(drv->p2p, bss->bssid, 1324 bss->freq, bss->level, 1325 (const u8 *) (bss + 1), 1326 bss->ie_len) > 0) 1327 return; 1328 } 1329 p2p_scan_res_handled(drv->p2p); 1330 #endif /* CONFIG_P2P */ 1331 return; 1332 } 1333 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL); 1334 } 1335 1336 1337 #ifdef DRIVER_TEST_UNIX 1338 static void wpa_driver_scan_dir(struct wpa_driver_test_data *drv, 1339 const char *path) 1340 { 1341 struct dirent *dent; 1342 DIR *dir; 1343 struct sockaddr_un addr; 1344 char cmd[512], *pos, *end; 1345 int ret; 1346 1347 dir = opendir(path); 1348 if (dir == NULL) 1349 return; 1350 1351 end = cmd + sizeof(cmd); 1352 pos = cmd; 1353 ret = os_snprintf(pos, end - pos, "SCAN " MACSTR, 1354 MAC2STR(drv->own_addr)); 1355 if (ret >= 0 && ret < end - pos) 1356 pos += ret; 1357 if (drv->probe_req_ie) { 1358 ret = os_snprintf(pos, end - pos, " "); 1359 if (ret >= 0 && ret < end - pos) 1360 pos += ret; 1361 pos += wpa_snprintf_hex(pos, end - pos, drv->probe_req_ie, 1362 drv->probe_req_ie_len); 1363 } 1364 if (drv->probe_req_ssid_len) { 1365 /* Add SSID IE */ 1366 ret = os_snprintf(pos, end - pos, "%02x%02x", 1367 WLAN_EID_SSID, 1368 (unsigned int) drv->probe_req_ssid_len); 1369 if (ret >= 0 && ret < end - pos) 1370 pos += ret; 1371 pos += wpa_snprintf_hex(pos, end - pos, drv->probe_req_ssid, 1372 drv->probe_req_ssid_len); 1373 } 1374 end[-1] = '\0'; 1375 1376 while ((dent = readdir(dir))) { 1377 if (os_strncmp(dent->d_name, "AP-", 3) != 0 && 1378 os_strncmp(dent->d_name, "STA-", 4) != 0) 1379 continue; 1380 if (drv->own_socket_path) { 1381 size_t olen, dlen; 1382 olen = os_strlen(drv->own_socket_path); 1383 dlen = os_strlen(dent->d_name); 1384 if (olen >= dlen && 1385 os_strcmp(dent->d_name, 1386 drv->own_socket_path + olen - dlen) == 0) 1387 continue; 1388 } 1389 wpa_printf(MSG_DEBUG, "%s: SCAN %s", __func__, dent->d_name); 1390 1391 os_memset(&addr, 0, sizeof(addr)); 1392 addr.sun_family = AF_UNIX; 1393 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", 1394 path, dent->d_name); 1395 1396 if (sendto(drv->test_socket, cmd, os_strlen(cmd), 0, 1397 (struct sockaddr *) &addr, sizeof(addr)) < 0) { 1398 perror("sendto(test_socket)"); 1399 } 1400 } 1401 closedir(dir); 1402 } 1403 #endif /* DRIVER_TEST_UNIX */ 1404 1405 1406 static int wpa_driver_test_scan(void *priv, 1407 struct wpa_driver_scan_params *params) 1408 { 1409 struct test_driver_bss *dbss = priv; 1410 struct wpa_driver_test_data *drv = dbss->drv; 1411 size_t i; 1412 1413 wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv); 1414 1415 os_free(drv->probe_req_ie); 1416 if (params->extra_ies) { 1417 drv->probe_req_ie = os_malloc(params->extra_ies_len); 1418 if (drv->probe_req_ie == NULL) { 1419 drv->probe_req_ie_len = 0; 1420 return -1; 1421 } 1422 os_memcpy(drv->probe_req_ie, params->extra_ies, 1423 params->extra_ies_len); 1424 drv->probe_req_ie_len = params->extra_ies_len; 1425 } else { 1426 drv->probe_req_ie = NULL; 1427 drv->probe_req_ie_len = 0; 1428 } 1429 1430 for (i = 0; i < params->num_ssids; i++) 1431 wpa_hexdump(MSG_DEBUG, "Scan SSID", 1432 params->ssids[i].ssid, params->ssids[i].ssid_len); 1433 drv->probe_req_ssid_len = 0; 1434 if (params->num_ssids) { 1435 os_memcpy(drv->probe_req_ssid, params->ssids[0].ssid, 1436 params->ssids[0].ssid_len); 1437 drv->probe_req_ssid_len = params->ssids[0].ssid_len; 1438 } 1439 wpa_hexdump(MSG_DEBUG, "Scan extra IE(s)", 1440 params->extra_ies, params->extra_ies_len); 1441 1442 drv->num_scanres = 0; 1443 1444 #ifdef DRIVER_TEST_UNIX 1445 if (drv->test_socket >= 0 && drv->test_dir) 1446 wpa_driver_scan_dir(drv, drv->test_dir); 1447 1448 if (drv->test_socket >= 0 && drv->hostapd_addr_set && 1449 sendto(drv->test_socket, "SCAN", 4, 0, 1450 (struct sockaddr *) &drv->hostapd_addr, 1451 sizeof(drv->hostapd_addr)) < 0) { 1452 perror("sendto(test_socket)"); 1453 } 1454 #endif /* DRIVER_TEST_UNIX */ 1455 1456 if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set && 1457 sendto(drv->test_socket, "SCAN", 4, 0, 1458 (struct sockaddr *) &drv->hostapd_addr_udp, 1459 sizeof(drv->hostapd_addr_udp)) < 0) { 1460 perror("sendto(test_socket)"); 1461 } 1462 1463 eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx); 1464 eloop_register_timeout(1, 0, wpa_driver_test_scan_timeout, drv, 1465 drv->ctx); 1466 return 0; 1467 } 1468 1469 1470 static struct wpa_scan_results * wpa_driver_test_get_scan_results2(void *priv) 1471 { 1472 struct test_driver_bss *dbss = priv; 1473 struct wpa_driver_test_data *drv = dbss->drv; 1474 struct wpa_scan_results *res; 1475 size_t i; 1476 1477 res = os_zalloc(sizeof(*res)); 1478 if (res == NULL) 1479 return NULL; 1480 1481 res->res = os_zalloc(drv->num_scanres * sizeof(struct wpa_scan_res *)); 1482 if (res->res == NULL) { 1483 os_free(res); 1484 return NULL; 1485 } 1486 1487 for (i = 0; i < drv->num_scanres; i++) { 1488 struct wpa_scan_res *r; 1489 if (drv->scanres[i] == NULL) 1490 continue; 1491 r = os_malloc(sizeof(*r) + drv->scanres[i]->ie_len); 1492 if (r == NULL) 1493 break; 1494 os_memcpy(r, drv->scanres[i], 1495 sizeof(*r) + drv->scanres[i]->ie_len); 1496 res->res[res->num++] = r; 1497 } 1498 1499 return res; 1500 } 1501 1502 1503 static int wpa_driver_test_set_key(const char *ifname, void *priv, 1504 enum wpa_alg alg, const u8 *addr, 1505 int key_idx, int set_tx, 1506 const u8 *seq, size_t seq_len, 1507 const u8 *key, size_t key_len) 1508 { 1509 wpa_printf(MSG_DEBUG, "%s: ifname=%s priv=%p alg=%d key_idx=%d " 1510 "set_tx=%d", 1511 __func__, ifname, priv, alg, key_idx, set_tx); 1512 if (addr) 1513 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr)); 1514 if (seq) 1515 wpa_hexdump(MSG_DEBUG, " seq", seq, seq_len); 1516 if (key) 1517 wpa_hexdump_key(MSG_DEBUG, " key", key, key_len); 1518 return 0; 1519 } 1520 1521 1522 static int wpa_driver_update_mode(struct wpa_driver_test_data *drv, int ap) 1523 { 1524 if (ap && !drv->ap) { 1525 wpa_driver_test_close_test_socket(drv); 1526 wpa_driver_test_attach(drv, drv->test_dir, 1); 1527 drv->ap = 1; 1528 } else if (!ap && drv->ap) { 1529 wpa_driver_test_close_test_socket(drv); 1530 wpa_driver_test_attach(drv, drv->test_dir, 0); 1531 drv->ap = 0; 1532 } 1533 1534 return 0; 1535 } 1536 1537 1538 static int wpa_driver_test_associate( 1539 void *priv, struct wpa_driver_associate_params *params) 1540 { 1541 struct test_driver_bss *dbss = priv; 1542 struct wpa_driver_test_data *drv = dbss->drv; 1543 wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d " 1544 "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d", 1545 __func__, priv, params->freq, params->pairwise_suite, 1546 params->group_suite, params->key_mgmt_suite, 1547 params->auth_alg, params->mode); 1548 wpa_driver_update_mode(drv, params->mode == IEEE80211_MODE_AP); 1549 if (params->bssid) { 1550 wpa_printf(MSG_DEBUG, " bssid=" MACSTR, 1551 MAC2STR(params->bssid)); 1552 } 1553 if (params->ssid) { 1554 wpa_hexdump_ascii(MSG_DEBUG, " ssid", 1555 params->ssid, params->ssid_len); 1556 } 1557 if (params->wpa_ie) { 1558 wpa_hexdump(MSG_DEBUG, " wpa_ie", 1559 params->wpa_ie, params->wpa_ie_len); 1560 drv->assoc_wpa_ie_len = params->wpa_ie_len; 1561 if (drv->assoc_wpa_ie_len > sizeof(drv->assoc_wpa_ie)) 1562 drv->assoc_wpa_ie_len = sizeof(drv->assoc_wpa_ie); 1563 os_memcpy(drv->assoc_wpa_ie, params->wpa_ie, 1564 drv->assoc_wpa_ie_len); 1565 } else 1566 drv->assoc_wpa_ie_len = 0; 1567 1568 wpa_driver_update_mode(drv, params->mode == IEEE80211_MODE_AP); 1569 1570 drv->ibss = params->mode == IEEE80211_MODE_IBSS; 1571 dbss->privacy = params->key_mgmt_suite & 1572 (WPA_KEY_MGMT_IEEE8021X | 1573 WPA_KEY_MGMT_PSK | 1574 WPA_KEY_MGMT_WPA_NONE | 1575 WPA_KEY_MGMT_FT_IEEE8021X | 1576 WPA_KEY_MGMT_FT_PSK | 1577 WPA_KEY_MGMT_IEEE8021X_SHA256 | 1578 WPA_KEY_MGMT_PSK_SHA256); 1579 if (params->wep_key_len[params->wep_tx_keyidx]) 1580 dbss->privacy = 1; 1581 1582 #ifdef DRIVER_TEST_UNIX 1583 if (drv->test_dir && params->bssid && 1584 params->mode != IEEE80211_MODE_IBSS) { 1585 os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr)); 1586 drv->hostapd_addr.sun_family = AF_UNIX; 1587 os_snprintf(drv->hostapd_addr.sun_path, 1588 sizeof(drv->hostapd_addr.sun_path), 1589 "%s/AP-" MACSTR, 1590 drv->test_dir, MAC2STR(params->bssid)); 1591 drv->hostapd_addr_set = 1; 1592 } 1593 #endif /* DRIVER_TEST_UNIX */ 1594 1595 if (params->mode == IEEE80211_MODE_AP) { 1596 os_memcpy(dbss->ssid, params->ssid, params->ssid_len); 1597 dbss->ssid_len = params->ssid_len; 1598 os_memcpy(dbss->bssid, drv->own_addr, ETH_ALEN); 1599 if (params->wpa_ie && params->wpa_ie_len) { 1600 dbss->ie = os_malloc(params->wpa_ie_len); 1601 if (dbss->ie) { 1602 os_memcpy(dbss->ie, params->wpa_ie, 1603 params->wpa_ie_len); 1604 dbss->ielen = params->wpa_ie_len; 1605 } 1606 } 1607 } else if (drv->test_socket >= 0 && 1608 (drv->hostapd_addr_set || drv->hostapd_addr_udp_set)) { 1609 char cmd[200], *pos, *end; 1610 int ret; 1611 end = cmd + sizeof(cmd); 1612 pos = cmd; 1613 ret = os_snprintf(pos, end - pos, "ASSOC " MACSTR " ", 1614 MAC2STR(drv->own_addr)); 1615 if (ret >= 0 && ret < end - pos) 1616 pos += ret; 1617 pos += wpa_snprintf_hex(pos, end - pos, params->ssid, 1618 params->ssid_len); 1619 ret = os_snprintf(pos, end - pos, " "); 1620 if (ret >= 0 && ret < end - pos) 1621 pos += ret; 1622 pos += wpa_snprintf_hex(pos, end - pos, params->wpa_ie, 1623 params->wpa_ie_len); 1624 end[-1] = '\0'; 1625 #ifdef DRIVER_TEST_UNIX 1626 if (drv->hostapd_addr_set && 1627 sendto(drv->test_socket, cmd, os_strlen(cmd), 0, 1628 (struct sockaddr *) &drv->hostapd_addr, 1629 sizeof(drv->hostapd_addr)) < 0) { 1630 perror("sendto(test_socket)"); 1631 return -1; 1632 } 1633 #endif /* DRIVER_TEST_UNIX */ 1634 if (drv->hostapd_addr_udp_set && 1635 sendto(drv->test_socket, cmd, os_strlen(cmd), 0, 1636 (struct sockaddr *) &drv->hostapd_addr_udp, 1637 sizeof(drv->hostapd_addr_udp)) < 0) { 1638 perror("sendto(test_socket)"); 1639 return -1; 1640 } 1641 1642 os_memcpy(dbss->ssid, params->ssid, params->ssid_len); 1643 dbss->ssid_len = params->ssid_len; 1644 } else { 1645 drv->associated = 1; 1646 if (params->mode == IEEE80211_MODE_IBSS) { 1647 os_memcpy(dbss->ssid, params->ssid, params->ssid_len); 1648 dbss->ssid_len = params->ssid_len; 1649 if (params->bssid) 1650 os_memcpy(dbss->bssid, params->bssid, 1651 ETH_ALEN); 1652 else { 1653 os_get_random(dbss->bssid, ETH_ALEN); 1654 dbss->bssid[0] &= ~0x01; 1655 dbss->bssid[0] |= 0x02; 1656 } 1657 } 1658 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL); 1659 } 1660 1661 return 0; 1662 } 1663 1664 1665 static int wpa_driver_test_get_bssid(void *priv, u8 *bssid) 1666 { 1667 struct test_driver_bss *dbss = priv; 1668 os_memcpy(bssid, dbss->bssid, ETH_ALEN); 1669 return 0; 1670 } 1671 1672 1673 static int wpa_driver_test_get_ssid(void *priv, u8 *ssid) 1674 { 1675 struct test_driver_bss *dbss = priv; 1676 os_memcpy(ssid, dbss->ssid, 32); 1677 return dbss->ssid_len; 1678 } 1679 1680 1681 static int wpa_driver_test_send_disassoc(struct wpa_driver_test_data *drv) 1682 { 1683 #ifdef DRIVER_TEST_UNIX 1684 if (drv->test_socket >= 0 && 1685 sendto(drv->test_socket, "DISASSOC", 8, 0, 1686 (struct sockaddr *) &drv->hostapd_addr, 1687 sizeof(drv->hostapd_addr)) < 0) { 1688 perror("sendto(test_socket)"); 1689 return -1; 1690 } 1691 #endif /* DRIVER_TEST_UNIX */ 1692 if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set && 1693 sendto(drv->test_socket, "DISASSOC", 8, 0, 1694 (struct sockaddr *) &drv->hostapd_addr_udp, 1695 sizeof(drv->hostapd_addr_udp)) < 0) { 1696 perror("sendto(test_socket)"); 1697 return -1; 1698 } 1699 return 0; 1700 } 1701 1702 1703 static int wpa_driver_test_deauthenticate(void *priv, const u8 *addr, 1704 int reason_code) 1705 { 1706 struct test_driver_bss *dbss = priv; 1707 struct wpa_driver_test_data *drv = dbss->drv; 1708 wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d", 1709 __func__, MAC2STR(addr), reason_code); 1710 os_memset(dbss->bssid, 0, ETH_ALEN); 1711 drv->associated = 0; 1712 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL); 1713 return wpa_driver_test_send_disassoc(drv); 1714 } 1715 1716 1717 static int wpa_driver_test_disassociate(void *priv, const u8 *addr, 1718 int reason_code) 1719 { 1720 struct test_driver_bss *dbss = priv; 1721 struct wpa_driver_test_data *drv = dbss->drv; 1722 wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d", 1723 __func__, MAC2STR(addr), reason_code); 1724 os_memset(dbss->bssid, 0, ETH_ALEN); 1725 drv->associated = 0; 1726 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL); 1727 return wpa_driver_test_send_disassoc(drv); 1728 } 1729 1730 1731 static const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie) 1732 { 1733 const u8 *end, *pos; 1734 1735 pos = (const u8 *) (res + 1); 1736 end = pos + res->ie_len; 1737 1738 while (pos + 1 < end) { 1739 if (pos + 2 + pos[1] > end) 1740 break; 1741 if (pos[0] == ie) 1742 return pos; 1743 pos += 2 + pos[1]; 1744 } 1745 1746 return NULL; 1747 } 1748 1749 1750 static void wpa_driver_test_scanresp(struct wpa_driver_test_data *drv, 1751 struct sockaddr *from, 1752 socklen_t fromlen, 1753 const char *data) 1754 { 1755 struct wpa_scan_res *res; 1756 const char *pos, *pos2; 1757 size_t len; 1758 u8 *ie_pos, *ie_start, *ie_end; 1759 #define MAX_IE_LEN 1000 1760 const u8 *ds_params; 1761 1762 wpa_printf(MSG_DEBUG, "test_driver: SCANRESP %s", data); 1763 if (drv->num_scanres >= MAX_SCAN_RESULTS) { 1764 wpa_printf(MSG_DEBUG, "test_driver: No room for the new scan " 1765 "result"); 1766 return; 1767 } 1768 1769 /* SCANRESP BSSID SSID IEs */ 1770 1771 res = os_zalloc(sizeof(*res) + MAX_IE_LEN); 1772 if (res == NULL) 1773 return; 1774 ie_start = ie_pos = (u8 *) (res + 1); 1775 ie_end = ie_pos + MAX_IE_LEN; 1776 1777 if (hwaddr_aton(data, res->bssid)) { 1778 wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in scanres"); 1779 os_free(res); 1780 return; 1781 } 1782 1783 pos = data + 17; 1784 while (*pos == ' ') 1785 pos++; 1786 pos2 = os_strchr(pos, ' '); 1787 if (pos2 == NULL) { 1788 wpa_printf(MSG_DEBUG, "test_driver: invalid SSID termination " 1789 "in scanres"); 1790 os_free(res); 1791 return; 1792 } 1793 len = (pos2 - pos) / 2; 1794 if (len > 32) 1795 len = 32; 1796 /* 1797 * Generate SSID IE from the SSID field since this IE is not included 1798 * in the main IE field. 1799 */ 1800 *ie_pos++ = WLAN_EID_SSID; 1801 *ie_pos++ = len; 1802 if (hexstr2bin(pos, ie_pos, len) < 0) { 1803 wpa_printf(MSG_DEBUG, "test_driver: invalid SSID in scanres"); 1804 os_free(res); 1805 return; 1806 } 1807 ie_pos += len; 1808 1809 pos = pos2 + 1; 1810 pos2 = os_strchr(pos, ' '); 1811 if (pos2 == NULL) 1812 len = os_strlen(pos) / 2; 1813 else 1814 len = (pos2 - pos) / 2; 1815 if ((int) len > ie_end - ie_pos) 1816 len = ie_end - ie_pos; 1817 if (hexstr2bin(pos, ie_pos, len) < 0) { 1818 wpa_printf(MSG_DEBUG, "test_driver: invalid IEs in scanres"); 1819 os_free(res); 1820 return; 1821 } 1822 ie_pos += len; 1823 res->ie_len = ie_pos - ie_start; 1824 1825 if (pos2) { 1826 pos = pos2 + 1; 1827 while (*pos == ' ') 1828 pos++; 1829 if (os_strstr(pos, "PRIVACY")) 1830 res->caps |= IEEE80211_CAP_PRIVACY; 1831 if (os_strstr(pos, "IBSS")) 1832 res->caps |= IEEE80211_CAP_IBSS; 1833 } 1834 1835 ds_params = wpa_scan_get_ie(res, WLAN_EID_DS_PARAMS); 1836 if (ds_params && ds_params[1] > 0) { 1837 if (ds_params[2] >= 1 && ds_params[2] <= 13) 1838 res->freq = 2407 + ds_params[2] * 5; 1839 } 1840 1841 os_free(drv->scanres[drv->num_scanres]); 1842 drv->scanres[drv->num_scanres++] = res; 1843 } 1844 1845 1846 static void wpa_driver_test_assocresp(struct wpa_driver_test_data *drv, 1847 struct sockaddr *from, 1848 socklen_t fromlen, 1849 const char *data) 1850 { 1851 struct test_driver_bss *bss; 1852 1853 bss = dl_list_first(&drv->bss, struct test_driver_bss, list); 1854 1855 /* ASSOCRESP BSSID <res> */ 1856 if (hwaddr_aton(data, bss->bssid)) { 1857 wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in " 1858 "assocresp"); 1859 } 1860 if (drv->use_associnfo) { 1861 union wpa_event_data event; 1862 os_memset(&event, 0, sizeof(event)); 1863 event.assoc_info.req_ies = drv->assoc_wpa_ie; 1864 event.assoc_info.req_ies_len = drv->assoc_wpa_ie_len; 1865 wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &event); 1866 } 1867 drv->associated = 1; 1868 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL); 1869 } 1870 1871 1872 static void wpa_driver_test_disassoc(struct wpa_driver_test_data *drv, 1873 struct sockaddr *from, 1874 socklen_t fromlen) 1875 { 1876 drv->associated = 0; 1877 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL); 1878 } 1879 1880 1881 static void wpa_driver_test_eapol(struct wpa_driver_test_data *drv, 1882 struct sockaddr *from, 1883 socklen_t fromlen, 1884 const u8 *data, size_t data_len) 1885 { 1886 const u8 *src; 1887 struct test_driver_bss *bss; 1888 1889 bss = dl_list_first(&drv->bss, struct test_driver_bss, list); 1890 1891 if (data_len > 14) { 1892 /* Skip Ethernet header */ 1893 src = data + ETH_ALEN; 1894 data += 14; 1895 data_len -= 14; 1896 } else 1897 src = bss->bssid; 1898 1899 drv_event_eapol_rx(drv->ctx, src, data, data_len); 1900 } 1901 1902 1903 static void wpa_driver_test_mlme(struct wpa_driver_test_data *drv, 1904 struct sockaddr *from, 1905 socklen_t fromlen, 1906 const u8 *data, size_t data_len) 1907 { 1908 int freq = 0, own_freq; 1909 union wpa_event_data event; 1910 const struct ieee80211_mgmt *mgmt; 1911 u16 fc; 1912 struct test_driver_bss *bss; 1913 1914 bss = dl_list_first(&drv->bss, struct test_driver_bss, list); 1915 if (data_len > 6 && os_memcmp(data, "freq=", 5) == 0) { 1916 size_t pos; 1917 for (pos = 5; pos < data_len; pos++) { 1918 if (data[pos] == ' ') 1919 break; 1920 } 1921 if (pos < data_len) { 1922 freq = atoi((const char *) &data[5]); 1923 wpa_printf(MSG_DEBUG, "test_driver(%s): MLME RX on " 1924 "freq %d MHz", bss->ifname, freq); 1925 pos++; 1926 data += pos; 1927 data_len -= pos; 1928 } 1929 } 1930 1931 if (drv->remain_on_channel_freq) 1932 own_freq = drv->remain_on_channel_freq; 1933 else 1934 own_freq = drv->current_freq; 1935 1936 if (freq && own_freq && freq != own_freq) { 1937 wpa_printf(MSG_DEBUG, "test_driver(%s): Ignore MLME RX on " 1938 "another frequency %d MHz (own %d MHz)", 1939 bss->ifname, freq, own_freq); 1940 return; 1941 } 1942 1943 os_memset(&event, 0, sizeof(event)); 1944 event.mlme_rx.buf = data; 1945 event.mlme_rx.len = data_len; 1946 event.mlme_rx.freq = freq; 1947 wpa_supplicant_event(drv->ctx, EVENT_MLME_RX, &event); 1948 1949 mgmt = (const struct ieee80211_mgmt *) data; 1950 fc = le_to_host16(mgmt->frame_control); 1951 1952 if (drv->probe_req_report && data_len >= 24) { 1953 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && 1954 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ) { 1955 os_memset(&event, 0, sizeof(event)); 1956 event.rx_probe_req.sa = mgmt->sa; 1957 event.rx_probe_req.da = mgmt->da; 1958 event.rx_probe_req.bssid = mgmt->bssid; 1959 event.rx_probe_req.ie = mgmt->u.probe_req.variable; 1960 event.rx_probe_req.ie_len = 1961 data_len - (mgmt->u.probe_req.variable - data); 1962 wpa_supplicant_event(drv->ctx, EVENT_RX_PROBE_REQ, 1963 &event); 1964 #ifdef CONFIG_P2P 1965 if (drv->p2p) 1966 p2p_probe_req_rx(drv->p2p, mgmt->sa, 1967 mgmt->da, mgmt->bssid, 1968 event.rx_probe_req.ie, 1969 event.rx_probe_req.ie_len); 1970 #endif /* CONFIG_P2P */ 1971 } 1972 } 1973 1974 #ifdef CONFIG_P2P 1975 if (drv->p2p && 1976 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && 1977 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) { 1978 size_t hdr_len; 1979 hdr_len = (const u8 *) 1980 &mgmt->u.action.u.vs_public_action.action - data; 1981 p2p_rx_action(drv->p2p, mgmt->da, mgmt->sa, mgmt->bssid, 1982 mgmt->u.action.category, 1983 &mgmt->u.action.u.vs_public_action.action, 1984 data_len - hdr_len, freq); 1985 } 1986 #endif /* CONFIG_P2P */ 1987 1988 } 1989 1990 1991 static void wpa_driver_test_scan_cmd(struct wpa_driver_test_data *drv, 1992 struct sockaddr *from, 1993 socklen_t fromlen, 1994 const u8 *data, size_t data_len) 1995 { 1996 char buf[512], *pos, *end; 1997 int ret; 1998 struct test_driver_bss *bss; 1999 2000 bss = dl_list_first(&drv->bss, struct test_driver_bss, list); 2001 2002 /* data: optional [ STA-addr | ' ' | IEs(hex) ] */ 2003 #ifdef CONFIG_P2P 2004 if (drv->probe_req_report && drv->p2p && data_len) { 2005 const char *d = (const char *) data; 2006 u8 sa[ETH_ALEN]; 2007 u8 ie[512]; 2008 size_t ielen; 2009 2010 if (hwaddr_aton(d, sa)) 2011 return; 2012 d += 18; 2013 while (*d == ' ') 2014 d++; 2015 ielen = os_strlen(d) / 2; 2016 if (ielen > sizeof(ie)) 2017 ielen = sizeof(ie); 2018 if (hexstr2bin(d, ie, ielen) < 0) 2019 ielen = 0; 2020 drv->probe_from = from; 2021 drv->probe_from_len = fromlen; 2022 p2p_probe_req_rx(drv->p2p, sa, NULL, NULL, ie, ielen); 2023 drv->probe_from = NULL; 2024 } 2025 #endif /* CONFIG_P2P */ 2026 2027 if (!drv->ibss) 2028 return; 2029 2030 pos = buf; 2031 end = buf + sizeof(buf); 2032 2033 /* reply: SCANRESP BSSID SSID IEs */ 2034 ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ", 2035 MAC2STR(bss->bssid)); 2036 if (ret < 0 || ret >= end - pos) 2037 return; 2038 pos += ret; 2039 pos += wpa_snprintf_hex(pos, end - pos, 2040 bss->ssid, bss->ssid_len); 2041 ret = snprintf(pos, end - pos, " "); 2042 if (ret < 0 || ret >= end - pos) 2043 return; 2044 pos += ret; 2045 pos += wpa_snprintf_hex(pos, end - pos, drv->assoc_wpa_ie, 2046 drv->assoc_wpa_ie_len); 2047 2048 if (bss->privacy) { 2049 ret = snprintf(pos, end - pos, " PRIVACY"); 2050 if (ret < 0 || ret >= end - pos) 2051 return; 2052 pos += ret; 2053 } 2054 2055 ret = snprintf(pos, end - pos, " IBSS"); 2056 if (ret < 0 || ret >= end - pos) 2057 return; 2058 pos += ret; 2059 2060 sendto(drv->test_socket, buf, pos - buf, 0, 2061 (struct sockaddr *) from, fromlen); 2062 } 2063 2064 2065 static void wpa_driver_test_receive_unix(int sock, void *eloop_ctx, 2066 void *sock_ctx) 2067 { 2068 struct wpa_driver_test_data *drv = eloop_ctx; 2069 char *buf; 2070 int res; 2071 struct sockaddr_storage from; 2072 socklen_t fromlen = sizeof(from); 2073 const size_t buflen = 2000; 2074 2075 if (drv->ap) { 2076 test_driver_receive_unix(sock, eloop_ctx, sock_ctx); 2077 return; 2078 } 2079 2080 buf = os_malloc(buflen); 2081 if (buf == NULL) 2082 return; 2083 res = recvfrom(sock, buf, buflen - 1, 0, 2084 (struct sockaddr *) &from, &fromlen); 2085 if (res < 0) { 2086 perror("recvfrom(test_socket)"); 2087 os_free(buf); 2088 return; 2089 } 2090 buf[res] = '\0'; 2091 2092 wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res); 2093 2094 if (os_strncmp(buf, "SCANRESP ", 9) == 0) { 2095 wpa_driver_test_scanresp(drv, (struct sockaddr *) &from, 2096 fromlen, buf + 9); 2097 } else if (os_strncmp(buf, "ASSOCRESP ", 10) == 0) { 2098 wpa_driver_test_assocresp(drv, (struct sockaddr *) &from, 2099 fromlen, buf + 10); 2100 } else if (os_strcmp(buf, "DISASSOC") == 0) { 2101 wpa_driver_test_disassoc(drv, (struct sockaddr *) &from, 2102 fromlen); 2103 } else if (os_strcmp(buf, "DEAUTH") == 0) { 2104 wpa_driver_test_disassoc(drv, (struct sockaddr *) &from, 2105 fromlen); 2106 } else if (os_strncmp(buf, "EAPOL ", 6) == 0) { 2107 wpa_driver_test_eapol(drv, (struct sockaddr *) &from, fromlen, 2108 (const u8 *) buf + 6, res - 6); 2109 } else if (os_strncmp(buf, "MLME ", 5) == 0) { 2110 wpa_driver_test_mlme(drv, (struct sockaddr *) &from, fromlen, 2111 (const u8 *) buf + 5, res - 5); 2112 } else if (os_strncmp(buf, "SCAN ", 5) == 0) { 2113 wpa_driver_test_scan_cmd(drv, (struct sockaddr *) &from, 2114 fromlen, 2115 (const u8 *) buf + 5, res - 5); 2116 } else { 2117 wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command", 2118 (u8 *) buf, res); 2119 } 2120 os_free(buf); 2121 } 2122 2123 2124 static void * wpa_driver_test_init2(void *ctx, const char *ifname, 2125 void *global_priv) 2126 { 2127 struct wpa_driver_test_data *drv; 2128 struct wpa_driver_test_global *global = global_priv; 2129 struct test_driver_bss *bss; 2130 2131 drv = test_alloc_data(ctx, ifname); 2132 if (drv == NULL) 2133 return NULL; 2134 bss = dl_list_first(&drv->bss, struct test_driver_bss, list); 2135 drv->global = global_priv; 2136 drv->test_socket = -1; 2137 2138 /* Set dummy BSSID and SSID for testing. */ 2139 bss->bssid[0] = 0x02; 2140 bss->bssid[1] = 0x00; 2141 bss->bssid[2] = 0x00; 2142 bss->bssid[3] = 0x00; 2143 bss->bssid[4] = 0x00; 2144 bss->bssid[5] = 0x01; 2145 os_memcpy(bss->ssid, "test", 5); 2146 bss->ssid_len = 4; 2147 2148 if (global->bss_add_used) { 2149 os_memcpy(drv->own_addr, global->req_addr, ETH_ALEN); 2150 global->bss_add_used = 0; 2151 } 2152 2153 eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL); 2154 2155 return bss; 2156 } 2157 2158 2159 static void wpa_driver_test_close_test_socket(struct wpa_driver_test_data *drv) 2160 { 2161 if (drv->test_socket >= 0) { 2162 eloop_unregister_read_sock(drv->test_socket); 2163 close(drv->test_socket); 2164 drv->test_socket = -1; 2165 } 2166 2167 if (drv->own_socket_path) { 2168 unlink(drv->own_socket_path); 2169 os_free(drv->own_socket_path); 2170 drv->own_socket_path = NULL; 2171 } 2172 } 2173 2174 2175 static void wpa_driver_test_deinit(void *priv) 2176 { 2177 struct test_driver_bss *dbss = priv; 2178 struct wpa_driver_test_data *drv = dbss->drv; 2179 struct test_client_socket *cli, *prev; 2180 int i; 2181 2182 #ifdef CONFIG_P2P 2183 if (drv->p2p) 2184 p2p_deinit(drv->p2p); 2185 wpabuf_free(drv->pending_action_tx); 2186 #endif /* CONFIG_P2P */ 2187 2188 cli = drv->cli; 2189 while (cli) { 2190 prev = cli; 2191 cli = cli->next; 2192 os_free(prev); 2193 } 2194 2195 #ifdef HOSTAPD 2196 /* There should be only one BSS remaining at this point. */ 2197 if (dl_list_len(&drv->bss) != 1) 2198 wpa_printf(MSG_ERROR, "%s: %u remaining BSS entries", 2199 __func__, dl_list_len(&drv->bss)); 2200 #endif /* HOSTAPD */ 2201 2202 test_driver_free_bsses(drv); 2203 2204 wpa_driver_test_close_test_socket(drv); 2205 eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx); 2206 eloop_cancel_timeout(wpa_driver_test_poll, drv, NULL); 2207 eloop_cancel_timeout(test_remain_on_channel_timeout, drv, NULL); 2208 os_free(drv->test_dir); 2209 for (i = 0; i < MAX_SCAN_RESULTS; i++) 2210 os_free(drv->scanres[i]); 2211 os_free(drv->probe_req_ie); 2212 wpa_trace_remove_ref(drv, ctx, drv->ctx); 2213 os_free(drv); 2214 } 2215 2216 2217 static int wpa_driver_test_attach(struct wpa_driver_test_data *drv, 2218 const char *dir, int ap) 2219 { 2220 #ifdef DRIVER_TEST_UNIX 2221 static unsigned int counter = 0; 2222 struct sockaddr_un addr; 2223 size_t len; 2224 2225 os_free(drv->own_socket_path); 2226 if (dir) { 2227 len = os_strlen(dir) + 30; 2228 drv->own_socket_path = os_malloc(len); 2229 if (drv->own_socket_path == NULL) 2230 return -1; 2231 os_snprintf(drv->own_socket_path, len, "%s/%s-" MACSTR, 2232 dir, ap ? "AP" : "STA", MAC2STR(drv->own_addr)); 2233 } else { 2234 drv->own_socket_path = os_malloc(100); 2235 if (drv->own_socket_path == NULL) 2236 return -1; 2237 os_snprintf(drv->own_socket_path, 100, 2238 "/tmp/wpa_supplicant_test-%d-%d", 2239 getpid(), counter++); 2240 } 2241 2242 drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0); 2243 if (drv->test_socket < 0) { 2244 perror("socket(PF_UNIX)"); 2245 os_free(drv->own_socket_path); 2246 drv->own_socket_path = NULL; 2247 return -1; 2248 } 2249 2250 os_memset(&addr, 0, sizeof(addr)); 2251 addr.sun_family = AF_UNIX; 2252 os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path)); 2253 if (bind(drv->test_socket, (struct sockaddr *) &addr, 2254 sizeof(addr)) < 0) { 2255 perror("bind(PF_UNIX)"); 2256 close(drv->test_socket); 2257 unlink(drv->own_socket_path); 2258 os_free(drv->own_socket_path); 2259 drv->own_socket_path = NULL; 2260 return -1; 2261 } 2262 2263 eloop_register_read_sock(drv->test_socket, 2264 wpa_driver_test_receive_unix, drv, NULL); 2265 2266 return 0; 2267 #else /* DRIVER_TEST_UNIX */ 2268 return -1; 2269 #endif /* DRIVER_TEST_UNIX */ 2270 } 2271 2272 2273 static int wpa_driver_test_attach_udp(struct wpa_driver_test_data *drv, 2274 char *dst) 2275 { 2276 char *pos; 2277 2278 pos = os_strchr(dst, ':'); 2279 if (pos == NULL) 2280 return -1; 2281 *pos++ = '\0'; 2282 wpa_printf(MSG_DEBUG, "%s: addr=%s port=%s", __func__, dst, pos); 2283 2284 drv->test_socket = socket(PF_INET, SOCK_DGRAM, 0); 2285 if (drv->test_socket < 0) { 2286 perror("socket(PF_INET)"); 2287 return -1; 2288 } 2289 2290 os_memset(&drv->hostapd_addr_udp, 0, sizeof(drv->hostapd_addr_udp)); 2291 drv->hostapd_addr_udp.sin_family = AF_INET; 2292 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA) 2293 { 2294 int a[4]; 2295 u8 *pos; 2296 sscanf(dst, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]); 2297 pos = (u8 *) &drv->hostapd_addr_udp.sin_addr; 2298 *pos++ = a[0]; 2299 *pos++ = a[1]; 2300 *pos++ = a[2]; 2301 *pos++ = a[3]; 2302 } 2303 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */ 2304 inet_aton(dst, &drv->hostapd_addr_udp.sin_addr); 2305 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */ 2306 drv->hostapd_addr_udp.sin_port = htons(atoi(pos)); 2307 2308 drv->hostapd_addr_udp_set = 1; 2309 2310 eloop_register_read_sock(drv->test_socket, 2311 wpa_driver_test_receive_unix, drv, NULL); 2312 2313 return 0; 2314 } 2315 2316 2317 static int wpa_driver_test_set_param(void *priv, const char *param) 2318 { 2319 struct test_driver_bss *dbss = priv; 2320 struct wpa_driver_test_data *drv = dbss->drv; 2321 const char *pos; 2322 2323 wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param); 2324 if (param == NULL) 2325 return 0; 2326 2327 wpa_driver_test_close_test_socket(drv); 2328 2329 #ifdef DRIVER_TEST_UNIX 2330 pos = os_strstr(param, "test_socket="); 2331 if (pos) { 2332 const char *pos2; 2333 size_t len; 2334 2335 pos += 12; 2336 pos2 = os_strchr(pos, ' '); 2337 if (pos2) 2338 len = pos2 - pos; 2339 else 2340 len = os_strlen(pos); 2341 if (len > sizeof(drv->hostapd_addr.sun_path)) 2342 return -1; 2343 os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr)); 2344 drv->hostapd_addr.sun_family = AF_UNIX; 2345 os_memcpy(drv->hostapd_addr.sun_path, pos, len); 2346 drv->hostapd_addr_set = 1; 2347 } 2348 #endif /* DRIVER_TEST_UNIX */ 2349 2350 pos = os_strstr(param, "test_dir="); 2351 if (pos) { 2352 char *end; 2353 os_free(drv->test_dir); 2354 drv->test_dir = os_strdup(pos + 9); 2355 if (drv->test_dir == NULL) 2356 return -1; 2357 end = os_strchr(drv->test_dir, ' '); 2358 if (end) 2359 *end = '\0'; 2360 if (wpa_driver_test_attach(drv, drv->test_dir, 0)) 2361 return -1; 2362 } else { 2363 pos = os_strstr(param, "test_udp="); 2364 if (pos) { 2365 char *dst, *epos; 2366 dst = os_strdup(pos + 9); 2367 if (dst == NULL) 2368 return -1; 2369 epos = os_strchr(dst, ' '); 2370 if (epos) 2371 *epos = '\0'; 2372 if (wpa_driver_test_attach_udp(drv, dst)) 2373 return -1; 2374 os_free(dst); 2375 } else if (wpa_driver_test_attach(drv, NULL, 0)) 2376 return -1; 2377 } 2378 2379 if (os_strstr(param, "use_associnfo=1")) { 2380 wpa_printf(MSG_DEBUG, "test_driver: Use AssocInfo events"); 2381 drv->use_associnfo = 1; 2382 } 2383 2384 if (os_strstr(param, "p2p_mgmt=1")) { 2385 wpa_printf(MSG_DEBUG, "test_driver: Use internal P2P " 2386 "management"); 2387 if (wpa_driver_test_init_p2p(drv) < 0) 2388 return -1; 2389 } 2390 2391 return 0; 2392 } 2393 2394 2395 static const u8 * wpa_driver_test_get_mac_addr(void *priv) 2396 { 2397 struct test_driver_bss *dbss = priv; 2398 struct wpa_driver_test_data *drv = dbss->drv; 2399 wpa_printf(MSG_DEBUG, "%s", __func__); 2400 return drv->own_addr; 2401 } 2402 2403 2404 static int wpa_driver_test_send_eapol(void *priv, const u8 *dest, u16 proto, 2405 const u8 *data, size_t data_len) 2406 { 2407 struct test_driver_bss *dbss = priv; 2408 struct wpa_driver_test_data *drv = dbss->drv; 2409 char *msg; 2410 size_t msg_len; 2411 struct l2_ethhdr eth; 2412 struct sockaddr *addr; 2413 socklen_t alen; 2414 #ifdef DRIVER_TEST_UNIX 2415 struct sockaddr_un addr_un; 2416 #endif /* DRIVER_TEST_UNIX */ 2417 2418 wpa_hexdump(MSG_MSGDUMP, "test_send_eapol TX frame", data, data_len); 2419 2420 os_memset(ð, 0, sizeof(eth)); 2421 os_memcpy(eth.h_dest, dest, ETH_ALEN); 2422 os_memcpy(eth.h_source, drv->own_addr, ETH_ALEN); 2423 eth.h_proto = host_to_be16(proto); 2424 2425 msg_len = 6 + sizeof(eth) + data_len; 2426 msg = os_malloc(msg_len); 2427 if (msg == NULL) 2428 return -1; 2429 os_memcpy(msg, "EAPOL ", 6); 2430 os_memcpy(msg + 6, ð, sizeof(eth)); 2431 os_memcpy(msg + 6 + sizeof(eth), data, data_len); 2432 2433 if (os_memcmp(dest, dbss->bssid, ETH_ALEN) == 0 || 2434 drv->test_dir == NULL) { 2435 if (drv->hostapd_addr_udp_set) { 2436 addr = (struct sockaddr *) &drv->hostapd_addr_udp; 2437 alen = sizeof(drv->hostapd_addr_udp); 2438 } else { 2439 #ifdef DRIVER_TEST_UNIX 2440 addr = (struct sockaddr *) &drv->hostapd_addr; 2441 alen = sizeof(drv->hostapd_addr); 2442 #else /* DRIVER_TEST_UNIX */ 2443 os_free(msg); 2444 return -1; 2445 #endif /* DRIVER_TEST_UNIX */ 2446 } 2447 } else { 2448 #ifdef DRIVER_TEST_UNIX 2449 struct stat st; 2450 os_memset(&addr_un, 0, sizeof(addr_un)); 2451 addr_un.sun_family = AF_UNIX; 2452 os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path), 2453 "%s/STA-" MACSTR, drv->test_dir, MAC2STR(dest)); 2454 if (stat(addr_un.sun_path, &st) < 0) { 2455 os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path), 2456 "%s/AP-" MACSTR, 2457 drv->test_dir, MAC2STR(dest)); 2458 } 2459 addr = (struct sockaddr *) &addr_un; 2460 alen = sizeof(addr_un); 2461 #else /* DRIVER_TEST_UNIX */ 2462 os_free(msg); 2463 return -1; 2464 #endif /* DRIVER_TEST_UNIX */ 2465 } 2466 2467 if (sendto(drv->test_socket, msg, msg_len, 0, addr, alen) < 0) { 2468 perror("sendmsg(test_socket)"); 2469 os_free(msg); 2470 return -1; 2471 } 2472 2473 os_free(msg); 2474 return 0; 2475 } 2476 2477 2478 static int wpa_driver_test_get_capa(void *priv, struct wpa_driver_capa *capa) 2479 { 2480 struct test_driver_bss *dbss = priv; 2481 struct wpa_driver_test_data *drv = dbss->drv; 2482 os_memset(capa, 0, sizeof(*capa)); 2483 capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | 2484 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | 2485 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK | 2486 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK | 2487 WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE | 2488 WPA_DRIVER_CAPA_KEY_MGMT_FT | 2489 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK; 2490 capa->enc = WPA_DRIVER_CAPA_ENC_WEP40 | 2491 WPA_DRIVER_CAPA_ENC_WEP104 | 2492 WPA_DRIVER_CAPA_ENC_TKIP | 2493 WPA_DRIVER_CAPA_ENC_CCMP; 2494 capa->auth = WPA_DRIVER_AUTH_OPEN | 2495 WPA_DRIVER_AUTH_SHARED | 2496 WPA_DRIVER_AUTH_LEAP; 2497 if (drv->p2p) 2498 capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT; 2499 capa->flags |= WPA_DRIVER_FLAGS_AP; 2500 capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT; 2501 capa->flags |= WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE; 2502 capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE; 2503 capa->max_scan_ssids = 2; 2504 capa->max_remain_on_chan = 60000; 2505 2506 return 0; 2507 } 2508 2509 2510 static int wpa_driver_test_mlme_setprotection(void *priv, const u8 *addr, 2511 int protect_type, 2512 int key_type) 2513 { 2514 wpa_printf(MSG_DEBUG, "%s: protect_type=%d key_type=%d", 2515 __func__, protect_type, key_type); 2516 2517 if (addr) { 2518 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, 2519 __func__, MAC2STR(addr)); 2520 } 2521 2522 return 0; 2523 } 2524 2525 2526 static void * wpa_driver_test_global_init(void) 2527 { 2528 struct wpa_driver_test_global *global; 2529 2530 global = os_zalloc(sizeof(*global)); 2531 return global; 2532 } 2533 2534 2535 static void wpa_driver_test_global_deinit(void *priv) 2536 { 2537 struct wpa_driver_test_global *global = priv; 2538 os_free(global); 2539 } 2540 2541 2542 static struct wpa_interface_info * 2543 wpa_driver_test_get_interfaces(void *global_priv) 2544 { 2545 /* struct wpa_driver_test_global *global = priv; */ 2546 struct wpa_interface_info *iface; 2547 2548 iface = os_zalloc(sizeof(*iface)); 2549 if (iface == NULL) 2550 return iface; 2551 iface->ifname = os_strdup("sta0"); 2552 iface->desc = os_strdup("test interface 0"); 2553 iface->drv_name = "test"; 2554 iface->next = os_zalloc(sizeof(*iface)); 2555 if (iface->next) { 2556 iface->next->ifname = os_strdup("sta1"); 2557 iface->next->desc = os_strdup("test interface 1"); 2558 iface->next->drv_name = "test"; 2559 } 2560 2561 return iface; 2562 } 2563 2564 2565 static struct hostapd_hw_modes * 2566 wpa_driver_test_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags) 2567 { 2568 struct hostapd_hw_modes *modes; 2569 size_t i; 2570 2571 *num_modes = 3; 2572 *flags = 0; 2573 modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes)); 2574 if (modes == NULL) 2575 return NULL; 2576 modes[0].mode = HOSTAPD_MODE_IEEE80211G; 2577 modes[0].num_channels = 11; 2578 modes[0].num_rates = 12; 2579 modes[0].channels = 2580 os_zalloc(11 * sizeof(struct hostapd_channel_data)); 2581 modes[0].rates = os_zalloc(modes[0].num_rates * sizeof(int)); 2582 if (modes[0].channels == NULL || modes[0].rates == NULL) 2583 goto fail; 2584 for (i = 0; i < 11; i++) { 2585 modes[0].channels[i].chan = i + 1; 2586 modes[0].channels[i].freq = 2412 + 5 * i; 2587 modes[0].channels[i].flag = 0; 2588 } 2589 modes[0].rates[0] = 10; 2590 modes[0].rates[1] = 20; 2591 modes[0].rates[2] = 55; 2592 modes[0].rates[3] = 110; 2593 modes[0].rates[4] = 60; 2594 modes[0].rates[5] = 90; 2595 modes[0].rates[6] = 120; 2596 modes[0].rates[7] = 180; 2597 modes[0].rates[8] = 240; 2598 modes[0].rates[9] = 360; 2599 modes[0].rates[10] = 480; 2600 modes[0].rates[11] = 540; 2601 2602 modes[1].mode = HOSTAPD_MODE_IEEE80211B; 2603 modes[1].num_channels = 11; 2604 modes[1].num_rates = 4; 2605 modes[1].channels = 2606 os_zalloc(11 * sizeof(struct hostapd_channel_data)); 2607 modes[1].rates = os_zalloc(modes[1].num_rates * sizeof(int)); 2608 if (modes[1].channels == NULL || modes[1].rates == NULL) 2609 goto fail; 2610 for (i = 0; i < 11; i++) { 2611 modes[1].channels[i].chan = i + 1; 2612 modes[1].channels[i].freq = 2412 + 5 * i; 2613 modes[1].channels[i].flag = 0; 2614 } 2615 modes[1].rates[0] = 10; 2616 modes[1].rates[1] = 20; 2617 modes[1].rates[2] = 55; 2618 modes[1].rates[3] = 110; 2619 2620 modes[2].mode = HOSTAPD_MODE_IEEE80211A; 2621 modes[2].num_channels = 1; 2622 modes[2].num_rates = 8; 2623 modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data)); 2624 modes[2].rates = os_zalloc(modes[2].num_rates * sizeof(int)); 2625 if (modes[2].channels == NULL || modes[2].rates == NULL) 2626 goto fail; 2627 modes[2].channels[0].chan = 60; 2628 modes[2].channels[0].freq = 5300; 2629 modes[2].channels[0].flag = 0; 2630 modes[2].rates[0] = 60; 2631 modes[2].rates[1] = 90; 2632 modes[2].rates[2] = 120; 2633 modes[2].rates[3] = 180; 2634 modes[2].rates[4] = 240; 2635 modes[2].rates[5] = 360; 2636 modes[2].rates[6] = 480; 2637 modes[2].rates[7] = 540; 2638 2639 return modes; 2640 2641 fail: 2642 if (modes) { 2643 for (i = 0; i < *num_modes; i++) { 2644 os_free(modes[i].channels); 2645 os_free(modes[i].rates); 2646 } 2647 os_free(modes); 2648 } 2649 return NULL; 2650 } 2651 2652 2653 static int wpa_driver_test_set_freq(void *priv, 2654 struct hostapd_freq_params *freq) 2655 { 2656 struct test_driver_bss *dbss = priv; 2657 struct wpa_driver_test_data *drv = dbss->drv; 2658 wpa_printf(MSG_DEBUG, "test: set_freq %u MHz", freq->freq); 2659 drv->current_freq = freq->freq; 2660 return 0; 2661 } 2662 2663 2664 static int wpa_driver_test_send_action(void *priv, unsigned int freq, 2665 unsigned int wait, 2666 const u8 *dst, const u8 *src, 2667 const u8 *bssid, 2668 const u8 *data, size_t data_len, 2669 int no_cck) 2670 { 2671 struct test_driver_bss *dbss = priv; 2672 struct wpa_driver_test_data *drv = dbss->drv; 2673 int ret = -1; 2674 u8 *buf; 2675 struct ieee80211_hdr *hdr; 2676 2677 wpa_printf(MSG_DEBUG, "test: Send Action frame"); 2678 2679 if ((drv->remain_on_channel_freq && 2680 freq != drv->remain_on_channel_freq) || 2681 (drv->remain_on_channel_freq == 0 && 2682 freq != (unsigned int) drv->current_freq)) { 2683 wpa_printf(MSG_DEBUG, "test: Reject Action frame TX on " 2684 "unexpected channel: freq=%u MHz (current_freq=%u " 2685 "MHz, remain-on-channel freq=%u MHz)", 2686 freq, drv->current_freq, 2687 drv->remain_on_channel_freq); 2688 return -1; 2689 } 2690 2691 buf = os_zalloc(24 + data_len); 2692 if (buf == NULL) 2693 return ret; 2694 os_memcpy(buf + 24, data, data_len); 2695 hdr = (struct ieee80211_hdr *) buf; 2696 hdr->frame_control = 2697 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION); 2698 os_memcpy(hdr->addr1, dst, ETH_ALEN); 2699 os_memcpy(hdr->addr2, src, ETH_ALEN); 2700 os_memcpy(hdr->addr3, bssid, ETH_ALEN); 2701 2702 ret = wpa_driver_test_send_mlme(priv, buf, 24 + data_len, 0); 2703 os_free(buf); 2704 return ret; 2705 } 2706 2707 2708 #ifdef CONFIG_P2P 2709 static void test_send_action_cb(void *eloop_ctx, void *timeout_ctx) 2710 { 2711 struct wpa_driver_test_data *drv = eloop_ctx; 2712 2713 if (drv->pending_action_tx == NULL) 2714 return; 2715 2716 if (drv->off_channel_freq != drv->pending_action_freq) { 2717 wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX " 2718 "waiting for another freq=%u", 2719 drv->pending_action_freq); 2720 return; 2721 } 2722 wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to " 2723 MACSTR, MAC2STR(drv->pending_action_dst)); 2724 wpa_driver_test_send_action(drv, drv->pending_action_freq, 0, 2725 drv->pending_action_dst, 2726 drv->pending_action_src, 2727 drv->pending_action_bssid, 2728 wpabuf_head(drv->pending_action_tx), 2729 wpabuf_len(drv->pending_action_tx), 2730 drv->pending_action_no_cck); 2731 } 2732 #endif /* CONFIG_P2P */ 2733 2734 2735 static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx) 2736 { 2737 struct wpa_driver_test_data *drv = eloop_ctx; 2738 union wpa_event_data data; 2739 2740 wpa_printf(MSG_DEBUG, "test: Remain-on-channel timeout"); 2741 2742 os_memset(&data, 0, sizeof(data)); 2743 data.remain_on_channel.freq = drv->remain_on_channel_freq; 2744 data.remain_on_channel.duration = drv->remain_on_channel_duration; 2745 2746 if (drv->p2p) 2747 drv->off_channel_freq = 0; 2748 2749 drv->remain_on_channel_freq = 0; 2750 2751 wpa_supplicant_event(drv->ctx, EVENT_CANCEL_REMAIN_ON_CHANNEL, &data); 2752 } 2753 2754 2755 static int wpa_driver_test_remain_on_channel(void *priv, unsigned int freq, 2756 unsigned int duration) 2757 { 2758 struct test_driver_bss *dbss = priv; 2759 struct wpa_driver_test_data *drv = dbss->drv; 2760 union wpa_event_data data; 2761 2762 wpa_printf(MSG_DEBUG, "%s(freq=%u, duration=%u)", 2763 __func__, freq, duration); 2764 if (drv->remain_on_channel_freq && 2765 drv->remain_on_channel_freq != freq) { 2766 wpa_printf(MSG_DEBUG, "test: Refuse concurrent " 2767 "remain_on_channel request"); 2768 return -1; 2769 } 2770 2771 drv->remain_on_channel_freq = freq; 2772 drv->remain_on_channel_duration = duration; 2773 eloop_cancel_timeout(test_remain_on_channel_timeout, drv, NULL); 2774 eloop_register_timeout(duration / 1000, (duration % 1000) * 1000, 2775 test_remain_on_channel_timeout, drv, NULL); 2776 2777 os_memset(&data, 0, sizeof(data)); 2778 data.remain_on_channel.freq = freq; 2779 data.remain_on_channel.duration = duration; 2780 wpa_supplicant_event(drv->ctx, EVENT_REMAIN_ON_CHANNEL, &data); 2781 2782 #ifdef CONFIG_P2P 2783 if (drv->p2p) { 2784 drv->off_channel_freq = drv->remain_on_channel_freq; 2785 test_send_action_cb(drv, NULL); 2786 if (drv->off_channel_freq == drv->pending_listen_freq) { 2787 p2p_listen_cb(drv->p2p, drv->pending_listen_freq, 2788 drv->pending_listen_duration); 2789 drv->pending_listen_freq = 0; 2790 } 2791 } 2792 #endif /* CONFIG_P2P */ 2793 2794 return 0; 2795 } 2796 2797 2798 static int wpa_driver_test_cancel_remain_on_channel(void *priv) 2799 { 2800 struct test_driver_bss *dbss = priv; 2801 struct wpa_driver_test_data *drv = dbss->drv; 2802 wpa_printf(MSG_DEBUG, "%s", __func__); 2803 if (!drv->remain_on_channel_freq) 2804 return -1; 2805 drv->remain_on_channel_freq = 0; 2806 eloop_cancel_timeout(test_remain_on_channel_timeout, drv, NULL); 2807 return 0; 2808 } 2809 2810 2811 static int wpa_driver_test_probe_req_report(void *priv, int report) 2812 { 2813 struct test_driver_bss *dbss = priv; 2814 struct wpa_driver_test_data *drv = dbss->drv; 2815 wpa_printf(MSG_DEBUG, "%s(report=%d)", __func__, report); 2816 drv->probe_req_report = report; 2817 return 0; 2818 } 2819 2820 2821 #ifdef CONFIG_P2P 2822 2823 static int wpa_driver_test_p2p_find(void *priv, unsigned int timeout, int type) 2824 { 2825 struct wpa_driver_test_data *drv = priv; 2826 wpa_printf(MSG_DEBUG, "%s(timeout=%u)", __func__, timeout); 2827 if (!drv->p2p) 2828 return -1; 2829 return p2p_find(drv->p2p, timeout, type, 0, NULL, NULL); 2830 } 2831 2832 2833 static int wpa_driver_test_p2p_stop_find(void *priv) 2834 { 2835 struct wpa_driver_test_data *drv = priv; 2836 wpa_printf(MSG_DEBUG, "%s", __func__); 2837 if (!drv->p2p) 2838 return -1; 2839 p2p_stop_find(drv->p2p); 2840 return 0; 2841 } 2842 2843 2844 static int wpa_driver_test_p2p_listen(void *priv, unsigned int timeout) 2845 { 2846 struct wpa_driver_test_data *drv = priv; 2847 wpa_printf(MSG_DEBUG, "%s(timeout=%u)", __func__, timeout); 2848 if (!drv->p2p) 2849 return -1; 2850 return p2p_listen(drv->p2p, timeout); 2851 } 2852 2853 2854 static int wpa_driver_test_p2p_connect(void *priv, const u8 *peer_addr, 2855 int wps_method, int go_intent, 2856 const u8 *own_interface_addr, 2857 unsigned int force_freq, 2858 int persistent_group) 2859 { 2860 struct wpa_driver_test_data *drv = priv; 2861 wpa_printf(MSG_DEBUG, "%s(peer_addr=" MACSTR " wps_method=%d " 2862 "go_intent=%d " 2863 "own_interface_addr=" MACSTR " force_freq=%u " 2864 "persistent_group=%d)", 2865 __func__, MAC2STR(peer_addr), wps_method, go_intent, 2866 MAC2STR(own_interface_addr), force_freq, persistent_group); 2867 if (!drv->p2p) 2868 return -1; 2869 return p2p_connect(drv->p2p, peer_addr, wps_method, go_intent, 2870 own_interface_addr, force_freq, persistent_group); 2871 } 2872 2873 2874 static int wpa_driver_test_wps_success_cb(void *priv, const u8 *peer_addr) 2875 { 2876 struct wpa_driver_test_data *drv = priv; 2877 wpa_printf(MSG_DEBUG, "%s(peer_addr=" MACSTR ")", 2878 __func__, MAC2STR(peer_addr)); 2879 if (!drv->p2p) 2880 return -1; 2881 p2p_wps_success_cb(drv->p2p, peer_addr); 2882 return 0; 2883 } 2884 2885 2886 static int wpa_driver_test_p2p_group_formation_failed(void *priv) 2887 { 2888 struct wpa_driver_test_data *drv = priv; 2889 wpa_printf(MSG_DEBUG, "%s", __func__); 2890 if (!drv->p2p) 2891 return -1; 2892 p2p_group_formation_failed(drv->p2p); 2893 return 0; 2894 } 2895 2896 2897 static int wpa_driver_test_p2p_set_params(void *priv, 2898 const struct p2p_params *params) 2899 { 2900 struct wpa_driver_test_data *drv = priv; 2901 wpa_printf(MSG_DEBUG, "%s", __func__); 2902 if (!drv->p2p) 2903 return -1; 2904 if (p2p_set_dev_name(drv->p2p, params->dev_name) < 0 || 2905 p2p_set_pri_dev_type(drv->p2p, params->pri_dev_type) < 0 || 2906 p2p_set_sec_dev_types(drv->p2p, params->sec_dev_type, 2907 params->num_sec_dev_types) < 0) 2908 return -1; 2909 return 0; 2910 } 2911 2912 2913 static int test_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, 2914 unsigned int num_req_dev_types, 2915 const u8 *req_dev_types, const u8 *dev_id) 2916 { 2917 struct wpa_driver_test_data *drv = ctx; 2918 struct wpa_driver_scan_params params; 2919 int ret; 2920 struct wpabuf *wps_ie, *ies; 2921 int social_channels[] = { 2412, 2437, 2462, 0, 0 }; 2922 size_t ielen; 2923 2924 wpa_printf(MSG_DEBUG, "%s(type=%d freq=%d)", 2925 __func__, type, freq); 2926 2927 os_memset(¶ms, 0, sizeof(params)); 2928 2929 /* P2P Wildcard SSID */ 2930 params.num_ssids = 1; 2931 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; 2932 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; 2933 2934 #if 0 /* TODO: WPS IE */ 2935 wpa_s->wps->dev.p2p = 1; 2936 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid, 2937 WPS_REQ_ENROLLEE); 2938 #else 2939 wps_ie = wpabuf_alloc(1); 2940 #endif 2941 if (wps_ie == NULL) 2942 return -1; 2943 2944 ielen = p2p_scan_ie_buf_len(drv->p2p); 2945 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); 2946 if (ies == NULL) { 2947 wpabuf_free(wps_ie); 2948 return -1; 2949 } 2950 wpabuf_put_buf(ies, wps_ie); 2951 wpabuf_free(wps_ie); 2952 2953 p2p_scan_ie(drv->p2p, ies, dev_id); 2954 2955 params.extra_ies = wpabuf_head(ies); 2956 params.extra_ies_len = wpabuf_len(ies); 2957 2958 switch (type) { 2959 case P2P_SCAN_SOCIAL: 2960 params.freqs = social_channels; 2961 break; 2962 case P2P_SCAN_FULL: 2963 break; 2964 case P2P_SCAN_SPECIFIC: 2965 social_channels[0] = freq; 2966 social_channels[1] = 0; 2967 params.freqs = social_channels; 2968 break; 2969 case P2P_SCAN_SOCIAL_PLUS_ONE: 2970 social_channels[3] = freq; 2971 params.freqs = social_channels; 2972 break; 2973 } 2974 2975 drv->pending_p2p_scan = 1; 2976 ret = wpa_driver_test_scan(drv, ¶ms); 2977 2978 wpabuf_free(ies); 2979 2980 return ret; 2981 } 2982 2983 2984 static int test_send_action(void *ctx, unsigned int freq, const u8 *dst, 2985 const u8 *src, const u8 *bssid, const u8 *buf, 2986 size_t len, unsigned int wait_time) 2987 { 2988 struct wpa_driver_test_data *drv = ctx; 2989 2990 wpa_printf(MSG_DEBUG, "%s(freq=%u dst=" MACSTR " src=" MACSTR 2991 " bssid=" MACSTR " len=%d", 2992 __func__, freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid), 2993 (int) len); 2994 if (freq <= 0) { 2995 wpa_printf(MSG_WARNING, "P2P: No frequency specified for " 2996 "action frame TX"); 2997 return -1; 2998 } 2999 3000 if (drv->pending_action_tx) { 3001 wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX " 3002 "to " MACSTR, MAC2STR(drv->pending_action_dst)); 3003 wpabuf_free(drv->pending_action_tx); 3004 } 3005 drv->pending_action_tx = wpabuf_alloc(len); 3006 if (drv->pending_action_tx == NULL) 3007 return -1; 3008 wpabuf_put_data(drv->pending_action_tx, buf, len); 3009 os_memcpy(drv->pending_action_src, src, ETH_ALEN); 3010 os_memcpy(drv->pending_action_dst, dst, ETH_ALEN); 3011 os_memcpy(drv->pending_action_bssid, bssid, ETH_ALEN); 3012 drv->pending_action_freq = freq; 3013 drv->pending_action_no_cck = 1; 3014 3015 if (drv->off_channel_freq == freq) { 3016 /* Already on requested channel; send immediately */ 3017 /* TODO: Would there ever be need to extend the current 3018 * duration on the channel? */ 3019 eloop_cancel_timeout(test_send_action_cb, drv, NULL); 3020 eloop_register_timeout(0, 0, test_send_action_cb, drv, NULL); 3021 return 0; 3022 } 3023 3024 wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted " 3025 "once the driver gets to the requested channel"); 3026 if (wpa_driver_test_remain_on_channel(drv, freq, wait_time) < 0) { 3027 wpa_printf(MSG_DEBUG, "P2P: Failed to request driver " 3028 "to remain on channel (%u MHz) for Action " 3029 "Frame TX", freq); 3030 return -1; 3031 } 3032 3033 return 0; 3034 } 3035 3036 3037 static void test_send_action_done(void *ctx) 3038 { 3039 wpa_printf(MSG_DEBUG, "%s", __func__); 3040 /* TODO */ 3041 } 3042 3043 3044 static void test_go_neg_completed(void *ctx, struct p2p_go_neg_results *res) 3045 { 3046 struct wpa_driver_test_data *drv = ctx; 3047 union wpa_event_data event; 3048 wpa_printf(MSG_DEBUG, "%s", __func__); 3049 os_memset(&event, 0, sizeof(event)); 3050 event.p2p_go_neg_completed.res = res; 3051 wpa_supplicant_event(drv->ctx, EVENT_P2P_GO_NEG_COMPLETED, &event); 3052 } 3053 3054 3055 static void test_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id) 3056 { 3057 struct wpa_driver_test_data *drv = ctx; 3058 union wpa_event_data event; 3059 wpa_printf(MSG_DEBUG, "%s(src=" MACSTR ")", __func__, MAC2STR(src)); 3060 os_memset(&event, 0, sizeof(event)); 3061 event.p2p_go_neg_req_rx.src = src; 3062 event.p2p_go_neg_req_rx.dev_passwd_id = dev_passwd_id; 3063 wpa_supplicant_event(drv->ctx, EVENT_P2P_GO_NEG_REQ_RX, &event); 3064 } 3065 3066 3067 static void test_dev_found(void *ctx, const u8 *addr, 3068 const struct p2p_peer_info *info, int new_device) 3069 { 3070 struct wpa_driver_test_data *drv = ctx; 3071 union wpa_event_data event; 3072 char devtype[WPS_DEV_TYPE_BUFSIZE]; 3073 wpa_printf(MSG_DEBUG, "%s(" MACSTR " p2p_dev_addr=" MACSTR 3074 " pri_dev_type=%s name='%s' config_methods=0x%x " 3075 "dev_capab=0x%x group_capab=0x%x)", 3076 __func__, MAC2STR(addr), MAC2STR(info->p2p_device_addr), 3077 wps_dev_type_bin2str(info->pri_dev_type, devtype, 3078 sizeof(devtype)), 3079 info->device_name, info->config_methods, info->dev_capab, 3080 info->group_capab); 3081 3082 os_memset(&event, 0, sizeof(event)); 3083 event.p2p_dev_found.addr = addr; 3084 event.p2p_dev_found.dev_addr = info->p2p_device_addr; 3085 event.p2p_dev_found.pri_dev_type = info->pri_dev_type; 3086 event.p2p_dev_found.dev_name = info->device_name; 3087 event.p2p_dev_found.config_methods = info->config_methods; 3088 event.p2p_dev_found.dev_capab = info->dev_capab; 3089 event.p2p_dev_found.group_capab = info->group_capab; 3090 wpa_supplicant_event(drv->ctx, EVENT_P2P_DEV_FOUND, &event); 3091 } 3092 3093 3094 static int test_start_listen(void *ctx, unsigned int freq, 3095 unsigned int duration, 3096 const struct wpabuf *probe_resp_ie) 3097 { 3098 struct wpa_driver_test_data *drv = ctx; 3099 3100 wpa_printf(MSG_DEBUG, "%s(freq=%u duration=%u)", 3101 __func__, freq, duration); 3102 3103 if (wpa_driver_test_probe_req_report(drv, 1) < 0) 3104 return -1; 3105 3106 drv->pending_listen_freq = freq; 3107 drv->pending_listen_duration = duration; 3108 3109 if (wpa_driver_test_remain_on_channel(drv, freq, duration) < 0) { 3110 drv->pending_listen_freq = 0; 3111 return -1; 3112 } 3113 3114 return 0; 3115 } 3116 3117 3118 static void test_stop_listen(void *ctx) 3119 { 3120 wpa_printf(MSG_DEBUG, "%s", __func__); 3121 /* TODO */ 3122 } 3123 3124 3125 static int test_send_probe_resp(void *ctx, const struct wpabuf *buf) 3126 { 3127 struct wpa_driver_test_data *drv = ctx; 3128 char resp[512], *pos, *end; 3129 int ret; 3130 const struct ieee80211_mgmt *mgmt; 3131 const u8 *ie, *ie_end; 3132 3133 wpa_printf(MSG_DEBUG, "%s", __func__); 3134 wpa_hexdump_buf(MSG_MSGDUMP, "Probe Response", buf); 3135 if (wpabuf_len(buf) < 24) 3136 return -1; 3137 if (!drv->probe_from) { 3138 wpa_printf(MSG_DEBUG, "%s: probe_from not set", __func__); 3139 return -1; 3140 } 3141 3142 pos = resp; 3143 end = resp + sizeof(resp); 3144 3145 mgmt = wpabuf_head(buf); 3146 3147 /* reply: SCANRESP BSSID SSID IEs */ 3148 ret = os_snprintf(pos, end - pos, "SCANRESP " MACSTR " ", 3149 MAC2STR(mgmt->bssid)); 3150 if (ret < 0 || ret >= end - pos) 3151 return -1; 3152 pos += ret; 3153 3154 ie = mgmt->u.probe_resp.variable; 3155 ie_end = wpabuf_head_u8(buf) + wpabuf_len(buf); 3156 if (ie_end - ie < 2 || ie[0] != WLAN_EID_SSID || 3157 ie + 2 + ie[1] > ie_end) 3158 return -1; 3159 pos += wpa_snprintf_hex(pos, end - pos, ie + 2, ie[1]); 3160 3161 ret = os_snprintf(pos, end - pos, " "); 3162 if (ret < 0 || ret >= end - pos) 3163 return -1; 3164 pos += ret; 3165 pos += wpa_snprintf_hex(pos, end - pos, ie, ie_end - ie); 3166 3167 sendto(drv->test_socket, resp, pos - resp, 0, 3168 drv->probe_from, drv->probe_from_len); 3169 3170 return 0; 3171 } 3172 3173 3174 static void test_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token, 3175 u16 update_indic, const u8 *tlvs, size_t tlvs_len) 3176 { 3177 wpa_printf(MSG_DEBUG, "%s", __func__); 3178 /* TODO */ 3179 } 3180 3181 3182 static void test_sd_response(void *ctx, const u8 *sa, u16 update_indic, 3183 const u8 *tlvs, size_t tlvs_len) 3184 { 3185 wpa_printf(MSG_DEBUG, "%s", __func__); 3186 /* TODO */ 3187 } 3188 3189 3190 static void test_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, 3191 const u8 *dev_addr, const u8 *pri_dev_type, 3192 const char *dev_name, u16 supp_config_methods, 3193 u8 dev_capab, u8 group_capab, 3194 const u8 *group_id, size_t group_id_len) 3195 { 3196 wpa_printf(MSG_DEBUG, "%s(peer=" MACSTR " config_methods=0x%x)", 3197 __func__, MAC2STR(peer), config_methods); 3198 /* TODO */ 3199 } 3200 3201 3202 static void test_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods) 3203 { 3204 wpa_printf(MSG_DEBUG, "%s(peer=" MACSTR " config_methods=0x%x)", 3205 __func__, MAC2STR(peer), config_methods); 3206 /* TODO */ 3207 } 3208 3209 #endif /* CONFIG_P2P */ 3210 3211 3212 static int wpa_driver_test_init_p2p(struct wpa_driver_test_data *drv) 3213 { 3214 #ifdef CONFIG_P2P 3215 struct p2p_config p2p; 3216 unsigned int r; 3217 int i; 3218 3219 os_memset(&p2p, 0, sizeof(p2p)); 3220 p2p.msg_ctx = drv->ctx; 3221 p2p.cb_ctx = drv; 3222 p2p.p2p_scan = test_p2p_scan; 3223 p2p.send_action = test_send_action; 3224 p2p.send_action_done = test_send_action_done; 3225 p2p.go_neg_completed = test_go_neg_completed; 3226 p2p.go_neg_req_rx = test_go_neg_req_rx; 3227 p2p.dev_found = test_dev_found; 3228 p2p.start_listen = test_start_listen; 3229 p2p.stop_listen = test_stop_listen; 3230 p2p.send_probe_resp = test_send_probe_resp; 3231 p2p.sd_request = test_sd_request; 3232 p2p.sd_response = test_sd_response; 3233 p2p.prov_disc_req = test_prov_disc_req; 3234 p2p.prov_disc_resp = test_prov_disc_resp; 3235 3236 os_memcpy(p2p.dev_addr, drv->own_addr, ETH_ALEN); 3237 3238 p2p.reg_class = 12; /* TODO: change depending on location */ 3239 /* 3240 * Pick one of the social channels randomly as the listen 3241 * channel. 3242 */ 3243 os_get_random((u8 *) &r, sizeof(r)); 3244 p2p.channel = 1 + (r % 3) * 5; 3245 3246 /* TODO: change depending on location */ 3247 p2p.op_reg_class = 12; 3248 /* 3249 * For initial tests, pick the operation channel randomly. 3250 * TODO: Use scan results (etc.) to select the best channel. 3251 */ 3252 p2p.op_channel = 1 + r % 11; 3253 3254 os_memcpy(p2p.country, "US ", 3); 3255 3256 /* FIX: fetch available channels from the driver */ 3257 p2p.channels.reg_classes = 1; 3258 p2p.channels.reg_class[0].reg_class = 12; /* US/12 = 2.4 GHz band */ 3259 p2p.channels.reg_class[0].channels = 11; 3260 for (i = 0; i < 11; i++) 3261 p2p.channels.reg_class[0].channel[i] = i + 1; 3262 3263 p2p.max_peers = 100; 3264 3265 drv->p2p = p2p_init(&p2p); 3266 if (drv->p2p == NULL) 3267 return -1; 3268 return 0; 3269 #else /* CONFIG_P2P */ 3270 wpa_printf(MSG_INFO, "driver_test: P2P support not included"); 3271 return -1; 3272 #endif /* CONFIG_P2P */ 3273 } 3274 3275 3276 const struct wpa_driver_ops wpa_driver_test_ops = { 3277 "test", 3278 "wpa_supplicant test driver", 3279 .hapd_init = test_driver_init, 3280 .hapd_deinit = wpa_driver_test_deinit, 3281 .hapd_send_eapol = test_driver_send_eapol, 3282 .send_mlme = wpa_driver_test_send_mlme, 3283 .set_generic_elem = test_driver_set_generic_elem, 3284 .sta_deauth = test_driver_sta_deauth, 3285 .sta_disassoc = test_driver_sta_disassoc, 3286 .get_hw_feature_data = wpa_driver_test_get_hw_feature_data, 3287 .if_add = test_driver_if_add, 3288 .if_remove = test_driver_if_remove, 3289 .hapd_set_ssid = test_driver_set_ssid, 3290 .set_privacy = test_driver_set_privacy, 3291 .set_sta_vlan = test_driver_set_sta_vlan, 3292 .sta_add = test_driver_sta_add, 3293 .send_ether = test_driver_send_ether, 3294 .set_ap_wps_ie = test_driver_set_ap_wps_ie, 3295 .get_bssid = wpa_driver_test_get_bssid, 3296 .get_ssid = wpa_driver_test_get_ssid, 3297 .set_key = wpa_driver_test_set_key, 3298 .deinit = wpa_driver_test_deinit, 3299 .set_param = wpa_driver_test_set_param, 3300 .deauthenticate = wpa_driver_test_deauthenticate, 3301 .disassociate = wpa_driver_test_disassociate, 3302 .associate = wpa_driver_test_associate, 3303 .get_capa = wpa_driver_test_get_capa, 3304 .get_mac_addr = wpa_driver_test_get_mac_addr, 3305 .send_eapol = wpa_driver_test_send_eapol, 3306 .mlme_setprotection = wpa_driver_test_mlme_setprotection, 3307 .get_scan_results2 = wpa_driver_test_get_scan_results2, 3308 .global_init = wpa_driver_test_global_init, 3309 .global_deinit = wpa_driver_test_global_deinit, 3310 .init2 = wpa_driver_test_init2, 3311 .get_interfaces = wpa_driver_test_get_interfaces, 3312 .scan2 = wpa_driver_test_scan, 3313 .set_freq = wpa_driver_test_set_freq, 3314 .send_action = wpa_driver_test_send_action, 3315 .remain_on_channel = wpa_driver_test_remain_on_channel, 3316 .cancel_remain_on_channel = wpa_driver_test_cancel_remain_on_channel, 3317 .probe_req_report = wpa_driver_test_probe_req_report, 3318 #ifdef CONFIG_P2P 3319 .p2p_find = wpa_driver_test_p2p_find, 3320 .p2p_stop_find = wpa_driver_test_p2p_stop_find, 3321 .p2p_listen = wpa_driver_test_p2p_listen, 3322 .p2p_connect = wpa_driver_test_p2p_connect, 3323 .wps_success_cb = wpa_driver_test_wps_success_cb, 3324 .p2p_group_formation_failed = 3325 wpa_driver_test_p2p_group_formation_failed, 3326 .p2p_set_params = wpa_driver_test_p2p_set_params, 3327 #endif /* CONFIG_P2P */ 3328 }; 3329