driver_nl80211.c revision 700a137ab366edc72e371da68ba187b4717ee660
1/* 2 * Driver interaction with Linux nl80211/cfg80211 3 * Copyright (c) 2002-2012, 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 software may be distributed under the terms of the BSD license. 10 * See README for more details. 11 */ 12 13#include "includes.h" 14#include <sys/ioctl.h> 15#include <sys/types.h> 16#include <sys/stat.h> 17#include <fcntl.h> 18#include <net/if.h> 19#include <netlink/genl/genl.h> 20#include <netlink/genl/family.h> 21#include <netlink/genl/ctrl.h> 22#include <linux/rtnetlink.h> 23#include <netpacket/packet.h> 24#include <linux/filter.h> 25#include <linux/errqueue.h> 26#include "nl80211_copy.h" 27 28#include "common.h" 29#include "eloop.h" 30#include "utils/list.h" 31#include "common/ieee802_11_defs.h" 32#include "common/ieee802_11_common.h" 33#include "l2_packet/l2_packet.h" 34#include "netlink.h" 35#include "linux_ioctl.h" 36#include "radiotap.h" 37#include "radiotap_iter.h" 38#include "rfkill.h" 39#include "driver.h" 40 41#ifndef SO_WIFI_STATUS 42# if defined(__sparc__) 43# define SO_WIFI_STATUS 0x0025 44# elif defined(__parisc__) 45# define SO_WIFI_STATUS 0x4022 46# else 47# define SO_WIFI_STATUS 41 48# endif 49 50# define SCM_WIFI_STATUS SO_WIFI_STATUS 51#endif 52 53#ifndef SO_EE_ORIGIN_TXSTATUS 54#define SO_EE_ORIGIN_TXSTATUS 4 55#endif 56 57#ifndef PACKET_TX_TIMESTAMP 58#define PACKET_TX_TIMESTAMP 16 59#endif 60 61#ifdef ANDROID 62#include "android_drv.h" 63#endif /* ANDROID */ 64#ifdef CONFIG_LIBNL20 65/* libnl 2.0 compatibility code */ 66#define nl_handle nl_sock 67#define nl80211_handle_alloc nl_socket_alloc_cb 68#define nl80211_handle_destroy nl_socket_free 69#else 70/* 71 * libnl 1.1 has a bug, it tries to allocate socket numbers densely 72 * but when you free a socket again it will mess up its bitmap and 73 * and use the wrong number the next time it needs a socket ID. 74 * Therefore, we wrap the handle alloc/destroy and add our own pid 75 * accounting. 76 */ 77static uint32_t port_bitmap[32] = { 0 }; 78 79static struct nl_handle *nl80211_handle_alloc(void *cb) 80{ 81 struct nl_handle *handle; 82 uint32_t pid = getpid() & 0x3FFFFF; 83 int i; 84 85 handle = nl_handle_alloc_cb(cb); 86 87 for (i = 0; i < 1024; i++) { 88 if (port_bitmap[i / 32] & (1 << (i % 32))) 89 continue; 90 port_bitmap[i / 32] |= 1 << (i % 32); 91 pid += i << 22; 92 break; 93 } 94 95 nl_socket_set_local_port(handle, pid); 96 97 return handle; 98} 99 100static void nl80211_handle_destroy(struct nl_handle *handle) 101{ 102 uint32_t port = nl_socket_get_local_port(handle); 103 104 port >>= 22; 105 port_bitmap[port / 32] &= ~(1 << (port % 32)); 106 107 nl_handle_destroy(handle); 108} 109#endif /* CONFIG_LIBNL20 */ 110 111 112static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg) 113{ 114 struct nl_handle *handle; 115 116 handle = nl80211_handle_alloc(cb); 117 if (handle == NULL) { 118 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " 119 "callbacks (%s)", dbg); 120 return NULL; 121 } 122 123 if (genl_connect(handle)) { 124 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic " 125 "netlink (%s)", dbg); 126 nl80211_handle_destroy(handle); 127 return NULL; 128 } 129 130 return handle; 131} 132 133 134static void nl_destroy_handles(struct nl_handle **handle) 135{ 136 if (*handle == NULL) 137 return; 138 nl80211_handle_destroy(*handle); 139 *handle = NULL; 140} 141 142 143#ifndef IFF_LOWER_UP 144#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */ 145#endif 146#ifndef IFF_DORMANT 147#define IFF_DORMANT 0x20000 /* driver signals dormant */ 148#endif 149 150#ifndef IF_OPER_DORMANT 151#define IF_OPER_DORMANT 5 152#endif 153#ifndef IF_OPER_UP 154#define IF_OPER_UP 6 155#endif 156 157struct nl80211_global { 158 struct dl_list interfaces; 159 int if_add_ifindex; 160 struct netlink_data *netlink; 161 struct nl_cb *nl_cb; 162 struct nl_handle *nl; 163 int nl80211_id; 164 int ioctl_sock; /* socket for ioctl() use */ 165 166 struct nl_handle *nl_event; 167}; 168 169struct nl80211_wiphy_data { 170 struct dl_list list; 171 struct dl_list bsss; 172 struct dl_list drvs; 173 174 struct nl_handle *nl_beacons; 175 struct nl_cb *nl_cb; 176 177 int wiphy_idx; 178}; 179 180static void nl80211_global_deinit(void *priv); 181 182struct i802_bss { 183 struct wpa_driver_nl80211_data *drv; 184 struct i802_bss *next; 185 int ifindex; 186 char ifname[IFNAMSIZ + 1]; 187 char brname[IFNAMSIZ]; 188 unsigned int beacon_set:1; 189 unsigned int added_if_into_bridge:1; 190 unsigned int added_bridge:1; 191 unsigned int in_deinit:1; 192 193 u8 addr[ETH_ALEN]; 194 195 int freq; 196 197 void *ctx; 198 struct nl_handle *nl_preq, *nl_mgmt; 199 struct nl_cb *nl_cb; 200 201 struct nl80211_wiphy_data *wiphy_data; 202 struct dl_list wiphy_list; 203}; 204 205struct wpa_driver_nl80211_data { 206 struct nl80211_global *global; 207 struct dl_list list; 208 struct dl_list wiphy_list; 209 char phyname[32]; 210 void *ctx; 211 int ifindex; 212 int if_removed; 213 int if_disabled; 214 int ignore_if_down_event; 215 struct rfkill_data *rfkill; 216 struct wpa_driver_capa capa; 217 int has_capability; 218 219 int operstate; 220 221 int scan_complete_events; 222 223 struct nl_cb *nl_cb; 224 225 u8 auth_bssid[ETH_ALEN]; 226 u8 bssid[ETH_ALEN]; 227 int associated; 228 u8 ssid[32]; 229 size_t ssid_len; 230 enum nl80211_iftype nlmode; 231 enum nl80211_iftype ap_scan_as_station; 232 unsigned int assoc_freq; 233 234 int monitor_sock; 235 int monitor_ifidx; 236 int monitor_refcount; 237 238 unsigned int disabled_11b_rates:1; 239 unsigned int pending_remain_on_chan:1; 240 unsigned int in_interface_list:1; 241 unsigned int device_ap_sme:1; 242 unsigned int poll_command_supported:1; 243 unsigned int data_tx_status:1; 244 unsigned int scan_for_auth:1; 245 unsigned int retry_auth:1; 246 unsigned int use_monitor:1; 247 unsigned int ignore_next_local_disconnect:1; 248 249 u64 remain_on_chan_cookie; 250 u64 send_action_cookie; 251 252 unsigned int last_mgmt_freq; 253 254 struct wpa_driver_scan_filter *filter_ssids; 255 size_t num_filter_ssids; 256 257 struct i802_bss first_bss; 258 259 int eapol_tx_sock; 260 261#ifdef HOSTAPD 262 int eapol_sock; /* socket for EAPOL frames */ 263 264 int default_if_indices[16]; 265 int *if_indices; 266 int num_if_indices; 267 268 int last_freq; 269 int last_freq_ht; 270#endif /* HOSTAPD */ 271 272 /* From failed authentication command */ 273 int auth_freq; 274 u8 auth_bssid_[ETH_ALEN]; 275 u8 auth_ssid[32]; 276 size_t auth_ssid_len; 277 int auth_alg; 278 u8 *auth_ie; 279 size_t auth_ie_len; 280 u8 auth_wep_key[4][16]; 281 size_t auth_wep_key_len[4]; 282 int auth_wep_tx_keyidx; 283 int auth_local_state_change; 284 int auth_p2p; 285}; 286 287 288static void wpa_driver_nl80211_deinit(struct i802_bss *bss); 289static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, 290 void *timeout_ctx); 291static int wpa_driver_nl80211_set_mode(struct i802_bss *bss, 292 enum nl80211_iftype nlmode); 293static int 294wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv); 295static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv, 296 const u8 *addr, int cmd, u16 reason_code, 297 int local_state_change); 298static void nl80211_remove_monitor_interface( 299 struct wpa_driver_nl80211_data *drv); 300static int nl80211_send_frame_cmd(struct i802_bss *bss, 301 unsigned int freq, unsigned int wait, 302 const u8 *buf, size_t buf_len, u64 *cookie, 303 int no_cck, int no_ack, int offchanok); 304static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, 305 int report); 306#ifdef ANDROID 307static int android_pno_start(struct i802_bss *bss, 308 struct wpa_driver_scan_params *params); 309static int android_pno_stop(struct i802_bss *bss); 310#endif /* ANDROID */ 311#ifdef ANDROID_P2P 312int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration); 313int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len); 314int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow); 315int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon, 316 const struct wpabuf *proberesp, 317 const struct wpabuf *assocresp); 318 319#endif 320#ifdef HOSTAPD 321static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); 322static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); 323static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); 324static int wpa_driver_nl80211_if_remove(struct i802_bss *bss, 325 enum wpa_driver_if_type type, 326 const char *ifname); 327#else /* HOSTAPD */ 328static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 329{ 330} 331 332static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 333{ 334} 335 336static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 337{ 338 return 0; 339} 340#endif /* HOSTAPD */ 341#ifdef ANDROID 342extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf, 343 size_t buf_len); 344#endif 345 346static int wpa_driver_nl80211_set_freq(struct i802_bss *bss, 347 struct hostapd_freq_params *freq); 348static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv, 349 int ifindex, int disabled); 350 351static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv); 352static int wpa_driver_nl80211_authenticate_retry( 353 struct wpa_driver_nl80211_data *drv); 354 355 356static int is_ap_interface(enum nl80211_iftype nlmode) 357{ 358 return (nlmode == NL80211_IFTYPE_AP || 359 nlmode == NL80211_IFTYPE_P2P_GO); 360} 361 362 363static int is_sta_interface(enum nl80211_iftype nlmode) 364{ 365 return (nlmode == NL80211_IFTYPE_STATION || 366 nlmode == NL80211_IFTYPE_P2P_CLIENT); 367} 368 369 370static int is_p2p_interface(enum nl80211_iftype nlmode) 371{ 372 return (nlmode == NL80211_IFTYPE_P2P_CLIENT || 373 nlmode == NL80211_IFTYPE_P2P_GO); 374} 375 376 377struct nl80211_bss_info_arg { 378 struct wpa_driver_nl80211_data *drv; 379 struct wpa_scan_results *res; 380 unsigned int assoc_freq; 381 u8 assoc_bssid[ETH_ALEN]; 382}; 383 384static int bss_info_handler(struct nl_msg *msg, void *arg); 385 386 387/* nl80211 code */ 388static int ack_handler(struct nl_msg *msg, void *arg) 389{ 390 int *err = arg; 391 *err = 0; 392 return NL_STOP; 393} 394 395static int finish_handler(struct nl_msg *msg, void *arg) 396{ 397 int *ret = arg; 398 *ret = 0; 399 return NL_SKIP; 400} 401 402static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, 403 void *arg) 404{ 405 int *ret = arg; 406 *ret = err->error; 407 return NL_SKIP; 408} 409 410 411static int no_seq_check(struct nl_msg *msg, void *arg) 412{ 413 return NL_OK; 414} 415 416 417static int send_and_recv(struct nl80211_global *global, 418 struct nl_handle *nl_handle, struct nl_msg *msg, 419 int (*valid_handler)(struct nl_msg *, void *), 420 void *valid_data) 421{ 422 struct nl_cb *cb; 423 int err = -ENOMEM; 424 425 cb = nl_cb_clone(global->nl_cb); 426 if (!cb) 427 goto out; 428 429 err = nl_send_auto_complete(nl_handle, msg); 430 if (err < 0) 431 goto out; 432 433 err = 1; 434 435 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); 436 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); 437 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); 438 439 if (valid_handler) 440 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, 441 valid_handler, valid_data); 442 443 while (err > 0) 444 nl_recvmsgs(nl_handle, cb); 445 out: 446 nl_cb_put(cb); 447 nlmsg_free(msg); 448 return err; 449} 450 451 452static int send_and_recv_msgs_global(struct nl80211_global *global, 453 struct nl_msg *msg, 454 int (*valid_handler)(struct nl_msg *, void *), 455 void *valid_data) 456{ 457 return send_and_recv(global, global->nl, msg, valid_handler, 458 valid_data); 459} 460 461 462#ifndef ANDROID 463static 464#endif 465int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv, 466 struct nl_msg *msg, 467 int (*valid_handler)(struct nl_msg *, void *), 468 void *valid_data) 469{ 470 return send_and_recv(drv->global, drv->global->nl, msg, 471 valid_handler, valid_data); 472} 473 474 475struct family_data { 476 const char *group; 477 int id; 478}; 479 480 481static int family_handler(struct nl_msg *msg, void *arg) 482{ 483 struct family_data *res = arg; 484 struct nlattr *tb[CTRL_ATTR_MAX + 1]; 485 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 486 struct nlattr *mcgrp; 487 int i; 488 489 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 490 genlmsg_attrlen(gnlh, 0), NULL); 491 if (!tb[CTRL_ATTR_MCAST_GROUPS]) 492 return NL_SKIP; 493 494 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) { 495 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1]; 496 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp), 497 nla_len(mcgrp), NULL); 498 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || 499 !tb2[CTRL_ATTR_MCAST_GRP_ID] || 500 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]), 501 res->group, 502 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0) 503 continue; 504 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]); 505 break; 506 }; 507 508 return NL_SKIP; 509} 510 511 512static int nl_get_multicast_id(struct nl80211_global *global, 513 const char *family, const char *group) 514{ 515 struct nl_msg *msg; 516 int ret = -1; 517 struct family_data res = { group, -ENOENT }; 518 519 msg = nlmsg_alloc(); 520 if (!msg) 521 return -ENOMEM; 522 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"), 523 0, 0, CTRL_CMD_GETFAMILY, 0); 524 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family); 525 526 ret = send_and_recv_msgs_global(global, msg, family_handler, &res); 527 msg = NULL; 528 if (ret == 0) 529 ret = res.id; 530 531nla_put_failure: 532 nlmsg_free(msg); 533 return ret; 534} 535 536 537static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv, 538 struct nl_msg *msg, int flags, uint8_t cmd) 539{ 540 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id, 541 0, flags, cmd, 0); 542} 543 544 545struct wiphy_idx_data { 546 int wiphy_idx; 547}; 548 549 550static int netdev_info_handler(struct nl_msg *msg, void *arg) 551{ 552 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 553 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 554 struct wiphy_idx_data *info = arg; 555 556 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 557 genlmsg_attrlen(gnlh, 0), NULL); 558 559 if (tb[NL80211_ATTR_WIPHY]) 560 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]); 561 562 return NL_SKIP; 563} 564 565 566static int nl80211_get_wiphy_index(struct i802_bss *bss) 567{ 568 struct nl_msg *msg; 569 struct wiphy_idx_data data = { 570 .wiphy_idx = -1, 571 }; 572 573 msg = nlmsg_alloc(); 574 if (!msg) 575 return -1; 576 577 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE); 578 579 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 580 581 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0) 582 return data.wiphy_idx; 583 msg = NULL; 584nla_put_failure: 585 nlmsg_free(msg); 586 return -1; 587} 588 589 590static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv, 591 struct nl80211_wiphy_data *w) 592{ 593 struct nl_msg *msg; 594 int ret = -1; 595 596 msg = nlmsg_alloc(); 597 if (!msg) 598 return -1; 599 600 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS); 601 602 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx); 603 604 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL); 605 msg = NULL; 606 if (ret) { 607 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command " 608 "failed: ret=%d (%s)", 609 ret, strerror(-ret)); 610 goto nla_put_failure; 611 } 612 ret = 0; 613nla_put_failure: 614 nlmsg_free(msg); 615 return ret; 616} 617 618 619static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle) 620{ 621 struct nl80211_wiphy_data *w = eloop_ctx; 622 623 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available"); 624 625 nl_recvmsgs(handle, w->nl_cb); 626} 627 628 629static int process_beacon_event(struct nl_msg *msg, void *arg) 630{ 631 struct nl80211_wiphy_data *w = arg; 632 struct wpa_driver_nl80211_data *drv; 633 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 634 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 635 union wpa_event_data event; 636 637 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 638 genlmsg_attrlen(gnlh, 0), NULL); 639 640 if (gnlh->cmd != NL80211_CMD_FRAME) { 641 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)", 642 gnlh->cmd); 643 return NL_SKIP; 644 } 645 646 if (!tb[NL80211_ATTR_FRAME]) 647 return NL_SKIP; 648 649 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data, 650 wiphy_list) { 651 os_memset(&event, 0, sizeof(event)); 652 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]); 653 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]); 654 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event); 655 } 656 657 return NL_SKIP; 658} 659 660 661static struct nl80211_wiphy_data * 662nl80211_get_wiphy_data_ap(struct i802_bss *bss) 663{ 664 static DEFINE_DL_LIST(nl80211_wiphys); 665 struct nl80211_wiphy_data *w; 666 int wiphy_idx, found = 0; 667 struct i802_bss *tmp_bss; 668 669 if (bss->wiphy_data != NULL) 670 return bss->wiphy_data; 671 672 wiphy_idx = nl80211_get_wiphy_index(bss); 673 674 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) { 675 if (w->wiphy_idx == wiphy_idx) 676 goto add; 677 } 678 679 /* alloc new one */ 680 w = os_zalloc(sizeof(*w)); 681 if (w == NULL) 682 return NULL; 683 w->wiphy_idx = wiphy_idx; 684 dl_list_init(&w->bsss); 685 dl_list_init(&w->drvs); 686 687 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); 688 if (!w->nl_cb) { 689 os_free(w); 690 return NULL; 691 } 692 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); 693 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event, 694 w); 695 696 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb, 697 "wiphy beacons"); 698 if (w->nl_beacons == NULL) { 699 os_free(w); 700 return NULL; 701 } 702 703 if (nl80211_register_beacons(bss->drv, w)) { 704 nl_destroy_handles(&w->nl_beacons); 705 os_free(w); 706 return NULL; 707 } 708 709 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons), 710 nl80211_recv_beacons, w, w->nl_beacons); 711 712 dl_list_add(&nl80211_wiphys, &w->list); 713 714add: 715 /* drv entry for this bss already there? */ 716 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) { 717 if (tmp_bss->drv == bss->drv) { 718 found = 1; 719 break; 720 } 721 } 722 /* if not add it */ 723 if (!found) 724 dl_list_add(&w->drvs, &bss->drv->wiphy_list); 725 726 dl_list_add(&w->bsss, &bss->wiphy_list); 727 bss->wiphy_data = w; 728 return w; 729} 730 731 732static void nl80211_put_wiphy_data_ap(struct i802_bss *bss) 733{ 734 struct nl80211_wiphy_data *w = bss->wiphy_data; 735 struct i802_bss *tmp_bss; 736 int found = 0; 737 738 if (w == NULL) 739 return; 740 bss->wiphy_data = NULL; 741 dl_list_del(&bss->wiphy_list); 742 743 /* still any for this drv present? */ 744 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) { 745 if (tmp_bss->drv == bss->drv) { 746 found = 1; 747 break; 748 } 749 } 750 /* if not remove it */ 751 if (!found) 752 dl_list_del(&bss->drv->wiphy_list); 753 754 if (!dl_list_empty(&w->bsss)) 755 return; 756 757 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons)); 758 759 nl_cb_put(w->nl_cb); 760 nl_destroy_handles(&w->nl_beacons); 761 dl_list_del(&w->list); 762 os_free(w); 763} 764 765 766static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid) 767{ 768 struct i802_bss *bss = priv; 769 struct wpa_driver_nl80211_data *drv = bss->drv; 770 if (!drv->associated) 771 return -1; 772 os_memcpy(bssid, drv->bssid, ETH_ALEN); 773 return 0; 774} 775 776 777static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid) 778{ 779 struct i802_bss *bss = priv; 780 struct wpa_driver_nl80211_data *drv = bss->drv; 781 if (!drv->associated) 782 return -1; 783 os_memcpy(ssid, drv->ssid, drv->ssid_len); 784 return drv->ssid_len; 785} 786 787 788static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv, 789 char *buf, size_t len, int del) 790{ 791 union wpa_event_data event; 792 793 os_memset(&event, 0, sizeof(event)); 794 if (len > sizeof(event.interface_status.ifname)) 795 len = sizeof(event.interface_status.ifname) - 1; 796 os_memcpy(event.interface_status.ifname, buf, len); 797 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED : 798 EVENT_INTERFACE_ADDED; 799 800 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s", 801 del ? "DEL" : "NEW", 802 event.interface_status.ifname, 803 del ? "removed" : "added"); 804 805 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) { 806 if (del) { 807 if (drv->if_removed) { 808 wpa_printf(MSG_DEBUG, "nl80211: if_removed " 809 "already set - ignore event"); 810 return; 811 } 812 drv->if_removed = 1; 813 } else { 814 if (if_nametoindex(drv->first_bss.ifname) == 0) { 815 wpa_printf(MSG_DEBUG, "nl80211: Interface %s " 816 "does not exist - ignore " 817 "RTM_NEWLINK", 818 drv->first_bss.ifname); 819 return; 820 } 821 if (!drv->if_removed) { 822 wpa_printf(MSG_DEBUG, "nl80211: if_removed " 823 "already cleared - ignore event"); 824 return; 825 } 826 drv->if_removed = 0; 827 } 828 } 829 830 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event); 831} 832 833 834static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv, 835 u8 *buf, size_t len) 836{ 837 int attrlen, rta_len; 838 struct rtattr *attr; 839 840 attrlen = len; 841 attr = (struct rtattr *) buf; 842 843 rta_len = RTA_ALIGN(sizeof(struct rtattr)); 844 while (RTA_OK(attr, attrlen)) { 845 if (attr->rta_type == IFLA_IFNAME) { 846 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname) 847 == 0) 848 return 1; 849 else 850 break; 851 } 852 attr = RTA_NEXT(attr, attrlen); 853 } 854 855 return 0; 856} 857 858 859static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv, 860 int ifindex, u8 *buf, size_t len) 861{ 862 if (drv->ifindex == ifindex) 863 return 1; 864 865 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) { 866 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname); 867 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed " 868 "interface"); 869 wpa_driver_nl80211_finish_drv_init(drv); 870 return 1; 871 } 872 873 return 0; 874} 875 876 877static struct wpa_driver_nl80211_data * 878nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len) 879{ 880 struct wpa_driver_nl80211_data *drv; 881 dl_list_for_each(drv, &global->interfaces, 882 struct wpa_driver_nl80211_data, list) { 883 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) || 884 have_ifidx(drv, idx)) 885 return drv; 886 } 887 return NULL; 888} 889 890 891static void wpa_driver_nl80211_event_rtm_newlink(void *ctx, 892 struct ifinfomsg *ifi, 893 u8 *buf, size_t len) 894{ 895 struct nl80211_global *global = ctx; 896 struct wpa_driver_nl80211_data *drv; 897 int attrlen, rta_len; 898 struct rtattr *attr; 899 u32 brid = 0; 900 char namebuf[IFNAMSIZ]; 901 902 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len); 903 if (!drv) { 904 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign " 905 "ifindex %d", ifi->ifi_index); 906 return; 907 } 908 909 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x " 910 "(%s%s%s%s)", 911 drv->operstate, ifi->ifi_flags, 912 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "", 913 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "", 914 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "", 915 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : ""); 916 917 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) { 918 if (if_indextoname(ifi->ifi_index, namebuf) && 919 linux_iface_up(drv->global->ioctl_sock, 920 drv->first_bss.ifname) > 0) { 921 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down " 922 "event since interface %s is up", namebuf); 923 return; 924 } 925 wpa_printf(MSG_DEBUG, "nl80211: Interface down"); 926 if (drv->ignore_if_down_event) { 927 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down " 928 "event generated by mode change"); 929 drv->ignore_if_down_event = 0; 930 } else { 931 drv->if_disabled = 1; 932 wpa_supplicant_event(drv->ctx, 933 EVENT_INTERFACE_DISABLED, NULL); 934 } 935 } 936 937 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) { 938 if (if_indextoname(ifi->ifi_index, namebuf) && 939 linux_iface_up(drv->global->ioctl_sock, 940 drv->first_bss.ifname) == 0) { 941 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " 942 "event since interface %s is down", 943 namebuf); 944 } else if (if_nametoindex(drv->first_bss.ifname) == 0) { 945 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " 946 "event since interface %s does not exist", 947 drv->first_bss.ifname); 948 } else if (drv->if_removed) { 949 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " 950 "event since interface %s is marked " 951 "removed", drv->first_bss.ifname); 952 } else { 953 wpa_printf(MSG_DEBUG, "nl80211: Interface up"); 954 drv->if_disabled = 0; 955 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, 956 NULL); 957 } 958 } 959 960 /* 961 * Some drivers send the association event before the operup event--in 962 * this case, lifting operstate in wpa_driver_nl80211_set_operstate() 963 * fails. This will hit us when wpa_supplicant does not need to do 964 * IEEE 802.1X authentication 965 */ 966 if (drv->operstate == 1 && 967 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP && 968 !(ifi->ifi_flags & IFF_RUNNING)) 969 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 970 -1, IF_OPER_UP); 971 972 attrlen = len; 973 attr = (struct rtattr *) buf; 974 rta_len = RTA_ALIGN(sizeof(struct rtattr)); 975 while (RTA_OK(attr, attrlen)) { 976 if (attr->rta_type == IFLA_IFNAME) { 977 wpa_driver_nl80211_event_link( 978 drv, 979 ((char *) attr) + rta_len, 980 attr->rta_len - rta_len, 0); 981 } else if (attr->rta_type == IFLA_MASTER) 982 brid = nla_get_u32((struct nlattr *) attr); 983 attr = RTA_NEXT(attr, attrlen); 984 } 985 986 if (ifi->ifi_family == AF_BRIDGE && brid) { 987 /* device has been added to bridge */ 988 if_indextoname(brid, namebuf); 989 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s", 990 brid, namebuf); 991 add_ifidx(drv, brid); 992 } 993} 994 995 996static void wpa_driver_nl80211_event_rtm_dellink(void *ctx, 997 struct ifinfomsg *ifi, 998 u8 *buf, size_t len) 999{ 1000 struct nl80211_global *global = ctx; 1001 struct wpa_driver_nl80211_data *drv; 1002 int attrlen, rta_len; 1003 struct rtattr *attr; 1004 u32 brid = 0; 1005 1006 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len); 1007 if (!drv) { 1008 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for " 1009 "foreign ifindex %d", ifi->ifi_index); 1010 return; 1011 } 1012 1013 attrlen = len; 1014 attr = (struct rtattr *) buf; 1015 1016 rta_len = RTA_ALIGN(sizeof(struct rtattr)); 1017 while (RTA_OK(attr, attrlen)) { 1018 if (attr->rta_type == IFLA_IFNAME) { 1019 wpa_driver_nl80211_event_link( 1020 drv, 1021 ((char *) attr) + rta_len, 1022 attr->rta_len - rta_len, 1); 1023 } else if (attr->rta_type == IFLA_MASTER) 1024 brid = nla_get_u32((struct nlattr *) attr); 1025 attr = RTA_NEXT(attr, attrlen); 1026 } 1027 1028 if (ifi->ifi_family == AF_BRIDGE && brid) { 1029 /* device has been removed from bridge */ 1030 char namebuf[IFNAMSIZ]; 1031 if_indextoname(brid, namebuf); 1032 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge " 1033 "%s", brid, namebuf); 1034 del_ifidx(drv, brid); 1035 } 1036} 1037 1038 1039static void mlme_event_auth(struct wpa_driver_nl80211_data *drv, 1040 const u8 *frame, size_t len) 1041{ 1042 const struct ieee80211_mgmt *mgmt; 1043 union wpa_event_data event; 1044 1045 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event"); 1046 mgmt = (const struct ieee80211_mgmt *) frame; 1047 if (len < 24 + sizeof(mgmt->u.auth)) { 1048 wpa_printf(MSG_DEBUG, "nl80211: Too short association event " 1049 "frame"); 1050 return; 1051 } 1052 1053 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN); 1054 os_memset(&event, 0, sizeof(event)); 1055 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN); 1056 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg); 1057 event.auth.auth_transaction = 1058 le_to_host16(mgmt->u.auth.auth_transaction); 1059 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code); 1060 if (len > 24 + sizeof(mgmt->u.auth)) { 1061 event.auth.ies = mgmt->u.auth.variable; 1062 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth); 1063 } 1064 1065 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event); 1066} 1067 1068 1069static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv) 1070{ 1071 struct nl_msg *msg; 1072 int ret; 1073 struct nl80211_bss_info_arg arg; 1074 1075 os_memset(&arg, 0, sizeof(arg)); 1076 msg = nlmsg_alloc(); 1077 if (!msg) 1078 goto nla_put_failure; 1079 1080 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN); 1081 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 1082 1083 arg.drv = drv; 1084 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg); 1085 msg = NULL; 1086 if (ret == 0) { 1087 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the " 1088 "associated BSS from scan results: %u MHz", 1089 arg.assoc_freq); 1090 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq; 1091 } 1092 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d " 1093 "(%s)", ret, strerror(-ret)); 1094nla_put_failure: 1095 nlmsg_free(msg); 1096 return drv->assoc_freq; 1097} 1098 1099 1100static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv, 1101 const u8 *frame, size_t len) 1102{ 1103 const struct ieee80211_mgmt *mgmt; 1104 union wpa_event_data event; 1105 u16 status; 1106 1107 wpa_printf(MSG_DEBUG, "nl80211: Associate event"); 1108 mgmt = (const struct ieee80211_mgmt *) frame; 1109 if (len < 24 + sizeof(mgmt->u.assoc_resp)) { 1110 wpa_printf(MSG_DEBUG, "nl80211: Too short association event " 1111 "frame"); 1112 return; 1113 } 1114 1115 status = le_to_host16(mgmt->u.assoc_resp.status_code); 1116 if (status != WLAN_STATUS_SUCCESS) { 1117 os_memset(&event, 0, sizeof(event)); 1118 event.assoc_reject.bssid = mgmt->bssid; 1119 if (len > 24 + sizeof(mgmt->u.assoc_resp)) { 1120 event.assoc_reject.resp_ies = 1121 (u8 *) mgmt->u.assoc_resp.variable; 1122 event.assoc_reject.resp_ies_len = 1123 len - 24 - sizeof(mgmt->u.assoc_resp); 1124 } 1125 event.assoc_reject.status_code = status; 1126 1127 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event); 1128 return; 1129 } 1130 1131 drv->associated = 1; 1132 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN); 1133 1134 os_memset(&event, 0, sizeof(event)); 1135 if (len > 24 + sizeof(mgmt->u.assoc_resp)) { 1136 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable; 1137 event.assoc_info.resp_ies_len = 1138 len - 24 - sizeof(mgmt->u.assoc_resp); 1139 } 1140 1141 event.assoc_info.freq = drv->assoc_freq; 1142 1143 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event); 1144} 1145 1146 1147static void mlme_event_connect(struct wpa_driver_nl80211_data *drv, 1148 enum nl80211_commands cmd, struct nlattr *status, 1149 struct nlattr *addr, struct nlattr *req_ie, 1150 struct nlattr *resp_ie) 1151{ 1152 union wpa_event_data event; 1153 1154 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { 1155 /* 1156 * Avoid reporting two association events that would confuse 1157 * the core code. 1158 */ 1159 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) " 1160 "when using userspace SME", cmd); 1161 return; 1162 } 1163 1164 if (cmd == NL80211_CMD_CONNECT) 1165 wpa_printf(MSG_DEBUG, "nl80211: Connect event"); 1166 else if (cmd == NL80211_CMD_ROAM) 1167 wpa_printf(MSG_DEBUG, "nl80211: Roam event"); 1168 1169 os_memset(&event, 0, sizeof(event)); 1170 if (cmd == NL80211_CMD_CONNECT && 1171 nla_get_u16(status) != WLAN_STATUS_SUCCESS) { 1172 if (addr) 1173 event.assoc_reject.bssid = nla_data(addr); 1174 if (resp_ie) { 1175 event.assoc_reject.resp_ies = nla_data(resp_ie); 1176 event.assoc_reject.resp_ies_len = nla_len(resp_ie); 1177 } 1178 event.assoc_reject.status_code = nla_get_u16(status); 1179 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event); 1180 return; 1181 } 1182 1183 drv->associated = 1; 1184 if (addr) 1185 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN); 1186 1187 if (req_ie) { 1188 event.assoc_info.req_ies = nla_data(req_ie); 1189 event.assoc_info.req_ies_len = nla_len(req_ie); 1190 } 1191 if (resp_ie) { 1192 event.assoc_info.resp_ies = nla_data(resp_ie); 1193 event.assoc_info.resp_ies_len = nla_len(resp_ie); 1194 } 1195 1196 event.assoc_info.freq = nl80211_get_assoc_freq(drv); 1197 1198 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event); 1199} 1200 1201 1202static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv, 1203 struct nlattr *reason, struct nlattr *addr, 1204 struct nlattr *by_ap) 1205{ 1206 union wpa_event_data data; 1207 unsigned int locally_generated = by_ap == NULL; 1208 1209 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { 1210 /* 1211 * Avoid reporting two disassociation events that could 1212 * confuse the core code. 1213 */ 1214 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect " 1215 "event when using userspace SME"); 1216 return; 1217 } 1218 1219 if (drv->ignore_next_local_disconnect) { 1220 drv->ignore_next_local_disconnect = 0; 1221 if (locally_generated) { 1222 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect " 1223 "event triggered during reassociation"); 1224 return; 1225 } 1226 wpa_printf(MSG_WARNING, "nl80211: Was expecting local " 1227 "disconnect but got another disconnect " 1228 "event first"); 1229 } 1230 1231 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event"); 1232 drv->associated = 0; 1233 os_memset(&data, 0, sizeof(data)); 1234 if (reason) 1235 data.deauth_info.reason_code = nla_get_u16(reason); 1236 data.deauth_info.locally_generated = by_ap == NULL; 1237 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data); 1238} 1239 1240 1241static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv, 1242 struct nlattr *freq, struct nlattr *type) 1243{ 1244 union wpa_event_data data; 1245 int ht_enabled = 1; 1246 int chan_offset = 0; 1247 1248 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event"); 1249 1250 if (!freq || !type) 1251 return; 1252 1253 switch (nla_get_u32(type)) { 1254 case NL80211_CHAN_NO_HT: 1255 ht_enabled = 0; 1256 break; 1257 case NL80211_CHAN_HT20: 1258 break; 1259 case NL80211_CHAN_HT40PLUS: 1260 chan_offset = 1; 1261 break; 1262 case NL80211_CHAN_HT40MINUS: 1263 chan_offset = -1; 1264 break; 1265 } 1266 1267 data.ch_switch.freq = nla_get_u32(freq); 1268 data.ch_switch.ht_enabled = ht_enabled; 1269 data.ch_switch.ch_offset = chan_offset; 1270 1271 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data); 1272} 1273 1274 1275static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv, 1276 enum nl80211_commands cmd, struct nlattr *addr) 1277{ 1278 union wpa_event_data event; 1279 enum wpa_event_type ev; 1280 1281 if (nla_len(addr) != ETH_ALEN) 1282 return; 1283 1284 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR, 1285 cmd, MAC2STR((u8 *) nla_data(addr))); 1286 1287 if (cmd == NL80211_CMD_AUTHENTICATE) 1288 ev = EVENT_AUTH_TIMED_OUT; 1289 else if (cmd == NL80211_CMD_ASSOCIATE) 1290 ev = EVENT_ASSOC_TIMED_OUT; 1291 else 1292 return; 1293 1294 os_memset(&event, 0, sizeof(event)); 1295 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN); 1296 wpa_supplicant_event(drv->ctx, ev, &event); 1297} 1298 1299 1300static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv, 1301 struct nlattr *freq, struct nlattr *sig, 1302 const u8 *frame, size_t len) 1303{ 1304 const struct ieee80211_mgmt *mgmt; 1305 union wpa_event_data event; 1306 u16 fc, stype; 1307 int ssi_signal = 0; 1308 1309 wpa_printf(MSG_DEBUG, "nl80211: Frame event"); 1310 mgmt = (const struct ieee80211_mgmt *) frame; 1311 if (len < 24) { 1312 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame"); 1313 return; 1314 } 1315 1316 fc = le_to_host16(mgmt->frame_control); 1317 stype = WLAN_FC_GET_STYPE(fc); 1318 1319 if (sig) 1320 ssi_signal = (s32) nla_get_u32(sig); 1321 1322 os_memset(&event, 0, sizeof(event)); 1323 if (freq) { 1324 event.rx_action.freq = nla_get_u32(freq); 1325 drv->last_mgmt_freq = event.rx_action.freq; 1326 } 1327 if (stype == WLAN_FC_STYPE_ACTION) { 1328 event.rx_action.da = mgmt->da; 1329 event.rx_action.sa = mgmt->sa; 1330 event.rx_action.bssid = mgmt->bssid; 1331 event.rx_action.category = mgmt->u.action.category; 1332 event.rx_action.data = &mgmt->u.action.category + 1; 1333 event.rx_action.len = frame + len - event.rx_action.data; 1334 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event); 1335 } else { 1336 event.rx_mgmt.frame = frame; 1337 event.rx_mgmt.frame_len = len; 1338 event.rx_mgmt.ssi_signal = ssi_signal; 1339 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event); 1340 } 1341} 1342 1343 1344static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv, 1345 struct nlattr *cookie, const u8 *frame, 1346 size_t len, struct nlattr *ack) 1347{ 1348 union wpa_event_data event; 1349 const struct ieee80211_hdr *hdr; 1350 u16 fc; 1351 1352 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event"); 1353 if (!is_ap_interface(drv->nlmode)) { 1354 u64 cookie_val; 1355 1356 if (!cookie) 1357 return; 1358 1359 cookie_val = nla_get_u64(cookie); 1360 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:" 1361 " cookie=0%llx%s (ack=%d)", 1362 (long long unsigned int) cookie_val, 1363 cookie_val == drv->send_action_cookie ? 1364 " (match)" : " (unknown)", ack != NULL); 1365 if (cookie_val != drv->send_action_cookie) 1366 return; 1367 } 1368 1369 hdr = (const struct ieee80211_hdr *) frame; 1370 fc = le_to_host16(hdr->frame_control); 1371 1372 os_memset(&event, 0, sizeof(event)); 1373 event.tx_status.type = WLAN_FC_GET_TYPE(fc); 1374 event.tx_status.stype = WLAN_FC_GET_STYPE(fc); 1375 event.tx_status.dst = hdr->addr1; 1376 event.tx_status.data = frame; 1377 event.tx_status.data_len = len; 1378 event.tx_status.ack = ack != NULL; 1379 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event); 1380} 1381 1382 1383static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv, 1384 enum wpa_event_type type, 1385 const u8 *frame, size_t len) 1386{ 1387 const struct ieee80211_mgmt *mgmt; 1388 union wpa_event_data event; 1389 const u8 *bssid = NULL; 1390 u16 reason_code = 0; 1391 1392 if (type == EVENT_DEAUTH) 1393 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event"); 1394 else 1395 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event"); 1396 1397 mgmt = (const struct ieee80211_mgmt *) frame; 1398 if (len >= 24) { 1399 bssid = mgmt->bssid; 1400 1401 if (drv->associated != 0 && 1402 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 && 1403 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) { 1404 /* 1405 * We have presumably received this deauth as a 1406 * response to a clear_state_mismatch() outgoing 1407 * deauth. Don't let it take us offline! 1408 */ 1409 wpa_printf(MSG_DEBUG, "nl80211: Deauth received " 1410 "from Unknown BSSID " MACSTR " -- ignoring", 1411 MAC2STR(bssid)); 1412 return; 1413 } 1414 } 1415 1416 drv->associated = 0; 1417 os_memset(&event, 0, sizeof(event)); 1418 1419 /* Note: Same offset for Reason Code in both frame subtypes */ 1420 if (len >= 24 + sizeof(mgmt->u.deauth)) 1421 reason_code = le_to_host16(mgmt->u.deauth.reason_code); 1422 1423 if (type == EVENT_DISASSOC) { 1424 event.disassoc_info.locally_generated = 1425 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN); 1426 event.disassoc_info.addr = bssid; 1427 event.disassoc_info.reason_code = reason_code; 1428 if (frame + len > mgmt->u.disassoc.variable) { 1429 event.disassoc_info.ie = mgmt->u.disassoc.variable; 1430 event.disassoc_info.ie_len = frame + len - 1431 mgmt->u.disassoc.variable; 1432 } 1433 } else { 1434 event.deauth_info.locally_generated = 1435 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN); 1436 event.deauth_info.addr = bssid; 1437 event.deauth_info.reason_code = reason_code; 1438 if (frame + len > mgmt->u.deauth.variable) { 1439 event.deauth_info.ie = mgmt->u.deauth.variable; 1440 event.deauth_info.ie_len = frame + len - 1441 mgmt->u.deauth.variable; 1442 } 1443 } 1444 1445 wpa_supplicant_event(drv->ctx, type, &event); 1446} 1447 1448 1449static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv, 1450 enum wpa_event_type type, 1451 const u8 *frame, size_t len) 1452{ 1453 const struct ieee80211_mgmt *mgmt; 1454 union wpa_event_data event; 1455 u16 reason_code = 0; 1456 1457 if (type == EVENT_UNPROT_DEAUTH) 1458 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event"); 1459 else 1460 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event"); 1461 1462 if (len < 24) 1463 return; 1464 1465 mgmt = (const struct ieee80211_mgmt *) frame; 1466 1467 os_memset(&event, 0, sizeof(event)); 1468 /* Note: Same offset for Reason Code in both frame subtypes */ 1469 if (len >= 24 + sizeof(mgmt->u.deauth)) 1470 reason_code = le_to_host16(mgmt->u.deauth.reason_code); 1471 1472 if (type == EVENT_UNPROT_DISASSOC) { 1473 event.unprot_disassoc.sa = mgmt->sa; 1474 event.unprot_disassoc.da = mgmt->da; 1475 event.unprot_disassoc.reason_code = reason_code; 1476 } else { 1477 event.unprot_deauth.sa = mgmt->sa; 1478 event.unprot_deauth.da = mgmt->da; 1479 event.unprot_deauth.reason_code = reason_code; 1480 } 1481 1482 wpa_supplicant_event(drv->ctx, type, &event); 1483} 1484 1485 1486static void mlme_event(struct wpa_driver_nl80211_data *drv, 1487 enum nl80211_commands cmd, struct nlattr *frame, 1488 struct nlattr *addr, struct nlattr *timed_out, 1489 struct nlattr *freq, struct nlattr *ack, 1490 struct nlattr *cookie, struct nlattr *sig) 1491{ 1492 if (timed_out && addr) { 1493 mlme_timeout_event(drv, cmd, addr); 1494 return; 1495 } 1496 1497 if (frame == NULL) { 1498 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame " 1499 "data", cmd); 1500 return; 1501 } 1502 1503 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd); 1504 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame", 1505 nla_data(frame), nla_len(frame)); 1506 1507 switch (cmd) { 1508 case NL80211_CMD_AUTHENTICATE: 1509 mlme_event_auth(drv, nla_data(frame), nla_len(frame)); 1510 break; 1511 case NL80211_CMD_ASSOCIATE: 1512 mlme_event_assoc(drv, nla_data(frame), nla_len(frame)); 1513 break; 1514 case NL80211_CMD_DEAUTHENTICATE: 1515 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH, 1516 nla_data(frame), nla_len(frame)); 1517 break; 1518 case NL80211_CMD_DISASSOCIATE: 1519 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC, 1520 nla_data(frame), nla_len(frame)); 1521 break; 1522 case NL80211_CMD_FRAME: 1523 mlme_event_mgmt(drv, freq, sig, nla_data(frame), 1524 nla_len(frame)); 1525 break; 1526 case NL80211_CMD_FRAME_TX_STATUS: 1527 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame), 1528 nla_len(frame), ack); 1529 break; 1530 case NL80211_CMD_UNPROT_DEAUTHENTICATE: 1531 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH, 1532 nla_data(frame), nla_len(frame)); 1533 break; 1534 case NL80211_CMD_UNPROT_DISASSOCIATE: 1535 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC, 1536 nla_data(frame), nla_len(frame)); 1537 break; 1538 default: 1539 break; 1540 } 1541} 1542 1543 1544static void mlme_event_michael_mic_failure(struct i802_bss *bss, 1545 struct nlattr *tb[]) 1546{ 1547 union wpa_event_data data; 1548 1549 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure"); 1550 os_memset(&data, 0, sizeof(data)); 1551 if (tb[NL80211_ATTR_MAC]) { 1552 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address", 1553 nla_data(tb[NL80211_ATTR_MAC]), 1554 nla_len(tb[NL80211_ATTR_MAC])); 1555 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]); 1556 } 1557 if (tb[NL80211_ATTR_KEY_SEQ]) { 1558 wpa_hexdump(MSG_DEBUG, "nl80211: TSC", 1559 nla_data(tb[NL80211_ATTR_KEY_SEQ]), 1560 nla_len(tb[NL80211_ATTR_KEY_SEQ])); 1561 } 1562 if (tb[NL80211_ATTR_KEY_TYPE]) { 1563 enum nl80211_key_type key_type = 1564 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]); 1565 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type); 1566 if (key_type == NL80211_KEYTYPE_PAIRWISE) 1567 data.michael_mic_failure.unicast = 1; 1568 } else 1569 data.michael_mic_failure.unicast = 1; 1570 1571 if (tb[NL80211_ATTR_KEY_IDX]) { 1572 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]); 1573 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id); 1574 } 1575 1576 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data); 1577} 1578 1579 1580static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv, 1581 struct nlattr *tb[]) 1582{ 1583 if (tb[NL80211_ATTR_MAC] == NULL) { 1584 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined " 1585 "event"); 1586 return; 1587 } 1588 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 1589 drv->associated = 1; 1590 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined", 1591 MAC2STR(drv->bssid)); 1592 1593 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL); 1594} 1595 1596 1597static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv, 1598 int cancel_event, struct nlattr *tb[]) 1599{ 1600 unsigned int freq, chan_type, duration; 1601 union wpa_event_data data; 1602 u64 cookie; 1603 1604 if (tb[NL80211_ATTR_WIPHY_FREQ]) 1605 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]); 1606 else 1607 freq = 0; 1608 1609 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) 1610 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]); 1611 else 1612 chan_type = 0; 1613 1614 if (tb[NL80211_ATTR_DURATION]) 1615 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]); 1616 else 1617 duration = 0; 1618 1619 if (tb[NL80211_ATTR_COOKIE]) 1620 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]); 1621 else 1622 cookie = 0; 1623 1624 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d " 1625 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))", 1626 cancel_event, freq, chan_type, duration, 1627 (long long unsigned int) cookie, 1628 cookie == drv->remain_on_chan_cookie ? "match" : "unknown"); 1629 1630 if (cookie != drv->remain_on_chan_cookie) 1631 return; /* not for us */ 1632 1633 if (cancel_event) 1634 drv->pending_remain_on_chan = 0; 1635 1636 os_memset(&data, 0, sizeof(data)); 1637 data.remain_on_channel.freq = freq; 1638 data.remain_on_channel.duration = duration; 1639 wpa_supplicant_event(drv->ctx, cancel_event ? 1640 EVENT_CANCEL_REMAIN_ON_CHANNEL : 1641 EVENT_REMAIN_ON_CHANNEL, &data); 1642} 1643 1644 1645static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv, 1646 struct nlattr *tb[]) 1647{ 1648 union wpa_event_data data; 1649 1650 os_memset(&data, 0, sizeof(data)); 1651 1652 if (tb[NL80211_ATTR_IE]) { 1653 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]); 1654 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]); 1655 } 1656 1657 if (tb[NL80211_ATTR_IE_RIC]) { 1658 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]); 1659 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]); 1660 } 1661 1662 if (tb[NL80211_ATTR_MAC]) 1663 os_memcpy(data.ft_ies.target_ap, 1664 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 1665 1666 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR, 1667 MAC2STR(data.ft_ies.target_ap)); 1668 1669 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data); 1670} 1671 1672 1673static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted, 1674 struct nlattr *tb[]) 1675{ 1676 union wpa_event_data event; 1677 struct nlattr *nl; 1678 int rem; 1679 struct scan_info *info; 1680#define MAX_REPORT_FREQS 50 1681 int freqs[MAX_REPORT_FREQS]; 1682 int num_freqs = 0; 1683 1684 if (drv->scan_for_auth) { 1685 drv->scan_for_auth = 0; 1686 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing " 1687 "cfg80211 BSS entry"); 1688 wpa_driver_nl80211_authenticate_retry(drv); 1689 return; 1690 } 1691 1692 os_memset(&event, 0, sizeof(event)); 1693 info = &event.scan_info; 1694 info->aborted = aborted; 1695 1696 if (tb[NL80211_ATTR_SCAN_SSIDS]) { 1697 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) { 1698 struct wpa_driver_scan_ssid *s = 1699 &info->ssids[info->num_ssids]; 1700 s->ssid = nla_data(nl); 1701 s->ssid_len = nla_len(nl); 1702 info->num_ssids++; 1703 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS) 1704 break; 1705 } 1706 } 1707 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) { 1708 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem) 1709 { 1710 freqs[num_freqs] = nla_get_u32(nl); 1711 num_freqs++; 1712 if (num_freqs == MAX_REPORT_FREQS - 1) 1713 break; 1714 } 1715 info->freqs = freqs; 1716 info->num_freqs = num_freqs; 1717 } 1718 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event); 1719} 1720 1721 1722static int get_link_signal(struct nl_msg *msg, void *arg) 1723{ 1724 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 1725 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 1726 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; 1727 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = { 1728 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, 1729 }; 1730 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; 1731 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { 1732 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, 1733 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, 1734 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, 1735 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, 1736 }; 1737 struct wpa_signal_info *sig_change = arg; 1738 1739 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 1740 genlmsg_attrlen(gnlh, 0), NULL); 1741 if (!tb[NL80211_ATTR_STA_INFO] || 1742 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, 1743 tb[NL80211_ATTR_STA_INFO], policy)) 1744 return NL_SKIP; 1745 if (!sinfo[NL80211_STA_INFO_SIGNAL]) 1746 return NL_SKIP; 1747 1748 sig_change->current_signal = 1749 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); 1750 1751 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) { 1752 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, 1753 sinfo[NL80211_STA_INFO_TX_BITRATE], 1754 rate_policy)) { 1755 sig_change->current_txrate = 0; 1756 } else { 1757 if (rinfo[NL80211_RATE_INFO_BITRATE]) { 1758 sig_change->current_txrate = 1759 nla_get_u16(rinfo[ 1760 NL80211_RATE_INFO_BITRATE]) * 100; 1761 } 1762 } 1763 } 1764 1765 return NL_SKIP; 1766} 1767 1768 1769static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv, 1770 struct wpa_signal_info *sig) 1771{ 1772 struct nl_msg *msg; 1773 1774 sig->current_signal = -9999; 1775 sig->current_txrate = 0; 1776 1777 msg = nlmsg_alloc(); 1778 if (!msg) 1779 return -ENOMEM; 1780 1781 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION); 1782 1783 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 1784 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid); 1785 1786 return send_and_recv_msgs(drv, msg, get_link_signal, sig); 1787 nla_put_failure: 1788 nlmsg_free(msg); 1789 return -ENOBUFS; 1790} 1791 1792 1793static int get_link_noise(struct nl_msg *msg, void *arg) 1794{ 1795 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 1796 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 1797 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; 1798 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { 1799 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, 1800 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, 1801 }; 1802 struct wpa_signal_info *sig_change = arg; 1803 1804 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 1805 genlmsg_attrlen(gnlh, 0), NULL); 1806 1807 if (!tb[NL80211_ATTR_SURVEY_INFO]) { 1808 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!"); 1809 return NL_SKIP; 1810 } 1811 1812 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, 1813 tb[NL80211_ATTR_SURVEY_INFO], 1814 survey_policy)) { 1815 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested " 1816 "attributes!"); 1817 return NL_SKIP; 1818 } 1819 1820 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) 1821 return NL_SKIP; 1822 1823 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) != 1824 sig_change->frequency) 1825 return NL_SKIP; 1826 1827 if (!sinfo[NL80211_SURVEY_INFO_NOISE]) 1828 return NL_SKIP; 1829 1830 sig_change->current_noise = 1831 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); 1832 1833 return NL_SKIP; 1834} 1835 1836 1837static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv, 1838 struct wpa_signal_info *sig_change) 1839{ 1840 struct nl_msg *msg; 1841 1842 sig_change->current_noise = 9999; 1843 sig_change->frequency = drv->assoc_freq; 1844 1845 msg = nlmsg_alloc(); 1846 if (!msg) 1847 return -ENOMEM; 1848 1849 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY); 1850 1851 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 1852 1853 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change); 1854 nla_put_failure: 1855 nlmsg_free(msg); 1856 return -ENOBUFS; 1857} 1858 1859 1860static int get_noise_for_scan_results(struct nl_msg *msg, void *arg) 1861{ 1862 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 1863 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 1864 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; 1865 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { 1866 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, 1867 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, 1868 }; 1869 struct wpa_scan_results *scan_results = arg; 1870 struct wpa_scan_res *scan_res; 1871 size_t i; 1872 1873 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 1874 genlmsg_attrlen(gnlh, 0), NULL); 1875 1876 if (!tb[NL80211_ATTR_SURVEY_INFO]) { 1877 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing"); 1878 return NL_SKIP; 1879 } 1880 1881 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, 1882 tb[NL80211_ATTR_SURVEY_INFO], 1883 survey_policy)) { 1884 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested " 1885 "attributes"); 1886 return NL_SKIP; 1887 } 1888 1889 if (!sinfo[NL80211_SURVEY_INFO_NOISE]) 1890 return NL_SKIP; 1891 1892 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) 1893 return NL_SKIP; 1894 1895 for (i = 0; i < scan_results->num; ++i) { 1896 scan_res = scan_results->res[i]; 1897 if (!scan_res) 1898 continue; 1899 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) != 1900 scan_res->freq) 1901 continue; 1902 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID)) 1903 continue; 1904 scan_res->noise = (s8) 1905 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); 1906 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID; 1907 } 1908 1909 return NL_SKIP; 1910} 1911 1912 1913static int nl80211_get_noise_for_scan_results( 1914 struct wpa_driver_nl80211_data *drv, 1915 struct wpa_scan_results *scan_res) 1916{ 1917 struct nl_msg *msg; 1918 1919 msg = nlmsg_alloc(); 1920 if (!msg) 1921 return -ENOMEM; 1922 1923 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY); 1924 1925 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 1926 1927 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results, 1928 scan_res); 1929 nla_put_failure: 1930 nlmsg_free(msg); 1931 return -ENOBUFS; 1932} 1933 1934 1935static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv, 1936 struct nlattr *tb[]) 1937{ 1938 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = { 1939 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 }, 1940 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 }, 1941 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 }, 1942 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 }, 1943 }; 1944 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1]; 1945 enum nl80211_cqm_rssi_threshold_event event; 1946 union wpa_event_data ed; 1947 struct wpa_signal_info sig; 1948 int res; 1949 1950 if (tb[NL80211_ATTR_CQM] == NULL || 1951 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM], 1952 cqm_policy)) { 1953 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event"); 1954 return; 1955 } 1956 1957 os_memset(&ed, 0, sizeof(ed)); 1958 1959 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) { 1960 if (!tb[NL80211_ATTR_MAC]) 1961 return; 1962 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]), 1963 ETH_ALEN); 1964 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed); 1965 return; 1966 } 1967 1968 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL) 1969 return; 1970 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]); 1971 1972 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) { 1973 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor " 1974 "event: RSSI high"); 1975 ed.signal_change.above_threshold = 1; 1976 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) { 1977 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor " 1978 "event: RSSI low"); 1979 ed.signal_change.above_threshold = 0; 1980 } else 1981 return; 1982 1983 res = nl80211_get_link_signal(drv, &sig); 1984 if (res == 0) { 1985 ed.signal_change.current_signal = sig.current_signal; 1986 ed.signal_change.current_txrate = sig.current_txrate; 1987 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d", 1988 sig.current_signal, sig.current_txrate); 1989 } 1990 1991 res = nl80211_get_link_noise(drv, &sig); 1992 if (res == 0) { 1993 ed.signal_change.current_noise = sig.current_noise; 1994 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm", 1995 sig.current_noise); 1996 } 1997 1998 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed); 1999} 2000 2001 2002static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv, 2003 struct nlattr **tb) 2004{ 2005 u8 *addr; 2006 union wpa_event_data data; 2007 2008 if (tb[NL80211_ATTR_MAC] == NULL) 2009 return; 2010 addr = nla_data(tb[NL80211_ATTR_MAC]); 2011 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr)); 2012 2013 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) { 2014 u8 *ies = NULL; 2015 size_t ies_len = 0; 2016 if (tb[NL80211_ATTR_IE]) { 2017 ies = nla_data(tb[NL80211_ATTR_IE]); 2018 ies_len = nla_len(tb[NL80211_ATTR_IE]); 2019 } 2020 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len); 2021 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0); 2022 return; 2023 } 2024 2025 if (drv->nlmode != NL80211_IFTYPE_ADHOC) 2026 return; 2027 2028 os_memset(&data, 0, sizeof(data)); 2029 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN); 2030 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data); 2031} 2032 2033 2034static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv, 2035 struct nlattr **tb) 2036{ 2037 u8 *addr; 2038 union wpa_event_data data; 2039 2040 if (tb[NL80211_ATTR_MAC] == NULL) 2041 return; 2042 addr = nla_data(tb[NL80211_ATTR_MAC]); 2043 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR, 2044 MAC2STR(addr)); 2045 2046 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) { 2047 drv_event_disassoc(drv->ctx, addr); 2048 return; 2049 } 2050 2051 if (drv->nlmode != NL80211_IFTYPE_ADHOC) 2052 return; 2053 2054 os_memset(&data, 0, sizeof(data)); 2055 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN); 2056 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data); 2057} 2058 2059 2060static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv, 2061 struct nlattr **tb) 2062{ 2063 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA]; 2064 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = { 2065 [NL80211_REKEY_DATA_KEK] = { 2066 .minlen = NL80211_KEK_LEN, 2067 .maxlen = NL80211_KEK_LEN, 2068 }, 2069 [NL80211_REKEY_DATA_KCK] = { 2070 .minlen = NL80211_KCK_LEN, 2071 .maxlen = NL80211_KCK_LEN, 2072 }, 2073 [NL80211_REKEY_DATA_REPLAY_CTR] = { 2074 .minlen = NL80211_REPLAY_CTR_LEN, 2075 .maxlen = NL80211_REPLAY_CTR_LEN, 2076 }, 2077 }; 2078 union wpa_event_data data; 2079 2080 if (!tb[NL80211_ATTR_MAC]) 2081 return; 2082 if (!tb[NL80211_ATTR_REKEY_DATA]) 2083 return; 2084 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA, 2085 tb[NL80211_ATTR_REKEY_DATA], rekey_policy)) 2086 return; 2087 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]) 2088 return; 2089 2090 os_memset(&data, 0, sizeof(data)); 2091 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]); 2092 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR, 2093 MAC2STR(data.driver_gtk_rekey.bssid)); 2094 data.driver_gtk_rekey.replay_ctr = 2095 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]); 2096 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter", 2097 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN); 2098 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data); 2099} 2100 2101 2102static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv, 2103 struct nlattr **tb) 2104{ 2105 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE]; 2106 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = { 2107 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 }, 2108 [NL80211_PMKSA_CANDIDATE_BSSID] = { 2109 .minlen = ETH_ALEN, 2110 .maxlen = ETH_ALEN, 2111 }, 2112 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG }, 2113 }; 2114 union wpa_event_data data; 2115 2116 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event"); 2117 2118 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE]) 2119 return; 2120 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE, 2121 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy)) 2122 return; 2123 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] || 2124 !cand[NL80211_PMKSA_CANDIDATE_BSSID]) 2125 return; 2126 2127 os_memset(&data, 0, sizeof(data)); 2128 os_memcpy(data.pmkid_candidate.bssid, 2129 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN); 2130 data.pmkid_candidate.index = 2131 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]); 2132 data.pmkid_candidate.preauth = 2133 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL; 2134 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data); 2135} 2136 2137 2138static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv, 2139 struct nlattr **tb) 2140{ 2141 union wpa_event_data data; 2142 2143 wpa_printf(MSG_DEBUG, "nl80211: Probe client event"); 2144 2145 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK]) 2146 return; 2147 2148 os_memset(&data, 0, sizeof(data)); 2149 os_memcpy(data.client_poll.addr, 2150 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 2151 2152 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data); 2153} 2154 2155 2156static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv, 2157 struct nlattr **tb) 2158{ 2159 union wpa_event_data data; 2160 2161 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event"); 2162 2163 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION]) 2164 return; 2165 2166 os_memset(&data, 0, sizeof(data)); 2167 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 2168 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) { 2169 case NL80211_TDLS_SETUP: 2170 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer " 2171 MACSTR, MAC2STR(data.tdls.peer)); 2172 data.tdls.oper = TDLS_REQUEST_SETUP; 2173 break; 2174 case NL80211_TDLS_TEARDOWN: 2175 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer " 2176 MACSTR, MAC2STR(data.tdls.peer)); 2177 data.tdls.oper = TDLS_REQUEST_TEARDOWN; 2178 break; 2179 default: 2180 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione " 2181 "event"); 2182 return; 2183 } 2184 if (tb[NL80211_ATTR_REASON_CODE]) { 2185 data.tdls.reason_code = 2186 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]); 2187 } 2188 2189 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data); 2190} 2191 2192 2193static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv, 2194 struct nlattr **tb) 2195{ 2196 union wpa_event_data data; 2197 u32 reason; 2198 2199 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event"); 2200 2201 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON]) 2202 return; 2203 2204 os_memset(&data, 0, sizeof(data)); 2205 os_memcpy(data.connect_failed_reason.addr, 2206 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 2207 2208 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]); 2209 switch (reason) { 2210 case NL80211_CONN_FAIL_MAX_CLIENTS: 2211 wpa_printf(MSG_DEBUG, "nl80211: Max client reached"); 2212 data.connect_failed_reason.code = MAX_CLIENT_REACHED; 2213 break; 2214 case NL80211_CONN_FAIL_BLOCKED_CLIENT: 2215 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR 2216 " tried to connect", 2217 MAC2STR(data.connect_failed_reason.addr)); 2218 data.connect_failed_reason.code = BLOCKED_CLIENT; 2219 break; 2220 default: 2221 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason " 2222 "%u", reason); 2223 return; 2224 } 2225 2226 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data); 2227} 2228 2229 2230static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb, 2231 int wds) 2232{ 2233 struct wpa_driver_nl80211_data *drv = bss->drv; 2234 union wpa_event_data event; 2235 2236 if (!tb[NL80211_ATTR_MAC]) 2237 return; 2238 2239 os_memset(&event, 0, sizeof(event)); 2240 event.rx_from_unknown.bssid = bss->addr; 2241 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]); 2242 event.rx_from_unknown.wds = wds; 2243 2244 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event); 2245} 2246 2247 2248static void do_process_drv_event(struct i802_bss *bss, int cmd, 2249 struct nlattr **tb) 2250{ 2251 struct wpa_driver_nl80211_data *drv = bss->drv; 2252 2253 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED && 2254 (cmd == NL80211_CMD_NEW_SCAN_RESULTS || 2255 cmd == NL80211_CMD_SCAN_ABORTED)) { 2256 wpa_driver_nl80211_set_mode(&drv->first_bss, 2257 drv->ap_scan_as_station); 2258 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED; 2259 } 2260 2261 switch (cmd) { 2262 case NL80211_CMD_TRIGGER_SCAN: 2263 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger"); 2264 break; 2265 case NL80211_CMD_START_SCHED_SCAN: 2266 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started"); 2267 break; 2268 case NL80211_CMD_SCHED_SCAN_STOPPED: 2269 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped"); 2270 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL); 2271 break; 2272 case NL80211_CMD_NEW_SCAN_RESULTS: 2273 wpa_dbg(drv->ctx, MSG_DEBUG, 2274 "nl80211: New scan results available"); 2275 drv->scan_complete_events = 1; 2276 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, 2277 drv->ctx); 2278 send_scan_event(drv, 0, tb); 2279 break; 2280 case NL80211_CMD_SCHED_SCAN_RESULTS: 2281 wpa_dbg(drv->ctx, MSG_DEBUG, 2282 "nl80211: New sched scan results available"); 2283 send_scan_event(drv, 0, tb); 2284 break; 2285 case NL80211_CMD_SCAN_ABORTED: 2286 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted"); 2287 /* 2288 * Need to indicate that scan results are available in order 2289 * not to make wpa_supplicant stop its scanning. 2290 */ 2291 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, 2292 drv->ctx); 2293 send_scan_event(drv, 1, tb); 2294 break; 2295 case NL80211_CMD_AUTHENTICATE: 2296 case NL80211_CMD_ASSOCIATE: 2297 case NL80211_CMD_DEAUTHENTICATE: 2298 case NL80211_CMD_DISASSOCIATE: 2299 case NL80211_CMD_FRAME_TX_STATUS: 2300 case NL80211_CMD_UNPROT_DEAUTHENTICATE: 2301 case NL80211_CMD_UNPROT_DISASSOCIATE: 2302 mlme_event(drv, cmd, tb[NL80211_ATTR_FRAME], 2303 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT], 2304 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK], 2305 tb[NL80211_ATTR_COOKIE], 2306 tb[NL80211_ATTR_RX_SIGNAL_DBM]); 2307 break; 2308 case NL80211_CMD_CONNECT: 2309 case NL80211_CMD_ROAM: 2310 mlme_event_connect(drv, cmd, 2311 tb[NL80211_ATTR_STATUS_CODE], 2312 tb[NL80211_ATTR_MAC], 2313 tb[NL80211_ATTR_REQ_IE], 2314 tb[NL80211_ATTR_RESP_IE]); 2315 break; 2316 case NL80211_CMD_CH_SWITCH_NOTIFY: 2317 mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ], 2318 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]); 2319 break; 2320 case NL80211_CMD_DISCONNECT: 2321 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE], 2322 tb[NL80211_ATTR_MAC], 2323 tb[NL80211_ATTR_DISCONNECTED_BY_AP]); 2324 break; 2325 case NL80211_CMD_MICHAEL_MIC_FAILURE: 2326 mlme_event_michael_mic_failure(bss, tb); 2327 break; 2328 case NL80211_CMD_JOIN_IBSS: 2329 mlme_event_join_ibss(drv, tb); 2330 break; 2331 case NL80211_CMD_REMAIN_ON_CHANNEL: 2332 mlme_event_remain_on_channel(drv, 0, tb); 2333 break; 2334 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL: 2335 mlme_event_remain_on_channel(drv, 1, tb); 2336 break; 2337 case NL80211_CMD_NOTIFY_CQM: 2338 nl80211_cqm_event(drv, tb); 2339 break; 2340 case NL80211_CMD_REG_CHANGE: 2341 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change"); 2342 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, 2343 NULL); 2344 break; 2345 case NL80211_CMD_REG_BEACON_HINT: 2346 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint"); 2347 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, 2348 NULL); 2349 break; 2350 case NL80211_CMD_NEW_STATION: 2351 nl80211_new_station_event(drv, tb); 2352 break; 2353 case NL80211_CMD_DEL_STATION: 2354 nl80211_del_station_event(drv, tb); 2355 break; 2356 case NL80211_CMD_SET_REKEY_OFFLOAD: 2357 nl80211_rekey_offload_event(drv, tb); 2358 break; 2359 case NL80211_CMD_PMKSA_CANDIDATE: 2360 nl80211_pmksa_candidate_event(drv, tb); 2361 break; 2362 case NL80211_CMD_PROBE_CLIENT: 2363 nl80211_client_probe_event(drv, tb); 2364 break; 2365 case NL80211_CMD_TDLS_OPER: 2366 nl80211_tdls_oper_event(drv, tb); 2367 break; 2368 case NL80211_CMD_CONN_FAILED: 2369 nl80211_connect_failed_event(drv, tb); 2370 break; 2371 case NL80211_CMD_FT_EVENT: 2372 mlme_event_ft_event(drv, tb); 2373 break; 2374 default: 2375 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event " 2376 "(cmd=%d)", cmd); 2377 break; 2378 } 2379} 2380 2381 2382static int process_drv_event(struct nl_msg *msg, void *arg) 2383{ 2384 struct wpa_driver_nl80211_data *drv = arg; 2385 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2386 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 2387 struct i802_bss *bss; 2388 int ifidx = -1; 2389 2390 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2391 genlmsg_attrlen(gnlh, 0), NULL); 2392 2393 if (tb[NL80211_ATTR_IFINDEX]) 2394 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]); 2395 2396 for (bss = &drv->first_bss; bss; bss = bss->next) { 2397 if (ifidx == -1 || ifidx == bss->ifindex) { 2398 do_process_drv_event(bss, gnlh->cmd, tb); 2399 return NL_SKIP; 2400 } 2401 } 2402 2403 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d) for foreign " 2404 "interface (ifindex %d)", gnlh->cmd, ifidx); 2405 2406 return NL_SKIP; 2407} 2408 2409 2410static int process_global_event(struct nl_msg *msg, void *arg) 2411{ 2412 struct nl80211_global *global = arg; 2413 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2414 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 2415 struct wpa_driver_nl80211_data *drv, *tmp; 2416 int ifidx = -1; 2417 struct i802_bss *bss; 2418 2419 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2420 genlmsg_attrlen(gnlh, 0), NULL); 2421 2422 if (tb[NL80211_ATTR_IFINDEX]) 2423 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]); 2424 2425 dl_list_for_each_safe(drv, tmp, &global->interfaces, 2426 struct wpa_driver_nl80211_data, list) { 2427 for (bss = &drv->first_bss; bss; bss = bss->next) { 2428 if (ifidx == -1 || ifidx == bss->ifindex) { 2429 do_process_drv_event(bss, gnlh->cmd, tb); 2430 return NL_SKIP; 2431 } 2432 } 2433 } 2434 2435 return NL_SKIP; 2436} 2437 2438 2439static int process_bss_event(struct nl_msg *msg, void *arg) 2440{ 2441 struct i802_bss *bss = arg; 2442 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2443 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 2444 2445 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2446 genlmsg_attrlen(gnlh, 0), NULL); 2447 2448 switch (gnlh->cmd) { 2449 case NL80211_CMD_FRAME: 2450 case NL80211_CMD_FRAME_TX_STATUS: 2451 mlme_event(bss->drv, gnlh->cmd, tb[NL80211_ATTR_FRAME], 2452 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT], 2453 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK], 2454 tb[NL80211_ATTR_COOKIE], 2455 tb[NL80211_ATTR_RX_SIGNAL_DBM]); 2456 break; 2457 case NL80211_CMD_UNEXPECTED_FRAME: 2458 nl80211_spurious_frame(bss, tb, 0); 2459 break; 2460 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME: 2461 nl80211_spurious_frame(bss, tb, 1); 2462 break; 2463 default: 2464 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event " 2465 "(cmd=%d)", gnlh->cmd); 2466 break; 2467 } 2468 2469 return NL_SKIP; 2470} 2471 2472 2473static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx, 2474 void *handle) 2475{ 2476 struct nl_cb *cb = eloop_ctx; 2477 2478 wpa_printf(MSG_DEBUG, "nl80211: Event message available"); 2479 2480 nl_recvmsgs(handle, cb); 2481} 2482 2483 2484/** 2485 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain 2486 * @priv: driver_nl80211 private data 2487 * @alpha2_arg: country to which to switch to 2488 * Returns: 0 on success, -1 on failure 2489 * 2490 * This asks nl80211 to set the regulatory domain for given 2491 * country ISO / IEC alpha2. 2492 */ 2493static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg) 2494{ 2495 struct i802_bss *bss = priv; 2496 struct wpa_driver_nl80211_data *drv = bss->drv; 2497 char alpha2[3]; 2498 struct nl_msg *msg; 2499 2500 msg = nlmsg_alloc(); 2501 if (!msg) 2502 return -ENOMEM; 2503 2504 alpha2[0] = alpha2_arg[0]; 2505 alpha2[1] = alpha2_arg[1]; 2506 alpha2[2] = '\0'; 2507 2508 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG); 2509 2510 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2); 2511 if (send_and_recv_msgs(drv, msg, NULL, NULL)) 2512 return -EINVAL; 2513 return 0; 2514nla_put_failure: 2515 nlmsg_free(msg); 2516 return -EINVAL; 2517} 2518 2519 2520static int protocol_feature_handler(struct nl_msg *msg, void *arg) 2521{ 2522 u32 *feat = arg; 2523 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; 2524 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2525 2526 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2527 genlmsg_attrlen(gnlh, 0), NULL); 2528 2529 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]) 2530 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]); 2531 2532 return NL_SKIP; 2533} 2534 2535 2536static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv) 2537{ 2538 u32 feat = 0; 2539 struct nl_msg *msg; 2540 2541 msg = nlmsg_alloc(); 2542 if (!msg) 2543 goto nla_put_failure; 2544 2545 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES); 2546 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0) 2547 return feat; 2548 2549 msg = NULL; 2550nla_put_failure: 2551 nlmsg_free(msg); 2552 return 0; 2553} 2554 2555 2556struct wiphy_info_data { 2557 struct wpa_driver_capa *capa; 2558 2559 unsigned int error:1; 2560 unsigned int device_ap_sme:1; 2561 unsigned int poll_command_supported:1; 2562 unsigned int data_tx_status:1; 2563 unsigned int monitor_supported:1; 2564 unsigned int auth_supported:1; 2565 unsigned int connect_supported:1; 2566 unsigned int p2p_go_supported:1; 2567 unsigned int p2p_client_supported:1; 2568 unsigned int p2p_concurrent:1; 2569 unsigned int p2p_multichan_concurrent:1; 2570}; 2571 2572 2573static unsigned int probe_resp_offload_support(int supp_protocols) 2574{ 2575 unsigned int prot = 0; 2576 2577 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS) 2578 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS; 2579 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2) 2580 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2; 2581 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P) 2582 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P; 2583 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U) 2584 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING; 2585 2586 return prot; 2587} 2588 2589 2590static void wiphy_info_supported_iftypes(struct wiphy_info_data *info, 2591 struct nlattr *tb) 2592{ 2593 struct nlattr *nl_mode; 2594 int i; 2595 2596 if (tb == NULL) 2597 return; 2598 2599 nla_for_each_nested(nl_mode, tb, i) { 2600 switch (nla_type(nl_mode)) { 2601 case NL80211_IFTYPE_AP: 2602 info->capa->flags |= WPA_DRIVER_FLAGS_AP; 2603 break; 2604 case NL80211_IFTYPE_ADHOC: 2605 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS; 2606 break; 2607 case NL80211_IFTYPE_P2P_GO: 2608 info->p2p_go_supported = 1; 2609 break; 2610 case NL80211_IFTYPE_P2P_CLIENT: 2611 info->p2p_client_supported = 1; 2612 break; 2613 case NL80211_IFTYPE_MONITOR: 2614 info->monitor_supported = 1; 2615 break; 2616 } 2617 } 2618} 2619 2620 2621static int wiphy_info_iface_comb_process(struct wiphy_info_data *info, 2622 struct nlattr *nl_combi) 2623{ 2624 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB]; 2625 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT]; 2626 struct nlattr *nl_limit, *nl_mode; 2627 int err, rem_limit, rem_mode; 2628 int combination_has_p2p = 0, combination_has_mgd = 0; 2629 static struct nla_policy 2630 iface_combination_policy[NUM_NL80211_IFACE_COMB] = { 2631 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED }, 2632 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 }, 2633 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG }, 2634 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 }, 2635 }, 2636 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = { 2637 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED }, 2638 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 }, 2639 }; 2640 2641 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB, 2642 nl_combi, iface_combination_policy); 2643 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] || 2644 !tb_comb[NL80211_IFACE_COMB_MAXNUM] || 2645 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) 2646 return 0; /* broken combination */ 2647 2648 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS], 2649 rem_limit) { 2650 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT, 2651 nl_limit, iface_limit_policy); 2652 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES]) 2653 return 0; /* broken combination */ 2654 2655 nla_for_each_nested(nl_mode, 2656 tb_limit[NL80211_IFACE_LIMIT_TYPES], 2657 rem_mode) { 2658 int ift = nla_type(nl_mode); 2659 if (ift == NL80211_IFTYPE_P2P_GO || 2660 ift == NL80211_IFTYPE_P2P_CLIENT) 2661 combination_has_p2p = 1; 2662 if (ift == NL80211_IFTYPE_STATION) 2663 combination_has_mgd = 1; 2664 } 2665 if (combination_has_p2p && combination_has_mgd) 2666 break; 2667 } 2668 2669 if (combination_has_p2p && combination_has_mgd) { 2670 info->p2p_concurrent = 1; 2671 if (nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) > 1) 2672 info->p2p_multichan_concurrent = 1; 2673 return 1; 2674 } 2675 2676 return 0; 2677} 2678 2679 2680static void wiphy_info_iface_comb(struct wiphy_info_data *info, 2681 struct nlattr *tb) 2682{ 2683 struct nlattr *nl_combi; 2684 int rem_combi; 2685 2686 if (tb == NULL) 2687 return; 2688 2689 nla_for_each_nested(nl_combi, tb, rem_combi) { 2690 if (wiphy_info_iface_comb_process(info, nl_combi) > 0) 2691 break; 2692 } 2693} 2694 2695 2696static void wiphy_info_supp_cmds(struct wiphy_info_data *info, 2697 struct nlattr *tb) 2698{ 2699 struct nlattr *nl_cmd; 2700 int i; 2701 2702 if (tb == NULL) 2703 return; 2704 2705 nla_for_each_nested(nl_cmd, tb, i) { 2706 switch (nla_get_u32(nl_cmd)) { 2707 case NL80211_CMD_AUTHENTICATE: 2708 info->auth_supported = 1; 2709 break; 2710 case NL80211_CMD_CONNECT: 2711 info->connect_supported = 1; 2712 break; 2713 case NL80211_CMD_START_SCHED_SCAN: 2714 info->capa->sched_scan_supported = 1; 2715 break; 2716 case NL80211_CMD_PROBE_CLIENT: 2717 info->poll_command_supported = 1; 2718 break; 2719 } 2720 } 2721} 2722 2723 2724static void wiphy_info_max_roc(struct wpa_driver_capa *capa, 2725 struct nlattr *tb) 2726{ 2727 /* default to 5000 since early versions of mac80211 don't set it */ 2728 capa->max_remain_on_chan = 5000; 2729 2730 if (tb) 2731 capa->max_remain_on_chan = nla_get_u32(tb); 2732} 2733 2734 2735static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls, 2736 struct nlattr *ext_setup) 2737{ 2738 if (tdls == NULL) 2739 return; 2740 2741 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported"); 2742 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT; 2743 2744 if (ext_setup) { 2745 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup"); 2746 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP; 2747 } 2748} 2749 2750 2751static void wiphy_info_feature_flags(struct wiphy_info_data *info, 2752 struct nlattr *tb) 2753{ 2754 u32 flags; 2755 struct wpa_driver_capa *capa = info->capa; 2756 2757 if (tb == NULL) 2758 return; 2759 2760 flags = nla_get_u32(tb); 2761 2762 if (flags & NL80211_FEATURE_SK_TX_STATUS) 2763 info->data_tx_status = 1; 2764 2765 if (flags & NL80211_FEATURE_INACTIVITY_TIMER) 2766 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER; 2767 2768 if (flags & NL80211_FEATURE_SAE) 2769 capa->flags |= WPA_DRIVER_FLAGS_SAE; 2770 2771 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN) 2772 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN; 2773} 2774 2775 2776static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa, 2777 struct nlattr *tb) 2778{ 2779 u32 protocols; 2780 2781 if (tb == NULL) 2782 return; 2783 2784 protocols = nla_get_u32(tb); 2785 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP " 2786 "mode"); 2787 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD; 2788 capa->probe_resp_offloads = probe_resp_offload_support(protocols); 2789} 2790 2791 2792static int wiphy_info_handler(struct nl_msg *msg, void *arg) 2793{ 2794 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 2795 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2796 struct wiphy_info_data *info = arg; 2797 struct wpa_driver_capa *capa = info->capa; 2798 2799 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2800 genlmsg_attrlen(gnlh, 0), NULL); 2801 2802 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]) 2803 capa->max_scan_ssids = 2804 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]); 2805 2806 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]) 2807 capa->max_sched_scan_ssids = 2808 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]); 2809 2810 if (tb[NL80211_ATTR_MAX_MATCH_SETS]) 2811 capa->max_match_sets = 2812 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]); 2813 2814 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]); 2815 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]); 2816 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]); 2817 2818 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) { 2819 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based " 2820 "off-channel TX"); 2821 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX; 2822 } 2823 2824 if (tb[NL80211_ATTR_ROAM_SUPPORT]) { 2825 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming"); 2826 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION; 2827 } 2828 2829 wiphy_info_max_roc(capa, 2830 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]); 2831 2832 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD]) 2833 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD; 2834 2835 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT], 2836 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]); 2837 2838 if (tb[NL80211_ATTR_DEVICE_AP_SME]) 2839 info->device_ap_sme = 1; 2840 2841 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]); 2842 wiphy_info_probe_resp_offload(capa, 2843 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]); 2844 2845 return NL_SKIP; 2846} 2847 2848 2849static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv, 2850 struct wiphy_info_data *info) 2851{ 2852 u32 feat; 2853 struct nl_msg *msg; 2854 2855 os_memset(info, 0, sizeof(*info)); 2856 info->capa = &drv->capa; 2857 2858 msg = nlmsg_alloc(); 2859 if (!msg) 2860 return -1; 2861 2862 feat = get_nl80211_protocol_features(drv); 2863 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP) 2864 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY); 2865 else 2866 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY); 2867 2868 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); 2869 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex); 2870 2871 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info)) 2872 return -1; 2873 2874 if (info->auth_supported) 2875 drv->capa.flags |= WPA_DRIVER_FLAGS_SME; 2876 else if (!info->connect_supported) { 2877 wpa_printf(MSG_INFO, "nl80211: Driver does not support " 2878 "authentication/association or connect commands"); 2879 info->error = 1; 2880 } 2881 2882 if (info->p2p_go_supported && info->p2p_client_supported) 2883 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE; 2884 if (info->p2p_concurrent) { 2885 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group " 2886 "interface (driver advertised support)"); 2887 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT; 2888 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P; 2889 } 2890 if (info->p2p_multichan_concurrent) { 2891 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel " 2892 "concurrent (driver advertised support)"); 2893 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT; 2894 } 2895 return 0; 2896nla_put_failure: 2897 nlmsg_free(msg); 2898 return -1; 2899} 2900 2901 2902static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv) 2903{ 2904 struct wiphy_info_data info; 2905 if (wpa_driver_nl80211_get_info(drv, &info)) 2906 return -1; 2907 2908 if (info.error) 2909 return -1; 2910 2911 drv->has_capability = 1; 2912 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */ 2913 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | 2914 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK | 2915 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | 2916 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; 2917 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 | 2918 WPA_DRIVER_CAPA_ENC_WEP104 | 2919 WPA_DRIVER_CAPA_ENC_TKIP | 2920 WPA_DRIVER_CAPA_ENC_CCMP; 2921 drv->capa.auth = WPA_DRIVER_AUTH_OPEN | 2922 WPA_DRIVER_AUTH_SHARED | 2923 WPA_DRIVER_AUTH_LEAP; 2924 2925 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES; 2926 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE; 2927 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS; 2928 2929 if (!info.device_ap_sme) { 2930 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS; 2931 2932 /* 2933 * No AP SME is currently assumed to also indicate no AP MLME 2934 * in the driver/firmware. 2935 */ 2936 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME; 2937 } 2938 2939 drv->device_ap_sme = info.device_ap_sme; 2940 drv->poll_command_supported = info.poll_command_supported; 2941 drv->data_tx_status = info.data_tx_status; 2942 2943#ifdef ANDROID_P2P 2944 if(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) { 2945 /* Driver is new enough to support monitorless mode*/ 2946 wpa_printf(MSG_DEBUG, "nl80211: Driver is new " 2947 "enough to support monitor-less mode"); 2948 drv->use_monitor = 0; 2949 } 2950#else 2951 /* 2952 * If poll command and tx status are supported, mac80211 is new enough 2953 * to have everything we need to not need monitor interfaces. 2954 */ 2955 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status; 2956#endif 2957 2958 if (drv->device_ap_sme && drv->use_monitor) { 2959 /* 2960 * Non-mac80211 drivers may not support monitor interface. 2961 * Make sure we do not get stuck with incorrect capability here 2962 * by explicitly testing this. 2963 */ 2964 if (!info.monitor_supported) { 2965 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor " 2966 "with device_ap_sme since no monitor mode " 2967 "support detected"); 2968 drv->use_monitor = 0; 2969 } 2970 } 2971 2972 /* 2973 * If we aren't going to use monitor interfaces, but the 2974 * driver doesn't support data TX status, we won't get TX 2975 * status for EAPOL frames. 2976 */ 2977 if (!drv->use_monitor && !info.data_tx_status) 2978 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS; 2979 2980 return 0; 2981} 2982 2983 2984#ifdef ANDROID 2985static int android_genl_ctrl_resolve(struct nl_handle *handle, 2986 const char *name) 2987{ 2988 /* 2989 * Android ICS has very minimal genl_ctrl_resolve() implementation, so 2990 * need to work around that. 2991 */ 2992 struct nl_cache *cache = NULL; 2993 struct genl_family *nl80211 = NULL; 2994 int id = -1; 2995 2996 if (genl_ctrl_alloc_cache(handle, &cache) < 0) { 2997 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic " 2998 "netlink cache"); 2999 goto fail; 3000 } 3001 3002 nl80211 = genl_ctrl_search_by_name(cache, name); 3003 if (nl80211 == NULL) 3004 goto fail; 3005 3006 id = genl_family_get_id(nl80211); 3007 3008fail: 3009 if (nl80211) 3010 genl_family_put(nl80211); 3011 if (cache) 3012 nl_cache_free(cache); 3013 3014 return id; 3015} 3016#define genl_ctrl_resolve android_genl_ctrl_resolve 3017#endif /* ANDROID */ 3018 3019 3020static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global) 3021{ 3022 int ret; 3023 3024 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); 3025 if (global->nl_cb == NULL) { 3026 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " 3027 "callbacks"); 3028 return -1; 3029 } 3030 3031 global->nl = nl_create_handle(global->nl_cb, "nl"); 3032 if (global->nl == NULL) 3033 goto err; 3034 3035 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211"); 3036 if (global->nl80211_id < 0) { 3037 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not " 3038 "found"); 3039 goto err; 3040 } 3041 3042 global->nl_event = nl_create_handle(global->nl_cb, "event"); 3043 if (global->nl_event == NULL) 3044 goto err; 3045 3046 ret = nl_get_multicast_id(global, "nl80211", "scan"); 3047 if (ret >= 0) 3048 ret = nl_socket_add_membership(global->nl_event, ret); 3049 if (ret < 0) { 3050 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast " 3051 "membership for scan events: %d (%s)", 3052 ret, strerror(-ret)); 3053 goto err; 3054 } 3055 3056 ret = nl_get_multicast_id(global, "nl80211", "mlme"); 3057 if (ret >= 0) 3058 ret = nl_socket_add_membership(global->nl_event, ret); 3059 if (ret < 0) { 3060 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast " 3061 "membership for mlme events: %d (%s)", 3062 ret, strerror(-ret)); 3063 goto err; 3064 } 3065 3066 ret = nl_get_multicast_id(global, "nl80211", "regulatory"); 3067 if (ret >= 0) 3068 ret = nl_socket_add_membership(global->nl_event, ret); 3069 if (ret < 0) { 3070 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast " 3071 "membership for regulatory events: %d (%s)", 3072 ret, strerror(-ret)); 3073 /* Continue without regulatory events */ 3074 } 3075 3076 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, 3077 no_seq_check, NULL); 3078 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, 3079 process_global_event, global); 3080 3081 eloop_register_read_sock(nl_socket_get_fd(global->nl_event), 3082 wpa_driver_nl80211_event_receive, 3083 global->nl_cb, global->nl_event); 3084 3085 return 0; 3086 3087err: 3088 nl_destroy_handles(&global->nl_event); 3089 nl_destroy_handles(&global->nl); 3090 nl_cb_put(global->nl_cb); 3091 global->nl_cb = NULL; 3092 return -1; 3093} 3094 3095 3096static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv) 3097{ 3098 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); 3099 if (!drv->nl_cb) { 3100 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct"); 3101 return -1; 3102 } 3103 3104 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, 3105 no_seq_check, NULL); 3106 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, 3107 process_drv_event, drv); 3108 3109 return 0; 3110} 3111 3112 3113static void wpa_driver_nl80211_rfkill_blocked(void *ctx) 3114{ 3115 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked"); 3116 /* 3117 * This may be for any interface; use ifdown event to disable 3118 * interface. 3119 */ 3120} 3121 3122 3123static void wpa_driver_nl80211_rfkill_unblocked(void *ctx) 3124{ 3125 struct wpa_driver_nl80211_data *drv = ctx; 3126 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked"); 3127 if (linux_set_iface_flags(drv->global->ioctl_sock, 3128 drv->first_bss.ifname, 1)) { 3129 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP " 3130 "after rfkill unblock"); 3131 return; 3132 } 3133 /* rtnetlink ifup handler will report interface as enabled */ 3134} 3135 3136 3137static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv) 3138{ 3139 /* Find phy (radio) to which this interface belongs */ 3140 char buf[90], *pos; 3141 int f, rv; 3142 3143 drv->phyname[0] = '\0'; 3144 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name", 3145 drv->first_bss.ifname); 3146 f = open(buf, O_RDONLY); 3147 if (f < 0) { 3148 wpa_printf(MSG_DEBUG, "Could not open file %s: %s", 3149 buf, strerror(errno)); 3150 return; 3151 } 3152 3153 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1); 3154 close(f); 3155 if (rv < 0) { 3156 wpa_printf(MSG_DEBUG, "Could not read file %s: %s", 3157 buf, strerror(errno)); 3158 return; 3159 } 3160 3161 drv->phyname[rv] = '\0'; 3162 pos = os_strchr(drv->phyname, '\n'); 3163 if (pos) 3164 *pos = '\0'; 3165 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s", 3166 drv->first_bss.ifname, drv->phyname); 3167} 3168 3169 3170static void wpa_driver_nl80211_handle_eapol_tx_status(int sock, 3171 void *eloop_ctx, 3172 void *handle) 3173{ 3174 struct wpa_driver_nl80211_data *drv = eloop_ctx; 3175 u8 data[2048]; 3176 struct msghdr msg; 3177 struct iovec entry; 3178 u8 control[512]; 3179 struct cmsghdr *cmsg; 3180 int res, found_ee = 0, found_wifi = 0, acked = 0; 3181 union wpa_event_data event; 3182 3183 memset(&msg, 0, sizeof(msg)); 3184 msg.msg_iov = &entry; 3185 msg.msg_iovlen = 1; 3186 entry.iov_base = data; 3187 entry.iov_len = sizeof(data); 3188 msg.msg_control = &control; 3189 msg.msg_controllen = sizeof(control); 3190 3191 res = recvmsg(sock, &msg, MSG_ERRQUEUE); 3192 /* if error or not fitting 802.3 header, return */ 3193 if (res < 14) 3194 return; 3195 3196 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) 3197 { 3198 if (cmsg->cmsg_level == SOL_SOCKET && 3199 cmsg->cmsg_type == SCM_WIFI_STATUS) { 3200 int *ack; 3201 3202 found_wifi = 1; 3203 ack = (void *)CMSG_DATA(cmsg); 3204 acked = *ack; 3205 } 3206 3207 if (cmsg->cmsg_level == SOL_PACKET && 3208 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) { 3209 struct sock_extended_err *err = 3210 (struct sock_extended_err *)CMSG_DATA(cmsg); 3211 3212 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS) 3213 found_ee = 1; 3214 } 3215 } 3216 3217 if (!found_ee || !found_wifi) 3218 return; 3219 3220 memset(&event, 0, sizeof(event)); 3221 event.eapol_tx_status.dst = data; 3222 event.eapol_tx_status.data = data + 14; 3223 event.eapol_tx_status.data_len = res - 14; 3224 event.eapol_tx_status.ack = acked; 3225 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event); 3226} 3227 3228 3229static int nl80211_init_bss(struct i802_bss *bss) 3230{ 3231 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); 3232 if (!bss->nl_cb) 3233 return -1; 3234 3235 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, 3236 no_seq_check, NULL); 3237 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, 3238 process_bss_event, bss); 3239 3240 return 0; 3241} 3242 3243 3244static void nl80211_destroy_bss(struct i802_bss *bss) 3245{ 3246 nl_cb_put(bss->nl_cb); 3247 bss->nl_cb = NULL; 3248} 3249 3250 3251/** 3252 * wpa_driver_nl80211_init - Initialize nl80211 driver interface 3253 * @ctx: context to be used when calling wpa_supplicant functions, 3254 * e.g., wpa_supplicant_event() 3255 * @ifname: interface name, e.g., wlan0 3256 * @global_priv: private driver global data from global_init() 3257 * Returns: Pointer to private data, %NULL on failure 3258 */ 3259static void * wpa_driver_nl80211_init(void *ctx, const char *ifname, 3260 void *global_priv) 3261{ 3262 struct wpa_driver_nl80211_data *drv; 3263 struct rfkill_config *rcfg; 3264 struct i802_bss *bss; 3265 3266 if (global_priv == NULL) 3267 return NULL; 3268 drv = os_zalloc(sizeof(*drv)); 3269 if (drv == NULL) 3270 return NULL; 3271 drv->global = global_priv; 3272 drv->ctx = ctx; 3273 bss = &drv->first_bss; 3274 bss->drv = drv; 3275 bss->ctx = ctx; 3276 3277 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname)); 3278 drv->monitor_ifidx = -1; 3279 drv->monitor_sock = -1; 3280 drv->eapol_tx_sock = -1; 3281 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED; 3282 3283 if (wpa_driver_nl80211_init_nl(drv)) { 3284 os_free(drv); 3285 return NULL; 3286 } 3287 3288 if (nl80211_init_bss(bss)) 3289 goto failed; 3290 3291 nl80211_get_phy_name(drv); 3292 3293 rcfg = os_zalloc(sizeof(*rcfg)); 3294 if (rcfg == NULL) 3295 goto failed; 3296 rcfg->ctx = drv; 3297 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname)); 3298 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked; 3299 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked; 3300 drv->rfkill = rfkill_init(rcfg); 3301 if (drv->rfkill == NULL) { 3302 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available"); 3303 os_free(rcfg); 3304 } 3305 3306 if (wpa_driver_nl80211_finish_drv_init(drv)) 3307 goto failed; 3308 3309 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0); 3310 if (drv->eapol_tx_sock < 0) 3311 goto failed; 3312 3313 if (drv->data_tx_status) { 3314 int enabled = 1; 3315 3316 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS, 3317 &enabled, sizeof(enabled)) < 0) { 3318 wpa_printf(MSG_DEBUG, 3319 "nl80211: wifi status sockopt failed\n"); 3320 drv->data_tx_status = 0; 3321 if (!drv->use_monitor) 3322 drv->capa.flags &= 3323 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS; 3324 } else { 3325 eloop_register_read_sock(drv->eapol_tx_sock, 3326 wpa_driver_nl80211_handle_eapol_tx_status, 3327 drv, NULL); 3328 } 3329 } 3330 3331 if (drv->global) { 3332 dl_list_add(&drv->global->interfaces, &drv->list); 3333 drv->in_interface_list = 1; 3334 } 3335 3336 return bss; 3337 3338failed: 3339 wpa_driver_nl80211_deinit(bss); 3340 return NULL; 3341} 3342 3343 3344static int nl80211_register_frame(struct i802_bss *bss, 3345 struct nl_handle *nl_handle, 3346 u16 type, const u8 *match, size_t match_len) 3347{ 3348 struct wpa_driver_nl80211_data *drv = bss->drv; 3349 struct nl_msg *msg; 3350 int ret = -1; 3351 3352 msg = nlmsg_alloc(); 3353 if (!msg) 3354 return -1; 3355 3356 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p", 3357 type, nl_handle); 3358 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match", 3359 match, match_len); 3360 3361 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION); 3362 3363 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 3364 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type); 3365 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match); 3366 3367 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL); 3368 msg = NULL; 3369 if (ret) { 3370 wpa_printf(MSG_DEBUG, "nl80211: Register frame command " 3371 "failed (type=%u): ret=%d (%s)", 3372 type, ret, strerror(-ret)); 3373 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match", 3374 match, match_len); 3375 goto nla_put_failure; 3376 } 3377 ret = 0; 3378nla_put_failure: 3379 nlmsg_free(msg); 3380 return ret; 3381} 3382 3383 3384static int nl80211_alloc_mgmt_handle(struct i802_bss *bss) 3385{ 3386 struct wpa_driver_nl80211_data *drv = bss->drv; 3387 3388 if (bss->nl_mgmt) { 3389 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting " 3390 "already on! (nl_mgmt=%p)", bss->nl_mgmt); 3391 return -1; 3392 } 3393 3394 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt"); 3395 if (bss->nl_mgmt == NULL) 3396 return -1; 3397 3398 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt), 3399 wpa_driver_nl80211_event_receive, bss->nl_cb, 3400 bss->nl_mgmt); 3401 3402 return 0; 3403} 3404 3405 3406static int nl80211_register_action_frame(struct i802_bss *bss, 3407 const u8 *match, size_t match_len) 3408{ 3409 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4); 3410 return nl80211_register_frame(bss, bss->nl_mgmt, 3411 type, match, match_len); 3412} 3413 3414 3415static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss) 3416{ 3417 struct wpa_driver_nl80211_data *drv = bss->drv; 3418 3419 if (nl80211_alloc_mgmt_handle(bss)) 3420 return -1; 3421 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP " 3422 "handle %p", bss->nl_mgmt); 3423 3424#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) 3425 /* GAS Initial Request */ 3426 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0) 3427 return -1; 3428 /* GAS Initial Response */ 3429 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0) 3430 return -1; 3431 /* GAS Comeback Request */ 3432 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0) 3433 return -1; 3434 /* GAS Comeback Response */ 3435 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0) 3436 return -1; 3437#endif /* CONFIG_P2P || CONFIG_INTERWORKING */ 3438#ifdef CONFIG_P2P 3439 /* P2P Public Action */ 3440 if (nl80211_register_action_frame(bss, 3441 (u8 *) "\x04\x09\x50\x6f\x9a\x09", 3442 6) < 0) 3443 return -1; 3444 /* P2P Action */ 3445 if (nl80211_register_action_frame(bss, 3446 (u8 *) "\x7f\x50\x6f\x9a\x09", 3447 5) < 0) 3448 return -1; 3449#endif /* CONFIG_P2P */ 3450#ifdef CONFIG_IEEE80211W 3451 /* SA Query Response */ 3452 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0) 3453 return -1; 3454#endif /* CONFIG_IEEE80211W */ 3455#ifdef CONFIG_TDLS 3456 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) { 3457 /* TDLS Discovery Response */ 3458 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) < 3459 0) 3460 return -1; 3461 } 3462#endif /* CONFIG_TDLS */ 3463 3464 /* FT Action frames */ 3465 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0) 3466 return -1; 3467 else 3468 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT | 3469 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK; 3470 3471 /* WNM - BSS Transition Management Request */ 3472 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0) 3473 return -1; 3474 /* WNM-Sleep Mode Response */ 3475 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0) 3476 return -1; 3477 3478 return 0; 3479} 3480 3481 3482static int nl80211_register_spurious_class3(struct i802_bss *bss) 3483{ 3484 struct wpa_driver_nl80211_data *drv = bss->drv; 3485 struct nl_msg *msg; 3486 int ret = -1; 3487 3488 msg = nlmsg_alloc(); 3489 if (!msg) 3490 return -1; 3491 3492 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME); 3493 3494 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 3495 3496 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL); 3497 msg = NULL; 3498 if (ret) { 3499 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 " 3500 "failed: ret=%d (%s)", 3501 ret, strerror(-ret)); 3502 goto nla_put_failure; 3503 } 3504 ret = 0; 3505nla_put_failure: 3506 nlmsg_free(msg); 3507 return ret; 3508} 3509 3510 3511static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss) 3512{ 3513 static const int stypes[] = { 3514 WLAN_FC_STYPE_AUTH, 3515 WLAN_FC_STYPE_ASSOC_REQ, 3516 WLAN_FC_STYPE_REASSOC_REQ, 3517 WLAN_FC_STYPE_DISASSOC, 3518 WLAN_FC_STYPE_DEAUTH, 3519 WLAN_FC_STYPE_ACTION, 3520 WLAN_FC_STYPE_PROBE_REQ, 3521/* Beacon doesn't work as mac80211 doesn't currently allow 3522 * it, but it wouldn't really be the right thing anyway as 3523 * it isn't per interface ... maybe just dump the scan 3524 * results periodically for OLBC? 3525 */ 3526// WLAN_FC_STYPE_BEACON, 3527 }; 3528 unsigned int i; 3529 3530 if (nl80211_alloc_mgmt_handle(bss)) 3531 return -1; 3532 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP " 3533 "handle %p", bss->nl_mgmt); 3534 3535 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) { 3536 if (nl80211_register_frame(bss, bss->nl_mgmt, 3537 (WLAN_FC_TYPE_MGMT << 2) | 3538 (stypes[i] << 4), 3539 NULL, 0) < 0) { 3540 goto out_err; 3541 } 3542 } 3543 3544 if (nl80211_register_spurious_class3(bss)) 3545 goto out_err; 3546 3547 if (nl80211_get_wiphy_data_ap(bss) == NULL) 3548 goto out_err; 3549 3550 return 0; 3551 3552out_err: 3553 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt)); 3554 nl_destroy_handles(&bss->nl_mgmt); 3555 return -1; 3556} 3557 3558 3559static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss) 3560{ 3561 if (nl80211_alloc_mgmt_handle(bss)) 3562 return -1; 3563 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP " 3564 "handle %p (device SME)", bss->nl_mgmt); 3565 3566 if (nl80211_register_frame(bss, bss->nl_mgmt, 3567 (WLAN_FC_TYPE_MGMT << 2) | 3568 (WLAN_FC_STYPE_ACTION << 4), 3569 NULL, 0) < 0) 3570 goto out_err; 3571 3572 return 0; 3573 3574out_err: 3575 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt)); 3576 nl_destroy_handles(&bss->nl_mgmt); 3577 return -1; 3578} 3579 3580 3581static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason) 3582{ 3583 if (bss->nl_mgmt == NULL) 3584 return; 3585 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p " 3586 "(%s)", bss->nl_mgmt, reason); 3587 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt)); 3588 nl_destroy_handles(&bss->nl_mgmt); 3589 3590 nl80211_put_wiphy_data_ap(bss); 3591} 3592 3593 3594static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx) 3595{ 3596 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL); 3597} 3598 3599 3600static int 3601wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv) 3602{ 3603 struct i802_bss *bss = &drv->first_bss; 3604 int send_rfkill_event = 0; 3605 3606 drv->ifindex = if_nametoindex(bss->ifname); 3607 drv->first_bss.ifindex = drv->ifindex; 3608 3609#ifndef HOSTAPD 3610 /* 3611 * Make sure the interface starts up in station mode unless this is a 3612 * dynamically added interface (e.g., P2P) that was already configured 3613 * with proper iftype. 3614 */ 3615 if (drv->ifindex != drv->global->if_add_ifindex && 3616 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) { 3617 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to " 3618 "use managed mode"); 3619 return -1; 3620 } 3621 3622 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) { 3623 if (rfkill_is_blocked(drv->rfkill)) { 3624 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable " 3625 "interface '%s' due to rfkill", 3626 bss->ifname); 3627 drv->if_disabled = 1; 3628 send_rfkill_event = 1; 3629 } else { 3630 wpa_printf(MSG_ERROR, "nl80211: Could not set " 3631 "interface '%s' UP", bss->ifname); 3632 return -1; 3633 } 3634 } 3635 3636 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 3637 1, IF_OPER_DORMANT); 3638#endif /* HOSTAPD */ 3639 3640 if (wpa_driver_nl80211_capa(drv)) 3641 return -1; 3642 3643 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 3644 bss->addr)) 3645 return -1; 3646 3647 if (send_rfkill_event) { 3648 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill, 3649 drv, drv->ctx); 3650 } 3651 3652 return 0; 3653} 3654 3655 3656static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv) 3657{ 3658 struct nl_msg *msg; 3659 3660 msg = nlmsg_alloc(); 3661 if (!msg) 3662 return -ENOMEM; 3663 3664 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON); 3665 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 3666 3667 return send_and_recv_msgs(drv, msg, NULL, NULL); 3668 nla_put_failure: 3669 nlmsg_free(msg); 3670 return -ENOBUFS; 3671} 3672 3673 3674/** 3675 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface 3676 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init() 3677 * 3678 * Shut down driver interface and processing of driver events. Free 3679 * private data buffer if one was allocated in wpa_driver_nl80211_init(). 3680 */ 3681static void wpa_driver_nl80211_deinit(struct i802_bss *bss) 3682{ 3683 struct wpa_driver_nl80211_data *drv = bss->drv; 3684 3685 bss->in_deinit = 1; 3686 if (drv->data_tx_status) 3687 eloop_unregister_read_sock(drv->eapol_tx_sock); 3688 if (drv->eapol_tx_sock >= 0) 3689 close(drv->eapol_tx_sock); 3690 3691 if (bss->nl_preq) 3692 wpa_driver_nl80211_probe_req_report(bss, 0); 3693 if (bss->added_if_into_bridge) { 3694 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname, 3695 bss->ifname) < 0) 3696 wpa_printf(MSG_INFO, "nl80211: Failed to remove " 3697 "interface %s from bridge %s: %s", 3698 bss->ifname, bss->brname, strerror(errno)); 3699 } 3700 if (bss->added_bridge) { 3701 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0) 3702 wpa_printf(MSG_INFO, "nl80211: Failed to remove " 3703 "bridge %s: %s", 3704 bss->brname, strerror(errno)); 3705 } 3706 3707 nl80211_remove_monitor_interface(drv); 3708 3709 if (is_ap_interface(drv->nlmode)) 3710 wpa_driver_nl80211_del_beacon(drv); 3711 3712#ifdef HOSTAPD 3713 if (drv->last_freq_ht) { 3714 /* Clear HT flags from the driver */ 3715 struct hostapd_freq_params freq; 3716 os_memset(&freq, 0, sizeof(freq)); 3717 freq.freq = drv->last_freq; 3718 wpa_driver_nl80211_set_freq(bss, &freq); 3719 } 3720 3721 if (drv->eapol_sock >= 0) { 3722 eloop_unregister_read_sock(drv->eapol_sock); 3723 close(drv->eapol_sock); 3724 } 3725 3726 if (drv->if_indices != drv->default_if_indices) 3727 os_free(drv->if_indices); 3728#endif /* HOSTAPD */ 3729 3730 if (drv->disabled_11b_rates) 3731 nl80211_disable_11b_rates(drv, drv->ifindex, 0); 3732 3733 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0, 3734 IF_OPER_UP); 3735 rfkill_deinit(drv->rfkill); 3736 3737 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx); 3738 3739 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0); 3740 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION); 3741 nl80211_mgmt_unsubscribe(bss, "deinit"); 3742 3743 nl_cb_put(drv->nl_cb); 3744 3745 nl80211_destroy_bss(&drv->first_bss); 3746 3747 os_free(drv->filter_ssids); 3748 3749 os_free(drv->auth_ie); 3750 3751 if (drv->in_interface_list) 3752 dl_list_del(&drv->list); 3753 3754 os_free(drv); 3755} 3756 3757 3758/** 3759 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion 3760 * @eloop_ctx: Driver private data 3761 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init() 3762 * 3763 * This function can be used as registered timeout when starting a scan to 3764 * generate a scan completed event if the driver does not report this. 3765 */ 3766static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx) 3767{ 3768 struct wpa_driver_nl80211_data *drv = eloop_ctx; 3769 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) { 3770 wpa_driver_nl80211_set_mode(&drv->first_bss, 3771 drv->ap_scan_as_station); 3772 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED; 3773 } 3774 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results"); 3775 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL); 3776} 3777 3778 3779static struct nl_msg * 3780nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd, 3781 struct wpa_driver_scan_params *params) 3782{ 3783 struct nl_msg *msg; 3784 int err; 3785 size_t i; 3786 3787 msg = nlmsg_alloc(); 3788 if (!msg) 3789 return NULL; 3790 3791 nl80211_cmd(drv, msg, 0, cmd); 3792 3793 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) < 0) 3794 goto fail; 3795 3796 if (params->num_ssids) { 3797 struct nl_msg *ssids = nlmsg_alloc(); 3798 if (ssids == NULL) 3799 goto fail; 3800 for (i = 0; i < params->num_ssids; i++) { 3801 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID", 3802 params->ssids[i].ssid, 3803 params->ssids[i].ssid_len); 3804 if (nla_put(ssids, i + 1, params->ssids[i].ssid_len, 3805 params->ssids[i].ssid) < 0) { 3806 nlmsg_free(ssids); 3807 goto fail; 3808 } 3809 } 3810 err = nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); 3811 nlmsg_free(ssids); 3812 if (err < 0) 3813 goto fail; 3814 } 3815 3816 if (params->extra_ies) { 3817 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs", 3818 params->extra_ies, params->extra_ies_len); 3819 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len, 3820 params->extra_ies) < 0) 3821 goto fail; 3822 } 3823 3824 if (params->freqs) { 3825 struct nl_msg *freqs = nlmsg_alloc(); 3826 if (freqs == NULL) 3827 goto fail; 3828 for (i = 0; params->freqs[i]; i++) { 3829 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u " 3830 "MHz", params->freqs[i]); 3831 if (nla_put_u32(freqs, i + 1, params->freqs[i]) < 0) { 3832 nlmsg_free(freqs); 3833 goto fail; 3834 } 3835 } 3836 err = nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, 3837 freqs); 3838 nlmsg_free(freqs); 3839 if (err < 0) 3840 goto fail; 3841 } 3842 3843 os_free(drv->filter_ssids); 3844 drv->filter_ssids = params->filter_ssids; 3845 params->filter_ssids = NULL; 3846 drv->num_filter_ssids = params->num_filter_ssids; 3847 3848 return msg; 3849 3850fail: 3851 nlmsg_free(msg); 3852 return NULL; 3853} 3854 3855 3856/** 3857 * wpa_driver_nl80211_scan - Request the driver to initiate scan 3858 * @bss: Pointer to private driver data from wpa_driver_nl80211_init() 3859 * @params: Scan parameters 3860 * Returns: 0 on success, -1 on failure 3861 */ 3862static int wpa_driver_nl80211_scan(struct i802_bss *bss, 3863 struct wpa_driver_scan_params *params) 3864{ 3865 struct wpa_driver_nl80211_data *drv = bss->drv; 3866 int ret = -1, timeout; 3867 struct nl_msg *msg, *rates = NULL; 3868 3869 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request"); 3870 drv->scan_for_auth = 0; 3871 3872 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params); 3873 if (!msg) 3874 return -1; 3875 3876 if (params->p2p_probe) { 3877 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates"); 3878 3879 rates = nlmsg_alloc(); 3880 if (rates == NULL) 3881 goto nla_put_failure; 3882 3883 /* 3884 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates 3885 * by masking out everything else apart from the OFDM rates 6, 3886 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz 3887 * rates are left enabled. 3888 */ 3889 NLA_PUT(rates, NL80211_BAND_2GHZ, 8, 3890 "\x0c\x12\x18\x24\x30\x48\x60\x6c"); 3891 if (nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates) < 3892 0) 3893 goto nla_put_failure; 3894 3895 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE); 3896 } 3897 3898 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 3899 msg = NULL; 3900 if (ret) { 3901 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d " 3902 "(%s)", ret, strerror(-ret)); 3903#ifdef HOSTAPD 3904 if (is_ap_interface(drv->nlmode)) { 3905 /* 3906 * mac80211 does not allow scan requests in AP mode, so 3907 * try to do this in station mode. 3908 */ 3909 if (wpa_driver_nl80211_set_mode( 3910 bss, NL80211_IFTYPE_STATION)) 3911 goto nla_put_failure; 3912 3913 if (wpa_driver_nl80211_scan(bss, params)) { 3914 wpa_driver_nl80211_set_mode(bss, drv->nlmode); 3915 goto nla_put_failure; 3916 } 3917 3918 /* Restore AP mode when processing scan results */ 3919 drv->ap_scan_as_station = drv->nlmode; 3920 ret = 0; 3921 } else 3922 goto nla_put_failure; 3923#else /* HOSTAPD */ 3924 goto nla_put_failure; 3925#endif /* HOSTAPD */ 3926 } 3927 3928 /* Not all drivers generate "scan completed" wireless event, so try to 3929 * read results after a timeout. */ 3930 timeout = 10; 3931 if (drv->scan_complete_events) { 3932 /* 3933 * The driver seems to deliver events to notify when scan is 3934 * complete, so use longer timeout to avoid race conditions 3935 * with scanning and following association request. 3936 */ 3937 timeout = 30; 3938 } 3939 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d " 3940 "seconds", ret, timeout); 3941 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx); 3942 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout, 3943 drv, drv->ctx); 3944 3945nla_put_failure: 3946 nlmsg_free(msg); 3947 nlmsg_free(rates); 3948 return ret; 3949} 3950 3951 3952/** 3953 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan 3954 * @priv: Pointer to private driver data from wpa_driver_nl80211_init() 3955 * @params: Scan parameters 3956 * @interval: Interval between scan cycles in milliseconds 3957 * Returns: 0 on success, -1 on failure or if not supported 3958 */ 3959static int wpa_driver_nl80211_sched_scan(void *priv, 3960 struct wpa_driver_scan_params *params, 3961 u32 interval) 3962{ 3963 struct i802_bss *bss = priv; 3964 struct wpa_driver_nl80211_data *drv = bss->drv; 3965 int ret = -1; 3966 struct nl_msg *msg; 3967 struct nl_msg *match_set_ssid = NULL, *match_sets = NULL; 3968 struct nl_msg *match_set_rssi = NULL; 3969 size_t i; 3970 3971 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request"); 3972 3973#ifdef ANDROID 3974 if (!drv->capa.sched_scan_supported) 3975 return android_pno_start(bss, params); 3976#endif /* ANDROID */ 3977 3978 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params); 3979 if (!msg) 3980 goto nla_put_failure; 3981 3982 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval); 3983 3984 if ((drv->num_filter_ssids && 3985 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) || 3986 params->filter_rssi) { 3987 match_sets = nlmsg_alloc(); 3988 if (match_sets == NULL) 3989 goto nla_put_failure; 3990 3991 for (i = 0; i < drv->num_filter_ssids; i++) { 3992 wpa_hexdump_ascii(MSG_MSGDUMP, 3993 "nl80211: Sched scan filter SSID", 3994 drv->filter_ssids[i].ssid, 3995 drv->filter_ssids[i].ssid_len); 3996 3997 match_set_ssid = nlmsg_alloc(); 3998 if (match_set_ssid == NULL) 3999 goto nla_put_failure; 4000 NLA_PUT(match_set_ssid, 4001 NL80211_ATTR_SCHED_SCAN_MATCH_SSID, 4002 drv->filter_ssids[i].ssid_len, 4003 drv->filter_ssids[i].ssid); 4004 4005 if (nla_put_nested(match_sets, i + 1, match_set_ssid) < 4006 0) 4007 goto nla_put_failure; 4008 } 4009 4010 if (params->filter_rssi) { 4011 match_set_rssi = nlmsg_alloc(); 4012 if (match_set_rssi == NULL) 4013 goto nla_put_failure; 4014 NLA_PUT_U32(match_set_rssi, 4015 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI, 4016 params->filter_rssi); 4017 wpa_printf(MSG_MSGDUMP, 4018 "nl80211: Sched scan RSSI filter %d dBm", 4019 params->filter_rssi); 4020 if (nla_put_nested(match_sets, 0, match_set_rssi) < 0) 4021 goto nla_put_failure; 4022 } 4023 4024 if (nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH, 4025 match_sets) < 0) 4026 goto nla_put_failure; 4027 } 4028 4029 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4030 4031 /* TODO: if we get an error here, we should fall back to normal scan */ 4032 4033 msg = NULL; 4034 if (ret) { 4035 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: " 4036 "ret=%d (%s)", ret, strerror(-ret)); 4037 goto nla_put_failure; 4038 } 4039 4040 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - " 4041 "scan interval %d msec", ret, interval); 4042 4043nla_put_failure: 4044 nlmsg_free(match_set_ssid); 4045 nlmsg_free(match_sets); 4046 nlmsg_free(match_set_rssi); 4047 nlmsg_free(msg); 4048 return ret; 4049} 4050 4051 4052/** 4053 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan 4054 * @priv: Pointer to private driver data from wpa_driver_nl80211_init() 4055 * Returns: 0 on success, -1 on failure or if not supported 4056 */ 4057static int wpa_driver_nl80211_stop_sched_scan(void *priv) 4058{ 4059 struct i802_bss *bss = priv; 4060 struct wpa_driver_nl80211_data *drv = bss->drv; 4061 int ret = 0; 4062 struct nl_msg *msg; 4063 4064#ifdef ANDROID 4065 if (!drv->capa.sched_scan_supported) 4066 return android_pno_stop(bss); 4067#endif /* ANDROID */ 4068 4069 msg = nlmsg_alloc(); 4070 if (!msg) 4071 return -1; 4072 4073 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN); 4074 4075 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 4076 4077 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4078 msg = NULL; 4079 if (ret) { 4080 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: " 4081 "ret=%d (%s)", ret, strerror(-ret)); 4082 goto nla_put_failure; 4083 } 4084 4085 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret); 4086 4087nla_put_failure: 4088 nlmsg_free(msg); 4089 return ret; 4090} 4091 4092 4093static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie) 4094{ 4095 const u8 *end, *pos; 4096 4097 if (ies == NULL) 4098 return NULL; 4099 4100 pos = ies; 4101 end = ies + ies_len; 4102 4103 while (pos + 1 < end) { 4104 if (pos + 2 + pos[1] > end) 4105 break; 4106 if (pos[0] == ie) 4107 return pos; 4108 pos += 2 + pos[1]; 4109 } 4110 4111 return NULL; 4112} 4113 4114 4115static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv, 4116 const u8 *ie, size_t ie_len) 4117{ 4118 const u8 *ssid; 4119 size_t i; 4120 4121 if (drv->filter_ssids == NULL) 4122 return 0; 4123 4124 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID); 4125 if (ssid == NULL) 4126 return 1; 4127 4128 for (i = 0; i < drv->num_filter_ssids; i++) { 4129 if (ssid[1] == drv->filter_ssids[i].ssid_len && 4130 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) == 4131 0) 4132 return 0; 4133 } 4134 4135 return 1; 4136} 4137 4138 4139static int bss_info_handler(struct nl_msg *msg, void *arg) 4140{ 4141 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 4142 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 4143 struct nlattr *bss[NL80211_BSS_MAX + 1]; 4144 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { 4145 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC }, 4146 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, 4147 [NL80211_BSS_TSF] = { .type = NLA_U64 }, 4148 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, 4149 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, 4150 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC }, 4151 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, 4152 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, 4153 [NL80211_BSS_STATUS] = { .type = NLA_U32 }, 4154 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, 4155 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC }, 4156 }; 4157 struct nl80211_bss_info_arg *_arg = arg; 4158 struct wpa_scan_results *res = _arg->res; 4159 struct wpa_scan_res **tmp; 4160 struct wpa_scan_res *r; 4161 const u8 *ie, *beacon_ie; 4162 size_t ie_len, beacon_ie_len; 4163 u8 *pos; 4164 size_t i; 4165 4166 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 4167 genlmsg_attrlen(gnlh, 0), NULL); 4168 if (!tb[NL80211_ATTR_BSS]) 4169 return NL_SKIP; 4170 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], 4171 bss_policy)) 4172 return NL_SKIP; 4173 if (bss[NL80211_BSS_STATUS]) { 4174 enum nl80211_bss_status status; 4175 status = nla_get_u32(bss[NL80211_BSS_STATUS]); 4176 if (status == NL80211_BSS_STATUS_ASSOCIATED && 4177 bss[NL80211_BSS_FREQUENCY]) { 4178 _arg->assoc_freq = 4179 nla_get_u32(bss[NL80211_BSS_FREQUENCY]); 4180 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz", 4181 _arg->assoc_freq); 4182 } 4183 if (status == NL80211_BSS_STATUS_ASSOCIATED && 4184 bss[NL80211_BSS_BSSID]) { 4185 os_memcpy(_arg->assoc_bssid, 4186 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN); 4187 wpa_printf(MSG_DEBUG, "nl80211: Associated with " 4188 MACSTR, MAC2STR(_arg->assoc_bssid)); 4189 } 4190 } 4191 if (!res) 4192 return NL_SKIP; 4193 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) { 4194 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 4195 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 4196 } else { 4197 ie = NULL; 4198 ie_len = 0; 4199 } 4200 if (bss[NL80211_BSS_BEACON_IES]) { 4201 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]); 4202 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]); 4203 } else { 4204 beacon_ie = NULL; 4205 beacon_ie_len = 0; 4206 } 4207 4208 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie, 4209 ie ? ie_len : beacon_ie_len)) 4210 return NL_SKIP; 4211 4212 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len); 4213 if (r == NULL) 4214 return NL_SKIP; 4215 if (bss[NL80211_BSS_BSSID]) 4216 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]), 4217 ETH_ALEN); 4218 if (bss[NL80211_BSS_FREQUENCY]) 4219 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]); 4220 if (bss[NL80211_BSS_BEACON_INTERVAL]) 4221 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]); 4222 if (bss[NL80211_BSS_CAPABILITY]) 4223 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); 4224 r->flags |= WPA_SCAN_NOISE_INVALID; 4225 if (bss[NL80211_BSS_SIGNAL_MBM]) { 4226 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); 4227 r->level /= 100; /* mBm to dBm */ 4228 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID; 4229 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { 4230 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); 4231 r->flags |= WPA_SCAN_QUAL_INVALID; 4232 } else 4233 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID; 4234 if (bss[NL80211_BSS_TSF]) 4235 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]); 4236 if (bss[NL80211_BSS_SEEN_MS_AGO]) 4237 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]); 4238 r->ie_len = ie_len; 4239 pos = (u8 *) (r + 1); 4240 if (ie) { 4241 os_memcpy(pos, ie, ie_len); 4242 pos += ie_len; 4243 } 4244 r->beacon_ie_len = beacon_ie_len; 4245 if (beacon_ie) 4246 os_memcpy(pos, beacon_ie, beacon_ie_len); 4247 4248 if (bss[NL80211_BSS_STATUS]) { 4249 enum nl80211_bss_status status; 4250 status = nla_get_u32(bss[NL80211_BSS_STATUS]); 4251 switch (status) { 4252 case NL80211_BSS_STATUS_AUTHENTICATED: 4253 r->flags |= WPA_SCAN_AUTHENTICATED; 4254 break; 4255 case NL80211_BSS_STATUS_ASSOCIATED: 4256 r->flags |= WPA_SCAN_ASSOCIATED; 4257 break; 4258 default: 4259 break; 4260 } 4261 } 4262 4263 /* 4264 * cfg80211 maintains separate BSS table entries for APs if the same 4265 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does 4266 * not use frequency as a separate key in the BSS table, so filter out 4267 * duplicated entries. Prefer associated BSS entry in such a case in 4268 * order to get the correct frequency into the BSS table. 4269 */ 4270 for (i = 0; i < res->num; i++) { 4271 const u8 *s1, *s2; 4272 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0) 4273 continue; 4274 4275 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1), 4276 res->res[i]->ie_len, WLAN_EID_SSID); 4277 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID); 4278 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] || 4279 os_memcmp(s1, s2, 2 + s1[1]) != 0) 4280 continue; 4281 4282 /* Same BSSID,SSID was already included in scan results */ 4283 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result " 4284 "for " MACSTR, MAC2STR(r->bssid)); 4285 4286 if ((r->flags & WPA_SCAN_ASSOCIATED) && 4287 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) { 4288 os_free(res->res[i]); 4289 res->res[i] = r; 4290 } else 4291 os_free(r); 4292 return NL_SKIP; 4293 } 4294 4295 tmp = os_realloc_array(res->res, res->num + 1, 4296 sizeof(struct wpa_scan_res *)); 4297 if (tmp == NULL) { 4298 os_free(r); 4299 return NL_SKIP; 4300 } 4301 tmp[res->num++] = r; 4302 res->res = tmp; 4303 4304 return NL_SKIP; 4305} 4306 4307 4308static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv, 4309 const u8 *addr) 4310{ 4311 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { 4312 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state " 4313 "mismatch (" MACSTR ")", MAC2STR(addr)); 4314 wpa_driver_nl80211_mlme(drv, addr, 4315 NL80211_CMD_DEAUTHENTICATE, 4316 WLAN_REASON_PREV_AUTH_NOT_VALID, 1); 4317 } 4318} 4319 4320 4321static void wpa_driver_nl80211_check_bss_status( 4322 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res) 4323{ 4324 size_t i; 4325 4326 for (i = 0; i < res->num; i++) { 4327 struct wpa_scan_res *r = res->res[i]; 4328 if (r->flags & WPA_SCAN_AUTHENTICATED) { 4329 wpa_printf(MSG_DEBUG, "nl80211: Scan results " 4330 "indicates BSS status with " MACSTR 4331 " as authenticated", 4332 MAC2STR(r->bssid)); 4333 if (is_sta_interface(drv->nlmode) && 4334 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 && 4335 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) != 4336 0) { 4337 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID" 4338 " in local state (auth=" MACSTR 4339 " assoc=" MACSTR ")", 4340 MAC2STR(drv->auth_bssid), 4341 MAC2STR(drv->bssid)); 4342 clear_state_mismatch(drv, r->bssid); 4343 } 4344 } 4345 4346 if (r->flags & WPA_SCAN_ASSOCIATED) { 4347 wpa_printf(MSG_DEBUG, "nl80211: Scan results " 4348 "indicate BSS status with " MACSTR 4349 " as associated", 4350 MAC2STR(r->bssid)); 4351 if (is_sta_interface(drv->nlmode) && 4352 !drv->associated) { 4353 wpa_printf(MSG_DEBUG, "nl80211: Local state " 4354 "(not associated) does not match " 4355 "with BSS state"); 4356 clear_state_mismatch(drv, r->bssid); 4357 } else if (is_sta_interface(drv->nlmode) && 4358 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) != 4359 0) { 4360 wpa_printf(MSG_DEBUG, "nl80211: Local state " 4361 "(associated with " MACSTR ") does " 4362 "not match with BSS state", 4363 MAC2STR(drv->bssid)); 4364 clear_state_mismatch(drv, r->bssid); 4365 clear_state_mismatch(drv, drv->bssid); 4366 } 4367 } 4368 } 4369} 4370 4371 4372static struct wpa_scan_results * 4373nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv) 4374{ 4375 struct nl_msg *msg; 4376 struct wpa_scan_results *res; 4377 int ret; 4378 struct nl80211_bss_info_arg arg; 4379 4380 res = os_zalloc(sizeof(*res)); 4381 if (res == NULL) 4382 return NULL; 4383 msg = nlmsg_alloc(); 4384 if (!msg) 4385 goto nla_put_failure; 4386 4387 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN); 4388 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 4389 4390 arg.drv = drv; 4391 arg.res = res; 4392 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg); 4393 msg = NULL; 4394 if (ret == 0) { 4395 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu " 4396 "BSSes)", (unsigned long) res->num); 4397 nl80211_get_noise_for_scan_results(drv, res); 4398 return res; 4399 } 4400 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d " 4401 "(%s)", ret, strerror(-ret)); 4402nla_put_failure: 4403 nlmsg_free(msg); 4404 wpa_scan_results_free(res); 4405 return NULL; 4406} 4407 4408 4409/** 4410 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results 4411 * @priv: Pointer to private wext data from wpa_driver_nl80211_init() 4412 * Returns: Scan results on success, -1 on failure 4413 */ 4414static struct wpa_scan_results * 4415wpa_driver_nl80211_get_scan_results(void *priv) 4416{ 4417 struct i802_bss *bss = priv; 4418 struct wpa_driver_nl80211_data *drv = bss->drv; 4419 struct wpa_scan_results *res; 4420 4421 res = nl80211_get_scan_results(drv); 4422 if (res) 4423 wpa_driver_nl80211_check_bss_status(drv, res); 4424 return res; 4425} 4426 4427 4428static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv) 4429{ 4430 struct wpa_scan_results *res; 4431 size_t i; 4432 4433 res = nl80211_get_scan_results(drv); 4434 if (res == NULL) { 4435 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results"); 4436 return; 4437 } 4438 4439 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump"); 4440 for (i = 0; i < res->num; i++) { 4441 struct wpa_scan_res *r = res->res[i]; 4442 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s", 4443 (int) i, (int) res->num, MAC2STR(r->bssid), 4444 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "", 4445 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : ""); 4446 } 4447 4448 wpa_scan_results_free(res); 4449} 4450 4451 4452static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss, 4453 enum wpa_alg alg, const u8 *addr, 4454 int key_idx, int set_tx, 4455 const u8 *seq, size_t seq_len, 4456 const u8 *key, size_t key_len) 4457{ 4458 struct wpa_driver_nl80211_data *drv = bss->drv; 4459 int ifindex = if_nametoindex(ifname); 4460 struct nl_msg *msg; 4461 int ret; 4462 4463 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d " 4464 "set_tx=%d seq_len=%lu key_len=%lu", 4465 __func__, ifindex, alg, addr, key_idx, set_tx, 4466 (unsigned long) seq_len, (unsigned long) key_len); 4467#ifdef CONFIG_TDLS 4468 if (key_idx == -1) 4469 key_idx = 0; 4470#endif /* CONFIG_TDLS */ 4471 4472 msg = nlmsg_alloc(); 4473 if (!msg) 4474 return -ENOMEM; 4475 4476 if (alg == WPA_ALG_NONE) { 4477 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY); 4478 } else { 4479 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY); 4480 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key); 4481 switch (alg) { 4482 case WPA_ALG_WEP: 4483 if (key_len == 5) 4484 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4485 WLAN_CIPHER_SUITE_WEP40); 4486 else 4487 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4488 WLAN_CIPHER_SUITE_WEP104); 4489 break; 4490 case WPA_ALG_TKIP: 4491 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4492 WLAN_CIPHER_SUITE_TKIP); 4493 break; 4494 case WPA_ALG_CCMP: 4495 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4496 WLAN_CIPHER_SUITE_CCMP); 4497 break; 4498 case WPA_ALG_GCMP: 4499 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4500 WLAN_CIPHER_SUITE_GCMP); 4501 break; 4502 case WPA_ALG_IGTK: 4503 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4504 WLAN_CIPHER_SUITE_AES_CMAC); 4505 break; 4506 case WPA_ALG_SMS4: 4507 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4508 WLAN_CIPHER_SUITE_SMS4); 4509 break; 4510 case WPA_ALG_KRK: 4511 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4512 WLAN_CIPHER_SUITE_KRK); 4513 break; 4514 default: 4515 wpa_printf(MSG_ERROR, "%s: Unsupported encryption " 4516 "algorithm %d", __func__, alg); 4517 nlmsg_free(msg); 4518 return -1; 4519 } 4520 } 4521 4522 if (seq && seq_len) 4523 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq); 4524 4525 if (addr && !is_broadcast_ether_addr(addr)) { 4526 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr)); 4527 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 4528 4529 if (alg != WPA_ALG_WEP && key_idx && !set_tx) { 4530 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK"); 4531 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, 4532 NL80211_KEYTYPE_GROUP); 4533 } 4534 } else if (addr && is_broadcast_ether_addr(addr)) { 4535 struct nl_msg *types; 4536 int err; 4537 wpa_printf(MSG_DEBUG, " broadcast key"); 4538 types = nlmsg_alloc(); 4539 if (!types) 4540 goto nla_put_failure; 4541 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST); 4542 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, 4543 types); 4544 nlmsg_free(types); 4545 if (err) 4546 goto nla_put_failure; 4547 } 4548 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); 4549 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 4550 4551 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4552 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE) 4553 ret = 0; 4554 if (ret) 4555 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)", 4556 ret, strerror(-ret)); 4557 4558 /* 4559 * If we failed or don't need to set the default TX key (below), 4560 * we're done here. 4561 */ 4562 if (ret || !set_tx || alg == WPA_ALG_NONE) 4563 return ret; 4564 if (is_ap_interface(drv->nlmode) && addr && 4565 !is_broadcast_ether_addr(addr)) 4566 return ret; 4567 4568 msg = nlmsg_alloc(); 4569 if (!msg) 4570 return -ENOMEM; 4571 4572 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY); 4573 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); 4574 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 4575 if (alg == WPA_ALG_IGTK) 4576 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT); 4577 else 4578 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT); 4579 if (addr && is_broadcast_ether_addr(addr)) { 4580 struct nl_msg *types; 4581 int err; 4582 types = nlmsg_alloc(); 4583 if (!types) 4584 goto nla_put_failure; 4585 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST); 4586 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, 4587 types); 4588 nlmsg_free(types); 4589 if (err) 4590 goto nla_put_failure; 4591 } else if (addr) { 4592 struct nl_msg *types; 4593 int err; 4594 types = nlmsg_alloc(); 4595 if (!types) 4596 goto nla_put_failure; 4597 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST); 4598 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, 4599 types); 4600 nlmsg_free(types); 4601 if (err) 4602 goto nla_put_failure; 4603 } 4604 4605 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4606 if (ret == -ENOENT) 4607 ret = 0; 4608 if (ret) 4609 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; " 4610 "err=%d %s)", ret, strerror(-ret)); 4611 return ret; 4612 4613nla_put_failure: 4614 nlmsg_free(msg); 4615 return -ENOBUFS; 4616} 4617 4618 4619static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg, 4620 int key_idx, int defkey, 4621 const u8 *seq, size_t seq_len, 4622 const u8 *key, size_t key_len) 4623{ 4624 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY); 4625 if (!key_attr) 4626 return -1; 4627 4628 if (defkey && alg == WPA_ALG_IGTK) 4629 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT); 4630 else if (defkey) 4631 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); 4632 4633 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx); 4634 4635 switch (alg) { 4636 case WPA_ALG_WEP: 4637 if (key_len == 5) 4638 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 4639 WLAN_CIPHER_SUITE_WEP40); 4640 else 4641 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 4642 WLAN_CIPHER_SUITE_WEP104); 4643 break; 4644 case WPA_ALG_TKIP: 4645 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP); 4646 break; 4647 case WPA_ALG_CCMP: 4648 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP); 4649 break; 4650 case WPA_ALG_GCMP: 4651 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP); 4652 break; 4653 case WPA_ALG_IGTK: 4654 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 4655 WLAN_CIPHER_SUITE_AES_CMAC); 4656 break; 4657 default: 4658 wpa_printf(MSG_ERROR, "%s: Unsupported encryption " 4659 "algorithm %d", __func__, alg); 4660 return -1; 4661 } 4662 4663 if (seq && seq_len) 4664 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq); 4665 4666 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key); 4667 4668 nla_nest_end(msg, key_attr); 4669 4670 return 0; 4671 nla_put_failure: 4672 return -1; 4673} 4674 4675 4676static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params, 4677 struct nl_msg *msg) 4678{ 4679 int i, privacy = 0; 4680 struct nlattr *nl_keys, *nl_key; 4681 4682 for (i = 0; i < 4; i++) { 4683 if (!params->wep_key[i]) 4684 continue; 4685 privacy = 1; 4686 break; 4687 } 4688 if (params->wps == WPS_MODE_PRIVACY) 4689 privacy = 1; 4690 if (params->pairwise_suite && 4691 params->pairwise_suite != WPA_CIPHER_NONE) 4692 privacy = 1; 4693 4694 if (!privacy) 4695 return 0; 4696 4697 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY); 4698 4699 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS); 4700 if (!nl_keys) 4701 goto nla_put_failure; 4702 4703 for (i = 0; i < 4; i++) { 4704 if (!params->wep_key[i]) 4705 continue; 4706 4707 nl_key = nla_nest_start(msg, i); 4708 if (!nl_key) 4709 goto nla_put_failure; 4710 4711 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i], 4712 params->wep_key[i]); 4713 if (params->wep_key_len[i] == 5) 4714 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 4715 WLAN_CIPHER_SUITE_WEP40); 4716 else 4717 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 4718 WLAN_CIPHER_SUITE_WEP104); 4719 4720 NLA_PUT_U8(msg, NL80211_KEY_IDX, i); 4721 4722 if (i == params->wep_tx_keyidx) 4723 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); 4724 4725 nla_nest_end(msg, nl_key); 4726 } 4727 nla_nest_end(msg, nl_keys); 4728 4729 return 0; 4730 4731nla_put_failure: 4732 return -ENOBUFS; 4733} 4734 4735 4736static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv, 4737 const u8 *addr, int cmd, u16 reason_code, 4738 int local_state_change) 4739{ 4740 int ret = -1; 4741 struct nl_msg *msg; 4742 4743 msg = nlmsg_alloc(); 4744 if (!msg) 4745 return -1; 4746 4747 nl80211_cmd(drv, msg, 0, cmd); 4748 4749 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 4750 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code); 4751 if (addr) 4752 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 4753 if (local_state_change) 4754 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE); 4755 4756 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4757 msg = NULL; 4758 if (ret) { 4759 wpa_dbg(drv->ctx, MSG_DEBUG, 4760 "nl80211: MLME command failed: reason=%u ret=%d (%s)", 4761 reason_code, ret, strerror(-ret)); 4762 goto nla_put_failure; 4763 } 4764 ret = 0; 4765 4766nla_put_failure: 4767 nlmsg_free(msg); 4768 return ret; 4769} 4770 4771 4772static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv, 4773 int reason_code) 4774{ 4775 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code); 4776 drv->associated = 0; 4777 drv->ignore_next_local_disconnect = 0; 4778 /* Disconnect command doesn't need BSSID - it uses cached value */ 4779 return wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT, 4780 reason_code, 0); 4781} 4782 4783 4784static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss, 4785 const u8 *addr, int reason_code) 4786{ 4787 struct wpa_driver_nl80211_data *drv = bss->drv; 4788 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) 4789 return wpa_driver_nl80211_disconnect(drv, reason_code); 4790 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)", 4791 __func__, MAC2STR(addr), reason_code); 4792 drv->associated = 0; 4793 if (drv->nlmode == NL80211_IFTYPE_ADHOC) 4794 return nl80211_leave_ibss(drv); 4795 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE, 4796 reason_code, 0); 4797} 4798 4799 4800static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv, 4801 struct wpa_driver_auth_params *params) 4802{ 4803 int i; 4804 4805 drv->auth_freq = params->freq; 4806 drv->auth_alg = params->auth_alg; 4807 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx; 4808 drv->auth_local_state_change = params->local_state_change; 4809 drv->auth_p2p = params->p2p; 4810 4811 if (params->bssid) 4812 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN); 4813 else 4814 os_memset(drv->auth_bssid_, 0, ETH_ALEN); 4815 4816 if (params->ssid) { 4817 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len); 4818 drv->auth_ssid_len = params->ssid_len; 4819 } else 4820 drv->auth_ssid_len = 0; 4821 4822 4823 os_free(drv->auth_ie); 4824 drv->auth_ie = NULL; 4825 drv->auth_ie_len = 0; 4826 if (params->ie) { 4827 drv->auth_ie = os_malloc(params->ie_len); 4828 if (drv->auth_ie) { 4829 os_memcpy(drv->auth_ie, params->ie, params->ie_len); 4830 drv->auth_ie_len = params->ie_len; 4831 } 4832 } 4833 4834 for (i = 0; i < 4; i++) { 4835 if (params->wep_key[i] && params->wep_key_len[i] && 4836 params->wep_key_len[i] <= 16) { 4837 os_memcpy(drv->auth_wep_key[i], params->wep_key[i], 4838 params->wep_key_len[i]); 4839 drv->auth_wep_key_len[i] = params->wep_key_len[i]; 4840 } else 4841 drv->auth_wep_key_len[i] = 0; 4842 } 4843} 4844 4845 4846static int wpa_driver_nl80211_authenticate( 4847 struct i802_bss *bss, struct wpa_driver_auth_params *params) 4848{ 4849 struct wpa_driver_nl80211_data *drv = bss->drv; 4850 int ret = -1, i; 4851 struct nl_msg *msg; 4852 enum nl80211_auth_type type; 4853 enum nl80211_iftype nlmode; 4854 int count = 0; 4855 int is_retry; 4856 4857 is_retry = drv->retry_auth; 4858 drv->retry_auth = 0; 4859 4860 drv->associated = 0; 4861 os_memset(drv->auth_bssid, 0, ETH_ALEN); 4862 /* FIX: IBSS mode */ 4863 nlmode = params->p2p ? 4864 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION; 4865 if (drv->nlmode != nlmode && 4866 wpa_driver_nl80211_set_mode(bss, nlmode) < 0) 4867 return -1; 4868 4869retry: 4870 msg = nlmsg_alloc(); 4871 if (!msg) 4872 return -1; 4873 4874 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)", 4875 drv->ifindex); 4876 4877 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE); 4878 4879 for (i = 0; i < 4; i++) { 4880 if (!params->wep_key[i]) 4881 continue; 4882 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP, 4883 NULL, i, 4884 i == params->wep_tx_keyidx, NULL, 0, 4885 params->wep_key[i], 4886 params->wep_key_len[i]); 4887 if (params->wep_tx_keyidx != i) 4888 continue; 4889 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0, 4890 params->wep_key[i], params->wep_key_len[i])) { 4891 nlmsg_free(msg); 4892 return -1; 4893 } 4894 } 4895 4896 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 4897 if (params->bssid) { 4898 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, 4899 MAC2STR(params->bssid)); 4900 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); 4901 } 4902 if (params->freq) { 4903 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); 4904 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); 4905 } 4906 if (params->ssid) { 4907 wpa_hexdump_ascii(MSG_DEBUG, " * SSID", 4908 params->ssid, params->ssid_len); 4909 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 4910 params->ssid); 4911 } 4912 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len); 4913 if (params->ie) 4914 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie); 4915 if (params->sae_data) { 4916 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data, 4917 params->sae_data_len); 4918 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len, 4919 params->sae_data); 4920 } 4921 if (params->auth_alg & WPA_AUTH_ALG_OPEN) 4922 type = NL80211_AUTHTYPE_OPEN_SYSTEM; 4923 else if (params->auth_alg & WPA_AUTH_ALG_SHARED) 4924 type = NL80211_AUTHTYPE_SHARED_KEY; 4925 else if (params->auth_alg & WPA_AUTH_ALG_LEAP) 4926 type = NL80211_AUTHTYPE_NETWORK_EAP; 4927 else if (params->auth_alg & WPA_AUTH_ALG_FT) 4928 type = NL80211_AUTHTYPE_FT; 4929 else if (params->auth_alg & WPA_AUTH_ALG_SAE) 4930 type = NL80211_AUTHTYPE_SAE; 4931 else 4932 goto nla_put_failure; 4933 wpa_printf(MSG_DEBUG, " * Auth Type %d", type); 4934 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type); 4935 if (params->local_state_change) { 4936 wpa_printf(MSG_DEBUG, " * Local state change only"); 4937 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE); 4938 } 4939 4940 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4941 msg = NULL; 4942 if (ret) { 4943 wpa_dbg(drv->ctx, MSG_DEBUG, 4944 "nl80211: MLME command failed (auth): ret=%d (%s)", 4945 ret, strerror(-ret)); 4946 count++; 4947 if (ret == -EALREADY && count == 1 && params->bssid && 4948 !params->local_state_change) { 4949 /* 4950 * mac80211 does not currently accept new 4951 * authentication if we are already authenticated. As a 4952 * workaround, force deauthentication and try again. 4953 */ 4954 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication " 4955 "after forced deauthentication"); 4956 wpa_driver_nl80211_deauthenticate( 4957 bss, params->bssid, 4958 WLAN_REASON_PREV_AUTH_NOT_VALID); 4959 nlmsg_free(msg); 4960 goto retry; 4961 } 4962 4963 if (ret == -ENOENT && params->freq && !is_retry) { 4964 /* 4965 * cfg80211 has likely expired the BSS entry even 4966 * though it was previously available in our internal 4967 * BSS table. To recover quickly, start a single 4968 * channel scan on the specified channel. 4969 */ 4970 struct wpa_driver_scan_params scan; 4971 int freqs[2]; 4972 4973 os_memset(&scan, 0, sizeof(scan)); 4974 scan.num_ssids = 1; 4975 if (params->ssid) { 4976 scan.ssids[0].ssid = params->ssid; 4977 scan.ssids[0].ssid_len = params->ssid_len; 4978 } 4979 freqs[0] = params->freq; 4980 freqs[1] = 0; 4981 scan.freqs = freqs; 4982 wpa_printf(MSG_DEBUG, "nl80211: Trigger single " 4983 "channel scan to refresh cfg80211 BSS " 4984 "entry"); 4985 ret = wpa_driver_nl80211_scan(bss, &scan); 4986 if (ret == 0) { 4987 nl80211_copy_auth_params(drv, params); 4988 drv->scan_for_auth = 1; 4989 } 4990 } else if (is_retry) { 4991 /* 4992 * Need to indicate this with an event since the return 4993 * value from the retry is not delivered to core code. 4994 */ 4995 union wpa_event_data event; 4996 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry " 4997 "failed"); 4998 os_memset(&event, 0, sizeof(event)); 4999 os_memcpy(event.timeout_event.addr, drv->auth_bssid_, 5000 ETH_ALEN); 5001 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT, 5002 &event); 5003 } 5004 5005 goto nla_put_failure; 5006 } 5007 ret = 0; 5008 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send " 5009 "successfully"); 5010 5011nla_put_failure: 5012 nlmsg_free(msg); 5013 return ret; 5014} 5015 5016 5017static int wpa_driver_nl80211_authenticate_retry( 5018 struct wpa_driver_nl80211_data *drv) 5019{ 5020 struct wpa_driver_auth_params params; 5021 struct i802_bss *bss = &drv->first_bss; 5022 int i; 5023 5024 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again"); 5025 5026 os_memset(¶ms, 0, sizeof(params)); 5027 params.freq = drv->auth_freq; 5028 params.auth_alg = drv->auth_alg; 5029 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx; 5030 params.local_state_change = drv->auth_local_state_change; 5031 params.p2p = drv->auth_p2p; 5032 5033 if (!is_zero_ether_addr(drv->auth_bssid_)) 5034 params.bssid = drv->auth_bssid_; 5035 5036 if (drv->auth_ssid_len) { 5037 params.ssid = drv->auth_ssid; 5038 params.ssid_len = drv->auth_ssid_len; 5039 } 5040 5041 params.ie = drv->auth_ie; 5042 params.ie_len = drv->auth_ie_len; 5043 5044 for (i = 0; i < 4; i++) { 5045 if (drv->auth_wep_key_len[i]) { 5046 params.wep_key[i] = drv->auth_wep_key[i]; 5047 params.wep_key_len[i] = drv->auth_wep_key_len[i]; 5048 } 5049 } 5050 5051 drv->retry_auth = 1; 5052 return wpa_driver_nl80211_authenticate(bss, ¶ms); 5053} 5054 5055 5056struct phy_info_arg { 5057 u16 *num_modes; 5058 struct hostapd_hw_modes *modes; 5059 int last_mode, last_chan_idx; 5060}; 5061 5062static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa, 5063 struct nlattr *ampdu_factor, 5064 struct nlattr *ampdu_density, 5065 struct nlattr *mcs_set) 5066{ 5067 if (capa) 5068 mode->ht_capab = nla_get_u16(capa); 5069 5070 if (ampdu_factor) 5071 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03; 5072 5073 if (ampdu_density) 5074 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2; 5075 5076 if (mcs_set && nla_len(mcs_set) >= 16) { 5077 u8 *mcs; 5078 mcs = nla_data(mcs_set); 5079 os_memcpy(mode->mcs_set, mcs, 16); 5080 } 5081} 5082 5083 5084static void phy_info_vht_capa(struct hostapd_hw_modes *mode, 5085 struct nlattr *capa, 5086 struct nlattr *mcs_set) 5087{ 5088 if (capa) 5089 mode->vht_capab = nla_get_u32(capa); 5090 5091 if (mcs_set && nla_len(mcs_set) >= 8) { 5092 u8 *mcs; 5093 mcs = nla_data(mcs_set); 5094 os_memcpy(mode->vht_mcs_set, mcs, 8); 5095 } 5096} 5097 5098 5099static void phy_info_freq(struct hostapd_hw_modes *mode, 5100 struct hostapd_channel_data *chan, 5101 struct nlattr *tb_freq[]) 5102{ 5103 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]); 5104 chan->flag = 0; 5105 5106 /* mode is not set */ 5107 if (mode->mode >= NUM_HOSTAPD_MODES) { 5108 /* crude heuristic */ 5109 if (chan->freq < 4000) 5110 mode->mode = HOSTAPD_MODE_IEEE80211B; 5111 else if (chan->freq > 50000) 5112 mode->mode = HOSTAPD_MODE_IEEE80211AD; 5113 else 5114 mode->mode = HOSTAPD_MODE_IEEE80211A; 5115 } 5116 5117 switch (mode->mode) { 5118 case HOSTAPD_MODE_IEEE80211AD: 5119 chan->chan = (chan->freq - 56160) / 2160; 5120 break; 5121 case HOSTAPD_MODE_IEEE80211A: 5122 chan->chan = chan->freq / 5 - 1000; 5123 break; 5124 case HOSTAPD_MODE_IEEE80211B: 5125 case HOSTAPD_MODE_IEEE80211G: 5126 if (chan->freq == 2484) 5127 chan->chan = 14; 5128 else 5129 chan->chan = (chan->freq - 2407) / 5; 5130 break; 5131 default: 5132 break; 5133 } 5134 5135 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) 5136 chan->flag |= HOSTAPD_CHAN_DISABLED; 5137 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]) 5138 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN; 5139 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS]) 5140 chan->flag |= HOSTAPD_CHAN_NO_IBSS; 5141 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR]) 5142 chan->flag |= HOSTAPD_CHAN_RADAR; 5143 5144 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] && 5145 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) 5146 chan->max_tx_power = nla_get_u32( 5147 tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100; 5148} 5149 5150 5151static int phy_info_freqs(struct phy_info_arg *phy_info, 5152 struct hostapd_hw_modes *mode, struct nlattr *tb) 5153{ 5154 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { 5155 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, 5156 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, 5157 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG }, 5158 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG }, 5159 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG }, 5160 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 }, 5161 }; 5162 int new_channels = 0; 5163 struct hostapd_channel_data *channel; 5164 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1]; 5165 struct nlattr *nl_freq; 5166 int rem_freq, idx; 5167 5168 if (tb == NULL) 5169 return NL_OK; 5170 5171 nla_for_each_nested(nl_freq, tb, rem_freq) { 5172 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, 5173 nla_data(nl_freq), nla_len(nl_freq), freq_policy); 5174 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) 5175 continue; 5176 new_channels++; 5177 } 5178 5179 channel = os_realloc_array(mode->channels, 5180 mode->num_channels + new_channels, 5181 sizeof(struct hostapd_channel_data)); 5182 if (!channel) 5183 return NL_SKIP; 5184 5185 mode->channels = channel; 5186 mode->num_channels += new_channels; 5187 5188 idx = phy_info->last_chan_idx; 5189 5190 nla_for_each_nested(nl_freq, tb, rem_freq) { 5191 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, 5192 nla_data(nl_freq), nla_len(nl_freq), freq_policy); 5193 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) 5194 continue; 5195 phy_info_freq(mode, &mode->channels[idx], tb_freq); 5196 idx++; 5197 } 5198 phy_info->last_chan_idx = idx; 5199 5200 return NL_OK; 5201} 5202 5203 5204static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb) 5205{ 5206 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = { 5207 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 }, 5208 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = 5209 { .type = NLA_FLAG }, 5210 }; 5211 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1]; 5212 struct nlattr *nl_rate; 5213 int rem_rate, idx; 5214 5215 if (tb == NULL) 5216 return NL_OK; 5217 5218 nla_for_each_nested(nl_rate, tb, rem_rate) { 5219 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, 5220 nla_data(nl_rate), nla_len(nl_rate), 5221 rate_policy); 5222 if (!tb_rate[NL80211_BITRATE_ATTR_RATE]) 5223 continue; 5224 mode->num_rates++; 5225 } 5226 5227 mode->rates = os_calloc(mode->num_rates, sizeof(int)); 5228 if (!mode->rates) 5229 return NL_SKIP; 5230 5231 idx = 0; 5232 5233 nla_for_each_nested(nl_rate, tb, rem_rate) { 5234 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, 5235 nla_data(nl_rate), nla_len(nl_rate), 5236 rate_policy); 5237 if (!tb_rate[NL80211_BITRATE_ATTR_RATE]) 5238 continue; 5239 mode->rates[idx] = nla_get_u32( 5240 tb_rate[NL80211_BITRATE_ATTR_RATE]); 5241 5242 /* crude heuristic */ 5243 if (mode->mode == HOSTAPD_MODE_IEEE80211B && 5244 mode->rates[idx] > 200) 5245 mode->mode = HOSTAPD_MODE_IEEE80211G; 5246 5247 idx++; 5248 } 5249 5250 return NL_OK; 5251} 5252 5253 5254static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band) 5255{ 5256 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1]; 5257 struct hostapd_hw_modes *mode; 5258 int ret; 5259 5260 if (phy_info->last_mode != nl_band->nla_type) { 5261 mode = os_realloc_array(phy_info->modes, 5262 *phy_info->num_modes + 1, 5263 sizeof(*mode)); 5264 if (!mode) 5265 return NL_SKIP; 5266 phy_info->modes = mode; 5267 5268 mode = &phy_info->modes[*(phy_info->num_modes)]; 5269 os_memset(mode, 0, sizeof(*mode)); 5270 mode->mode = NUM_HOSTAPD_MODES; 5271 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN; 5272 *(phy_info->num_modes) += 1; 5273 phy_info->last_mode = nl_band->nla_type; 5274 phy_info->last_chan_idx = 0; 5275 } else 5276 mode = &phy_info->modes[*(phy_info->num_modes) - 1]; 5277 5278 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band), 5279 nla_len(nl_band), NULL); 5280 5281 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA], 5282 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR], 5283 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY], 5284 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]); 5285 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA], 5286 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]); 5287 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]); 5288 if (ret != NL_OK) 5289 return ret; 5290 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]); 5291 if (ret != NL_OK) 5292 return ret; 5293 5294 return NL_OK; 5295} 5296 5297 5298static int phy_info_handler(struct nl_msg *msg, void *arg) 5299{ 5300 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; 5301 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 5302 struct phy_info_arg *phy_info = arg; 5303 struct nlattr *nl_band; 5304 int rem_band; 5305 5306 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 5307 genlmsg_attrlen(gnlh, 0), NULL); 5308 5309 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS]) 5310 return NL_SKIP; 5311 5312 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) 5313 { 5314 int res = phy_info_band(phy_info, nl_band); 5315 if (res != NL_OK) 5316 return res; 5317 } 5318 5319 return NL_SKIP; 5320} 5321 5322 5323static struct hostapd_hw_modes * 5324wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes) 5325{ 5326 u16 m; 5327 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode; 5328 int i, mode11g_idx = -1; 5329 5330 /* If only 802.11g mode is included, use it to construct matching 5331 * 802.11b mode data. */ 5332 5333 for (m = 0; m < *num_modes; m++) { 5334 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B) 5335 return modes; /* 802.11b already included */ 5336 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G) 5337 mode11g_idx = m; 5338 } 5339 5340 if (mode11g_idx < 0) 5341 return modes; /* 2.4 GHz band not supported at all */ 5342 5343 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes)); 5344 if (nmodes == NULL) 5345 return modes; /* Could not add 802.11b mode */ 5346 5347 mode = &nmodes[*num_modes]; 5348 os_memset(mode, 0, sizeof(*mode)); 5349 (*num_modes)++; 5350 modes = nmodes; 5351 5352 mode->mode = HOSTAPD_MODE_IEEE80211B; 5353 5354 mode11g = &modes[mode11g_idx]; 5355 mode->num_channels = mode11g->num_channels; 5356 mode->channels = os_malloc(mode11g->num_channels * 5357 sizeof(struct hostapd_channel_data)); 5358 if (mode->channels == NULL) { 5359 (*num_modes)--; 5360 return modes; /* Could not add 802.11b mode */ 5361 } 5362 os_memcpy(mode->channels, mode11g->channels, 5363 mode11g->num_channels * sizeof(struct hostapd_channel_data)); 5364 5365 mode->num_rates = 0; 5366 mode->rates = os_malloc(4 * sizeof(int)); 5367 if (mode->rates == NULL) { 5368 os_free(mode->channels); 5369 (*num_modes)--; 5370 return modes; /* Could not add 802.11b mode */ 5371 } 5372 5373 for (i = 0; i < mode11g->num_rates; i++) { 5374 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 && 5375 mode11g->rates[i] != 55 && mode11g->rates[i] != 110) 5376 continue; 5377 mode->rates[mode->num_rates] = mode11g->rates[i]; 5378 mode->num_rates++; 5379 if (mode->num_rates == 4) 5380 break; 5381 } 5382 5383 if (mode->num_rates == 0) { 5384 os_free(mode->channels); 5385 os_free(mode->rates); 5386 (*num_modes)--; 5387 return modes; /* No 802.11b rates */ 5388 } 5389 5390 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g " 5391 "information"); 5392 5393 return modes; 5394} 5395 5396 5397static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start, 5398 int end) 5399{ 5400 int c; 5401 5402 for (c = 0; c < mode->num_channels; c++) { 5403 struct hostapd_channel_data *chan = &mode->channels[c]; 5404 if (chan->freq - 10 >= start && chan->freq + 10 <= end) 5405 chan->flag |= HOSTAPD_CHAN_HT40; 5406 } 5407} 5408 5409 5410static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start, 5411 int end) 5412{ 5413 int c; 5414 5415 for (c = 0; c < mode->num_channels; c++) { 5416 struct hostapd_channel_data *chan = &mode->channels[c]; 5417 if (!(chan->flag & HOSTAPD_CHAN_HT40)) 5418 continue; 5419 if (chan->freq - 30 >= start && chan->freq - 10 <= end) 5420 chan->flag |= HOSTAPD_CHAN_HT40MINUS; 5421 if (chan->freq + 10 >= start && chan->freq + 30 <= end) 5422 chan->flag |= HOSTAPD_CHAN_HT40PLUS; 5423 } 5424} 5425 5426 5427static void nl80211_reg_rule_ht40(struct nlattr *tb[], 5428 struct phy_info_arg *results) 5429{ 5430 u32 start, end, max_bw; 5431 u16 m; 5432 5433 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL || 5434 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL || 5435 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL) 5436 return; 5437 5438 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000; 5439 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000; 5440 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000; 5441 5442 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz", 5443 start, end, max_bw); 5444 if (max_bw < 40) 5445 return; 5446 5447 for (m = 0; m < *results->num_modes; m++) { 5448 if (!(results->modes[m].ht_capab & 5449 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) 5450 continue; 5451 nl80211_set_ht40_mode(&results->modes[m], start, end); 5452 } 5453} 5454 5455 5456static void nl80211_reg_rule_sec(struct nlattr *tb[], 5457 struct phy_info_arg *results) 5458{ 5459 u32 start, end, max_bw; 5460 u16 m; 5461 5462 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL || 5463 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL || 5464 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL) 5465 return; 5466 5467 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000; 5468 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000; 5469 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000; 5470 5471 if (max_bw < 20) 5472 return; 5473 5474 for (m = 0; m < *results->num_modes; m++) { 5475 if (!(results->modes[m].ht_capab & 5476 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) 5477 continue; 5478 nl80211_set_ht40_mode_sec(&results->modes[m], start, end); 5479 } 5480} 5481 5482 5483static int nl80211_get_reg(struct nl_msg *msg, void *arg) 5484{ 5485 struct phy_info_arg *results = arg; 5486 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; 5487 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 5488 struct nlattr *nl_rule; 5489 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1]; 5490 int rem_rule; 5491 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { 5492 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 }, 5493 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 }, 5494 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 }, 5495 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 }, 5496 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 }, 5497 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 }, 5498 }; 5499 5500 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 5501 genlmsg_attrlen(gnlh, 0), NULL); 5502 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] || 5503 !tb_msg[NL80211_ATTR_REG_RULES]) { 5504 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information " 5505 "available"); 5506 return NL_SKIP; 5507 } 5508 5509 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s", 5510 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2])); 5511 5512 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) 5513 { 5514 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, 5515 nla_data(nl_rule), nla_len(nl_rule), reg_policy); 5516 nl80211_reg_rule_ht40(tb_rule, results); 5517 } 5518 5519 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) 5520 { 5521 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, 5522 nla_data(nl_rule), nla_len(nl_rule), reg_policy); 5523 nl80211_reg_rule_sec(tb_rule, results); 5524 } 5525 5526 return NL_SKIP; 5527} 5528 5529 5530static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv, 5531 struct phy_info_arg *results) 5532{ 5533 struct nl_msg *msg; 5534 5535 msg = nlmsg_alloc(); 5536 if (!msg) 5537 return -ENOMEM; 5538 5539 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG); 5540 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results); 5541} 5542 5543 5544static struct hostapd_hw_modes * 5545wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags) 5546{ 5547 u32 feat; 5548 struct i802_bss *bss = priv; 5549 struct wpa_driver_nl80211_data *drv = bss->drv; 5550 struct nl_msg *msg; 5551 struct phy_info_arg result = { 5552 .num_modes = num_modes, 5553 .modes = NULL, 5554 .last_mode = -1, 5555 }; 5556 5557 *num_modes = 0; 5558 *flags = 0; 5559 5560 msg = nlmsg_alloc(); 5561 if (!msg) 5562 return NULL; 5563 5564 feat = get_nl80211_protocol_features(drv); 5565 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP) 5566 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY); 5567 else 5568 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY); 5569 5570 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); 5571 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 5572 5573 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) { 5574 nl80211_set_ht40_flags(drv, &result); 5575 return wpa_driver_nl80211_add_11b(result.modes, num_modes); 5576 } 5577 msg = NULL; 5578 nla_put_failure: 5579 nlmsg_free(msg); 5580 return NULL; 5581} 5582 5583 5584static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv, 5585 const void *data, size_t len, 5586 int encrypt, int noack) 5587{ 5588 __u8 rtap_hdr[] = { 5589 0x00, 0x00, /* radiotap version */ 5590 0x0e, 0x00, /* radiotap length */ 5591 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */ 5592 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */ 5593 0x00, /* padding */ 5594 0x00, 0x00, /* RX and TX flags to indicate that */ 5595 0x00, 0x00, /* this is the injected frame directly */ 5596 }; 5597 struct iovec iov[2] = { 5598 { 5599 .iov_base = &rtap_hdr, 5600 .iov_len = sizeof(rtap_hdr), 5601 }, 5602 { 5603 .iov_base = (void *) data, 5604 .iov_len = len, 5605 } 5606 }; 5607 struct msghdr msg = { 5608 .msg_name = NULL, 5609 .msg_namelen = 0, 5610 .msg_iov = iov, 5611 .msg_iovlen = 2, 5612 .msg_control = NULL, 5613 .msg_controllen = 0, 5614 .msg_flags = 0, 5615 }; 5616 int res; 5617 u16 txflags = 0; 5618 5619 if (encrypt) 5620 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP; 5621 5622 if (drv->monitor_sock < 0) { 5623 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available " 5624 "for %s", __func__); 5625 return -1; 5626 } 5627 5628 if (noack) 5629 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK; 5630 WPA_PUT_LE16(&rtap_hdr[12], txflags); 5631 5632 res = sendmsg(drv->monitor_sock, &msg, 0); 5633 if (res < 0) { 5634 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno)); 5635 return -1; 5636 } 5637 return 0; 5638} 5639 5640 5641static int wpa_driver_nl80211_send_frame(struct i802_bss *bss, 5642 const void *data, size_t len, 5643 int encrypt, int noack, 5644 unsigned int freq, int no_cck, 5645 int offchanok, unsigned int wait_time) 5646{ 5647 struct wpa_driver_nl80211_data *drv = bss->drv; 5648 u64 cookie; 5649 5650 if (freq == 0) 5651 freq = bss->freq; 5652 5653 if (drv->use_monitor) 5654 return wpa_driver_nl80211_send_mntr(drv, data, len, 5655 encrypt, noack); 5656 5657 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len, 5658 &cookie, no_cck, noack, offchanok); 5659} 5660 5661 5662static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data, 5663 size_t data_len, int noack, 5664 unsigned int freq, int no_cck, 5665 int offchanok, 5666 unsigned int wait_time) 5667{ 5668 struct wpa_driver_nl80211_data *drv = bss->drv; 5669 struct ieee80211_mgmt *mgmt; 5670 int encrypt = 1; 5671 u16 fc; 5672 5673 mgmt = (struct ieee80211_mgmt *) data; 5674 fc = le_to_host16(mgmt->frame_control); 5675 5676 if (is_sta_interface(drv->nlmode) && 5677 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && 5678 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) { 5679 /* 5680 * The use of last_mgmt_freq is a bit of a hack, 5681 * but it works due to the single-threaded nature 5682 * of wpa_supplicant. 5683 */ 5684 if (freq == 0) 5685 freq = drv->last_mgmt_freq; 5686 return nl80211_send_frame_cmd(bss, freq, 0, 5687 data, data_len, NULL, 1, noack, 5688 1); 5689 } 5690 5691 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) { 5692 if (freq == 0) 5693 freq = bss->freq; 5694 return nl80211_send_frame_cmd(bss, freq, 5695 (int) freq == bss->freq ? 0 : 5696 wait_time, 5697 data, data_len, 5698 &drv->send_action_cookie, 5699 no_cck, noack, offchanok); 5700 } 5701 5702 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && 5703 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) { 5704 /* 5705 * Only one of the authentication frame types is encrypted. 5706 * In order for static WEP encryption to work properly (i.e., 5707 * to not encrypt the frame), we need to tell mac80211 about 5708 * the frames that must not be encrypted. 5709 */ 5710 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg); 5711 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction); 5712 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3) 5713 encrypt = 0; 5714 } 5715 5716 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 5717 noack, freq, no_cck, offchanok, 5718 wait_time); 5719} 5720 5721 5722static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble, 5723 int slot, int ht_opmode, int ap_isolate, 5724 int *basic_rates) 5725{ 5726 struct wpa_driver_nl80211_data *drv = bss->drv; 5727 struct nl_msg *msg; 5728 5729 msg = nlmsg_alloc(); 5730 if (!msg) 5731 return -ENOMEM; 5732 5733 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS); 5734 5735 if (cts >= 0) 5736 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts); 5737 if (preamble >= 0) 5738 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble); 5739 if (slot >= 0) 5740 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot); 5741 if (ht_opmode >= 0) 5742 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode); 5743 if (ap_isolate >= 0) 5744 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate); 5745 5746 if (basic_rates) { 5747 u8 rates[NL80211_MAX_SUPP_RATES]; 5748 u8 rates_len = 0; 5749 int i; 5750 5751 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; 5752 i++) 5753 rates[rates_len++] = basic_rates[i] / 5; 5754 5755 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates); 5756 } 5757 5758 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 5759 5760 return send_and_recv_msgs(drv, msg, NULL, NULL); 5761 nla_put_failure: 5762 nlmsg_free(msg); 5763 return -ENOBUFS; 5764} 5765 5766 5767static int wpa_driver_nl80211_set_ap(void *priv, 5768 struct wpa_driver_ap_params *params) 5769{ 5770 struct i802_bss *bss = priv; 5771 struct wpa_driver_nl80211_data *drv = bss->drv; 5772 struct nl_msg *msg; 5773 u8 cmd = NL80211_CMD_NEW_BEACON; 5774 int ret; 5775 int beacon_set; 5776 int ifindex = if_nametoindex(bss->ifname); 5777 int num_suites; 5778 u32 suites[10]; 5779 u32 ver; 5780 5781 beacon_set = bss->beacon_set; 5782 5783 msg = nlmsg_alloc(); 5784 if (!msg) 5785 return -ENOMEM; 5786 5787 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)", 5788 beacon_set); 5789 if (beacon_set) 5790 cmd = NL80211_CMD_SET_BEACON; 5791 5792 nl80211_cmd(drv, msg, 0, cmd); 5793 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head); 5794 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail); 5795 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 5796 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int); 5797 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period); 5798 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 5799 params->ssid); 5800 if (params->proberesp && params->proberesp_len) 5801 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len, 5802 params->proberesp); 5803 switch (params->hide_ssid) { 5804 case NO_SSID_HIDING: 5805 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID, 5806 NL80211_HIDDEN_SSID_NOT_IN_USE); 5807 break; 5808 case HIDDEN_SSID_ZERO_LEN: 5809 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID, 5810 NL80211_HIDDEN_SSID_ZERO_LEN); 5811 break; 5812 case HIDDEN_SSID_ZERO_CONTENTS: 5813 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID, 5814 NL80211_HIDDEN_SSID_ZERO_CONTENTS); 5815 break; 5816 } 5817 if (params->privacy) 5818 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY); 5819 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) == 5820 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) { 5821 /* Leave out the attribute */ 5822 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) 5823 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, 5824 NL80211_AUTHTYPE_SHARED_KEY); 5825 else 5826 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, 5827 NL80211_AUTHTYPE_OPEN_SYSTEM); 5828 5829 ver = 0; 5830 if (params->wpa_version & WPA_PROTO_WPA) 5831 ver |= NL80211_WPA_VERSION_1; 5832 if (params->wpa_version & WPA_PROTO_RSN) 5833 ver |= NL80211_WPA_VERSION_2; 5834 if (ver) 5835 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver); 5836 5837 num_suites = 0; 5838 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X) 5839 suites[num_suites++] = WLAN_AKM_SUITE_8021X; 5840 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK) 5841 suites[num_suites++] = WLAN_AKM_SUITE_PSK; 5842 if (num_suites) { 5843 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES, 5844 num_suites * sizeof(u32), suites); 5845 } 5846 5847 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X && 5848 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) 5849 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT); 5850 5851 num_suites = 0; 5852 if (params->pairwise_ciphers & WPA_CIPHER_CCMP) 5853 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP; 5854 if (params->pairwise_ciphers & WPA_CIPHER_GCMP) 5855 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP; 5856 if (params->pairwise_ciphers & WPA_CIPHER_TKIP) 5857 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP; 5858 if (params->pairwise_ciphers & WPA_CIPHER_WEP104) 5859 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104; 5860 if (params->pairwise_ciphers & WPA_CIPHER_WEP40) 5861 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40; 5862 if (num_suites) { 5863 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, 5864 num_suites * sizeof(u32), suites); 5865 } 5866 5867 switch (params->group_cipher) { 5868 case WPA_CIPHER_CCMP: 5869 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, 5870 WLAN_CIPHER_SUITE_CCMP); 5871 break; 5872 case WPA_CIPHER_GCMP: 5873 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, 5874 WLAN_CIPHER_SUITE_GCMP); 5875 break; 5876 case WPA_CIPHER_TKIP: 5877 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, 5878 WLAN_CIPHER_SUITE_TKIP); 5879 break; 5880 case WPA_CIPHER_WEP104: 5881 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, 5882 WLAN_CIPHER_SUITE_WEP104); 5883 break; 5884 case WPA_CIPHER_WEP40: 5885 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, 5886 WLAN_CIPHER_SUITE_WEP40); 5887 break; 5888 } 5889 5890 if (params->beacon_ies) { 5891 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies), 5892 wpabuf_head(params->beacon_ies)); 5893 } 5894 if (params->proberesp_ies) { 5895 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP, 5896 wpabuf_len(params->proberesp_ies), 5897 wpabuf_head(params->proberesp_ies)); 5898 } 5899 if (params->assocresp_ies) { 5900 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP, 5901 wpabuf_len(params->assocresp_ies), 5902 wpabuf_head(params->assocresp_ies)); 5903 } 5904 5905 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) { 5906 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT, 5907 params->ap_max_inactivity); 5908 } 5909 5910 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5911 if (ret) { 5912 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)", 5913 ret, strerror(-ret)); 5914 } else { 5915 bss->beacon_set = 1; 5916 nl80211_set_bss(bss, params->cts_protect, params->preamble, 5917 params->short_slot_time, params->ht_opmode, 5918 params->isolate, params->basic_rates); 5919 } 5920 return ret; 5921 nla_put_failure: 5922 nlmsg_free(msg); 5923 return -ENOBUFS; 5924} 5925 5926 5927static int wpa_driver_nl80211_set_freq(struct i802_bss *bss, 5928 struct hostapd_freq_params *freq) 5929{ 5930 struct wpa_driver_nl80211_data *drv = bss->drv; 5931 struct nl_msg *msg; 5932 int ret; 5933 5934 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d," 5935 " bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)", 5936 freq->freq, freq->ht_enabled, freq->vht_enabled, 5937 freq->bandwidth, freq->center_freq1, freq->center_freq2); 5938 msg = nlmsg_alloc(); 5939 if (!msg) 5940 return -1; 5941 5942 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY); 5943 5944 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 5945 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq); 5946 if (freq->vht_enabled) { 5947 switch (freq->bandwidth) { 5948 case 20: 5949 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, 5950 NL80211_CHAN_WIDTH_20); 5951 break; 5952 case 40: 5953 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, 5954 NL80211_CHAN_WIDTH_40); 5955 break; 5956 case 80: 5957 if (freq->center_freq2) 5958 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, 5959 NL80211_CHAN_WIDTH_80P80); 5960 else 5961 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, 5962 NL80211_CHAN_WIDTH_80); 5963 break; 5964 case 160: 5965 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, 5966 NL80211_CHAN_WIDTH_160); 5967 break; 5968 default: 5969 return -1; 5970 } 5971 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1); 5972 if (freq->center_freq2) 5973 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2, 5974 freq->center_freq2); 5975 } else if (freq->ht_enabled) { 5976 switch (freq->sec_channel_offset) { 5977 case -1: 5978 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, 5979 NL80211_CHAN_HT40MINUS); 5980 break; 5981 case 1: 5982 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, 5983 NL80211_CHAN_HT40PLUS); 5984 break; 5985 default: 5986 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, 5987 NL80211_CHAN_HT20); 5988 break; 5989 } 5990 } 5991 5992 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5993 msg = NULL; 5994 if (ret == 0) { 5995 bss->freq = freq->freq; 5996 return 0; 5997 } 5998 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): " 5999 "%d (%s)", freq->freq, ret, strerror(-ret)); 6000nla_put_failure: 6001 nlmsg_free(msg); 6002 return -1; 6003} 6004 6005 6006static u32 sta_flags_nl80211(int flags) 6007{ 6008 u32 f = 0; 6009 6010 if (flags & WPA_STA_AUTHORIZED) 6011 f |= BIT(NL80211_STA_FLAG_AUTHORIZED); 6012 if (flags & WPA_STA_WMM) 6013 f |= BIT(NL80211_STA_FLAG_WME); 6014 if (flags & WPA_STA_SHORT_PREAMBLE) 6015 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE); 6016 if (flags & WPA_STA_MFP) 6017 f |= BIT(NL80211_STA_FLAG_MFP); 6018 if (flags & WPA_STA_TDLS_PEER) 6019 f |= BIT(NL80211_STA_FLAG_TDLS_PEER); 6020 6021 return f; 6022} 6023 6024 6025static int wpa_driver_nl80211_sta_add(void *priv, 6026 struct hostapd_sta_add_params *params) 6027{ 6028 struct i802_bss *bss = priv; 6029 struct wpa_driver_nl80211_data *drv = bss->drv; 6030 struct nl_msg *msg, *wme = NULL; 6031 struct nl80211_sta_flag_update upd; 6032 int ret = -ENOBUFS; 6033 6034 if ((params->flags & WPA_STA_TDLS_PEER) && 6035 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) 6036 return -EOPNOTSUPP; 6037 6038 msg = nlmsg_alloc(); 6039 if (!msg) 6040 return -ENOMEM; 6041 6042 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR, 6043 params->set ? "Set" : "Add", MAC2STR(params->addr)); 6044 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION : 6045 NL80211_CMD_NEW_STATION); 6046 6047 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 6048 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr); 6049 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len, 6050 params->supp_rates); 6051 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates, 6052 params->supp_rates_len); 6053 if (!params->set) { 6054 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid); 6055 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid); 6056 wpa_printf(MSG_DEBUG, " * listen_interval=%u", 6057 params->listen_interval); 6058 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, 6059 params->listen_interval); 6060 } 6061 if (params->ht_capabilities) { 6062 wpa_hexdump(MSG_DEBUG, " * ht_capabilities", 6063 (u8 *) params->ht_capabilities, 6064 sizeof(*params->ht_capabilities)); 6065 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, 6066 sizeof(*params->ht_capabilities), 6067 params->ht_capabilities); 6068 } 6069 6070 if (params->vht_capabilities) { 6071 wpa_hexdump(MSG_DEBUG, " * vht_capabilities", 6072 (u8 *) params->vht_capabilities, 6073 sizeof(*params->vht_capabilities)); 6074 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, 6075 sizeof(*params->vht_capabilities), 6076 params->vht_capabilities); 6077 } 6078 6079 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability); 6080 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability); 6081 6082 if (params->ext_capab) { 6083 wpa_hexdump(MSG_DEBUG, " * ext_capab", 6084 params->ext_capab, params->ext_capab_len); 6085 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY, 6086 params->ext_capab_len, params->ext_capab); 6087 } 6088 6089 os_memset(&upd, 0, sizeof(upd)); 6090 upd.mask = sta_flags_nl80211(params->flags); 6091 upd.set = upd.mask; 6092 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x", 6093 upd.set, upd.mask); 6094 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); 6095 6096 if (params->flags & WPA_STA_WMM) { 6097 wme = nlmsg_alloc(); 6098 if (!wme) 6099 goto nla_put_failure; 6100 6101 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo); 6102 NLA_PUT_U8(wme, NL80211_STA_WME_UAPSD_QUEUES, 6103 params->qosinfo & WMM_QOSINFO_STA_AC_MASK); 6104 NLA_PUT_U8(wme, NL80211_STA_WME_MAX_SP, 6105 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) & 6106 WMM_QOSINFO_STA_SP_MASK); 6107 if (nla_put_nested(msg, NL80211_ATTR_STA_WME, wme) < 0) 6108 goto nla_put_failure; 6109 } 6110 6111 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 6112 msg = NULL; 6113 if (ret) 6114 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION " 6115 "result: %d (%s)", params->set ? "SET" : "NEW", ret, 6116 strerror(-ret)); 6117 if (ret == -EEXIST) 6118 ret = 0; 6119 nla_put_failure: 6120 nlmsg_free(wme); 6121 nlmsg_free(msg); 6122 return ret; 6123} 6124 6125 6126static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr) 6127{ 6128 struct wpa_driver_nl80211_data *drv = bss->drv; 6129 struct nl_msg *msg; 6130 int ret; 6131 6132 msg = nlmsg_alloc(); 6133 if (!msg) 6134 return -ENOMEM; 6135 6136 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION); 6137 6138 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, 6139 if_nametoindex(bss->ifname)); 6140 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 6141 6142 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 6143 if (ret == -ENOENT) 6144 return 0; 6145 return ret; 6146 nla_put_failure: 6147 nlmsg_free(msg); 6148 return -ENOBUFS; 6149} 6150 6151 6152static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, 6153 int ifidx) 6154{ 6155 struct nl_msg *msg; 6156 6157 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx); 6158 6159 /* stop listening for EAPOL on this interface */ 6160 del_ifidx(drv, ifidx); 6161 6162 msg = nlmsg_alloc(); 6163 if (!msg) 6164 goto nla_put_failure; 6165 6166 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE); 6167 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx); 6168 6169 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0) 6170 return; 6171 msg = NULL; 6172 nla_put_failure: 6173 nlmsg_free(msg); 6174 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx); 6175} 6176 6177 6178static const char * nl80211_iftype_str(enum nl80211_iftype mode) 6179{ 6180 switch (mode) { 6181 case NL80211_IFTYPE_ADHOC: 6182 return "ADHOC"; 6183 case NL80211_IFTYPE_STATION: 6184 return "STATION"; 6185 case NL80211_IFTYPE_AP: 6186 return "AP"; 6187 case NL80211_IFTYPE_MONITOR: 6188 return "MONITOR"; 6189 case NL80211_IFTYPE_P2P_CLIENT: 6190 return "P2P_CLIENT"; 6191 case NL80211_IFTYPE_P2P_GO: 6192 return "P2P_GO"; 6193 default: 6194 return "unknown"; 6195 } 6196} 6197 6198 6199static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv, 6200 const char *ifname, 6201 enum nl80211_iftype iftype, 6202 const u8 *addr, int wds) 6203{ 6204 struct nl_msg *msg, *flags = NULL; 6205 int ifidx; 6206 int ret = -ENOBUFS; 6207 6208 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)", 6209 iftype, nl80211_iftype_str(iftype)); 6210 6211 msg = nlmsg_alloc(); 6212 if (!msg) 6213 return -1; 6214 6215 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE); 6216 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 6217 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname); 6218 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype); 6219 6220 if (iftype == NL80211_IFTYPE_MONITOR) { 6221 int err; 6222 6223 flags = nlmsg_alloc(); 6224 if (!flags) 6225 goto nla_put_failure; 6226 6227 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES); 6228 6229 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags); 6230 6231 nlmsg_free(flags); 6232 6233 if (err) 6234 goto nla_put_failure; 6235 } else if (wds) { 6236 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds); 6237 } 6238 6239 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 6240 msg = NULL; 6241 if (ret) { 6242 nla_put_failure: 6243 nlmsg_free(msg); 6244 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)", 6245 ifname, ret, strerror(-ret)); 6246 return ret; 6247 } 6248 6249 ifidx = if_nametoindex(ifname); 6250 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d", 6251 ifname, ifidx); 6252 6253 if (ifidx <= 0) 6254 return -1; 6255 6256 /* start listening for EAPOL on this interface */ 6257 add_ifidx(drv, ifidx); 6258 6259 if (addr && iftype != NL80211_IFTYPE_MONITOR && 6260 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) { 6261 nl80211_remove_iface(drv, ifidx); 6262 return -1; 6263 } 6264 6265 return ifidx; 6266} 6267 6268 6269static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv, 6270 const char *ifname, enum nl80211_iftype iftype, 6271 const u8 *addr, int wds) 6272{ 6273 int ret; 6274 6275 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds); 6276 6277 /* if error occurred and interface exists already */ 6278 if (ret == -ENFILE && if_nametoindex(ifname)) { 6279 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname); 6280 6281 /* Try to remove the interface that was already there. */ 6282 nl80211_remove_iface(drv, if_nametoindex(ifname)); 6283 6284 /* Try to create the interface again */ 6285 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, 6286 wds); 6287 } 6288 6289 if (ret >= 0 && is_p2p_interface(iftype)) 6290 nl80211_disable_11b_rates(drv, ret, 1); 6291 6292 return ret; 6293} 6294 6295 6296static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok) 6297{ 6298 struct ieee80211_hdr *hdr; 6299 u16 fc; 6300 union wpa_event_data event; 6301 6302 hdr = (struct ieee80211_hdr *) buf; 6303 fc = le_to_host16(hdr->frame_control); 6304 6305 os_memset(&event, 0, sizeof(event)); 6306 event.tx_status.type = WLAN_FC_GET_TYPE(fc); 6307 event.tx_status.stype = WLAN_FC_GET_STYPE(fc); 6308 event.tx_status.dst = hdr->addr1; 6309 event.tx_status.data = buf; 6310 event.tx_status.data_len = len; 6311 event.tx_status.ack = ok; 6312 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event); 6313} 6314 6315 6316static void from_unknown_sta(struct wpa_driver_nl80211_data *drv, 6317 u8 *buf, size_t len) 6318{ 6319 struct ieee80211_hdr *hdr = (void *)buf; 6320 u16 fc; 6321 union wpa_event_data event; 6322 6323 if (len < sizeof(*hdr)) 6324 return; 6325 6326 fc = le_to_host16(hdr->frame_control); 6327 6328 os_memset(&event, 0, sizeof(event)); 6329 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len); 6330 event.rx_from_unknown.addr = hdr->addr2; 6331 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) == 6332 (WLAN_FC_FROMDS | WLAN_FC_TODS); 6333 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event); 6334} 6335 6336 6337static void handle_frame(struct wpa_driver_nl80211_data *drv, 6338 u8 *buf, size_t len, int datarate, int ssi_signal) 6339{ 6340 struct ieee80211_hdr *hdr; 6341 u16 fc; 6342 union wpa_event_data event; 6343 6344 hdr = (struct ieee80211_hdr *) buf; 6345 fc = le_to_host16(hdr->frame_control); 6346 6347 switch (WLAN_FC_GET_TYPE(fc)) { 6348 case WLAN_FC_TYPE_MGMT: 6349 os_memset(&event, 0, sizeof(event)); 6350 event.rx_mgmt.frame = buf; 6351 event.rx_mgmt.frame_len = len; 6352 event.rx_mgmt.datarate = datarate; 6353 event.rx_mgmt.ssi_signal = ssi_signal; 6354 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event); 6355 break; 6356 case WLAN_FC_TYPE_CTRL: 6357 /* can only get here with PS-Poll frames */ 6358 wpa_printf(MSG_DEBUG, "CTRL"); 6359 from_unknown_sta(drv, buf, len); 6360 break; 6361 case WLAN_FC_TYPE_DATA: 6362 from_unknown_sta(drv, buf, len); 6363 break; 6364 } 6365} 6366 6367 6368static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx) 6369{ 6370 struct wpa_driver_nl80211_data *drv = eloop_ctx; 6371 int len; 6372 unsigned char buf[3000]; 6373 struct ieee80211_radiotap_iterator iter; 6374 int ret; 6375 int datarate = 0, ssi_signal = 0; 6376 int injected = 0, failed = 0, rxflags = 0; 6377 6378 len = recv(sock, buf, sizeof(buf), 0); 6379 if (len < 0) { 6380 perror("recv"); 6381 return; 6382 } 6383 6384 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) { 6385 printf("received invalid radiotap frame\n"); 6386 return; 6387 } 6388 6389 while (1) { 6390 ret = ieee80211_radiotap_iterator_next(&iter); 6391 if (ret == -ENOENT) 6392 break; 6393 if (ret) { 6394 printf("received invalid radiotap frame (%d)\n", ret); 6395 return; 6396 } 6397 switch (iter.this_arg_index) { 6398 case IEEE80211_RADIOTAP_FLAGS: 6399 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS) 6400 len -= 4; 6401 break; 6402 case IEEE80211_RADIOTAP_RX_FLAGS: 6403 rxflags = 1; 6404 break; 6405 case IEEE80211_RADIOTAP_TX_FLAGS: 6406 injected = 1; 6407 failed = le_to_host16((*(uint16_t *) iter.this_arg)) & 6408 IEEE80211_RADIOTAP_F_TX_FAIL; 6409 break; 6410 case IEEE80211_RADIOTAP_DATA_RETRIES: 6411 break; 6412 case IEEE80211_RADIOTAP_CHANNEL: 6413 /* TODO: convert from freq/flags to channel number */ 6414 break; 6415 case IEEE80211_RADIOTAP_RATE: 6416 datarate = *iter.this_arg * 5; 6417 break; 6418 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL: 6419 ssi_signal = (s8) *iter.this_arg; 6420 break; 6421 } 6422 } 6423 6424 if (rxflags && injected) 6425 return; 6426 6427 if (!injected) 6428 handle_frame(drv, buf + iter.max_length, 6429 len - iter.max_length, datarate, ssi_signal); 6430 else 6431 handle_tx_callback(drv->ctx, buf + iter.max_length, 6432 len - iter.max_length, !failed); 6433} 6434 6435 6436/* 6437 * we post-process the filter code later and rewrite 6438 * this to the offset to the last instruction 6439 */ 6440#define PASS 0xFF 6441#define FAIL 0xFE 6442 6443static struct sock_filter msock_filter_insns[] = { 6444 /* 6445 * do a little-endian load of the radiotap length field 6446 */ 6447 /* load lower byte into A */ 6448 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2), 6449 /* put it into X (== index register) */ 6450 BPF_STMT(BPF_MISC| BPF_TAX, 0), 6451 /* load upper byte into A */ 6452 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3), 6453 /* left-shift it by 8 */ 6454 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8), 6455 /* or with X */ 6456 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0), 6457 /* put result into X */ 6458 BPF_STMT(BPF_MISC| BPF_TAX, 0), 6459 6460 /* 6461 * Allow management frames through, this also gives us those 6462 * management frames that we sent ourselves with status 6463 */ 6464 /* load the lower byte of the IEEE 802.11 frame control field */ 6465 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), 6466 /* mask off frame type and version */ 6467 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF), 6468 /* accept frame if it's both 0, fall through otherwise */ 6469 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0), 6470 6471 /* 6472 * TODO: add a bit to radiotap RX flags that indicates 6473 * that the sending station is not associated, then 6474 * add a filter here that filters on our DA and that flag 6475 * to allow us to deauth frames to that bad station. 6476 * 6477 * For now allow all To DS data frames through. 6478 */ 6479 /* load the IEEE 802.11 frame control field */ 6480 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0), 6481 /* mask off frame type, version and DS status */ 6482 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03), 6483 /* accept frame if version 0, type 2 and To DS, fall through otherwise 6484 */ 6485 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0), 6486 6487#if 0 6488 /* 6489 * drop non-data frames 6490 */ 6491 /* load the lower byte of the frame control field */ 6492 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), 6493 /* mask off QoS bit */ 6494 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c), 6495 /* drop non-data frames */ 6496 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL), 6497#endif 6498 /* load the upper byte of the frame control field */ 6499 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1), 6500 /* mask off toDS/fromDS */ 6501 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03), 6502 /* accept WDS frames */ 6503 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0), 6504 6505 /* 6506 * add header length to index 6507 */ 6508 /* load the lower byte of the frame control field */ 6509 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), 6510 /* mask off QoS bit */ 6511 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80), 6512 /* right shift it by 6 to give 0 or 2 */ 6513 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6), 6514 /* add data frame header length */ 6515 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24), 6516 /* add index, was start of 802.11 header */ 6517 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), 6518 /* move to index, now start of LL header */ 6519 BPF_STMT(BPF_MISC | BPF_TAX, 0), 6520 6521 /* 6522 * Accept empty data frames, we use those for 6523 * polling activity. 6524 */ 6525 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0), 6526 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0), 6527 6528 /* 6529 * Accept EAPOL frames 6530 */ 6531 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0), 6532 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL), 6533 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4), 6534 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL), 6535 6536 /* keep these last two statements or change the code below */ 6537 /* return 0 == "DROP" */ 6538 BPF_STMT(BPF_RET | BPF_K, 0), 6539 /* return ~0 == "keep all" */ 6540 BPF_STMT(BPF_RET | BPF_K, ~0), 6541}; 6542 6543static struct sock_fprog msock_filter = { 6544 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]), 6545 .filter = msock_filter_insns, 6546}; 6547 6548 6549static int add_monitor_filter(int s) 6550{ 6551 int idx; 6552 6553 /* rewrite all PASS/FAIL jump offsets */ 6554 for (idx = 0; idx < msock_filter.len; idx++) { 6555 struct sock_filter *insn = &msock_filter_insns[idx]; 6556 6557 if (BPF_CLASS(insn->code) == BPF_JMP) { 6558 if (insn->code == (BPF_JMP|BPF_JA)) { 6559 if (insn->k == PASS) 6560 insn->k = msock_filter.len - idx - 2; 6561 else if (insn->k == FAIL) 6562 insn->k = msock_filter.len - idx - 3; 6563 } 6564 6565 if (insn->jt == PASS) 6566 insn->jt = msock_filter.len - idx - 2; 6567 else if (insn->jt == FAIL) 6568 insn->jt = msock_filter.len - idx - 3; 6569 6570 if (insn->jf == PASS) 6571 insn->jf = msock_filter.len - idx - 2; 6572 else if (insn->jf == FAIL) 6573 insn->jf = msock_filter.len - idx - 3; 6574 } 6575 } 6576 6577 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, 6578 &msock_filter, sizeof(msock_filter))) { 6579 perror("SO_ATTACH_FILTER"); 6580 return -1; 6581 } 6582 6583 return 0; 6584} 6585 6586 6587static void nl80211_remove_monitor_interface( 6588 struct wpa_driver_nl80211_data *drv) 6589{ 6590 drv->monitor_refcount--; 6591 if (drv->monitor_refcount > 0) 6592 return; 6593 6594 if (drv->monitor_ifidx >= 0) { 6595 nl80211_remove_iface(drv, drv->monitor_ifidx); 6596 drv->monitor_ifidx = -1; 6597 } 6598 if (drv->monitor_sock >= 0) { 6599 eloop_unregister_read_sock(drv->monitor_sock); 6600 close(drv->monitor_sock); 6601 drv->monitor_sock = -1; 6602 } 6603} 6604 6605 6606static int 6607nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv) 6608{ 6609 char buf[IFNAMSIZ]; 6610 struct sockaddr_ll ll; 6611 int optval; 6612 socklen_t optlen; 6613 6614 if (drv->monitor_ifidx >= 0) { 6615 drv->monitor_refcount++; 6616 return 0; 6617 } 6618 6619 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) { 6620 /* 6621 * P2P interface name is of the format p2p-%s-%d. For monitor 6622 * interface name corresponding to P2P GO, replace "p2p-" with 6623 * "mon-" to retain the same interface name length and to 6624 * indicate that it is a monitor interface. 6625 */ 6626 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4); 6627 } else { 6628 /* Non-P2P interface with AP functionality. */ 6629 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname); 6630 } 6631 6632 buf[IFNAMSIZ - 1] = '\0'; 6633 6634 drv->monitor_ifidx = 6635 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL, 6636 0); 6637 6638 if (drv->monitor_ifidx == -EOPNOTSUPP) { 6639 /* 6640 * This is backward compatibility for a few versions of 6641 * the kernel only that didn't advertise the right 6642 * attributes for the only driver that then supported 6643 * AP mode w/o monitor -- ath6kl. 6644 */ 6645 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support " 6646 "monitor interface type - try to run without it"); 6647 drv->device_ap_sme = 1; 6648 } 6649 6650 if (drv->monitor_ifidx < 0) 6651 return -1; 6652 6653 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1)) 6654 goto error; 6655 6656 memset(&ll, 0, sizeof(ll)); 6657 ll.sll_family = AF_PACKET; 6658 ll.sll_ifindex = drv->monitor_ifidx; 6659 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); 6660 if (drv->monitor_sock < 0) { 6661 perror("socket[PF_PACKET,SOCK_RAW]"); 6662 goto error; 6663 } 6664 6665 if (add_monitor_filter(drv->monitor_sock)) { 6666 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor " 6667 "interface; do filtering in user space"); 6668 /* This works, but will cost in performance. */ 6669 } 6670 6671 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) { 6672 perror("monitor socket bind"); 6673 goto error; 6674 } 6675 6676 optlen = sizeof(optval); 6677 optval = 20; 6678 if (setsockopt 6679 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) { 6680 perror("Failed to set socket priority"); 6681 goto error; 6682 } 6683 6684 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read, 6685 drv, NULL)) { 6686 printf("Could not register monitor read socket\n"); 6687 goto error; 6688 } 6689 6690 return 0; 6691 error: 6692 nl80211_remove_monitor_interface(drv); 6693 return -1; 6694} 6695 6696 6697static int nl80211_setup_ap(struct i802_bss *bss) 6698{ 6699 struct wpa_driver_nl80211_data *drv = bss->drv; 6700 6701 wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d " 6702 "use_monitor=%d", drv->device_ap_sme, drv->use_monitor); 6703 6704 /* 6705 * Disable Probe Request reporting unless we need it in this way for 6706 * devices that include the AP SME, in the other case (unless using 6707 * monitor iface) we'll get it through the nl_mgmt socket instead. 6708 */ 6709 if (!drv->device_ap_sme) 6710 wpa_driver_nl80211_probe_req_report(bss, 0); 6711 6712 if (!drv->device_ap_sme && !drv->use_monitor) 6713 if (nl80211_mgmt_subscribe_ap(bss)) 6714 return -1; 6715 6716 if (drv->device_ap_sme && !drv->use_monitor) 6717 if (nl80211_mgmt_subscribe_ap_dev_sme(bss)) 6718 return -1; 6719 6720 if (!drv->device_ap_sme && drv->use_monitor && 6721 nl80211_create_monitor_interface(drv) && 6722 !drv->device_ap_sme) 6723 return -1; 6724 6725#ifdef ANDROID_P2P 6726 if (drv->device_ap_sme && drv->use_monitor) 6727 if (nl80211_mgmt_subscribe_ap_dev_sme(bss)) 6728 return -1; 6729 6730 if (drv->use_monitor && 6731 nl80211_create_monitor_interface(drv)) 6732 return -1; 6733#endif 6734 6735 if (drv->device_ap_sme && 6736 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) { 6737 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable " 6738 "Probe Request frame reporting in AP mode"); 6739 /* Try to survive without this */ 6740 } 6741 6742 return 0; 6743} 6744 6745 6746static void nl80211_teardown_ap(struct i802_bss *bss) 6747{ 6748 struct wpa_driver_nl80211_data *drv = bss->drv; 6749 6750 if (drv->device_ap_sme) { 6751 wpa_driver_nl80211_probe_req_report(bss, 0); 6752 if (!drv->use_monitor) 6753 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)"); 6754 } else if (drv->use_monitor) 6755 nl80211_remove_monitor_interface(drv); 6756 else 6757 nl80211_mgmt_unsubscribe(bss, "AP teardown"); 6758 6759 bss->beacon_set = 0; 6760} 6761 6762 6763static int nl80211_send_eapol_data(struct i802_bss *bss, 6764 const u8 *addr, const u8 *data, 6765 size_t data_len) 6766{ 6767 struct sockaddr_ll ll; 6768 int ret; 6769 6770 if (bss->drv->eapol_tx_sock < 0) { 6771 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL"); 6772 return -1; 6773 } 6774 6775 os_memset(&ll, 0, sizeof(ll)); 6776 ll.sll_family = AF_PACKET; 6777 ll.sll_ifindex = bss->ifindex; 6778 ll.sll_protocol = htons(ETH_P_PAE); 6779 ll.sll_halen = ETH_ALEN; 6780 os_memcpy(ll.sll_addr, addr, ETH_ALEN); 6781 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0, 6782 (struct sockaddr *) &ll, sizeof(ll)); 6783 if (ret < 0) 6784 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s", 6785 strerror(errno)); 6786 6787 return ret; 6788} 6789 6790 6791static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; 6792 6793static int wpa_driver_nl80211_hapd_send_eapol( 6794 void *priv, const u8 *addr, const u8 *data, 6795 size_t data_len, int encrypt, const u8 *own_addr, u32 flags) 6796{ 6797 struct i802_bss *bss = priv; 6798 struct wpa_driver_nl80211_data *drv = bss->drv; 6799 struct ieee80211_hdr *hdr; 6800 size_t len; 6801 u8 *pos; 6802 int res; 6803 int qos = flags & WPA_STA_WMM; 6804#ifndef ANDROID_P2P 6805 if (drv->device_ap_sme || !drv->use_monitor) 6806#else 6807 if (drv->device_ap_sme && !drv->use_monitor) 6808#endif 6809 return nl80211_send_eapol_data(bss, addr, data, data_len); 6810 6811 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 + 6812 data_len; 6813 hdr = os_zalloc(len); 6814 if (hdr == NULL) { 6815 printf("malloc() failed for i802_send_data(len=%lu)\n", 6816 (unsigned long) len); 6817 return -1; 6818 } 6819 6820 hdr->frame_control = 6821 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA); 6822 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS); 6823 if (encrypt) 6824 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP); 6825 if (qos) { 6826 hdr->frame_control |= 6827 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4); 6828 } 6829 6830 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN); 6831 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN); 6832 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN); 6833 pos = (u8 *) (hdr + 1); 6834 6835 if (qos) { 6836 /* Set highest priority in QoS header */ 6837 pos[0] = 7; 6838 pos[1] = 0; 6839 pos += 2; 6840 } 6841 6842 memcpy(pos, rfc1042_header, sizeof(rfc1042_header)); 6843 pos += sizeof(rfc1042_header); 6844 WPA_PUT_BE16(pos, ETH_P_PAE); 6845 pos += 2; 6846 memcpy(pos, data, data_len); 6847 6848 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0, 6849 0, 0, 0, 0); 6850 if (res < 0) { 6851 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - " 6852 "failed: %d (%s)", 6853 (unsigned long) len, errno, strerror(errno)); 6854 } 6855 os_free(hdr); 6856 6857 return res; 6858} 6859 6860 6861static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr, 6862 int total_flags, 6863 int flags_or, int flags_and) 6864{ 6865 struct i802_bss *bss = priv; 6866 struct wpa_driver_nl80211_data *drv = bss->drv; 6867 struct nl_msg *msg, *flags = NULL; 6868 struct nl80211_sta_flag_update upd; 6869 6870 msg = nlmsg_alloc(); 6871 if (!msg) 6872 return -ENOMEM; 6873 6874 flags = nlmsg_alloc(); 6875 if (!flags) { 6876 nlmsg_free(msg); 6877 return -ENOMEM; 6878 } 6879 6880 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION); 6881 6882 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, 6883 if_nametoindex(bss->ifname)); 6884 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 6885 6886 /* 6887 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This 6888 * can be removed eventually. 6889 */ 6890 if (total_flags & WPA_STA_AUTHORIZED) 6891 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED); 6892 6893 if (total_flags & WPA_STA_WMM) 6894 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME); 6895 6896 if (total_flags & WPA_STA_SHORT_PREAMBLE) 6897 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE); 6898 6899 if (total_flags & WPA_STA_MFP) 6900 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP); 6901 6902 if (total_flags & WPA_STA_TDLS_PEER) 6903 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER); 6904 6905 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags)) 6906 goto nla_put_failure; 6907 6908 os_memset(&upd, 0, sizeof(upd)); 6909 upd.mask = sta_flags_nl80211(flags_or | ~flags_and); 6910 upd.set = sta_flags_nl80211(flags_or); 6911 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); 6912 6913 nlmsg_free(flags); 6914 6915 return send_and_recv_msgs(drv, msg, NULL, NULL); 6916 nla_put_failure: 6917 nlmsg_free(msg); 6918 nlmsg_free(flags); 6919 return -ENOBUFS; 6920} 6921 6922 6923static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv, 6924 struct wpa_driver_associate_params *params) 6925{ 6926 enum nl80211_iftype nlmode, old_mode; 6927 struct hostapd_freq_params freq = { 6928 .freq = params->freq, 6929 }; 6930 6931 if (params->p2p) { 6932 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P " 6933 "group (GO)"); 6934 nlmode = NL80211_IFTYPE_P2P_GO; 6935 } else 6936 nlmode = NL80211_IFTYPE_AP; 6937 6938 old_mode = drv->nlmode; 6939 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode)) { 6940 nl80211_remove_monitor_interface(drv); 6941 return -1; 6942 } 6943 6944 if (wpa_driver_nl80211_set_freq(&drv->first_bss, &freq)) { 6945 if (old_mode != nlmode) 6946 wpa_driver_nl80211_set_mode(&drv->first_bss, old_mode); 6947 nl80211_remove_monitor_interface(drv); 6948 return -1; 6949 } 6950 6951 return 0; 6952} 6953 6954 6955static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv) 6956{ 6957 struct nl_msg *msg; 6958 int ret = -1; 6959 6960 msg = nlmsg_alloc(); 6961 if (!msg) 6962 return -1; 6963 6964 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS); 6965 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 6966 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 6967 msg = NULL; 6968 if (ret) { 6969 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d " 6970 "(%s)", ret, strerror(-ret)); 6971 goto nla_put_failure; 6972 } 6973 6974 ret = 0; 6975 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully"); 6976 6977nla_put_failure: 6978 nlmsg_free(msg); 6979 return ret; 6980} 6981 6982 6983static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv, 6984 struct wpa_driver_associate_params *params) 6985{ 6986 struct nl_msg *msg; 6987 int ret = -1; 6988 int count = 0; 6989 6990 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex); 6991 6992 if (wpa_driver_nl80211_set_mode(&drv->first_bss, 6993 NL80211_IFTYPE_ADHOC)) { 6994 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into " 6995 "IBSS mode"); 6996 return -1; 6997 } 6998 6999retry: 7000 msg = nlmsg_alloc(); 7001 if (!msg) 7002 return -1; 7003 7004 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS); 7005 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 7006 7007 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid)) 7008 goto nla_put_failure; 7009 7010 wpa_hexdump_ascii(MSG_DEBUG, " * SSID", 7011 params->ssid, params->ssid_len); 7012 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 7013 params->ssid); 7014 os_memcpy(drv->ssid, params->ssid, params->ssid_len); 7015 drv->ssid_len = params->ssid_len; 7016 7017 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); 7018 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); 7019 7020 ret = nl80211_set_conn_keys(params, msg); 7021 if (ret) 7022 goto nla_put_failure; 7023 7024 if (params->bssid && params->fixed_bssid) { 7025 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR, 7026 MAC2STR(params->bssid)); 7027 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); 7028 } 7029 7030 if (params->key_mgmt_suite == KEY_MGMT_802_1X || 7031 params->key_mgmt_suite == KEY_MGMT_PSK || 7032 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 || 7033 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) { 7034 wpa_printf(MSG_DEBUG, " * control port"); 7035 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT); 7036 } 7037 7038 if (params->wpa_ie) { 7039 wpa_hexdump(MSG_DEBUG, 7040 " * Extra IEs for Beacon/Probe Response frames", 7041 params->wpa_ie, params->wpa_ie_len); 7042 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len, 7043 params->wpa_ie); 7044 } 7045 7046 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 7047 msg = NULL; 7048 if (ret) { 7049 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)", 7050 ret, strerror(-ret)); 7051 count++; 7052 if (ret == -EALREADY && count == 1) { 7053 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after " 7054 "forced leave"); 7055 nl80211_leave_ibss(drv); 7056 nlmsg_free(msg); 7057 goto retry; 7058 } 7059 7060 goto nla_put_failure; 7061 } 7062 ret = 0; 7063 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully"); 7064 7065nla_put_failure: 7066 nlmsg_free(msg); 7067 return ret; 7068} 7069 7070 7071static int wpa_driver_nl80211_try_connect( 7072 struct wpa_driver_nl80211_data *drv, 7073 struct wpa_driver_associate_params *params) 7074{ 7075 struct nl_msg *msg; 7076 enum nl80211_auth_type type; 7077 int ret = 0; 7078 int algs; 7079 7080 msg = nlmsg_alloc(); 7081 if (!msg) 7082 return -1; 7083 7084 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex); 7085 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT); 7086 7087 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 7088 if (params->bssid) { 7089 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, 7090 MAC2STR(params->bssid)); 7091 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); 7092 } 7093 if (params->freq) { 7094 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); 7095 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); 7096 } 7097 if (params->bg_scan_period >= 0) { 7098 wpa_printf(MSG_DEBUG, " * bg scan period=%d", 7099 params->bg_scan_period); 7100 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD, 7101 params->bg_scan_period); 7102 } 7103 if (params->ssid) { 7104 wpa_hexdump_ascii(MSG_DEBUG, " * SSID", 7105 params->ssid, params->ssid_len); 7106 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 7107 params->ssid); 7108 if (params->ssid_len > sizeof(drv->ssid)) 7109 goto nla_put_failure; 7110 os_memcpy(drv->ssid, params->ssid, params->ssid_len); 7111 drv->ssid_len = params->ssid_len; 7112 } 7113 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len); 7114 if (params->wpa_ie) 7115 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len, 7116 params->wpa_ie); 7117 7118 algs = 0; 7119 if (params->auth_alg & WPA_AUTH_ALG_OPEN) 7120 algs++; 7121 if (params->auth_alg & WPA_AUTH_ALG_SHARED) 7122 algs++; 7123 if (params->auth_alg & WPA_AUTH_ALG_LEAP) 7124 algs++; 7125 if (algs > 1) { 7126 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic " 7127 "selection"); 7128 goto skip_auth_type; 7129 } 7130 7131 if (params->auth_alg & WPA_AUTH_ALG_OPEN) 7132 type = NL80211_AUTHTYPE_OPEN_SYSTEM; 7133 else if (params->auth_alg & WPA_AUTH_ALG_SHARED) 7134 type = NL80211_AUTHTYPE_SHARED_KEY; 7135 else if (params->auth_alg & WPA_AUTH_ALG_LEAP) 7136 type = NL80211_AUTHTYPE_NETWORK_EAP; 7137 else if (params->auth_alg & WPA_AUTH_ALG_FT) 7138 type = NL80211_AUTHTYPE_FT; 7139 else 7140 goto nla_put_failure; 7141 7142 wpa_printf(MSG_DEBUG, " * Auth Type %d", type); 7143 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type); 7144 7145skip_auth_type: 7146 if (params->wpa_proto) { 7147 enum nl80211_wpa_versions ver = 0; 7148 7149 if (params->wpa_proto & WPA_PROTO_WPA) 7150 ver |= NL80211_WPA_VERSION_1; 7151 if (params->wpa_proto & WPA_PROTO_RSN) 7152 ver |= NL80211_WPA_VERSION_2; 7153 7154 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver); 7155 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver); 7156 } 7157 7158 if (params->pairwise_suite != CIPHER_NONE) { 7159 int cipher; 7160 7161 switch (params->pairwise_suite) { 7162 case CIPHER_SMS4: 7163 cipher = WLAN_CIPHER_SUITE_SMS4; 7164 break; 7165 case CIPHER_WEP40: 7166 cipher = WLAN_CIPHER_SUITE_WEP40; 7167 break; 7168 case CIPHER_WEP104: 7169 cipher = WLAN_CIPHER_SUITE_WEP104; 7170 break; 7171 case CIPHER_CCMP: 7172 cipher = WLAN_CIPHER_SUITE_CCMP; 7173 break; 7174 case CIPHER_GCMP: 7175 cipher = WLAN_CIPHER_SUITE_GCMP; 7176 break; 7177 case CIPHER_TKIP: 7178 default: 7179 cipher = WLAN_CIPHER_SUITE_TKIP; 7180 break; 7181 } 7182 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher); 7183 } 7184 7185 if (params->group_suite != CIPHER_NONE) { 7186 int cipher; 7187 7188 switch (params->group_suite) { 7189 case CIPHER_SMS4: 7190 cipher = WLAN_CIPHER_SUITE_SMS4; 7191 break; 7192 case CIPHER_WEP40: 7193 cipher = WLAN_CIPHER_SUITE_WEP40; 7194 break; 7195 case CIPHER_WEP104: 7196 cipher = WLAN_CIPHER_SUITE_WEP104; 7197 break; 7198 case CIPHER_CCMP: 7199 cipher = WLAN_CIPHER_SUITE_CCMP; 7200 break; 7201 case CIPHER_GCMP: 7202 cipher = WLAN_CIPHER_SUITE_GCMP; 7203 break; 7204 case CIPHER_TKIP: 7205 default: 7206 cipher = WLAN_CIPHER_SUITE_TKIP; 7207 break; 7208 } 7209 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher); 7210 } 7211 7212 if (params->key_mgmt_suite == KEY_MGMT_802_1X || 7213 params->key_mgmt_suite == KEY_MGMT_PSK || 7214 params->key_mgmt_suite == KEY_MGMT_FT_802_1X || 7215 params->key_mgmt_suite == KEY_MGMT_FT_PSK || 7216 params->key_mgmt_suite == KEY_MGMT_CCKM) { 7217 int mgmt = WLAN_AKM_SUITE_PSK; 7218 7219 switch (params->key_mgmt_suite) { 7220 case KEY_MGMT_CCKM: 7221 mgmt = WLAN_AKM_SUITE_CCKM; 7222 break; 7223 case KEY_MGMT_802_1X: 7224 mgmt = WLAN_AKM_SUITE_8021X; 7225 break; 7226 case KEY_MGMT_FT_802_1X: 7227 mgmt = WLAN_AKM_SUITE_FT_8021X; 7228 break; 7229 case KEY_MGMT_FT_PSK: 7230 mgmt = WLAN_AKM_SUITE_FT_PSK; 7231 break; 7232 case KEY_MGMT_PSK: 7233 default: 7234 mgmt = WLAN_AKM_SUITE_PSK; 7235 break; 7236 } 7237 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt); 7238 } 7239 7240#ifdef CONFIG_IEEE80211W 7241 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED) 7242 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED); 7243#endif /* CONFIG_IEEE80211W */ 7244 7245 if (params->disable_ht) 7246 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT); 7247 7248 if (params->htcaps && params->htcaps_mask) { 7249 int sz = sizeof(struct ieee80211_ht_capabilities); 7250 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps); 7251 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz, 7252 params->htcaps_mask); 7253 } 7254 7255#ifdef CONFIG_VHT_OVERRIDES 7256 if (params->disable_vht) { 7257 wpa_printf(MSG_DEBUG, " * VHT disabled"); 7258 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT); 7259 } 7260 7261 if (params->vhtcaps && params->vhtcaps_mask) { 7262 int sz = sizeof(struct ieee80211_vht_capabilities); 7263 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps); 7264 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz, 7265 params->vhtcaps_mask); 7266 } 7267#endif /* CONFIG_VHT_OVERRIDES */ 7268 7269 ret = nl80211_set_conn_keys(params, msg); 7270 if (ret) 7271 goto nla_put_failure; 7272 7273 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 7274 msg = NULL; 7275 if (ret) { 7276 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d " 7277 "(%s)", ret, strerror(-ret)); 7278 goto nla_put_failure; 7279 } 7280 ret = 0; 7281 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully"); 7282 7283nla_put_failure: 7284 nlmsg_free(msg); 7285 return ret; 7286 7287} 7288 7289 7290static int wpa_driver_nl80211_connect( 7291 struct wpa_driver_nl80211_data *drv, 7292 struct wpa_driver_associate_params *params) 7293{ 7294 int ret = wpa_driver_nl80211_try_connect(drv, params); 7295 if (ret == -EALREADY) { 7296 /* 7297 * cfg80211 does not currently accept new connections if 7298 * we are already connected. As a workaround, force 7299 * disconnection and try again. 7300 */ 7301 wpa_printf(MSG_DEBUG, "nl80211: Explicitly " 7302 "disconnecting before reassociation " 7303 "attempt"); 7304 if (wpa_driver_nl80211_disconnect( 7305 drv, WLAN_REASON_PREV_AUTH_NOT_VALID)) 7306 return -1; 7307 /* Ignore the next local disconnect message. */ 7308 drv->ignore_next_local_disconnect = 1; 7309 ret = wpa_driver_nl80211_try_connect(drv, params); 7310 } 7311 return ret; 7312} 7313 7314 7315static int wpa_driver_nl80211_associate( 7316 void *priv, struct wpa_driver_associate_params *params) 7317{ 7318 struct i802_bss *bss = priv; 7319 struct wpa_driver_nl80211_data *drv = bss->drv; 7320 int ret = -1; 7321 struct nl_msg *msg; 7322 7323 if (params->mode == IEEE80211_MODE_AP) 7324 return wpa_driver_nl80211_ap(drv, params); 7325 7326 if (params->mode == IEEE80211_MODE_IBSS) 7327 return wpa_driver_nl80211_ibss(drv, params); 7328 7329 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) { 7330 enum nl80211_iftype nlmode = params->p2p ? 7331 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION; 7332 7333 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0) 7334 return -1; 7335 return wpa_driver_nl80211_connect(drv, params); 7336 } 7337 7338 drv->associated = 0; 7339 7340 msg = nlmsg_alloc(); 7341 if (!msg) 7342 return -1; 7343 7344 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)", 7345 drv->ifindex); 7346 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE); 7347 7348 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 7349 if (params->bssid) { 7350 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, 7351 MAC2STR(params->bssid)); 7352 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); 7353 } 7354 if (params->freq) { 7355 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); 7356 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); 7357 drv->assoc_freq = params->freq; 7358 } else 7359 drv->assoc_freq = 0; 7360 if (params->bg_scan_period >= 0) { 7361 wpa_printf(MSG_DEBUG, " * bg scan period=%d", 7362 params->bg_scan_period); 7363 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD, 7364 params->bg_scan_period); 7365 } 7366 if (params->ssid) { 7367 wpa_hexdump_ascii(MSG_DEBUG, " * SSID", 7368 params->ssid, params->ssid_len); 7369 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 7370 params->ssid); 7371 if (params->ssid_len > sizeof(drv->ssid)) 7372 goto nla_put_failure; 7373 os_memcpy(drv->ssid, params->ssid, params->ssid_len); 7374 drv->ssid_len = params->ssid_len; 7375 } 7376 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len); 7377 if (params->wpa_ie) 7378 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len, 7379 params->wpa_ie); 7380 7381 if (params->pairwise_suite != CIPHER_NONE) { 7382 int cipher; 7383 7384 switch (params->pairwise_suite) { 7385 case CIPHER_WEP40: 7386 cipher = WLAN_CIPHER_SUITE_WEP40; 7387 break; 7388 case CIPHER_WEP104: 7389 cipher = WLAN_CIPHER_SUITE_WEP104; 7390 break; 7391 case CIPHER_CCMP: 7392 cipher = WLAN_CIPHER_SUITE_CCMP; 7393 break; 7394 case CIPHER_GCMP: 7395 cipher = WLAN_CIPHER_SUITE_GCMP; 7396 break; 7397 case CIPHER_TKIP: 7398 default: 7399 cipher = WLAN_CIPHER_SUITE_TKIP; 7400 break; 7401 } 7402 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher); 7403 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher); 7404 } 7405 7406 if (params->group_suite != CIPHER_NONE) { 7407 int cipher; 7408 7409 switch (params->group_suite) { 7410 case CIPHER_WEP40: 7411 cipher = WLAN_CIPHER_SUITE_WEP40; 7412 break; 7413 case CIPHER_WEP104: 7414 cipher = WLAN_CIPHER_SUITE_WEP104; 7415 break; 7416 case CIPHER_CCMP: 7417 cipher = WLAN_CIPHER_SUITE_CCMP; 7418 break; 7419 case CIPHER_GCMP: 7420 cipher = WLAN_CIPHER_SUITE_GCMP; 7421 break; 7422 case CIPHER_TKIP: 7423 default: 7424 cipher = WLAN_CIPHER_SUITE_TKIP; 7425 break; 7426 } 7427 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher); 7428 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher); 7429 } 7430 7431#ifdef CONFIG_IEEE80211W 7432 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED) 7433 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED); 7434#endif /* CONFIG_IEEE80211W */ 7435 7436 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT); 7437 7438 if (params->prev_bssid) { 7439 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR, 7440 MAC2STR(params->prev_bssid)); 7441 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN, 7442 params->prev_bssid); 7443 } 7444 7445 if (params->disable_ht) 7446 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT); 7447 7448 if (params->htcaps && params->htcaps_mask) { 7449 int sz = sizeof(struct ieee80211_ht_capabilities); 7450 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps); 7451 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz, 7452 params->htcaps_mask); 7453 } 7454 7455#ifdef CONFIG_VHT_OVERRIDES 7456 if (params->disable_vht) { 7457 wpa_printf(MSG_DEBUG, " * VHT disabled"); 7458 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT); 7459 } 7460 7461 if (params->vhtcaps && params->vhtcaps_mask) { 7462 int sz = sizeof(struct ieee80211_vht_capabilities); 7463 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps); 7464 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz, 7465 params->vhtcaps_mask); 7466 } 7467#endif /* CONFIG_VHT_OVERRIDES */ 7468 7469 if (params->p2p) 7470 wpa_printf(MSG_DEBUG, " * P2P group"); 7471 7472 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 7473 msg = NULL; 7474 if (ret) { 7475 wpa_dbg(drv->ctx, MSG_DEBUG, 7476 "nl80211: MLME command failed (assoc): ret=%d (%s)", 7477 ret, strerror(-ret)); 7478 nl80211_dump_scan(drv); 7479 goto nla_put_failure; 7480 } 7481 ret = 0; 7482 wpa_printf(MSG_DEBUG, "nl80211: Association request send " 7483 "successfully"); 7484 7485nla_put_failure: 7486 nlmsg_free(msg); 7487 return ret; 7488} 7489 7490 7491static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv, 7492 int ifindex, enum nl80211_iftype mode) 7493{ 7494 struct nl_msg *msg; 7495 int ret = -ENOBUFS; 7496 7497 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)", 7498 ifindex, mode, nl80211_iftype_str(mode)); 7499 7500 msg = nlmsg_alloc(); 7501 if (!msg) 7502 return -ENOMEM; 7503 7504 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE); 7505 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 7506 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode); 7507 7508 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 7509 msg = NULL; 7510 if (!ret) 7511 return 0; 7512nla_put_failure: 7513 nlmsg_free(msg); 7514 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:" 7515 " %d (%s)", ifindex, mode, ret, strerror(-ret)); 7516 return ret; 7517} 7518 7519 7520static int wpa_driver_nl80211_set_mode(struct i802_bss *bss, 7521 enum nl80211_iftype nlmode) 7522{ 7523 struct wpa_driver_nl80211_data *drv = bss->drv; 7524 int ret = -1; 7525 int i; 7526 int was_ap = is_ap_interface(drv->nlmode); 7527 int res; 7528 7529 res = nl80211_set_mode(drv, drv->ifindex, nlmode); 7530 if (res == 0) { 7531 drv->nlmode = nlmode; 7532 ret = 0; 7533 goto done; 7534 } 7535 7536 if (res == -ENODEV) 7537 return -1; 7538 7539 if (nlmode == drv->nlmode) { 7540 wpa_printf(MSG_DEBUG, "nl80211: Interface already in " 7541 "requested mode - ignore error"); 7542 ret = 0; 7543 goto done; /* Already in the requested mode */ 7544 } 7545 7546 /* mac80211 doesn't allow mode changes while the device is up, so 7547 * take the device down, try to set the mode again, and bring the 7548 * device back up. 7549 */ 7550 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting " 7551 "interface down"); 7552 for (i = 0; i < 10; i++) { 7553 res = linux_set_iface_flags(drv->global->ioctl_sock, 7554 bss->ifname, 0); 7555 if (res == -EACCES || res == -ENODEV) 7556 break; 7557 if (res == 0) { 7558 /* Try to set the mode again while the interface is 7559 * down */ 7560 ret = nl80211_set_mode(drv, drv->ifindex, nlmode); 7561 if (ret == -EACCES) 7562 break; 7563 res = linux_set_iface_flags(drv->global->ioctl_sock, 7564 bss->ifname, 1); 7565 if (res && !ret) 7566 ret = -1; 7567 else if (ret != -EBUSY) 7568 break; 7569 } else 7570 wpa_printf(MSG_DEBUG, "nl80211: Failed to set " 7571 "interface down"); 7572 os_sleep(0, 100000); 7573 } 7574 7575 if (!ret) { 7576 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while " 7577 "interface is down"); 7578 drv->nlmode = nlmode; 7579 drv->ignore_if_down_event = 1; 7580 } 7581 7582done: 7583 if (ret) { 7584 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d " 7585 "from %d failed", nlmode, drv->nlmode); 7586 return ret; 7587 } 7588 7589 if (is_p2p_interface(nlmode)) 7590 nl80211_disable_11b_rates(drv, drv->ifindex, 1); 7591 else if (drv->disabled_11b_rates) 7592 nl80211_disable_11b_rates(drv, drv->ifindex, 0); 7593 7594 if (is_ap_interface(nlmode)) { 7595 nl80211_mgmt_unsubscribe(bss, "start AP"); 7596 /* Setup additional AP mode functionality if needed */ 7597 if (nl80211_setup_ap(bss)) 7598 return -1; 7599 } else if (was_ap) { 7600 /* Remove additional AP mode functionality */ 7601 nl80211_teardown_ap(bss); 7602 } else { 7603 nl80211_mgmt_unsubscribe(bss, "mode change"); 7604 } 7605 7606 if (!bss->in_deinit && !is_ap_interface(nlmode) && 7607 nl80211_mgmt_subscribe_non_ap(bss) < 0) 7608 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action " 7609 "frame processing - ignore for now"); 7610 7611 return 0; 7612} 7613 7614 7615static int wpa_driver_nl80211_get_capa(void *priv, 7616 struct wpa_driver_capa *capa) 7617{ 7618 struct i802_bss *bss = priv; 7619 struct wpa_driver_nl80211_data *drv = bss->drv; 7620 if (!drv->has_capability) 7621 return -1; 7622 os_memcpy(capa, &drv->capa, sizeof(*capa)); 7623 return 0; 7624} 7625 7626 7627static int wpa_driver_nl80211_set_operstate(void *priv, int state) 7628{ 7629 struct i802_bss *bss = priv; 7630 struct wpa_driver_nl80211_data *drv = bss->drv; 7631 7632 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)", 7633 __func__, drv->operstate, state, state ? "UP" : "DORMANT"); 7634 drv->operstate = state; 7635 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1, 7636 state ? IF_OPER_UP : IF_OPER_DORMANT); 7637} 7638 7639 7640static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized) 7641{ 7642 struct i802_bss *bss = priv; 7643 struct wpa_driver_nl80211_data *drv = bss->drv; 7644 struct nl_msg *msg; 7645 struct nl80211_sta_flag_update upd; 7646 7647 msg = nlmsg_alloc(); 7648 if (!msg) 7649 return -ENOMEM; 7650 7651 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION); 7652 7653 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, 7654 if_nametoindex(bss->ifname)); 7655 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid); 7656 7657 os_memset(&upd, 0, sizeof(upd)); 7658 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED); 7659 if (authorized) 7660 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED); 7661 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); 7662 7663 return send_and_recv_msgs(drv, msg, NULL, NULL); 7664 nla_put_failure: 7665 nlmsg_free(msg); 7666 return -ENOBUFS; 7667} 7668 7669 7670/* Set kernel driver on given frequency (MHz) */ 7671static int i802_set_freq(void *priv, struct hostapd_freq_params *freq) 7672{ 7673 struct i802_bss *bss = priv; 7674 return wpa_driver_nl80211_set_freq(bss, freq); 7675} 7676 7677 7678#if defined(HOSTAPD) || defined(CONFIG_AP) 7679 7680static inline int min_int(int a, int b) 7681{ 7682 if (a < b) 7683 return a; 7684 return b; 7685} 7686 7687 7688static int get_key_handler(struct nl_msg *msg, void *arg) 7689{ 7690 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 7691 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 7692 7693 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 7694 genlmsg_attrlen(gnlh, 0), NULL); 7695 7696 /* 7697 * TODO: validate the key index and mac address! 7698 * Otherwise, there's a race condition as soon as 7699 * the kernel starts sending key notifications. 7700 */ 7701 7702 if (tb[NL80211_ATTR_KEY_SEQ]) 7703 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]), 7704 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6)); 7705 return NL_SKIP; 7706} 7707 7708 7709static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr, 7710 int idx, u8 *seq) 7711{ 7712 struct i802_bss *bss = priv; 7713 struct wpa_driver_nl80211_data *drv = bss->drv; 7714 struct nl_msg *msg; 7715 7716 msg = nlmsg_alloc(); 7717 if (!msg) 7718 return -ENOMEM; 7719 7720 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY); 7721 7722 if (addr) 7723 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 7724 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx); 7725 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface)); 7726 7727 memset(seq, 0, 6); 7728 7729 return send_and_recv_msgs(drv, msg, get_key_handler, seq); 7730 nla_put_failure: 7731 nlmsg_free(msg); 7732 return -ENOBUFS; 7733} 7734 7735 7736static int i802_set_rts(void *priv, int rts) 7737{ 7738 struct i802_bss *bss = priv; 7739 struct wpa_driver_nl80211_data *drv = bss->drv; 7740 struct nl_msg *msg; 7741 int ret = -ENOBUFS; 7742 u32 val; 7743 7744 msg = nlmsg_alloc(); 7745 if (!msg) 7746 return -ENOMEM; 7747 7748 if (rts >= 2347) 7749 val = (u32) -1; 7750 else 7751 val = rts; 7752 7753 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY); 7754 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 7755 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val); 7756 7757 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 7758 msg = NULL; 7759 if (!ret) 7760 return 0; 7761nla_put_failure: 7762 nlmsg_free(msg); 7763 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: " 7764 "%d (%s)", rts, ret, strerror(-ret)); 7765 return ret; 7766} 7767 7768 7769static int i802_set_frag(void *priv, int frag) 7770{ 7771 struct i802_bss *bss = priv; 7772 struct wpa_driver_nl80211_data *drv = bss->drv; 7773 struct nl_msg *msg; 7774 int ret = -ENOBUFS; 7775 u32 val; 7776 7777 msg = nlmsg_alloc(); 7778 if (!msg) 7779 return -ENOMEM; 7780 7781 if (frag >= 2346) 7782 val = (u32) -1; 7783 else 7784 val = frag; 7785 7786 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY); 7787 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 7788 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val); 7789 7790 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 7791 msg = NULL; 7792 if (!ret) 7793 return 0; 7794nla_put_failure: 7795 nlmsg_free(msg); 7796 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold " 7797 "%d: %d (%s)", frag, ret, strerror(-ret)); 7798 return ret; 7799} 7800 7801 7802static int i802_flush(void *priv) 7803{ 7804 struct i802_bss *bss = priv; 7805 struct wpa_driver_nl80211_data *drv = bss->drv; 7806 struct nl_msg *msg; 7807 int res; 7808 7809 msg = nlmsg_alloc(); 7810 if (!msg) 7811 return -1; 7812 7813 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION); 7814 7815 /* 7816 * XXX: FIX! this needs to flush all VLANs too 7817 */ 7818 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, 7819 if_nametoindex(bss->ifname)); 7820 7821 res = send_and_recv_msgs(drv, msg, NULL, NULL); 7822 if (res) { 7823 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d " 7824 "(%s)", res, strerror(-res)); 7825 } 7826 return res; 7827 nla_put_failure: 7828 nlmsg_free(msg); 7829 return -ENOBUFS; 7830} 7831 7832#endif /* HOSTAPD || CONFIG_AP */ 7833 7834 7835static int get_sta_handler(struct nl_msg *msg, void *arg) 7836{ 7837 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 7838 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 7839 struct hostap_sta_driver_data *data = arg; 7840 struct nlattr *stats[NL80211_STA_INFO_MAX + 1]; 7841 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { 7842 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, 7843 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, 7844 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, 7845 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, 7846 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, 7847 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 }, 7848 }; 7849 7850 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 7851 genlmsg_attrlen(gnlh, 0), NULL); 7852 7853 /* 7854 * TODO: validate the interface and mac address! 7855 * Otherwise, there's a race condition as soon as 7856 * the kernel starts sending station notifications. 7857 */ 7858 7859 if (!tb[NL80211_ATTR_STA_INFO]) { 7860 wpa_printf(MSG_DEBUG, "sta stats missing!"); 7861 return NL_SKIP; 7862 } 7863 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX, 7864 tb[NL80211_ATTR_STA_INFO], 7865 stats_policy)) { 7866 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!"); 7867 return NL_SKIP; 7868 } 7869 7870 if (stats[NL80211_STA_INFO_INACTIVE_TIME]) 7871 data->inactive_msec = 7872 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]); 7873 if (stats[NL80211_STA_INFO_RX_BYTES]) 7874 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]); 7875 if (stats[NL80211_STA_INFO_TX_BYTES]) 7876 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]); 7877 if (stats[NL80211_STA_INFO_RX_PACKETS]) 7878 data->rx_packets = 7879 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]); 7880 if (stats[NL80211_STA_INFO_TX_PACKETS]) 7881 data->tx_packets = 7882 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]); 7883 if (stats[NL80211_STA_INFO_TX_FAILED]) 7884 data->tx_retry_failed = 7885 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]); 7886 7887 return NL_SKIP; 7888} 7889 7890static int i802_read_sta_data(struct i802_bss *bss, 7891 struct hostap_sta_driver_data *data, 7892 const u8 *addr) 7893{ 7894 struct wpa_driver_nl80211_data *drv = bss->drv; 7895 struct nl_msg *msg; 7896 7897 os_memset(data, 0, sizeof(*data)); 7898 msg = nlmsg_alloc(); 7899 if (!msg) 7900 return -ENOMEM; 7901 7902 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION); 7903 7904 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 7905 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 7906 7907 return send_and_recv_msgs(drv, msg, get_sta_handler, data); 7908 nla_put_failure: 7909 nlmsg_free(msg); 7910 return -ENOBUFS; 7911} 7912 7913 7914#if defined(HOSTAPD) || defined(CONFIG_AP) 7915 7916static int i802_set_tx_queue_params(void *priv, int queue, int aifs, 7917 int cw_min, int cw_max, int burst_time) 7918{ 7919 struct i802_bss *bss = priv; 7920 struct wpa_driver_nl80211_data *drv = bss->drv; 7921 struct nl_msg *msg; 7922 struct nlattr *txq, *params; 7923 7924 msg = nlmsg_alloc(); 7925 if (!msg) 7926 return -1; 7927 7928 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY); 7929 7930 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 7931 7932 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS); 7933 if (!txq) 7934 goto nla_put_failure; 7935 7936 /* We are only sending parameters for a single TXQ at a time */ 7937 params = nla_nest_start(msg, 1); 7938 if (!params) 7939 goto nla_put_failure; 7940 7941 switch (queue) { 7942 case 0: 7943 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO); 7944 break; 7945 case 1: 7946 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI); 7947 break; 7948 case 2: 7949 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE); 7950 break; 7951 case 3: 7952 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK); 7953 break; 7954 } 7955 /* Burst time is configured in units of 0.1 msec and TXOP parameter in 7956 * 32 usec, so need to convert the value here. */ 7957 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32); 7958 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min); 7959 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max); 7960 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs); 7961 7962 nla_nest_end(msg, params); 7963 7964 nla_nest_end(msg, txq); 7965 7966 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0) 7967 return 0; 7968 msg = NULL; 7969 nla_put_failure: 7970 nlmsg_free(msg); 7971 return -1; 7972} 7973 7974 7975static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr, 7976 const char *ifname, int vlan_id) 7977{ 7978 struct wpa_driver_nl80211_data *drv = bss->drv; 7979 struct nl_msg *msg; 7980 int ret = -ENOBUFS; 7981 7982 msg = nlmsg_alloc(); 7983 if (!msg) 7984 return -ENOMEM; 7985 7986 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION); 7987 7988 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, 7989 if_nametoindex(bss->ifname)); 7990 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 7991 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN, 7992 if_nametoindex(ifname)); 7993 7994 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 7995 msg = NULL; 7996 if (ret < 0) { 7997 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr=" 7998 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)", 7999 MAC2STR(addr), ifname, vlan_id, ret, 8000 strerror(-ret)); 8001 } 8002 nla_put_failure: 8003 nlmsg_free(msg); 8004 return ret; 8005} 8006 8007 8008static int i802_get_inact_sec(void *priv, const u8 *addr) 8009{ 8010 struct hostap_sta_driver_data data; 8011 int ret; 8012 8013 data.inactive_msec = (unsigned long) -1; 8014 ret = i802_read_sta_data(priv, &data, addr); 8015 if (ret || data.inactive_msec == (unsigned long) -1) 8016 return -1; 8017 return data.inactive_msec / 1000; 8018} 8019 8020 8021static int i802_sta_clear_stats(void *priv, const u8 *addr) 8022{ 8023#if 0 8024 /* TODO */ 8025#endif 8026 return 0; 8027} 8028 8029 8030static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, 8031 int reason) 8032{ 8033 struct i802_bss *bss = priv; 8034 struct wpa_driver_nl80211_data *drv = bss->drv; 8035 struct ieee80211_mgmt mgmt; 8036 8037 if (drv->device_ap_sme) 8038 return wpa_driver_nl80211_sta_remove(bss, addr); 8039 8040 memset(&mgmt, 0, sizeof(mgmt)); 8041 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, 8042 WLAN_FC_STYPE_DEAUTH); 8043 memcpy(mgmt.da, addr, ETH_ALEN); 8044 memcpy(mgmt.sa, own_addr, ETH_ALEN); 8045 memcpy(mgmt.bssid, own_addr, ETH_ALEN); 8046 mgmt.u.deauth.reason_code = host_to_le16(reason); 8047 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt, 8048 IEEE80211_HDRLEN + 8049 sizeof(mgmt.u.deauth), 0, 0, 0, 0, 8050 0); 8051} 8052 8053 8054static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr, 8055 int reason) 8056{ 8057 struct i802_bss *bss = priv; 8058 struct wpa_driver_nl80211_data *drv = bss->drv; 8059 struct ieee80211_mgmt mgmt; 8060 8061 if (drv->device_ap_sme) 8062 return wpa_driver_nl80211_sta_remove(bss, addr); 8063 8064 memset(&mgmt, 0, sizeof(mgmt)); 8065 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, 8066 WLAN_FC_STYPE_DISASSOC); 8067 memcpy(mgmt.da, addr, ETH_ALEN); 8068 memcpy(mgmt.sa, own_addr, ETH_ALEN); 8069 memcpy(mgmt.bssid, own_addr, ETH_ALEN); 8070 mgmt.u.disassoc.reason_code = host_to_le16(reason); 8071 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt, 8072 IEEE80211_HDRLEN + 8073 sizeof(mgmt.u.disassoc), 0, 0, 0, 0, 8074 0); 8075} 8076 8077#endif /* HOSTAPD || CONFIG_AP */ 8078 8079#ifdef HOSTAPD 8080 8081static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 8082{ 8083 int i; 8084 int *old; 8085 8086 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d", 8087 ifidx); 8088 for (i = 0; i < drv->num_if_indices; i++) { 8089 if (drv->if_indices[i] == 0) { 8090 drv->if_indices[i] = ifidx; 8091 return; 8092 } 8093 } 8094 8095 if (drv->if_indices != drv->default_if_indices) 8096 old = drv->if_indices; 8097 else 8098 old = NULL; 8099 8100 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1, 8101 sizeof(int)); 8102 if (!drv->if_indices) { 8103 if (!old) 8104 drv->if_indices = drv->default_if_indices; 8105 else 8106 drv->if_indices = old; 8107 wpa_printf(MSG_ERROR, "Failed to reallocate memory for " 8108 "interfaces"); 8109 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx); 8110 return; 8111 } else if (!old) 8112 os_memcpy(drv->if_indices, drv->default_if_indices, 8113 sizeof(drv->default_if_indices)); 8114 drv->if_indices[drv->num_if_indices] = ifidx; 8115 drv->num_if_indices++; 8116} 8117 8118 8119static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 8120{ 8121 int i; 8122 8123 for (i = 0; i < drv->num_if_indices; i++) { 8124 if (drv->if_indices[i] == ifidx) { 8125 drv->if_indices[i] = 0; 8126 break; 8127 } 8128 } 8129} 8130 8131 8132static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 8133{ 8134 int i; 8135 8136 for (i = 0; i < drv->num_if_indices; i++) 8137 if (drv->if_indices[i] == ifidx) 8138 return 1; 8139 8140 return 0; 8141} 8142 8143 8144static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val, 8145 const char *bridge_ifname) 8146{ 8147 struct i802_bss *bss = priv; 8148 struct wpa_driver_nl80211_data *drv = bss->drv; 8149 char name[IFNAMSIZ + 1]; 8150 8151 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid); 8152 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR 8153 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name); 8154 if (val) { 8155 if (!if_nametoindex(name)) { 8156 if (nl80211_create_iface(drv, name, 8157 NL80211_IFTYPE_AP_VLAN, 8158 bss->addr, 1) < 0) 8159 return -1; 8160 if (bridge_ifname && 8161 linux_br_add_if(drv->global->ioctl_sock, 8162 bridge_ifname, name) < 0) 8163 return -1; 8164 } 8165 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) { 8166 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA " 8167 "interface %s up", name); 8168 } 8169 return i802_set_sta_vlan(priv, addr, name, 0); 8170 } else { 8171 if (bridge_ifname) 8172 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname, 8173 name); 8174 8175 i802_set_sta_vlan(priv, addr, bss->ifname, 0); 8176 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN, 8177 name); 8178 } 8179} 8180 8181 8182static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx) 8183{ 8184 struct wpa_driver_nl80211_data *drv = eloop_ctx; 8185 struct sockaddr_ll lladdr; 8186 unsigned char buf[3000]; 8187 int len; 8188 socklen_t fromlen = sizeof(lladdr); 8189 8190 len = recvfrom(sock, buf, sizeof(buf), 0, 8191 (struct sockaddr *)&lladdr, &fromlen); 8192 if (len < 0) { 8193 perror("recv"); 8194 return; 8195 } 8196 8197 if (have_ifidx(drv, lladdr.sll_ifindex)) 8198 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len); 8199} 8200 8201 8202static int i802_check_bridge(struct wpa_driver_nl80211_data *drv, 8203 struct i802_bss *bss, 8204 const char *brname, const char *ifname) 8205{ 8206 int ifindex; 8207 char in_br[IFNAMSIZ]; 8208 8209 os_strlcpy(bss->brname, brname, IFNAMSIZ); 8210 ifindex = if_nametoindex(brname); 8211 if (ifindex == 0) { 8212 /* 8213 * Bridge was configured, but the bridge device does 8214 * not exist. Try to add it now. 8215 */ 8216 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) { 8217 wpa_printf(MSG_ERROR, "nl80211: Failed to add the " 8218 "bridge interface %s: %s", 8219 brname, strerror(errno)); 8220 return -1; 8221 } 8222 bss->added_bridge = 1; 8223 add_ifidx(drv, if_nametoindex(brname)); 8224 } 8225 8226 if (linux_br_get(in_br, ifname) == 0) { 8227 if (os_strcmp(in_br, brname) == 0) 8228 return 0; /* already in the bridge */ 8229 8230 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from " 8231 "bridge %s", ifname, in_br); 8232 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) < 8233 0) { 8234 wpa_printf(MSG_ERROR, "nl80211: Failed to " 8235 "remove interface %s from bridge " 8236 "%s: %s", 8237 ifname, brname, strerror(errno)); 8238 return -1; 8239 } 8240 } 8241 8242 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s", 8243 ifname, brname); 8244 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) { 8245 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s " 8246 "into bridge %s: %s", 8247 ifname, brname, strerror(errno)); 8248 return -1; 8249 } 8250 bss->added_if_into_bridge = 1; 8251 8252 return 0; 8253} 8254 8255 8256static void *i802_init(struct hostapd_data *hapd, 8257 struct wpa_init_params *params) 8258{ 8259 struct wpa_driver_nl80211_data *drv; 8260 struct i802_bss *bss; 8261 size_t i; 8262 char brname[IFNAMSIZ]; 8263 int ifindex, br_ifindex; 8264 int br_added = 0; 8265 8266 bss = wpa_driver_nl80211_init(hapd, params->ifname, 8267 params->global_priv); 8268 if (bss == NULL) 8269 return NULL; 8270 8271 drv = bss->drv; 8272 drv->nlmode = NL80211_IFTYPE_AP; 8273 drv->eapol_sock = -1; 8274 8275 if (linux_br_get(brname, params->ifname) == 0) { 8276 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s", 8277 params->ifname, brname); 8278 br_ifindex = if_nametoindex(brname); 8279 } else { 8280 brname[0] = '\0'; 8281 br_ifindex = 0; 8282 } 8283 8284 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int); 8285 drv->if_indices = drv->default_if_indices; 8286 for (i = 0; i < params->num_bridge; i++) { 8287 if (params->bridge[i]) { 8288 ifindex = if_nametoindex(params->bridge[i]); 8289 if (ifindex) 8290 add_ifidx(drv, ifindex); 8291 if (ifindex == br_ifindex) 8292 br_added = 1; 8293 } 8294 } 8295 if (!br_added && br_ifindex && 8296 (params->num_bridge == 0 || !params->bridge[0])) 8297 add_ifidx(drv, br_ifindex); 8298 8299 /* start listening for EAPOL on the default AP interface */ 8300 add_ifidx(drv, drv->ifindex); 8301 8302 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0)) 8303 goto failed; 8304 8305 if (params->bssid) { 8306 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 8307 params->bssid)) 8308 goto failed; 8309 } 8310 8311 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) { 8312 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s " 8313 "into AP mode", bss->ifname); 8314 goto failed; 8315 } 8316 8317 if (params->num_bridge && params->bridge[0] && 8318 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0) 8319 goto failed; 8320 8321 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) 8322 goto failed; 8323 8324 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE)); 8325 if (drv->eapol_sock < 0) { 8326 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)"); 8327 goto failed; 8328 } 8329 8330 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL)) 8331 { 8332 printf("Could not register read socket for eapol\n"); 8333 goto failed; 8334 } 8335 8336 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 8337 params->own_addr)) 8338 goto failed; 8339 8340 memcpy(bss->addr, params->own_addr, ETH_ALEN); 8341 8342 return bss; 8343 8344failed: 8345 wpa_driver_nl80211_deinit(bss); 8346 return NULL; 8347} 8348 8349 8350static void i802_deinit(void *priv) 8351{ 8352 struct i802_bss *bss = priv; 8353 wpa_driver_nl80211_deinit(bss); 8354} 8355 8356#endif /* HOSTAPD */ 8357 8358 8359static enum nl80211_iftype wpa_driver_nl80211_if_type( 8360 enum wpa_driver_if_type type) 8361{ 8362 switch (type) { 8363 case WPA_IF_STATION: 8364 return NL80211_IFTYPE_STATION; 8365 case WPA_IF_P2P_CLIENT: 8366 case WPA_IF_P2P_GROUP: 8367 return NL80211_IFTYPE_P2P_CLIENT; 8368 case WPA_IF_AP_VLAN: 8369 return NL80211_IFTYPE_AP_VLAN; 8370 case WPA_IF_AP_BSS: 8371 return NL80211_IFTYPE_AP; 8372 case WPA_IF_P2P_GO: 8373 return NL80211_IFTYPE_P2P_GO; 8374 } 8375 return -1; 8376} 8377 8378 8379#ifdef CONFIG_P2P 8380 8381static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr) 8382{ 8383 struct wpa_driver_nl80211_data *drv; 8384 dl_list_for_each(drv, &global->interfaces, 8385 struct wpa_driver_nl80211_data, list) { 8386 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0) 8387 return 1; 8388 } 8389 return 0; 8390} 8391 8392 8393static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv, 8394 u8 *new_addr) 8395{ 8396 unsigned int idx; 8397 8398 if (!drv->global) 8399 return -1; 8400 8401 os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN); 8402 for (idx = 0; idx < 64; idx++) { 8403 new_addr[0] = drv->first_bss.addr[0] | 0x02; 8404 new_addr[0] ^= idx << 2; 8405 if (!nl80211_addr_in_use(drv->global, new_addr)) 8406 break; 8407 } 8408 if (idx == 64) 8409 return -1; 8410 8411 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address " 8412 MACSTR, MAC2STR(new_addr)); 8413 8414 return 0; 8415} 8416 8417#endif /* CONFIG_P2P */ 8418 8419 8420static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type, 8421 const char *ifname, const u8 *addr, 8422 void *bss_ctx, void **drv_priv, 8423 char *force_ifname, u8 *if_addr, 8424 const char *bridge) 8425{ 8426 struct i802_bss *bss = priv; 8427 struct wpa_driver_nl80211_data *drv = bss->drv; 8428 int ifidx; 8429#ifdef HOSTAPD 8430 struct i802_bss *new_bss = NULL; 8431 8432 if (type == WPA_IF_AP_BSS) { 8433 new_bss = os_zalloc(sizeof(*new_bss)); 8434 if (new_bss == NULL) 8435 return -1; 8436 } 8437#endif /* HOSTAPD */ 8438 8439 if (addr) 8440 os_memcpy(if_addr, addr, ETH_ALEN); 8441 ifidx = nl80211_create_iface(drv, ifname, 8442 wpa_driver_nl80211_if_type(type), addr, 8443 0); 8444 if (ifidx < 0) { 8445#ifdef HOSTAPD 8446 os_free(new_bss); 8447#endif /* HOSTAPD */ 8448 return -1; 8449 } 8450 8451 if (!addr && 8452 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 8453 if_addr) < 0) { 8454 nl80211_remove_iface(drv, ifidx); 8455 return -1; 8456 } 8457 8458#ifdef CONFIG_P2P 8459 if (!addr && 8460 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP || 8461 type == WPA_IF_P2P_GO)) { 8462 /* Enforce unique P2P Interface Address */ 8463 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN]; 8464 8465 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 8466 own_addr) < 0 || 8467 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname, 8468 new_addr) < 0) { 8469 nl80211_remove_iface(drv, ifidx); 8470 return -1; 8471 } 8472 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) { 8473 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address " 8474 "for P2P group interface"); 8475 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) { 8476 nl80211_remove_iface(drv, ifidx); 8477 return -1; 8478 } 8479 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, 8480 new_addr) < 0) { 8481 nl80211_remove_iface(drv, ifidx); 8482 return -1; 8483 } 8484 } 8485 os_memcpy(if_addr, new_addr, ETH_ALEN); 8486 } 8487#endif /* CONFIG_P2P */ 8488 8489#ifdef HOSTAPD 8490 if (bridge && 8491 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) { 8492 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new " 8493 "interface %s to a bridge %s", ifname, bridge); 8494 nl80211_remove_iface(drv, ifidx); 8495 os_free(new_bss); 8496 return -1; 8497 } 8498 8499 if (type == WPA_IF_AP_BSS) { 8500 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1)) 8501 { 8502 nl80211_remove_iface(drv, ifidx); 8503 os_free(new_bss); 8504 return -1; 8505 } 8506 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ); 8507 os_memcpy(new_bss->addr, if_addr, ETH_ALEN); 8508 new_bss->ifindex = ifidx; 8509 new_bss->drv = drv; 8510 new_bss->next = drv->first_bss.next; 8511 new_bss->freq = drv->first_bss.freq; 8512 new_bss->ctx = bss_ctx; 8513 drv->first_bss.next = new_bss; 8514 if (drv_priv) 8515 *drv_priv = new_bss; 8516 nl80211_init_bss(new_bss); 8517 8518 /* Subscribe management frames for this WPA_IF_AP_BSS */ 8519 if (nl80211_setup_ap(new_bss)) 8520 return -1; 8521 } 8522#endif /* HOSTAPD */ 8523 8524 if (drv->global) 8525 drv->global->if_add_ifindex = ifidx; 8526 8527 return 0; 8528} 8529 8530 8531static int wpa_driver_nl80211_if_remove(struct i802_bss *bss, 8532 enum wpa_driver_if_type type, 8533 const char *ifname) 8534{ 8535 struct wpa_driver_nl80211_data *drv = bss->drv; 8536 int ifindex = if_nametoindex(ifname); 8537 8538 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d", 8539 __func__, type, ifname, ifindex); 8540 if (ifindex <= 0) 8541 return -1; 8542 8543 nl80211_remove_iface(drv, ifindex); 8544 8545#ifdef HOSTAPD 8546 if (type != WPA_IF_AP_BSS) 8547 return 0; 8548 8549 if (bss->added_if_into_bridge) { 8550 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname, 8551 bss->ifname) < 0) 8552 wpa_printf(MSG_INFO, "nl80211: Failed to remove " 8553 "interface %s from bridge %s: %s", 8554 bss->ifname, bss->brname, strerror(errno)); 8555 } 8556 if (bss->added_bridge) { 8557 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0) 8558 wpa_printf(MSG_INFO, "nl80211: Failed to remove " 8559 "bridge %s: %s", 8560 bss->brname, strerror(errno)); 8561 } 8562 8563 if (bss != &drv->first_bss) { 8564 struct i802_bss *tbss; 8565 8566 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) { 8567 if (tbss->next == bss) { 8568 tbss->next = bss->next; 8569 /* Unsubscribe management frames */ 8570 nl80211_teardown_ap(bss); 8571 nl80211_destroy_bss(bss); 8572 os_free(bss); 8573 bss = NULL; 8574 break; 8575 } 8576 } 8577 if (bss) 8578 wpa_printf(MSG_INFO, "nl80211: %s - could not find " 8579 "BSS %p in the list", __func__, bss); 8580 } 8581#endif /* HOSTAPD */ 8582 8583 return 0; 8584} 8585 8586 8587static int cookie_handler(struct nl_msg *msg, void *arg) 8588{ 8589 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 8590 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 8591 u64 *cookie = arg; 8592 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 8593 genlmsg_attrlen(gnlh, 0), NULL); 8594 if (tb[NL80211_ATTR_COOKIE]) 8595 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]); 8596 return NL_SKIP; 8597} 8598 8599 8600static int nl80211_send_frame_cmd(struct i802_bss *bss, 8601 unsigned int freq, unsigned int wait, 8602 const u8 *buf, size_t buf_len, 8603 u64 *cookie_out, int no_cck, int no_ack, 8604 int offchanok) 8605{ 8606 struct wpa_driver_nl80211_data *drv = bss->drv; 8607 struct nl_msg *msg; 8608 u64 cookie; 8609 int ret = -1; 8610 8611 msg = nlmsg_alloc(); 8612 if (!msg) 8613 return -1; 8614 8615 wpa_printf(MSG_DEBUG, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d " 8616 "no_ack=%d offchanok=%d", 8617 freq, wait, no_cck, no_ack, offchanok); 8618 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME); 8619 8620 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 8621 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); 8622 if (wait) 8623 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait); 8624 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX)) 8625 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK); 8626 if (no_cck) 8627 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE); 8628 if (no_ack) 8629 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK); 8630 8631 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf); 8632 8633 cookie = 0; 8634 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie); 8635 msg = NULL; 8636 if (ret) { 8637 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d " 8638 "(%s) (freq=%u wait=%u)", ret, strerror(-ret), 8639 freq, wait); 8640 goto nla_put_failure; 8641 } 8642 wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted%s; " 8643 "cookie 0x%llx", no_ack ? " (no ACK)" : "", 8644 (long long unsigned int) cookie); 8645 8646 if (cookie_out) 8647 *cookie_out = no_ack ? (u64) -1 : cookie; 8648 8649nla_put_failure: 8650 nlmsg_free(msg); 8651 return ret; 8652} 8653 8654 8655static int wpa_driver_nl80211_send_action(struct i802_bss *bss, 8656 unsigned int freq, 8657 unsigned int wait_time, 8658 const u8 *dst, const u8 *src, 8659 const u8 *bssid, 8660 const u8 *data, size_t data_len, 8661 int no_cck) 8662{ 8663 struct wpa_driver_nl80211_data *drv = bss->drv; 8664 int ret = -1; 8665 u8 *buf; 8666 struct ieee80211_hdr *hdr; 8667 8668 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, " 8669 "freq=%u MHz wait=%d ms no_cck=%d)", 8670 drv->ifindex, freq, wait_time, no_cck); 8671 8672 buf = os_zalloc(24 + data_len); 8673 if (buf == NULL) 8674 return ret; 8675 os_memcpy(buf + 24, data, data_len); 8676 hdr = (struct ieee80211_hdr *) buf; 8677 hdr->frame_control = 8678 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION); 8679 os_memcpy(hdr->addr1, dst, ETH_ALEN); 8680 os_memcpy(hdr->addr2, src, ETH_ALEN); 8681 os_memcpy(hdr->addr3, bssid, ETH_ALEN); 8682 8683 if (is_ap_interface(drv->nlmode)) 8684 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len, 8685 0, freq, no_cck, 1, 8686 wait_time); 8687 else 8688 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf, 8689 24 + data_len, 8690 &drv->send_action_cookie, 8691 no_cck, 0, 1); 8692 8693 os_free(buf); 8694 return ret; 8695} 8696 8697 8698static void wpa_driver_nl80211_send_action_cancel_wait(void *priv) 8699{ 8700 struct i802_bss *bss = priv; 8701 struct wpa_driver_nl80211_data *drv = bss->drv; 8702 struct nl_msg *msg; 8703 int ret; 8704 8705 msg = nlmsg_alloc(); 8706 if (!msg) 8707 return; 8708 8709 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx", 8710 (long long unsigned int) drv->send_action_cookie); 8711 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL); 8712 8713 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 8714 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie); 8715 8716 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 8717 msg = NULL; 8718 if (ret) 8719 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d " 8720 "(%s)", ret, strerror(-ret)); 8721 8722 nla_put_failure: 8723 nlmsg_free(msg); 8724} 8725 8726 8727static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq, 8728 unsigned int duration) 8729{ 8730 struct i802_bss *bss = priv; 8731 struct wpa_driver_nl80211_data *drv = bss->drv; 8732 struct nl_msg *msg; 8733 int ret; 8734 u64 cookie; 8735 8736 msg = nlmsg_alloc(); 8737 if (!msg) 8738 return -1; 8739 8740 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL); 8741 8742 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 8743 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); 8744 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration); 8745 8746 cookie = 0; 8747 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie); 8748 msg = NULL; 8749 if (ret == 0) { 8750 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie " 8751 "0x%llx for freq=%u MHz duration=%u", 8752 (long long unsigned int) cookie, freq, duration); 8753 drv->remain_on_chan_cookie = cookie; 8754 drv->pending_remain_on_chan = 1; 8755 return 0; 8756 } 8757 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel " 8758 "(freq=%d duration=%u): %d (%s)", 8759 freq, duration, ret, strerror(-ret)); 8760nla_put_failure: 8761 nlmsg_free(msg); 8762 return -1; 8763} 8764 8765 8766static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv) 8767{ 8768 struct i802_bss *bss = priv; 8769 struct wpa_driver_nl80211_data *drv = bss->drv; 8770 struct nl_msg *msg; 8771 int ret; 8772 8773 if (!drv->pending_remain_on_chan) { 8774 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel " 8775 "to cancel"); 8776 return -1; 8777 } 8778 8779 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie " 8780 "0x%llx", 8781 (long long unsigned int) drv->remain_on_chan_cookie); 8782 8783 msg = nlmsg_alloc(); 8784 if (!msg) 8785 return -1; 8786 8787 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL); 8788 8789 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 8790 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie); 8791 8792 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 8793 msg = NULL; 8794 if (ret == 0) 8795 return 0; 8796 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: " 8797 "%d (%s)", ret, strerror(-ret)); 8798nla_put_failure: 8799 nlmsg_free(msg); 8800 return -1; 8801} 8802 8803 8804static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report) 8805{ 8806 struct wpa_driver_nl80211_data *drv = bss->drv; 8807 8808 if (!report) { 8809 if (bss->nl_preq && drv->device_ap_sme && 8810 is_ap_interface(drv->nlmode)) { 8811 /* 8812 * Do not disable Probe Request reporting that was 8813 * enabled in nl80211_setup_ap(). 8814 */ 8815 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of " 8816 "Probe Request reporting nl_preq=%p while " 8817 "in AP mode", bss->nl_preq); 8818 } else if (bss->nl_preq) { 8819 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request " 8820 "reporting nl_preq=%p", bss->nl_preq); 8821 eloop_unregister_read_sock( 8822 nl_socket_get_fd(bss->nl_preq)); 8823 nl_destroy_handles(&bss->nl_preq); 8824 } 8825 return 0; 8826 } 8827 8828 if (bss->nl_preq) { 8829 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting " 8830 "already on! nl_preq=%p", bss->nl_preq); 8831 return 0; 8832 } 8833 8834 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq"); 8835 if (bss->nl_preq == NULL) 8836 return -1; 8837 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request " 8838 "reporting nl_preq=%p", bss->nl_preq); 8839 8840 if (nl80211_register_frame(bss, bss->nl_preq, 8841 (WLAN_FC_TYPE_MGMT << 2) | 8842 (WLAN_FC_STYPE_PROBE_REQ << 4), 8843 NULL, 0) < 0) 8844 goto out_err; 8845 8846 eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq), 8847 wpa_driver_nl80211_event_receive, bss->nl_cb, 8848 bss->nl_preq); 8849 8850 return 0; 8851 8852 out_err: 8853 nl_destroy_handles(&bss->nl_preq); 8854 return -1; 8855} 8856 8857 8858static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv, 8859 int ifindex, int disabled) 8860{ 8861 struct nl_msg *msg; 8862 struct nlattr *bands, *band; 8863 int ret; 8864 8865 msg = nlmsg_alloc(); 8866 if (!msg) 8867 return -1; 8868 8869 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK); 8870 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 8871 8872 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES); 8873 if (!bands) 8874 goto nla_put_failure; 8875 8876 /* 8877 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything 8878 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS 8879 * rates. All 5 GHz rates are left enabled. 8880 */ 8881 band = nla_nest_start(msg, NL80211_BAND_2GHZ); 8882 if (!band) 8883 goto nla_put_failure; 8884 if (disabled) { 8885 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8, 8886 "\x0c\x12\x18\x24\x30\x48\x60\x6c"); 8887 } 8888 nla_nest_end(msg, band); 8889 8890 nla_nest_end(msg, bands); 8891 8892 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 8893 msg = NULL; 8894 if (ret) { 8895 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d " 8896 "(%s)", ret, strerror(-ret)); 8897 } else 8898 drv->disabled_11b_rates = disabled; 8899 8900 return ret; 8901 8902nla_put_failure: 8903 nlmsg_free(msg); 8904 return -1; 8905} 8906 8907 8908static int wpa_driver_nl80211_deinit_ap(void *priv) 8909{ 8910 struct i802_bss *bss = priv; 8911 struct wpa_driver_nl80211_data *drv = bss->drv; 8912 if (!is_ap_interface(drv->nlmode)) 8913 return -1; 8914 wpa_driver_nl80211_del_beacon(drv); 8915 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION); 8916} 8917 8918 8919static int wpa_driver_nl80211_deinit_p2p_cli(void *priv) 8920{ 8921 struct i802_bss *bss = priv; 8922 struct wpa_driver_nl80211_data *drv = bss->drv; 8923 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT) 8924 return -1; 8925 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION); 8926} 8927 8928 8929static void wpa_driver_nl80211_resume(void *priv) 8930{ 8931 struct i802_bss *bss = priv; 8932 struct wpa_driver_nl80211_data *drv = bss->drv; 8933 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) { 8934 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on " 8935 "resume event"); 8936 } 8937} 8938 8939 8940static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap, 8941 const u8 *ies, size_t ies_len) 8942{ 8943 struct i802_bss *bss = priv; 8944 struct wpa_driver_nl80211_data *drv = bss->drv; 8945 int ret; 8946 u8 *data, *pos; 8947 size_t data_len; 8948 const u8 *own_addr = bss->addr; 8949 8950 if (action != 1) { 8951 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action " 8952 "action %d", action); 8953 return -1; 8954 } 8955 8956 /* 8957 * Action frame payload: 8958 * Category[1] = 6 (Fast BSS Transition) 8959 * Action[1] = 1 (Fast BSS Transition Request) 8960 * STA Address 8961 * Target AP Address 8962 * FT IEs 8963 */ 8964 8965 data_len = 2 + 2 * ETH_ALEN + ies_len; 8966 data = os_malloc(data_len); 8967 if (data == NULL) 8968 return -1; 8969 pos = data; 8970 *pos++ = 0x06; /* FT Action category */ 8971 *pos++ = action; 8972 os_memcpy(pos, own_addr, ETH_ALEN); 8973 pos += ETH_ALEN; 8974 os_memcpy(pos, target_ap, ETH_ALEN); 8975 pos += ETH_ALEN; 8976 os_memcpy(pos, ies, ies_len); 8977 8978 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0, 8979 drv->bssid, own_addr, drv->bssid, 8980 data, data_len, 0); 8981 os_free(data); 8982 8983 return ret; 8984} 8985 8986 8987static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis) 8988{ 8989 struct i802_bss *bss = priv; 8990 struct wpa_driver_nl80211_data *drv = bss->drv; 8991 struct nl_msg *msg, *cqm = NULL; 8992 int ret = -1; 8993 8994 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d " 8995 "hysteresis=%d", threshold, hysteresis); 8996 8997 msg = nlmsg_alloc(); 8998 if (!msg) 8999 return -1; 9000 9001 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM); 9002 9003 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 9004 9005 cqm = nlmsg_alloc(); 9006 if (cqm == NULL) 9007 goto nla_put_failure; 9008 9009 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold); 9010 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis); 9011 if (nla_put_nested(msg, NL80211_ATTR_CQM, cqm) < 0) 9012 goto nla_put_failure; 9013 9014 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 9015 msg = NULL; 9016 9017nla_put_failure: 9018 nlmsg_free(cqm); 9019 nlmsg_free(msg); 9020 return ret; 9021} 9022 9023 9024static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si) 9025{ 9026 struct i802_bss *bss = priv; 9027 struct wpa_driver_nl80211_data *drv = bss->drv; 9028 int res; 9029 9030 os_memset(si, 0, sizeof(*si)); 9031 res = nl80211_get_link_signal(drv, si); 9032 if (res != 0) 9033 return res; 9034 9035 return nl80211_get_link_noise(drv, si); 9036} 9037 9038 9039static int wpa_driver_nl80211_shared_freq(void *priv) 9040{ 9041 struct i802_bss *bss = priv; 9042 struct wpa_driver_nl80211_data *drv = bss->drv; 9043 struct wpa_driver_nl80211_data *driver; 9044 int freq = 0; 9045 9046 /* 9047 * If the same PHY is in connected state with some other interface, 9048 * then retrieve the assoc freq. 9049 */ 9050 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s", 9051 drv->phyname); 9052 9053 dl_list_for_each(driver, &drv->global->interfaces, 9054 struct wpa_driver_nl80211_data, list) { 9055 if (drv == driver || 9056 os_strcmp(drv->phyname, driver->phyname) != 0 || 9057#ifdef ANDROID_P2P 9058 (!driver->associated && !is_ap_interface(driver->nlmode))) 9059#else 9060 !driver->associated) 9061#endif 9062 continue; 9063 9064 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s " 9065 MACSTR, 9066 driver->phyname, driver->first_bss.ifname, 9067 MAC2STR(driver->first_bss.addr)); 9068 if (is_ap_interface(driver->nlmode)) 9069 freq = driver->first_bss.freq; 9070 else 9071 freq = nl80211_get_assoc_freq(driver); 9072 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d", 9073 drv->phyname, freq); 9074 } 9075 9076 if (!freq) 9077 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for " 9078 "PHY (%s) in associated state", drv->phyname); 9079 9080 return freq; 9081} 9082 9083 9084static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len, 9085 int encrypt) 9086{ 9087 struct i802_bss *bss = priv; 9088 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0, 9089 0, 0, 0, 0); 9090} 9091 9092 9093static int nl80211_set_param(void *priv, const char *param) 9094{ 9095 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param); 9096 if (param == NULL) 9097 return 0; 9098 9099#ifdef CONFIG_P2P 9100 if (os_strstr(param, "use_p2p_group_interface=1")) { 9101 struct i802_bss *bss = priv; 9102 struct wpa_driver_nl80211_data *drv = bss->drv; 9103 9104 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group " 9105 "interface"); 9106 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT; 9107 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P; 9108 } 9109#ifdef ANDROID_P2P 9110 if(os_strstr(param, "use_multi_chan_concurrent=1")) { 9111 struct i802_bss *bss = priv; 9112 struct wpa_driver_nl80211_data *drv = bss->drv; 9113 wpa_printf(MSG_DEBUG, "nl80211: Use Multi channel " 9114 "concurrency"); 9115 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT; 9116 } 9117#endif 9118#endif /* CONFIG_P2P */ 9119 9120 return 0; 9121} 9122 9123 9124static void * nl80211_global_init(void) 9125{ 9126 struct nl80211_global *global; 9127 struct netlink_config *cfg; 9128 9129 global = os_zalloc(sizeof(*global)); 9130 if (global == NULL) 9131 return NULL; 9132 global->ioctl_sock = -1; 9133 dl_list_init(&global->interfaces); 9134 global->if_add_ifindex = -1; 9135 9136 cfg = os_zalloc(sizeof(*cfg)); 9137 if (cfg == NULL) 9138 goto err; 9139 9140 cfg->ctx = global; 9141 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink; 9142 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink; 9143 global->netlink = netlink_init(cfg); 9144 if (global->netlink == NULL) { 9145 os_free(cfg); 9146 goto err; 9147 } 9148 9149 if (wpa_driver_nl80211_init_nl_global(global) < 0) 9150 goto err; 9151 9152 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0); 9153 if (global->ioctl_sock < 0) { 9154 perror("socket(PF_INET,SOCK_DGRAM)"); 9155 goto err; 9156 } 9157 9158 return global; 9159 9160err: 9161 nl80211_global_deinit(global); 9162 return NULL; 9163} 9164 9165 9166static void nl80211_global_deinit(void *priv) 9167{ 9168 struct nl80211_global *global = priv; 9169 if (global == NULL) 9170 return; 9171 if (!dl_list_empty(&global->interfaces)) { 9172 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at " 9173 "nl80211_global_deinit", 9174 dl_list_len(&global->interfaces)); 9175 } 9176 9177 if (global->netlink) 9178 netlink_deinit(global->netlink); 9179 9180 nl_destroy_handles(&global->nl); 9181 9182 if (global->nl_event) { 9183 eloop_unregister_read_sock( 9184 nl_socket_get_fd(global->nl_event)); 9185 nl_destroy_handles(&global->nl_event); 9186 } 9187 9188 nl_cb_put(global->nl_cb); 9189 9190 if (global->ioctl_sock >= 0) 9191 close(global->ioctl_sock); 9192 9193 os_free(global); 9194} 9195 9196 9197static const char * nl80211_get_radio_name(void *priv) 9198{ 9199 struct i802_bss *bss = priv; 9200 struct wpa_driver_nl80211_data *drv = bss->drv; 9201 return drv->phyname; 9202} 9203 9204 9205static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid, 9206 const u8 *pmkid) 9207{ 9208 struct nl_msg *msg; 9209 9210 msg = nlmsg_alloc(); 9211 if (!msg) 9212 return -ENOMEM; 9213 9214 nl80211_cmd(bss->drv, msg, 0, cmd); 9215 9216 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 9217 if (pmkid) 9218 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid); 9219 if (bssid) 9220 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid); 9221 9222 return send_and_recv_msgs(bss->drv, msg, NULL, NULL); 9223 nla_put_failure: 9224 nlmsg_free(msg); 9225 return -ENOBUFS; 9226} 9227 9228 9229static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid) 9230{ 9231 struct i802_bss *bss = priv; 9232 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid)); 9233 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid); 9234} 9235 9236 9237static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid) 9238{ 9239 struct i802_bss *bss = priv; 9240 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR, 9241 MAC2STR(bssid)); 9242 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid); 9243} 9244 9245 9246static int nl80211_flush_pmkid(void *priv) 9247{ 9248 struct i802_bss *bss = priv; 9249 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs"); 9250 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL); 9251} 9252 9253 9254static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck, 9255 const u8 *replay_ctr) 9256{ 9257 struct i802_bss *bss = priv; 9258 struct wpa_driver_nl80211_data *drv = bss->drv; 9259 struct nlattr *replay_nested; 9260 struct nl_msg *msg; 9261 9262 msg = nlmsg_alloc(); 9263 if (!msg) 9264 return; 9265 9266 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD); 9267 9268 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 9269 9270 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA); 9271 if (!replay_nested) 9272 goto nla_put_failure; 9273 9274 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek); 9275 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck); 9276 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN, 9277 replay_ctr); 9278 9279 nla_nest_end(msg, replay_nested); 9280 9281 send_and_recv_msgs(drv, msg, NULL, NULL); 9282 return; 9283 nla_put_failure: 9284 nlmsg_free(msg); 9285} 9286 9287 9288static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr, 9289 const u8 *addr, int qos) 9290{ 9291 /* send data frame to poll STA and check whether 9292 * this frame is ACKed */ 9293 struct { 9294 struct ieee80211_hdr hdr; 9295 u16 qos_ctl; 9296 } STRUCT_PACKED nulldata; 9297 size_t size; 9298 9299 /* Send data frame to poll STA and check whether this frame is ACKed */ 9300 9301 os_memset(&nulldata, 0, sizeof(nulldata)); 9302 9303 if (qos) { 9304 nulldata.hdr.frame_control = 9305 IEEE80211_FC(WLAN_FC_TYPE_DATA, 9306 WLAN_FC_STYPE_QOS_NULL); 9307 size = sizeof(nulldata); 9308 } else { 9309 nulldata.hdr.frame_control = 9310 IEEE80211_FC(WLAN_FC_TYPE_DATA, 9311 WLAN_FC_STYPE_NULLFUNC); 9312 size = sizeof(struct ieee80211_hdr); 9313 } 9314 9315 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS); 9316 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN); 9317 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN); 9318 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN); 9319 9320 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0, 9321 0, 0) < 0) 9322 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to " 9323 "send poll frame"); 9324} 9325 9326static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr, 9327 int qos) 9328{ 9329 struct i802_bss *bss = priv; 9330 struct wpa_driver_nl80211_data *drv = bss->drv; 9331 struct nl_msg *msg; 9332 9333 if (!drv->poll_command_supported) { 9334 nl80211_send_null_frame(bss, own_addr, addr, qos); 9335 return; 9336 } 9337 9338 msg = nlmsg_alloc(); 9339 if (!msg) 9340 return; 9341 9342 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT); 9343 9344 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 9345 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 9346 9347 send_and_recv_msgs(drv, msg, NULL, NULL); 9348 return; 9349 nla_put_failure: 9350 nlmsg_free(msg); 9351} 9352 9353 9354static int nl80211_set_power_save(struct i802_bss *bss, int enabled) 9355{ 9356 struct nl_msg *msg; 9357 9358 msg = nlmsg_alloc(); 9359 if (!msg) 9360 return -ENOMEM; 9361 9362 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE); 9363 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 9364 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, 9365 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED); 9366 return send_and_recv_msgs(bss->drv, msg, NULL, NULL); 9367nla_put_failure: 9368 nlmsg_free(msg); 9369 return -ENOBUFS; 9370} 9371 9372 9373static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps, 9374 int ctwindow) 9375{ 9376 struct i802_bss *bss = priv; 9377 9378 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d " 9379 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow); 9380 9381 if (opp_ps != -1 || ctwindow != -1) 9382#ifdef ANDROID_P2P 9383 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow); 9384#else 9385 return -1; /* Not yet supported */ 9386#endif 9387 9388 if (legacy_ps == -1) 9389 return 0; 9390 if (legacy_ps != 0 && legacy_ps != 1) 9391 return -1; /* Not yet supported */ 9392 9393 return nl80211_set_power_save(bss, legacy_ps); 9394} 9395 9396 9397#ifdef CONFIG_TDLS 9398 9399static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code, 9400 u8 dialog_token, u16 status_code, 9401 const u8 *buf, size_t len) 9402{ 9403 struct i802_bss *bss = priv; 9404 struct wpa_driver_nl80211_data *drv = bss->drv; 9405 struct nl_msg *msg; 9406 9407 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) 9408 return -EOPNOTSUPP; 9409 9410 if (!dst) 9411 return -EINVAL; 9412 9413 msg = nlmsg_alloc(); 9414 if (!msg) 9415 return -ENOMEM; 9416 9417 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT); 9418 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 9419 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst); 9420 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code); 9421 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token); 9422 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code); 9423 NLA_PUT(msg, NL80211_ATTR_IE, len, buf); 9424 9425 return send_and_recv_msgs(drv, msg, NULL, NULL); 9426 9427nla_put_failure: 9428 nlmsg_free(msg); 9429 return -ENOBUFS; 9430} 9431 9432 9433static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer) 9434{ 9435 struct i802_bss *bss = priv; 9436 struct wpa_driver_nl80211_data *drv = bss->drv; 9437 struct nl_msg *msg; 9438 enum nl80211_tdls_operation nl80211_oper; 9439 9440 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) 9441 return -EOPNOTSUPP; 9442 9443 switch (oper) { 9444 case TDLS_DISCOVERY_REQ: 9445 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ; 9446 break; 9447 case TDLS_SETUP: 9448 nl80211_oper = NL80211_TDLS_SETUP; 9449 break; 9450 case TDLS_TEARDOWN: 9451 nl80211_oper = NL80211_TDLS_TEARDOWN; 9452 break; 9453 case TDLS_ENABLE_LINK: 9454 nl80211_oper = NL80211_TDLS_ENABLE_LINK; 9455 break; 9456 case TDLS_DISABLE_LINK: 9457 nl80211_oper = NL80211_TDLS_DISABLE_LINK; 9458 break; 9459 case TDLS_ENABLE: 9460 return 0; 9461 case TDLS_DISABLE: 9462 return 0; 9463 default: 9464 return -EINVAL; 9465 } 9466 9467 msg = nlmsg_alloc(); 9468 if (!msg) 9469 return -ENOMEM; 9470 9471 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER); 9472 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper); 9473 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 9474 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer); 9475 9476 return send_and_recv_msgs(drv, msg, NULL, NULL); 9477 9478nla_put_failure: 9479 nlmsg_free(msg); 9480 return -ENOBUFS; 9481} 9482 9483#endif /* CONFIG TDLS */ 9484 9485 9486#ifdef ANDROID 9487 9488typedef struct android_wifi_priv_cmd { 9489 char *buf; 9490 int used_len; 9491 int total_len; 9492} android_wifi_priv_cmd; 9493 9494static int drv_errors = 0; 9495 9496static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv) 9497{ 9498 drv_errors++; 9499 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) { 9500 drv_errors = 0; 9501 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED"); 9502 } 9503} 9504 9505 9506static int android_priv_cmd(struct i802_bss *bss, const char *cmd) 9507{ 9508 struct wpa_driver_nl80211_data *drv = bss->drv; 9509 struct ifreq ifr; 9510 android_wifi_priv_cmd priv_cmd; 9511 char buf[MAX_DRV_CMD_SIZE]; 9512 int ret; 9513 9514 os_memset(&ifr, 0, sizeof(ifr)); 9515 os_memset(&priv_cmd, 0, sizeof(priv_cmd)); 9516 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ); 9517 9518 os_memset(buf, 0, sizeof(buf)); 9519 os_strlcpy(buf, cmd, sizeof(buf)); 9520 9521 priv_cmd.buf = buf; 9522 priv_cmd.used_len = sizeof(buf); 9523 priv_cmd.total_len = sizeof(buf); 9524 ifr.ifr_data = &priv_cmd; 9525 9526 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr); 9527 if (ret < 0) { 9528 wpa_printf(MSG_ERROR, "%s: failed to issue private commands", 9529 __func__); 9530 wpa_driver_send_hang_msg(drv); 9531 return ret; 9532 } 9533 9534 drv_errors = 0; 9535 return 0; 9536} 9537 9538 9539static int android_pno_start(struct i802_bss *bss, 9540 struct wpa_driver_scan_params *params) 9541{ 9542 struct wpa_driver_nl80211_data *drv = bss->drv; 9543 struct ifreq ifr; 9544 android_wifi_priv_cmd priv_cmd; 9545 int ret = 0, i = 0, bp; 9546 char buf[WEXT_PNO_MAX_COMMAND_SIZE]; 9547 9548 bp = WEXT_PNOSETUP_HEADER_SIZE; 9549 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp); 9550 buf[bp++] = WEXT_PNO_TLV_PREFIX; 9551 buf[bp++] = WEXT_PNO_TLV_VERSION; 9552 buf[bp++] = WEXT_PNO_TLV_SUBVERSION; 9553 buf[bp++] = WEXT_PNO_TLV_RESERVED; 9554 9555 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) { 9556 /* Check that there is enough space needed for 1 more SSID, the 9557 * other sections and null termination */ 9558 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN + 9559 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf)) 9560 break; 9561 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan", 9562 params->ssids[i].ssid, 9563 params->ssids[i].ssid_len); 9564 buf[bp++] = WEXT_PNO_SSID_SECTION; 9565 buf[bp++] = params->ssids[i].ssid_len; 9566 os_memcpy(&buf[bp], params->ssids[i].ssid, 9567 params->ssids[i].ssid_len); 9568 bp += params->ssids[i].ssid_len; 9569 i++; 9570 } 9571 9572 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION; 9573 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x", 9574 WEXT_PNO_SCAN_INTERVAL); 9575 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH; 9576 9577 buf[bp++] = WEXT_PNO_REPEAT_SECTION; 9578 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x", 9579 WEXT_PNO_REPEAT); 9580 bp += WEXT_PNO_REPEAT_LENGTH; 9581 9582 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION; 9583 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x", 9584 WEXT_PNO_MAX_REPEAT); 9585 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1; 9586 9587 memset(&ifr, 0, sizeof(ifr)); 9588 memset(&priv_cmd, 0, sizeof(priv_cmd)); 9589 os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ); 9590 9591 priv_cmd.buf = buf; 9592 priv_cmd.used_len = bp; 9593 priv_cmd.total_len = bp; 9594 ifr.ifr_data = &priv_cmd; 9595 9596 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr); 9597 9598 if (ret < 0) { 9599 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d", 9600 ret); 9601 wpa_driver_send_hang_msg(drv); 9602 return ret; 9603 } 9604 9605 drv_errors = 0; 9606 9607 return android_priv_cmd(bss, "PNOFORCE 1"); 9608} 9609 9610 9611static int android_pno_stop(struct i802_bss *bss) 9612{ 9613 return android_priv_cmd(bss, "PNOFORCE 0"); 9614} 9615 9616#endif /* ANDROID */ 9617 9618 9619static int driver_nl80211_set_key(const char *ifname, void *priv, 9620 enum wpa_alg alg, const u8 *addr, 9621 int key_idx, int set_tx, 9622 const u8 *seq, size_t seq_len, 9623 const u8 *key, size_t key_len) 9624{ 9625 struct i802_bss *bss = priv; 9626 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx, 9627 set_tx, seq, seq_len, key, key_len); 9628} 9629 9630 9631static int driver_nl80211_scan2(void *priv, 9632 struct wpa_driver_scan_params *params) 9633{ 9634 struct i802_bss *bss = priv; 9635 return wpa_driver_nl80211_scan(bss, params); 9636} 9637 9638 9639static int driver_nl80211_deauthenticate(void *priv, const u8 *addr, 9640 int reason_code) 9641{ 9642 struct i802_bss *bss = priv; 9643 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code); 9644} 9645 9646 9647static int driver_nl80211_authenticate(void *priv, 9648 struct wpa_driver_auth_params *params) 9649{ 9650 struct i802_bss *bss = priv; 9651 return wpa_driver_nl80211_authenticate(bss, params); 9652} 9653 9654 9655static void driver_nl80211_deinit(void *priv) 9656{ 9657 struct i802_bss *bss = priv; 9658 wpa_driver_nl80211_deinit(bss); 9659} 9660 9661 9662static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type, 9663 const char *ifname) 9664{ 9665 struct i802_bss *bss = priv; 9666 return wpa_driver_nl80211_if_remove(bss, type, ifname); 9667} 9668 9669 9670static int driver_nl80211_send_mlme(void *priv, const u8 *data, 9671 size_t data_len, int noack) 9672{ 9673 struct i802_bss *bss = priv; 9674 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack, 9675 0, 0, 0, 0); 9676} 9677 9678 9679static int driver_nl80211_sta_remove(void *priv, const u8 *addr) 9680{ 9681 struct i802_bss *bss = priv; 9682 return wpa_driver_nl80211_sta_remove(bss, addr); 9683} 9684 9685 9686#if defined(HOSTAPD) || defined(CONFIG_AP) 9687static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr, 9688 const char *ifname, int vlan_id) 9689{ 9690 struct i802_bss *bss = priv; 9691 return i802_set_sta_vlan(bss, addr, ifname, vlan_id); 9692} 9693#endif /* HOSTAPD || CONFIG_AP */ 9694 9695 9696static int driver_nl80211_read_sta_data(void *priv, 9697 struct hostap_sta_driver_data *data, 9698 const u8 *addr) 9699{ 9700 struct i802_bss *bss = priv; 9701 return i802_read_sta_data(bss, data, addr); 9702} 9703 9704 9705static int driver_nl80211_send_action(void *priv, unsigned int freq, 9706 unsigned int wait_time, 9707 const u8 *dst, const u8 *src, 9708 const u8 *bssid, 9709 const u8 *data, size_t data_len, 9710 int no_cck) 9711{ 9712 struct i802_bss *bss = priv; 9713 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src, 9714 bssid, data, data_len, no_cck); 9715} 9716 9717 9718static int driver_nl80211_probe_req_report(void *priv, int report) 9719{ 9720 struct i802_bss *bss = priv; 9721 return wpa_driver_nl80211_probe_req_report(bss, report); 9722} 9723 9724 9725static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md, 9726 const u8 *ies, size_t ies_len) 9727{ 9728 int ret; 9729 struct nl_msg *msg; 9730 struct i802_bss *bss = priv; 9731 struct wpa_driver_nl80211_data *drv = bss->drv; 9732 u16 mdid = WPA_GET_LE16(md); 9733 9734 msg = nlmsg_alloc(); 9735 if (!msg) 9736 return -ENOMEM; 9737 9738 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs"); 9739 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES); 9740 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 9741 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies); 9742 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid); 9743 9744 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 9745 if (ret) { 9746 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed " 9747 "err=%d (%s)", ret, strerror(-ret)); 9748 } 9749 9750 return ret; 9751 9752nla_put_failure: 9753 nlmsg_free(msg); 9754 return -ENOBUFS; 9755} 9756 9757 9758const struct wpa_driver_ops wpa_driver_nl80211_ops = { 9759 .name = "nl80211", 9760 .desc = "Linux nl80211/cfg80211", 9761 .get_bssid = wpa_driver_nl80211_get_bssid, 9762 .get_ssid = wpa_driver_nl80211_get_ssid, 9763 .set_key = driver_nl80211_set_key, 9764 .scan2 = driver_nl80211_scan2, 9765 .sched_scan = wpa_driver_nl80211_sched_scan, 9766 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan, 9767 .get_scan_results2 = wpa_driver_nl80211_get_scan_results, 9768 .deauthenticate = driver_nl80211_deauthenticate, 9769 .authenticate = driver_nl80211_authenticate, 9770 .associate = wpa_driver_nl80211_associate, 9771 .global_init = nl80211_global_init, 9772 .global_deinit = nl80211_global_deinit, 9773 .init2 = wpa_driver_nl80211_init, 9774 .deinit = driver_nl80211_deinit, 9775 .get_capa = wpa_driver_nl80211_get_capa, 9776 .set_operstate = wpa_driver_nl80211_set_operstate, 9777 .set_supp_port = wpa_driver_nl80211_set_supp_port, 9778 .set_country = wpa_driver_nl80211_set_country, 9779 .set_ap = wpa_driver_nl80211_set_ap, 9780 .if_add = wpa_driver_nl80211_if_add, 9781 .if_remove = driver_nl80211_if_remove, 9782 .send_mlme = driver_nl80211_send_mlme, 9783 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data, 9784 .sta_add = wpa_driver_nl80211_sta_add, 9785 .sta_remove = driver_nl80211_sta_remove, 9786 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol, 9787 .sta_set_flags = wpa_driver_nl80211_sta_set_flags, 9788#ifdef HOSTAPD 9789 .hapd_init = i802_init, 9790 .hapd_deinit = i802_deinit, 9791 .set_wds_sta = i802_set_wds_sta, 9792#endif /* HOSTAPD */ 9793#if defined(HOSTAPD) || defined(CONFIG_AP) 9794 .get_seqnum = i802_get_seqnum, 9795 .flush = i802_flush, 9796 .get_inact_sec = i802_get_inact_sec, 9797 .sta_clear_stats = i802_sta_clear_stats, 9798 .set_rts = i802_set_rts, 9799 .set_frag = i802_set_frag, 9800 .set_tx_queue_params = i802_set_tx_queue_params, 9801 .set_sta_vlan = driver_nl80211_set_sta_vlan, 9802 .sta_deauth = i802_sta_deauth, 9803 .sta_disassoc = i802_sta_disassoc, 9804#endif /* HOSTAPD || CONFIG_AP */ 9805 .read_sta_data = driver_nl80211_read_sta_data, 9806 .set_freq = i802_set_freq, 9807 .send_action = driver_nl80211_send_action, 9808 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait, 9809 .remain_on_channel = wpa_driver_nl80211_remain_on_channel, 9810 .cancel_remain_on_channel = 9811 wpa_driver_nl80211_cancel_remain_on_channel, 9812 .probe_req_report = driver_nl80211_probe_req_report, 9813 .deinit_ap = wpa_driver_nl80211_deinit_ap, 9814 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli, 9815 .resume = wpa_driver_nl80211_resume, 9816 .send_ft_action = nl80211_send_ft_action, 9817 .signal_monitor = nl80211_signal_monitor, 9818 .signal_poll = nl80211_signal_poll, 9819 .send_frame = nl80211_send_frame, 9820 .shared_freq = wpa_driver_nl80211_shared_freq, 9821 .set_param = nl80211_set_param, 9822 .get_radio_name = nl80211_get_radio_name, 9823 .add_pmkid = nl80211_add_pmkid, 9824 .remove_pmkid = nl80211_remove_pmkid, 9825 .flush_pmkid = nl80211_flush_pmkid, 9826 .set_rekey_info = nl80211_set_rekey_info, 9827 .poll_client = nl80211_poll_client, 9828 .set_p2p_powersave = nl80211_set_p2p_powersave, 9829#ifdef CONFIG_TDLS 9830 .send_tdls_mgmt = nl80211_send_tdls_mgmt, 9831 .tdls_oper = nl80211_tdls_oper, 9832#endif /* CONFIG_TDLS */ 9833 .update_ft_ies = wpa_driver_nl80211_update_ft_ies, 9834#ifdef ANDROID_P2P 9835 .set_noa = wpa_driver_set_p2p_noa, 9836 .get_noa = wpa_driver_get_p2p_noa, 9837 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie, 9838#endif 9839#ifdef ANDROID 9840 .driver_cmd = wpa_driver_nl80211_driver_cmd, 9841#endif 9842}; 9843