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