1 /* 2 * Driver interaction with Linux nl80211/cfg80211 3 * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi> 4 * Copyright (c) 2003-2004, Instant802 Networks, Inc. 5 * Copyright (c) 2005-2006, Devicescape Software, Inc. 6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net> 7 * Copyright (c) 2009-2010, Atheros Communications 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12 * 13 * Alternatively, this software may be distributed under the terms of BSD 14 * license. 15 * 16 * See README and COPYING for more details. 17 */ 18 19 #include "includes.h" 20 #include <sys/ioctl.h> 21 #include <sys/types.h> 22 #include <sys/stat.h> 23 #include <fcntl.h> 24 #include <net/if.h> 25 #include <netlink/genl/genl.h> 26 #include <netlink/genl/family.h> 27 #include <netlink/genl/ctrl.h> 28 #include <linux/rtnetlink.h> 29 #include <netpacket/packet.h> 30 #include <linux/filter.h> 31 #include "nl80211_copy.h" 32 33 #include "common.h" 34 #include "eloop.h" 35 #include "utils/list.h" 36 #include "common/ieee802_11_defs.h" 37 #include "common/ieee802_11_common.h" 38 #include "l2_packet/l2_packet.h" 39 #include "netlink.h" 40 #include "linux_ioctl.h" 41 #include "radiotap.h" 42 #include "radiotap_iter.h" 43 #include "rfkill.h" 44 #include "driver.h" 45 46 #ifdef CONFIG_LIBNL20 47 /* libnl 2.0 compatibility code */ 48 #define nl_handle nl_sock 49 #define nl80211_handle_alloc nl_socket_alloc_cb 50 #define nl80211_handle_destroy nl_socket_free 51 #else 52 /* 53 * libnl 1.1 has a bug, it tries to allocate socket numbers densely 54 * but when you free a socket again it will mess up its bitmap and 55 * and use the wrong number the next time it needs a socket ID. 56 * Therefore, we wrap the handle alloc/destroy and add our own pid 57 * accounting. 58 */ 59 static uint32_t port_bitmap[32] = { 0 }; 60 61 static struct nl_handle *nl80211_handle_alloc(void *cb) 62 { 63 struct nl_handle *handle; 64 uint32_t pid = getpid() & 0x3FFFFF; 65 int i; 66 67 handle = nl_handle_alloc_cb(cb); 68 69 for (i = 0; i < 1024; i++) { 70 if (port_bitmap[i / 32] & (1 << (i % 32))) 71 continue; 72 port_bitmap[i / 32] |= 1 << (i % 32); 73 pid += i << 22; 74 break; 75 } 76 77 nl_socket_set_local_port(handle, pid); 78 79 return handle; 80 } 81 82 static void nl80211_handle_destroy(struct nl_handle *handle) 83 { 84 uint32_t port = nl_socket_get_local_port(handle); 85 86 port >>= 22; 87 port_bitmap[port / 32] &= ~(1 << (port % 32)); 88 89 nl_handle_destroy(handle); 90 } 91 92 static inline int __genl_ctrl_alloc_cache(struct nl_handle *h, 93 struct nl_cache **cache) 94 { 95 struct nl_cache *tmp = genl_ctrl_alloc_cache(h); 96 if (!tmp) 97 return -ENOMEM; 98 *cache = tmp; 99 return 0; 100 } 101 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache 102 #endif /* CONFIG_LIBNL20 */ 103 104 105 struct nl80211_handles { 106 struct nl_handle *handle; 107 struct nl_cache *cache; 108 }; 109 110 111 static int nl_create_handles(struct nl80211_handles *handles, struct nl_cb *cb, 112 const char *dbg) 113 { 114 if (!handles) 115 return -1; 116 117 handles->handle = nl80211_handle_alloc(cb); 118 if (handles->handle == NULL) { 119 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " 120 "callbacks (%s)", dbg); 121 return -1; 122 } 123 124 if (genl_connect(handles->handle)) { 125 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic " 126 "netlink (%s)", dbg); 127 goto err; 128 } 129 130 if (genl_ctrl_alloc_cache(handles->handle, &handles->cache) < 0) { 131 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic " 132 "netlink cache (%s)", dbg); 133 goto err; 134 } 135 136 return 0; 137 err: 138 nl80211_handle_destroy(handles->handle); 139 return -1; 140 } 141 142 143 static void nl_destroy_handles(struct nl80211_handles *handles) 144 { 145 if (handles->handle == NULL) 146 return; 147 nl_cache_free(handles->cache); 148 nl80211_handle_destroy(handles->handle); 149 handles->handle = NULL; 150 } 151 152 153 #ifndef IFF_LOWER_UP 154 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */ 155 #endif 156 #ifndef IFF_DORMANT 157 #define IFF_DORMANT 0x20000 /* driver signals dormant */ 158 #endif 159 160 #ifndef IF_OPER_DORMANT 161 #define IF_OPER_DORMANT 5 162 #endif 163 #ifndef IF_OPER_UP 164 #define IF_OPER_UP 6 165 #endif 166 167 struct nl80211_global { 168 struct dl_list interfaces; 169 int if_add_ifindex; 170 struct netlink_data *netlink; 171 struct nl_cb *nl_cb; 172 struct nl80211_handles nl; 173 struct genl_family *nl80211; 174 int ioctl_sock; /* socket for ioctl() use */ 175 }; 176 177 static void nl80211_global_deinit(void *priv); 178 static void wpa_driver_nl80211_deinit(void *priv); 179 180 struct i802_bss { 181 struct wpa_driver_nl80211_data *drv; 182 struct i802_bss *next; 183 int ifindex; 184 char ifname[IFNAMSIZ + 1]; 185 char brname[IFNAMSIZ]; 186 unsigned int beacon_set:1; 187 unsigned int added_if_into_bridge:1; 188 unsigned int added_bridge:1; 189 }; 190 191 struct wpa_driver_nl80211_data { 192 struct nl80211_global *global; 193 struct dl_list list; 194 u8 addr[ETH_ALEN]; 195 char phyname[32]; 196 void *ctx; 197 int ifindex; 198 int if_removed; 199 int if_disabled; 200 int ignore_if_down_event; 201 struct rfkill_data *rfkill; 202 struct wpa_driver_capa capa; 203 int has_capability; 204 205 int operstate; 206 207 int scan_complete_events; 208 209 struct nl80211_handles nl_event, nl_preq; 210 211 u8 auth_bssid[ETH_ALEN]; 212 u8 bssid[ETH_ALEN]; 213 int associated; 214 u8 ssid[32]; 215 size_t ssid_len; 216 enum nl80211_iftype nlmode; 217 enum nl80211_iftype ap_scan_as_station; 218 unsigned int assoc_freq; 219 220 int monitor_sock; 221 int monitor_ifidx; 222 int no_monitor_iface_capab; 223 224 unsigned int disabled_11b_rates:1; 225 unsigned int pending_remain_on_chan:1; 226 unsigned int in_interface_list:1; 227 228 u64 remain_on_chan_cookie; 229 u64 send_action_cookie; 230 231 unsigned int last_mgmt_freq; 232 unsigned int ap_oper_freq; 233 234 struct wpa_driver_scan_filter *filter_ssids; 235 size_t num_filter_ssids; 236 237 struct i802_bss first_bss; 238 239 #ifdef CONFIG_AP 240 struct l2_packet_data *l2; 241 #endif /* CONFIG_AP */ 242 243 #ifdef HOSTAPD 244 int eapol_sock; /* socket for EAPOL frames */ 245 246 int default_if_indices[16]; 247 int *if_indices; 248 int num_if_indices; 249 250 int last_freq; 251 int last_freq_ht; 252 #endif /* HOSTAPD */ 253 }; 254 255 256 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, 257 void *timeout_ctx); 258 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss, 259 enum nl80211_iftype nlmode); 260 static int 261 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv); 262 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv, 263 const u8 *addr, int cmd, u16 reason_code, 264 int local_state_change); 265 static void nl80211_remove_monitor_interface( 266 struct wpa_driver_nl80211_data *drv); 267 static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv, 268 unsigned int freq, unsigned int wait, 269 const u8 *buf, size_t buf_len, u64 *cookie, 270 int no_cck); 271 static int wpa_driver_nl80211_probe_req_report(void *priv, int report); 272 273 #ifdef HOSTAPD 274 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); 275 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); 276 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); 277 static int wpa_driver_nl80211_if_remove(void *priv, 278 enum wpa_driver_if_type type, 279 const char *ifname); 280 #else /* HOSTAPD */ 281 static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 282 { 283 } 284 285 static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 286 { 287 } 288 289 static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 290 { 291 return 0; 292 } 293 #endif /* HOSTAPD */ 294 295 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq); 296 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv, 297 int ifindex, int disabled); 298 299 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv); 300 301 302 static int is_ap_interface(enum nl80211_iftype nlmode) 303 { 304 return (nlmode == NL80211_IFTYPE_AP || 305 nlmode == NL80211_IFTYPE_P2P_GO); 306 } 307 308 309 static int is_sta_interface(enum nl80211_iftype nlmode) 310 { 311 return (nlmode == NL80211_IFTYPE_STATION || 312 nlmode == NL80211_IFTYPE_P2P_CLIENT); 313 } 314 315 316 static int is_p2p_interface(enum nl80211_iftype nlmode) 317 { 318 return (nlmode == NL80211_IFTYPE_P2P_CLIENT || 319 nlmode == NL80211_IFTYPE_P2P_GO); 320 } 321 322 323 struct nl80211_bss_info_arg { 324 struct wpa_driver_nl80211_data *drv; 325 struct wpa_scan_results *res; 326 unsigned int assoc_freq; 327 u8 assoc_bssid[ETH_ALEN]; 328 }; 329 330 static int bss_info_handler(struct nl_msg *msg, void *arg); 331 332 333 /* nl80211 code */ 334 static int ack_handler(struct nl_msg *msg, void *arg) 335 { 336 int *err = arg; 337 *err = 0; 338 return NL_STOP; 339 } 340 341 static int finish_handler(struct nl_msg *msg, void *arg) 342 { 343 int *ret = arg; 344 *ret = 0; 345 return NL_SKIP; 346 } 347 348 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, 349 void *arg) 350 { 351 int *ret = arg; 352 *ret = err->error; 353 return NL_SKIP; 354 } 355 356 357 static int no_seq_check(struct nl_msg *msg, void *arg) 358 { 359 return NL_OK; 360 } 361 362 363 static int send_and_recv(struct wpa_driver_nl80211_data *drv, 364 struct nl_handle *nl_handle, struct nl_msg *msg, 365 int (*valid_handler)(struct nl_msg *, void *), 366 void *valid_data) 367 { 368 struct nl_cb *cb; 369 int err = -ENOMEM; 370 371 cb = nl_cb_clone(drv->global->nl_cb); 372 if (!cb) 373 goto out; 374 375 err = nl_send_auto_complete(nl_handle, msg); 376 if (err < 0) 377 goto out; 378 379 err = 1; 380 381 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); 382 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); 383 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); 384 385 if (valid_handler) 386 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, 387 valid_handler, valid_data); 388 389 while (err > 0) 390 nl_recvmsgs(nl_handle, cb); 391 out: 392 nl_cb_put(cb); 393 nlmsg_free(msg); 394 return err; 395 } 396 397 398 static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv, 399 struct nl_msg *msg, 400 int (*valid_handler)(struct nl_msg *, void *), 401 void *valid_data) 402 { 403 return send_and_recv(drv, drv->global->nl.handle, msg, valid_handler, 404 valid_data); 405 } 406 407 408 struct family_data { 409 const char *group; 410 int id; 411 }; 412 413 414 static int family_handler(struct nl_msg *msg, void *arg) 415 { 416 struct family_data *res = arg; 417 struct nlattr *tb[CTRL_ATTR_MAX + 1]; 418 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 419 struct nlattr *mcgrp; 420 int i; 421 422 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 423 genlmsg_attrlen(gnlh, 0), NULL); 424 if (!tb[CTRL_ATTR_MCAST_GROUPS]) 425 return NL_SKIP; 426 427 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) { 428 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1]; 429 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp), 430 nla_len(mcgrp), NULL); 431 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || 432 !tb2[CTRL_ATTR_MCAST_GRP_ID] || 433 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]), 434 res->group, 435 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0) 436 continue; 437 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]); 438 break; 439 }; 440 441 return NL_SKIP; 442 } 443 444 445 static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv, 446 const char *family, const char *group) 447 { 448 struct nl_msg *msg; 449 int ret = -1; 450 struct family_data res = { group, -ENOENT }; 451 452 msg = nlmsg_alloc(); 453 if (!msg) 454 return -ENOMEM; 455 genlmsg_put(msg, 0, 0, 456 genl_ctrl_resolve(drv->global->nl.handle, "nlctrl"), 457 0, 0, CTRL_CMD_GETFAMILY, 0); 458 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family); 459 460 ret = send_and_recv_msgs(drv, msg, family_handler, &res); 461 msg = NULL; 462 if (ret == 0) 463 ret = res.id; 464 465 nla_put_failure: 466 nlmsg_free(msg); 467 return ret; 468 } 469 470 471 static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv, 472 struct nl_msg *msg, int flags, uint8_t cmd) 473 { 474 return genlmsg_put(msg, 0, 0, genl_family_get_id(drv->global->nl80211), 475 0, flags, cmd, 0); 476 } 477 478 479 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid) 480 { 481 struct i802_bss *bss = priv; 482 struct wpa_driver_nl80211_data *drv = bss->drv; 483 if (!drv->associated) 484 return -1; 485 os_memcpy(bssid, drv->bssid, ETH_ALEN); 486 return 0; 487 } 488 489 490 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid) 491 { 492 struct i802_bss *bss = priv; 493 struct wpa_driver_nl80211_data *drv = bss->drv; 494 if (!drv->associated) 495 return -1; 496 os_memcpy(ssid, drv->ssid, drv->ssid_len); 497 return drv->ssid_len; 498 } 499 500 501 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv, 502 char *buf, size_t len, int del) 503 { 504 union wpa_event_data event; 505 506 os_memset(&event, 0, sizeof(event)); 507 if (len > sizeof(event.interface_status.ifname)) 508 len = sizeof(event.interface_status.ifname) - 1; 509 os_memcpy(event.interface_status.ifname, buf, len); 510 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED : 511 EVENT_INTERFACE_ADDED; 512 513 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s", 514 del ? "DEL" : "NEW", 515 event.interface_status.ifname, 516 del ? "removed" : "added"); 517 518 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) { 519 if (del) { 520 if (drv->if_removed) { 521 wpa_printf(MSG_DEBUG, "nl80211: if_removed " 522 "already set - ignore event"); 523 return; 524 } 525 drv->if_removed = 1; 526 } else { 527 if (if_nametoindex(drv->first_bss.ifname) == 0) { 528 wpa_printf(MSG_DEBUG, "nl80211: Interface %s " 529 "does not exist - ignore " 530 "RTM_NEWLINK", 531 drv->first_bss.ifname); 532 return; 533 } 534 if (!drv->if_removed) { 535 wpa_printf(MSG_DEBUG, "nl80211: if_removed " 536 "already cleared - ignore event"); 537 return; 538 } 539 drv->if_removed = 0; 540 } 541 } 542 543 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event); 544 } 545 546 547 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv, 548 u8 *buf, size_t len) 549 { 550 int attrlen, rta_len; 551 struct rtattr *attr; 552 553 attrlen = len; 554 attr = (struct rtattr *) buf; 555 556 rta_len = RTA_ALIGN(sizeof(struct rtattr)); 557 while (RTA_OK(attr, attrlen)) { 558 if (attr->rta_type == IFLA_IFNAME) { 559 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname) 560 == 0) 561 return 1; 562 else 563 break; 564 } 565 attr = RTA_NEXT(attr, attrlen); 566 } 567 568 return 0; 569 } 570 571 572 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv, 573 int ifindex, u8 *buf, size_t len) 574 { 575 if (drv->ifindex == ifindex) 576 return 1; 577 578 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) { 579 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname); 580 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed " 581 "interface"); 582 wpa_driver_nl80211_finish_drv_init(drv); 583 return 1; 584 } 585 586 return 0; 587 } 588 589 590 static struct wpa_driver_nl80211_data * 591 nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len) 592 { 593 struct wpa_driver_nl80211_data *drv; 594 dl_list_for_each(drv, &global->interfaces, 595 struct wpa_driver_nl80211_data, list) { 596 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) || 597 have_ifidx(drv, idx)) 598 return drv; 599 } 600 return NULL; 601 } 602 603 604 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx, 605 struct ifinfomsg *ifi, 606 u8 *buf, size_t len) 607 { 608 struct nl80211_global *global = ctx; 609 struct wpa_driver_nl80211_data *drv; 610 int attrlen, rta_len; 611 struct rtattr *attr; 612 u32 brid = 0; 613 char namebuf[IFNAMSIZ]; 614 615 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len); 616 if (!drv) { 617 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign " 618 "ifindex %d", ifi->ifi_index); 619 return; 620 } 621 622 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x " 623 "(%s%s%s%s)", 624 drv->operstate, ifi->ifi_flags, 625 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "", 626 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "", 627 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "", 628 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : ""); 629 630 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) { 631 if (if_indextoname(ifi->ifi_index, namebuf) && 632 linux_iface_up(drv->global->ioctl_sock, 633 drv->first_bss.ifname) > 0) { 634 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down " 635 "event since interface %s is up", namebuf); 636 return; 637 } 638 wpa_printf(MSG_DEBUG, "nl80211: Interface down"); 639 if (drv->ignore_if_down_event) { 640 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down " 641 "event generated by mode change"); 642 drv->ignore_if_down_event = 0; 643 } else { 644 drv->if_disabled = 1; 645 wpa_supplicant_event(drv->ctx, 646 EVENT_INTERFACE_DISABLED, NULL); 647 } 648 } 649 650 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) { 651 if (if_indextoname(ifi->ifi_index, namebuf) && 652 linux_iface_up(drv->global->ioctl_sock, 653 drv->first_bss.ifname) == 0) { 654 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " 655 "event since interface %s is down", 656 namebuf); 657 } else if (if_nametoindex(drv->first_bss.ifname) == 0) { 658 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " 659 "event since interface %s does not exist", 660 drv->first_bss.ifname); 661 } else if (drv->if_removed) { 662 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " 663 "event since interface %s is marked " 664 "removed", drv->first_bss.ifname); 665 } else { 666 wpa_printf(MSG_DEBUG, "nl80211: Interface up"); 667 drv->if_disabled = 0; 668 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, 669 NULL); 670 } 671 } 672 673 /* 674 * Some drivers send the association event before the operup event--in 675 * this case, lifting operstate in wpa_driver_nl80211_set_operstate() 676 * fails. This will hit us when wpa_supplicant does not need to do 677 * IEEE 802.1X authentication 678 */ 679 if (drv->operstate == 1 && 680 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP && 681 !(ifi->ifi_flags & IFF_RUNNING)) 682 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 683 -1, IF_OPER_UP); 684 685 attrlen = len; 686 attr = (struct rtattr *) buf; 687 rta_len = RTA_ALIGN(sizeof(struct rtattr)); 688 while (RTA_OK(attr, attrlen)) { 689 if (attr->rta_type == IFLA_IFNAME) { 690 wpa_driver_nl80211_event_link( 691 drv, 692 ((char *) attr) + rta_len, 693 attr->rta_len - rta_len, 0); 694 } else if (attr->rta_type == IFLA_MASTER) 695 brid = nla_get_u32((struct nlattr *) attr); 696 attr = RTA_NEXT(attr, attrlen); 697 } 698 699 if (ifi->ifi_family == AF_BRIDGE && brid) { 700 /* device has been added to bridge */ 701 if_indextoname(brid, namebuf); 702 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s", 703 brid, namebuf); 704 add_ifidx(drv, brid); 705 } 706 } 707 708 709 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx, 710 struct ifinfomsg *ifi, 711 u8 *buf, size_t len) 712 { 713 struct nl80211_global *global = ctx; 714 struct wpa_driver_nl80211_data *drv; 715 int attrlen, rta_len; 716 struct rtattr *attr; 717 u32 brid = 0; 718 719 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len); 720 if (!drv) { 721 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for " 722 "foreign ifindex %d", ifi->ifi_index); 723 return; 724 } 725 726 attrlen = len; 727 attr = (struct rtattr *) buf; 728 729 rta_len = RTA_ALIGN(sizeof(struct rtattr)); 730 while (RTA_OK(attr, attrlen)) { 731 if (attr->rta_type == IFLA_IFNAME) { 732 wpa_driver_nl80211_event_link( 733 drv, 734 ((char *) attr) + rta_len, 735 attr->rta_len - rta_len, 1); 736 } else if (attr->rta_type == IFLA_MASTER) 737 brid = nla_get_u32((struct nlattr *) attr); 738 attr = RTA_NEXT(attr, attrlen); 739 } 740 741 if (ifi->ifi_family == AF_BRIDGE && brid) { 742 /* device has been removed from bridge */ 743 char namebuf[IFNAMSIZ]; 744 if_indextoname(brid, namebuf); 745 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge " 746 "%s", brid, namebuf); 747 del_ifidx(drv, brid); 748 } 749 } 750 751 752 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv, 753 const u8 *frame, size_t len) 754 { 755 const struct ieee80211_mgmt *mgmt; 756 union wpa_event_data event; 757 758 mgmt = (const struct ieee80211_mgmt *) frame; 759 if (len < 24 + sizeof(mgmt->u.auth)) { 760 wpa_printf(MSG_DEBUG, "nl80211: Too short association event " 761 "frame"); 762 return; 763 } 764 765 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN); 766 os_memset(&event, 0, sizeof(event)); 767 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN); 768 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg); 769 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code); 770 if (len > 24 + sizeof(mgmt->u.auth)) { 771 event.auth.ies = mgmt->u.auth.variable; 772 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth); 773 } 774 775 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event); 776 } 777 778 779 static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv) 780 { 781 struct nl_msg *msg; 782 int ret; 783 struct nl80211_bss_info_arg arg; 784 785 os_memset(&arg, 0, sizeof(arg)); 786 msg = nlmsg_alloc(); 787 if (!msg) 788 goto nla_put_failure; 789 790 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN); 791 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 792 793 arg.drv = drv; 794 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg); 795 msg = NULL; 796 if (ret == 0) { 797 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the " 798 "associated BSS from scan results: %u MHz", 799 arg.assoc_freq); 800 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq; 801 } 802 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d " 803 "(%s)", ret, strerror(-ret)); 804 nla_put_failure: 805 nlmsg_free(msg); 806 return drv->assoc_freq; 807 } 808 809 810 static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv, 811 const u8 *frame, size_t len) 812 { 813 const struct ieee80211_mgmt *mgmt; 814 union wpa_event_data event; 815 u16 status; 816 817 mgmt = (const struct ieee80211_mgmt *) frame; 818 if (len < 24 + sizeof(mgmt->u.assoc_resp)) { 819 wpa_printf(MSG_DEBUG, "nl80211: Too short association event " 820 "frame"); 821 return; 822 } 823 824 status = le_to_host16(mgmt->u.assoc_resp.status_code); 825 if (status != WLAN_STATUS_SUCCESS) { 826 os_memset(&event, 0, sizeof(event)); 827 event.assoc_reject.bssid = mgmt->bssid; 828 if (len > 24 + sizeof(mgmt->u.assoc_resp)) { 829 event.assoc_reject.resp_ies = 830 (u8 *) mgmt->u.assoc_resp.variable; 831 event.assoc_reject.resp_ies_len = 832 len - 24 - sizeof(mgmt->u.assoc_resp); 833 } 834 event.assoc_reject.status_code = status; 835 836 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event); 837 return; 838 } 839 840 drv->associated = 1; 841 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN); 842 843 os_memset(&event, 0, sizeof(event)); 844 if (len > 24 + sizeof(mgmt->u.assoc_resp)) { 845 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable; 846 event.assoc_info.resp_ies_len = 847 len - 24 - sizeof(mgmt->u.assoc_resp); 848 } 849 850 event.assoc_info.freq = drv->assoc_freq; 851 852 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event); 853 } 854 855 856 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv, 857 enum nl80211_commands cmd, struct nlattr *status, 858 struct nlattr *addr, struct nlattr *req_ie, 859 struct nlattr *resp_ie) 860 { 861 union wpa_event_data event; 862 863 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { 864 /* 865 * Avoid reporting two association events that would confuse 866 * the core code. 867 */ 868 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) " 869 "when using userspace SME", cmd); 870 return; 871 } 872 873 os_memset(&event, 0, sizeof(event)); 874 if (cmd == NL80211_CMD_CONNECT && 875 nla_get_u16(status) != WLAN_STATUS_SUCCESS) { 876 if (addr) 877 event.assoc_reject.bssid = nla_data(addr); 878 if (resp_ie) { 879 event.assoc_reject.resp_ies = nla_data(resp_ie); 880 event.assoc_reject.resp_ies_len = nla_len(resp_ie); 881 } 882 event.assoc_reject.status_code = nla_get_u16(status); 883 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event); 884 return; 885 } 886 887 drv->associated = 1; 888 if (addr) 889 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN); 890 891 if (req_ie) { 892 event.assoc_info.req_ies = nla_data(req_ie); 893 event.assoc_info.req_ies_len = nla_len(req_ie); 894 } 895 if (resp_ie) { 896 event.assoc_info.resp_ies = nla_data(resp_ie); 897 event.assoc_info.resp_ies_len = nla_len(resp_ie); 898 } 899 900 event.assoc_info.freq = nl80211_get_assoc_freq(drv); 901 902 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event); 903 } 904 905 906 static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv, 907 struct nlattr *reason, struct nlattr *addr) 908 { 909 union wpa_event_data data; 910 911 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { 912 /* 913 * Avoid reporting two disassociation events that could 914 * confuse the core code. 915 */ 916 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect " 917 "event when using userspace SME"); 918 return; 919 } 920 921 drv->associated = 0; 922 os_memset(&data, 0, sizeof(data)); 923 if (reason) 924 data.disassoc_info.reason_code = nla_get_u16(reason); 925 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, &data); 926 } 927 928 929 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv, 930 enum nl80211_commands cmd, struct nlattr *addr) 931 { 932 union wpa_event_data event; 933 enum wpa_event_type ev; 934 935 if (nla_len(addr) != ETH_ALEN) 936 return; 937 938 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR, 939 cmd, MAC2STR((u8 *) nla_data(addr))); 940 941 if (cmd == NL80211_CMD_AUTHENTICATE) 942 ev = EVENT_AUTH_TIMED_OUT; 943 else if (cmd == NL80211_CMD_ASSOCIATE) 944 ev = EVENT_ASSOC_TIMED_OUT; 945 else 946 return; 947 948 os_memset(&event, 0, sizeof(event)); 949 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN); 950 wpa_supplicant_event(drv->ctx, ev, &event); 951 } 952 953 954 static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv, 955 struct nlattr *freq, const u8 *frame, size_t len) 956 { 957 const struct ieee80211_mgmt *mgmt; 958 union wpa_event_data event; 959 u16 fc, stype; 960 961 mgmt = (const struct ieee80211_mgmt *) frame; 962 if (len < 24) { 963 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame"); 964 return; 965 } 966 967 fc = le_to_host16(mgmt->frame_control); 968 stype = WLAN_FC_GET_STYPE(fc); 969 970 os_memset(&event, 0, sizeof(event)); 971 if (freq) { 972 event.rx_action.freq = nla_get_u32(freq); 973 drv->last_mgmt_freq = event.rx_action.freq; 974 } 975 if (stype == WLAN_FC_STYPE_ACTION) { 976 event.rx_action.da = mgmt->da; 977 event.rx_action.sa = mgmt->sa; 978 event.rx_action.bssid = mgmt->bssid; 979 event.rx_action.category = mgmt->u.action.category; 980 event.rx_action.data = &mgmt->u.action.category + 1; 981 event.rx_action.len = frame + len - event.rx_action.data; 982 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event); 983 } else { 984 event.rx_mgmt.frame = frame; 985 event.rx_mgmt.frame_len = len; 986 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event); 987 } 988 } 989 990 991 static void mlme_event_action_tx_status(struct wpa_driver_nl80211_data *drv, 992 struct nlattr *cookie, const u8 *frame, 993 size_t len, struct nlattr *ack) 994 { 995 union wpa_event_data event; 996 const struct ieee80211_hdr *hdr; 997 u16 fc; 998 u64 cookie_val; 999 1000 if (!cookie) 1001 return; 1002 1003 cookie_val = nla_get_u64(cookie); 1004 wpa_printf(MSG_DEBUG, "nl80211: Action TX status: cookie=0%llx%s " 1005 "(ack=%d)", 1006 (long long unsigned int) cookie_val, 1007 cookie_val == drv->send_action_cookie ? 1008 " (match)" : " (unknown)", ack != NULL); 1009 if (cookie_val != drv->send_action_cookie) 1010 return; 1011 1012 hdr = (const struct ieee80211_hdr *) frame; 1013 fc = le_to_host16(hdr->frame_control); 1014 1015 os_memset(&event, 0, sizeof(event)); 1016 event.tx_status.type = WLAN_FC_GET_TYPE(fc); 1017 event.tx_status.stype = WLAN_FC_GET_STYPE(fc); 1018 event.tx_status.dst = hdr->addr1; 1019 event.tx_status.data = frame; 1020 event.tx_status.data_len = len; 1021 event.tx_status.ack = ack != NULL; 1022 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event); 1023 } 1024 1025 1026 static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv, 1027 enum wpa_event_type type, 1028 const u8 *frame, size_t len) 1029 { 1030 const struct ieee80211_mgmt *mgmt; 1031 union wpa_event_data event; 1032 const u8 *bssid = NULL; 1033 u16 reason_code = 0; 1034 1035 mgmt = (const struct ieee80211_mgmt *) frame; 1036 if (len >= 24) { 1037 bssid = mgmt->bssid; 1038 1039 if (drv->associated != 0 && 1040 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 && 1041 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) { 1042 /* 1043 * We have presumably received this deauth as a 1044 * response to a clear_state_mismatch() outgoing 1045 * deauth. Don't let it take us offline! 1046 */ 1047 wpa_printf(MSG_DEBUG, "nl80211: Deauth received " 1048 "from Unknown BSSID " MACSTR " -- ignoring", 1049 MAC2STR(bssid)); 1050 return; 1051 } 1052 } 1053 1054 drv->associated = 0; 1055 os_memset(&event, 0, sizeof(event)); 1056 1057 /* Note: Same offset for Reason Code in both frame subtypes */ 1058 if (len >= 24 + sizeof(mgmt->u.deauth)) 1059 reason_code = le_to_host16(mgmt->u.deauth.reason_code); 1060 1061 if (type == EVENT_DISASSOC) { 1062 event.disassoc_info.addr = bssid; 1063 event.disassoc_info.reason_code = reason_code; 1064 if (frame + len > mgmt->u.disassoc.variable) { 1065 event.disassoc_info.ie = mgmt->u.disassoc.variable; 1066 event.disassoc_info.ie_len = frame + len - 1067 mgmt->u.disassoc.variable; 1068 } 1069 } else { 1070 event.deauth_info.addr = bssid; 1071 event.deauth_info.reason_code = reason_code; 1072 if (frame + len > mgmt->u.deauth.variable) { 1073 event.deauth_info.ie = mgmt->u.deauth.variable; 1074 event.deauth_info.ie_len = frame + len - 1075 mgmt->u.deauth.variable; 1076 } 1077 } 1078 1079 wpa_supplicant_event(drv->ctx, type, &event); 1080 } 1081 1082 1083 static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv, 1084 enum wpa_event_type type, 1085 const u8 *frame, size_t len) 1086 { 1087 const struct ieee80211_mgmt *mgmt; 1088 union wpa_event_data event; 1089 u16 reason_code = 0; 1090 1091 if (len < 24) 1092 return; 1093 1094 mgmt = (const struct ieee80211_mgmt *) frame; 1095 1096 os_memset(&event, 0, sizeof(event)); 1097 /* Note: Same offset for Reason Code in both frame subtypes */ 1098 if (len >= 24 + sizeof(mgmt->u.deauth)) 1099 reason_code = le_to_host16(mgmt->u.deauth.reason_code); 1100 1101 if (type == EVENT_UNPROT_DISASSOC) { 1102 event.unprot_disassoc.sa = mgmt->sa; 1103 event.unprot_disassoc.da = mgmt->da; 1104 event.unprot_disassoc.reason_code = reason_code; 1105 } else { 1106 event.unprot_deauth.sa = mgmt->sa; 1107 event.unprot_deauth.da = mgmt->da; 1108 event.unprot_deauth.reason_code = reason_code; 1109 } 1110 1111 wpa_supplicant_event(drv->ctx, type, &event); 1112 } 1113 1114 1115 static void mlme_event(struct wpa_driver_nl80211_data *drv, 1116 enum nl80211_commands cmd, struct nlattr *frame, 1117 struct nlattr *addr, struct nlattr *timed_out, 1118 struct nlattr *freq, struct nlattr *ack, 1119 struct nlattr *cookie) 1120 { 1121 if (timed_out && addr) { 1122 mlme_timeout_event(drv, cmd, addr); 1123 return; 1124 } 1125 1126 if (frame == NULL) { 1127 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame " 1128 "data", cmd); 1129 return; 1130 } 1131 1132 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd); 1133 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame", 1134 nla_data(frame), nla_len(frame)); 1135 1136 switch (cmd) { 1137 case NL80211_CMD_AUTHENTICATE: 1138 mlme_event_auth(drv, nla_data(frame), nla_len(frame)); 1139 break; 1140 case NL80211_CMD_ASSOCIATE: 1141 mlme_event_assoc(drv, nla_data(frame), nla_len(frame)); 1142 break; 1143 case NL80211_CMD_DEAUTHENTICATE: 1144 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH, 1145 nla_data(frame), nla_len(frame)); 1146 break; 1147 case NL80211_CMD_DISASSOCIATE: 1148 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC, 1149 nla_data(frame), nla_len(frame)); 1150 break; 1151 case NL80211_CMD_FRAME: 1152 mlme_event_mgmt(drv, freq, nla_data(frame), nla_len(frame)); 1153 break; 1154 case NL80211_CMD_FRAME_TX_STATUS: 1155 mlme_event_action_tx_status(drv, cookie, nla_data(frame), 1156 nla_len(frame), ack); 1157 break; 1158 case NL80211_CMD_UNPROT_DEAUTHENTICATE: 1159 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH, 1160 nla_data(frame), nla_len(frame)); 1161 break; 1162 case NL80211_CMD_UNPROT_DISASSOCIATE: 1163 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC, 1164 nla_data(frame), nla_len(frame)); 1165 break; 1166 default: 1167 break; 1168 } 1169 } 1170 1171 1172 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv, 1173 struct nlattr *tb[]) 1174 { 1175 union wpa_event_data data; 1176 1177 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure"); 1178 os_memset(&data, 0, sizeof(data)); 1179 if (tb[NL80211_ATTR_MAC]) { 1180 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address", 1181 nla_data(tb[NL80211_ATTR_MAC]), 1182 nla_len(tb[NL80211_ATTR_MAC])); 1183 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]); 1184 } 1185 if (tb[NL80211_ATTR_KEY_SEQ]) { 1186 wpa_hexdump(MSG_DEBUG, "nl80211: TSC", 1187 nla_data(tb[NL80211_ATTR_KEY_SEQ]), 1188 nla_len(tb[NL80211_ATTR_KEY_SEQ])); 1189 } 1190 if (tb[NL80211_ATTR_KEY_TYPE]) { 1191 enum nl80211_key_type key_type = 1192 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]); 1193 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type); 1194 if (key_type == NL80211_KEYTYPE_PAIRWISE) 1195 data.michael_mic_failure.unicast = 1; 1196 } else 1197 data.michael_mic_failure.unicast = 1; 1198 1199 if (tb[NL80211_ATTR_KEY_IDX]) { 1200 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]); 1201 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id); 1202 } 1203 1204 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data); 1205 } 1206 1207 1208 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv, 1209 struct nlattr *tb[]) 1210 { 1211 if (tb[NL80211_ATTR_MAC] == NULL) { 1212 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined " 1213 "event"); 1214 return; 1215 } 1216 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 1217 drv->associated = 1; 1218 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined", 1219 MAC2STR(drv->bssid)); 1220 1221 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL); 1222 } 1223 1224 1225 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv, 1226 int cancel_event, struct nlattr *tb[]) 1227 { 1228 unsigned int freq, chan_type, duration; 1229 union wpa_event_data data; 1230 u64 cookie; 1231 1232 if (tb[NL80211_ATTR_WIPHY_FREQ]) 1233 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]); 1234 else 1235 freq = 0; 1236 1237 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) 1238 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]); 1239 else 1240 chan_type = 0; 1241 1242 if (tb[NL80211_ATTR_DURATION]) 1243 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]); 1244 else 1245 duration = 0; 1246 1247 if (tb[NL80211_ATTR_COOKIE]) 1248 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]); 1249 else 1250 cookie = 0; 1251 1252 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d " 1253 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))", 1254 cancel_event, freq, chan_type, duration, 1255 (long long unsigned int) cookie, 1256 cookie == drv->remain_on_chan_cookie ? "match" : "unknown"); 1257 1258 if (cookie != drv->remain_on_chan_cookie) 1259 return; /* not for us */ 1260 1261 if (cancel_event) 1262 drv->pending_remain_on_chan = 0; 1263 1264 os_memset(&data, 0, sizeof(data)); 1265 data.remain_on_channel.freq = freq; 1266 data.remain_on_channel.duration = duration; 1267 wpa_supplicant_event(drv->ctx, cancel_event ? 1268 EVENT_CANCEL_REMAIN_ON_CHANNEL : 1269 EVENT_REMAIN_ON_CHANNEL, &data); 1270 } 1271 1272 1273 static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted, 1274 struct nlattr *tb[]) 1275 { 1276 union wpa_event_data event; 1277 struct nlattr *nl; 1278 int rem; 1279 struct scan_info *info; 1280 #define MAX_REPORT_FREQS 50 1281 int freqs[MAX_REPORT_FREQS]; 1282 int num_freqs = 0; 1283 1284 os_memset(&event, 0, sizeof(event)); 1285 info = &event.scan_info; 1286 info->aborted = aborted; 1287 1288 if (tb[NL80211_ATTR_SCAN_SSIDS]) { 1289 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) { 1290 struct wpa_driver_scan_ssid *s = 1291 &info->ssids[info->num_ssids]; 1292 s->ssid = nla_data(nl); 1293 s->ssid_len = nla_len(nl); 1294 info->num_ssids++; 1295 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS) 1296 break; 1297 } 1298 } 1299 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) { 1300 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem) 1301 { 1302 freqs[num_freqs] = nla_get_u32(nl); 1303 num_freqs++; 1304 if (num_freqs == MAX_REPORT_FREQS - 1) 1305 break; 1306 } 1307 info->freqs = freqs; 1308 info->num_freqs = num_freqs; 1309 } 1310 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event); 1311 } 1312 1313 1314 static int get_link_signal(struct nl_msg *msg, void *arg) 1315 { 1316 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 1317 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 1318 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; 1319 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = { 1320 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, 1321 }; 1322 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; 1323 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { 1324 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, 1325 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, 1326 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, 1327 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, 1328 }; 1329 struct wpa_signal_info *sig_change = arg; 1330 1331 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 1332 genlmsg_attrlen(gnlh, 0), NULL); 1333 if (!tb[NL80211_ATTR_STA_INFO] || 1334 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, 1335 tb[NL80211_ATTR_STA_INFO], policy)) 1336 return NL_SKIP; 1337 if (!sinfo[NL80211_STA_INFO_SIGNAL]) 1338 return NL_SKIP; 1339 1340 sig_change->current_signal = 1341 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); 1342 1343 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) { 1344 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, 1345 sinfo[NL80211_STA_INFO_TX_BITRATE], 1346 rate_policy)) { 1347 sig_change->current_txrate = 0; 1348 } else { 1349 if (rinfo[NL80211_RATE_INFO_BITRATE]) { 1350 sig_change->current_txrate = 1351 nla_get_u16(rinfo[ 1352 NL80211_RATE_INFO_BITRATE]) * 100; 1353 } 1354 } 1355 } 1356 1357 return NL_SKIP; 1358 } 1359 1360 1361 static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv, 1362 struct wpa_signal_info *sig) 1363 { 1364 struct nl_msg *msg; 1365 1366 sig->current_signal = -9999; 1367 sig->current_txrate = 0; 1368 1369 msg = nlmsg_alloc(); 1370 if (!msg) 1371 return -ENOMEM; 1372 1373 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION); 1374 1375 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 1376 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid); 1377 1378 return send_and_recv_msgs(drv, msg, get_link_signal, sig); 1379 nla_put_failure: 1380 nlmsg_free(msg); 1381 return -ENOBUFS; 1382 } 1383 1384 1385 static int get_link_noise(struct nl_msg *msg, void *arg) 1386 { 1387 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 1388 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 1389 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; 1390 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { 1391 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, 1392 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, 1393 }; 1394 struct wpa_signal_info *sig_change = arg; 1395 1396 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 1397 genlmsg_attrlen(gnlh, 0), NULL); 1398 1399 if (!tb[NL80211_ATTR_SURVEY_INFO]) { 1400 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!"); 1401 return NL_SKIP; 1402 } 1403 1404 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, 1405 tb[NL80211_ATTR_SURVEY_INFO], 1406 survey_policy)) { 1407 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested " 1408 "attributes!"); 1409 return NL_SKIP; 1410 } 1411 1412 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) 1413 return NL_SKIP; 1414 1415 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) != 1416 sig_change->frequency) 1417 return NL_SKIP; 1418 1419 if (!sinfo[NL80211_SURVEY_INFO_NOISE]) 1420 return NL_SKIP; 1421 1422 sig_change->current_noise = 1423 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); 1424 1425 return NL_SKIP; 1426 } 1427 1428 1429 static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv, 1430 struct wpa_signal_info *sig_change) 1431 { 1432 struct nl_msg *msg; 1433 1434 sig_change->current_noise = 9999; 1435 sig_change->frequency = drv->assoc_freq; 1436 1437 msg = nlmsg_alloc(); 1438 if (!msg) 1439 return -ENOMEM; 1440 1441 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY); 1442 1443 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 1444 1445 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change); 1446 nla_put_failure: 1447 nlmsg_free(msg); 1448 return -ENOBUFS; 1449 } 1450 1451 1452 static int get_noise_for_scan_results(struct nl_msg *msg, void *arg) 1453 { 1454 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 1455 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 1456 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; 1457 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { 1458 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, 1459 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, 1460 }; 1461 struct wpa_scan_results *scan_results = arg; 1462 struct wpa_scan_res *scan_res; 1463 size_t i; 1464 1465 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 1466 genlmsg_attrlen(gnlh, 0), NULL); 1467 1468 if (!tb[NL80211_ATTR_SURVEY_INFO]) { 1469 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing"); 1470 return NL_SKIP; 1471 } 1472 1473 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, 1474 tb[NL80211_ATTR_SURVEY_INFO], 1475 survey_policy)) { 1476 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested " 1477 "attributes"); 1478 return NL_SKIP; 1479 } 1480 1481 if (!sinfo[NL80211_SURVEY_INFO_NOISE]) 1482 return NL_SKIP; 1483 1484 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) 1485 return NL_SKIP; 1486 1487 for (i = 0; i < scan_results->num; ++i) { 1488 scan_res = scan_results->res[i]; 1489 if (!scan_res) 1490 continue; 1491 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) != 1492 scan_res->freq) 1493 continue; 1494 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID)) 1495 continue; 1496 scan_res->noise = (s8) 1497 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); 1498 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID; 1499 } 1500 1501 return NL_SKIP; 1502 } 1503 1504 1505 static int nl80211_get_noise_for_scan_results( 1506 struct wpa_driver_nl80211_data *drv, 1507 struct wpa_scan_results *scan_res) 1508 { 1509 struct nl_msg *msg; 1510 1511 msg = nlmsg_alloc(); 1512 if (!msg) 1513 return -ENOMEM; 1514 1515 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY); 1516 1517 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 1518 1519 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results, 1520 scan_res); 1521 nla_put_failure: 1522 nlmsg_free(msg); 1523 return -ENOBUFS; 1524 } 1525 1526 1527 static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv, 1528 struct nlattr *tb[]) 1529 { 1530 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = { 1531 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 }, 1532 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 }, 1533 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 }, 1534 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 }, 1535 }; 1536 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1]; 1537 enum nl80211_cqm_rssi_threshold_event event; 1538 union wpa_event_data ed; 1539 struct wpa_signal_info sig; 1540 int res; 1541 1542 if (tb[NL80211_ATTR_CQM] == NULL || 1543 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM], 1544 cqm_policy)) { 1545 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event"); 1546 return; 1547 } 1548 1549 os_memset(&ed, 0, sizeof(ed)); 1550 1551 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) { 1552 if (!tb[NL80211_ATTR_MAC]) 1553 return; 1554 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]), 1555 ETH_ALEN); 1556 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed); 1557 return; 1558 } 1559 1560 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL) 1561 return; 1562 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]); 1563 1564 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) { 1565 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor " 1566 "event: RSSI high"); 1567 ed.signal_change.above_threshold = 1; 1568 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) { 1569 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor " 1570 "event: RSSI low"); 1571 ed.signal_change.above_threshold = 0; 1572 } else 1573 return; 1574 1575 res = nl80211_get_link_signal(drv, &sig); 1576 if (res == 0) { 1577 ed.signal_change.current_signal = sig.current_signal; 1578 ed.signal_change.current_txrate = sig.current_txrate; 1579 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d", 1580 sig.current_signal, sig.current_txrate); 1581 } 1582 1583 res = nl80211_get_link_noise(drv, &sig); 1584 if (res == 0) { 1585 ed.signal_change.current_noise = sig.current_noise; 1586 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm", 1587 sig.current_noise); 1588 } 1589 1590 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed); 1591 } 1592 1593 1594 static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv, 1595 struct nlattr **tb) 1596 { 1597 u8 *addr; 1598 union wpa_event_data data; 1599 1600 if (tb[NL80211_ATTR_MAC] == NULL) 1601 return; 1602 addr = nla_data(tb[NL80211_ATTR_MAC]); 1603 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr)); 1604 1605 if (is_ap_interface(drv->nlmode) && drv->no_monitor_iface_capab) { 1606 u8 *ies = NULL; 1607 size_t ies_len = 0; 1608 if (tb[NL80211_ATTR_IE]) { 1609 ies = nla_data(tb[NL80211_ATTR_IE]); 1610 ies_len = nla_len(tb[NL80211_ATTR_IE]); 1611 } 1612 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len); 1613 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0); 1614 return; 1615 } 1616 1617 if (drv->nlmode != NL80211_IFTYPE_ADHOC) 1618 return; 1619 1620 os_memset(&data, 0, sizeof(data)); 1621 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN); 1622 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data); 1623 } 1624 1625 1626 static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv, 1627 struct nlattr **tb) 1628 { 1629 u8 *addr; 1630 union wpa_event_data data; 1631 1632 if (tb[NL80211_ATTR_MAC] == NULL) 1633 return; 1634 addr = nla_data(tb[NL80211_ATTR_MAC]); 1635 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR, 1636 MAC2STR(addr)); 1637 1638 if (is_ap_interface(drv->nlmode) && drv->no_monitor_iface_capab) { 1639 drv_event_disassoc(drv->ctx, addr); 1640 return; 1641 } 1642 1643 if (drv->nlmode != NL80211_IFTYPE_ADHOC) 1644 return; 1645 1646 os_memset(&data, 0, sizeof(data)); 1647 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN); 1648 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data); 1649 } 1650 1651 1652 static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv, 1653 struct nlattr **tb) 1654 { 1655 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA]; 1656 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = { 1657 [NL80211_REKEY_DATA_KEK] = { 1658 .minlen = NL80211_KEK_LEN, 1659 .maxlen = NL80211_KEK_LEN, 1660 }, 1661 [NL80211_REKEY_DATA_KCK] = { 1662 .minlen = NL80211_KCK_LEN, 1663 .maxlen = NL80211_KCK_LEN, 1664 }, 1665 [NL80211_REKEY_DATA_REPLAY_CTR] = { 1666 .minlen = NL80211_REPLAY_CTR_LEN, 1667 .maxlen = NL80211_REPLAY_CTR_LEN, 1668 }, 1669 }; 1670 union wpa_event_data data; 1671 1672 if (!tb[NL80211_ATTR_MAC]) 1673 return; 1674 if (!tb[NL80211_ATTR_REKEY_DATA]) 1675 return; 1676 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA, 1677 tb[NL80211_ATTR_REKEY_DATA], rekey_policy)) 1678 return; 1679 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]) 1680 return; 1681 1682 os_memset(&data, 0, sizeof(data)); 1683 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]); 1684 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR, 1685 MAC2STR(data.driver_gtk_rekey.bssid)); 1686 data.driver_gtk_rekey.replay_ctr = 1687 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]); 1688 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter", 1689 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN); 1690 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data); 1691 } 1692 1693 1694 static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv, 1695 struct nlattr **tb) 1696 { 1697 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE]; 1698 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = { 1699 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 }, 1700 [NL80211_PMKSA_CANDIDATE_BSSID] = { 1701 .minlen = ETH_ALEN, 1702 .maxlen = ETH_ALEN, 1703 }, 1704 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG }, 1705 }; 1706 union wpa_event_data data; 1707 1708 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE]) 1709 return; 1710 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE, 1711 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy)) 1712 return; 1713 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] || 1714 !cand[NL80211_PMKSA_CANDIDATE_BSSID]) 1715 return; 1716 1717 os_memset(&data, 0, sizeof(data)); 1718 os_memcpy(data.pmkid_candidate.bssid, 1719 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN); 1720 data.pmkid_candidate.index = 1721 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]); 1722 data.pmkid_candidate.preauth = 1723 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL; 1724 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data); 1725 } 1726 1727 1728 static int process_event(struct nl_msg *msg, void *arg) 1729 { 1730 struct wpa_driver_nl80211_data *drv = arg; 1731 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 1732 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 1733 1734 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 1735 genlmsg_attrlen(gnlh, 0), NULL); 1736 1737 if (tb[NL80211_ATTR_IFINDEX]) { 1738 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]); 1739 if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) { 1740 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)" 1741 " for foreign interface (ifindex %d)", 1742 gnlh->cmd, ifindex); 1743 return NL_SKIP; 1744 } 1745 } 1746 1747 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED && 1748 (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS || 1749 gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) { 1750 wpa_driver_nl80211_set_mode(&drv->first_bss, 1751 drv->ap_scan_as_station); 1752 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED; 1753 } 1754 1755 switch (gnlh->cmd) { 1756 case NL80211_CMD_TRIGGER_SCAN: 1757 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger"); 1758 break; 1759 case NL80211_CMD_START_SCHED_SCAN: 1760 wpa_printf(MSG_DEBUG, "nl80211: Sched scan started"); 1761 break; 1762 case NL80211_CMD_SCHED_SCAN_STOPPED: 1763 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stopped"); 1764 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL); 1765 break; 1766 case NL80211_CMD_NEW_SCAN_RESULTS: 1767 wpa_printf(MSG_DEBUG, "nl80211: New scan results available"); 1768 drv->scan_complete_events = 1; 1769 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, 1770 drv->ctx); 1771 send_scan_event(drv, 0, tb); 1772 break; 1773 case NL80211_CMD_SCHED_SCAN_RESULTS: 1774 wpa_printf(MSG_DEBUG, 1775 "nl80211: New sched scan results available"); 1776 send_scan_event(drv, 0, tb); 1777 break; 1778 case NL80211_CMD_SCAN_ABORTED: 1779 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted"); 1780 /* 1781 * Need to indicate that scan results are available in order 1782 * not to make wpa_supplicant stop its scanning. 1783 */ 1784 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, 1785 drv->ctx); 1786 send_scan_event(drv, 1, tb); 1787 break; 1788 case NL80211_CMD_AUTHENTICATE: 1789 case NL80211_CMD_ASSOCIATE: 1790 case NL80211_CMD_DEAUTHENTICATE: 1791 case NL80211_CMD_DISASSOCIATE: 1792 case NL80211_CMD_FRAME: 1793 case NL80211_CMD_FRAME_TX_STATUS: 1794 case NL80211_CMD_UNPROT_DEAUTHENTICATE: 1795 case NL80211_CMD_UNPROT_DISASSOCIATE: 1796 mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME], 1797 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT], 1798 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK], 1799 tb[NL80211_ATTR_COOKIE]); 1800 break; 1801 case NL80211_CMD_CONNECT: 1802 case NL80211_CMD_ROAM: 1803 mlme_event_connect(drv, gnlh->cmd, 1804 tb[NL80211_ATTR_STATUS_CODE], 1805 tb[NL80211_ATTR_MAC], 1806 tb[NL80211_ATTR_REQ_IE], 1807 tb[NL80211_ATTR_RESP_IE]); 1808 break; 1809 case NL80211_CMD_DISCONNECT: 1810 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE], 1811 tb[NL80211_ATTR_MAC]); 1812 break; 1813 case NL80211_CMD_MICHAEL_MIC_FAILURE: 1814 mlme_event_michael_mic_failure(drv, tb); 1815 break; 1816 case NL80211_CMD_JOIN_IBSS: 1817 mlme_event_join_ibss(drv, tb); 1818 break; 1819 case NL80211_CMD_REMAIN_ON_CHANNEL: 1820 mlme_event_remain_on_channel(drv, 0, tb); 1821 break; 1822 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL: 1823 mlme_event_remain_on_channel(drv, 1, tb); 1824 break; 1825 case NL80211_CMD_NOTIFY_CQM: 1826 nl80211_cqm_event(drv, tb); 1827 break; 1828 case NL80211_CMD_REG_CHANGE: 1829 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change"); 1830 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, 1831 NULL); 1832 break; 1833 case NL80211_CMD_REG_BEACON_HINT: 1834 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint"); 1835 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, 1836 NULL); 1837 break; 1838 case NL80211_CMD_NEW_STATION: 1839 nl80211_new_station_event(drv, tb); 1840 break; 1841 case NL80211_CMD_DEL_STATION: 1842 nl80211_del_station_event(drv, tb); 1843 break; 1844 case NL80211_CMD_SET_REKEY_OFFLOAD: 1845 nl80211_rekey_offload_event(drv, tb); 1846 break; 1847 case NL80211_CMD_PMKSA_CANDIDATE: 1848 nl80211_pmksa_candidate_event(drv, tb); 1849 break; 1850 default: 1851 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event " 1852 "(cmd=%d)", gnlh->cmd); 1853 break; 1854 } 1855 1856 return NL_SKIP; 1857 } 1858 1859 1860 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx, 1861 void *handle) 1862 { 1863 struct nl_cb *cb; 1864 struct wpa_driver_nl80211_data *drv = eloop_ctx; 1865 1866 wpa_printf(MSG_DEBUG, "nl80211: Event message available"); 1867 1868 cb = nl_cb_clone(drv->global->nl_cb); 1869 if (!cb) 1870 return; 1871 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); 1872 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv); 1873 nl_recvmsgs(handle, cb); 1874 nl_cb_put(cb); 1875 } 1876 1877 1878 /** 1879 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain 1880 * @priv: driver_nl80211 private data 1881 * @alpha2_arg: country to which to switch to 1882 * Returns: 0 on success, -1 on failure 1883 * 1884 * This asks nl80211 to set the regulatory domain for given 1885 * country ISO / IEC alpha2. 1886 */ 1887 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg) 1888 { 1889 struct i802_bss *bss = priv; 1890 struct wpa_driver_nl80211_data *drv = bss->drv; 1891 char alpha2[3]; 1892 struct nl_msg *msg; 1893 1894 msg = nlmsg_alloc(); 1895 if (!msg) 1896 return -ENOMEM; 1897 1898 alpha2[0] = alpha2_arg[0]; 1899 alpha2[1] = alpha2_arg[1]; 1900 alpha2[2] = '\0'; 1901 1902 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG); 1903 1904 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2); 1905 if (send_and_recv_msgs(drv, msg, NULL, NULL)) 1906 return -EINVAL; 1907 return 0; 1908 nla_put_failure: 1909 nlmsg_free(msg); 1910 return -EINVAL; 1911 } 1912 1913 1914 struct wiphy_info_data { 1915 struct wpa_driver_capa *capa; 1916 1917 unsigned int error:1; 1918 }; 1919 1920 1921 static int wiphy_info_handler(struct nl_msg *msg, void *arg) 1922 { 1923 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 1924 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 1925 struct wiphy_info_data *info = arg; 1926 int p2p_go_supported = 0, p2p_client_supported = 0; 1927 int p2p_concurrent = 0; 1928 int auth_supported = 0, connect_supported = 0; 1929 struct wpa_driver_capa *capa = info->capa; 1930 static struct nla_policy 1931 iface_combination_policy[NUM_NL80211_IFACE_COMB] = { 1932 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED }, 1933 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 }, 1934 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG }, 1935 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 }, 1936 }, 1937 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = { 1938 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED }, 1939 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 }, 1940 }; 1941 1942 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 1943 genlmsg_attrlen(gnlh, 0), NULL); 1944 1945 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]) 1946 capa->max_scan_ssids = 1947 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]); 1948 1949 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]) 1950 capa->max_sched_scan_ssids = 1951 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]); 1952 1953 if (tb[NL80211_ATTR_MAX_MATCH_SETS]) 1954 capa->max_match_sets = 1955 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]); 1956 1957 if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) { 1958 struct nlattr *nl_mode; 1959 int i; 1960 nla_for_each_nested(nl_mode, 1961 tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) { 1962 switch (nla_type(nl_mode)) { 1963 case NL80211_IFTYPE_AP: 1964 capa->flags |= WPA_DRIVER_FLAGS_AP; 1965 break; 1966 case NL80211_IFTYPE_P2P_GO: 1967 p2p_go_supported = 1; 1968 break; 1969 case NL80211_IFTYPE_P2P_CLIENT: 1970 p2p_client_supported = 1; 1971 break; 1972 } 1973 } 1974 } 1975 1976 if (tb[NL80211_ATTR_INTERFACE_COMBINATIONS]) { 1977 struct nlattr *nl_combi; 1978 int rem_combi; 1979 1980 nla_for_each_nested(nl_combi, 1981 tb[NL80211_ATTR_INTERFACE_COMBINATIONS], 1982 rem_combi) { 1983 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB]; 1984 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT]; 1985 struct nlattr *nl_limit, *nl_mode; 1986 int err, rem_limit, rem_mode; 1987 int combination_has_p2p = 0, combination_has_mgd = 0; 1988 1989 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB, 1990 nl_combi, 1991 iface_combination_policy); 1992 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] || 1993 !tb_comb[NL80211_IFACE_COMB_MAXNUM] || 1994 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) 1995 goto broken_combination; 1996 1997 nla_for_each_nested(nl_limit, 1998 tb_comb[NL80211_IFACE_COMB_LIMITS], 1999 rem_limit) { 2000 err = nla_parse_nested(tb_limit, 2001 MAX_NL80211_IFACE_LIMIT, 2002 nl_limit, 2003 iface_limit_policy); 2004 if (err || 2005 !tb_limit[NL80211_IFACE_LIMIT_TYPES]) 2006 goto broken_combination; 2007 2008 nla_for_each_nested( 2009 nl_mode, 2010 tb_limit[NL80211_IFACE_LIMIT_TYPES], 2011 rem_mode) { 2012 int ift = nla_type(nl_mode); 2013 if (ift == NL80211_IFTYPE_P2P_GO || 2014 ift == NL80211_IFTYPE_P2P_CLIENT) 2015 combination_has_p2p = 1; 2016 if (ift == NL80211_IFTYPE_STATION) 2017 combination_has_mgd = 1; 2018 } 2019 if (combination_has_p2p && combination_has_mgd) 2020 break; 2021 } 2022 2023 if (combination_has_p2p && combination_has_mgd) { 2024 p2p_concurrent = 1; 2025 break; 2026 } 2027 2028 broken_combination: 2029 ; 2030 } 2031 } 2032 2033 if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) { 2034 struct nlattr *nl_cmd; 2035 int i; 2036 2037 nla_for_each_nested(nl_cmd, 2038 tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) { 2039 switch (nla_get_u32(nl_cmd)) { 2040 case NL80211_CMD_AUTHENTICATE: 2041 auth_supported = 1; 2042 break; 2043 case NL80211_CMD_CONNECT: 2044 connect_supported = 1; 2045 break; 2046 case NL80211_CMD_START_SCHED_SCAN: 2047 /* 2048 * Disabled for 1.x for now as it is 2049 * broken there due to the way it ends 2050 * up getting used. 2051 capa->sched_scan_supported = 1; 2052 */ 2053 break; 2054 } 2055 } 2056 } 2057 2058 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) { 2059 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based " 2060 "off-channel TX"); 2061 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX; 2062 } 2063 2064 if (tb[NL80211_ATTR_ROAM_SUPPORT]) { 2065 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming"); 2066 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION; 2067 } 2068 2069 /* default to 5000 since early versions of mac80211 don't set it */ 2070 capa->max_remain_on_chan = 5000; 2071 2072 if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]) 2073 capa->max_remain_on_chan = 2074 nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]); 2075 2076 if (auth_supported) 2077 capa->flags |= WPA_DRIVER_FLAGS_SME; 2078 else if (!connect_supported) { 2079 wpa_printf(MSG_INFO, "nl80211: Driver does not support " 2080 "authentication/association or connect commands"); 2081 info->error = 1; 2082 } 2083 2084 if (p2p_go_supported && p2p_client_supported) 2085 capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE; 2086 if (p2p_concurrent) { 2087 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group " 2088 "interface (driver advertised support)"); 2089 capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT; 2090 capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P; 2091 } 2092 2093 if (tb[NL80211_ATTR_TDLS_SUPPORT]) { 2094 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported"); 2095 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT; 2096 2097 if (tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]) { 2098 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup"); 2099 capa->flags |= 2100 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP; 2101 } 2102 } 2103 2104 return NL_SKIP; 2105 } 2106 2107 2108 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv, 2109 struct wiphy_info_data *info) 2110 { 2111 struct nl_msg *msg; 2112 2113 os_memset(info, 0, sizeof(*info)); 2114 info->capa = &drv->capa; 2115 2116 msg = nlmsg_alloc(); 2117 if (!msg) 2118 return -1; 2119 2120 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY); 2121 2122 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex); 2123 2124 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0) 2125 return 0; 2126 msg = NULL; 2127 nla_put_failure: 2128 nlmsg_free(msg); 2129 return -1; 2130 } 2131 2132 2133 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv) 2134 { 2135 struct wiphy_info_data info; 2136 if (wpa_driver_nl80211_get_info(drv, &info)) 2137 return -1; 2138 2139 if (info.error) 2140 return -1; 2141 2142 drv->has_capability = 1; 2143 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */ 2144 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | 2145 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK | 2146 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | 2147 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; 2148 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 | 2149 WPA_DRIVER_CAPA_ENC_WEP104 | 2150 WPA_DRIVER_CAPA_ENC_TKIP | 2151 WPA_DRIVER_CAPA_ENC_CCMP; 2152 drv->capa.auth = WPA_DRIVER_AUTH_OPEN | 2153 WPA_DRIVER_AUTH_SHARED | 2154 WPA_DRIVER_AUTH_LEAP; 2155 2156 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES; 2157 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE; 2158 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS; 2159 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS; 2160 2161 return 0; 2162 } 2163 2164 2165 #ifdef ANDROID 2166 static int android_genl_ctrl_resolve(struct nl_handle *handle, 2167 const char *name) 2168 { 2169 /* 2170 * Android ICS has very minimal genl_ctrl_resolve() implementation, so 2171 * need to work around that. 2172 */ 2173 struct nl_cache *cache = NULL; 2174 struct genl_family *nl80211 = NULL; 2175 int id = -1; 2176 2177 if (genl_ctrl_alloc_cache(handle, &cache) < 0) { 2178 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic " 2179 "netlink cache"); 2180 goto fail; 2181 } 2182 2183 nl80211 = genl_ctrl_search_by_name(cache, name); 2184 if (nl80211 == NULL) 2185 goto fail; 2186 2187 id = genl_family_get_id(nl80211); 2188 2189 fail: 2190 if (nl80211) 2191 genl_family_put(nl80211); 2192 if (cache) 2193 nl_cache_free(cache); 2194 2195 return id; 2196 } 2197 #define genl_ctrl_resolve android_genl_ctrl_resolve 2198 #endif /* ANDROID */ 2199 2200 2201 static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global) 2202 { 2203 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); 2204 if (global->nl_cb == NULL) { 2205 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " 2206 "callbacks"); 2207 return -1; 2208 } 2209 2210 if (nl_create_handles(&global->nl, global->nl_cb, "nl")) 2211 return -1; 2212 2213 global->nl80211 = genl_ctrl_search_by_name(global->nl.cache, 2214 "nl80211"); 2215 if (global->nl80211 == NULL) { 2216 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not " 2217 "found"); 2218 return -1; 2219 } 2220 2221 return 0; 2222 } 2223 2224 2225 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv) 2226 { 2227 struct nl80211_global *global = drv->global; 2228 int ret; 2229 2230 /* Initialize generic netlink and nl80211 */ 2231 2232 if (nl_create_handles(&drv->nl_event, global->nl_cb, "event")) 2233 goto err3; 2234 2235 ret = nl_get_multicast_id(drv, "nl80211", "scan"); 2236 if (ret >= 0) 2237 ret = nl_socket_add_membership(drv->nl_event.handle, ret); 2238 if (ret < 0) { 2239 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast " 2240 "membership for scan events: %d (%s)", 2241 ret, strerror(-ret)); 2242 goto err4; 2243 } 2244 2245 ret = nl_get_multicast_id(drv, "nl80211", "mlme"); 2246 if (ret >= 0) 2247 ret = nl_socket_add_membership(drv->nl_event.handle, ret); 2248 if (ret < 0) { 2249 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast " 2250 "membership for mlme events: %d (%s)", 2251 ret, strerror(-ret)); 2252 goto err4; 2253 } 2254 2255 ret = nl_get_multicast_id(drv, "nl80211", "regulatory"); 2256 if (ret >= 0) 2257 ret = nl_socket_add_membership(drv->nl_event.handle, ret); 2258 if (ret < 0) { 2259 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast " 2260 "membership for regulatory events: %d (%s)", 2261 ret, strerror(-ret)); 2262 /* Continue without regulatory events */ 2263 } 2264 2265 eloop_register_read_sock(nl_socket_get_fd(drv->nl_event.handle), 2266 wpa_driver_nl80211_event_receive, drv, 2267 drv->nl_event.handle); 2268 2269 return 0; 2270 2271 err4: 2272 nl_destroy_handles(&drv->nl_event); 2273 err3: 2274 return -1; 2275 } 2276 2277 2278 static void wpa_driver_nl80211_rfkill_blocked(void *ctx) 2279 { 2280 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked"); 2281 /* 2282 * This may be for any interface; use ifdown event to disable 2283 * interface. 2284 */ 2285 } 2286 2287 2288 static void wpa_driver_nl80211_rfkill_unblocked(void *ctx) 2289 { 2290 struct wpa_driver_nl80211_data *drv = ctx; 2291 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked"); 2292 if (linux_set_iface_flags(drv->global->ioctl_sock, 2293 drv->first_bss.ifname, 1)) { 2294 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP " 2295 "after rfkill unblock"); 2296 return; 2297 } 2298 /* rtnetlink ifup handler will report interface as enabled */ 2299 } 2300 2301 2302 static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv) 2303 { 2304 /* Find phy (radio) to which this interface belongs */ 2305 char buf[90], *pos; 2306 int f, rv; 2307 2308 drv->phyname[0] = '\0'; 2309 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name", 2310 drv->first_bss.ifname); 2311 f = open(buf, O_RDONLY); 2312 if (f < 0) { 2313 wpa_printf(MSG_DEBUG, "Could not open file %s: %s", 2314 buf, strerror(errno)); 2315 return; 2316 } 2317 2318 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1); 2319 close(f); 2320 if (rv < 0) { 2321 wpa_printf(MSG_DEBUG, "Could not read file %s: %s", 2322 buf, strerror(errno)); 2323 return; 2324 } 2325 2326 drv->phyname[rv] = '\0'; 2327 pos = os_strchr(drv->phyname, '\n'); 2328 if (pos) 2329 *pos = '\0'; 2330 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s", 2331 drv->first_bss.ifname, drv->phyname); 2332 } 2333 2334 2335 #ifdef CONFIG_AP 2336 static void nl80211_l2_read(void *ctx, const u8 *src_addr, const u8 *buf, 2337 size_t len) 2338 { 2339 wpa_printf(MSG_DEBUG, "nl80211: l2_packet read %u", 2340 (unsigned int) len); 2341 } 2342 #endif /* CONFIG_AP */ 2343 2344 2345 /** 2346 * wpa_driver_nl80211_init - Initialize nl80211 driver interface 2347 * @ctx: context to be used when calling wpa_supplicant functions, 2348 * e.g., wpa_supplicant_event() 2349 * @ifname: interface name, e.g., wlan0 2350 * @global_priv: private driver global data from global_init() 2351 * Returns: Pointer to private data, %NULL on failure 2352 */ 2353 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname, 2354 void *global_priv) 2355 { 2356 struct wpa_driver_nl80211_data *drv; 2357 struct rfkill_config *rcfg; 2358 struct i802_bss *bss; 2359 2360 if (global_priv == NULL) 2361 return NULL; 2362 drv = os_zalloc(sizeof(*drv)); 2363 if (drv == NULL) 2364 return NULL; 2365 drv->global = global_priv; 2366 drv->ctx = ctx; 2367 bss = &drv->first_bss; 2368 bss->drv = drv; 2369 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname)); 2370 drv->monitor_ifidx = -1; 2371 drv->monitor_sock = -1; 2372 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED; 2373 2374 if (wpa_driver_nl80211_init_nl(drv)) { 2375 os_free(drv); 2376 return NULL; 2377 } 2378 2379 nl80211_get_phy_name(drv); 2380 2381 rcfg = os_zalloc(sizeof(*rcfg)); 2382 if (rcfg == NULL) 2383 goto failed; 2384 rcfg->ctx = drv; 2385 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname)); 2386 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked; 2387 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked; 2388 drv->rfkill = rfkill_init(rcfg); 2389 if (drv->rfkill == NULL) { 2390 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available"); 2391 os_free(rcfg); 2392 } 2393 2394 if (wpa_driver_nl80211_finish_drv_init(drv)) 2395 goto failed; 2396 2397 #ifdef CONFIG_AP 2398 drv->l2 = l2_packet_init(ifname, NULL, ETH_P_EAPOL, 2399 nl80211_l2_read, drv, 0); 2400 #endif /* CONFIG_AP */ 2401 2402 if (drv->global) { 2403 dl_list_add(&drv->global->interfaces, &drv->list); 2404 drv->in_interface_list = 1; 2405 } 2406 2407 return bss; 2408 2409 failed: 2410 wpa_driver_nl80211_deinit(bss); 2411 return NULL; 2412 } 2413 2414 2415 static int nl80211_register_frame(struct wpa_driver_nl80211_data *drv, 2416 struct nl_handle *nl_handle, 2417 u16 type, const u8 *match, size_t match_len) 2418 { 2419 struct nl_msg *msg; 2420 int ret = -1; 2421 2422 msg = nlmsg_alloc(); 2423 if (!msg) 2424 return -1; 2425 2426 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION); 2427 2428 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 2429 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type); 2430 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match); 2431 2432 ret = send_and_recv(drv, nl_handle, msg, NULL, NULL); 2433 msg = NULL; 2434 if (ret) { 2435 wpa_printf(MSG_DEBUG, "nl80211: Register frame command " 2436 "failed (type=%u): ret=%d (%s)", 2437 type, ret, strerror(-ret)); 2438 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match", 2439 match, match_len); 2440 goto nla_put_failure; 2441 } 2442 ret = 0; 2443 nla_put_failure: 2444 nlmsg_free(msg); 2445 return ret; 2446 } 2447 2448 2449 static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv, 2450 const u8 *match, size_t match_len) 2451 { 2452 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4); 2453 return nl80211_register_frame(drv, drv->nl_event.handle, 2454 type, match, match_len); 2455 } 2456 2457 2458 static int nl80211_register_action_frames(struct wpa_driver_nl80211_data *drv) 2459 { 2460 #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) 2461 /* GAS Initial Request */ 2462 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0a", 2) < 0) 2463 return -1; 2464 /* GAS Initial Response */ 2465 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0b", 2) < 0) 2466 return -1; 2467 /* GAS Comeback Request */ 2468 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0c", 2) < 0) 2469 return -1; 2470 /* GAS Comeback Response */ 2471 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0d", 2) < 0) 2472 return -1; 2473 #endif /* CONFIG_P2P || CONFIG_INTERWORKING */ 2474 #ifdef CONFIG_P2P 2475 /* P2P Public Action */ 2476 if (nl80211_register_action_frame(drv, 2477 (u8 *) "\x04\x09\x50\x6f\x9a\x09", 2478 6) < 0) 2479 return -1; 2480 /* P2P Action */ 2481 if (nl80211_register_action_frame(drv, 2482 (u8 *) "\x7f\x50\x6f\x9a\x09", 2483 5) < 0) 2484 return -1; 2485 #endif /* CONFIG_P2P */ 2486 #ifdef CONFIG_IEEE80211W 2487 /* SA Query Response */ 2488 if (nl80211_register_action_frame(drv, (u8 *) "\x08\x01", 2) < 0) 2489 return -1; 2490 #endif /* CONFIG_IEEE80211W */ 2491 #ifdef CONFIG_TDLS 2492 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) { 2493 /* TDLS Discovery Response */ 2494 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0e", 2) < 2495 0) 2496 return -1; 2497 } 2498 #endif /* CONFIG_TDLS */ 2499 2500 /* FT Action frames */ 2501 if (nl80211_register_action_frame(drv, (u8 *) "\x06", 1) < 0) 2502 return -1; 2503 else 2504 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT | 2505 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK; 2506 2507 /* WNM - BSS Transition Management Request */ 2508 if (nl80211_register_action_frame(drv, (u8 *) "\x0a\x07", 2) < 0) 2509 return -1; 2510 2511 return 0; 2512 } 2513 2514 2515 static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx) 2516 { 2517 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL); 2518 } 2519 2520 2521 static int 2522 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv) 2523 { 2524 struct i802_bss *bss = &drv->first_bss; 2525 int send_rfkill_event = 0; 2526 2527 drv->ifindex = if_nametoindex(bss->ifname); 2528 drv->first_bss.ifindex = drv->ifindex; 2529 2530 #ifndef HOSTAPD 2531 /* 2532 * Make sure the interface starts up in station mode unless this is a 2533 * dynamically added interface (e.g., P2P) that was already configured 2534 * with proper iftype. 2535 */ 2536 if (drv->ifindex != drv->global->if_add_ifindex && 2537 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) { 2538 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to " 2539 "use managed mode"); 2540 return -1; 2541 } 2542 2543 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) { 2544 if (rfkill_is_blocked(drv->rfkill)) { 2545 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable " 2546 "interface '%s' due to rfkill", 2547 bss->ifname); 2548 drv->if_disabled = 1; 2549 send_rfkill_event = 1; 2550 } else { 2551 wpa_printf(MSG_ERROR, "nl80211: Could not set " 2552 "interface '%s' UP", bss->ifname); 2553 return -1; 2554 } 2555 } 2556 2557 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 2558 1, IF_OPER_DORMANT); 2559 #endif /* HOSTAPD */ 2560 2561 if (wpa_driver_nl80211_capa(drv)) 2562 return -1; 2563 2564 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 2565 drv->addr)) 2566 return -1; 2567 2568 if (nl80211_register_action_frames(drv) < 0) { 2569 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action " 2570 "frame processing - ignore for now"); 2571 /* 2572 * Older kernel versions did not support this, so ignore the 2573 * error for now. Some functionality may not be available 2574 * because of this. 2575 */ 2576 } 2577 2578 if (send_rfkill_event) { 2579 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill, 2580 drv, drv->ctx); 2581 } 2582 2583 return 0; 2584 } 2585 2586 2587 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv) 2588 { 2589 struct nl_msg *msg; 2590 2591 msg = nlmsg_alloc(); 2592 if (!msg) 2593 return -ENOMEM; 2594 2595 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON); 2596 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 2597 2598 return send_and_recv_msgs(drv, msg, NULL, NULL); 2599 nla_put_failure: 2600 nlmsg_free(msg); 2601 return -ENOBUFS; 2602 } 2603 2604 2605 /** 2606 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface 2607 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init() 2608 * 2609 * Shut down driver interface and processing of driver events. Free 2610 * private data buffer if one was allocated in wpa_driver_nl80211_init(). 2611 */ 2612 static void wpa_driver_nl80211_deinit(void *priv) 2613 { 2614 struct i802_bss *bss = priv; 2615 struct wpa_driver_nl80211_data *drv = bss->drv; 2616 2617 #ifdef CONFIG_AP 2618 if (drv->l2) 2619 l2_packet_deinit(drv->l2); 2620 #endif /* CONFIG_AP */ 2621 2622 if (drv->nl_preq.handle) 2623 wpa_driver_nl80211_probe_req_report(bss, 0); 2624 if (bss->added_if_into_bridge) { 2625 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname, 2626 bss->ifname) < 0) 2627 wpa_printf(MSG_INFO, "nl80211: Failed to remove " 2628 "interface %s from bridge %s: %s", 2629 bss->ifname, bss->brname, strerror(errno)); 2630 } 2631 if (bss->added_bridge) { 2632 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0) 2633 wpa_printf(MSG_INFO, "nl80211: Failed to remove " 2634 "bridge %s: %s", 2635 bss->brname, strerror(errno)); 2636 } 2637 2638 nl80211_remove_monitor_interface(drv); 2639 2640 if (is_ap_interface(drv->nlmode)) 2641 wpa_driver_nl80211_del_beacon(drv); 2642 2643 #ifdef HOSTAPD 2644 if (drv->last_freq_ht) { 2645 /* Clear HT flags from the driver */ 2646 struct hostapd_freq_params freq; 2647 os_memset(&freq, 0, sizeof(freq)); 2648 freq.freq = drv->last_freq; 2649 i802_set_freq(priv, &freq); 2650 } 2651 2652 if (drv->eapol_sock >= 0) { 2653 eloop_unregister_read_sock(drv->eapol_sock); 2654 close(drv->eapol_sock); 2655 } 2656 2657 if (drv->if_indices != drv->default_if_indices) 2658 os_free(drv->if_indices); 2659 #endif /* HOSTAPD */ 2660 2661 if (drv->disabled_11b_rates) 2662 nl80211_disable_11b_rates(drv, drv->ifindex, 0); 2663 2664 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0, 2665 IF_OPER_UP); 2666 rfkill_deinit(drv->rfkill); 2667 2668 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx); 2669 2670 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0); 2671 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION); 2672 2673 eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_event.handle)); 2674 nl_destroy_handles(&drv->nl_event); 2675 2676 os_free(drv->filter_ssids); 2677 2678 if (drv->in_interface_list) 2679 dl_list_del(&drv->list); 2680 2681 os_free(drv); 2682 } 2683 2684 2685 /** 2686 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion 2687 * @eloop_ctx: Driver private data 2688 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init() 2689 * 2690 * This function can be used as registered timeout when starting a scan to 2691 * generate a scan completed event if the driver does not report this. 2692 */ 2693 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx) 2694 { 2695 struct wpa_driver_nl80211_data *drv = eloop_ctx; 2696 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) { 2697 wpa_driver_nl80211_set_mode(&drv->first_bss, 2698 drv->ap_scan_as_station); 2699 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED; 2700 } 2701 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results"); 2702 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL); 2703 } 2704 2705 2706 /** 2707 * wpa_driver_nl80211_scan - Request the driver to initiate scan 2708 * @priv: Pointer to private driver data from wpa_driver_nl80211_init() 2709 * @params: Scan parameters 2710 * Returns: 0 on success, -1 on failure 2711 */ 2712 static int wpa_driver_nl80211_scan(void *priv, 2713 struct wpa_driver_scan_params *params) 2714 { 2715 struct i802_bss *bss = priv; 2716 struct wpa_driver_nl80211_data *drv = bss->drv; 2717 int ret = 0, timeout; 2718 struct nl_msg *msg, *ssids, *freqs, *rates; 2719 size_t i; 2720 2721 msg = nlmsg_alloc(); 2722 ssids = nlmsg_alloc(); 2723 freqs = nlmsg_alloc(); 2724 rates = nlmsg_alloc(); 2725 if (!msg || !ssids || !freqs || !rates) { 2726 nlmsg_free(msg); 2727 nlmsg_free(ssids); 2728 nlmsg_free(freqs); 2729 nlmsg_free(rates); 2730 return -1; 2731 } 2732 2733 os_free(drv->filter_ssids); 2734 drv->filter_ssids = params->filter_ssids; 2735 params->filter_ssids = NULL; 2736 drv->num_filter_ssids = params->num_filter_ssids; 2737 2738 nl80211_cmd(drv, msg, 0, NL80211_CMD_TRIGGER_SCAN); 2739 2740 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 2741 2742 for (i = 0; i < params->num_ssids; i++) { 2743 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID", 2744 params->ssids[i].ssid, 2745 params->ssids[i].ssid_len); 2746 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len, 2747 params->ssids[i].ssid); 2748 } 2749 if (params->num_ssids) 2750 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); 2751 2752 if (params->extra_ies) { 2753 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan extra IEs", 2754 params->extra_ies, params->extra_ies_len); 2755 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len, 2756 params->extra_ies); 2757 } 2758 2759 if (params->freqs) { 2760 for (i = 0; params->freqs[i]; i++) { 2761 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u " 2762 "MHz", params->freqs[i]); 2763 NLA_PUT_U32(freqs, i + 1, params->freqs[i]); 2764 } 2765 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs); 2766 } 2767 2768 if (params->p2p_probe) { 2769 /* 2770 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates 2771 * by masking out everything else apart from the OFDM rates 6, 2772 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz 2773 * rates are left enabled. 2774 */ 2775 NLA_PUT(rates, NL80211_BAND_2GHZ, 8, 2776 "\x0c\x12\x18\x24\x30\x48\x60\x6c"); 2777 nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates); 2778 2779 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE); 2780 } 2781 2782 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 2783 msg = NULL; 2784 if (ret) { 2785 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d " 2786 "(%s)", ret, strerror(-ret)); 2787 #ifdef HOSTAPD 2788 if (is_ap_interface(drv->nlmode)) { 2789 /* 2790 * mac80211 does not allow scan requests in AP mode, so 2791 * try to do this in station mode. 2792 */ 2793 if (wpa_driver_nl80211_set_mode( 2794 bss, NL80211_IFTYPE_STATION)) 2795 goto nla_put_failure; 2796 2797 if (wpa_driver_nl80211_scan(drv, params)) { 2798 wpa_driver_nl80211_set_mode(bss, drv->nlmode); 2799 goto nla_put_failure; 2800 } 2801 2802 /* Restore AP mode when processing scan results */ 2803 drv->ap_scan_as_station = drv->nlmode; 2804 ret = 0; 2805 } else 2806 goto nla_put_failure; 2807 #else /* HOSTAPD */ 2808 goto nla_put_failure; 2809 #endif /* HOSTAPD */ 2810 } 2811 2812 /* Not all drivers generate "scan completed" wireless event, so try to 2813 * read results after a timeout. */ 2814 timeout = 10; 2815 if (drv->scan_complete_events) { 2816 /* 2817 * The driver seems to deliver events to notify when scan is 2818 * complete, so use longer timeout to avoid race conditions 2819 * with scanning and following association request. 2820 */ 2821 timeout = 30; 2822 } 2823 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d " 2824 "seconds", ret, timeout); 2825 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx); 2826 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout, 2827 drv, drv->ctx); 2828 2829 nla_put_failure: 2830 nlmsg_free(ssids); 2831 nlmsg_free(msg); 2832 nlmsg_free(freqs); 2833 nlmsg_free(rates); 2834 return ret; 2835 } 2836 2837 2838 /** 2839 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan 2840 * @priv: Pointer to private driver data from wpa_driver_nl80211_init() 2841 * @params: Scan parameters 2842 * @interval: Interval between scan cycles in milliseconds 2843 * Returns: 0 on success, -1 on failure or if not supported 2844 */ 2845 static int wpa_driver_nl80211_sched_scan(void *priv, 2846 struct wpa_driver_scan_params *params, 2847 u32 interval) 2848 { 2849 struct i802_bss *bss = priv; 2850 struct wpa_driver_nl80211_data *drv = bss->drv; 2851 int ret = 0; 2852 struct nl_msg *msg, *ssids, *freqs, *match_set_ssid, *match_sets; 2853 size_t i; 2854 2855 msg = nlmsg_alloc(); 2856 ssids = nlmsg_alloc(); 2857 freqs = nlmsg_alloc(); 2858 if (!msg || !ssids || !freqs) { 2859 nlmsg_free(msg); 2860 nlmsg_free(ssids); 2861 nlmsg_free(freqs); 2862 return -1; 2863 } 2864 2865 os_free(drv->filter_ssids); 2866 drv->filter_ssids = params->filter_ssids; 2867 params->filter_ssids = NULL; 2868 drv->num_filter_ssids = params->num_filter_ssids; 2869 2870 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_SCHED_SCAN); 2871 2872 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 2873 2874 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval); 2875 2876 if (drv->num_filter_ssids) { 2877 match_sets = nlmsg_alloc(); 2878 2879 for (i = 0; i < drv->num_filter_ssids; i++) { 2880 wpa_hexdump_ascii(MSG_MSGDUMP, 2881 "nl80211: Sched scan filter SSID", 2882 drv->filter_ssids[i].ssid, 2883 drv->filter_ssids[i].ssid_len); 2884 2885 match_set_ssid = nlmsg_alloc(); 2886 nla_put(match_set_ssid, 2887 NL80211_ATTR_SCHED_SCAN_MATCH_SSID, 2888 drv->filter_ssids[i].ssid_len, 2889 drv->filter_ssids[i].ssid); 2890 2891 nla_put_nested(match_sets, i + 1, match_set_ssid); 2892 2893 nlmsg_free(match_set_ssid); 2894 } 2895 2896 nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH, 2897 match_sets); 2898 nlmsg_free(match_sets); 2899 } 2900 2901 for (i = 0; i < params->num_ssids; i++) { 2902 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan SSID", 2903 params->ssids[i].ssid, 2904 params->ssids[i].ssid_len); 2905 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len, 2906 params->ssids[i].ssid); 2907 } 2908 if (params->num_ssids) 2909 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); 2910 2911 if (params->extra_ies) { 2912 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan extra IEs", 2913 params->extra_ies, params->extra_ies_len); 2914 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len, 2915 params->extra_ies); 2916 } 2917 2918 if (params->freqs) { 2919 for (i = 0; params->freqs[i]; i++) { 2920 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u " 2921 "MHz", params->freqs[i]); 2922 NLA_PUT_U32(freqs, i + 1, params->freqs[i]); 2923 } 2924 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs); 2925 } 2926 2927 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 2928 2929 /* TODO: if we get an error here, we should fall back to normal scan */ 2930 2931 msg = NULL; 2932 if (ret) { 2933 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: " 2934 "ret=%d (%s)", ret, strerror(-ret)); 2935 goto nla_put_failure; 2936 } 2937 2938 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - " 2939 "scan interval %d msec", ret, interval); 2940 2941 nla_put_failure: 2942 nlmsg_free(ssids); 2943 nlmsg_free(msg); 2944 nlmsg_free(freqs); 2945 return ret; 2946 } 2947 2948 2949 /** 2950 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan 2951 * @priv: Pointer to private driver data from wpa_driver_nl80211_init() 2952 * Returns: 0 on success, -1 on failure or if not supported 2953 */ 2954 static int wpa_driver_nl80211_stop_sched_scan(void *priv) 2955 { 2956 struct i802_bss *bss = priv; 2957 struct wpa_driver_nl80211_data *drv = bss->drv; 2958 int ret = 0; 2959 struct nl_msg *msg; 2960 2961 msg = nlmsg_alloc(); 2962 if (!msg) 2963 return -1; 2964 2965 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN); 2966 2967 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 2968 2969 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 2970 msg = NULL; 2971 if (ret) { 2972 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: " 2973 "ret=%d (%s)", ret, strerror(-ret)); 2974 goto nla_put_failure; 2975 } 2976 2977 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret); 2978 2979 nla_put_failure: 2980 nlmsg_free(msg); 2981 return ret; 2982 } 2983 2984 2985 static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie) 2986 { 2987 const u8 *end, *pos; 2988 2989 if (ies == NULL) 2990 return NULL; 2991 2992 pos = ies; 2993 end = ies + ies_len; 2994 2995 while (pos + 1 < end) { 2996 if (pos + 2 + pos[1] > end) 2997 break; 2998 if (pos[0] == ie) 2999 return pos; 3000 pos += 2 + pos[1]; 3001 } 3002 3003 return NULL; 3004 } 3005 3006 3007 static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv, 3008 const u8 *ie, size_t ie_len) 3009 { 3010 const u8 *ssid; 3011 size_t i; 3012 3013 if (drv->filter_ssids == NULL) 3014 return 0; 3015 3016 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID); 3017 if (ssid == NULL) 3018 return 1; 3019 3020 for (i = 0; i < drv->num_filter_ssids; i++) { 3021 if (ssid[1] == drv->filter_ssids[i].ssid_len && 3022 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) == 3023 0) 3024 return 0; 3025 } 3026 3027 return 1; 3028 } 3029 3030 3031 static int bss_info_handler(struct nl_msg *msg, void *arg) 3032 { 3033 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 3034 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 3035 struct nlattr *bss[NL80211_BSS_MAX + 1]; 3036 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { 3037 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC }, 3038 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, 3039 [NL80211_BSS_TSF] = { .type = NLA_U64 }, 3040 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, 3041 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, 3042 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC }, 3043 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, 3044 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, 3045 [NL80211_BSS_STATUS] = { .type = NLA_U32 }, 3046 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, 3047 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC }, 3048 }; 3049 struct nl80211_bss_info_arg *_arg = arg; 3050 struct wpa_scan_results *res = _arg->res; 3051 struct wpa_scan_res **tmp; 3052 struct wpa_scan_res *r; 3053 const u8 *ie, *beacon_ie; 3054 size_t ie_len, beacon_ie_len; 3055 u8 *pos; 3056 size_t i; 3057 3058 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 3059 genlmsg_attrlen(gnlh, 0), NULL); 3060 if (!tb[NL80211_ATTR_BSS]) 3061 return NL_SKIP; 3062 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], 3063 bss_policy)) 3064 return NL_SKIP; 3065 if (bss[NL80211_BSS_STATUS]) { 3066 enum nl80211_bss_status status; 3067 status = nla_get_u32(bss[NL80211_BSS_STATUS]); 3068 if (status == NL80211_BSS_STATUS_ASSOCIATED && 3069 bss[NL80211_BSS_FREQUENCY]) { 3070 _arg->assoc_freq = 3071 nla_get_u32(bss[NL80211_BSS_FREQUENCY]); 3072 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz", 3073 _arg->assoc_freq); 3074 } 3075 if (status == NL80211_BSS_STATUS_ASSOCIATED && 3076 bss[NL80211_BSS_BSSID]) { 3077 os_memcpy(_arg->assoc_bssid, 3078 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN); 3079 wpa_printf(MSG_DEBUG, "nl80211: Associated with " 3080 MACSTR, MAC2STR(_arg->assoc_bssid)); 3081 } 3082 } 3083 if (!res) 3084 return NL_SKIP; 3085 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) { 3086 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 3087 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 3088 } else { 3089 ie = NULL; 3090 ie_len = 0; 3091 } 3092 if (bss[NL80211_BSS_BEACON_IES]) { 3093 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]); 3094 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]); 3095 } else { 3096 beacon_ie = NULL; 3097 beacon_ie_len = 0; 3098 } 3099 3100 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie, 3101 ie ? ie_len : beacon_ie_len)) 3102 return NL_SKIP; 3103 3104 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len); 3105 if (r == NULL) 3106 return NL_SKIP; 3107 if (bss[NL80211_BSS_BSSID]) 3108 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]), 3109 ETH_ALEN); 3110 if (bss[NL80211_BSS_FREQUENCY]) 3111 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]); 3112 if (bss[NL80211_BSS_BEACON_INTERVAL]) 3113 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]); 3114 if (bss[NL80211_BSS_CAPABILITY]) 3115 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); 3116 r->flags |= WPA_SCAN_NOISE_INVALID; 3117 if (bss[NL80211_BSS_SIGNAL_MBM]) { 3118 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); 3119 r->level /= 100; /* mBm to dBm */ 3120 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID; 3121 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { 3122 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); 3123 r->flags |= WPA_SCAN_QUAL_INVALID; 3124 } else 3125 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID; 3126 if (bss[NL80211_BSS_TSF]) 3127 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]); 3128 if (bss[NL80211_BSS_SEEN_MS_AGO]) 3129 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]); 3130 r->ie_len = ie_len; 3131 pos = (u8 *) (r + 1); 3132 if (ie) { 3133 os_memcpy(pos, ie, ie_len); 3134 pos += ie_len; 3135 } 3136 r->beacon_ie_len = beacon_ie_len; 3137 if (beacon_ie) 3138 os_memcpy(pos, beacon_ie, beacon_ie_len); 3139 3140 if (bss[NL80211_BSS_STATUS]) { 3141 enum nl80211_bss_status status; 3142 status = nla_get_u32(bss[NL80211_BSS_STATUS]); 3143 switch (status) { 3144 case NL80211_BSS_STATUS_AUTHENTICATED: 3145 r->flags |= WPA_SCAN_AUTHENTICATED; 3146 break; 3147 case NL80211_BSS_STATUS_ASSOCIATED: 3148 r->flags |= WPA_SCAN_ASSOCIATED; 3149 break; 3150 default: 3151 break; 3152 } 3153 } 3154 3155 /* 3156 * cfg80211 maintains separate BSS table entries for APs if the same 3157 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does 3158 * not use frequency as a separate key in the BSS table, so filter out 3159 * duplicated entries. Prefer associated BSS entry in such a case in 3160 * order to get the correct frequency into the BSS table. 3161 */ 3162 for (i = 0; i < res->num; i++) { 3163 const u8 *s1, *s2; 3164 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0) 3165 continue; 3166 3167 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1), 3168 res->res[i]->ie_len, WLAN_EID_SSID); 3169 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID); 3170 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] || 3171 os_memcmp(s1, s2, 2 + s1[1]) != 0) 3172 continue; 3173 3174 /* Same BSSID,SSID was already included in scan results */ 3175 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result " 3176 "for " MACSTR, MAC2STR(r->bssid)); 3177 3178 if ((r->flags & WPA_SCAN_ASSOCIATED) && 3179 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) { 3180 os_free(res->res[i]); 3181 res->res[i] = r; 3182 } else 3183 os_free(r); 3184 return NL_SKIP; 3185 } 3186 3187 tmp = os_realloc(res->res, 3188 (res->num + 1) * sizeof(struct wpa_scan_res *)); 3189 if (tmp == NULL) { 3190 os_free(r); 3191 return NL_SKIP; 3192 } 3193 tmp[res->num++] = r; 3194 res->res = tmp; 3195 3196 return NL_SKIP; 3197 } 3198 3199 3200 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv, 3201 const u8 *addr) 3202 { 3203 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { 3204 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state " 3205 "mismatch (" MACSTR ")", MAC2STR(addr)); 3206 wpa_driver_nl80211_mlme(drv, addr, 3207 NL80211_CMD_DEAUTHENTICATE, 3208 WLAN_REASON_PREV_AUTH_NOT_VALID, 1); 3209 } 3210 } 3211 3212 3213 static void wpa_driver_nl80211_check_bss_status( 3214 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res) 3215 { 3216 size_t i; 3217 3218 for (i = 0; i < res->num; i++) { 3219 struct wpa_scan_res *r = res->res[i]; 3220 if (r->flags & WPA_SCAN_AUTHENTICATED) { 3221 wpa_printf(MSG_DEBUG, "nl80211: Scan results " 3222 "indicates BSS status with " MACSTR 3223 " as authenticated", 3224 MAC2STR(r->bssid)); 3225 if (is_sta_interface(drv->nlmode) && 3226 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 && 3227 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) != 3228 0) { 3229 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID" 3230 " in local state (auth=" MACSTR 3231 " assoc=" MACSTR ")", 3232 MAC2STR(drv->auth_bssid), 3233 MAC2STR(drv->bssid)); 3234 clear_state_mismatch(drv, r->bssid); 3235 } 3236 } 3237 3238 if (r->flags & WPA_SCAN_ASSOCIATED) { 3239 wpa_printf(MSG_DEBUG, "nl80211: Scan results " 3240 "indicate BSS status with " MACSTR 3241 " as associated", 3242 MAC2STR(r->bssid)); 3243 if (is_sta_interface(drv->nlmode) && 3244 !drv->associated) { 3245 wpa_printf(MSG_DEBUG, "nl80211: Local state " 3246 "(not associated) does not match " 3247 "with BSS state"); 3248 clear_state_mismatch(drv, r->bssid); 3249 } else if (is_sta_interface(drv->nlmode) && 3250 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) != 3251 0) { 3252 wpa_printf(MSG_DEBUG, "nl80211: Local state " 3253 "(associated with " MACSTR ") does " 3254 "not match with BSS state", 3255 MAC2STR(drv->bssid)); 3256 clear_state_mismatch(drv, r->bssid); 3257 clear_state_mismatch(drv, drv->bssid); 3258 } 3259 } 3260 } 3261 } 3262 3263 3264 static struct wpa_scan_results * 3265 nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv) 3266 { 3267 struct nl_msg *msg; 3268 struct wpa_scan_results *res; 3269 int ret; 3270 struct nl80211_bss_info_arg arg; 3271 3272 res = os_zalloc(sizeof(*res)); 3273 if (res == NULL) 3274 return NULL; 3275 msg = nlmsg_alloc(); 3276 if (!msg) 3277 goto nla_put_failure; 3278 3279 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN); 3280 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 3281 3282 arg.drv = drv; 3283 arg.res = res; 3284 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg); 3285 msg = NULL; 3286 if (ret == 0) { 3287 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu " 3288 "BSSes)", (unsigned long) res->num); 3289 nl80211_get_noise_for_scan_results(drv, res); 3290 return res; 3291 } 3292 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d " 3293 "(%s)", ret, strerror(-ret)); 3294 nla_put_failure: 3295 nlmsg_free(msg); 3296 wpa_scan_results_free(res); 3297 return NULL; 3298 } 3299 3300 3301 /** 3302 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results 3303 * @priv: Pointer to private wext data from wpa_driver_nl80211_init() 3304 * Returns: Scan results on success, -1 on failure 3305 */ 3306 static struct wpa_scan_results * 3307 wpa_driver_nl80211_get_scan_results(void *priv) 3308 { 3309 struct i802_bss *bss = priv; 3310 struct wpa_driver_nl80211_data *drv = bss->drv; 3311 struct wpa_scan_results *res; 3312 3313 res = nl80211_get_scan_results(drv); 3314 if (res) 3315 wpa_driver_nl80211_check_bss_status(drv, res); 3316 return res; 3317 } 3318 3319 3320 static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv) 3321 { 3322 struct wpa_scan_results *res; 3323 size_t i; 3324 3325 res = nl80211_get_scan_results(drv); 3326 if (res == NULL) { 3327 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results"); 3328 return; 3329 } 3330 3331 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump"); 3332 for (i = 0; i < res->num; i++) { 3333 struct wpa_scan_res *r = res->res[i]; 3334 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s", 3335 (int) i, (int) res->num, MAC2STR(r->bssid), 3336 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "", 3337 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : ""); 3338 } 3339 3340 wpa_scan_results_free(res); 3341 } 3342 3343 3344 static int wpa_driver_nl80211_set_key(const char *ifname, void *priv, 3345 enum wpa_alg alg, const u8 *addr, 3346 int key_idx, int set_tx, 3347 const u8 *seq, size_t seq_len, 3348 const u8 *key, size_t key_len) 3349 { 3350 struct i802_bss *bss = priv; 3351 struct wpa_driver_nl80211_data *drv = bss->drv; 3352 int ifindex = if_nametoindex(ifname); 3353 struct nl_msg *msg; 3354 int ret; 3355 3356 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d " 3357 "set_tx=%d seq_len=%lu key_len=%lu", 3358 __func__, ifindex, alg, addr, key_idx, set_tx, 3359 (unsigned long) seq_len, (unsigned long) key_len); 3360 #ifdef CONFIG_TDLS 3361 if (key_idx == -1) 3362 key_idx = 0; 3363 #endif /* CONFIG_TDLS */ 3364 3365 msg = nlmsg_alloc(); 3366 if (!msg) 3367 return -ENOMEM; 3368 3369 if (alg == WPA_ALG_NONE) { 3370 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY); 3371 } else { 3372 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY); 3373 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key); 3374 switch (alg) { 3375 case WPA_ALG_WEP: 3376 if (key_len == 5) 3377 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 3378 WLAN_CIPHER_SUITE_WEP40); 3379 else 3380 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 3381 WLAN_CIPHER_SUITE_WEP104); 3382 break; 3383 case WPA_ALG_TKIP: 3384 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 3385 WLAN_CIPHER_SUITE_TKIP); 3386 break; 3387 case WPA_ALG_CCMP: 3388 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 3389 WLAN_CIPHER_SUITE_CCMP); 3390 break; 3391 case WPA_ALG_IGTK: 3392 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 3393 WLAN_CIPHER_SUITE_AES_CMAC); 3394 break; 3395 default: 3396 wpa_printf(MSG_ERROR, "%s: Unsupported encryption " 3397 "algorithm %d", __func__, alg); 3398 nlmsg_free(msg); 3399 return -1; 3400 } 3401 } 3402 3403 if (seq && seq_len) 3404 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq); 3405 3406 if (addr && !is_broadcast_ether_addr(addr)) { 3407 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr)); 3408 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 3409 3410 if (alg != WPA_ALG_WEP && key_idx && !set_tx) { 3411 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK"); 3412 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, 3413 NL80211_KEYTYPE_GROUP); 3414 } 3415 } else if (addr && is_broadcast_ether_addr(addr)) { 3416 struct nl_msg *types; 3417 int err; 3418 wpa_printf(MSG_DEBUG, " broadcast key"); 3419 types = nlmsg_alloc(); 3420 if (!types) 3421 goto nla_put_failure; 3422 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST); 3423 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, 3424 types); 3425 nlmsg_free(types); 3426 if (err) 3427 goto nla_put_failure; 3428 } 3429 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); 3430 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 3431 3432 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 3433 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE) 3434 ret = 0; 3435 if (ret) 3436 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)", 3437 ret, strerror(-ret)); 3438 3439 /* 3440 * If we failed or don't need to set the default TX key (below), 3441 * we're done here. 3442 */ 3443 if (ret || !set_tx || alg == WPA_ALG_NONE) 3444 return ret; 3445 if (is_ap_interface(drv->nlmode) && addr && 3446 !is_broadcast_ether_addr(addr)) 3447 return ret; 3448 3449 msg = nlmsg_alloc(); 3450 if (!msg) 3451 return -ENOMEM; 3452 3453 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY); 3454 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); 3455 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 3456 if (alg == WPA_ALG_IGTK) 3457 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT); 3458 else 3459 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT); 3460 if (addr && is_broadcast_ether_addr(addr)) { 3461 struct nl_msg *types; 3462 int err; 3463 types = nlmsg_alloc(); 3464 if (!types) 3465 goto nla_put_failure; 3466 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST); 3467 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, 3468 types); 3469 nlmsg_free(types); 3470 if (err) 3471 goto nla_put_failure; 3472 } else if (addr) { 3473 struct nl_msg *types; 3474 int err; 3475 types = nlmsg_alloc(); 3476 if (!types) 3477 goto nla_put_failure; 3478 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST); 3479 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, 3480 types); 3481 nlmsg_free(types); 3482 if (err) 3483 goto nla_put_failure; 3484 } 3485 3486 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 3487 if (ret == -ENOENT) 3488 ret = 0; 3489 if (ret) 3490 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; " 3491 "err=%d %s)", ret, strerror(-ret)); 3492 return ret; 3493 3494 nla_put_failure: 3495 nlmsg_free(msg); 3496 return -ENOBUFS; 3497 } 3498 3499 3500 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg, 3501 int key_idx, int defkey, 3502 const u8 *seq, size_t seq_len, 3503 const u8 *key, size_t key_len) 3504 { 3505 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY); 3506 if (!key_attr) 3507 return -1; 3508 3509 if (defkey && alg == WPA_ALG_IGTK) 3510 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT); 3511 else if (defkey) 3512 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); 3513 3514 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx); 3515 3516 switch (alg) { 3517 case WPA_ALG_WEP: 3518 if (key_len == 5) 3519 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 3520 WLAN_CIPHER_SUITE_WEP40); 3521 else 3522 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 3523 WLAN_CIPHER_SUITE_WEP104); 3524 break; 3525 case WPA_ALG_TKIP: 3526 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP); 3527 break; 3528 case WPA_ALG_CCMP: 3529 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP); 3530 break; 3531 case WPA_ALG_IGTK: 3532 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 3533 WLAN_CIPHER_SUITE_AES_CMAC); 3534 break; 3535 default: 3536 wpa_printf(MSG_ERROR, "%s: Unsupported encryption " 3537 "algorithm %d", __func__, alg); 3538 return -1; 3539 } 3540 3541 if (seq && seq_len) 3542 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq); 3543 3544 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key); 3545 3546 nla_nest_end(msg, key_attr); 3547 3548 return 0; 3549 nla_put_failure: 3550 return -1; 3551 } 3552 3553 3554 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params, 3555 struct nl_msg *msg) 3556 { 3557 int i, privacy = 0; 3558 struct nlattr *nl_keys, *nl_key; 3559 3560 for (i = 0; i < 4; i++) { 3561 if (!params->wep_key[i]) 3562 continue; 3563 privacy = 1; 3564 break; 3565 } 3566 if (params->wps == WPS_MODE_PRIVACY) 3567 privacy = 1; 3568 if (params->pairwise_suite && 3569 params->pairwise_suite != WPA_CIPHER_NONE) 3570 privacy = 1; 3571 3572 if (!privacy) 3573 return 0; 3574 3575 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY); 3576 3577 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS); 3578 if (!nl_keys) 3579 goto nla_put_failure; 3580 3581 for (i = 0; i < 4; i++) { 3582 if (!params->wep_key[i]) 3583 continue; 3584 3585 nl_key = nla_nest_start(msg, i); 3586 if (!nl_key) 3587 goto nla_put_failure; 3588 3589 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i], 3590 params->wep_key[i]); 3591 if (params->wep_key_len[i] == 5) 3592 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 3593 WLAN_CIPHER_SUITE_WEP40); 3594 else 3595 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 3596 WLAN_CIPHER_SUITE_WEP104); 3597 3598 NLA_PUT_U8(msg, NL80211_KEY_IDX, i); 3599 3600 if (i == params->wep_tx_keyidx) 3601 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); 3602 3603 nla_nest_end(msg, nl_key); 3604 } 3605 nla_nest_end(msg, nl_keys); 3606 3607 return 0; 3608 3609 nla_put_failure: 3610 return -ENOBUFS; 3611 } 3612 3613 3614 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv, 3615 const u8 *addr, int cmd, u16 reason_code, 3616 int local_state_change) 3617 { 3618 int ret = -1; 3619 struct nl_msg *msg; 3620 3621 msg = nlmsg_alloc(); 3622 if (!msg) 3623 return -1; 3624 3625 nl80211_cmd(drv, msg, 0, cmd); 3626 3627 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 3628 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code); 3629 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 3630 if (local_state_change) 3631 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE); 3632 3633 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 3634 msg = NULL; 3635 if (ret) { 3636 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d " 3637 "(%s)", ret, strerror(-ret)); 3638 goto nla_put_failure; 3639 } 3640 ret = 0; 3641 3642 nla_put_failure: 3643 nlmsg_free(msg); 3644 return ret; 3645 } 3646 3647 3648 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv, 3649 const u8 *addr, int reason_code) 3650 { 3651 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)", 3652 __func__, MAC2STR(addr), reason_code); 3653 drv->associated = 0; 3654 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT, 3655 reason_code, 0); 3656 } 3657 3658 3659 static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr, 3660 int reason_code) 3661 { 3662 struct i802_bss *bss = priv; 3663 struct wpa_driver_nl80211_data *drv = bss->drv; 3664 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) 3665 return wpa_driver_nl80211_disconnect(drv, addr, reason_code); 3666 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)", 3667 __func__, MAC2STR(addr), reason_code); 3668 drv->associated = 0; 3669 if (drv->nlmode == NL80211_IFTYPE_ADHOC) 3670 return nl80211_leave_ibss(drv); 3671 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE, 3672 reason_code, 0); 3673 } 3674 3675 3676 static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr, 3677 int reason_code) 3678 { 3679 struct i802_bss *bss = priv; 3680 struct wpa_driver_nl80211_data *drv = bss->drv; 3681 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) 3682 return wpa_driver_nl80211_disconnect(drv, addr, reason_code); 3683 wpa_printf(MSG_DEBUG, "%s", __func__); 3684 drv->associated = 0; 3685 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE, 3686 reason_code, 0); 3687 } 3688 3689 3690 static int wpa_driver_nl80211_authenticate( 3691 void *priv, struct wpa_driver_auth_params *params) 3692 { 3693 struct i802_bss *bss = priv; 3694 struct wpa_driver_nl80211_data *drv = bss->drv; 3695 int ret = -1, i; 3696 struct nl_msg *msg; 3697 enum nl80211_auth_type type; 3698 enum nl80211_iftype nlmode; 3699 int count = 0; 3700 3701 drv->associated = 0; 3702 os_memset(drv->auth_bssid, 0, ETH_ALEN); 3703 /* FIX: IBSS mode */ 3704 nlmode = params->p2p ? 3705 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION; 3706 if (drv->nlmode != nlmode && 3707 wpa_driver_nl80211_set_mode(priv, nlmode) < 0) 3708 return -1; 3709 3710 retry: 3711 msg = nlmsg_alloc(); 3712 if (!msg) 3713 return -1; 3714 3715 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)", 3716 drv->ifindex); 3717 3718 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE); 3719 3720 for (i = 0; i < 4; i++) { 3721 if (!params->wep_key[i]) 3722 continue; 3723 wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP, 3724 NULL, i, 3725 i == params->wep_tx_keyidx, NULL, 0, 3726 params->wep_key[i], 3727 params->wep_key_len[i]); 3728 if (params->wep_tx_keyidx != i) 3729 continue; 3730 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0, 3731 params->wep_key[i], params->wep_key_len[i])) { 3732 nlmsg_free(msg); 3733 return -1; 3734 } 3735 } 3736 3737 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 3738 if (params->bssid) { 3739 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, 3740 MAC2STR(params->bssid)); 3741 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); 3742 } 3743 if (params->freq) { 3744 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); 3745 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); 3746 } 3747 if (params->ssid) { 3748 wpa_hexdump_ascii(MSG_DEBUG, " * SSID", 3749 params->ssid, params->ssid_len); 3750 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 3751 params->ssid); 3752 } 3753 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len); 3754 if (params->ie) 3755 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie); 3756 if (params->auth_alg & WPA_AUTH_ALG_OPEN) 3757 type = NL80211_AUTHTYPE_OPEN_SYSTEM; 3758 else if (params->auth_alg & WPA_AUTH_ALG_SHARED) 3759 type = NL80211_AUTHTYPE_SHARED_KEY; 3760 else if (params->auth_alg & WPA_AUTH_ALG_LEAP) 3761 type = NL80211_AUTHTYPE_NETWORK_EAP; 3762 else if (params->auth_alg & WPA_AUTH_ALG_FT) 3763 type = NL80211_AUTHTYPE_FT; 3764 else 3765 goto nla_put_failure; 3766 wpa_printf(MSG_DEBUG, " * Auth Type %d", type); 3767 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type); 3768 if (params->local_state_change) { 3769 wpa_printf(MSG_DEBUG, " * Local state change only"); 3770 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE); 3771 } 3772 3773 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 3774 msg = NULL; 3775 if (ret) { 3776 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d " 3777 "(%s)", ret, strerror(-ret)); 3778 count++; 3779 if (ret == -EALREADY && count == 1 && params->bssid && 3780 !params->local_state_change) { 3781 /* 3782 * mac80211 does not currently accept new 3783 * authentication if we are already authenticated. As a 3784 * workaround, force deauthentication and try again. 3785 */ 3786 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication " 3787 "after forced deauthentication"); 3788 wpa_driver_nl80211_deauthenticate( 3789 bss, params->bssid, 3790 WLAN_REASON_PREV_AUTH_NOT_VALID); 3791 nlmsg_free(msg); 3792 goto retry; 3793 } 3794 goto nla_put_failure; 3795 } 3796 ret = 0; 3797 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send " 3798 "successfully"); 3799 3800 nla_put_failure: 3801 nlmsg_free(msg); 3802 return ret; 3803 } 3804 3805 3806 struct phy_info_arg { 3807 u16 *num_modes; 3808 struct hostapd_hw_modes *modes; 3809 }; 3810 3811 static int phy_info_handler(struct nl_msg *msg, void *arg) 3812 { 3813 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; 3814 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 3815 struct phy_info_arg *phy_info = arg; 3816 3817 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1]; 3818 3819 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1]; 3820 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { 3821 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, 3822 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, 3823 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG }, 3824 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG }, 3825 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG }, 3826 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 }, 3827 }; 3828 3829 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1]; 3830 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = { 3831 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 }, 3832 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG }, 3833 }; 3834 3835 struct nlattr *nl_band; 3836 struct nlattr *nl_freq; 3837 struct nlattr *nl_rate; 3838 int rem_band, rem_freq, rem_rate; 3839 struct hostapd_hw_modes *mode; 3840 int idx, mode_is_set; 3841 3842 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 3843 genlmsg_attrlen(gnlh, 0), NULL); 3844 3845 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS]) 3846 return NL_SKIP; 3847 3848 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) { 3849 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode)); 3850 if (!mode) 3851 return NL_SKIP; 3852 phy_info->modes = mode; 3853 3854 mode_is_set = 0; 3855 3856 mode = &phy_info->modes[*(phy_info->num_modes)]; 3857 memset(mode, 0, sizeof(*mode)); 3858 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN; 3859 *(phy_info->num_modes) += 1; 3860 3861 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band), 3862 nla_len(nl_band), NULL); 3863 3864 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) { 3865 mode->ht_capab = nla_get_u16( 3866 tb_band[NL80211_BAND_ATTR_HT_CAPA]); 3867 } 3868 3869 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) { 3870 mode->a_mpdu_params |= nla_get_u8( 3871 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) & 3872 0x03; 3873 } 3874 3875 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) { 3876 mode->a_mpdu_params |= nla_get_u8( 3877 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) << 3878 2; 3879 } 3880 3881 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] && 3882 nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) { 3883 u8 *mcs; 3884 mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]); 3885 os_memcpy(mode->mcs_set, mcs, 16); 3886 } 3887 3888 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) { 3889 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq), 3890 nla_len(nl_freq), freq_policy); 3891 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) 3892 continue; 3893 mode->num_channels++; 3894 } 3895 3896 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data)); 3897 if (!mode->channels) 3898 return NL_SKIP; 3899 3900 idx = 0; 3901 3902 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) { 3903 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq), 3904 nla_len(nl_freq), freq_policy); 3905 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) 3906 continue; 3907 3908 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]); 3909 mode->channels[idx].flag = 0; 3910 3911 if (!mode_is_set) { 3912 /* crude heuristic */ 3913 if (mode->channels[idx].freq < 4000) 3914 mode->mode = HOSTAPD_MODE_IEEE80211B; 3915 else 3916 mode->mode = HOSTAPD_MODE_IEEE80211A; 3917 mode_is_set = 1; 3918 } 3919 3920 /* crude heuristic */ 3921 if (mode->channels[idx].freq < 4000) 3922 if (mode->channels[idx].freq == 2484) 3923 mode->channels[idx].chan = 14; 3924 else 3925 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5; 3926 else 3927 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000; 3928 3929 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) 3930 mode->channels[idx].flag |= 3931 HOSTAPD_CHAN_DISABLED; 3932 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]) 3933 mode->channels[idx].flag |= 3934 HOSTAPD_CHAN_PASSIVE_SCAN; 3935 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS]) 3936 mode->channels[idx].flag |= 3937 HOSTAPD_CHAN_NO_IBSS; 3938 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR]) 3939 mode->channels[idx].flag |= 3940 HOSTAPD_CHAN_RADAR; 3941 3942 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] && 3943 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) 3944 mode->channels[idx].max_tx_power = 3945 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100; 3946 3947 idx++; 3948 } 3949 3950 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) { 3951 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate), 3952 nla_len(nl_rate), rate_policy); 3953 if (!tb_rate[NL80211_BITRATE_ATTR_RATE]) 3954 continue; 3955 mode->num_rates++; 3956 } 3957 3958 mode->rates = os_zalloc(mode->num_rates * sizeof(int)); 3959 if (!mode->rates) 3960 return NL_SKIP; 3961 3962 idx = 0; 3963 3964 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) { 3965 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate), 3966 nla_len(nl_rate), rate_policy); 3967 if (!tb_rate[NL80211_BITRATE_ATTR_RATE]) 3968 continue; 3969 mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]); 3970 3971 /* crude heuristic */ 3972 if (mode->mode == HOSTAPD_MODE_IEEE80211B && 3973 mode->rates[idx] > 200) 3974 mode->mode = HOSTAPD_MODE_IEEE80211G; 3975 3976 idx++; 3977 } 3978 } 3979 3980 return NL_SKIP; 3981 } 3982 3983 static struct hostapd_hw_modes * 3984 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes) 3985 { 3986 u16 m; 3987 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode; 3988 int i, mode11g_idx = -1; 3989 3990 /* If only 802.11g mode is included, use it to construct matching 3991 * 802.11b mode data. */ 3992 3993 for (m = 0; m < *num_modes; m++) { 3994 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B) 3995 return modes; /* 802.11b already included */ 3996 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G) 3997 mode11g_idx = m; 3998 } 3999 4000 if (mode11g_idx < 0) 4001 return modes; /* 2.4 GHz band not supported at all */ 4002 4003 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes)); 4004 if (nmodes == NULL) 4005 return modes; /* Could not add 802.11b mode */ 4006 4007 mode = &nmodes[*num_modes]; 4008 os_memset(mode, 0, sizeof(*mode)); 4009 (*num_modes)++; 4010 modes = nmodes; 4011 4012 mode->mode = HOSTAPD_MODE_IEEE80211B; 4013 4014 mode11g = &modes[mode11g_idx]; 4015 mode->num_channels = mode11g->num_channels; 4016 mode->channels = os_malloc(mode11g->num_channels * 4017 sizeof(struct hostapd_channel_data)); 4018 if (mode->channels == NULL) { 4019 (*num_modes)--; 4020 return modes; /* Could not add 802.11b mode */ 4021 } 4022 os_memcpy(mode->channels, mode11g->channels, 4023 mode11g->num_channels * sizeof(struct hostapd_channel_data)); 4024 4025 mode->num_rates = 0; 4026 mode->rates = os_malloc(4 * sizeof(int)); 4027 if (mode->rates == NULL) { 4028 os_free(mode->channels); 4029 (*num_modes)--; 4030 return modes; /* Could not add 802.11b mode */ 4031 } 4032 4033 for (i = 0; i < mode11g->num_rates; i++) { 4034 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 && 4035 mode11g->rates[i] != 55 && mode11g->rates[i] != 110) 4036 continue; 4037 mode->rates[mode->num_rates] = mode11g->rates[i]; 4038 mode->num_rates++; 4039 if (mode->num_rates == 4) 4040 break; 4041 } 4042 4043 if (mode->num_rates == 0) { 4044 os_free(mode->channels); 4045 os_free(mode->rates); 4046 (*num_modes)--; 4047 return modes; /* No 802.11b rates */ 4048 } 4049 4050 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g " 4051 "information"); 4052 4053 return modes; 4054 } 4055 4056 4057 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start, 4058 int end) 4059 { 4060 int c; 4061 4062 for (c = 0; c < mode->num_channels; c++) { 4063 struct hostapd_channel_data *chan = &mode->channels[c]; 4064 if (chan->freq - 10 >= start && chan->freq + 10 <= end) 4065 chan->flag |= HOSTAPD_CHAN_HT40; 4066 } 4067 } 4068 4069 4070 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start, 4071 int end) 4072 { 4073 int c; 4074 4075 for (c = 0; c < mode->num_channels; c++) { 4076 struct hostapd_channel_data *chan = &mode->channels[c]; 4077 if (!(chan->flag & HOSTAPD_CHAN_HT40)) 4078 continue; 4079 if (chan->freq - 30 >= start && chan->freq - 10 <= end) 4080 chan->flag |= HOSTAPD_CHAN_HT40MINUS; 4081 if (chan->freq + 10 >= start && chan->freq + 30 <= end) 4082 chan->flag |= HOSTAPD_CHAN_HT40PLUS; 4083 } 4084 } 4085 4086 4087 static void nl80211_reg_rule_ht40(struct nlattr *tb[], 4088 struct phy_info_arg *results) 4089 { 4090 u32 start, end, max_bw; 4091 u16 m; 4092 4093 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL || 4094 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL || 4095 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL) 4096 return; 4097 4098 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000; 4099 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000; 4100 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000; 4101 4102 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz", 4103 start, end, max_bw); 4104 if (max_bw < 40) 4105 return; 4106 4107 for (m = 0; m < *results->num_modes; m++) { 4108 if (!(results->modes[m].ht_capab & 4109 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) 4110 continue; 4111 nl80211_set_ht40_mode(&results->modes[m], start, end); 4112 } 4113 } 4114 4115 4116 static void nl80211_reg_rule_sec(struct nlattr *tb[], 4117 struct phy_info_arg *results) 4118 { 4119 u32 start, end, max_bw; 4120 u16 m; 4121 4122 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL || 4123 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL || 4124 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL) 4125 return; 4126 4127 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000; 4128 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000; 4129 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000; 4130 4131 if (max_bw < 20) 4132 return; 4133 4134 for (m = 0; m < *results->num_modes; m++) { 4135 if (!(results->modes[m].ht_capab & 4136 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) 4137 continue; 4138 nl80211_set_ht40_mode_sec(&results->modes[m], start, end); 4139 } 4140 } 4141 4142 4143 static int nl80211_get_reg(struct nl_msg *msg, void *arg) 4144 { 4145 struct phy_info_arg *results = arg; 4146 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; 4147 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 4148 struct nlattr *nl_rule; 4149 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1]; 4150 int rem_rule; 4151 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { 4152 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 }, 4153 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 }, 4154 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 }, 4155 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 }, 4156 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 }, 4157 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 }, 4158 }; 4159 4160 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 4161 genlmsg_attrlen(gnlh, 0), NULL); 4162 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] || 4163 !tb_msg[NL80211_ATTR_REG_RULES]) { 4164 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information " 4165 "available"); 4166 return NL_SKIP; 4167 } 4168 4169 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s", 4170 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2])); 4171 4172 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) 4173 { 4174 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, 4175 nla_data(nl_rule), nla_len(nl_rule), reg_policy); 4176 nl80211_reg_rule_ht40(tb_rule, results); 4177 } 4178 4179 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) 4180 { 4181 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, 4182 nla_data(nl_rule), nla_len(nl_rule), reg_policy); 4183 nl80211_reg_rule_sec(tb_rule, results); 4184 } 4185 4186 return NL_SKIP; 4187 } 4188 4189 4190 static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv, 4191 struct phy_info_arg *results) 4192 { 4193 struct nl_msg *msg; 4194 4195 msg = nlmsg_alloc(); 4196 if (!msg) 4197 return -ENOMEM; 4198 4199 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG); 4200 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results); 4201 } 4202 4203 4204 static struct hostapd_hw_modes * 4205 wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags) 4206 { 4207 struct i802_bss *bss = priv; 4208 struct wpa_driver_nl80211_data *drv = bss->drv; 4209 struct nl_msg *msg; 4210 struct phy_info_arg result = { 4211 .num_modes = num_modes, 4212 .modes = NULL, 4213 }; 4214 4215 *num_modes = 0; 4216 *flags = 0; 4217 4218 msg = nlmsg_alloc(); 4219 if (!msg) 4220 return NULL; 4221 4222 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY); 4223 4224 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 4225 4226 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) { 4227 nl80211_set_ht40_flags(drv, &result); 4228 return wpa_driver_nl80211_add_11b(result.modes, num_modes); 4229 } 4230 msg = NULL; 4231 nla_put_failure: 4232 nlmsg_free(msg); 4233 return NULL; 4234 } 4235 4236 4237 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv, 4238 const void *data, size_t len, 4239 int encrypt) 4240 { 4241 __u8 rtap_hdr[] = { 4242 0x00, 0x00, /* radiotap version */ 4243 0x0e, 0x00, /* radiotap length */ 4244 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */ 4245 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */ 4246 0x00, /* padding */ 4247 0x00, 0x00, /* RX and TX flags to indicate that */ 4248 0x00, 0x00, /* this is the injected frame directly */ 4249 }; 4250 struct iovec iov[2] = { 4251 { 4252 .iov_base = &rtap_hdr, 4253 .iov_len = sizeof(rtap_hdr), 4254 }, 4255 { 4256 .iov_base = (void *) data, 4257 .iov_len = len, 4258 } 4259 }; 4260 struct msghdr msg = { 4261 .msg_name = NULL, 4262 .msg_namelen = 0, 4263 .msg_iov = iov, 4264 .msg_iovlen = 2, 4265 .msg_control = NULL, 4266 .msg_controllen = 0, 4267 .msg_flags = 0, 4268 }; 4269 int res; 4270 4271 if (encrypt) 4272 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP; 4273 4274 if (drv->monitor_sock < 0) { 4275 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available " 4276 "for %s", __func__); 4277 return -1; 4278 } 4279 4280 res = sendmsg(drv->monitor_sock, &msg, 0); 4281 if (res < 0) { 4282 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno)); 4283 return -1; 4284 } 4285 return 0; 4286 } 4287 4288 4289 static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data, 4290 size_t data_len) 4291 { 4292 struct i802_bss *bss = priv; 4293 struct wpa_driver_nl80211_data *drv = bss->drv; 4294 struct ieee80211_mgmt *mgmt; 4295 int encrypt = 1; 4296 u16 fc; 4297 4298 mgmt = (struct ieee80211_mgmt *) data; 4299 fc = le_to_host16(mgmt->frame_control); 4300 4301 if (is_sta_interface(drv->nlmode) && 4302 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && 4303 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) { 4304 /* 4305 * The use of last_mgmt_freq is a bit of a hack, 4306 * but it works due to the single-threaded nature 4307 * of wpa_supplicant. 4308 */ 4309 return nl80211_send_frame_cmd(drv, drv->last_mgmt_freq, 0, 4310 data, data_len, NULL, 1); 4311 } 4312 4313 if (drv->no_monitor_iface_capab && is_ap_interface(drv->nlmode)) { 4314 return nl80211_send_frame_cmd(drv, drv->ap_oper_freq, 0, 4315 data, data_len, 4316 &drv->send_action_cookie, 0); 4317 } 4318 4319 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && 4320 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) { 4321 /* 4322 * Only one of the authentication frame types is encrypted. 4323 * In order for static WEP encryption to work properly (i.e., 4324 * to not encrypt the frame), we need to tell mac80211 about 4325 * the frames that must not be encrypted. 4326 */ 4327 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg); 4328 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction); 4329 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3) 4330 encrypt = 0; 4331 } 4332 4333 return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt); 4334 } 4335 4336 4337 static int nl80211_set_ap_isolate(struct i802_bss *bss, int enabled) 4338 { 4339 struct wpa_driver_nl80211_data *drv = bss->drv; 4340 struct nl_msg *msg; 4341 4342 msg = nlmsg_alloc(); 4343 if (!msg) 4344 return -ENOMEM; 4345 4346 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS); 4347 4348 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 4349 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, enabled); 4350 4351 return send_and_recv_msgs(drv, msg, NULL, NULL); 4352 nla_put_failure: 4353 return -ENOBUFS; 4354 } 4355 4356 4357 static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble, 4358 int slot, int ht_opmode) 4359 { 4360 struct wpa_driver_nl80211_data *drv = bss->drv; 4361 struct nl_msg *msg; 4362 4363 msg = nlmsg_alloc(); 4364 if (!msg) 4365 return -ENOMEM; 4366 4367 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS); 4368 4369 if (cts >= 0) 4370 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts); 4371 if (preamble >= 0) 4372 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble); 4373 if (slot >= 0) 4374 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot); 4375 if (ht_opmode >= 0) 4376 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode); 4377 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 4378 4379 return send_and_recv_msgs(drv, msg, NULL, NULL); 4380 nla_put_failure: 4381 nlmsg_free(msg); 4382 return -ENOBUFS; 4383 } 4384 4385 4386 static int wpa_driver_nl80211_set_ap(void *priv, 4387 struct wpa_driver_ap_params *params) 4388 { 4389 struct i802_bss *bss = priv; 4390 struct wpa_driver_nl80211_data *drv = bss->drv; 4391 struct nl_msg *msg; 4392 u8 cmd = NL80211_CMD_NEW_BEACON; 4393 int ret; 4394 int beacon_set; 4395 int ifindex = if_nametoindex(bss->ifname); 4396 int num_suites; 4397 u32 suites[10]; 4398 u32 ver; 4399 4400 beacon_set = bss->beacon_set; 4401 4402 msg = nlmsg_alloc(); 4403 if (!msg) 4404 return -ENOMEM; 4405 4406 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)", 4407 beacon_set); 4408 if (beacon_set) 4409 cmd = NL80211_CMD_SET_BEACON; 4410 4411 nl80211_cmd(drv, msg, 0, cmd); 4412 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head); 4413 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail); 4414 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 4415 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int); 4416 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period); 4417 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 4418 params->ssid); 4419 switch (params->hide_ssid) { 4420 case NO_SSID_HIDING: 4421 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID, 4422 NL80211_HIDDEN_SSID_NOT_IN_USE); 4423 break; 4424 case HIDDEN_SSID_ZERO_LEN: 4425 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID, 4426 NL80211_HIDDEN_SSID_ZERO_LEN); 4427 break; 4428 case HIDDEN_SSID_ZERO_CONTENTS: 4429 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID, 4430 NL80211_HIDDEN_SSID_ZERO_CONTENTS); 4431 break; 4432 } 4433 if (params->privacy) 4434 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY); 4435 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) == 4436 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) { 4437 /* Leave out the attribute */ 4438 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) 4439 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, 4440 NL80211_AUTHTYPE_SHARED_KEY); 4441 else 4442 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, 4443 NL80211_AUTHTYPE_OPEN_SYSTEM); 4444 4445 ver = 0; 4446 if (params->wpa_version & WPA_PROTO_WPA) 4447 ver |= NL80211_WPA_VERSION_1; 4448 if (params->wpa_version & WPA_PROTO_RSN) 4449 ver |= NL80211_WPA_VERSION_2; 4450 if (ver) 4451 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver); 4452 4453 num_suites = 0; 4454 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X) 4455 suites[num_suites++] = WLAN_AKM_SUITE_8021X; 4456 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK) 4457 suites[num_suites++] = WLAN_AKM_SUITE_PSK; 4458 if (num_suites) { 4459 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES, 4460 num_suites * sizeof(u32), suites); 4461 } 4462 4463 num_suites = 0; 4464 if (params->pairwise_ciphers & WPA_CIPHER_CCMP) 4465 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP; 4466 if (params->pairwise_ciphers & WPA_CIPHER_TKIP) 4467 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP; 4468 if (params->pairwise_ciphers & WPA_CIPHER_WEP104) 4469 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104; 4470 if (params->pairwise_ciphers & WPA_CIPHER_WEP40) 4471 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40; 4472 if (num_suites) { 4473 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, 4474 num_suites * sizeof(u32), suites); 4475 } 4476 4477 switch (params->group_cipher) { 4478 case WPA_CIPHER_CCMP: 4479 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, 4480 WLAN_CIPHER_SUITE_CCMP); 4481 break; 4482 case WPA_CIPHER_TKIP: 4483 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, 4484 WLAN_CIPHER_SUITE_TKIP); 4485 break; 4486 case WPA_CIPHER_WEP104: 4487 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, 4488 WLAN_CIPHER_SUITE_WEP104); 4489 break; 4490 case WPA_CIPHER_WEP40: 4491 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, 4492 WLAN_CIPHER_SUITE_WEP40); 4493 break; 4494 } 4495 4496 if (params->beacon_ies) { 4497 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies), 4498 wpabuf_head(params->beacon_ies)); 4499 } 4500 if (params->proberesp_ies) { 4501 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP, 4502 wpabuf_len(params->proberesp_ies), 4503 wpabuf_head(params->proberesp_ies)); 4504 } 4505 if (params->assocresp_ies) { 4506 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP, 4507 wpabuf_len(params->assocresp_ies), 4508 wpabuf_head(params->assocresp_ies)); 4509 } 4510 4511 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4512 if (ret) { 4513 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)", 4514 ret, strerror(-ret)); 4515 } else { 4516 bss->beacon_set = 1; 4517 ret = nl80211_set_ap_isolate(bss, params->isolate); 4518 if (!params->isolate && ret) { 4519 wpa_printf(MSG_DEBUG, "nl80211: Ignore AP isolation " 4520 "configuration error since isolation is " 4521 "not used"); 4522 ret = 0; 4523 } 4524 4525 nl80211_set_bss(bss, params->cts_protect, params->preamble, 4526 params->short_slot_time, params->ht_opmode); 4527 } 4528 return ret; 4529 nla_put_failure: 4530 nlmsg_free(msg); 4531 return -ENOBUFS; 4532 } 4533 4534 4535 static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv, 4536 int freq, int ht_enabled, 4537 int sec_channel_offset) 4538 { 4539 struct nl_msg *msg; 4540 int ret; 4541 4542 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d " 4543 "sec_channel_offset=%d)", 4544 freq, ht_enabled, sec_channel_offset); 4545 msg = nlmsg_alloc(); 4546 if (!msg) 4547 return -1; 4548 4549 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY); 4550 4551 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 4552 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); 4553 if (ht_enabled) { 4554 switch (sec_channel_offset) { 4555 case -1: 4556 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, 4557 NL80211_CHAN_HT40MINUS); 4558 break; 4559 case 1: 4560 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, 4561 NL80211_CHAN_HT40PLUS); 4562 break; 4563 default: 4564 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, 4565 NL80211_CHAN_HT20); 4566 break; 4567 } 4568 } 4569 4570 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4571 msg = NULL; 4572 if (ret == 0) 4573 return 0; 4574 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): " 4575 "%d (%s)", freq, ret, strerror(-ret)); 4576 nla_put_failure: 4577 nlmsg_free(msg); 4578 return -1; 4579 } 4580 4581 4582 static u32 sta_flags_nl80211(int flags) 4583 { 4584 u32 f = 0; 4585 4586 if (flags & WPA_STA_AUTHORIZED) 4587 f |= BIT(NL80211_STA_FLAG_AUTHORIZED); 4588 if (flags & WPA_STA_WMM) 4589 f |= BIT(NL80211_STA_FLAG_WME); 4590 if (flags & WPA_STA_SHORT_PREAMBLE) 4591 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE); 4592 if (flags & WPA_STA_MFP) 4593 f |= BIT(NL80211_STA_FLAG_MFP); 4594 if (flags & WPA_STA_TDLS_PEER) 4595 f |= BIT(NL80211_STA_FLAG_TDLS_PEER); 4596 4597 return f; 4598 } 4599 4600 4601 static int wpa_driver_nl80211_sta_add(void *priv, 4602 struct hostapd_sta_add_params *params) 4603 { 4604 struct i802_bss *bss = priv; 4605 struct wpa_driver_nl80211_data *drv = bss->drv; 4606 struct nl_msg *msg; 4607 struct nl80211_sta_flag_update upd; 4608 int ret = -ENOBUFS; 4609 4610 if ((params->flags & WPA_STA_TDLS_PEER) && 4611 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) 4612 return -EOPNOTSUPP; 4613 4614 msg = nlmsg_alloc(); 4615 if (!msg) 4616 return -ENOMEM; 4617 4618 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION : 4619 NL80211_CMD_NEW_STATION); 4620 4621 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 4622 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr); 4623 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len, 4624 params->supp_rates); 4625 if (!params->set) { 4626 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid); 4627 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, 4628 params->listen_interval); 4629 } 4630 if (params->ht_capabilities) { 4631 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, 4632 sizeof(*params->ht_capabilities), 4633 params->ht_capabilities); 4634 } 4635 4636 os_memset(&upd, 0, sizeof(upd)); 4637 upd.mask = sta_flags_nl80211(params->flags); 4638 upd.set = upd.mask; 4639 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); 4640 4641 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4642 msg = NULL; 4643 if (ret) 4644 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION " 4645 "result: %d (%s)", params->set ? "SET" : "NEW", ret, 4646 strerror(-ret)); 4647 if (ret == -EEXIST) 4648 ret = 0; 4649 nla_put_failure: 4650 nlmsg_free(msg); 4651 return ret; 4652 } 4653 4654 4655 static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr) 4656 { 4657 struct i802_bss *bss = priv; 4658 struct wpa_driver_nl80211_data *drv = bss->drv; 4659 struct nl_msg *msg; 4660 int ret; 4661 4662 msg = nlmsg_alloc(); 4663 if (!msg) 4664 return -ENOMEM; 4665 4666 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION); 4667 4668 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, 4669 if_nametoindex(bss->ifname)); 4670 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 4671 4672 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4673 if (ret == -ENOENT) 4674 return 0; 4675 return ret; 4676 nla_put_failure: 4677 nlmsg_free(msg); 4678 return -ENOBUFS; 4679 } 4680 4681 4682 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, 4683 int ifidx) 4684 { 4685 struct nl_msg *msg; 4686 4687 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx); 4688 4689 /* stop listening for EAPOL on this interface */ 4690 del_ifidx(drv, ifidx); 4691 4692 msg = nlmsg_alloc(); 4693 if (!msg) 4694 goto nla_put_failure; 4695 4696 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE); 4697 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx); 4698 4699 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0) 4700 return; 4701 msg = NULL; 4702 nla_put_failure: 4703 nlmsg_free(msg); 4704 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx); 4705 } 4706 4707 4708 static const char * nl80211_iftype_str(enum nl80211_iftype mode) 4709 { 4710 switch (mode) { 4711 case NL80211_IFTYPE_ADHOC: 4712 return "ADHOC"; 4713 case NL80211_IFTYPE_STATION: 4714 return "STATION"; 4715 case NL80211_IFTYPE_AP: 4716 return "AP"; 4717 case NL80211_IFTYPE_MONITOR: 4718 return "MONITOR"; 4719 case NL80211_IFTYPE_P2P_CLIENT: 4720 return "P2P_CLIENT"; 4721 case NL80211_IFTYPE_P2P_GO: 4722 return "P2P_GO"; 4723 default: 4724 return "unknown"; 4725 } 4726 } 4727 4728 4729 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv, 4730 const char *ifname, 4731 enum nl80211_iftype iftype, 4732 const u8 *addr, int wds) 4733 { 4734 struct nl_msg *msg, *flags = NULL; 4735 int ifidx; 4736 int ret = -ENOBUFS; 4737 4738 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)", 4739 iftype, nl80211_iftype_str(iftype)); 4740 4741 msg = nlmsg_alloc(); 4742 if (!msg) 4743 return -1; 4744 4745 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE); 4746 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 4747 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname); 4748 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype); 4749 4750 if (iftype == NL80211_IFTYPE_MONITOR) { 4751 int err; 4752 4753 flags = nlmsg_alloc(); 4754 if (!flags) 4755 goto nla_put_failure; 4756 4757 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES); 4758 4759 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags); 4760 4761 nlmsg_free(flags); 4762 4763 if (err) 4764 goto nla_put_failure; 4765 } else if (wds) { 4766 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds); 4767 } 4768 4769 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4770 msg = NULL; 4771 if (ret) { 4772 nla_put_failure: 4773 nlmsg_free(msg); 4774 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)", 4775 ifname, ret, strerror(-ret)); 4776 return ret; 4777 } 4778 4779 ifidx = if_nametoindex(ifname); 4780 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d", 4781 ifname, ifidx); 4782 4783 if (ifidx <= 0) 4784 return -1; 4785 4786 /* start listening for EAPOL on this interface */ 4787 add_ifidx(drv, ifidx); 4788 4789 if (addr && iftype != NL80211_IFTYPE_MONITOR && 4790 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) { 4791 nl80211_remove_iface(drv, ifidx); 4792 return -1; 4793 } 4794 4795 return ifidx; 4796 } 4797 4798 4799 static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv, 4800 const char *ifname, enum nl80211_iftype iftype, 4801 const u8 *addr, int wds) 4802 { 4803 int ret; 4804 4805 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds); 4806 4807 /* if error occurred and interface exists already */ 4808 if (ret == -ENFILE && if_nametoindex(ifname)) { 4809 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname); 4810 4811 /* Try to remove the interface that was already there. */ 4812 nl80211_remove_iface(drv, if_nametoindex(ifname)); 4813 4814 /* Try to create the interface again */ 4815 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, 4816 wds); 4817 } 4818 4819 if (ret >= 0 && is_p2p_interface(iftype)) 4820 nl80211_disable_11b_rates(drv, ret, 1); 4821 4822 return ret; 4823 } 4824 4825 4826 static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok) 4827 { 4828 struct ieee80211_hdr *hdr; 4829 u16 fc; 4830 union wpa_event_data event; 4831 4832 hdr = (struct ieee80211_hdr *) buf; 4833 fc = le_to_host16(hdr->frame_control); 4834 4835 os_memset(&event, 0, sizeof(event)); 4836 event.tx_status.type = WLAN_FC_GET_TYPE(fc); 4837 event.tx_status.stype = WLAN_FC_GET_STYPE(fc); 4838 event.tx_status.dst = hdr->addr1; 4839 event.tx_status.data = buf; 4840 event.tx_status.data_len = len; 4841 event.tx_status.ack = ok; 4842 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event); 4843 } 4844 4845 4846 static void from_unknown_sta(struct wpa_driver_nl80211_data *drv, 4847 u8 *buf, size_t len) 4848 { 4849 struct ieee80211_hdr *hdr = (void *)buf; 4850 u16 fc; 4851 union wpa_event_data event; 4852 4853 if (len < sizeof(*hdr)) 4854 return; 4855 4856 fc = le_to_host16(hdr->frame_control); 4857 4858 os_memset(&event, 0, sizeof(event)); 4859 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len); 4860 event.rx_from_unknown.addr = hdr->addr2; 4861 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) == 4862 (WLAN_FC_FROMDS | WLAN_FC_TODS); 4863 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event); 4864 } 4865 4866 4867 static void handle_frame(struct wpa_driver_nl80211_data *drv, 4868 u8 *buf, size_t len, int datarate, int ssi_signal) 4869 { 4870 struct ieee80211_hdr *hdr; 4871 u16 fc; 4872 union wpa_event_data event; 4873 4874 hdr = (struct ieee80211_hdr *) buf; 4875 fc = le_to_host16(hdr->frame_control); 4876 4877 switch (WLAN_FC_GET_TYPE(fc)) { 4878 case WLAN_FC_TYPE_MGMT: 4879 os_memset(&event, 0, sizeof(event)); 4880 event.rx_mgmt.frame = buf; 4881 event.rx_mgmt.frame_len = len; 4882 event.rx_mgmt.datarate = datarate; 4883 event.rx_mgmt.ssi_signal = ssi_signal; 4884 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event); 4885 break; 4886 case WLAN_FC_TYPE_CTRL: 4887 /* can only get here with PS-Poll frames */ 4888 wpa_printf(MSG_DEBUG, "CTRL"); 4889 from_unknown_sta(drv, buf, len); 4890 break; 4891 case WLAN_FC_TYPE_DATA: 4892 from_unknown_sta(drv, buf, len); 4893 break; 4894 } 4895 } 4896 4897 4898 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx) 4899 { 4900 struct wpa_driver_nl80211_data *drv = eloop_ctx; 4901 int len; 4902 unsigned char buf[3000]; 4903 struct ieee80211_radiotap_iterator iter; 4904 int ret; 4905 int datarate = 0, ssi_signal = 0; 4906 int injected = 0, failed = 0, rxflags = 0; 4907 4908 len = recv(sock, buf, sizeof(buf), 0); 4909 if (len < 0) { 4910 perror("recv"); 4911 return; 4912 } 4913 4914 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) { 4915 printf("received invalid radiotap frame\n"); 4916 return; 4917 } 4918 4919 while (1) { 4920 ret = ieee80211_radiotap_iterator_next(&iter); 4921 if (ret == -ENOENT) 4922 break; 4923 if (ret) { 4924 printf("received invalid radiotap frame (%d)\n", ret); 4925 return; 4926 } 4927 switch (iter.this_arg_index) { 4928 case IEEE80211_RADIOTAP_FLAGS: 4929 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS) 4930 len -= 4; 4931 break; 4932 case IEEE80211_RADIOTAP_RX_FLAGS: 4933 rxflags = 1; 4934 break; 4935 case IEEE80211_RADIOTAP_TX_FLAGS: 4936 injected = 1; 4937 failed = le_to_host16((*(uint16_t *) iter.this_arg)) & 4938 IEEE80211_RADIOTAP_F_TX_FAIL; 4939 break; 4940 case IEEE80211_RADIOTAP_DATA_RETRIES: 4941 break; 4942 case IEEE80211_RADIOTAP_CHANNEL: 4943 /* TODO: convert from freq/flags to channel number */ 4944 break; 4945 case IEEE80211_RADIOTAP_RATE: 4946 datarate = *iter.this_arg * 5; 4947 break; 4948 case IEEE80211_RADIOTAP_DB_ANTSIGNAL: 4949 ssi_signal = *iter.this_arg; 4950 break; 4951 } 4952 } 4953 4954 if (rxflags && injected) 4955 return; 4956 4957 if (!injected) 4958 handle_frame(drv, buf + iter.max_length, 4959 len - iter.max_length, datarate, ssi_signal); 4960 else 4961 handle_tx_callback(drv->ctx, buf + iter.max_length, 4962 len - iter.max_length, !failed); 4963 } 4964 4965 4966 /* 4967 * we post-process the filter code later and rewrite 4968 * this to the offset to the last instruction 4969 */ 4970 #define PASS 0xFF 4971 #define FAIL 0xFE 4972 4973 static struct sock_filter msock_filter_insns[] = { 4974 /* 4975 * do a little-endian load of the radiotap length field 4976 */ 4977 /* load lower byte into A */ 4978 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2), 4979 /* put it into X (== index register) */ 4980 BPF_STMT(BPF_MISC| BPF_TAX, 0), 4981 /* load upper byte into A */ 4982 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3), 4983 /* left-shift it by 8 */ 4984 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8), 4985 /* or with X */ 4986 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0), 4987 /* put result into X */ 4988 BPF_STMT(BPF_MISC| BPF_TAX, 0), 4989 4990 /* 4991 * Allow management frames through, this also gives us those 4992 * management frames that we sent ourselves with status 4993 */ 4994 /* load the lower byte of the IEEE 802.11 frame control field */ 4995 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), 4996 /* mask off frame type and version */ 4997 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF), 4998 /* accept frame if it's both 0, fall through otherwise */ 4999 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0), 5000 5001 /* 5002 * TODO: add a bit to radiotap RX flags that indicates 5003 * that the sending station is not associated, then 5004 * add a filter here that filters on our DA and that flag 5005 * to allow us to deauth frames to that bad station. 5006 * 5007 * For now allow all To DS data frames through. 5008 */ 5009 /* load the IEEE 802.11 frame control field */ 5010 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0), 5011 /* mask off frame type, version and DS status */ 5012 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03), 5013 /* accept frame if version 0, type 2 and To DS, fall through otherwise 5014 */ 5015 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0), 5016 5017 #if 0 5018 /* 5019 * drop non-data frames 5020 */ 5021 /* load the lower byte of the frame control field */ 5022 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), 5023 /* mask off QoS bit */ 5024 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c), 5025 /* drop non-data frames */ 5026 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL), 5027 #endif 5028 /* load the upper byte of the frame control field */ 5029 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1), 5030 /* mask off toDS/fromDS */ 5031 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03), 5032 /* accept WDS frames */ 5033 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0), 5034 5035 /* 5036 * add header length to index 5037 */ 5038 /* load the lower byte of the frame control field */ 5039 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), 5040 /* mask off QoS bit */ 5041 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80), 5042 /* right shift it by 6 to give 0 or 2 */ 5043 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6), 5044 /* add data frame header length */ 5045 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24), 5046 /* add index, was start of 802.11 header */ 5047 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), 5048 /* move to index, now start of LL header */ 5049 BPF_STMT(BPF_MISC | BPF_TAX, 0), 5050 5051 /* 5052 * Accept empty data frames, we use those for 5053 * polling activity. 5054 */ 5055 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0), 5056 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0), 5057 5058 /* 5059 * Accept EAPOL frames 5060 */ 5061 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0), 5062 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL), 5063 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4), 5064 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL), 5065 5066 /* keep these last two statements or change the code below */ 5067 /* return 0 == "DROP" */ 5068 BPF_STMT(BPF_RET | BPF_K, 0), 5069 /* return ~0 == "keep all" */ 5070 BPF_STMT(BPF_RET | BPF_K, ~0), 5071 }; 5072 5073 static struct sock_fprog msock_filter = { 5074 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]), 5075 .filter = msock_filter_insns, 5076 }; 5077 5078 5079 static int add_monitor_filter(int s) 5080 { 5081 int idx; 5082 5083 /* rewrite all PASS/FAIL jump offsets */ 5084 for (idx = 0; idx < msock_filter.len; idx++) { 5085 struct sock_filter *insn = &msock_filter_insns[idx]; 5086 5087 if (BPF_CLASS(insn->code) == BPF_JMP) { 5088 if (insn->code == (BPF_JMP|BPF_JA)) { 5089 if (insn->k == PASS) 5090 insn->k = msock_filter.len - idx - 2; 5091 else if (insn->k == FAIL) 5092 insn->k = msock_filter.len - idx - 3; 5093 } 5094 5095 if (insn->jt == PASS) 5096 insn->jt = msock_filter.len - idx - 2; 5097 else if (insn->jt == FAIL) 5098 insn->jt = msock_filter.len - idx - 3; 5099 5100 if (insn->jf == PASS) 5101 insn->jf = msock_filter.len - idx - 2; 5102 else if (insn->jf == FAIL) 5103 insn->jf = msock_filter.len - idx - 3; 5104 } 5105 } 5106 5107 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, 5108 &msock_filter, sizeof(msock_filter))) { 5109 perror("SO_ATTACH_FILTER"); 5110 return -1; 5111 } 5112 5113 return 0; 5114 } 5115 5116 5117 static void nl80211_remove_monitor_interface( 5118 struct wpa_driver_nl80211_data *drv) 5119 { 5120 if (drv->monitor_ifidx >= 0) { 5121 nl80211_remove_iface(drv, drv->monitor_ifidx); 5122 drv->monitor_ifidx = -1; 5123 } 5124 if (drv->monitor_sock >= 0) { 5125 eloop_unregister_read_sock(drv->monitor_sock); 5126 close(drv->monitor_sock); 5127 drv->monitor_sock = -1; 5128 } 5129 } 5130 5131 5132 static int 5133 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv) 5134 { 5135 char buf[IFNAMSIZ]; 5136 struct sockaddr_ll ll; 5137 int optval; 5138 socklen_t optlen; 5139 5140 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) { 5141 /* 5142 * P2P interface name is of the format p2p-%s-%d. For monitor 5143 * interface name corresponding to P2P GO, replace "p2p-" with 5144 * "mon-" to retain the same interface name length and to 5145 * indicate that it is a monitor interface. 5146 */ 5147 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4); 5148 } else { 5149 /* Non-P2P interface with AP functionality. */ 5150 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname); 5151 } 5152 5153 buf[IFNAMSIZ - 1] = '\0'; 5154 5155 drv->monitor_ifidx = 5156 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL, 5157 0); 5158 5159 if (drv->monitor_ifidx == -EOPNOTSUPP) { 5160 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support " 5161 "monitor interface type - try to run without it"); 5162 drv->no_monitor_iface_capab = 1; 5163 } 5164 5165 if (drv->monitor_ifidx < 0) 5166 return -1; 5167 5168 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1)) 5169 goto error; 5170 5171 memset(&ll, 0, sizeof(ll)); 5172 ll.sll_family = AF_PACKET; 5173 ll.sll_ifindex = drv->monitor_ifidx; 5174 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); 5175 if (drv->monitor_sock < 0) { 5176 perror("socket[PF_PACKET,SOCK_RAW]"); 5177 goto error; 5178 } 5179 5180 if (add_monitor_filter(drv->monitor_sock)) { 5181 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor " 5182 "interface; do filtering in user space"); 5183 /* This works, but will cost in performance. */ 5184 } 5185 5186 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) { 5187 perror("monitor socket bind"); 5188 goto error; 5189 } 5190 5191 optlen = sizeof(optval); 5192 optval = 20; 5193 if (setsockopt 5194 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) { 5195 perror("Failed to set socket priority"); 5196 goto error; 5197 } 5198 5199 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read, 5200 drv, NULL)) { 5201 printf("Could not register monitor read socket\n"); 5202 goto error; 5203 } 5204 5205 return 0; 5206 error: 5207 nl80211_remove_monitor_interface(drv); 5208 return -1; 5209 } 5210 5211 5212 #ifdef CONFIG_AP 5213 static int nl80211_send_eapol_data(struct i802_bss *bss, 5214 const u8 *addr, const u8 *data, 5215 size_t data_len, const u8 *own_addr) 5216 { 5217 if (bss->drv->l2 == NULL) { 5218 wpa_printf(MSG_DEBUG, "nl80211: No l2_packet to send EAPOL"); 5219 return -1; 5220 } 5221 5222 if (l2_packet_send(bss->drv->l2, addr, ETH_P_EAPOL, data, data_len) < 5223 0) 5224 return -1; 5225 return 0; 5226 } 5227 #endif /* CONFIG_AP */ 5228 5229 5230 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; 5231 5232 static int wpa_driver_nl80211_hapd_send_eapol( 5233 void *priv, const u8 *addr, const u8 *data, 5234 size_t data_len, int encrypt, const u8 *own_addr, u32 flags) 5235 { 5236 struct i802_bss *bss = priv; 5237 struct wpa_driver_nl80211_data *drv = bss->drv; 5238 struct ieee80211_hdr *hdr; 5239 size_t len; 5240 u8 *pos; 5241 int res; 5242 int qos = flags & WPA_STA_WMM; 5243 5244 #ifdef CONFIG_AP 5245 if (drv->no_monitor_iface_capab) 5246 return nl80211_send_eapol_data(bss, addr, data, data_len, 5247 own_addr); 5248 #endif /* CONFIG_AP */ 5249 5250 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 + 5251 data_len; 5252 hdr = os_zalloc(len); 5253 if (hdr == NULL) { 5254 printf("malloc() failed for i802_send_data(len=%lu)\n", 5255 (unsigned long) len); 5256 return -1; 5257 } 5258 5259 hdr->frame_control = 5260 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA); 5261 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS); 5262 if (encrypt) 5263 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP); 5264 if (qos) { 5265 hdr->frame_control |= 5266 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4); 5267 } 5268 5269 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN); 5270 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN); 5271 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN); 5272 pos = (u8 *) (hdr + 1); 5273 5274 if (qos) { 5275 /* add an empty QoS header if needed */ 5276 pos[0] = 0; 5277 pos[1] = 0; 5278 pos += 2; 5279 } 5280 5281 memcpy(pos, rfc1042_header, sizeof(rfc1042_header)); 5282 pos += sizeof(rfc1042_header); 5283 WPA_PUT_BE16(pos, ETH_P_PAE); 5284 pos += 2; 5285 memcpy(pos, data, data_len); 5286 5287 res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt); 5288 if (res < 0) { 5289 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - " 5290 "failed: %d (%s)", 5291 (unsigned long) len, errno, strerror(errno)); 5292 } 5293 os_free(hdr); 5294 5295 return res; 5296 } 5297 5298 5299 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr, 5300 int total_flags, 5301 int flags_or, int flags_and) 5302 { 5303 struct i802_bss *bss = priv; 5304 struct wpa_driver_nl80211_data *drv = bss->drv; 5305 struct nl_msg *msg, *flags = NULL; 5306 struct nl80211_sta_flag_update upd; 5307 5308 msg = nlmsg_alloc(); 5309 if (!msg) 5310 return -ENOMEM; 5311 5312 flags = nlmsg_alloc(); 5313 if (!flags) { 5314 nlmsg_free(msg); 5315 return -ENOMEM; 5316 } 5317 5318 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION); 5319 5320 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, 5321 if_nametoindex(bss->ifname)); 5322 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 5323 5324 /* 5325 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This 5326 * can be removed eventually. 5327 */ 5328 if (total_flags & WPA_STA_AUTHORIZED) 5329 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED); 5330 5331 if (total_flags & WPA_STA_WMM) 5332 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME); 5333 5334 if (total_flags & WPA_STA_SHORT_PREAMBLE) 5335 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE); 5336 5337 if (total_flags & WPA_STA_MFP) 5338 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP); 5339 5340 if (total_flags & WPA_STA_TDLS_PEER) 5341 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER); 5342 5343 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags)) 5344 goto nla_put_failure; 5345 5346 os_memset(&upd, 0, sizeof(upd)); 5347 upd.mask = sta_flags_nl80211(flags_or | ~flags_and); 5348 upd.set = sta_flags_nl80211(flags_or); 5349 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); 5350 5351 nlmsg_free(flags); 5352 5353 return send_and_recv_msgs(drv, msg, NULL, NULL); 5354 nla_put_failure: 5355 nlmsg_free(msg); 5356 nlmsg_free(flags); 5357 return -ENOBUFS; 5358 } 5359 5360 5361 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv, 5362 struct wpa_driver_associate_params *params) 5363 { 5364 enum nl80211_iftype nlmode; 5365 5366 if (params->p2p) { 5367 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P " 5368 "group (GO)"); 5369 nlmode = NL80211_IFTYPE_P2P_GO; 5370 } else 5371 nlmode = NL80211_IFTYPE_AP; 5372 5373 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode) || 5374 wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) { 5375 nl80211_remove_monitor_interface(drv); 5376 return -1; 5377 } 5378 5379 if (drv->no_monitor_iface_capab) { 5380 if (wpa_driver_nl80211_probe_req_report(&drv->first_bss, 1) < 0) 5381 { 5382 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable " 5383 "Probe Request frame reporting in AP mode"); 5384 /* Try to survive without this */ 5385 } 5386 } 5387 5388 drv->ap_oper_freq = params->freq; 5389 5390 return 0; 5391 } 5392 5393 5394 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv) 5395 { 5396 struct nl_msg *msg; 5397 int ret = -1; 5398 5399 msg = nlmsg_alloc(); 5400 if (!msg) 5401 return -1; 5402 5403 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS); 5404 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 5405 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5406 msg = NULL; 5407 if (ret) { 5408 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d " 5409 "(%s)", ret, strerror(-ret)); 5410 goto nla_put_failure; 5411 } 5412 5413 ret = 0; 5414 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully"); 5415 5416 nla_put_failure: 5417 nlmsg_free(msg); 5418 return ret; 5419 } 5420 5421 5422 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv, 5423 struct wpa_driver_associate_params *params) 5424 { 5425 struct nl_msg *msg; 5426 int ret = -1; 5427 int count = 0; 5428 5429 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex); 5430 5431 if (wpa_driver_nl80211_set_mode(&drv->first_bss, 5432 NL80211_IFTYPE_ADHOC)) { 5433 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into " 5434 "IBSS mode"); 5435 return -1; 5436 } 5437 5438 retry: 5439 msg = nlmsg_alloc(); 5440 if (!msg) 5441 return -1; 5442 5443 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS); 5444 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 5445 5446 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid)) 5447 goto nla_put_failure; 5448 5449 wpa_hexdump_ascii(MSG_DEBUG, " * SSID", 5450 params->ssid, params->ssid_len); 5451 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 5452 params->ssid); 5453 os_memcpy(drv->ssid, params->ssid, params->ssid_len); 5454 drv->ssid_len = params->ssid_len; 5455 5456 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); 5457 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); 5458 5459 ret = nl80211_set_conn_keys(params, msg); 5460 if (ret) 5461 goto nla_put_failure; 5462 5463 if (params->wpa_ie) { 5464 wpa_hexdump(MSG_DEBUG, 5465 " * Extra IEs for Beacon/Probe Response frames", 5466 params->wpa_ie, params->wpa_ie_len); 5467 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len, 5468 params->wpa_ie); 5469 } 5470 5471 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5472 msg = NULL; 5473 if (ret) { 5474 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)", 5475 ret, strerror(-ret)); 5476 count++; 5477 if (ret == -EALREADY && count == 1) { 5478 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after " 5479 "forced leave"); 5480 nl80211_leave_ibss(drv); 5481 nlmsg_free(msg); 5482 goto retry; 5483 } 5484 5485 goto nla_put_failure; 5486 } 5487 ret = 0; 5488 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully"); 5489 5490 nla_put_failure: 5491 nlmsg_free(msg); 5492 return ret; 5493 } 5494 5495 5496 static unsigned int nl80211_get_assoc_bssid(struct wpa_driver_nl80211_data *drv, 5497 u8 *bssid) 5498 { 5499 struct nl_msg *msg; 5500 int ret; 5501 struct nl80211_bss_info_arg arg; 5502 5503 os_memset(&arg, 0, sizeof(arg)); 5504 msg = nlmsg_alloc(); 5505 if (!msg) 5506 goto nla_put_failure; 5507 5508 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN); 5509 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 5510 5511 arg.drv = drv; 5512 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg); 5513 msg = NULL; 5514 if (ret == 0) { 5515 if (is_zero_ether_addr(arg.assoc_bssid)) 5516 return -ENOTCONN; 5517 os_memcpy(bssid, arg.assoc_bssid, ETH_ALEN); 5518 return 0; 5519 } 5520 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d " 5521 "(%s)", ret, strerror(-ret)); 5522 nla_put_failure: 5523 nlmsg_free(msg); 5524 return drv->assoc_freq; 5525 } 5526 5527 5528 static int nl80211_disconnect(struct wpa_driver_nl80211_data *drv, 5529 const u8 *bssid) 5530 { 5531 u8 addr[ETH_ALEN]; 5532 5533 if (bssid == NULL) { 5534 int res = nl80211_get_assoc_bssid(drv, addr); 5535 if (res) 5536 return res; 5537 bssid = addr; 5538 } 5539 5540 return wpa_driver_nl80211_disconnect(drv, bssid, 5541 WLAN_REASON_PREV_AUTH_NOT_VALID); 5542 } 5543 5544 5545 static int wpa_driver_nl80211_connect( 5546 struct wpa_driver_nl80211_data *drv, 5547 struct wpa_driver_associate_params *params) 5548 { 5549 struct nl_msg *msg; 5550 enum nl80211_auth_type type; 5551 int ret = 0; 5552 int algs; 5553 5554 msg = nlmsg_alloc(); 5555 if (!msg) 5556 return -1; 5557 5558 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex); 5559 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT); 5560 5561 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 5562 if (params->bssid) { 5563 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, 5564 MAC2STR(params->bssid)); 5565 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); 5566 } 5567 if (params->freq) { 5568 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); 5569 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); 5570 } 5571 if (params->ssid) { 5572 wpa_hexdump_ascii(MSG_DEBUG, " * SSID", 5573 params->ssid, params->ssid_len); 5574 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 5575 params->ssid); 5576 if (params->ssid_len > sizeof(drv->ssid)) 5577 goto nla_put_failure; 5578 os_memcpy(drv->ssid, params->ssid, params->ssid_len); 5579 drv->ssid_len = params->ssid_len; 5580 } 5581 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len); 5582 if (params->wpa_ie) 5583 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len, 5584 params->wpa_ie); 5585 5586 algs = 0; 5587 if (params->auth_alg & WPA_AUTH_ALG_OPEN) 5588 algs++; 5589 if (params->auth_alg & WPA_AUTH_ALG_SHARED) 5590 algs++; 5591 if (params->auth_alg & WPA_AUTH_ALG_LEAP) 5592 algs++; 5593 if (algs > 1) { 5594 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic " 5595 "selection"); 5596 goto skip_auth_type; 5597 } 5598 5599 if (params->auth_alg & WPA_AUTH_ALG_OPEN) 5600 type = NL80211_AUTHTYPE_OPEN_SYSTEM; 5601 else if (params->auth_alg & WPA_AUTH_ALG_SHARED) 5602 type = NL80211_AUTHTYPE_SHARED_KEY; 5603 else if (params->auth_alg & WPA_AUTH_ALG_LEAP) 5604 type = NL80211_AUTHTYPE_NETWORK_EAP; 5605 else if (params->auth_alg & WPA_AUTH_ALG_FT) 5606 type = NL80211_AUTHTYPE_FT; 5607 else 5608 goto nla_put_failure; 5609 5610 wpa_printf(MSG_DEBUG, " * Auth Type %d", type); 5611 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type); 5612 5613 skip_auth_type: 5614 if (params->wpa_proto) { 5615 enum nl80211_wpa_versions ver = 0; 5616 5617 if (params->wpa_proto & WPA_PROTO_WPA) 5618 ver |= NL80211_WPA_VERSION_1; 5619 if (params->wpa_proto & WPA_PROTO_RSN) 5620 ver |= NL80211_WPA_VERSION_2; 5621 5622 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver); 5623 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver); 5624 } 5625 5626 if (params->pairwise_suite != CIPHER_NONE) { 5627 int cipher; 5628 5629 switch (params->pairwise_suite) { 5630 case CIPHER_WEP40: 5631 cipher = WLAN_CIPHER_SUITE_WEP40; 5632 break; 5633 case CIPHER_WEP104: 5634 cipher = WLAN_CIPHER_SUITE_WEP104; 5635 break; 5636 case CIPHER_CCMP: 5637 cipher = WLAN_CIPHER_SUITE_CCMP; 5638 break; 5639 case CIPHER_TKIP: 5640 default: 5641 cipher = WLAN_CIPHER_SUITE_TKIP; 5642 break; 5643 } 5644 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher); 5645 } 5646 5647 if (params->group_suite != CIPHER_NONE) { 5648 int cipher; 5649 5650 switch (params->group_suite) { 5651 case CIPHER_WEP40: 5652 cipher = WLAN_CIPHER_SUITE_WEP40; 5653 break; 5654 case CIPHER_WEP104: 5655 cipher = WLAN_CIPHER_SUITE_WEP104; 5656 break; 5657 case CIPHER_CCMP: 5658 cipher = WLAN_CIPHER_SUITE_CCMP; 5659 break; 5660 case CIPHER_TKIP: 5661 default: 5662 cipher = WLAN_CIPHER_SUITE_TKIP; 5663 break; 5664 } 5665 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher); 5666 } 5667 5668 if (params->key_mgmt_suite == KEY_MGMT_802_1X || 5669 params->key_mgmt_suite == KEY_MGMT_PSK) { 5670 int mgmt = WLAN_AKM_SUITE_PSK; 5671 5672 switch (params->key_mgmt_suite) { 5673 case KEY_MGMT_802_1X: 5674 mgmt = WLAN_AKM_SUITE_8021X; 5675 break; 5676 case KEY_MGMT_PSK: 5677 default: 5678 mgmt = WLAN_AKM_SUITE_PSK; 5679 break; 5680 } 5681 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt); 5682 } 5683 5684 ret = nl80211_set_conn_keys(params, msg); 5685 if (ret) 5686 goto nla_put_failure; 5687 5688 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5689 msg = NULL; 5690 if (ret) { 5691 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d " 5692 "(%s)", ret, strerror(-ret)); 5693 /* 5694 * cfg80211 does not currently accept new connection if we are 5695 * already connected. As a workaround, force disconnection and 5696 * try again once the driver indicates it completed 5697 * disconnection. 5698 */ 5699 if (ret == -EALREADY) 5700 nl80211_disconnect(drv, params->bssid); 5701 goto nla_put_failure; 5702 } 5703 ret = 0; 5704 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully"); 5705 5706 nla_put_failure: 5707 nlmsg_free(msg); 5708 return ret; 5709 5710 } 5711 5712 5713 static int wpa_driver_nl80211_associate( 5714 void *priv, struct wpa_driver_associate_params *params) 5715 { 5716 struct i802_bss *bss = priv; 5717 struct wpa_driver_nl80211_data *drv = bss->drv; 5718 int ret = -1; 5719 struct nl_msg *msg; 5720 5721 if (params->mode == IEEE80211_MODE_AP) 5722 return wpa_driver_nl80211_ap(drv, params); 5723 5724 if (params->mode == IEEE80211_MODE_IBSS) 5725 return wpa_driver_nl80211_ibss(drv, params); 5726 5727 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) { 5728 enum nl80211_iftype nlmode = params->p2p ? 5729 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION; 5730 5731 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0) 5732 return -1; 5733 return wpa_driver_nl80211_connect(drv, params); 5734 } 5735 5736 drv->associated = 0; 5737 5738 msg = nlmsg_alloc(); 5739 if (!msg) 5740 return -1; 5741 5742 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)", 5743 drv->ifindex); 5744 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE); 5745 5746 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 5747 if (params->bssid) { 5748 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, 5749 MAC2STR(params->bssid)); 5750 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); 5751 } 5752 if (params->freq) { 5753 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); 5754 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); 5755 drv->assoc_freq = params->freq; 5756 } else 5757 drv->assoc_freq = 0; 5758 if (params->ssid) { 5759 wpa_hexdump_ascii(MSG_DEBUG, " * SSID", 5760 params->ssid, params->ssid_len); 5761 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 5762 params->ssid); 5763 if (params->ssid_len > sizeof(drv->ssid)) 5764 goto nla_put_failure; 5765 os_memcpy(drv->ssid, params->ssid, params->ssid_len); 5766 drv->ssid_len = params->ssid_len; 5767 } 5768 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len); 5769 if (params->wpa_ie) 5770 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len, 5771 params->wpa_ie); 5772 5773 if (params->pairwise_suite != CIPHER_NONE) { 5774 int cipher; 5775 5776 switch (params->pairwise_suite) { 5777 case CIPHER_WEP40: 5778 cipher = WLAN_CIPHER_SUITE_WEP40; 5779 break; 5780 case CIPHER_WEP104: 5781 cipher = WLAN_CIPHER_SUITE_WEP104; 5782 break; 5783 case CIPHER_CCMP: 5784 cipher = WLAN_CIPHER_SUITE_CCMP; 5785 break; 5786 case CIPHER_TKIP: 5787 default: 5788 cipher = WLAN_CIPHER_SUITE_TKIP; 5789 break; 5790 } 5791 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher); 5792 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher); 5793 } 5794 5795 if (params->group_suite != CIPHER_NONE) { 5796 int cipher; 5797 5798 switch (params->group_suite) { 5799 case CIPHER_WEP40: 5800 cipher = WLAN_CIPHER_SUITE_WEP40; 5801 break; 5802 case CIPHER_WEP104: 5803 cipher = WLAN_CIPHER_SUITE_WEP104; 5804 break; 5805 case CIPHER_CCMP: 5806 cipher = WLAN_CIPHER_SUITE_CCMP; 5807 break; 5808 case CIPHER_TKIP: 5809 default: 5810 cipher = WLAN_CIPHER_SUITE_TKIP; 5811 break; 5812 } 5813 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher); 5814 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher); 5815 } 5816 5817 #ifdef CONFIG_IEEE80211W 5818 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED) 5819 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED); 5820 #endif /* CONFIG_IEEE80211W */ 5821 5822 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT); 5823 5824 if (params->prev_bssid) { 5825 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR, 5826 MAC2STR(params->prev_bssid)); 5827 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN, 5828 params->prev_bssid); 5829 } 5830 5831 if (params->p2p) 5832 wpa_printf(MSG_DEBUG, " * P2P group"); 5833 5834 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5835 msg = NULL; 5836 if (ret) { 5837 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d " 5838 "(%s)", ret, strerror(-ret)); 5839 nl80211_dump_scan(drv); 5840 goto nla_put_failure; 5841 } 5842 ret = 0; 5843 wpa_printf(MSG_DEBUG, "nl80211: Association request send " 5844 "successfully"); 5845 5846 nla_put_failure: 5847 nlmsg_free(msg); 5848 return ret; 5849 } 5850 5851 5852 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv, 5853 int ifindex, enum nl80211_iftype mode) 5854 { 5855 struct nl_msg *msg; 5856 int ret = -ENOBUFS; 5857 5858 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)", 5859 ifindex, mode, nl80211_iftype_str(mode)); 5860 5861 msg = nlmsg_alloc(); 5862 if (!msg) 5863 return -ENOMEM; 5864 5865 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE); 5866 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 5867 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode); 5868 5869 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5870 msg = NULL; 5871 if (!ret) 5872 return 0; 5873 nla_put_failure: 5874 nlmsg_free(msg); 5875 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:" 5876 " %d (%s)", ifindex, mode, ret, strerror(-ret)); 5877 return ret; 5878 } 5879 5880 5881 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss, 5882 enum nl80211_iftype nlmode) 5883 { 5884 struct wpa_driver_nl80211_data *drv = bss->drv; 5885 int ret = -1; 5886 int i; 5887 int was_ap = is_ap_interface(drv->nlmode); 5888 5889 if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) { 5890 drv->nlmode = nlmode; 5891 ret = 0; 5892 goto done; 5893 } 5894 5895 if (nlmode == drv->nlmode) { 5896 wpa_printf(MSG_DEBUG, "nl80211: Interface already in " 5897 "requested mode - ignore error"); 5898 ret = 0; 5899 goto done; /* Already in the requested mode */ 5900 } 5901 5902 /* mac80211 doesn't allow mode changes while the device is up, so 5903 * take the device down, try to set the mode again, and bring the 5904 * device back up. 5905 */ 5906 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting " 5907 "interface down"); 5908 for (i = 0; i < 10; i++) { 5909 int res; 5910 res = linux_set_iface_flags(drv->global->ioctl_sock, 5911 bss->ifname, 0); 5912 if (res == -EACCES || res == -ENODEV) 5913 break; 5914 if (res == 0) { 5915 /* Try to set the mode again while the interface is 5916 * down */ 5917 ret = nl80211_set_mode(drv, drv->ifindex, nlmode); 5918 if (ret == -EACCES) 5919 break; 5920 res = linux_set_iface_flags(drv->global->ioctl_sock, 5921 bss->ifname, 1); 5922 if (res && !ret) 5923 ret = -1; 5924 else if (ret != -EBUSY) 5925 break; 5926 } else 5927 wpa_printf(MSG_DEBUG, "nl80211: Failed to set " 5928 "interface down"); 5929 os_sleep(0, 100000); 5930 } 5931 5932 if (!ret) { 5933 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while " 5934 "interface is down"); 5935 drv->nlmode = nlmode; 5936 drv->ignore_if_down_event = 1; 5937 } 5938 5939 done: 5940 if (!ret && is_ap_interface(nlmode)) { 5941 /* Setup additional AP mode functionality if needed */ 5942 if (!drv->no_monitor_iface_capab && drv->monitor_ifidx < 0 && 5943 nl80211_create_monitor_interface(drv) && 5944 !drv->no_monitor_iface_capab) 5945 return -1; 5946 } else if (!ret && !is_ap_interface(nlmode)) { 5947 /* Remove additional AP mode functionality */ 5948 if (was_ap && drv->no_monitor_iface_capab) 5949 wpa_driver_nl80211_probe_req_report(bss, 0); 5950 nl80211_remove_monitor_interface(drv); 5951 bss->beacon_set = 0; 5952 } 5953 5954 if (!ret && is_p2p_interface(drv->nlmode)) { 5955 nl80211_disable_11b_rates(drv, drv->ifindex, 1); 5956 drv->disabled_11b_rates = 1; 5957 } else if (!ret && drv->disabled_11b_rates) { 5958 nl80211_disable_11b_rates(drv, drv->ifindex, 0); 5959 drv->disabled_11b_rates = 0; 5960 } 5961 5962 if (ret) 5963 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d " 5964 "from %d failed", nlmode, drv->nlmode); 5965 5966 return ret; 5967 } 5968 5969 5970 static int wpa_driver_nl80211_get_capa(void *priv, 5971 struct wpa_driver_capa *capa) 5972 { 5973 struct i802_bss *bss = priv; 5974 struct wpa_driver_nl80211_data *drv = bss->drv; 5975 if (!drv->has_capability) 5976 return -1; 5977 os_memcpy(capa, &drv->capa, sizeof(*capa)); 5978 return 0; 5979 } 5980 5981 5982 static int wpa_driver_nl80211_set_operstate(void *priv, int state) 5983 { 5984 struct i802_bss *bss = priv; 5985 struct wpa_driver_nl80211_data *drv = bss->drv; 5986 5987 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)", 5988 __func__, drv->operstate, state, state ? "UP" : "DORMANT"); 5989 drv->operstate = state; 5990 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1, 5991 state ? IF_OPER_UP : IF_OPER_DORMANT); 5992 } 5993 5994 5995 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized) 5996 { 5997 struct i802_bss *bss = priv; 5998 struct wpa_driver_nl80211_data *drv = bss->drv; 5999 struct nl_msg *msg; 6000 struct nl80211_sta_flag_update upd; 6001 6002 msg = nlmsg_alloc(); 6003 if (!msg) 6004 return -ENOMEM; 6005 6006 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION); 6007 6008 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, 6009 if_nametoindex(bss->ifname)); 6010 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid); 6011 6012 os_memset(&upd, 0, sizeof(upd)); 6013 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED); 6014 if (authorized) 6015 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED); 6016 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); 6017 6018 return send_and_recv_msgs(drv, msg, NULL, NULL); 6019 nla_put_failure: 6020 nlmsg_free(msg); 6021 return -ENOBUFS; 6022 } 6023 6024 6025 /* Set kernel driver on given frequency (MHz) */ 6026 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq) 6027 { 6028 struct i802_bss *bss = priv; 6029 struct wpa_driver_nl80211_data *drv = bss->drv; 6030 return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled, 6031 freq->sec_channel_offset); 6032 } 6033 6034 6035 #if defined(HOSTAPD) || defined(CONFIG_AP) 6036 6037 static inline int min_int(int a, int b) 6038 { 6039 if (a < b) 6040 return a; 6041 return b; 6042 } 6043 6044 6045 static int get_key_handler(struct nl_msg *msg, void *arg) 6046 { 6047 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 6048 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 6049 6050 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 6051 genlmsg_attrlen(gnlh, 0), NULL); 6052 6053 /* 6054 * TODO: validate the key index and mac address! 6055 * Otherwise, there's a race condition as soon as 6056 * the kernel starts sending key notifications. 6057 */ 6058 6059 if (tb[NL80211_ATTR_KEY_SEQ]) 6060 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]), 6061 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6)); 6062 return NL_SKIP; 6063 } 6064 6065 6066 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr, 6067 int idx, u8 *seq) 6068 { 6069 struct i802_bss *bss = priv; 6070 struct wpa_driver_nl80211_data *drv = bss->drv; 6071 struct nl_msg *msg; 6072 6073 msg = nlmsg_alloc(); 6074 if (!msg) 6075 return -ENOMEM; 6076 6077 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY); 6078 6079 if (addr) 6080 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 6081 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx); 6082 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface)); 6083 6084 memset(seq, 0, 6); 6085 6086 return send_and_recv_msgs(drv, msg, get_key_handler, seq); 6087 nla_put_failure: 6088 nlmsg_free(msg); 6089 return -ENOBUFS; 6090 } 6091 6092 6093 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates, 6094 int mode) 6095 { 6096 struct i802_bss *bss = priv; 6097 struct wpa_driver_nl80211_data *drv = bss->drv; 6098 struct nl_msg *msg; 6099 u8 rates[NL80211_MAX_SUPP_RATES]; 6100 u8 rates_len = 0; 6101 int i; 6102 6103 msg = nlmsg_alloc(); 6104 if (!msg) 6105 return -ENOMEM; 6106 6107 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS); 6108 6109 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++) 6110 rates[rates_len++] = basic_rates[i] / 5; 6111 6112 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates); 6113 6114 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 6115 6116 return send_and_recv_msgs(drv, msg, NULL, NULL); 6117 nla_put_failure: 6118 return -ENOBUFS; 6119 } 6120 6121 6122 static int i802_set_rts(void *priv, int rts) 6123 { 6124 struct i802_bss *bss = priv; 6125 struct wpa_driver_nl80211_data *drv = bss->drv; 6126 struct nl_msg *msg; 6127 int ret = -ENOBUFS; 6128 u32 val; 6129 6130 msg = nlmsg_alloc(); 6131 if (!msg) 6132 return -ENOMEM; 6133 6134 if (rts >= 2347) 6135 val = (u32) -1; 6136 else 6137 val = rts; 6138 6139 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY); 6140 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 6141 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val); 6142 6143 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 6144 msg = NULL; 6145 if (!ret) 6146 return 0; 6147 nla_put_failure: 6148 nlmsg_free(msg); 6149 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: " 6150 "%d (%s)", rts, ret, strerror(-ret)); 6151 return ret; 6152 } 6153 6154 6155 static int i802_set_frag(void *priv, int frag) 6156 { 6157 struct i802_bss *bss = priv; 6158 struct wpa_driver_nl80211_data *drv = bss->drv; 6159 struct nl_msg *msg; 6160 int ret = -ENOBUFS; 6161 u32 val; 6162 6163 msg = nlmsg_alloc(); 6164 if (!msg) 6165 return -ENOMEM; 6166 6167 if (frag >= 2346) 6168 val = (u32) -1; 6169 else 6170 val = frag; 6171 6172 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY); 6173 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 6174 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val); 6175 6176 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 6177 msg = NULL; 6178 if (!ret) 6179 return 0; 6180 nla_put_failure: 6181 nlmsg_free(msg); 6182 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold " 6183 "%d: %d (%s)", frag, ret, strerror(-ret)); 6184 return ret; 6185 } 6186 6187 6188 static int i802_flush(void *priv) 6189 { 6190 struct i802_bss *bss = priv; 6191 struct wpa_driver_nl80211_data *drv = bss->drv; 6192 struct nl_msg *msg; 6193 6194 msg = nlmsg_alloc(); 6195 if (!msg) 6196 return -1; 6197 6198 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION); 6199 6200 /* 6201 * XXX: FIX! this needs to flush all VLANs too 6202 */ 6203 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, 6204 if_nametoindex(bss->ifname)); 6205 6206 return send_and_recv_msgs(drv, msg, NULL, NULL); 6207 nla_put_failure: 6208 nlmsg_free(msg); 6209 return -ENOBUFS; 6210 } 6211 6212 6213 static int get_sta_handler(struct nl_msg *msg, void *arg) 6214 { 6215 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 6216 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 6217 struct hostap_sta_driver_data *data = arg; 6218 struct nlattr *stats[NL80211_STA_INFO_MAX + 1]; 6219 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { 6220 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, 6221 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, 6222 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, 6223 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, 6224 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, 6225 }; 6226 6227 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 6228 genlmsg_attrlen(gnlh, 0), NULL); 6229 6230 /* 6231 * TODO: validate the interface and mac address! 6232 * Otherwise, there's a race condition as soon as 6233 * the kernel starts sending station notifications. 6234 */ 6235 6236 if (!tb[NL80211_ATTR_STA_INFO]) { 6237 wpa_printf(MSG_DEBUG, "sta stats missing!"); 6238 return NL_SKIP; 6239 } 6240 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX, 6241 tb[NL80211_ATTR_STA_INFO], 6242 stats_policy)) { 6243 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!"); 6244 return NL_SKIP; 6245 } 6246 6247 if (stats[NL80211_STA_INFO_INACTIVE_TIME]) 6248 data->inactive_msec = 6249 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]); 6250 if (stats[NL80211_STA_INFO_RX_BYTES]) 6251 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]); 6252 if (stats[NL80211_STA_INFO_TX_BYTES]) 6253 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]); 6254 if (stats[NL80211_STA_INFO_RX_PACKETS]) 6255 data->rx_packets = 6256 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]); 6257 if (stats[NL80211_STA_INFO_TX_PACKETS]) 6258 data->tx_packets = 6259 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]); 6260 6261 return NL_SKIP; 6262 } 6263 6264 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data, 6265 const u8 *addr) 6266 { 6267 struct i802_bss *bss = priv; 6268 struct wpa_driver_nl80211_data *drv = bss->drv; 6269 struct nl_msg *msg; 6270 6271 os_memset(data, 0, sizeof(*data)); 6272 msg = nlmsg_alloc(); 6273 if (!msg) 6274 return -ENOMEM; 6275 6276 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION); 6277 6278 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 6279 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 6280 6281 return send_and_recv_msgs(drv, msg, get_sta_handler, data); 6282 nla_put_failure: 6283 nlmsg_free(msg); 6284 return -ENOBUFS; 6285 } 6286 6287 6288 static int i802_set_tx_queue_params(void *priv, int queue, int aifs, 6289 int cw_min, int cw_max, int burst_time) 6290 { 6291 struct i802_bss *bss = priv; 6292 struct wpa_driver_nl80211_data *drv = bss->drv; 6293 struct nl_msg *msg; 6294 struct nlattr *txq, *params; 6295 6296 msg = nlmsg_alloc(); 6297 if (!msg) 6298 return -1; 6299 6300 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY); 6301 6302 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 6303 6304 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS); 6305 if (!txq) 6306 goto nla_put_failure; 6307 6308 /* We are only sending parameters for a single TXQ at a time */ 6309 params = nla_nest_start(msg, 1); 6310 if (!params) 6311 goto nla_put_failure; 6312 6313 switch (queue) { 6314 case 0: 6315 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO); 6316 break; 6317 case 1: 6318 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI); 6319 break; 6320 case 2: 6321 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE); 6322 break; 6323 case 3: 6324 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK); 6325 break; 6326 } 6327 /* Burst time is configured in units of 0.1 msec and TXOP parameter in 6328 * 32 usec, so need to convert the value here. */ 6329 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32); 6330 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min); 6331 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max); 6332 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs); 6333 6334 nla_nest_end(msg, params); 6335 6336 nla_nest_end(msg, txq); 6337 6338 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0) 6339 return 0; 6340 msg = NULL; 6341 nla_put_failure: 6342 nlmsg_free(msg); 6343 return -1; 6344 } 6345 6346 6347 static int i802_set_sta_vlan(void *priv, const u8 *addr, 6348 const char *ifname, int vlan_id) 6349 { 6350 struct i802_bss *bss = priv; 6351 struct wpa_driver_nl80211_data *drv = bss->drv; 6352 struct nl_msg *msg; 6353 int ret = -ENOBUFS; 6354 6355 msg = nlmsg_alloc(); 6356 if (!msg) 6357 return -ENOMEM; 6358 6359 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION); 6360 6361 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, 6362 if_nametoindex(bss->ifname)); 6363 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 6364 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN, 6365 if_nametoindex(ifname)); 6366 6367 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 6368 msg = NULL; 6369 if (ret < 0) { 6370 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr=" 6371 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)", 6372 MAC2STR(addr), ifname, vlan_id, ret, 6373 strerror(-ret)); 6374 } 6375 nla_put_failure: 6376 nlmsg_free(msg); 6377 return ret; 6378 } 6379 6380 6381 static int i802_get_inact_sec(void *priv, const u8 *addr) 6382 { 6383 struct hostap_sta_driver_data data; 6384 int ret; 6385 6386 data.inactive_msec = (unsigned long) -1; 6387 ret = i802_read_sta_data(priv, &data, addr); 6388 if (ret || data.inactive_msec == (unsigned long) -1) 6389 return -1; 6390 return data.inactive_msec / 1000; 6391 } 6392 6393 6394 static int i802_sta_clear_stats(void *priv, const u8 *addr) 6395 { 6396 #if 0 6397 /* TODO */ 6398 #endif 6399 return 0; 6400 } 6401 6402 6403 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, 6404 int reason) 6405 { 6406 struct i802_bss *bss = priv; 6407 struct ieee80211_mgmt mgmt; 6408 6409 memset(&mgmt, 0, sizeof(mgmt)); 6410 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, 6411 WLAN_FC_STYPE_DEAUTH); 6412 memcpy(mgmt.da, addr, ETH_ALEN); 6413 memcpy(mgmt.sa, own_addr, ETH_ALEN); 6414 memcpy(mgmt.bssid, own_addr, ETH_ALEN); 6415 mgmt.u.deauth.reason_code = host_to_le16(reason); 6416 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt, 6417 IEEE80211_HDRLEN + 6418 sizeof(mgmt.u.deauth)); 6419 } 6420 6421 6422 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr, 6423 int reason) 6424 { 6425 struct i802_bss *bss = priv; 6426 struct ieee80211_mgmt mgmt; 6427 6428 memset(&mgmt, 0, sizeof(mgmt)); 6429 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, 6430 WLAN_FC_STYPE_DISASSOC); 6431 memcpy(mgmt.da, addr, ETH_ALEN); 6432 memcpy(mgmt.sa, own_addr, ETH_ALEN); 6433 memcpy(mgmt.bssid, own_addr, ETH_ALEN); 6434 mgmt.u.disassoc.reason_code = host_to_le16(reason); 6435 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt, 6436 IEEE80211_HDRLEN + 6437 sizeof(mgmt.u.disassoc)); 6438 } 6439 6440 #endif /* HOSTAPD || CONFIG_AP */ 6441 6442 #ifdef HOSTAPD 6443 6444 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 6445 { 6446 int i; 6447 int *old; 6448 6449 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d", 6450 ifidx); 6451 for (i = 0; i < drv->num_if_indices; i++) { 6452 if (drv->if_indices[i] == 0) { 6453 drv->if_indices[i] = ifidx; 6454 return; 6455 } 6456 } 6457 6458 if (drv->if_indices != drv->default_if_indices) 6459 old = drv->if_indices; 6460 else 6461 old = NULL; 6462 6463 drv->if_indices = os_realloc(old, 6464 sizeof(int) * (drv->num_if_indices + 1)); 6465 if (!drv->if_indices) { 6466 if (!old) 6467 drv->if_indices = drv->default_if_indices; 6468 else 6469 drv->if_indices = old; 6470 wpa_printf(MSG_ERROR, "Failed to reallocate memory for " 6471 "interfaces"); 6472 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx); 6473 return; 6474 } else if (!old) 6475 os_memcpy(drv->if_indices, drv->default_if_indices, 6476 sizeof(drv->default_if_indices)); 6477 drv->if_indices[drv->num_if_indices] = ifidx; 6478 drv->num_if_indices++; 6479 } 6480 6481 6482 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 6483 { 6484 int i; 6485 6486 for (i = 0; i < drv->num_if_indices; i++) { 6487 if (drv->if_indices[i] == ifidx) { 6488 drv->if_indices[i] = 0; 6489 break; 6490 } 6491 } 6492 } 6493 6494 6495 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 6496 { 6497 int i; 6498 6499 for (i = 0; i < drv->num_if_indices; i++) 6500 if (drv->if_indices[i] == ifidx) 6501 return 1; 6502 6503 return 0; 6504 } 6505 6506 6507 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val, 6508 const char *bridge_ifname) 6509 { 6510 struct i802_bss *bss = priv; 6511 struct wpa_driver_nl80211_data *drv = bss->drv; 6512 char name[IFNAMSIZ + 1]; 6513 6514 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid); 6515 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR 6516 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name); 6517 if (val) { 6518 if (!if_nametoindex(name)) { 6519 if (nl80211_create_iface(drv, name, 6520 NL80211_IFTYPE_AP_VLAN, 6521 NULL, 1) < 0) 6522 return -1; 6523 if (bridge_ifname && 6524 linux_br_add_if(drv->global->ioctl_sock, 6525 bridge_ifname, name) < 0) 6526 return -1; 6527 } 6528 linux_set_iface_flags(drv->global->ioctl_sock, name, 1); 6529 return i802_set_sta_vlan(priv, addr, name, 0); 6530 } else { 6531 i802_set_sta_vlan(priv, addr, bss->ifname, 0); 6532 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN, 6533 name); 6534 } 6535 } 6536 6537 6538 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx) 6539 { 6540 struct wpa_driver_nl80211_data *drv = eloop_ctx; 6541 struct sockaddr_ll lladdr; 6542 unsigned char buf[3000]; 6543 int len; 6544 socklen_t fromlen = sizeof(lladdr); 6545 6546 len = recvfrom(sock, buf, sizeof(buf), 0, 6547 (struct sockaddr *)&lladdr, &fromlen); 6548 if (len < 0) { 6549 perror("recv"); 6550 return; 6551 } 6552 6553 if (have_ifidx(drv, lladdr.sll_ifindex)) 6554 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len); 6555 } 6556 6557 6558 static int i802_check_bridge(struct wpa_driver_nl80211_data *drv, 6559 struct i802_bss *bss, 6560 const char *brname, const char *ifname) 6561 { 6562 int ifindex; 6563 char in_br[IFNAMSIZ]; 6564 6565 os_strlcpy(bss->brname, brname, IFNAMSIZ); 6566 ifindex = if_nametoindex(brname); 6567 if (ifindex == 0) { 6568 /* 6569 * Bridge was configured, but the bridge device does 6570 * not exist. Try to add it now. 6571 */ 6572 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) { 6573 wpa_printf(MSG_ERROR, "nl80211: Failed to add the " 6574 "bridge interface %s: %s", 6575 brname, strerror(errno)); 6576 return -1; 6577 } 6578 bss->added_bridge = 1; 6579 add_ifidx(drv, if_nametoindex(brname)); 6580 } 6581 6582 if (linux_br_get(in_br, ifname) == 0) { 6583 if (os_strcmp(in_br, brname) == 0) 6584 return 0; /* already in the bridge */ 6585 6586 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from " 6587 "bridge %s", ifname, in_br); 6588 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) < 6589 0) { 6590 wpa_printf(MSG_ERROR, "nl80211: Failed to " 6591 "remove interface %s from bridge " 6592 "%s: %s", 6593 ifname, brname, strerror(errno)); 6594 return -1; 6595 } 6596 } 6597 6598 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s", 6599 ifname, brname); 6600 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) { 6601 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s " 6602 "into bridge %s: %s", 6603 ifname, brname, strerror(errno)); 6604 return -1; 6605 } 6606 bss->added_if_into_bridge = 1; 6607 6608 return 0; 6609 } 6610 6611 6612 static void *i802_init(struct hostapd_data *hapd, 6613 struct wpa_init_params *params) 6614 { 6615 struct wpa_driver_nl80211_data *drv; 6616 struct i802_bss *bss; 6617 size_t i; 6618 char brname[IFNAMSIZ]; 6619 int ifindex, br_ifindex; 6620 int br_added = 0; 6621 6622 bss = wpa_driver_nl80211_init(hapd, params->ifname, 6623 params->global_priv); 6624 if (bss == NULL) 6625 return NULL; 6626 6627 drv = bss->drv; 6628 drv->nlmode = NL80211_IFTYPE_AP; 6629 drv->eapol_sock = -1; 6630 6631 if (linux_br_get(brname, params->ifname) == 0) { 6632 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s", 6633 params->ifname, brname); 6634 br_ifindex = if_nametoindex(brname); 6635 } else { 6636 brname[0] = '\0'; 6637 br_ifindex = 0; 6638 } 6639 6640 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int); 6641 drv->if_indices = drv->default_if_indices; 6642 for (i = 0; i < params->num_bridge; i++) { 6643 if (params->bridge[i]) { 6644 ifindex = if_nametoindex(params->bridge[i]); 6645 if (ifindex) 6646 add_ifidx(drv, ifindex); 6647 if (ifindex == br_ifindex) 6648 br_added = 1; 6649 } 6650 } 6651 if (!br_added && br_ifindex && 6652 (params->num_bridge == 0 || !params->bridge[0])) 6653 add_ifidx(drv, br_ifindex); 6654 6655 /* start listening for EAPOL on the default AP interface */ 6656 add_ifidx(drv, drv->ifindex); 6657 6658 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0)) 6659 goto failed; 6660 6661 if (params->bssid) { 6662 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 6663 params->bssid)) 6664 goto failed; 6665 } 6666 6667 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) { 6668 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s " 6669 "into AP mode", bss->ifname); 6670 goto failed; 6671 } 6672 6673 if (params->num_bridge && params->bridge[0] && 6674 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0) 6675 goto failed; 6676 6677 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) 6678 goto failed; 6679 6680 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE)); 6681 if (drv->eapol_sock < 0) { 6682 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)"); 6683 goto failed; 6684 } 6685 6686 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL)) 6687 { 6688 printf("Could not register read socket for eapol\n"); 6689 goto failed; 6690 } 6691 6692 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 6693 params->own_addr)) 6694 goto failed; 6695 6696 return bss; 6697 6698 failed: 6699 wpa_driver_nl80211_deinit(bss); 6700 return NULL; 6701 } 6702 6703 6704 static void i802_deinit(void *priv) 6705 { 6706 wpa_driver_nl80211_deinit(priv); 6707 } 6708 6709 #endif /* HOSTAPD */ 6710 6711 6712 static enum nl80211_iftype wpa_driver_nl80211_if_type( 6713 enum wpa_driver_if_type type) 6714 { 6715 switch (type) { 6716 case WPA_IF_STATION: 6717 return NL80211_IFTYPE_STATION; 6718 case WPA_IF_P2P_CLIENT: 6719 case WPA_IF_P2P_GROUP: 6720 return NL80211_IFTYPE_P2P_CLIENT; 6721 case WPA_IF_AP_VLAN: 6722 return NL80211_IFTYPE_AP_VLAN; 6723 case WPA_IF_AP_BSS: 6724 return NL80211_IFTYPE_AP; 6725 case WPA_IF_P2P_GO: 6726 return NL80211_IFTYPE_P2P_GO; 6727 } 6728 return -1; 6729 } 6730 6731 6732 #ifdef CONFIG_P2P 6733 6734 static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr) 6735 { 6736 struct wpa_driver_nl80211_data *drv; 6737 dl_list_for_each(drv, &global->interfaces, 6738 struct wpa_driver_nl80211_data, list) { 6739 if (os_memcmp(addr, drv->addr, ETH_ALEN) == 0) 6740 return 1; 6741 } 6742 return 0; 6743 } 6744 6745 6746 static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv, 6747 u8 *new_addr) 6748 { 6749 unsigned int idx; 6750 6751 if (!drv->global) 6752 return -1; 6753 6754 os_memcpy(new_addr, drv->addr, ETH_ALEN); 6755 for (idx = 0; idx < 64; idx++) { 6756 new_addr[0] = drv->addr[0] | 0x02; 6757 new_addr[0] ^= idx << 2; 6758 if (!nl80211_addr_in_use(drv->global, new_addr)) 6759 break; 6760 } 6761 if (idx == 64) 6762 return -1; 6763 6764 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address " 6765 MACSTR, MAC2STR(new_addr)); 6766 6767 return 0; 6768 } 6769 6770 #endif /* CONFIG_P2P */ 6771 6772 6773 static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type, 6774 const char *ifname, const u8 *addr, 6775 void *bss_ctx, void **drv_priv, 6776 char *force_ifname, u8 *if_addr, 6777 const char *bridge) 6778 { 6779 struct i802_bss *bss = priv; 6780 struct wpa_driver_nl80211_data *drv = bss->drv; 6781 int ifidx; 6782 #ifdef HOSTAPD 6783 struct i802_bss *new_bss = NULL; 6784 6785 if (type == WPA_IF_AP_BSS) { 6786 new_bss = os_zalloc(sizeof(*new_bss)); 6787 if (new_bss == NULL) 6788 return -1; 6789 } 6790 #endif /* HOSTAPD */ 6791 6792 if (addr) 6793 os_memcpy(if_addr, addr, ETH_ALEN); 6794 ifidx = nl80211_create_iface(drv, ifname, 6795 wpa_driver_nl80211_if_type(type), addr, 6796 0); 6797 if (ifidx < 0) { 6798 #ifdef HOSTAPD 6799 os_free(new_bss); 6800 #endif /* HOSTAPD */ 6801 return -1; 6802 } 6803 6804 if (!addr && 6805 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 6806 if_addr) < 0) { 6807 nl80211_remove_iface(drv, ifidx); 6808 return -1; 6809 } 6810 6811 #ifdef CONFIG_P2P 6812 if (!addr && 6813 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP || 6814 type == WPA_IF_P2P_GO)) { 6815 /* Enforce unique P2P Interface Address */ 6816 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN]; 6817 6818 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 6819 own_addr) < 0 || 6820 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname, 6821 new_addr) < 0) { 6822 nl80211_remove_iface(drv, ifidx); 6823 return -1; 6824 } 6825 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) { 6826 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address " 6827 "for P2P group interface"); 6828 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) { 6829 nl80211_remove_iface(drv, ifidx); 6830 return -1; 6831 } 6832 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, 6833 new_addr) < 0) { 6834 nl80211_remove_iface(drv, ifidx); 6835 return -1; 6836 } 6837 } 6838 os_memcpy(if_addr, new_addr, ETH_ALEN); 6839 } 6840 #endif /* CONFIG_P2P */ 6841 6842 #ifdef HOSTAPD 6843 if (bridge && 6844 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) { 6845 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new " 6846 "interface %s to a bridge %s", ifname, bridge); 6847 nl80211_remove_iface(drv, ifidx); 6848 os_free(new_bss); 6849 return -1; 6850 } 6851 6852 if (type == WPA_IF_AP_BSS) { 6853 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1)) 6854 { 6855 nl80211_remove_iface(drv, ifidx); 6856 os_free(new_bss); 6857 return -1; 6858 } 6859 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ); 6860 new_bss->ifindex = ifidx; 6861 new_bss->drv = drv; 6862 new_bss->next = drv->first_bss.next; 6863 drv->first_bss.next = new_bss; 6864 if (drv_priv) 6865 *drv_priv = new_bss; 6866 } 6867 #endif /* HOSTAPD */ 6868 6869 if (drv->global) 6870 drv->global->if_add_ifindex = ifidx; 6871 6872 return 0; 6873 } 6874 6875 6876 static int wpa_driver_nl80211_if_remove(void *priv, 6877 enum wpa_driver_if_type type, 6878 const char *ifname) 6879 { 6880 struct i802_bss *bss = priv; 6881 struct wpa_driver_nl80211_data *drv = bss->drv; 6882 int ifindex = if_nametoindex(ifname); 6883 6884 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d", 6885 __func__, type, ifname, ifindex); 6886 if (ifindex <= 0) 6887 return -1; 6888 6889 #ifdef HOSTAPD 6890 if (bss->added_if_into_bridge) { 6891 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname, 6892 bss->ifname) < 0) 6893 wpa_printf(MSG_INFO, "nl80211: Failed to remove " 6894 "interface %s from bridge %s: %s", 6895 bss->ifname, bss->brname, strerror(errno)); 6896 } 6897 if (bss->added_bridge) { 6898 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0) 6899 wpa_printf(MSG_INFO, "nl80211: Failed to remove " 6900 "bridge %s: %s", 6901 bss->brname, strerror(errno)); 6902 } 6903 #endif /* HOSTAPD */ 6904 6905 nl80211_remove_iface(drv, ifindex); 6906 6907 #ifdef HOSTAPD 6908 if (type != WPA_IF_AP_BSS) 6909 return 0; 6910 6911 if (bss != &drv->first_bss) { 6912 struct i802_bss *tbss; 6913 6914 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) { 6915 if (tbss->next == bss) { 6916 tbss->next = bss->next; 6917 os_free(bss); 6918 bss = NULL; 6919 break; 6920 } 6921 } 6922 if (bss) 6923 wpa_printf(MSG_INFO, "nl80211: %s - could not find " 6924 "BSS %p in the list", __func__, bss); 6925 } 6926 #endif /* HOSTAPD */ 6927 6928 return 0; 6929 } 6930 6931 6932 static int cookie_handler(struct nl_msg *msg, void *arg) 6933 { 6934 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 6935 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 6936 u64 *cookie = arg; 6937 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 6938 genlmsg_attrlen(gnlh, 0), NULL); 6939 if (tb[NL80211_ATTR_COOKIE]) 6940 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]); 6941 return NL_SKIP; 6942 } 6943 6944 6945 static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv, 6946 unsigned int freq, unsigned int wait, 6947 const u8 *buf, size_t buf_len, 6948 u64 *cookie_out, int no_cck) 6949 { 6950 struct nl_msg *msg; 6951 u64 cookie; 6952 int ret = -1; 6953 6954 msg = nlmsg_alloc(); 6955 if (!msg) 6956 return -1; 6957 6958 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME); 6959 6960 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 6961 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); 6962 if (wait) 6963 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait); 6964 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK); 6965 if (no_cck) 6966 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE); 6967 6968 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf); 6969 6970 cookie = 0; 6971 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie); 6972 msg = NULL; 6973 if (ret) { 6974 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d " 6975 "(%s) (freq=%u wait=%u)", ret, strerror(-ret), 6976 freq, wait); 6977 goto nla_put_failure; 6978 } 6979 wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted; " 6980 "cookie 0x%llx", (long long unsigned int) cookie); 6981 6982 if (cookie_out) 6983 *cookie_out = cookie; 6984 6985 nla_put_failure: 6986 nlmsg_free(msg); 6987 return ret; 6988 } 6989 6990 6991 static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq, 6992 unsigned int wait_time, 6993 const u8 *dst, const u8 *src, 6994 const u8 *bssid, 6995 const u8 *data, size_t data_len, 6996 int no_cck) 6997 { 6998 struct i802_bss *bss = priv; 6999 struct wpa_driver_nl80211_data *drv = bss->drv; 7000 int ret = -1; 7001 u8 *buf; 7002 struct ieee80211_hdr *hdr; 7003 7004 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, " 7005 "wait=%d ms no_cck=%d)", drv->ifindex, wait_time, no_cck); 7006 7007 buf = os_zalloc(24 + data_len); 7008 if (buf == NULL) 7009 return ret; 7010 os_memcpy(buf + 24, data, data_len); 7011 hdr = (struct ieee80211_hdr *) buf; 7012 hdr->frame_control = 7013 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION); 7014 os_memcpy(hdr->addr1, dst, ETH_ALEN); 7015 os_memcpy(hdr->addr2, src, ETH_ALEN); 7016 os_memcpy(hdr->addr3, bssid, ETH_ALEN); 7017 7018 if (is_ap_interface(drv->nlmode)) 7019 ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len); 7020 else 7021 ret = nl80211_send_frame_cmd(drv, freq, wait_time, buf, 7022 24 + data_len, 7023 &drv->send_action_cookie, 7024 no_cck); 7025 7026 os_free(buf); 7027 return ret; 7028 } 7029 7030 7031 static void wpa_driver_nl80211_send_action_cancel_wait(void *priv) 7032 { 7033 struct i802_bss *bss = priv; 7034 struct wpa_driver_nl80211_data *drv = bss->drv; 7035 struct nl_msg *msg; 7036 int ret; 7037 7038 msg = nlmsg_alloc(); 7039 if (!msg) 7040 return; 7041 7042 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL); 7043 7044 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 7045 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie); 7046 7047 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 7048 msg = NULL; 7049 if (ret) 7050 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d " 7051 "(%s)", ret, strerror(-ret)); 7052 7053 nla_put_failure: 7054 nlmsg_free(msg); 7055 } 7056 7057 7058 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq, 7059 unsigned int duration) 7060 { 7061 struct i802_bss *bss = priv; 7062 struct wpa_driver_nl80211_data *drv = bss->drv; 7063 struct nl_msg *msg; 7064 int ret; 7065 u64 cookie; 7066 7067 msg = nlmsg_alloc(); 7068 if (!msg) 7069 return -1; 7070 7071 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL); 7072 7073 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 7074 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); 7075 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration); 7076 7077 cookie = 0; 7078 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie); 7079 msg = NULL; 7080 if (ret == 0) { 7081 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie " 7082 "0x%llx for freq=%u MHz duration=%u", 7083 (long long unsigned int) cookie, freq, duration); 7084 drv->remain_on_chan_cookie = cookie; 7085 drv->pending_remain_on_chan = 1; 7086 return 0; 7087 } 7088 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel " 7089 "(freq=%d duration=%u): %d (%s)", 7090 freq, duration, ret, strerror(-ret)); 7091 nla_put_failure: 7092 nlmsg_free(msg); 7093 return -1; 7094 } 7095 7096 7097 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv) 7098 { 7099 struct i802_bss *bss = priv; 7100 struct wpa_driver_nl80211_data *drv = bss->drv; 7101 struct nl_msg *msg; 7102 int ret; 7103 7104 if (!drv->pending_remain_on_chan) { 7105 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel " 7106 "to cancel"); 7107 return -1; 7108 } 7109 7110 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie " 7111 "0x%llx", 7112 (long long unsigned int) drv->remain_on_chan_cookie); 7113 7114 msg = nlmsg_alloc(); 7115 if (!msg) 7116 return -1; 7117 7118 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL); 7119 7120 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 7121 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie); 7122 7123 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 7124 msg = NULL; 7125 if (ret == 0) 7126 return 0; 7127 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: " 7128 "%d (%s)", ret, strerror(-ret)); 7129 nla_put_failure: 7130 nlmsg_free(msg); 7131 return -1; 7132 } 7133 7134 7135 static int wpa_driver_nl80211_probe_req_report(void *priv, int report) 7136 { 7137 struct i802_bss *bss = priv; 7138 struct wpa_driver_nl80211_data *drv = bss->drv; 7139 7140 if (!report) { 7141 if (drv->nl_preq.handle && drv->no_monitor_iface_capab && 7142 is_ap_interface(drv->nlmode)) { 7143 /* 7144 * Do not disable Probe Request reporting that was 7145 * enabled in nl80211_setup_ap(). 7146 */ 7147 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of " 7148 "Probe Request reporting nl_preq=%p while " 7149 "in AP mode", drv->nl_preq.handle); 7150 } else if (drv->nl_preq.handle) { 7151 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request " 7152 "reporting nl_preq=%p", drv->nl_preq.handle); 7153 eloop_unregister_read_sock( 7154 nl_socket_get_fd(drv->nl_preq.handle)); 7155 nl_destroy_handles(&drv->nl_preq); 7156 } 7157 return 0; 7158 } 7159 7160 if (drv->nl_preq.handle) { 7161 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting " 7162 "already on!"); 7163 return 0; 7164 } 7165 7166 if (nl_create_handles(&drv->nl_preq, drv->global->nl_cb, "preq")) 7167 return -1; 7168 7169 if (nl80211_register_frame(drv, drv->nl_preq.handle, 7170 (WLAN_FC_TYPE_MGMT << 2) | 7171 (WLAN_FC_STYPE_PROBE_REQ << 4), 7172 NULL, 0) < 0) 7173 goto out_err; 7174 7175 eloop_register_read_sock(nl_socket_get_fd(drv->nl_preq.handle), 7176 wpa_driver_nl80211_event_receive, drv, 7177 drv->nl_preq.handle); 7178 7179 return 0; 7180 7181 out_err: 7182 nl_destroy_handles(&drv->nl_preq); 7183 return -1; 7184 } 7185 7186 7187 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv, 7188 int ifindex, int disabled) 7189 { 7190 struct nl_msg *msg; 7191 struct nlattr *bands, *band; 7192 int ret; 7193 7194 msg = nlmsg_alloc(); 7195 if (!msg) 7196 return -1; 7197 7198 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK); 7199 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 7200 7201 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES); 7202 if (!bands) 7203 goto nla_put_failure; 7204 7205 /* 7206 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything 7207 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS 7208 * rates. All 5 GHz rates are left enabled. 7209 */ 7210 band = nla_nest_start(msg, NL80211_BAND_2GHZ); 7211 if (!band) 7212 goto nla_put_failure; 7213 if (disabled) { 7214 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8, 7215 "\x0c\x12\x18\x24\x30\x48\x60\x6c"); 7216 } 7217 nla_nest_end(msg, band); 7218 7219 nla_nest_end(msg, bands); 7220 7221 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 7222 msg = NULL; 7223 if (ret) { 7224 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d " 7225 "(%s)", ret, strerror(-ret)); 7226 } 7227 7228 return ret; 7229 7230 nla_put_failure: 7231 nlmsg_free(msg); 7232 return -1; 7233 } 7234 7235 7236 static int wpa_driver_nl80211_deinit_ap(void *priv) 7237 { 7238 struct i802_bss *bss = priv; 7239 struct wpa_driver_nl80211_data *drv = bss->drv; 7240 if (!is_ap_interface(drv->nlmode)) 7241 return -1; 7242 wpa_driver_nl80211_del_beacon(drv); 7243 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION); 7244 } 7245 7246 7247 static int wpa_driver_nl80211_deinit_p2p_cli(void *priv) 7248 { 7249 struct i802_bss *bss = priv; 7250 struct wpa_driver_nl80211_data *drv = bss->drv; 7251 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT) 7252 return -1; 7253 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION); 7254 } 7255 7256 7257 static void wpa_driver_nl80211_resume(void *priv) 7258 { 7259 struct i802_bss *bss = priv; 7260 struct wpa_driver_nl80211_data *drv = bss->drv; 7261 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) { 7262 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on " 7263 "resume event"); 7264 } 7265 } 7266 7267 7268 static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap, 7269 const u8 *ies, size_t ies_len) 7270 { 7271 struct i802_bss *bss = priv; 7272 struct wpa_driver_nl80211_data *drv = bss->drv; 7273 int ret; 7274 u8 *data, *pos; 7275 size_t data_len; 7276 u8 own_addr[ETH_ALEN]; 7277 7278 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 7279 own_addr) < 0) 7280 return -1; 7281 7282 if (action != 1) { 7283 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action " 7284 "action %d", action); 7285 return -1; 7286 } 7287 7288 /* 7289 * Action frame payload: 7290 * Category[1] = 6 (Fast BSS Transition) 7291 * Action[1] = 1 (Fast BSS Transition Request) 7292 * STA Address 7293 * Target AP Address 7294 * FT IEs 7295 */ 7296 7297 data_len = 2 + 2 * ETH_ALEN + ies_len; 7298 data = os_malloc(data_len); 7299 if (data == NULL) 7300 return -1; 7301 pos = data; 7302 *pos++ = 0x06; /* FT Action category */ 7303 *pos++ = action; 7304 os_memcpy(pos, own_addr, ETH_ALEN); 7305 pos += ETH_ALEN; 7306 os_memcpy(pos, target_ap, ETH_ALEN); 7307 pos += ETH_ALEN; 7308 os_memcpy(pos, ies, ies_len); 7309 7310 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0, 7311 drv->bssid, own_addr, drv->bssid, 7312 data, data_len, 0); 7313 os_free(data); 7314 7315 return ret; 7316 } 7317 7318 7319 static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis) 7320 { 7321 struct i802_bss *bss = priv; 7322 struct wpa_driver_nl80211_data *drv = bss->drv; 7323 struct nl_msg *msg, *cqm = NULL; 7324 7325 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d " 7326 "hysteresis=%d", threshold, hysteresis); 7327 7328 msg = nlmsg_alloc(); 7329 if (!msg) 7330 return -1; 7331 7332 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM); 7333 7334 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 7335 7336 cqm = nlmsg_alloc(); 7337 if (cqm == NULL) 7338 return -1; 7339 7340 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold); 7341 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis); 7342 nla_put_nested(msg, NL80211_ATTR_CQM, cqm); 7343 7344 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0) 7345 return 0; 7346 msg = NULL; 7347 7348 nla_put_failure: 7349 nlmsg_free(cqm); 7350 nlmsg_free(msg); 7351 return -1; 7352 } 7353 7354 7355 static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si) 7356 { 7357 struct i802_bss *bss = priv; 7358 struct wpa_driver_nl80211_data *drv = bss->drv; 7359 int res; 7360 7361 os_memset(si, 0, sizeof(*si)); 7362 res = nl80211_get_link_signal(drv, si); 7363 if (res != 0) 7364 return res; 7365 7366 return nl80211_get_link_noise(drv, si); 7367 } 7368 7369 7370 static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len, 7371 int encrypt) 7372 { 7373 struct i802_bss *bss = priv; 7374 struct wpa_driver_nl80211_data *drv = bss->drv; 7375 return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt); 7376 } 7377 7378 7379 static int nl80211_set_param(void *priv, const char *param) 7380 { 7381 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param); 7382 if (param == NULL) 7383 return 0; 7384 7385 #ifdef CONFIG_P2P 7386 if (os_strstr(param, "use_p2p_group_interface=1")) { 7387 struct i802_bss *bss = priv; 7388 struct wpa_driver_nl80211_data *drv = bss->drv; 7389 7390 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group " 7391 "interface"); 7392 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT; 7393 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P; 7394 } 7395 #endif /* CONFIG_P2P */ 7396 7397 return 0; 7398 } 7399 7400 7401 static void * nl80211_global_init(void) 7402 { 7403 struct nl80211_global *global; 7404 struct netlink_config *cfg; 7405 7406 global = os_zalloc(sizeof(*global)); 7407 if (global == NULL) 7408 return NULL; 7409 global->ioctl_sock = -1; 7410 dl_list_init(&global->interfaces); 7411 global->if_add_ifindex = -1; 7412 7413 cfg = os_zalloc(sizeof(*cfg)); 7414 if (cfg == NULL) 7415 goto err; 7416 7417 cfg->ctx = global; 7418 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink; 7419 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink; 7420 global->netlink = netlink_init(cfg); 7421 if (global->netlink == NULL) { 7422 os_free(cfg); 7423 goto err; 7424 } 7425 7426 if (wpa_driver_nl80211_init_nl_global(global) < 0) 7427 goto err; 7428 7429 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0); 7430 if (global->ioctl_sock < 0) { 7431 perror("socket(PF_INET,SOCK_DGRAM)"); 7432 goto err; 7433 } 7434 7435 return global; 7436 7437 err: 7438 nl80211_global_deinit(global); 7439 return NULL; 7440 } 7441 7442 7443 static void nl80211_global_deinit(void *priv) 7444 { 7445 struct nl80211_global *global = priv; 7446 if (global == NULL) 7447 return; 7448 if (!dl_list_empty(&global->interfaces)) { 7449 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at " 7450 "nl80211_global_deinit", 7451 dl_list_len(&global->interfaces)); 7452 } 7453 7454 if (global->netlink) 7455 netlink_deinit(global->netlink); 7456 7457 if (global->nl80211) 7458 genl_family_put(global->nl80211); 7459 nl_destroy_handles(&global->nl); 7460 7461 if (global->nl_cb) 7462 nl_cb_put(global->nl_cb); 7463 7464 if (global->ioctl_sock >= 0) 7465 close(global->ioctl_sock); 7466 7467 os_free(global); 7468 } 7469 7470 7471 static const char * nl80211_get_radio_name(void *priv) 7472 { 7473 struct i802_bss *bss = priv; 7474 struct wpa_driver_nl80211_data *drv = bss->drv; 7475 return drv->phyname; 7476 } 7477 7478 7479 static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid, 7480 const u8 *pmkid) 7481 { 7482 struct nl_msg *msg; 7483 7484 msg = nlmsg_alloc(); 7485 if (!msg) 7486 return -ENOMEM; 7487 7488 nl80211_cmd(bss->drv, msg, 0, cmd); 7489 7490 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 7491 if (pmkid) 7492 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid); 7493 if (bssid) 7494 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid); 7495 7496 return send_and_recv_msgs(bss->drv, msg, NULL, NULL); 7497 nla_put_failure: 7498 nlmsg_free(msg); 7499 return -ENOBUFS; 7500 } 7501 7502 7503 static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid) 7504 { 7505 struct i802_bss *bss = priv; 7506 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid)); 7507 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid); 7508 } 7509 7510 7511 static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid) 7512 { 7513 struct i802_bss *bss = priv; 7514 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR, 7515 MAC2STR(bssid)); 7516 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid); 7517 } 7518 7519 7520 static int nl80211_flush_pmkid(void *priv) 7521 { 7522 struct i802_bss *bss = priv; 7523 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs"); 7524 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL); 7525 } 7526 7527 7528 static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck, 7529 const u8 *replay_ctr) 7530 { 7531 struct i802_bss *bss = priv; 7532 struct wpa_driver_nl80211_data *drv = bss->drv; 7533 struct nlattr *replay_nested; 7534 struct nl_msg *msg; 7535 7536 msg = nlmsg_alloc(); 7537 if (!msg) 7538 return; 7539 7540 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD); 7541 7542 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 7543 7544 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA); 7545 if (!replay_nested) 7546 goto nla_put_failure; 7547 7548 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek); 7549 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck); 7550 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN, 7551 replay_ctr); 7552 7553 nla_nest_end(msg, replay_nested); 7554 7555 send_and_recv_msgs(drv, msg, NULL, NULL); 7556 return; 7557 nla_put_failure: 7558 nlmsg_free(msg); 7559 } 7560 7561 7562 static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr, 7563 int qos) 7564 { 7565 struct i802_bss *bss = priv; 7566 struct { 7567 struct ieee80211_hdr hdr; 7568 u16 qos_ctl; 7569 } STRUCT_PACKED nulldata; 7570 size_t size; 7571 7572 /* Send data frame to poll STA and check whether this frame is ACKed */ 7573 7574 os_memset(&nulldata, 0, sizeof(nulldata)); 7575 7576 if (qos) { 7577 nulldata.hdr.frame_control = 7578 IEEE80211_FC(WLAN_FC_TYPE_DATA, 7579 WLAN_FC_STYPE_QOS_NULL); 7580 size = sizeof(nulldata); 7581 } else { 7582 nulldata.hdr.frame_control = 7583 IEEE80211_FC(WLAN_FC_TYPE_DATA, 7584 WLAN_FC_STYPE_NULLFUNC); 7585 size = sizeof(struct ieee80211_hdr); 7586 } 7587 7588 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS); 7589 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN); 7590 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN); 7591 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN); 7592 7593 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size) < 0) 7594 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to " 7595 "send poll frame"); 7596 } 7597 7598 7599 #ifdef CONFIG_TDLS 7600 7601 static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code, 7602 u8 dialog_token, u16 status_code, 7603 const u8 *buf, size_t len) 7604 { 7605 struct i802_bss *bss = priv; 7606 struct wpa_driver_nl80211_data *drv = bss->drv; 7607 struct nl_msg *msg; 7608 7609 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) 7610 return -EOPNOTSUPP; 7611 7612 if (!dst) 7613 return -EINVAL; 7614 7615 msg = nlmsg_alloc(); 7616 if (!msg) 7617 return -ENOMEM; 7618 7619 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT); 7620 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 7621 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst); 7622 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code); 7623 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token); 7624 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code); 7625 NLA_PUT(msg, NL80211_ATTR_IE, len, buf); 7626 7627 return send_and_recv_msgs(drv, msg, NULL, NULL); 7628 7629 nla_put_failure: 7630 nlmsg_free(msg); 7631 return -ENOBUFS; 7632 } 7633 7634 7635 static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer) 7636 { 7637 struct i802_bss *bss = priv; 7638 struct wpa_driver_nl80211_data *drv = bss->drv; 7639 struct nl_msg *msg; 7640 enum nl80211_tdls_operation nl80211_oper; 7641 7642 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) 7643 return -EOPNOTSUPP; 7644 7645 switch (oper) { 7646 case TDLS_DISCOVERY_REQ: 7647 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ; 7648 break; 7649 case TDLS_SETUP: 7650 nl80211_oper = NL80211_TDLS_SETUP; 7651 break; 7652 case TDLS_TEARDOWN: 7653 nl80211_oper = NL80211_TDLS_TEARDOWN; 7654 break; 7655 case TDLS_ENABLE_LINK: 7656 nl80211_oper = NL80211_TDLS_ENABLE_LINK; 7657 break; 7658 case TDLS_DISABLE_LINK: 7659 nl80211_oper = NL80211_TDLS_DISABLE_LINK; 7660 break; 7661 case TDLS_ENABLE: 7662 return 0; 7663 case TDLS_DISABLE: 7664 return 0; 7665 default: 7666 return -EINVAL; 7667 } 7668 7669 msg = nlmsg_alloc(); 7670 if (!msg) 7671 return -ENOMEM; 7672 7673 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER); 7674 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper); 7675 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 7676 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer); 7677 7678 return send_and_recv_msgs(drv, msg, NULL, NULL); 7679 7680 nla_put_failure: 7681 nlmsg_free(msg); 7682 return -ENOBUFS; 7683 } 7684 7685 #endif /* CONFIG TDLS */ 7686 7687 7688 const struct wpa_driver_ops wpa_driver_nl80211_ops = { 7689 .name = "nl80211", 7690 .desc = "Linux nl80211/cfg80211", 7691 .get_bssid = wpa_driver_nl80211_get_bssid, 7692 .get_ssid = wpa_driver_nl80211_get_ssid, 7693 .set_key = wpa_driver_nl80211_set_key, 7694 .scan2 = wpa_driver_nl80211_scan, 7695 .sched_scan = wpa_driver_nl80211_sched_scan, 7696 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan, 7697 .get_scan_results2 = wpa_driver_nl80211_get_scan_results, 7698 .deauthenticate = wpa_driver_nl80211_deauthenticate, 7699 .disassociate = wpa_driver_nl80211_disassociate, 7700 .authenticate = wpa_driver_nl80211_authenticate, 7701 .associate = wpa_driver_nl80211_associate, 7702 .global_init = nl80211_global_init, 7703 .global_deinit = nl80211_global_deinit, 7704 .init2 = wpa_driver_nl80211_init, 7705 .deinit = wpa_driver_nl80211_deinit, 7706 .get_capa = wpa_driver_nl80211_get_capa, 7707 .set_operstate = wpa_driver_nl80211_set_operstate, 7708 .set_supp_port = wpa_driver_nl80211_set_supp_port, 7709 .set_country = wpa_driver_nl80211_set_country, 7710 .set_ap = wpa_driver_nl80211_set_ap, 7711 .if_add = wpa_driver_nl80211_if_add, 7712 .if_remove = wpa_driver_nl80211_if_remove, 7713 .send_mlme = wpa_driver_nl80211_send_mlme, 7714 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data, 7715 .sta_add = wpa_driver_nl80211_sta_add, 7716 .sta_remove = wpa_driver_nl80211_sta_remove, 7717 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol, 7718 .sta_set_flags = wpa_driver_nl80211_sta_set_flags, 7719 #ifdef HOSTAPD 7720 .hapd_init = i802_init, 7721 .hapd_deinit = i802_deinit, 7722 .set_wds_sta = i802_set_wds_sta, 7723 #endif /* HOSTAPD */ 7724 #if defined(HOSTAPD) || defined(CONFIG_AP) 7725 .get_seqnum = i802_get_seqnum, 7726 .flush = i802_flush, 7727 .read_sta_data = i802_read_sta_data, 7728 .get_inact_sec = i802_get_inact_sec, 7729 .sta_clear_stats = i802_sta_clear_stats, 7730 .set_rts = i802_set_rts, 7731 .set_frag = i802_set_frag, 7732 .set_tx_queue_params = i802_set_tx_queue_params, 7733 .set_sta_vlan = i802_set_sta_vlan, 7734 .set_rate_sets = i802_set_rate_sets, 7735 .sta_deauth = i802_sta_deauth, 7736 .sta_disassoc = i802_sta_disassoc, 7737 #endif /* HOSTAPD || CONFIG_AP */ 7738 .set_freq = i802_set_freq, 7739 .send_action = wpa_driver_nl80211_send_action, 7740 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait, 7741 .remain_on_channel = wpa_driver_nl80211_remain_on_channel, 7742 .cancel_remain_on_channel = 7743 wpa_driver_nl80211_cancel_remain_on_channel, 7744 .probe_req_report = wpa_driver_nl80211_probe_req_report, 7745 .deinit_ap = wpa_driver_nl80211_deinit_ap, 7746 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli, 7747 .resume = wpa_driver_nl80211_resume, 7748 .send_ft_action = nl80211_send_ft_action, 7749 .signal_monitor = nl80211_signal_monitor, 7750 .signal_poll = nl80211_signal_poll, 7751 .send_frame = nl80211_send_frame, 7752 .set_param = nl80211_set_param, 7753 .get_radio_name = nl80211_get_radio_name, 7754 .add_pmkid = nl80211_add_pmkid, 7755 .remove_pmkid = nl80211_remove_pmkid, 7756 .flush_pmkid = nl80211_flush_pmkid, 7757 .set_rekey_info = nl80211_set_rekey_info, 7758 .poll_client = nl80211_poll_client, 7759 #ifdef CONFIG_TDLS 7760 .send_tdls_mgmt = nl80211_send_tdls_mgmt, 7761 .tdls_oper = nl80211_tdls_oper, 7762 #endif /* CONFIG_TDLS */ 7763 }; 7764