1 /* 2 * Driver interaction with Linux nl80211/cfg80211 3 * Copyright (c) 2002-2012, Jouni Malinen <j (at) 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 (at) 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 */ 77 static uint32_t port_bitmap[32] = { 0 }; 78 79 static 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 100 static 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 static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg) 113 { 114 struct nl_handle *handle; 115 116 handle = nl80211_handle_alloc(cb); 117 if (handle == NULL) { 118 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " 119 "callbacks (%s)", dbg); 120 return NULL; 121 } 122 123 if (genl_connect(handle)) { 124 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic " 125 "netlink (%s)", dbg); 126 nl80211_handle_destroy(handle); 127 return NULL; 128 } 129 130 return handle; 131 } 132 133 134 static void nl_destroy_handles(struct nl_handle **handle) 135 { 136 if (*handle == NULL) 137 return; 138 nl80211_handle_destroy(*handle); 139 *handle = NULL; 140 } 141 142 143 #ifndef IFF_LOWER_UP 144 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */ 145 #endif 146 #ifndef IFF_DORMANT 147 #define IFF_DORMANT 0x20000 /* driver signals dormant */ 148 #endif 149 150 #ifndef IF_OPER_DORMANT 151 #define IF_OPER_DORMANT 5 152 #endif 153 #ifndef IF_OPER_UP 154 #define IF_OPER_UP 6 155 #endif 156 157 struct nl80211_global { 158 struct dl_list interfaces; 159 int if_add_ifindex; 160 u64 if_add_wdevid; 161 int if_add_wdevid_set; 162 struct netlink_data *netlink; 163 struct nl_cb *nl_cb; 164 struct nl_handle *nl; 165 int nl80211_id; 166 int ioctl_sock; /* socket for ioctl() use */ 167 168 struct nl_handle *nl_event; 169 }; 170 171 struct nl80211_wiphy_data { 172 struct dl_list list; 173 struct dl_list bsss; 174 struct dl_list drvs; 175 176 struct nl_handle *nl_beacons; 177 struct nl_cb *nl_cb; 178 179 int wiphy_idx; 180 }; 181 182 static void nl80211_global_deinit(void *priv); 183 184 struct i802_bss { 185 struct wpa_driver_nl80211_data *drv; 186 struct i802_bss *next; 187 int ifindex; 188 u64 wdev_id; 189 char ifname[IFNAMSIZ + 1]; 190 char brname[IFNAMSIZ]; 191 unsigned int beacon_set:1; 192 unsigned int added_if_into_bridge:1; 193 unsigned int added_bridge:1; 194 unsigned int in_deinit:1; 195 unsigned int wdev_id_set:1; 196 197 u8 addr[ETH_ALEN]; 198 199 int freq; 200 int if_dynamic; 201 202 void *ctx; 203 struct nl_handle *nl_preq, *nl_mgmt; 204 struct nl_cb *nl_cb; 205 206 struct nl80211_wiphy_data *wiphy_data; 207 struct dl_list wiphy_list; 208 }; 209 210 struct wpa_driver_nl80211_data { 211 struct nl80211_global *global; 212 struct dl_list list; 213 struct dl_list wiphy_list; 214 char phyname[32]; 215 void *ctx; 216 int ifindex; 217 int if_removed; 218 int if_disabled; 219 int ignore_if_down_event; 220 struct rfkill_data *rfkill; 221 struct wpa_driver_capa capa; 222 u8 *extended_capa, *extended_capa_mask; 223 unsigned int extended_capa_len; 224 int has_capability; 225 226 int operstate; 227 228 int scan_complete_events; 229 230 struct nl_cb *nl_cb; 231 232 u8 auth_bssid[ETH_ALEN]; 233 u8 auth_attempt_bssid[ETH_ALEN]; 234 u8 bssid[ETH_ALEN]; 235 u8 prev_bssid[ETH_ALEN]; 236 int associated; 237 u8 ssid[32]; 238 size_t ssid_len; 239 enum nl80211_iftype nlmode; 240 enum nl80211_iftype ap_scan_as_station; 241 unsigned int assoc_freq; 242 243 int monitor_sock; 244 int monitor_ifidx; 245 int monitor_refcount; 246 247 unsigned int disabled_11b_rates:1; 248 unsigned int pending_remain_on_chan:1; 249 unsigned int in_interface_list:1; 250 unsigned int device_ap_sme:1; 251 unsigned int poll_command_supported:1; 252 unsigned int data_tx_status:1; 253 unsigned int scan_for_auth:1; 254 unsigned int retry_auth:1; 255 unsigned int use_monitor:1; 256 unsigned int ignore_next_local_disconnect:1; 257 unsigned int allow_p2p_device:1; 258 259 u64 remain_on_chan_cookie; 260 u64 send_action_cookie; 261 262 unsigned int last_mgmt_freq; 263 264 struct wpa_driver_scan_filter *filter_ssids; 265 size_t num_filter_ssids; 266 267 struct i802_bss first_bss; 268 269 int eapol_tx_sock; 270 271 #ifdef HOSTAPD 272 int eapol_sock; /* socket for EAPOL frames */ 273 274 int default_if_indices[16]; 275 int *if_indices; 276 int num_if_indices; 277 278 int last_freq; 279 int last_freq_ht; 280 #endif /* HOSTAPD */ 281 282 /* From failed authentication command */ 283 int auth_freq; 284 u8 auth_bssid_[ETH_ALEN]; 285 u8 auth_ssid[32]; 286 size_t auth_ssid_len; 287 int auth_alg; 288 u8 *auth_ie; 289 size_t auth_ie_len; 290 u8 auth_wep_key[4][16]; 291 size_t auth_wep_key_len[4]; 292 int auth_wep_tx_keyidx; 293 int auth_local_state_change; 294 int auth_p2p; 295 }; 296 297 298 static void wpa_driver_nl80211_deinit(struct i802_bss *bss); 299 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, 300 void *timeout_ctx); 301 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss, 302 enum nl80211_iftype nlmode); 303 static int 304 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv); 305 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv, 306 const u8 *addr, int cmd, u16 reason_code, 307 int local_state_change); 308 static void nl80211_remove_monitor_interface( 309 struct wpa_driver_nl80211_data *drv); 310 static int nl80211_send_frame_cmd(struct i802_bss *bss, 311 unsigned int freq, unsigned int wait, 312 const u8 *buf, size_t buf_len, u64 *cookie, 313 int no_cck, int no_ack, int offchanok); 314 static int nl80211_register_frame(struct i802_bss *bss, 315 struct nl_handle *hl_handle, 316 u16 type, const u8 *match, size_t match_len); 317 static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, 318 int report); 319 #ifdef ANDROID 320 static int android_pno_start(struct i802_bss *bss, 321 struct wpa_driver_scan_params *params); 322 static int android_pno_stop(struct i802_bss *bss); 323 #endif /* ANDROID */ 324 #ifdef ANDROID_P2P 325 int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration); 326 int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len); 327 int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow); 328 int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon, 329 const struct wpabuf *proberesp, 330 const struct wpabuf *assocresp); 331 332 #endif 333 #ifdef HOSTAPD 334 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); 335 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); 336 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); 337 static int wpa_driver_nl80211_if_remove(struct i802_bss *bss, 338 enum wpa_driver_if_type type, 339 const char *ifname); 340 #else /* HOSTAPD */ 341 static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 342 { 343 } 344 345 static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 346 { 347 } 348 349 static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) 350 { 351 return 0; 352 } 353 #endif /* HOSTAPD */ 354 #ifdef ANDROID 355 extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf, 356 size_t buf_len); 357 #endif 358 359 static int wpa_driver_nl80211_set_freq(struct i802_bss *bss, 360 struct hostapd_freq_params *freq); 361 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv, 362 int ifindex, int disabled); 363 364 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv); 365 static int wpa_driver_nl80211_authenticate_retry( 366 struct wpa_driver_nl80211_data *drv); 367 368 static int i802_set_iface_flags(struct i802_bss *bss, int up); 369 370 371 static const char * nl80211_command_to_string(enum nl80211_commands cmd) 372 { 373 #define C2S(x) case x: return #x; 374 switch (cmd) { 375 C2S(NL80211_CMD_UNSPEC) 376 C2S(NL80211_CMD_GET_WIPHY) 377 C2S(NL80211_CMD_SET_WIPHY) 378 C2S(NL80211_CMD_NEW_WIPHY) 379 C2S(NL80211_CMD_DEL_WIPHY) 380 C2S(NL80211_CMD_GET_INTERFACE) 381 C2S(NL80211_CMD_SET_INTERFACE) 382 C2S(NL80211_CMD_NEW_INTERFACE) 383 C2S(NL80211_CMD_DEL_INTERFACE) 384 C2S(NL80211_CMD_GET_KEY) 385 C2S(NL80211_CMD_SET_KEY) 386 C2S(NL80211_CMD_NEW_KEY) 387 C2S(NL80211_CMD_DEL_KEY) 388 C2S(NL80211_CMD_GET_BEACON) 389 C2S(NL80211_CMD_SET_BEACON) 390 C2S(NL80211_CMD_START_AP) 391 C2S(NL80211_CMD_STOP_AP) 392 C2S(NL80211_CMD_GET_STATION) 393 C2S(NL80211_CMD_SET_STATION) 394 C2S(NL80211_CMD_NEW_STATION) 395 C2S(NL80211_CMD_DEL_STATION) 396 C2S(NL80211_CMD_GET_MPATH) 397 C2S(NL80211_CMD_SET_MPATH) 398 C2S(NL80211_CMD_NEW_MPATH) 399 C2S(NL80211_CMD_DEL_MPATH) 400 C2S(NL80211_CMD_SET_BSS) 401 C2S(NL80211_CMD_SET_REG) 402 C2S(NL80211_CMD_REQ_SET_REG) 403 C2S(NL80211_CMD_GET_MESH_CONFIG) 404 C2S(NL80211_CMD_SET_MESH_CONFIG) 405 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE) 406 C2S(NL80211_CMD_GET_REG) 407 C2S(NL80211_CMD_GET_SCAN) 408 C2S(NL80211_CMD_TRIGGER_SCAN) 409 C2S(NL80211_CMD_NEW_SCAN_RESULTS) 410 C2S(NL80211_CMD_SCAN_ABORTED) 411 C2S(NL80211_CMD_REG_CHANGE) 412 C2S(NL80211_CMD_AUTHENTICATE) 413 C2S(NL80211_CMD_ASSOCIATE) 414 C2S(NL80211_CMD_DEAUTHENTICATE) 415 C2S(NL80211_CMD_DISASSOCIATE) 416 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE) 417 C2S(NL80211_CMD_REG_BEACON_HINT) 418 C2S(NL80211_CMD_JOIN_IBSS) 419 C2S(NL80211_CMD_LEAVE_IBSS) 420 C2S(NL80211_CMD_TESTMODE) 421 C2S(NL80211_CMD_CONNECT) 422 C2S(NL80211_CMD_ROAM) 423 C2S(NL80211_CMD_DISCONNECT) 424 C2S(NL80211_CMD_SET_WIPHY_NETNS) 425 C2S(NL80211_CMD_GET_SURVEY) 426 C2S(NL80211_CMD_NEW_SURVEY_RESULTS) 427 C2S(NL80211_CMD_SET_PMKSA) 428 C2S(NL80211_CMD_DEL_PMKSA) 429 C2S(NL80211_CMD_FLUSH_PMKSA) 430 C2S(NL80211_CMD_REMAIN_ON_CHANNEL) 431 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL) 432 C2S(NL80211_CMD_SET_TX_BITRATE_MASK) 433 C2S(NL80211_CMD_REGISTER_FRAME) 434 C2S(NL80211_CMD_FRAME) 435 C2S(NL80211_CMD_FRAME_TX_STATUS) 436 C2S(NL80211_CMD_SET_POWER_SAVE) 437 C2S(NL80211_CMD_GET_POWER_SAVE) 438 C2S(NL80211_CMD_SET_CQM) 439 C2S(NL80211_CMD_NOTIFY_CQM) 440 C2S(NL80211_CMD_SET_CHANNEL) 441 C2S(NL80211_CMD_SET_WDS_PEER) 442 C2S(NL80211_CMD_FRAME_WAIT_CANCEL) 443 C2S(NL80211_CMD_JOIN_MESH) 444 C2S(NL80211_CMD_LEAVE_MESH) 445 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE) 446 C2S(NL80211_CMD_UNPROT_DISASSOCIATE) 447 C2S(NL80211_CMD_NEW_PEER_CANDIDATE) 448 C2S(NL80211_CMD_GET_WOWLAN) 449 C2S(NL80211_CMD_SET_WOWLAN) 450 C2S(NL80211_CMD_START_SCHED_SCAN) 451 C2S(NL80211_CMD_STOP_SCHED_SCAN) 452 C2S(NL80211_CMD_SCHED_SCAN_RESULTS) 453 C2S(NL80211_CMD_SCHED_SCAN_STOPPED) 454 C2S(NL80211_CMD_SET_REKEY_OFFLOAD) 455 C2S(NL80211_CMD_PMKSA_CANDIDATE) 456 C2S(NL80211_CMD_TDLS_OPER) 457 C2S(NL80211_CMD_TDLS_MGMT) 458 C2S(NL80211_CMD_UNEXPECTED_FRAME) 459 C2S(NL80211_CMD_PROBE_CLIENT) 460 C2S(NL80211_CMD_REGISTER_BEACONS) 461 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME) 462 C2S(NL80211_CMD_SET_NOACK_MAP) 463 C2S(NL80211_CMD_CH_SWITCH_NOTIFY) 464 C2S(NL80211_CMD_START_P2P_DEVICE) 465 C2S(NL80211_CMD_STOP_P2P_DEVICE) 466 C2S(NL80211_CMD_CONN_FAILED) 467 C2S(NL80211_CMD_SET_MCAST_RATE) 468 C2S(NL80211_CMD_SET_MAC_ACL) 469 C2S(NL80211_CMD_RADAR_DETECT) 470 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES) 471 C2S(NL80211_CMD_UPDATE_FT_IES) 472 C2S(NL80211_CMD_FT_EVENT) 473 C2S(NL80211_CMD_CRIT_PROTOCOL_START) 474 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP) 475 default: 476 return "NL80211_CMD_UNKNOWN"; 477 } 478 #undef C2S 479 } 480 481 482 static int is_ap_interface(enum nl80211_iftype nlmode) 483 { 484 return (nlmode == NL80211_IFTYPE_AP || 485 nlmode == NL80211_IFTYPE_P2P_GO); 486 } 487 488 489 static int is_sta_interface(enum nl80211_iftype nlmode) 490 { 491 return (nlmode == NL80211_IFTYPE_STATION || 492 nlmode == NL80211_IFTYPE_P2P_CLIENT); 493 } 494 495 496 static int is_p2p_net_interface(enum nl80211_iftype nlmode) 497 { 498 return (nlmode == NL80211_IFTYPE_P2P_CLIENT || 499 nlmode == NL80211_IFTYPE_P2P_GO); 500 } 501 502 503 static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv) 504 { 505 if (drv->associated) 506 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN); 507 drv->associated = 0; 508 os_memset(drv->bssid, 0, ETH_ALEN); 509 } 510 511 512 struct nl80211_bss_info_arg { 513 struct wpa_driver_nl80211_data *drv; 514 struct wpa_scan_results *res; 515 unsigned int assoc_freq; 516 u8 assoc_bssid[ETH_ALEN]; 517 }; 518 519 static int bss_info_handler(struct nl_msg *msg, void *arg); 520 521 522 /* nl80211 code */ 523 static int ack_handler(struct nl_msg *msg, void *arg) 524 { 525 int *err = arg; 526 *err = 0; 527 return NL_STOP; 528 } 529 530 static int finish_handler(struct nl_msg *msg, void *arg) 531 { 532 int *ret = arg; 533 *ret = 0; 534 return NL_SKIP; 535 } 536 537 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, 538 void *arg) 539 { 540 int *ret = arg; 541 *ret = err->error; 542 return NL_SKIP; 543 } 544 545 546 static int no_seq_check(struct nl_msg *msg, void *arg) 547 { 548 return NL_OK; 549 } 550 551 552 static int send_and_recv(struct nl80211_global *global, 553 struct nl_handle *nl_handle, struct nl_msg *msg, 554 int (*valid_handler)(struct nl_msg *, void *), 555 void *valid_data) 556 { 557 struct nl_cb *cb; 558 int err = -ENOMEM; 559 560 cb = nl_cb_clone(global->nl_cb); 561 if (!cb) 562 goto out; 563 564 err = nl_send_auto_complete(nl_handle, msg); 565 if (err < 0) 566 goto out; 567 568 err = 1; 569 570 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); 571 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); 572 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); 573 574 if (valid_handler) 575 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, 576 valid_handler, valid_data); 577 578 while (err > 0) 579 nl_recvmsgs(nl_handle, cb); 580 out: 581 nl_cb_put(cb); 582 nlmsg_free(msg); 583 return err; 584 } 585 586 587 static int send_and_recv_msgs_global(struct nl80211_global *global, 588 struct nl_msg *msg, 589 int (*valid_handler)(struct nl_msg *, void *), 590 void *valid_data) 591 { 592 return send_and_recv(global, global->nl, msg, valid_handler, 593 valid_data); 594 } 595 596 597 #ifndef ANDROID 598 static 599 #endif 600 int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv, 601 struct nl_msg *msg, 602 int (*valid_handler)(struct nl_msg *, void *), 603 void *valid_data) 604 { 605 return send_and_recv(drv->global, drv->global->nl, msg, 606 valid_handler, valid_data); 607 } 608 609 610 struct family_data { 611 const char *group; 612 int id; 613 }; 614 615 616 static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss) 617 { 618 if (bss->wdev_id_set) 619 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id); 620 else 621 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 622 return 0; 623 624 nla_put_failure: 625 return -1; 626 } 627 628 629 static int family_handler(struct nl_msg *msg, void *arg) 630 { 631 struct family_data *res = arg; 632 struct nlattr *tb[CTRL_ATTR_MAX + 1]; 633 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 634 struct nlattr *mcgrp; 635 int i; 636 637 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 638 genlmsg_attrlen(gnlh, 0), NULL); 639 if (!tb[CTRL_ATTR_MCAST_GROUPS]) 640 return NL_SKIP; 641 642 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) { 643 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1]; 644 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp), 645 nla_len(mcgrp), NULL); 646 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || 647 !tb2[CTRL_ATTR_MCAST_GRP_ID] || 648 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]), 649 res->group, 650 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0) 651 continue; 652 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]); 653 break; 654 }; 655 656 return NL_SKIP; 657 } 658 659 660 static int nl_get_multicast_id(struct nl80211_global *global, 661 const char *family, const char *group) 662 { 663 struct nl_msg *msg; 664 int ret = -1; 665 struct family_data res = { group, -ENOENT }; 666 667 msg = nlmsg_alloc(); 668 if (!msg) 669 return -ENOMEM; 670 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"), 671 0, 0, CTRL_CMD_GETFAMILY, 0); 672 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family); 673 674 ret = send_and_recv_msgs_global(global, msg, family_handler, &res); 675 msg = NULL; 676 if (ret == 0) 677 ret = res.id; 678 679 nla_put_failure: 680 nlmsg_free(msg); 681 return ret; 682 } 683 684 685 static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv, 686 struct nl_msg *msg, int flags, uint8_t cmd) 687 { 688 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id, 689 0, flags, cmd, 0); 690 } 691 692 693 struct wiphy_idx_data { 694 int wiphy_idx; 695 enum nl80211_iftype nlmode; 696 u8 *macaddr; 697 }; 698 699 700 static int netdev_info_handler(struct nl_msg *msg, void *arg) 701 { 702 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 703 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 704 struct wiphy_idx_data *info = arg; 705 706 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 707 genlmsg_attrlen(gnlh, 0), NULL); 708 709 if (tb[NL80211_ATTR_WIPHY]) 710 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]); 711 712 if (tb[NL80211_ATTR_IFTYPE]) 713 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]); 714 715 if (tb[NL80211_ATTR_MAC] && info->macaddr) 716 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]), 717 ETH_ALEN); 718 719 return NL_SKIP; 720 } 721 722 723 static int nl80211_get_wiphy_index(struct i802_bss *bss) 724 { 725 struct nl_msg *msg; 726 struct wiphy_idx_data data = { 727 .wiphy_idx = -1, 728 .macaddr = NULL, 729 }; 730 731 msg = nlmsg_alloc(); 732 if (!msg) 733 return NL80211_IFTYPE_UNSPECIFIED; 734 735 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE); 736 737 if (nl80211_set_iface_id(msg, bss) < 0) 738 goto nla_put_failure; 739 740 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0) 741 return data.wiphy_idx; 742 msg = NULL; 743 nla_put_failure: 744 nlmsg_free(msg); 745 return -1; 746 } 747 748 749 static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss) 750 { 751 struct nl_msg *msg; 752 struct wiphy_idx_data data = { 753 .nlmode = NL80211_IFTYPE_UNSPECIFIED, 754 .macaddr = NULL, 755 }; 756 757 msg = nlmsg_alloc(); 758 if (!msg) 759 return -1; 760 761 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE); 762 763 if (nl80211_set_iface_id(msg, bss) < 0) 764 goto nla_put_failure; 765 766 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0) 767 return data.nlmode; 768 msg = NULL; 769 nla_put_failure: 770 nlmsg_free(msg); 771 return NL80211_IFTYPE_UNSPECIFIED; 772 } 773 774 775 #ifndef HOSTAPD 776 static int nl80211_get_macaddr(struct i802_bss *bss) 777 { 778 struct nl_msg *msg; 779 struct wiphy_idx_data data = { 780 .macaddr = bss->addr, 781 }; 782 783 msg = nlmsg_alloc(); 784 if (!msg) 785 return NL80211_IFTYPE_UNSPECIFIED; 786 787 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE); 788 if (nl80211_set_iface_id(msg, bss) < 0) 789 goto nla_put_failure; 790 791 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data); 792 793 nla_put_failure: 794 nlmsg_free(msg); 795 return NL80211_IFTYPE_UNSPECIFIED; 796 } 797 #endif /* HOSTAPD */ 798 799 800 static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv, 801 struct nl80211_wiphy_data *w) 802 { 803 struct nl_msg *msg; 804 int ret = -1; 805 806 msg = nlmsg_alloc(); 807 if (!msg) 808 return -1; 809 810 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS); 811 812 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx); 813 814 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL); 815 msg = NULL; 816 if (ret) { 817 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command " 818 "failed: ret=%d (%s)", 819 ret, strerror(-ret)); 820 goto nla_put_failure; 821 } 822 ret = 0; 823 nla_put_failure: 824 nlmsg_free(msg); 825 return ret; 826 } 827 828 829 static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle) 830 { 831 struct nl80211_wiphy_data *w = eloop_ctx; 832 833 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available"); 834 835 nl_recvmsgs(handle, w->nl_cb); 836 } 837 838 839 static int process_beacon_event(struct nl_msg *msg, void *arg) 840 { 841 struct nl80211_wiphy_data *w = arg; 842 struct wpa_driver_nl80211_data *drv; 843 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 844 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 845 union wpa_event_data event; 846 847 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 848 genlmsg_attrlen(gnlh, 0), NULL); 849 850 if (gnlh->cmd != NL80211_CMD_FRAME) { 851 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)", 852 gnlh->cmd); 853 return NL_SKIP; 854 } 855 856 if (!tb[NL80211_ATTR_FRAME]) 857 return NL_SKIP; 858 859 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data, 860 wiphy_list) { 861 os_memset(&event, 0, sizeof(event)); 862 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]); 863 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]); 864 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event); 865 } 866 867 return NL_SKIP; 868 } 869 870 871 static struct nl80211_wiphy_data * 872 nl80211_get_wiphy_data_ap(struct i802_bss *bss) 873 { 874 static DEFINE_DL_LIST(nl80211_wiphys); 875 struct nl80211_wiphy_data *w; 876 int wiphy_idx, found = 0; 877 struct i802_bss *tmp_bss; 878 879 if (bss->wiphy_data != NULL) 880 return bss->wiphy_data; 881 882 wiphy_idx = nl80211_get_wiphy_index(bss); 883 884 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) { 885 if (w->wiphy_idx == wiphy_idx) 886 goto add; 887 } 888 889 /* alloc new one */ 890 w = os_zalloc(sizeof(*w)); 891 if (w == NULL) 892 return NULL; 893 w->wiphy_idx = wiphy_idx; 894 dl_list_init(&w->bsss); 895 dl_list_init(&w->drvs); 896 897 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); 898 if (!w->nl_cb) { 899 os_free(w); 900 return NULL; 901 } 902 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); 903 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event, 904 w); 905 906 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb, 907 "wiphy beacons"); 908 if (w->nl_beacons == NULL) { 909 os_free(w); 910 return NULL; 911 } 912 913 if (nl80211_register_beacons(bss->drv, w)) { 914 nl_destroy_handles(&w->nl_beacons); 915 os_free(w); 916 return NULL; 917 } 918 919 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons), 920 nl80211_recv_beacons, w, w->nl_beacons); 921 922 dl_list_add(&nl80211_wiphys, &w->list); 923 924 add: 925 /* drv entry for this bss already there? */ 926 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) { 927 if (tmp_bss->drv == bss->drv) { 928 found = 1; 929 break; 930 } 931 } 932 /* if not add it */ 933 if (!found) 934 dl_list_add(&w->drvs, &bss->drv->wiphy_list); 935 936 dl_list_add(&w->bsss, &bss->wiphy_list); 937 bss->wiphy_data = w; 938 return w; 939 } 940 941 942 static void nl80211_put_wiphy_data_ap(struct i802_bss *bss) 943 { 944 struct nl80211_wiphy_data *w = bss->wiphy_data; 945 struct i802_bss *tmp_bss; 946 int found = 0; 947 948 if (w == NULL) 949 return; 950 bss->wiphy_data = NULL; 951 dl_list_del(&bss->wiphy_list); 952 953 /* still any for this drv present? */ 954 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) { 955 if (tmp_bss->drv == bss->drv) { 956 found = 1; 957 break; 958 } 959 } 960 /* if not remove it */ 961 if (!found) 962 dl_list_del(&bss->drv->wiphy_list); 963 964 if (!dl_list_empty(&w->bsss)) 965 return; 966 967 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons)); 968 969 nl_cb_put(w->nl_cb); 970 nl_destroy_handles(&w->nl_beacons); 971 dl_list_del(&w->list); 972 os_free(w); 973 } 974 975 976 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid) 977 { 978 struct i802_bss *bss = priv; 979 struct wpa_driver_nl80211_data *drv = bss->drv; 980 if (!drv->associated) 981 return -1; 982 os_memcpy(bssid, drv->bssid, ETH_ALEN); 983 return 0; 984 } 985 986 987 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid) 988 { 989 struct i802_bss *bss = priv; 990 struct wpa_driver_nl80211_data *drv = bss->drv; 991 if (!drv->associated) 992 return -1; 993 os_memcpy(ssid, drv->ssid, drv->ssid_len); 994 return drv->ssid_len; 995 } 996 997 998 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv, 999 char *buf, size_t len, int del) 1000 { 1001 union wpa_event_data event; 1002 1003 os_memset(&event, 0, sizeof(event)); 1004 if (len > sizeof(event.interface_status.ifname)) 1005 len = sizeof(event.interface_status.ifname) - 1; 1006 os_memcpy(event.interface_status.ifname, buf, len); 1007 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED : 1008 EVENT_INTERFACE_ADDED; 1009 1010 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s", 1011 del ? "DEL" : "NEW", 1012 event.interface_status.ifname, 1013 del ? "removed" : "added"); 1014 1015 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) { 1016 if (del) { 1017 if (drv->if_removed) { 1018 wpa_printf(MSG_DEBUG, "nl80211: if_removed " 1019 "already set - ignore event"); 1020 return; 1021 } 1022 drv->if_removed = 1; 1023 } else { 1024 if (if_nametoindex(drv->first_bss.ifname) == 0) { 1025 wpa_printf(MSG_DEBUG, "nl80211: Interface %s " 1026 "does not exist - ignore " 1027 "RTM_NEWLINK", 1028 drv->first_bss.ifname); 1029 return; 1030 } 1031 if (!drv->if_removed) { 1032 wpa_printf(MSG_DEBUG, "nl80211: if_removed " 1033 "already cleared - ignore event"); 1034 return; 1035 } 1036 drv->if_removed = 0; 1037 } 1038 } 1039 1040 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event); 1041 } 1042 1043 1044 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv, 1045 u8 *buf, size_t len) 1046 { 1047 int attrlen, rta_len; 1048 struct rtattr *attr; 1049 1050 attrlen = len; 1051 attr = (struct rtattr *) buf; 1052 1053 rta_len = RTA_ALIGN(sizeof(struct rtattr)); 1054 while (RTA_OK(attr, attrlen)) { 1055 if (attr->rta_type == IFLA_IFNAME) { 1056 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname) 1057 == 0) 1058 return 1; 1059 else 1060 break; 1061 } 1062 attr = RTA_NEXT(attr, attrlen); 1063 } 1064 1065 return 0; 1066 } 1067 1068 1069 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv, 1070 int ifindex, u8 *buf, size_t len) 1071 { 1072 if (drv->ifindex == ifindex) 1073 return 1; 1074 1075 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) { 1076 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed " 1077 "interface"); 1078 wpa_driver_nl80211_finish_drv_init(drv); 1079 return 1; 1080 } 1081 1082 return 0; 1083 } 1084 1085 1086 static struct wpa_driver_nl80211_data * 1087 nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len) 1088 { 1089 struct wpa_driver_nl80211_data *drv; 1090 dl_list_for_each(drv, &global->interfaces, 1091 struct wpa_driver_nl80211_data, list) { 1092 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) || 1093 have_ifidx(drv, idx)) 1094 return drv; 1095 } 1096 return NULL; 1097 } 1098 1099 1100 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx, 1101 struct ifinfomsg *ifi, 1102 u8 *buf, size_t len) 1103 { 1104 struct nl80211_global *global = ctx; 1105 struct wpa_driver_nl80211_data *drv; 1106 int attrlen, rta_len; 1107 struct rtattr *attr; 1108 u32 brid = 0; 1109 char namebuf[IFNAMSIZ]; 1110 1111 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len); 1112 if (!drv) { 1113 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign " 1114 "ifindex %d", ifi->ifi_index); 1115 return; 1116 } 1117 1118 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x " 1119 "(%s%s%s%s)", 1120 drv->operstate, ifi->ifi_flags, 1121 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "", 1122 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "", 1123 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "", 1124 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : ""); 1125 1126 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) { 1127 if (if_indextoname(ifi->ifi_index, namebuf) && 1128 linux_iface_up(drv->global->ioctl_sock, 1129 drv->first_bss.ifname) > 0) { 1130 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down " 1131 "event since interface %s is up", namebuf); 1132 return; 1133 } 1134 wpa_printf(MSG_DEBUG, "nl80211: Interface down"); 1135 if (drv->ignore_if_down_event) { 1136 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down " 1137 "event generated by mode change"); 1138 drv->ignore_if_down_event = 0; 1139 } else { 1140 drv->if_disabled = 1; 1141 wpa_supplicant_event(drv->ctx, 1142 EVENT_INTERFACE_DISABLED, NULL); 1143 } 1144 } 1145 1146 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) { 1147 if (if_indextoname(ifi->ifi_index, namebuf) && 1148 linux_iface_up(drv->global->ioctl_sock, 1149 drv->first_bss.ifname) == 0) { 1150 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " 1151 "event since interface %s is down", 1152 namebuf); 1153 } else if (if_nametoindex(drv->first_bss.ifname) == 0) { 1154 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " 1155 "event since interface %s does not exist", 1156 drv->first_bss.ifname); 1157 } else if (drv->if_removed) { 1158 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " 1159 "event since interface %s is marked " 1160 "removed", drv->first_bss.ifname); 1161 } else { 1162 wpa_printf(MSG_DEBUG, "nl80211: Interface up"); 1163 drv->if_disabled = 0; 1164 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, 1165 NULL); 1166 } 1167 } 1168 1169 /* 1170 * Some drivers send the association event before the operup event--in 1171 * this case, lifting operstate in wpa_driver_nl80211_set_operstate() 1172 * fails. This will hit us when wpa_supplicant does not need to do 1173 * IEEE 802.1X authentication 1174 */ 1175 if (drv->operstate == 1 && 1176 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP && 1177 !(ifi->ifi_flags & IFF_RUNNING)) 1178 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 1179 -1, IF_OPER_UP); 1180 1181 attrlen = len; 1182 attr = (struct rtattr *) buf; 1183 rta_len = RTA_ALIGN(sizeof(struct rtattr)); 1184 while (RTA_OK(attr, attrlen)) { 1185 if (attr->rta_type == IFLA_IFNAME) { 1186 wpa_driver_nl80211_event_link( 1187 drv, 1188 ((char *) attr) + rta_len, 1189 attr->rta_len - rta_len, 0); 1190 } else if (attr->rta_type == IFLA_MASTER) 1191 brid = nla_get_u32((struct nlattr *) attr); 1192 attr = RTA_NEXT(attr, attrlen); 1193 } 1194 1195 if (ifi->ifi_family == AF_BRIDGE && brid) { 1196 /* device has been added to bridge */ 1197 if_indextoname(brid, namebuf); 1198 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s", 1199 brid, namebuf); 1200 add_ifidx(drv, brid); 1201 } 1202 } 1203 1204 1205 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx, 1206 struct ifinfomsg *ifi, 1207 u8 *buf, size_t len) 1208 { 1209 struct nl80211_global *global = ctx; 1210 struct wpa_driver_nl80211_data *drv; 1211 int attrlen, rta_len; 1212 struct rtattr *attr; 1213 u32 brid = 0; 1214 1215 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len); 1216 if (!drv) { 1217 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for " 1218 "foreign ifindex %d", ifi->ifi_index); 1219 return; 1220 } 1221 1222 attrlen = len; 1223 attr = (struct rtattr *) buf; 1224 1225 rta_len = RTA_ALIGN(sizeof(struct rtattr)); 1226 while (RTA_OK(attr, attrlen)) { 1227 if (attr->rta_type == IFLA_IFNAME) { 1228 wpa_driver_nl80211_event_link( 1229 drv, 1230 ((char *) attr) + rta_len, 1231 attr->rta_len - rta_len, 1); 1232 } else if (attr->rta_type == IFLA_MASTER) 1233 brid = nla_get_u32((struct nlattr *) attr); 1234 attr = RTA_NEXT(attr, attrlen); 1235 } 1236 1237 if (ifi->ifi_family == AF_BRIDGE && brid) { 1238 /* device has been removed from bridge */ 1239 char namebuf[IFNAMSIZ]; 1240 if_indextoname(brid, namebuf); 1241 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge " 1242 "%s", brid, namebuf); 1243 del_ifidx(drv, brid); 1244 } 1245 } 1246 1247 1248 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv, 1249 const u8 *frame, size_t len) 1250 { 1251 const struct ieee80211_mgmt *mgmt; 1252 union wpa_event_data event; 1253 1254 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event"); 1255 mgmt = (const struct ieee80211_mgmt *) frame; 1256 if (len < 24 + sizeof(mgmt->u.auth)) { 1257 wpa_printf(MSG_DEBUG, "nl80211: Too short association event " 1258 "frame"); 1259 return; 1260 } 1261 1262 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN); 1263 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN); 1264 os_memset(&event, 0, sizeof(event)); 1265 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN); 1266 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg); 1267 event.auth.auth_transaction = 1268 le_to_host16(mgmt->u.auth.auth_transaction); 1269 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code); 1270 if (len > 24 + sizeof(mgmt->u.auth)) { 1271 event.auth.ies = mgmt->u.auth.variable; 1272 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth); 1273 } 1274 1275 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event); 1276 } 1277 1278 1279 static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv) 1280 { 1281 struct nl_msg *msg; 1282 int ret; 1283 struct nl80211_bss_info_arg arg; 1284 1285 os_memset(&arg, 0, sizeof(arg)); 1286 msg = nlmsg_alloc(); 1287 if (!msg) 1288 goto nla_put_failure; 1289 1290 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN); 1291 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 1292 1293 arg.drv = drv; 1294 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg); 1295 msg = NULL; 1296 if (ret == 0) { 1297 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the " 1298 "associated BSS from scan results: %u MHz", 1299 arg.assoc_freq); 1300 if (arg.assoc_freq) 1301 drv->assoc_freq = arg.assoc_freq; 1302 return drv->assoc_freq; 1303 } 1304 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d " 1305 "(%s)", ret, strerror(-ret)); 1306 nla_put_failure: 1307 nlmsg_free(msg); 1308 return drv->assoc_freq; 1309 } 1310 1311 1312 static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv, 1313 const u8 *frame, size_t len) 1314 { 1315 const struct ieee80211_mgmt *mgmt; 1316 union wpa_event_data event; 1317 u16 status; 1318 1319 wpa_printf(MSG_DEBUG, "nl80211: Associate event"); 1320 mgmt = (const struct ieee80211_mgmt *) frame; 1321 if (len < 24 + sizeof(mgmt->u.assoc_resp)) { 1322 wpa_printf(MSG_DEBUG, "nl80211: Too short association event " 1323 "frame"); 1324 return; 1325 } 1326 1327 status = le_to_host16(mgmt->u.assoc_resp.status_code); 1328 if (status != WLAN_STATUS_SUCCESS) { 1329 os_memset(&event, 0, sizeof(event)); 1330 event.assoc_reject.bssid = mgmt->bssid; 1331 if (len > 24 + sizeof(mgmt->u.assoc_resp)) { 1332 event.assoc_reject.resp_ies = 1333 (u8 *) mgmt->u.assoc_resp.variable; 1334 event.assoc_reject.resp_ies_len = 1335 len - 24 - sizeof(mgmt->u.assoc_resp); 1336 } 1337 event.assoc_reject.status_code = status; 1338 1339 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event); 1340 return; 1341 } 1342 1343 drv->associated = 1; 1344 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN); 1345 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN); 1346 1347 os_memset(&event, 0, sizeof(event)); 1348 if (len > 24 + sizeof(mgmt->u.assoc_resp)) { 1349 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable; 1350 event.assoc_info.resp_ies_len = 1351 len - 24 - sizeof(mgmt->u.assoc_resp); 1352 } 1353 1354 event.assoc_info.freq = drv->assoc_freq; 1355 1356 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event); 1357 } 1358 1359 1360 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv, 1361 enum nl80211_commands cmd, struct nlattr *status, 1362 struct nlattr *addr, struct nlattr *req_ie, 1363 struct nlattr *resp_ie) 1364 { 1365 union wpa_event_data event; 1366 1367 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { 1368 /* 1369 * Avoid reporting two association events that would confuse 1370 * the core code. 1371 */ 1372 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) " 1373 "when using userspace SME", cmd); 1374 return; 1375 } 1376 1377 if (cmd == NL80211_CMD_CONNECT) 1378 wpa_printf(MSG_DEBUG, "nl80211: Connect event"); 1379 else if (cmd == NL80211_CMD_ROAM) 1380 wpa_printf(MSG_DEBUG, "nl80211: Roam event"); 1381 1382 os_memset(&event, 0, sizeof(event)); 1383 if (cmd == NL80211_CMD_CONNECT && 1384 nla_get_u16(status) != WLAN_STATUS_SUCCESS) { 1385 if (addr) 1386 event.assoc_reject.bssid = nla_data(addr); 1387 if (resp_ie) { 1388 event.assoc_reject.resp_ies = nla_data(resp_ie); 1389 event.assoc_reject.resp_ies_len = nla_len(resp_ie); 1390 } 1391 event.assoc_reject.status_code = nla_get_u16(status); 1392 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event); 1393 return; 1394 } 1395 1396 drv->associated = 1; 1397 if (addr) { 1398 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN); 1399 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN); 1400 } 1401 1402 if (req_ie) { 1403 event.assoc_info.req_ies = nla_data(req_ie); 1404 event.assoc_info.req_ies_len = nla_len(req_ie); 1405 } 1406 if (resp_ie) { 1407 event.assoc_info.resp_ies = nla_data(resp_ie); 1408 event.assoc_info.resp_ies_len = nla_len(resp_ie); 1409 } 1410 1411 event.assoc_info.freq = nl80211_get_assoc_freq(drv); 1412 1413 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event); 1414 } 1415 1416 1417 static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv, 1418 struct nlattr *reason, struct nlattr *addr, 1419 struct nlattr *by_ap) 1420 { 1421 union wpa_event_data data; 1422 unsigned int locally_generated = by_ap == NULL; 1423 1424 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { 1425 /* 1426 * Avoid reporting two disassociation events that could 1427 * confuse the core code. 1428 */ 1429 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect " 1430 "event when using userspace SME"); 1431 return; 1432 } 1433 1434 if (drv->ignore_next_local_disconnect) { 1435 drv->ignore_next_local_disconnect = 0; 1436 if (locally_generated) { 1437 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect " 1438 "event triggered during reassociation"); 1439 return; 1440 } 1441 wpa_printf(MSG_WARNING, "nl80211: Was expecting local " 1442 "disconnect but got another disconnect " 1443 "event first"); 1444 } 1445 1446 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event"); 1447 nl80211_mark_disconnected(drv); 1448 os_memset(&data, 0, sizeof(data)); 1449 if (reason) 1450 data.deauth_info.reason_code = nla_get_u16(reason); 1451 data.deauth_info.locally_generated = by_ap == NULL; 1452 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data); 1453 } 1454 1455 1456 static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv, 1457 struct nlattr *freq, struct nlattr *type) 1458 { 1459 union wpa_event_data data; 1460 int ht_enabled = 1; 1461 int chan_offset = 0; 1462 1463 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event"); 1464 1465 if (!freq || !type) 1466 return; 1467 1468 switch (nla_get_u32(type)) { 1469 case NL80211_CHAN_NO_HT: 1470 ht_enabled = 0; 1471 break; 1472 case NL80211_CHAN_HT20: 1473 break; 1474 case NL80211_CHAN_HT40PLUS: 1475 chan_offset = 1; 1476 break; 1477 case NL80211_CHAN_HT40MINUS: 1478 chan_offset = -1; 1479 break; 1480 } 1481 1482 data.ch_switch.freq = nla_get_u32(freq); 1483 data.ch_switch.ht_enabled = ht_enabled; 1484 data.ch_switch.ch_offset = chan_offset; 1485 1486 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data); 1487 } 1488 1489 1490 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv, 1491 enum nl80211_commands cmd, struct nlattr *addr) 1492 { 1493 union wpa_event_data event; 1494 enum wpa_event_type ev; 1495 1496 if (nla_len(addr) != ETH_ALEN) 1497 return; 1498 1499 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR, 1500 cmd, MAC2STR((u8 *) nla_data(addr))); 1501 1502 if (cmd == NL80211_CMD_AUTHENTICATE) 1503 ev = EVENT_AUTH_TIMED_OUT; 1504 else if (cmd == NL80211_CMD_ASSOCIATE) 1505 ev = EVENT_ASSOC_TIMED_OUT; 1506 else 1507 return; 1508 1509 os_memset(&event, 0, sizeof(event)); 1510 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN); 1511 wpa_supplicant_event(drv->ctx, ev, &event); 1512 } 1513 1514 1515 static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv, 1516 struct nlattr *freq, struct nlattr *sig, 1517 const u8 *frame, size_t len) 1518 { 1519 const struct ieee80211_mgmt *mgmt; 1520 union wpa_event_data event; 1521 u16 fc, stype; 1522 int ssi_signal = 0; 1523 1524 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event"); 1525 mgmt = (const struct ieee80211_mgmt *) frame; 1526 if (len < 24) { 1527 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame"); 1528 return; 1529 } 1530 1531 fc = le_to_host16(mgmt->frame_control); 1532 stype = WLAN_FC_GET_STYPE(fc); 1533 1534 if (sig) 1535 ssi_signal = (s32) nla_get_u32(sig); 1536 1537 os_memset(&event, 0, sizeof(event)); 1538 if (freq) { 1539 event.rx_action.freq = nla_get_u32(freq); 1540 drv->last_mgmt_freq = event.rx_action.freq; 1541 } 1542 if (stype == WLAN_FC_STYPE_ACTION) { 1543 event.rx_action.da = mgmt->da; 1544 event.rx_action.sa = mgmt->sa; 1545 event.rx_action.bssid = mgmt->bssid; 1546 event.rx_action.category = mgmt->u.action.category; 1547 event.rx_action.data = &mgmt->u.action.category + 1; 1548 event.rx_action.len = frame + len - event.rx_action.data; 1549 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event); 1550 } else { 1551 event.rx_mgmt.frame = frame; 1552 event.rx_mgmt.frame_len = len; 1553 event.rx_mgmt.ssi_signal = ssi_signal; 1554 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event); 1555 } 1556 } 1557 1558 1559 static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv, 1560 struct nlattr *cookie, const u8 *frame, 1561 size_t len, struct nlattr *ack) 1562 { 1563 union wpa_event_data event; 1564 const struct ieee80211_hdr *hdr; 1565 u16 fc; 1566 1567 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event"); 1568 if (!is_ap_interface(drv->nlmode)) { 1569 u64 cookie_val; 1570 1571 if (!cookie) 1572 return; 1573 1574 cookie_val = nla_get_u64(cookie); 1575 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:" 1576 " cookie=0%llx%s (ack=%d)", 1577 (long long unsigned int) cookie_val, 1578 cookie_val == drv->send_action_cookie ? 1579 " (match)" : " (unknown)", ack != NULL); 1580 if (cookie_val != drv->send_action_cookie) 1581 return; 1582 } 1583 1584 hdr = (const struct ieee80211_hdr *) frame; 1585 fc = le_to_host16(hdr->frame_control); 1586 1587 os_memset(&event, 0, sizeof(event)); 1588 event.tx_status.type = WLAN_FC_GET_TYPE(fc); 1589 event.tx_status.stype = WLAN_FC_GET_STYPE(fc); 1590 event.tx_status.dst = hdr->addr1; 1591 event.tx_status.data = frame; 1592 event.tx_status.data_len = len; 1593 event.tx_status.ack = ack != NULL; 1594 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event); 1595 } 1596 1597 1598 static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv, 1599 enum wpa_event_type type, 1600 const u8 *frame, size_t len) 1601 { 1602 const struct ieee80211_mgmt *mgmt; 1603 union wpa_event_data event; 1604 const u8 *bssid = NULL; 1605 u16 reason_code = 0; 1606 1607 if (type == EVENT_DEAUTH) 1608 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event"); 1609 else 1610 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event"); 1611 1612 mgmt = (const struct ieee80211_mgmt *) frame; 1613 if (len >= 24) { 1614 bssid = mgmt->bssid; 1615 1616 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) && 1617 !drv->associated && 1618 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 && 1619 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 && 1620 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) { 1621 /* 1622 * Avoid issues with some roaming cases where 1623 * disconnection event for the old AP may show up after 1624 * we have started connection with the new AP. 1625 */ 1626 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR, 1627 MAC2STR(bssid), 1628 MAC2STR(drv->auth_attempt_bssid)); 1629 return; 1630 } 1631 1632 if (drv->associated != 0 && 1633 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 && 1634 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) { 1635 /* 1636 * We have presumably received this deauth as a 1637 * response to a clear_state_mismatch() outgoing 1638 * deauth. Don't let it take us offline! 1639 */ 1640 wpa_printf(MSG_DEBUG, "nl80211: Deauth received " 1641 "from Unknown BSSID " MACSTR " -- ignoring", 1642 MAC2STR(bssid)); 1643 return; 1644 } 1645 } 1646 1647 nl80211_mark_disconnected(drv); 1648 os_memset(&event, 0, sizeof(event)); 1649 1650 /* Note: Same offset for Reason Code in both frame subtypes */ 1651 if (len >= 24 + sizeof(mgmt->u.deauth)) 1652 reason_code = le_to_host16(mgmt->u.deauth.reason_code); 1653 1654 if (type == EVENT_DISASSOC) { 1655 event.disassoc_info.locally_generated = 1656 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN); 1657 event.disassoc_info.addr = bssid; 1658 event.disassoc_info.reason_code = reason_code; 1659 if (frame + len > mgmt->u.disassoc.variable) { 1660 event.disassoc_info.ie = mgmt->u.disassoc.variable; 1661 event.disassoc_info.ie_len = frame + len - 1662 mgmt->u.disassoc.variable; 1663 } 1664 } else { 1665 event.deauth_info.locally_generated = 1666 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN); 1667 event.deauth_info.addr = bssid; 1668 event.deauth_info.reason_code = reason_code; 1669 if (frame + len > mgmt->u.deauth.variable) { 1670 event.deauth_info.ie = mgmt->u.deauth.variable; 1671 event.deauth_info.ie_len = frame + len - 1672 mgmt->u.deauth.variable; 1673 } 1674 } 1675 1676 wpa_supplicant_event(drv->ctx, type, &event); 1677 } 1678 1679 1680 static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv, 1681 enum wpa_event_type type, 1682 const u8 *frame, size_t len) 1683 { 1684 const struct ieee80211_mgmt *mgmt; 1685 union wpa_event_data event; 1686 u16 reason_code = 0; 1687 1688 if (type == EVENT_UNPROT_DEAUTH) 1689 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event"); 1690 else 1691 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event"); 1692 1693 if (len < 24) 1694 return; 1695 1696 mgmt = (const struct ieee80211_mgmt *) frame; 1697 1698 os_memset(&event, 0, sizeof(event)); 1699 /* Note: Same offset for Reason Code in both frame subtypes */ 1700 if (len >= 24 + sizeof(mgmt->u.deauth)) 1701 reason_code = le_to_host16(mgmt->u.deauth.reason_code); 1702 1703 if (type == EVENT_UNPROT_DISASSOC) { 1704 event.unprot_disassoc.sa = mgmt->sa; 1705 event.unprot_disassoc.da = mgmt->da; 1706 event.unprot_disassoc.reason_code = reason_code; 1707 } else { 1708 event.unprot_deauth.sa = mgmt->sa; 1709 event.unprot_deauth.da = mgmt->da; 1710 event.unprot_deauth.reason_code = reason_code; 1711 } 1712 1713 wpa_supplicant_event(drv->ctx, type, &event); 1714 } 1715 1716 1717 static void mlme_event(struct i802_bss *bss, 1718 enum nl80211_commands cmd, struct nlattr *frame, 1719 struct nlattr *addr, struct nlattr *timed_out, 1720 struct nlattr *freq, struct nlattr *ack, 1721 struct nlattr *cookie, struct nlattr *sig) 1722 { 1723 struct wpa_driver_nl80211_data *drv = bss->drv; 1724 const u8 *data; 1725 size_t len; 1726 1727 if (timed_out && addr) { 1728 mlme_timeout_event(drv, cmd, addr); 1729 return; 1730 } 1731 1732 if (frame == NULL) { 1733 wpa_printf(MSG_DEBUG, 1734 "nl80211: MLME event %d (%s) without frame data", 1735 cmd, nl80211_command_to_string(cmd)); 1736 return; 1737 } 1738 1739 data = nla_data(frame); 1740 len = nla_len(frame); 1741 if (len < 4 + 2 * ETH_ALEN) { 1742 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" 1743 MACSTR ") - too short", 1744 cmd, nl80211_command_to_string(cmd), bss->ifname, 1745 MAC2STR(bss->addr)); 1746 return; 1747 } 1748 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR 1749 ") A1=" MACSTR " A2=" MACSTR, cmd, 1750 nl80211_command_to_string(cmd), bss->ifname, 1751 MAC2STR(bss->addr), MAC2STR(data + 4), 1752 MAC2STR(data + 4 + ETH_ALEN)); 1753 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) && 1754 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 && 1755 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) { 1756 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event " 1757 "for foreign address", bss->ifname); 1758 return; 1759 } 1760 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame", 1761 nla_data(frame), nla_len(frame)); 1762 1763 switch (cmd) { 1764 case NL80211_CMD_AUTHENTICATE: 1765 mlme_event_auth(drv, nla_data(frame), nla_len(frame)); 1766 break; 1767 case NL80211_CMD_ASSOCIATE: 1768 mlme_event_assoc(drv, nla_data(frame), nla_len(frame)); 1769 break; 1770 case NL80211_CMD_DEAUTHENTICATE: 1771 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH, 1772 nla_data(frame), nla_len(frame)); 1773 break; 1774 case NL80211_CMD_DISASSOCIATE: 1775 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC, 1776 nla_data(frame), nla_len(frame)); 1777 break; 1778 case NL80211_CMD_FRAME: 1779 mlme_event_mgmt(drv, freq, sig, nla_data(frame), 1780 nla_len(frame)); 1781 break; 1782 case NL80211_CMD_FRAME_TX_STATUS: 1783 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame), 1784 nla_len(frame), ack); 1785 break; 1786 case NL80211_CMD_UNPROT_DEAUTHENTICATE: 1787 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH, 1788 nla_data(frame), nla_len(frame)); 1789 break; 1790 case NL80211_CMD_UNPROT_DISASSOCIATE: 1791 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC, 1792 nla_data(frame), nla_len(frame)); 1793 break; 1794 default: 1795 break; 1796 } 1797 } 1798 1799 1800 static void mlme_event_michael_mic_failure(struct i802_bss *bss, 1801 struct nlattr *tb[]) 1802 { 1803 union wpa_event_data data; 1804 1805 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure"); 1806 os_memset(&data, 0, sizeof(data)); 1807 if (tb[NL80211_ATTR_MAC]) { 1808 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address", 1809 nla_data(tb[NL80211_ATTR_MAC]), 1810 nla_len(tb[NL80211_ATTR_MAC])); 1811 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]); 1812 } 1813 if (tb[NL80211_ATTR_KEY_SEQ]) { 1814 wpa_hexdump(MSG_DEBUG, "nl80211: TSC", 1815 nla_data(tb[NL80211_ATTR_KEY_SEQ]), 1816 nla_len(tb[NL80211_ATTR_KEY_SEQ])); 1817 } 1818 if (tb[NL80211_ATTR_KEY_TYPE]) { 1819 enum nl80211_key_type key_type = 1820 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]); 1821 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type); 1822 if (key_type == NL80211_KEYTYPE_PAIRWISE) 1823 data.michael_mic_failure.unicast = 1; 1824 } else 1825 data.michael_mic_failure.unicast = 1; 1826 1827 if (tb[NL80211_ATTR_KEY_IDX]) { 1828 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]); 1829 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id); 1830 } 1831 1832 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data); 1833 } 1834 1835 1836 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv, 1837 struct nlattr *tb[]) 1838 { 1839 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4); 1840 1841 if (tb[NL80211_ATTR_MAC] == NULL) { 1842 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined " 1843 "event"); 1844 return; 1845 } 1846 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 1847 1848 /* register for any AUTH message */ 1849 nl80211_register_frame(&drv->first_bss, drv->first_bss.nl_mgmt, 1850 type, NULL, 0); 1851 1852 drv->associated = 1; 1853 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined", 1854 MAC2STR(drv->bssid)); 1855 1856 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL); 1857 } 1858 1859 1860 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv, 1861 int cancel_event, struct nlattr *tb[]) 1862 { 1863 unsigned int freq, chan_type, duration; 1864 union wpa_event_data data; 1865 u64 cookie; 1866 1867 if (tb[NL80211_ATTR_WIPHY_FREQ]) 1868 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]); 1869 else 1870 freq = 0; 1871 1872 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) 1873 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]); 1874 else 1875 chan_type = 0; 1876 1877 if (tb[NL80211_ATTR_DURATION]) 1878 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]); 1879 else 1880 duration = 0; 1881 1882 if (tb[NL80211_ATTR_COOKIE]) 1883 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]); 1884 else 1885 cookie = 0; 1886 1887 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d " 1888 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))", 1889 cancel_event, freq, chan_type, duration, 1890 (long long unsigned int) cookie, 1891 cookie == drv->remain_on_chan_cookie ? "match" : "unknown"); 1892 1893 if (cookie != drv->remain_on_chan_cookie) 1894 return; /* not for us */ 1895 1896 if (cancel_event) 1897 drv->pending_remain_on_chan = 0; 1898 1899 os_memset(&data, 0, sizeof(data)); 1900 data.remain_on_channel.freq = freq; 1901 data.remain_on_channel.duration = duration; 1902 wpa_supplicant_event(drv->ctx, cancel_event ? 1903 EVENT_CANCEL_REMAIN_ON_CHANNEL : 1904 EVENT_REMAIN_ON_CHANNEL, &data); 1905 } 1906 1907 1908 static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv, 1909 struct nlattr *tb[]) 1910 { 1911 union wpa_event_data data; 1912 1913 os_memset(&data, 0, sizeof(data)); 1914 1915 if (tb[NL80211_ATTR_IE]) { 1916 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]); 1917 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]); 1918 } 1919 1920 if (tb[NL80211_ATTR_IE_RIC]) { 1921 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]); 1922 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]); 1923 } 1924 1925 if (tb[NL80211_ATTR_MAC]) 1926 os_memcpy(data.ft_ies.target_ap, 1927 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 1928 1929 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR, 1930 MAC2STR(data.ft_ies.target_ap)); 1931 1932 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data); 1933 } 1934 1935 1936 static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted, 1937 struct nlattr *tb[]) 1938 { 1939 union wpa_event_data event; 1940 struct nlattr *nl; 1941 int rem; 1942 struct scan_info *info; 1943 #define MAX_REPORT_FREQS 50 1944 int freqs[MAX_REPORT_FREQS]; 1945 int num_freqs = 0; 1946 1947 if (drv->scan_for_auth) { 1948 drv->scan_for_auth = 0; 1949 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing " 1950 "cfg80211 BSS entry"); 1951 wpa_driver_nl80211_authenticate_retry(drv); 1952 return; 1953 } 1954 1955 os_memset(&event, 0, sizeof(event)); 1956 info = &event.scan_info; 1957 info->aborted = aborted; 1958 1959 if (tb[NL80211_ATTR_SCAN_SSIDS]) { 1960 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) { 1961 struct wpa_driver_scan_ssid *s = 1962 &info->ssids[info->num_ssids]; 1963 s->ssid = nla_data(nl); 1964 s->ssid_len = nla_len(nl); 1965 info->num_ssids++; 1966 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS) 1967 break; 1968 } 1969 } 1970 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) { 1971 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem) 1972 { 1973 freqs[num_freqs] = nla_get_u32(nl); 1974 num_freqs++; 1975 if (num_freqs == MAX_REPORT_FREQS - 1) 1976 break; 1977 } 1978 info->freqs = freqs; 1979 info->num_freqs = num_freqs; 1980 } 1981 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event); 1982 } 1983 1984 1985 static int get_link_signal(struct nl_msg *msg, void *arg) 1986 { 1987 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 1988 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 1989 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; 1990 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = { 1991 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, 1992 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 }, 1993 }; 1994 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; 1995 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { 1996 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, 1997 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, 1998 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, 1999 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, 2000 }; 2001 struct wpa_signal_info *sig_change = arg; 2002 2003 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2004 genlmsg_attrlen(gnlh, 0), NULL); 2005 if (!tb[NL80211_ATTR_STA_INFO] || 2006 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, 2007 tb[NL80211_ATTR_STA_INFO], policy)) 2008 return NL_SKIP; 2009 if (!sinfo[NL80211_STA_INFO_SIGNAL]) 2010 return NL_SKIP; 2011 2012 sig_change->current_signal = 2013 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); 2014 2015 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG]) 2016 sig_change->avg_signal = 2017 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]); 2018 else 2019 sig_change->avg_signal = 0; 2020 2021 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) { 2022 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, 2023 sinfo[NL80211_STA_INFO_TX_BITRATE], 2024 rate_policy)) { 2025 sig_change->current_txrate = 0; 2026 } else { 2027 if (rinfo[NL80211_RATE_INFO_BITRATE]) { 2028 sig_change->current_txrate = 2029 nla_get_u16(rinfo[ 2030 NL80211_RATE_INFO_BITRATE]) * 100; 2031 } 2032 } 2033 } 2034 2035 return NL_SKIP; 2036 } 2037 2038 2039 static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv, 2040 struct wpa_signal_info *sig) 2041 { 2042 struct nl_msg *msg; 2043 2044 sig->current_signal = -9999; 2045 sig->current_txrate = 0; 2046 2047 msg = nlmsg_alloc(); 2048 if (!msg) 2049 return -ENOMEM; 2050 2051 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION); 2052 2053 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 2054 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid); 2055 2056 return send_and_recv_msgs(drv, msg, get_link_signal, sig); 2057 nla_put_failure: 2058 nlmsg_free(msg); 2059 return -ENOBUFS; 2060 } 2061 2062 2063 static int get_link_noise(struct nl_msg *msg, void *arg) 2064 { 2065 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 2066 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2067 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; 2068 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { 2069 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, 2070 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, 2071 }; 2072 struct wpa_signal_info *sig_change = arg; 2073 2074 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2075 genlmsg_attrlen(gnlh, 0), NULL); 2076 2077 if (!tb[NL80211_ATTR_SURVEY_INFO]) { 2078 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!"); 2079 return NL_SKIP; 2080 } 2081 2082 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, 2083 tb[NL80211_ATTR_SURVEY_INFO], 2084 survey_policy)) { 2085 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested " 2086 "attributes!"); 2087 return NL_SKIP; 2088 } 2089 2090 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) 2091 return NL_SKIP; 2092 2093 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) != 2094 sig_change->frequency) 2095 return NL_SKIP; 2096 2097 if (!sinfo[NL80211_SURVEY_INFO_NOISE]) 2098 return NL_SKIP; 2099 2100 sig_change->current_noise = 2101 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); 2102 2103 return NL_SKIP; 2104 } 2105 2106 2107 static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv, 2108 struct wpa_signal_info *sig_change) 2109 { 2110 struct nl_msg *msg; 2111 2112 sig_change->current_noise = 9999; 2113 sig_change->frequency = drv->assoc_freq; 2114 2115 msg = nlmsg_alloc(); 2116 if (!msg) 2117 return -ENOMEM; 2118 2119 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY); 2120 2121 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 2122 2123 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change); 2124 nla_put_failure: 2125 nlmsg_free(msg); 2126 return -ENOBUFS; 2127 } 2128 2129 2130 static int get_noise_for_scan_results(struct nl_msg *msg, void *arg) 2131 { 2132 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 2133 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2134 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; 2135 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { 2136 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, 2137 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, 2138 }; 2139 struct wpa_scan_results *scan_results = arg; 2140 struct wpa_scan_res *scan_res; 2141 size_t i; 2142 2143 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2144 genlmsg_attrlen(gnlh, 0), NULL); 2145 2146 if (!tb[NL80211_ATTR_SURVEY_INFO]) { 2147 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing"); 2148 return NL_SKIP; 2149 } 2150 2151 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, 2152 tb[NL80211_ATTR_SURVEY_INFO], 2153 survey_policy)) { 2154 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested " 2155 "attributes"); 2156 return NL_SKIP; 2157 } 2158 2159 if (!sinfo[NL80211_SURVEY_INFO_NOISE]) 2160 return NL_SKIP; 2161 2162 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) 2163 return NL_SKIP; 2164 2165 for (i = 0; i < scan_results->num; ++i) { 2166 scan_res = scan_results->res[i]; 2167 if (!scan_res) 2168 continue; 2169 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) != 2170 scan_res->freq) 2171 continue; 2172 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID)) 2173 continue; 2174 scan_res->noise = (s8) 2175 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); 2176 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID; 2177 } 2178 2179 return NL_SKIP; 2180 } 2181 2182 2183 static int nl80211_get_noise_for_scan_results( 2184 struct wpa_driver_nl80211_data *drv, 2185 struct wpa_scan_results *scan_res) 2186 { 2187 struct nl_msg *msg; 2188 2189 msg = nlmsg_alloc(); 2190 if (!msg) 2191 return -ENOMEM; 2192 2193 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY); 2194 2195 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 2196 2197 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results, 2198 scan_res); 2199 nla_put_failure: 2200 nlmsg_free(msg); 2201 return -ENOBUFS; 2202 } 2203 2204 2205 static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv, 2206 struct nlattr *tb[]) 2207 { 2208 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = { 2209 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 }, 2210 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 }, 2211 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 }, 2212 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 }, 2213 }; 2214 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1]; 2215 enum nl80211_cqm_rssi_threshold_event event; 2216 union wpa_event_data ed; 2217 struct wpa_signal_info sig; 2218 int res; 2219 2220 if (tb[NL80211_ATTR_CQM] == NULL || 2221 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM], 2222 cqm_policy)) { 2223 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event"); 2224 return; 2225 } 2226 2227 os_memset(&ed, 0, sizeof(ed)); 2228 2229 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) { 2230 if (!tb[NL80211_ATTR_MAC]) 2231 return; 2232 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]), 2233 ETH_ALEN); 2234 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed); 2235 return; 2236 } 2237 2238 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL) 2239 return; 2240 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]); 2241 2242 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) { 2243 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor " 2244 "event: RSSI high"); 2245 ed.signal_change.above_threshold = 1; 2246 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) { 2247 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor " 2248 "event: RSSI low"); 2249 ed.signal_change.above_threshold = 0; 2250 } else 2251 return; 2252 2253 res = nl80211_get_link_signal(drv, &sig); 2254 if (res == 0) { 2255 ed.signal_change.current_signal = sig.current_signal; 2256 ed.signal_change.current_txrate = sig.current_txrate; 2257 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d", 2258 sig.current_signal, sig.current_txrate); 2259 } 2260 2261 res = nl80211_get_link_noise(drv, &sig); 2262 if (res == 0) { 2263 ed.signal_change.current_noise = sig.current_noise; 2264 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm", 2265 sig.current_noise); 2266 } 2267 2268 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed); 2269 } 2270 2271 2272 static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv, 2273 struct nlattr **tb) 2274 { 2275 u8 *addr; 2276 union wpa_event_data data; 2277 2278 if (tb[NL80211_ATTR_MAC] == NULL) 2279 return; 2280 addr = nla_data(tb[NL80211_ATTR_MAC]); 2281 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr)); 2282 2283 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) { 2284 u8 *ies = NULL; 2285 size_t ies_len = 0; 2286 if (tb[NL80211_ATTR_IE]) { 2287 ies = nla_data(tb[NL80211_ATTR_IE]); 2288 ies_len = nla_len(tb[NL80211_ATTR_IE]); 2289 } 2290 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len); 2291 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0); 2292 return; 2293 } 2294 2295 if (drv->nlmode != NL80211_IFTYPE_ADHOC) 2296 return; 2297 2298 os_memset(&data, 0, sizeof(data)); 2299 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN); 2300 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data); 2301 } 2302 2303 2304 static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv, 2305 struct nlattr **tb) 2306 { 2307 u8 *addr; 2308 union wpa_event_data data; 2309 2310 if (tb[NL80211_ATTR_MAC] == NULL) 2311 return; 2312 addr = nla_data(tb[NL80211_ATTR_MAC]); 2313 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR, 2314 MAC2STR(addr)); 2315 2316 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) { 2317 drv_event_disassoc(drv->ctx, addr); 2318 return; 2319 } 2320 2321 if (drv->nlmode != NL80211_IFTYPE_ADHOC) 2322 return; 2323 2324 os_memset(&data, 0, sizeof(data)); 2325 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN); 2326 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data); 2327 } 2328 2329 2330 static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv, 2331 struct nlattr **tb) 2332 { 2333 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA]; 2334 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = { 2335 [NL80211_REKEY_DATA_KEK] = { 2336 .minlen = NL80211_KEK_LEN, 2337 .maxlen = NL80211_KEK_LEN, 2338 }, 2339 [NL80211_REKEY_DATA_KCK] = { 2340 .minlen = NL80211_KCK_LEN, 2341 .maxlen = NL80211_KCK_LEN, 2342 }, 2343 [NL80211_REKEY_DATA_REPLAY_CTR] = { 2344 .minlen = NL80211_REPLAY_CTR_LEN, 2345 .maxlen = NL80211_REPLAY_CTR_LEN, 2346 }, 2347 }; 2348 union wpa_event_data data; 2349 2350 if (!tb[NL80211_ATTR_MAC]) 2351 return; 2352 if (!tb[NL80211_ATTR_REKEY_DATA]) 2353 return; 2354 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA, 2355 tb[NL80211_ATTR_REKEY_DATA], rekey_policy)) 2356 return; 2357 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]) 2358 return; 2359 2360 os_memset(&data, 0, sizeof(data)); 2361 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]); 2362 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR, 2363 MAC2STR(data.driver_gtk_rekey.bssid)); 2364 data.driver_gtk_rekey.replay_ctr = 2365 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]); 2366 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter", 2367 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN); 2368 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data); 2369 } 2370 2371 2372 static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv, 2373 struct nlattr **tb) 2374 { 2375 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE]; 2376 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = { 2377 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 }, 2378 [NL80211_PMKSA_CANDIDATE_BSSID] = { 2379 .minlen = ETH_ALEN, 2380 .maxlen = ETH_ALEN, 2381 }, 2382 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG }, 2383 }; 2384 union wpa_event_data data; 2385 2386 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event"); 2387 2388 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE]) 2389 return; 2390 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE, 2391 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy)) 2392 return; 2393 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] || 2394 !cand[NL80211_PMKSA_CANDIDATE_BSSID]) 2395 return; 2396 2397 os_memset(&data, 0, sizeof(data)); 2398 os_memcpy(data.pmkid_candidate.bssid, 2399 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN); 2400 data.pmkid_candidate.index = 2401 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]); 2402 data.pmkid_candidate.preauth = 2403 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL; 2404 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data); 2405 } 2406 2407 2408 static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv, 2409 struct nlattr **tb) 2410 { 2411 union wpa_event_data data; 2412 2413 wpa_printf(MSG_DEBUG, "nl80211: Probe client event"); 2414 2415 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK]) 2416 return; 2417 2418 os_memset(&data, 0, sizeof(data)); 2419 os_memcpy(data.client_poll.addr, 2420 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 2421 2422 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data); 2423 } 2424 2425 2426 static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv, 2427 struct nlattr **tb) 2428 { 2429 union wpa_event_data data; 2430 2431 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event"); 2432 2433 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION]) 2434 return; 2435 2436 os_memset(&data, 0, sizeof(data)); 2437 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 2438 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) { 2439 case NL80211_TDLS_SETUP: 2440 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer " 2441 MACSTR, MAC2STR(data.tdls.peer)); 2442 data.tdls.oper = TDLS_REQUEST_SETUP; 2443 break; 2444 case NL80211_TDLS_TEARDOWN: 2445 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer " 2446 MACSTR, MAC2STR(data.tdls.peer)); 2447 data.tdls.oper = TDLS_REQUEST_TEARDOWN; 2448 break; 2449 default: 2450 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione " 2451 "event"); 2452 return; 2453 } 2454 if (tb[NL80211_ATTR_REASON_CODE]) { 2455 data.tdls.reason_code = 2456 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]); 2457 } 2458 2459 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data); 2460 } 2461 2462 2463 static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv, 2464 struct nlattr **tb) 2465 { 2466 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL); 2467 } 2468 2469 2470 static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv, 2471 struct nlattr **tb) 2472 { 2473 union wpa_event_data data; 2474 u32 reason; 2475 2476 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event"); 2477 2478 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON]) 2479 return; 2480 2481 os_memset(&data, 0, sizeof(data)); 2482 os_memcpy(data.connect_failed_reason.addr, 2483 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); 2484 2485 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]); 2486 switch (reason) { 2487 case NL80211_CONN_FAIL_MAX_CLIENTS: 2488 wpa_printf(MSG_DEBUG, "nl80211: Max client reached"); 2489 data.connect_failed_reason.code = MAX_CLIENT_REACHED; 2490 break; 2491 case NL80211_CONN_FAIL_BLOCKED_CLIENT: 2492 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR 2493 " tried to connect", 2494 MAC2STR(data.connect_failed_reason.addr)); 2495 data.connect_failed_reason.code = BLOCKED_CLIENT; 2496 break; 2497 default: 2498 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason " 2499 "%u", reason); 2500 return; 2501 } 2502 2503 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data); 2504 } 2505 2506 2507 static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv, 2508 struct nlattr **tb) 2509 { 2510 union wpa_event_data data; 2511 enum nl80211_radar_event event_type; 2512 2513 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT]) 2514 return; 2515 2516 os_memset(&data, 0, sizeof(data)); 2517 data.dfs_event.freq = nla_get_u16(tb[NL80211_ATTR_WIPHY_FREQ]); 2518 event_type = nla_get_u8(tb[NL80211_ATTR_RADAR_EVENT]); 2519 2520 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz", 2521 data.dfs_event.freq); 2522 2523 switch (event_type) { 2524 case NL80211_RADAR_DETECTED: 2525 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data); 2526 break; 2527 case NL80211_RADAR_CAC_FINISHED: 2528 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data); 2529 break; 2530 case NL80211_RADAR_CAC_ABORTED: 2531 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data); 2532 break; 2533 case NL80211_RADAR_NOP_FINISHED: 2534 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data); 2535 break; 2536 default: 2537 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d " 2538 "received", event_type); 2539 break; 2540 } 2541 } 2542 2543 2544 static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb, 2545 int wds) 2546 { 2547 struct wpa_driver_nl80211_data *drv = bss->drv; 2548 union wpa_event_data event; 2549 2550 if (!tb[NL80211_ATTR_MAC]) 2551 return; 2552 2553 os_memset(&event, 0, sizeof(event)); 2554 event.rx_from_unknown.bssid = bss->addr; 2555 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]); 2556 event.rx_from_unknown.wds = wds; 2557 2558 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event); 2559 } 2560 2561 2562 static void do_process_drv_event(struct i802_bss *bss, int cmd, 2563 struct nlattr **tb) 2564 { 2565 struct wpa_driver_nl80211_data *drv = bss->drv; 2566 2567 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s", 2568 cmd, nl80211_command_to_string(cmd), bss->ifname); 2569 2570 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED && 2571 (cmd == NL80211_CMD_NEW_SCAN_RESULTS || 2572 cmd == NL80211_CMD_SCAN_ABORTED)) { 2573 wpa_driver_nl80211_set_mode(&drv->first_bss, 2574 drv->ap_scan_as_station); 2575 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED; 2576 } 2577 2578 switch (cmd) { 2579 case NL80211_CMD_TRIGGER_SCAN: 2580 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger"); 2581 break; 2582 case NL80211_CMD_START_SCHED_SCAN: 2583 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started"); 2584 break; 2585 case NL80211_CMD_SCHED_SCAN_STOPPED: 2586 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped"); 2587 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL); 2588 break; 2589 case NL80211_CMD_NEW_SCAN_RESULTS: 2590 wpa_dbg(drv->ctx, MSG_DEBUG, 2591 "nl80211: New scan results available"); 2592 drv->scan_complete_events = 1; 2593 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, 2594 drv->ctx); 2595 send_scan_event(drv, 0, tb); 2596 break; 2597 case NL80211_CMD_SCHED_SCAN_RESULTS: 2598 wpa_dbg(drv->ctx, MSG_DEBUG, 2599 "nl80211: New sched scan results available"); 2600 send_scan_event(drv, 0, tb); 2601 break; 2602 case NL80211_CMD_SCAN_ABORTED: 2603 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted"); 2604 /* 2605 * Need to indicate that scan results are available in order 2606 * not to make wpa_supplicant stop its scanning. 2607 */ 2608 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, 2609 drv->ctx); 2610 send_scan_event(drv, 1, tb); 2611 break; 2612 case NL80211_CMD_AUTHENTICATE: 2613 case NL80211_CMD_ASSOCIATE: 2614 case NL80211_CMD_DEAUTHENTICATE: 2615 case NL80211_CMD_DISASSOCIATE: 2616 case NL80211_CMD_FRAME_TX_STATUS: 2617 case NL80211_CMD_UNPROT_DEAUTHENTICATE: 2618 case NL80211_CMD_UNPROT_DISASSOCIATE: 2619 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME], 2620 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT], 2621 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK], 2622 tb[NL80211_ATTR_COOKIE], 2623 tb[NL80211_ATTR_RX_SIGNAL_DBM]); 2624 break; 2625 case NL80211_CMD_CONNECT: 2626 case NL80211_CMD_ROAM: 2627 mlme_event_connect(drv, cmd, 2628 tb[NL80211_ATTR_STATUS_CODE], 2629 tb[NL80211_ATTR_MAC], 2630 tb[NL80211_ATTR_REQ_IE], 2631 tb[NL80211_ATTR_RESP_IE]); 2632 break; 2633 case NL80211_CMD_CH_SWITCH_NOTIFY: 2634 mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ], 2635 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]); 2636 break; 2637 case NL80211_CMD_DISCONNECT: 2638 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE], 2639 tb[NL80211_ATTR_MAC], 2640 tb[NL80211_ATTR_DISCONNECTED_BY_AP]); 2641 break; 2642 case NL80211_CMD_MICHAEL_MIC_FAILURE: 2643 mlme_event_michael_mic_failure(bss, tb); 2644 break; 2645 case NL80211_CMD_JOIN_IBSS: 2646 mlme_event_join_ibss(drv, tb); 2647 break; 2648 case NL80211_CMD_REMAIN_ON_CHANNEL: 2649 mlme_event_remain_on_channel(drv, 0, tb); 2650 break; 2651 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL: 2652 mlme_event_remain_on_channel(drv, 1, tb); 2653 break; 2654 case NL80211_CMD_NOTIFY_CQM: 2655 nl80211_cqm_event(drv, tb); 2656 break; 2657 case NL80211_CMD_REG_CHANGE: 2658 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change"); 2659 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, 2660 NULL); 2661 break; 2662 case NL80211_CMD_REG_BEACON_HINT: 2663 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint"); 2664 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, 2665 NULL); 2666 break; 2667 case NL80211_CMD_NEW_STATION: 2668 nl80211_new_station_event(drv, tb); 2669 break; 2670 case NL80211_CMD_DEL_STATION: 2671 nl80211_del_station_event(drv, tb); 2672 break; 2673 case NL80211_CMD_SET_REKEY_OFFLOAD: 2674 nl80211_rekey_offload_event(drv, tb); 2675 break; 2676 case NL80211_CMD_PMKSA_CANDIDATE: 2677 nl80211_pmksa_candidate_event(drv, tb); 2678 break; 2679 case NL80211_CMD_PROBE_CLIENT: 2680 nl80211_client_probe_event(drv, tb); 2681 break; 2682 case NL80211_CMD_TDLS_OPER: 2683 nl80211_tdls_oper_event(drv, tb); 2684 break; 2685 case NL80211_CMD_CONN_FAILED: 2686 nl80211_connect_failed_event(drv, tb); 2687 break; 2688 case NL80211_CMD_FT_EVENT: 2689 mlme_event_ft_event(drv, tb); 2690 break; 2691 case NL80211_CMD_RADAR_DETECT: 2692 nl80211_radar_event(drv, tb); 2693 break; 2694 case NL80211_CMD_STOP_AP: 2695 nl80211_stop_ap(drv, tb); 2696 break; 2697 default: 2698 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event " 2699 "(cmd=%d)", cmd); 2700 break; 2701 } 2702 } 2703 2704 2705 static int process_drv_event(struct nl_msg *msg, void *arg) 2706 { 2707 struct wpa_driver_nl80211_data *drv = arg; 2708 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2709 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 2710 struct i802_bss *bss; 2711 int ifidx = -1; 2712 2713 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2714 genlmsg_attrlen(gnlh, 0), NULL); 2715 2716 if (tb[NL80211_ATTR_IFINDEX]) { 2717 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]); 2718 2719 for (bss = &drv->first_bss; bss; bss = bss->next) 2720 if (ifidx == -1 || ifidx == bss->ifindex) { 2721 do_process_drv_event(bss, gnlh->cmd, tb); 2722 return NL_SKIP; 2723 } 2724 wpa_printf(MSG_DEBUG, 2725 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)", 2726 gnlh->cmd, ifidx); 2727 } else if (tb[NL80211_ATTR_WDEV]) { 2728 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]); 2729 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device"); 2730 for (bss = &drv->first_bss; bss; bss = bss->next) { 2731 if (bss->wdev_id_set && wdev_id == bss->wdev_id) { 2732 do_process_drv_event(bss, gnlh->cmd, tb); 2733 return NL_SKIP; 2734 } 2735 } 2736 wpa_printf(MSG_DEBUG, 2737 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)", 2738 gnlh->cmd, (long long unsigned int) wdev_id); 2739 } 2740 2741 return NL_SKIP; 2742 } 2743 2744 2745 static int process_global_event(struct nl_msg *msg, void *arg) 2746 { 2747 struct nl80211_global *global = arg; 2748 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2749 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 2750 struct wpa_driver_nl80211_data *drv, *tmp; 2751 int ifidx = -1; 2752 struct i802_bss *bss; 2753 u64 wdev_id = 0; 2754 int wdev_id_set = 0; 2755 2756 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2757 genlmsg_attrlen(gnlh, 0), NULL); 2758 2759 if (tb[NL80211_ATTR_IFINDEX]) 2760 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]); 2761 else if (tb[NL80211_ATTR_WDEV]) { 2762 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]); 2763 wdev_id_set = 1; 2764 } 2765 2766 dl_list_for_each_safe(drv, tmp, &global->interfaces, 2767 struct wpa_driver_nl80211_data, list) { 2768 for (bss = &drv->first_bss; bss; bss = bss->next) { 2769 if ((ifidx == -1 && !wdev_id_set) || 2770 ifidx == bss->ifindex || 2771 (wdev_id_set && bss->wdev_id_set && 2772 wdev_id == bss->wdev_id)) { 2773 do_process_drv_event(bss, gnlh->cmd, tb); 2774 return NL_SKIP; 2775 } 2776 } 2777 } 2778 2779 return NL_SKIP; 2780 } 2781 2782 2783 static int process_bss_event(struct nl_msg *msg, void *arg) 2784 { 2785 struct i802_bss *bss = arg; 2786 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2787 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 2788 2789 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2790 genlmsg_attrlen(gnlh, 0), NULL); 2791 2792 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s", 2793 gnlh->cmd, nl80211_command_to_string(gnlh->cmd), 2794 bss->ifname); 2795 2796 switch (gnlh->cmd) { 2797 case NL80211_CMD_FRAME: 2798 case NL80211_CMD_FRAME_TX_STATUS: 2799 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME], 2800 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT], 2801 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK], 2802 tb[NL80211_ATTR_COOKIE], 2803 tb[NL80211_ATTR_RX_SIGNAL_DBM]); 2804 break; 2805 case NL80211_CMD_UNEXPECTED_FRAME: 2806 nl80211_spurious_frame(bss, tb, 0); 2807 break; 2808 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME: 2809 nl80211_spurious_frame(bss, tb, 1); 2810 break; 2811 default: 2812 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event " 2813 "(cmd=%d)", gnlh->cmd); 2814 break; 2815 } 2816 2817 return NL_SKIP; 2818 } 2819 2820 2821 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx, 2822 void *handle) 2823 { 2824 struct nl_cb *cb = eloop_ctx; 2825 2826 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available"); 2827 2828 nl_recvmsgs(handle, cb); 2829 } 2830 2831 2832 /** 2833 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain 2834 * @priv: driver_nl80211 private data 2835 * @alpha2_arg: country to which to switch to 2836 * Returns: 0 on success, -1 on failure 2837 * 2838 * This asks nl80211 to set the regulatory domain for given 2839 * country ISO / IEC alpha2. 2840 */ 2841 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg) 2842 { 2843 struct i802_bss *bss = priv; 2844 struct wpa_driver_nl80211_data *drv = bss->drv; 2845 char alpha2[3]; 2846 struct nl_msg *msg; 2847 2848 msg = nlmsg_alloc(); 2849 if (!msg) 2850 return -ENOMEM; 2851 2852 alpha2[0] = alpha2_arg[0]; 2853 alpha2[1] = alpha2_arg[1]; 2854 alpha2[2] = '\0'; 2855 2856 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG); 2857 2858 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2); 2859 if (send_and_recv_msgs(drv, msg, NULL, NULL)) 2860 return -EINVAL; 2861 return 0; 2862 nla_put_failure: 2863 nlmsg_free(msg); 2864 return -EINVAL; 2865 } 2866 2867 2868 static int protocol_feature_handler(struct nl_msg *msg, void *arg) 2869 { 2870 u32 *feat = arg; 2871 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; 2872 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 2873 2874 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 2875 genlmsg_attrlen(gnlh, 0), NULL); 2876 2877 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]) 2878 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]); 2879 2880 return NL_SKIP; 2881 } 2882 2883 2884 static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv) 2885 { 2886 u32 feat = 0; 2887 struct nl_msg *msg; 2888 2889 msg = nlmsg_alloc(); 2890 if (!msg) 2891 goto nla_put_failure; 2892 2893 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES); 2894 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0) 2895 return feat; 2896 2897 msg = NULL; 2898 nla_put_failure: 2899 nlmsg_free(msg); 2900 return 0; 2901 } 2902 2903 2904 struct wiphy_info_data { 2905 struct wpa_driver_nl80211_data *drv; 2906 struct wpa_driver_capa *capa; 2907 2908 unsigned int num_multichan_concurrent; 2909 2910 unsigned int error:1; 2911 unsigned int device_ap_sme:1; 2912 unsigned int poll_command_supported:1; 2913 unsigned int data_tx_status:1; 2914 unsigned int monitor_supported:1; 2915 unsigned int auth_supported:1; 2916 unsigned int connect_supported:1; 2917 unsigned int p2p_go_supported:1; 2918 unsigned int p2p_client_supported:1; 2919 unsigned int p2p_concurrent:1; 2920 }; 2921 2922 2923 static unsigned int probe_resp_offload_support(int supp_protocols) 2924 { 2925 unsigned int prot = 0; 2926 2927 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS) 2928 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS; 2929 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2) 2930 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2; 2931 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P) 2932 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P; 2933 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U) 2934 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING; 2935 2936 return prot; 2937 } 2938 2939 2940 static void wiphy_info_supported_iftypes(struct wiphy_info_data *info, 2941 struct nlattr *tb) 2942 { 2943 struct nlattr *nl_mode; 2944 int i; 2945 2946 if (tb == NULL) 2947 return; 2948 2949 nla_for_each_nested(nl_mode, tb, i) { 2950 switch (nla_type(nl_mode)) { 2951 case NL80211_IFTYPE_AP: 2952 info->capa->flags |= WPA_DRIVER_FLAGS_AP; 2953 break; 2954 case NL80211_IFTYPE_ADHOC: 2955 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS; 2956 break; 2957 case NL80211_IFTYPE_P2P_DEVICE: 2958 info->capa->flags |= 2959 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE; 2960 break; 2961 case NL80211_IFTYPE_P2P_GO: 2962 info->p2p_go_supported = 1; 2963 break; 2964 case NL80211_IFTYPE_P2P_CLIENT: 2965 info->p2p_client_supported = 1; 2966 break; 2967 case NL80211_IFTYPE_MONITOR: 2968 info->monitor_supported = 1; 2969 break; 2970 } 2971 } 2972 } 2973 2974 2975 static int wiphy_info_iface_comb_process(struct wiphy_info_data *info, 2976 struct nlattr *nl_combi) 2977 { 2978 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB]; 2979 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT]; 2980 struct nlattr *nl_limit, *nl_mode; 2981 int err, rem_limit, rem_mode; 2982 int combination_has_p2p = 0, combination_has_mgd = 0; 2983 static struct nla_policy 2984 iface_combination_policy[NUM_NL80211_IFACE_COMB] = { 2985 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED }, 2986 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 }, 2987 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG }, 2988 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 }, 2989 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 }, 2990 }, 2991 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = { 2992 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED }, 2993 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 }, 2994 }; 2995 2996 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB, 2997 nl_combi, iface_combination_policy); 2998 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] || 2999 !tb_comb[NL80211_IFACE_COMB_MAXNUM] || 3000 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) 3001 return 0; /* broken combination */ 3002 3003 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS]) 3004 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR; 3005 3006 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS], 3007 rem_limit) { 3008 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT, 3009 nl_limit, iface_limit_policy); 3010 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES]) 3011 return 0; /* broken combination */ 3012 3013 nla_for_each_nested(nl_mode, 3014 tb_limit[NL80211_IFACE_LIMIT_TYPES], 3015 rem_mode) { 3016 int ift = nla_type(nl_mode); 3017 if (ift == NL80211_IFTYPE_P2P_GO || 3018 ift == NL80211_IFTYPE_P2P_CLIENT) 3019 combination_has_p2p = 1; 3020 if (ift == NL80211_IFTYPE_STATION) 3021 combination_has_mgd = 1; 3022 } 3023 if (combination_has_p2p && combination_has_mgd) 3024 break; 3025 } 3026 3027 if (combination_has_p2p && combination_has_mgd) { 3028 info->p2p_concurrent = 1; 3029 info->num_multichan_concurrent = 3030 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]); 3031 return 1; 3032 } 3033 3034 return 0; 3035 } 3036 3037 3038 static void wiphy_info_iface_comb(struct wiphy_info_data *info, 3039 struct nlattr *tb) 3040 { 3041 struct nlattr *nl_combi; 3042 int rem_combi; 3043 3044 if (tb == NULL) 3045 return; 3046 3047 nla_for_each_nested(nl_combi, tb, rem_combi) { 3048 if (wiphy_info_iface_comb_process(info, nl_combi) > 0) 3049 break; 3050 } 3051 } 3052 3053 3054 static void wiphy_info_supp_cmds(struct wiphy_info_data *info, 3055 struct nlattr *tb) 3056 { 3057 struct nlattr *nl_cmd; 3058 int i; 3059 3060 if (tb == NULL) 3061 return; 3062 3063 nla_for_each_nested(nl_cmd, tb, i) { 3064 switch (nla_get_u32(nl_cmd)) { 3065 case NL80211_CMD_AUTHENTICATE: 3066 info->auth_supported = 1; 3067 break; 3068 case NL80211_CMD_CONNECT: 3069 info->connect_supported = 1; 3070 break; 3071 case NL80211_CMD_START_SCHED_SCAN: 3072 info->capa->sched_scan_supported = 1; 3073 break; 3074 case NL80211_CMD_PROBE_CLIENT: 3075 info->poll_command_supported = 1; 3076 break; 3077 } 3078 } 3079 } 3080 3081 3082 static void wiphy_info_max_roc(struct wpa_driver_capa *capa, 3083 struct nlattr *tb) 3084 { 3085 if (tb) 3086 capa->max_remain_on_chan = nla_get_u32(tb); 3087 } 3088 3089 3090 static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls, 3091 struct nlattr *ext_setup) 3092 { 3093 if (tdls == NULL) 3094 return; 3095 3096 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported"); 3097 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT; 3098 3099 if (ext_setup) { 3100 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup"); 3101 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP; 3102 } 3103 } 3104 3105 3106 static void wiphy_info_feature_flags(struct wiphy_info_data *info, 3107 struct nlattr *tb) 3108 { 3109 u32 flags; 3110 struct wpa_driver_capa *capa = info->capa; 3111 3112 if (tb == NULL) 3113 return; 3114 3115 flags = nla_get_u32(tb); 3116 3117 if (flags & NL80211_FEATURE_SK_TX_STATUS) 3118 info->data_tx_status = 1; 3119 3120 if (flags & NL80211_FEATURE_INACTIVITY_TIMER) 3121 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER; 3122 3123 if (flags & NL80211_FEATURE_SAE) 3124 capa->flags |= WPA_DRIVER_FLAGS_SAE; 3125 3126 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN) 3127 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN; 3128 } 3129 3130 3131 static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa, 3132 struct nlattr *tb) 3133 { 3134 u32 protocols; 3135 3136 if (tb == NULL) 3137 return; 3138 3139 protocols = nla_get_u32(tb); 3140 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP " 3141 "mode"); 3142 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD; 3143 capa->probe_resp_offloads = probe_resp_offload_support(protocols); 3144 } 3145 3146 3147 static int wiphy_info_handler(struct nl_msg *msg, void *arg) 3148 { 3149 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 3150 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 3151 struct wiphy_info_data *info = arg; 3152 struct wpa_driver_capa *capa = info->capa; 3153 struct wpa_driver_nl80211_data *drv = info->drv; 3154 3155 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 3156 genlmsg_attrlen(gnlh, 0), NULL); 3157 3158 if (tb[NL80211_ATTR_WIPHY_NAME]) 3159 os_strncpy(drv->phyname, 3160 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]), 3161 sizeof(drv->phyname)); 3162 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]) 3163 capa->max_scan_ssids = 3164 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]); 3165 3166 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]) 3167 capa->max_sched_scan_ssids = 3168 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]); 3169 3170 if (tb[NL80211_ATTR_MAX_MATCH_SETS]) 3171 capa->max_match_sets = 3172 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]); 3173 3174 if (tb[NL80211_ATTR_MAC_ACL_MAX]) 3175 capa->max_acl_mac_addrs = 3176 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]); 3177 3178 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]); 3179 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]); 3180 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]); 3181 3182 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) { 3183 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based " 3184 "off-channel TX"); 3185 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX; 3186 } 3187 3188 if (tb[NL80211_ATTR_ROAM_SUPPORT]) { 3189 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming"); 3190 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION; 3191 } 3192 3193 wiphy_info_max_roc(capa, 3194 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]); 3195 3196 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD]) 3197 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD; 3198 3199 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT], 3200 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]); 3201 3202 if (tb[NL80211_ATTR_DEVICE_AP_SME]) 3203 info->device_ap_sme = 1; 3204 3205 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]); 3206 wiphy_info_probe_resp_offload(capa, 3207 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]); 3208 3209 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] && 3210 drv->extended_capa == NULL) { 3211 drv->extended_capa = 3212 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA])); 3213 if (drv->extended_capa) { 3214 os_memcpy(drv->extended_capa, 3215 nla_data(tb[NL80211_ATTR_EXT_CAPA]), 3216 nla_len(tb[NL80211_ATTR_EXT_CAPA])); 3217 drv->extended_capa_len = 3218 nla_len(tb[NL80211_ATTR_EXT_CAPA]); 3219 } 3220 drv->extended_capa_mask = 3221 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA])); 3222 if (drv->extended_capa_mask) { 3223 os_memcpy(drv->extended_capa_mask, 3224 nla_data(tb[NL80211_ATTR_EXT_CAPA]), 3225 nla_len(tb[NL80211_ATTR_EXT_CAPA])); 3226 } else { 3227 os_free(drv->extended_capa); 3228 drv->extended_capa = NULL; 3229 drv->extended_capa_len = 0; 3230 } 3231 } 3232 3233 return NL_SKIP; 3234 } 3235 3236 3237 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv, 3238 struct wiphy_info_data *info) 3239 { 3240 u32 feat; 3241 struct nl_msg *msg; 3242 3243 os_memset(info, 0, sizeof(*info)); 3244 info->capa = &drv->capa; 3245 info->drv = drv; 3246 3247 msg = nlmsg_alloc(); 3248 if (!msg) 3249 return -1; 3250 3251 feat = get_nl80211_protocol_features(drv); 3252 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP) 3253 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY); 3254 else 3255 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY); 3256 3257 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); 3258 if (nl80211_set_iface_id(msg, &drv->first_bss) < 0) 3259 goto nla_put_failure; 3260 3261 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info)) 3262 return -1; 3263 3264 if (info->auth_supported) 3265 drv->capa.flags |= WPA_DRIVER_FLAGS_SME; 3266 else if (!info->connect_supported) { 3267 wpa_printf(MSG_INFO, "nl80211: Driver does not support " 3268 "authentication/association or connect commands"); 3269 info->error = 1; 3270 } 3271 3272 if (info->p2p_go_supported && info->p2p_client_supported) 3273 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE; 3274 if (info->p2p_concurrent) { 3275 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group " 3276 "interface (driver advertised support)"); 3277 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT; 3278 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P; 3279 } 3280 if (info->num_multichan_concurrent > 1) { 3281 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel " 3282 "concurrent (driver advertised support)"); 3283 drv->capa.num_multichan_concurrent = 3284 info->num_multichan_concurrent; 3285 } 3286 3287 /* default to 5000 since early versions of mac80211 don't set it */ 3288 if (!drv->capa.max_remain_on_chan) 3289 drv->capa.max_remain_on_chan = 5000; 3290 3291 return 0; 3292 nla_put_failure: 3293 nlmsg_free(msg); 3294 return -1; 3295 } 3296 3297 3298 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv) 3299 { 3300 struct wiphy_info_data info; 3301 if (wpa_driver_nl80211_get_info(drv, &info)) 3302 return -1; 3303 3304 if (info.error) 3305 return -1; 3306 3307 drv->has_capability = 1; 3308 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */ 3309 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | 3310 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK | 3311 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | 3312 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; 3313 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 | 3314 WPA_DRIVER_CAPA_ENC_WEP104 | 3315 WPA_DRIVER_CAPA_ENC_TKIP | 3316 WPA_DRIVER_CAPA_ENC_CCMP; 3317 drv->capa.auth = WPA_DRIVER_AUTH_OPEN | 3318 WPA_DRIVER_AUTH_SHARED | 3319 WPA_DRIVER_AUTH_LEAP; 3320 3321 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES; 3322 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE; 3323 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS; 3324 3325 if (!info.device_ap_sme) { 3326 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS; 3327 3328 /* 3329 * No AP SME is currently assumed to also indicate no AP MLME 3330 * in the driver/firmware. 3331 */ 3332 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME; 3333 } 3334 3335 drv->device_ap_sme = info.device_ap_sme; 3336 drv->poll_command_supported = info.poll_command_supported; 3337 drv->data_tx_status = info.data_tx_status; 3338 3339 #ifdef ANDROID_P2P 3340 if(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) { 3341 /* Driver is new enough to support monitorless mode*/ 3342 wpa_printf(MSG_DEBUG, "nl80211: Driver is new " 3343 "enough to support monitor-less mode"); 3344 drv->use_monitor = 0; 3345 } 3346 #else 3347 /* 3348 * If poll command and tx status are supported, mac80211 is new enough 3349 * to have everything we need to not need monitor interfaces. 3350 */ 3351 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status; 3352 #endif 3353 3354 if (drv->device_ap_sme && drv->use_monitor) { 3355 /* 3356 * Non-mac80211 drivers may not support monitor interface. 3357 * Make sure we do not get stuck with incorrect capability here 3358 * by explicitly testing this. 3359 */ 3360 if (!info.monitor_supported) { 3361 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor " 3362 "with device_ap_sme since no monitor mode " 3363 "support detected"); 3364 drv->use_monitor = 0; 3365 } 3366 } 3367 3368 /* 3369 * If we aren't going to use monitor interfaces, but the 3370 * driver doesn't support data TX status, we won't get TX 3371 * status for EAPOL frames. 3372 */ 3373 if (!drv->use_monitor && !info.data_tx_status) 3374 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS; 3375 3376 return 0; 3377 } 3378 3379 3380 #ifdef ANDROID 3381 static int android_genl_ctrl_resolve(struct nl_handle *handle, 3382 const char *name) 3383 { 3384 /* 3385 * Android ICS has very minimal genl_ctrl_resolve() implementation, so 3386 * need to work around that. 3387 */ 3388 struct nl_cache *cache = NULL; 3389 struct genl_family *nl80211 = NULL; 3390 int id = -1; 3391 3392 if (genl_ctrl_alloc_cache(handle, &cache) < 0) { 3393 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic " 3394 "netlink cache"); 3395 goto fail; 3396 } 3397 3398 nl80211 = genl_ctrl_search_by_name(cache, name); 3399 if (nl80211 == NULL) 3400 goto fail; 3401 3402 id = genl_family_get_id(nl80211); 3403 3404 fail: 3405 if (nl80211) 3406 genl_family_put(nl80211); 3407 if (cache) 3408 nl_cache_free(cache); 3409 3410 return id; 3411 } 3412 #define genl_ctrl_resolve android_genl_ctrl_resolve 3413 #endif /* ANDROID */ 3414 3415 3416 static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global) 3417 { 3418 int ret; 3419 3420 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); 3421 if (global->nl_cb == NULL) { 3422 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " 3423 "callbacks"); 3424 return -1; 3425 } 3426 3427 global->nl = nl_create_handle(global->nl_cb, "nl"); 3428 if (global->nl == NULL) 3429 goto err; 3430 3431 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211"); 3432 if (global->nl80211_id < 0) { 3433 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not " 3434 "found"); 3435 goto err; 3436 } 3437 3438 global->nl_event = nl_create_handle(global->nl_cb, "event"); 3439 if (global->nl_event == NULL) 3440 goto err; 3441 3442 ret = nl_get_multicast_id(global, "nl80211", "scan"); 3443 if (ret >= 0) 3444 ret = nl_socket_add_membership(global->nl_event, ret); 3445 if (ret < 0) { 3446 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast " 3447 "membership for scan events: %d (%s)", 3448 ret, strerror(-ret)); 3449 goto err; 3450 } 3451 3452 ret = nl_get_multicast_id(global, "nl80211", "mlme"); 3453 if (ret >= 0) 3454 ret = nl_socket_add_membership(global->nl_event, ret); 3455 if (ret < 0) { 3456 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast " 3457 "membership for mlme events: %d (%s)", 3458 ret, strerror(-ret)); 3459 goto err; 3460 } 3461 3462 ret = nl_get_multicast_id(global, "nl80211", "regulatory"); 3463 if (ret >= 0) 3464 ret = nl_socket_add_membership(global->nl_event, ret); 3465 if (ret < 0) { 3466 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast " 3467 "membership for regulatory events: %d (%s)", 3468 ret, strerror(-ret)); 3469 /* Continue without regulatory events */ 3470 } 3471 3472 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, 3473 no_seq_check, NULL); 3474 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, 3475 process_global_event, global); 3476 3477 eloop_register_read_sock(nl_socket_get_fd(global->nl_event), 3478 wpa_driver_nl80211_event_receive, 3479 global->nl_cb, global->nl_event); 3480 3481 return 0; 3482 3483 err: 3484 nl_destroy_handles(&global->nl_event); 3485 nl_destroy_handles(&global->nl); 3486 nl_cb_put(global->nl_cb); 3487 global->nl_cb = NULL; 3488 return -1; 3489 } 3490 3491 3492 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv) 3493 { 3494 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); 3495 if (!drv->nl_cb) { 3496 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct"); 3497 return -1; 3498 } 3499 3500 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, 3501 no_seq_check, NULL); 3502 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, 3503 process_drv_event, drv); 3504 3505 return 0; 3506 } 3507 3508 3509 static void wpa_driver_nl80211_rfkill_blocked(void *ctx) 3510 { 3511 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked"); 3512 /* 3513 * This may be for any interface; use ifdown event to disable 3514 * interface. 3515 */ 3516 } 3517 3518 3519 static void wpa_driver_nl80211_rfkill_unblocked(void *ctx) 3520 { 3521 struct wpa_driver_nl80211_data *drv = ctx; 3522 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked"); 3523 if (i802_set_iface_flags(&drv->first_bss, 1)) { 3524 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP " 3525 "after rfkill unblock"); 3526 return; 3527 } 3528 /* rtnetlink ifup handler will report interface as enabled */ 3529 } 3530 3531 3532 static void wpa_driver_nl80211_handle_eapol_tx_status(int sock, 3533 void *eloop_ctx, 3534 void *handle) 3535 { 3536 struct wpa_driver_nl80211_data *drv = eloop_ctx; 3537 u8 data[2048]; 3538 struct msghdr msg; 3539 struct iovec entry; 3540 u8 control[512]; 3541 struct cmsghdr *cmsg; 3542 int res, found_ee = 0, found_wifi = 0, acked = 0; 3543 union wpa_event_data event; 3544 3545 memset(&msg, 0, sizeof(msg)); 3546 msg.msg_iov = &entry; 3547 msg.msg_iovlen = 1; 3548 entry.iov_base = data; 3549 entry.iov_len = sizeof(data); 3550 msg.msg_control = &control; 3551 msg.msg_controllen = sizeof(control); 3552 3553 res = recvmsg(sock, &msg, MSG_ERRQUEUE); 3554 /* if error or not fitting 802.3 header, return */ 3555 if (res < 14) 3556 return; 3557 3558 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) 3559 { 3560 if (cmsg->cmsg_level == SOL_SOCKET && 3561 cmsg->cmsg_type == SCM_WIFI_STATUS) { 3562 int *ack; 3563 3564 found_wifi = 1; 3565 ack = (void *)CMSG_DATA(cmsg); 3566 acked = *ack; 3567 } 3568 3569 if (cmsg->cmsg_level == SOL_PACKET && 3570 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) { 3571 struct sock_extended_err *err = 3572 (struct sock_extended_err *)CMSG_DATA(cmsg); 3573 3574 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS) 3575 found_ee = 1; 3576 } 3577 } 3578 3579 if (!found_ee || !found_wifi) 3580 return; 3581 3582 memset(&event, 0, sizeof(event)); 3583 event.eapol_tx_status.dst = data; 3584 event.eapol_tx_status.data = data + 14; 3585 event.eapol_tx_status.data_len = res - 14; 3586 event.eapol_tx_status.ack = acked; 3587 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event); 3588 } 3589 3590 3591 static int nl80211_init_bss(struct i802_bss *bss) 3592 { 3593 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); 3594 if (!bss->nl_cb) 3595 return -1; 3596 3597 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, 3598 no_seq_check, NULL); 3599 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, 3600 process_bss_event, bss); 3601 3602 return 0; 3603 } 3604 3605 3606 static void nl80211_destroy_bss(struct i802_bss *bss) 3607 { 3608 nl_cb_put(bss->nl_cb); 3609 bss->nl_cb = NULL; 3610 } 3611 3612 3613 /** 3614 * wpa_driver_nl80211_init - Initialize nl80211 driver interface 3615 * @ctx: context to be used when calling wpa_supplicant functions, 3616 * e.g., wpa_supplicant_event() 3617 * @ifname: interface name, e.g., wlan0 3618 * @global_priv: private driver global data from global_init() 3619 * Returns: Pointer to private data, %NULL on failure 3620 */ 3621 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname, 3622 void *global_priv) 3623 { 3624 struct wpa_driver_nl80211_data *drv; 3625 struct rfkill_config *rcfg; 3626 struct i802_bss *bss; 3627 3628 if (global_priv == NULL) 3629 return NULL; 3630 drv = os_zalloc(sizeof(*drv)); 3631 if (drv == NULL) 3632 return NULL; 3633 drv->global = global_priv; 3634 drv->ctx = ctx; 3635 bss = &drv->first_bss; 3636 bss->drv = drv; 3637 bss->ctx = ctx; 3638 3639 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname)); 3640 drv->monitor_ifidx = -1; 3641 drv->monitor_sock = -1; 3642 drv->eapol_tx_sock = -1; 3643 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED; 3644 3645 if (wpa_driver_nl80211_init_nl(drv)) { 3646 os_free(drv); 3647 return NULL; 3648 } 3649 3650 if (nl80211_init_bss(bss)) 3651 goto failed; 3652 3653 rcfg = os_zalloc(sizeof(*rcfg)); 3654 if (rcfg == NULL) 3655 goto failed; 3656 rcfg->ctx = drv; 3657 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname)); 3658 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked; 3659 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked; 3660 drv->rfkill = rfkill_init(rcfg); 3661 if (drv->rfkill == NULL) { 3662 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available"); 3663 os_free(rcfg); 3664 } 3665 3666 if (wpa_driver_nl80211_finish_drv_init(drv)) 3667 goto failed; 3668 3669 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0); 3670 if (drv->eapol_tx_sock < 0) 3671 goto failed; 3672 3673 if (drv->data_tx_status) { 3674 int enabled = 1; 3675 3676 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS, 3677 &enabled, sizeof(enabled)) < 0) { 3678 wpa_printf(MSG_DEBUG, 3679 "nl80211: wifi status sockopt failed\n"); 3680 drv->data_tx_status = 0; 3681 if (!drv->use_monitor) 3682 drv->capa.flags &= 3683 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS; 3684 } else { 3685 eloop_register_read_sock(drv->eapol_tx_sock, 3686 wpa_driver_nl80211_handle_eapol_tx_status, 3687 drv, NULL); 3688 } 3689 } 3690 3691 if (drv->global) { 3692 dl_list_add(&drv->global->interfaces, &drv->list); 3693 drv->in_interface_list = 1; 3694 } 3695 3696 return bss; 3697 3698 failed: 3699 wpa_driver_nl80211_deinit(bss); 3700 return NULL; 3701 } 3702 3703 3704 static int nl80211_register_frame(struct i802_bss *bss, 3705 struct nl_handle *nl_handle, 3706 u16 type, const u8 *match, size_t match_len) 3707 { 3708 struct wpa_driver_nl80211_data *drv = bss->drv; 3709 struct nl_msg *msg; 3710 int ret = -1; 3711 3712 msg = nlmsg_alloc(); 3713 if (!msg) 3714 return -1; 3715 3716 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p", 3717 type, nl_handle); 3718 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match", 3719 match, match_len); 3720 3721 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION); 3722 3723 if (nl80211_set_iface_id(msg, bss) < 0) 3724 goto nla_put_failure; 3725 3726 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type); 3727 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match); 3728 3729 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL); 3730 msg = NULL; 3731 if (ret) { 3732 wpa_printf(MSG_DEBUG, "nl80211: Register frame command " 3733 "failed (type=%u): ret=%d (%s)", 3734 type, ret, strerror(-ret)); 3735 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match", 3736 match, match_len); 3737 goto nla_put_failure; 3738 } 3739 ret = 0; 3740 nla_put_failure: 3741 nlmsg_free(msg); 3742 return ret; 3743 } 3744 3745 3746 static int nl80211_alloc_mgmt_handle(struct i802_bss *bss) 3747 { 3748 struct wpa_driver_nl80211_data *drv = bss->drv; 3749 3750 if (bss->nl_mgmt) { 3751 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting " 3752 "already on! (nl_mgmt=%p)", bss->nl_mgmt); 3753 return -1; 3754 } 3755 3756 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt"); 3757 if (bss->nl_mgmt == NULL) 3758 return -1; 3759 3760 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt), 3761 wpa_driver_nl80211_event_receive, bss->nl_cb, 3762 bss->nl_mgmt); 3763 3764 return 0; 3765 } 3766 3767 3768 static int nl80211_register_action_frame(struct i802_bss *bss, 3769 const u8 *match, size_t match_len) 3770 { 3771 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4); 3772 return nl80211_register_frame(bss, bss->nl_mgmt, 3773 type, match, match_len); 3774 } 3775 3776 3777 static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss) 3778 { 3779 struct wpa_driver_nl80211_data *drv = bss->drv; 3780 3781 if (nl80211_alloc_mgmt_handle(bss)) 3782 return -1; 3783 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP " 3784 "handle %p", bss->nl_mgmt); 3785 3786 #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) 3787 /* GAS Initial Request */ 3788 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0) 3789 return -1; 3790 /* GAS Initial Response */ 3791 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0) 3792 return -1; 3793 /* GAS Comeback Request */ 3794 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0) 3795 return -1; 3796 /* GAS Comeback Response */ 3797 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0) 3798 return -1; 3799 #endif /* CONFIG_P2P || CONFIG_INTERWORKING */ 3800 #ifdef CONFIG_P2P 3801 /* P2P Public Action */ 3802 if (nl80211_register_action_frame(bss, 3803 (u8 *) "\x04\x09\x50\x6f\x9a\x09", 3804 6) < 0) 3805 return -1; 3806 /* P2P Action */ 3807 if (nl80211_register_action_frame(bss, 3808 (u8 *) "\x7f\x50\x6f\x9a\x09", 3809 5) < 0) 3810 return -1; 3811 #endif /* CONFIG_P2P */ 3812 #ifdef CONFIG_IEEE80211W 3813 /* SA Query Response */ 3814 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0) 3815 return -1; 3816 #endif /* CONFIG_IEEE80211W */ 3817 #ifdef CONFIG_TDLS 3818 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) { 3819 /* TDLS Discovery Response */ 3820 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) < 3821 0) 3822 return -1; 3823 } 3824 #endif /* CONFIG_TDLS */ 3825 3826 /* FT Action frames */ 3827 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0) 3828 return -1; 3829 else 3830 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT | 3831 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK; 3832 3833 /* WNM - BSS Transition Management Request */ 3834 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0) 3835 return -1; 3836 /* WNM-Sleep Mode Response */ 3837 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0) 3838 return -1; 3839 3840 return 0; 3841 } 3842 3843 3844 static int nl80211_register_spurious_class3(struct i802_bss *bss) 3845 { 3846 struct wpa_driver_nl80211_data *drv = bss->drv; 3847 struct nl_msg *msg; 3848 int ret = -1; 3849 3850 msg = nlmsg_alloc(); 3851 if (!msg) 3852 return -1; 3853 3854 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME); 3855 3856 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); 3857 3858 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL); 3859 msg = NULL; 3860 if (ret) { 3861 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 " 3862 "failed: ret=%d (%s)", 3863 ret, strerror(-ret)); 3864 goto nla_put_failure; 3865 } 3866 ret = 0; 3867 nla_put_failure: 3868 nlmsg_free(msg); 3869 return ret; 3870 } 3871 3872 3873 static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss) 3874 { 3875 static const int stypes[] = { 3876 WLAN_FC_STYPE_AUTH, 3877 WLAN_FC_STYPE_ASSOC_REQ, 3878 WLAN_FC_STYPE_REASSOC_REQ, 3879 WLAN_FC_STYPE_DISASSOC, 3880 WLAN_FC_STYPE_DEAUTH, 3881 WLAN_FC_STYPE_ACTION, 3882 WLAN_FC_STYPE_PROBE_REQ, 3883 /* Beacon doesn't work as mac80211 doesn't currently allow 3884 * it, but it wouldn't really be the right thing anyway as 3885 * it isn't per interface ... maybe just dump the scan 3886 * results periodically for OLBC? 3887 */ 3888 // WLAN_FC_STYPE_BEACON, 3889 }; 3890 unsigned int i; 3891 3892 if (nl80211_alloc_mgmt_handle(bss)) 3893 return -1; 3894 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP " 3895 "handle %p", bss->nl_mgmt); 3896 3897 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) { 3898 if (nl80211_register_frame(bss, bss->nl_mgmt, 3899 (WLAN_FC_TYPE_MGMT << 2) | 3900 (stypes[i] << 4), 3901 NULL, 0) < 0) { 3902 goto out_err; 3903 } 3904 } 3905 3906 if (nl80211_register_spurious_class3(bss)) 3907 goto out_err; 3908 3909 if (nl80211_get_wiphy_data_ap(bss) == NULL) 3910 goto out_err; 3911 3912 return 0; 3913 3914 out_err: 3915 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt)); 3916 nl_destroy_handles(&bss->nl_mgmt); 3917 return -1; 3918 } 3919 3920 3921 static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss) 3922 { 3923 if (nl80211_alloc_mgmt_handle(bss)) 3924 return -1; 3925 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP " 3926 "handle %p (device SME)", bss->nl_mgmt); 3927 3928 if (nl80211_register_frame(bss, bss->nl_mgmt, 3929 (WLAN_FC_TYPE_MGMT << 2) | 3930 (WLAN_FC_STYPE_ACTION << 4), 3931 NULL, 0) < 0) 3932 goto out_err; 3933 3934 return 0; 3935 3936 out_err: 3937 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt)); 3938 nl_destroy_handles(&bss->nl_mgmt); 3939 return -1; 3940 } 3941 3942 3943 static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason) 3944 { 3945 if (bss->nl_mgmt == NULL) 3946 return; 3947 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p " 3948 "(%s)", bss->nl_mgmt, reason); 3949 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt)); 3950 nl_destroy_handles(&bss->nl_mgmt); 3951 3952 nl80211_put_wiphy_data_ap(bss); 3953 } 3954 3955 3956 static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx) 3957 { 3958 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL); 3959 } 3960 3961 3962 static void nl80211_del_p2pdev(struct i802_bss *bss) 3963 { 3964 struct wpa_driver_nl80211_data *drv = bss->drv; 3965 struct nl_msg *msg; 3966 int ret; 3967 3968 msg = nlmsg_alloc(); 3969 if (!msg) 3970 return; 3971 3972 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE); 3973 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id); 3974 3975 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 3976 msg = NULL; 3977 3978 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s", 3979 bss->ifname, (long long unsigned int) bss->wdev_id, 3980 strerror(ret)); 3981 3982 nla_put_failure: 3983 nlmsg_free(msg); 3984 } 3985 3986 3987 static int nl80211_set_p2pdev(struct i802_bss *bss, int start) 3988 { 3989 struct wpa_driver_nl80211_data *drv = bss->drv; 3990 struct nl_msg *msg; 3991 int ret = -1; 3992 3993 msg = nlmsg_alloc(); 3994 if (!msg) 3995 return -1; 3996 3997 if (start) 3998 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE); 3999 else 4000 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE); 4001 4002 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id); 4003 4004 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4005 msg = NULL; 4006 4007 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s", 4008 start ? "Start" : "Stop", 4009 bss->ifname, (long long unsigned int) bss->wdev_id, 4010 strerror(ret)); 4011 4012 nla_put_failure: 4013 nlmsg_free(msg); 4014 return ret; 4015 } 4016 4017 4018 static int i802_set_iface_flags(struct i802_bss *bss, int up) 4019 { 4020 enum nl80211_iftype nlmode; 4021 4022 nlmode = nl80211_get_ifmode(bss); 4023 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) { 4024 return linux_set_iface_flags(bss->drv->global->ioctl_sock, 4025 bss->ifname, up); 4026 } 4027 4028 /* P2P Device has start/stop which is equivalent */ 4029 return nl80211_set_p2pdev(bss, up); 4030 } 4031 4032 4033 static int 4034 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv) 4035 { 4036 #ifndef HOSTAPD 4037 enum nl80211_iftype nlmode = NL80211_IFTYPE_STATION; 4038 #endif /* HOSTAPD */ 4039 struct i802_bss *bss = &drv->first_bss; 4040 int send_rfkill_event = 0; 4041 4042 drv->ifindex = if_nametoindex(bss->ifname); 4043 bss->ifindex = drv->ifindex; 4044 bss->wdev_id = drv->global->if_add_wdevid; 4045 bss->wdev_id_set = drv->global->if_add_wdevid_set; 4046 4047 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex; 4048 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set; 4049 drv->global->if_add_wdevid_set = 0; 4050 4051 if (wpa_driver_nl80211_capa(drv)) 4052 return -1; 4053 4054 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s", 4055 bss->ifname, drv->phyname); 4056 4057 #ifndef HOSTAPD 4058 if (bss->if_dynamic) 4059 nlmode = nl80211_get_ifmode(bss); 4060 4061 /* 4062 * Make sure the interface starts up in station mode unless this is a 4063 * dynamically added interface (e.g., P2P) that was already configured 4064 * with proper iftype. 4065 */ 4066 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) { 4067 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to use managed mode"); 4068 return -1; 4069 } 4070 drv->nlmode = nlmode; 4071 4072 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) { 4073 int ret = nl80211_set_p2pdev(bss, 1); 4074 if (ret < 0) 4075 wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device"); 4076 nl80211_get_macaddr(bss); 4077 return ret; 4078 } 4079 4080 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) { 4081 if (rfkill_is_blocked(drv->rfkill)) { 4082 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable " 4083 "interface '%s' due to rfkill", 4084 bss->ifname); 4085 drv->if_disabled = 1; 4086 send_rfkill_event = 1; 4087 } else { 4088 wpa_printf(MSG_ERROR, "nl80211: Could not set " 4089 "interface '%s' UP", bss->ifname); 4090 return -1; 4091 } 4092 } 4093 4094 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 4095 1, IF_OPER_DORMANT); 4096 #endif /* HOSTAPD */ 4097 4098 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, 4099 bss->addr)) 4100 return -1; 4101 4102 if (send_rfkill_event) { 4103 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill, 4104 drv, drv->ctx); 4105 } 4106 4107 return 0; 4108 } 4109 4110 4111 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv) 4112 { 4113 struct nl_msg *msg; 4114 4115 msg = nlmsg_alloc(); 4116 if (!msg) 4117 return -ENOMEM; 4118 4119 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON); 4120 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 4121 4122 return send_and_recv_msgs(drv, msg, NULL, NULL); 4123 nla_put_failure: 4124 nlmsg_free(msg); 4125 return -ENOBUFS; 4126 } 4127 4128 4129 /** 4130 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface 4131 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init() 4132 * 4133 * Shut down driver interface and processing of driver events. Free 4134 * private data buffer if one was allocated in wpa_driver_nl80211_init(). 4135 */ 4136 static void wpa_driver_nl80211_deinit(struct i802_bss *bss) 4137 { 4138 struct wpa_driver_nl80211_data *drv = bss->drv; 4139 4140 bss->in_deinit = 1; 4141 if (drv->data_tx_status) 4142 eloop_unregister_read_sock(drv->eapol_tx_sock); 4143 if (drv->eapol_tx_sock >= 0) 4144 close(drv->eapol_tx_sock); 4145 4146 if (bss->nl_preq) 4147 wpa_driver_nl80211_probe_req_report(bss, 0); 4148 if (bss->added_if_into_bridge) { 4149 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname, 4150 bss->ifname) < 0) 4151 wpa_printf(MSG_INFO, "nl80211: Failed to remove " 4152 "interface %s from bridge %s: %s", 4153 bss->ifname, bss->brname, strerror(errno)); 4154 } 4155 if (bss->added_bridge) { 4156 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0) 4157 wpa_printf(MSG_INFO, "nl80211: Failed to remove " 4158 "bridge %s: %s", 4159 bss->brname, strerror(errno)); 4160 } 4161 4162 nl80211_remove_monitor_interface(drv); 4163 4164 if (is_ap_interface(drv->nlmode)) 4165 wpa_driver_nl80211_del_beacon(drv); 4166 4167 #ifdef HOSTAPD 4168 if (drv->last_freq_ht) { 4169 /* Clear HT flags from the driver */ 4170 struct hostapd_freq_params freq; 4171 os_memset(&freq, 0, sizeof(freq)); 4172 freq.freq = drv->last_freq; 4173 wpa_driver_nl80211_set_freq(bss, &freq); 4174 } 4175 4176 if (drv->eapol_sock >= 0) { 4177 eloop_unregister_read_sock(drv->eapol_sock); 4178 close(drv->eapol_sock); 4179 } 4180 4181 if (drv->if_indices != drv->default_if_indices) 4182 os_free(drv->if_indices); 4183 #endif /* HOSTAPD */ 4184 4185 if (drv->disabled_11b_rates) 4186 nl80211_disable_11b_rates(drv, drv->ifindex, 0); 4187 4188 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0, 4189 IF_OPER_UP); 4190 rfkill_deinit(drv->rfkill); 4191 4192 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx); 4193 4194 (void) i802_set_iface_flags(bss, 0); 4195 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) { 4196 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION); 4197 nl80211_mgmt_unsubscribe(bss, "deinit"); 4198 } else { 4199 nl80211_mgmt_unsubscribe(bss, "deinit"); 4200 nl80211_del_p2pdev(bss); 4201 } 4202 nl_cb_put(drv->nl_cb); 4203 4204 nl80211_destroy_bss(&drv->first_bss); 4205 4206 os_free(drv->filter_ssids); 4207 4208 os_free(drv->auth_ie); 4209 4210 if (drv->in_interface_list) 4211 dl_list_del(&drv->list); 4212 4213 os_free(drv->extended_capa); 4214 os_free(drv->extended_capa_mask); 4215 os_free(drv); 4216 } 4217 4218 4219 /** 4220 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion 4221 * @eloop_ctx: Driver private data 4222 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init() 4223 * 4224 * This function can be used as registered timeout when starting a scan to 4225 * generate a scan completed event if the driver does not report this. 4226 */ 4227 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx) 4228 { 4229 struct wpa_driver_nl80211_data *drv = eloop_ctx; 4230 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) { 4231 wpa_driver_nl80211_set_mode(&drv->first_bss, 4232 drv->ap_scan_as_station); 4233 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED; 4234 } 4235 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results"); 4236 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL); 4237 } 4238 4239 4240 static struct nl_msg * 4241 nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd, 4242 struct wpa_driver_scan_params *params, u64 *wdev_id) 4243 { 4244 struct nl_msg *msg; 4245 size_t i; 4246 4247 msg = nlmsg_alloc(); 4248 if (!msg) 4249 return NULL; 4250 4251 nl80211_cmd(drv, msg, 0, cmd); 4252 4253 if (!wdev_id) 4254 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 4255 else 4256 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id); 4257 4258 if (params->num_ssids) { 4259 struct nlattr *ssids; 4260 4261 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS); 4262 if (ssids == NULL) 4263 goto fail; 4264 for (i = 0; i < params->num_ssids; i++) { 4265 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID", 4266 params->ssids[i].ssid, 4267 params->ssids[i].ssid_len); 4268 if (nla_put(msg, i + 1, params->ssids[i].ssid_len, 4269 params->ssids[i].ssid) < 0) 4270 goto fail; 4271 } 4272 nla_nest_end(msg, ssids); 4273 } 4274 4275 if (params->extra_ies) { 4276 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs", 4277 params->extra_ies, params->extra_ies_len); 4278 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len, 4279 params->extra_ies) < 0) 4280 goto fail; 4281 } 4282 4283 if (params->freqs) { 4284 struct nlattr *freqs; 4285 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES); 4286 if (freqs == NULL) 4287 goto fail; 4288 for (i = 0; params->freqs[i]; i++) { 4289 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u " 4290 "MHz", params->freqs[i]); 4291 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0) 4292 goto fail; 4293 } 4294 nla_nest_end(msg, freqs); 4295 } 4296 4297 os_free(drv->filter_ssids); 4298 drv->filter_ssids = params->filter_ssids; 4299 params->filter_ssids = NULL; 4300 drv->num_filter_ssids = params->num_filter_ssids; 4301 4302 return msg; 4303 4304 fail: 4305 nla_put_failure: 4306 nlmsg_free(msg); 4307 return NULL; 4308 } 4309 4310 4311 /** 4312 * wpa_driver_nl80211_scan - Request the driver to initiate scan 4313 * @bss: Pointer to private driver data from wpa_driver_nl80211_init() 4314 * @params: Scan parameters 4315 * Returns: 0 on success, -1 on failure 4316 */ 4317 static int wpa_driver_nl80211_scan(struct i802_bss *bss, 4318 struct wpa_driver_scan_params *params) 4319 { 4320 struct wpa_driver_nl80211_data *drv = bss->drv; 4321 int ret = -1, timeout; 4322 struct nl_msg *msg = NULL; 4323 4324 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request"); 4325 drv->scan_for_auth = 0; 4326 4327 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params, 4328 bss->wdev_id_set ? &bss->wdev_id : NULL); 4329 if (!msg) 4330 return -1; 4331 4332 if (params->p2p_probe) { 4333 struct nlattr *rates; 4334 4335 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates"); 4336 4337 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES); 4338 if (rates == NULL) 4339 goto nla_put_failure; 4340 4341 /* 4342 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates 4343 * by masking out everything else apart from the OFDM rates 6, 4344 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz 4345 * rates are left enabled. 4346 */ 4347 NLA_PUT(msg, NL80211_BAND_2GHZ, 8, 4348 "\x0c\x12\x18\x24\x30\x48\x60\x6c"); 4349 nla_nest_end(msg, rates); 4350 4351 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE); 4352 } 4353 4354 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4355 msg = NULL; 4356 if (ret) { 4357 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d " 4358 "(%s)", ret, strerror(-ret)); 4359 #ifdef HOSTAPD 4360 if (is_ap_interface(drv->nlmode)) { 4361 /* 4362 * mac80211 does not allow scan requests in AP mode, so 4363 * try to do this in station mode. 4364 */ 4365 if (wpa_driver_nl80211_set_mode( 4366 bss, NL80211_IFTYPE_STATION)) 4367 goto nla_put_failure; 4368 4369 if (wpa_driver_nl80211_scan(bss, params)) { 4370 wpa_driver_nl80211_set_mode(bss, drv->nlmode); 4371 goto nla_put_failure; 4372 } 4373 4374 /* Restore AP mode when processing scan results */ 4375 drv->ap_scan_as_station = drv->nlmode; 4376 ret = 0; 4377 } else 4378 goto nla_put_failure; 4379 #else /* HOSTAPD */ 4380 goto nla_put_failure; 4381 #endif /* HOSTAPD */ 4382 } 4383 4384 /* Not all drivers generate "scan completed" wireless event, so try to 4385 * read results after a timeout. */ 4386 timeout = 10; 4387 if (drv->scan_complete_events) { 4388 /* 4389 * The driver seems to deliver events to notify when scan is 4390 * complete, so use longer timeout to avoid race conditions 4391 * with scanning and following association request. 4392 */ 4393 timeout = 30; 4394 } 4395 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d " 4396 "seconds", ret, timeout); 4397 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx); 4398 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout, 4399 drv, drv->ctx); 4400 4401 nla_put_failure: 4402 nlmsg_free(msg); 4403 return ret; 4404 } 4405 4406 4407 /** 4408 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan 4409 * @priv: Pointer to private driver data from wpa_driver_nl80211_init() 4410 * @params: Scan parameters 4411 * @interval: Interval between scan cycles in milliseconds 4412 * Returns: 0 on success, -1 on failure or if not supported 4413 */ 4414 static int wpa_driver_nl80211_sched_scan(void *priv, 4415 struct wpa_driver_scan_params *params, 4416 u32 interval) 4417 { 4418 struct i802_bss *bss = priv; 4419 struct wpa_driver_nl80211_data *drv = bss->drv; 4420 int ret = -1; 4421 struct nl_msg *msg; 4422 size_t i; 4423 4424 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request"); 4425 4426 #ifdef ANDROID 4427 if (!drv->capa.sched_scan_supported) 4428 return android_pno_start(bss, params); 4429 #endif /* ANDROID */ 4430 4431 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params, 4432 bss->wdev_id_set ? &bss->wdev_id : NULL); 4433 if (!msg) 4434 goto nla_put_failure; 4435 4436 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval); 4437 4438 if ((drv->num_filter_ssids && 4439 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) || 4440 params->filter_rssi) { 4441 struct nlattr *match_sets; 4442 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH); 4443 if (match_sets == NULL) 4444 goto nla_put_failure; 4445 4446 for (i = 0; i < drv->num_filter_ssids; i++) { 4447 struct nlattr *match_set_ssid; 4448 wpa_hexdump_ascii(MSG_MSGDUMP, 4449 "nl80211: Sched scan filter SSID", 4450 drv->filter_ssids[i].ssid, 4451 drv->filter_ssids[i].ssid_len); 4452 4453 match_set_ssid = nla_nest_start(msg, i + 1); 4454 if (match_set_ssid == NULL) 4455 goto nla_put_failure; 4456 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID, 4457 drv->filter_ssids[i].ssid_len, 4458 drv->filter_ssids[i].ssid); 4459 4460 nla_nest_end(msg, match_set_ssid); 4461 } 4462 4463 if (params->filter_rssi) { 4464 struct nlattr *match_set_rssi; 4465 match_set_rssi = nla_nest_start(msg, 0); 4466 if (match_set_rssi == NULL) 4467 goto nla_put_failure; 4468 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI, 4469 params->filter_rssi); 4470 wpa_printf(MSG_MSGDUMP, 4471 "nl80211: Sched scan RSSI filter %d dBm", 4472 params->filter_rssi); 4473 nla_nest_end(msg, match_set_rssi); 4474 } 4475 4476 nla_nest_end(msg, match_sets); 4477 } 4478 4479 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4480 4481 /* TODO: if we get an error here, we should fall back to normal scan */ 4482 4483 msg = NULL; 4484 if (ret) { 4485 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: " 4486 "ret=%d (%s)", ret, strerror(-ret)); 4487 goto nla_put_failure; 4488 } 4489 4490 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - " 4491 "scan interval %d msec", ret, interval); 4492 4493 nla_put_failure: 4494 nlmsg_free(msg); 4495 return ret; 4496 } 4497 4498 4499 /** 4500 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan 4501 * @priv: Pointer to private driver data from wpa_driver_nl80211_init() 4502 * Returns: 0 on success, -1 on failure or if not supported 4503 */ 4504 static int wpa_driver_nl80211_stop_sched_scan(void *priv) 4505 { 4506 struct i802_bss *bss = priv; 4507 struct wpa_driver_nl80211_data *drv = bss->drv; 4508 int ret = 0; 4509 struct nl_msg *msg; 4510 4511 #ifdef ANDROID 4512 if (!drv->capa.sched_scan_supported) 4513 return android_pno_stop(bss); 4514 #endif /* ANDROID */ 4515 4516 msg = nlmsg_alloc(); 4517 if (!msg) 4518 return -1; 4519 4520 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN); 4521 4522 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 4523 4524 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 4525 msg = NULL; 4526 if (ret) { 4527 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: " 4528 "ret=%d (%s)", ret, strerror(-ret)); 4529 goto nla_put_failure; 4530 } 4531 4532 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret); 4533 4534 nla_put_failure: 4535 nlmsg_free(msg); 4536 return ret; 4537 } 4538 4539 4540 static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie) 4541 { 4542 const u8 *end, *pos; 4543 4544 if (ies == NULL) 4545 return NULL; 4546 4547 pos = ies; 4548 end = ies + ies_len; 4549 4550 while (pos + 1 < end) { 4551 if (pos + 2 + pos[1] > end) 4552 break; 4553 if (pos[0] == ie) 4554 return pos; 4555 pos += 2 + pos[1]; 4556 } 4557 4558 return NULL; 4559 } 4560 4561 4562 static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv, 4563 const u8 *ie, size_t ie_len) 4564 { 4565 const u8 *ssid; 4566 size_t i; 4567 4568 if (drv->filter_ssids == NULL) 4569 return 0; 4570 4571 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID); 4572 if (ssid == NULL) 4573 return 1; 4574 4575 for (i = 0; i < drv->num_filter_ssids; i++) { 4576 if (ssid[1] == drv->filter_ssids[i].ssid_len && 4577 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) == 4578 0) 4579 return 0; 4580 } 4581 4582 return 1; 4583 } 4584 4585 4586 static int bss_info_handler(struct nl_msg *msg, void *arg) 4587 { 4588 struct nlattr *tb[NL80211_ATTR_MAX + 1]; 4589 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 4590 struct nlattr *bss[NL80211_BSS_MAX + 1]; 4591 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { 4592 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC }, 4593 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, 4594 [NL80211_BSS_TSF] = { .type = NLA_U64 }, 4595 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, 4596 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, 4597 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC }, 4598 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, 4599 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, 4600 [NL80211_BSS_STATUS] = { .type = NLA_U32 }, 4601 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, 4602 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC }, 4603 }; 4604 struct nl80211_bss_info_arg *_arg = arg; 4605 struct wpa_scan_results *res = _arg->res; 4606 struct wpa_scan_res **tmp; 4607 struct wpa_scan_res *r; 4608 const u8 *ie, *beacon_ie; 4609 size_t ie_len, beacon_ie_len; 4610 u8 *pos; 4611 size_t i; 4612 4613 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 4614 genlmsg_attrlen(gnlh, 0), NULL); 4615 if (!tb[NL80211_ATTR_BSS]) 4616 return NL_SKIP; 4617 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], 4618 bss_policy)) 4619 return NL_SKIP; 4620 if (bss[NL80211_BSS_STATUS]) { 4621 enum nl80211_bss_status status; 4622 status = nla_get_u32(bss[NL80211_BSS_STATUS]); 4623 if (status == NL80211_BSS_STATUS_ASSOCIATED && 4624 bss[NL80211_BSS_FREQUENCY]) { 4625 _arg->assoc_freq = 4626 nla_get_u32(bss[NL80211_BSS_FREQUENCY]); 4627 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz", 4628 _arg->assoc_freq); 4629 } 4630 if (status == NL80211_BSS_STATUS_ASSOCIATED && 4631 bss[NL80211_BSS_BSSID]) { 4632 os_memcpy(_arg->assoc_bssid, 4633 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN); 4634 wpa_printf(MSG_DEBUG, "nl80211: Associated with " 4635 MACSTR, MAC2STR(_arg->assoc_bssid)); 4636 } 4637 } 4638 if (!res) 4639 return NL_SKIP; 4640 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) { 4641 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 4642 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 4643 } else { 4644 ie = NULL; 4645 ie_len = 0; 4646 } 4647 if (bss[NL80211_BSS_BEACON_IES]) { 4648 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]); 4649 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]); 4650 } else { 4651 beacon_ie = NULL; 4652 beacon_ie_len = 0; 4653 } 4654 4655 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie, 4656 ie ? ie_len : beacon_ie_len)) 4657 return NL_SKIP; 4658 4659 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len); 4660 if (r == NULL) 4661 return NL_SKIP; 4662 if (bss[NL80211_BSS_BSSID]) 4663 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]), 4664 ETH_ALEN); 4665 if (bss[NL80211_BSS_FREQUENCY]) 4666 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]); 4667 if (bss[NL80211_BSS_BEACON_INTERVAL]) 4668 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]); 4669 if (bss[NL80211_BSS_CAPABILITY]) 4670 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); 4671 r->flags |= WPA_SCAN_NOISE_INVALID; 4672 if (bss[NL80211_BSS_SIGNAL_MBM]) { 4673 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); 4674 r->level /= 100; /* mBm to dBm */ 4675 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID; 4676 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { 4677 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); 4678 r->flags |= WPA_SCAN_QUAL_INVALID; 4679 } else 4680 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID; 4681 if (bss[NL80211_BSS_TSF]) 4682 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]); 4683 if (bss[NL80211_BSS_SEEN_MS_AGO]) 4684 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]); 4685 r->ie_len = ie_len; 4686 pos = (u8 *) (r + 1); 4687 if (ie) { 4688 os_memcpy(pos, ie, ie_len); 4689 pos += ie_len; 4690 } 4691 r->beacon_ie_len = beacon_ie_len; 4692 if (beacon_ie) 4693 os_memcpy(pos, beacon_ie, beacon_ie_len); 4694 4695 if (bss[NL80211_BSS_STATUS]) { 4696 enum nl80211_bss_status status; 4697 status = nla_get_u32(bss[NL80211_BSS_STATUS]); 4698 switch (status) { 4699 case NL80211_BSS_STATUS_AUTHENTICATED: 4700 r->flags |= WPA_SCAN_AUTHENTICATED; 4701 break; 4702 case NL80211_BSS_STATUS_ASSOCIATED: 4703 r->flags |= WPA_SCAN_ASSOCIATED; 4704 break; 4705 default: 4706 break; 4707 } 4708 } 4709 4710 /* 4711 * cfg80211 maintains separate BSS table entries for APs if the same 4712 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does 4713 * not use frequency as a separate key in the BSS table, so filter out 4714 * duplicated entries. Prefer associated BSS entry in such a case in 4715 * order to get the correct frequency into the BSS table. 4716 */ 4717 for (i = 0; i < res->num; i++) { 4718 const u8 *s1, *s2; 4719 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0) 4720 continue; 4721 4722 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1), 4723 res->res[i]->ie_len, WLAN_EID_SSID); 4724 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID); 4725 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] || 4726 os_memcmp(s1, s2, 2 + s1[1]) != 0) 4727 continue; 4728 4729 /* Same BSSID,SSID was already included in scan results */ 4730 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result " 4731 "for " MACSTR, MAC2STR(r->bssid)); 4732 4733 if ((r->flags & WPA_SCAN_ASSOCIATED) && 4734 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) { 4735 os_free(res->res[i]); 4736 res->res[i] = r; 4737 } else 4738 os_free(r); 4739 return NL_SKIP; 4740 } 4741 4742 tmp = os_realloc_array(res->res, res->num + 1, 4743 sizeof(struct wpa_scan_res *)); 4744 if (tmp == NULL) { 4745 os_free(r); 4746 return NL_SKIP; 4747 } 4748 tmp[res->num++] = r; 4749 res->res = tmp; 4750 4751 return NL_SKIP; 4752 } 4753 4754 4755 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv, 4756 const u8 *addr) 4757 { 4758 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { 4759 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state " 4760 "mismatch (" MACSTR ")", MAC2STR(addr)); 4761 wpa_driver_nl80211_mlme(drv, addr, 4762 NL80211_CMD_DEAUTHENTICATE, 4763 WLAN_REASON_PREV_AUTH_NOT_VALID, 1); 4764 } 4765 } 4766 4767 4768 static void wpa_driver_nl80211_check_bss_status( 4769 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res) 4770 { 4771 size_t i; 4772 4773 for (i = 0; i < res->num; i++) { 4774 struct wpa_scan_res *r = res->res[i]; 4775 if (r->flags & WPA_SCAN_AUTHENTICATED) { 4776 wpa_printf(MSG_DEBUG, "nl80211: Scan results " 4777 "indicates BSS status with " MACSTR 4778 " as authenticated", 4779 MAC2STR(r->bssid)); 4780 if (is_sta_interface(drv->nlmode) && 4781 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 && 4782 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) != 4783 0) { 4784 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID" 4785 " in local state (auth=" MACSTR 4786 " assoc=" MACSTR ")", 4787 MAC2STR(drv->auth_bssid), 4788 MAC2STR(drv->bssid)); 4789 clear_state_mismatch(drv, r->bssid); 4790 } 4791 } 4792 4793 if (r->flags & WPA_SCAN_ASSOCIATED) { 4794 wpa_printf(MSG_DEBUG, "nl80211: Scan results " 4795 "indicate BSS status with " MACSTR 4796 " as associated", 4797 MAC2STR(r->bssid)); 4798 if (is_sta_interface(drv->nlmode) && 4799 !drv->associated) { 4800 wpa_printf(MSG_DEBUG, "nl80211: Local state " 4801 "(not associated) does not match " 4802 "with BSS state"); 4803 clear_state_mismatch(drv, r->bssid); 4804 } else if (is_sta_interface(drv->nlmode) && 4805 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) != 4806 0) { 4807 wpa_printf(MSG_DEBUG, "nl80211: Local state " 4808 "(associated with " MACSTR ") does " 4809 "not match with BSS state", 4810 MAC2STR(drv->bssid)); 4811 clear_state_mismatch(drv, r->bssid); 4812 clear_state_mismatch(drv, drv->bssid); 4813 } 4814 } 4815 } 4816 } 4817 4818 4819 static struct wpa_scan_results * 4820 nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv) 4821 { 4822 struct nl_msg *msg; 4823 struct wpa_scan_results *res; 4824 int ret; 4825 struct nl80211_bss_info_arg arg; 4826 4827 res = os_zalloc(sizeof(*res)); 4828 if (res == NULL) 4829 return NULL; 4830 msg = nlmsg_alloc(); 4831 if (!msg) 4832 goto nla_put_failure; 4833 4834 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN); 4835 if (nl80211_set_iface_id(msg, &drv->first_bss) < 0) 4836 goto nla_put_failure; 4837 4838 arg.drv = drv; 4839 arg.res = res; 4840 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg); 4841 msg = NULL; 4842 if (ret == 0) { 4843 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu " 4844 "BSSes)", (unsigned long) res->num); 4845 nl80211_get_noise_for_scan_results(drv, res); 4846 return res; 4847 } 4848 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d " 4849 "(%s)", ret, strerror(-ret)); 4850 nla_put_failure: 4851 nlmsg_free(msg); 4852 wpa_scan_results_free(res); 4853 return NULL; 4854 } 4855 4856 4857 /** 4858 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results 4859 * @priv: Pointer to private wext data from wpa_driver_nl80211_init() 4860 * Returns: Scan results on success, -1 on failure 4861 */ 4862 static struct wpa_scan_results * 4863 wpa_driver_nl80211_get_scan_results(void *priv) 4864 { 4865 struct i802_bss *bss = priv; 4866 struct wpa_driver_nl80211_data *drv = bss->drv; 4867 struct wpa_scan_results *res; 4868 4869 res = nl80211_get_scan_results(drv); 4870 if (res) 4871 wpa_driver_nl80211_check_bss_status(drv, res); 4872 return res; 4873 } 4874 4875 4876 static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv) 4877 { 4878 struct wpa_scan_results *res; 4879 size_t i; 4880 4881 res = nl80211_get_scan_results(drv); 4882 if (res == NULL) { 4883 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results"); 4884 return; 4885 } 4886 4887 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump"); 4888 for (i = 0; i < res->num; i++) { 4889 struct wpa_scan_res *r = res->res[i]; 4890 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s", 4891 (int) i, (int) res->num, MAC2STR(r->bssid), 4892 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "", 4893 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : ""); 4894 } 4895 4896 wpa_scan_results_free(res); 4897 } 4898 4899 4900 static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss, 4901 enum wpa_alg alg, const u8 *addr, 4902 int key_idx, int set_tx, 4903 const u8 *seq, size_t seq_len, 4904 const u8 *key, size_t key_len) 4905 { 4906 struct wpa_driver_nl80211_data *drv = bss->drv; 4907 int ifindex; 4908 struct nl_msg *msg; 4909 int ret; 4910 int tdls = 0; 4911 4912 /* Ignore for P2P Device */ 4913 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) 4914 return 0; 4915 4916 ifindex = if_nametoindex(ifname); 4917 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d " 4918 "set_tx=%d seq_len=%lu key_len=%lu", 4919 __func__, ifindex, ifname, alg, addr, key_idx, set_tx, 4920 (unsigned long) seq_len, (unsigned long) key_len); 4921 #ifdef CONFIG_TDLS 4922 if (key_idx == -1) { 4923 key_idx = 0; 4924 tdls = 1; 4925 } 4926 #endif /* CONFIG_TDLS */ 4927 4928 msg = nlmsg_alloc(); 4929 if (!msg) 4930 return -ENOMEM; 4931 4932 if (alg == WPA_ALG_NONE) { 4933 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY); 4934 } else { 4935 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY); 4936 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key); 4937 switch (alg) { 4938 case WPA_ALG_WEP: 4939 if (key_len == 5) 4940 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4941 WLAN_CIPHER_SUITE_WEP40); 4942 else 4943 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4944 WLAN_CIPHER_SUITE_WEP104); 4945 break; 4946 case WPA_ALG_TKIP: 4947 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4948 WLAN_CIPHER_SUITE_TKIP); 4949 break; 4950 case WPA_ALG_CCMP: 4951 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4952 WLAN_CIPHER_SUITE_CCMP); 4953 break; 4954 case WPA_ALG_GCMP: 4955 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4956 WLAN_CIPHER_SUITE_GCMP); 4957 break; 4958 case WPA_ALG_IGTK: 4959 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4960 WLAN_CIPHER_SUITE_AES_CMAC); 4961 break; 4962 case WPA_ALG_SMS4: 4963 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4964 WLAN_CIPHER_SUITE_SMS4); 4965 break; 4966 case WPA_ALG_KRK: 4967 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 4968 WLAN_CIPHER_SUITE_KRK); 4969 break; 4970 default: 4971 wpa_printf(MSG_ERROR, "%s: Unsupported encryption " 4972 "algorithm %d", __func__, alg); 4973 nlmsg_free(msg); 4974 return -1; 4975 } 4976 } 4977 4978 if (seq && seq_len) 4979 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq); 4980 4981 if (addr && !is_broadcast_ether_addr(addr)) { 4982 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr)); 4983 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 4984 4985 if (alg != WPA_ALG_WEP && key_idx && !set_tx) { 4986 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK"); 4987 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, 4988 NL80211_KEYTYPE_GROUP); 4989 } 4990 } else if (addr && is_broadcast_ether_addr(addr)) { 4991 struct nlattr *types; 4992 4993 wpa_printf(MSG_DEBUG, " broadcast key"); 4994 4995 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES); 4996 if (!types) 4997 goto nla_put_failure; 4998 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST); 4999 nla_nest_end(msg, types); 5000 } 5001 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); 5002 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 5003 5004 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5005 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE) 5006 ret = 0; 5007 if (ret) 5008 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)", 5009 ret, strerror(-ret)); 5010 5011 /* 5012 * If we failed or don't need to set the default TX key (below), 5013 * we're done here. 5014 */ 5015 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls) 5016 return ret; 5017 if (is_ap_interface(drv->nlmode) && addr && 5018 !is_broadcast_ether_addr(addr)) 5019 return ret; 5020 5021 msg = nlmsg_alloc(); 5022 if (!msg) 5023 return -ENOMEM; 5024 5025 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY); 5026 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); 5027 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); 5028 if (alg == WPA_ALG_IGTK) 5029 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT); 5030 else 5031 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT); 5032 if (addr && is_broadcast_ether_addr(addr)) { 5033 struct nlattr *types; 5034 5035 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES); 5036 if (!types) 5037 goto nla_put_failure; 5038 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST); 5039 nla_nest_end(msg, types); 5040 } else if (addr) { 5041 struct nlattr *types; 5042 5043 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES); 5044 if (!types) 5045 goto nla_put_failure; 5046 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST); 5047 nla_nest_end(msg, types); 5048 } 5049 5050 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5051 if (ret == -ENOENT) 5052 ret = 0; 5053 if (ret) 5054 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; " 5055 "err=%d %s)", ret, strerror(-ret)); 5056 return ret; 5057 5058 nla_put_failure: 5059 nlmsg_free(msg); 5060 return -ENOBUFS; 5061 } 5062 5063 5064 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg, 5065 int key_idx, int defkey, 5066 const u8 *seq, size_t seq_len, 5067 const u8 *key, size_t key_len) 5068 { 5069 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY); 5070 if (!key_attr) 5071 return -1; 5072 5073 if (defkey && alg == WPA_ALG_IGTK) 5074 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT); 5075 else if (defkey) 5076 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); 5077 5078 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx); 5079 5080 switch (alg) { 5081 case WPA_ALG_WEP: 5082 if (key_len == 5) 5083 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 5084 WLAN_CIPHER_SUITE_WEP40); 5085 else 5086 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 5087 WLAN_CIPHER_SUITE_WEP104); 5088 break; 5089 case WPA_ALG_TKIP: 5090 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP); 5091 break; 5092 case WPA_ALG_CCMP: 5093 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP); 5094 break; 5095 case WPA_ALG_GCMP: 5096 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP); 5097 break; 5098 case WPA_ALG_IGTK: 5099 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 5100 WLAN_CIPHER_SUITE_AES_CMAC); 5101 break; 5102 default: 5103 wpa_printf(MSG_ERROR, "%s: Unsupported encryption " 5104 "algorithm %d", __func__, alg); 5105 return -1; 5106 } 5107 5108 if (seq && seq_len) 5109 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq); 5110 5111 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key); 5112 5113 nla_nest_end(msg, key_attr); 5114 5115 return 0; 5116 nla_put_failure: 5117 return -1; 5118 } 5119 5120 5121 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params, 5122 struct nl_msg *msg) 5123 { 5124 int i, privacy = 0; 5125 struct nlattr *nl_keys, *nl_key; 5126 5127 for (i = 0; i < 4; i++) { 5128 if (!params->wep_key[i]) 5129 continue; 5130 privacy = 1; 5131 break; 5132 } 5133 if (params->wps == WPS_MODE_PRIVACY) 5134 privacy = 1; 5135 if (params->pairwise_suite && 5136 params->pairwise_suite != WPA_CIPHER_NONE) 5137 privacy = 1; 5138 5139 if (!privacy) 5140 return 0; 5141 5142 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY); 5143 5144 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS); 5145 if (!nl_keys) 5146 goto nla_put_failure; 5147 5148 for (i = 0; i < 4; i++) { 5149 if (!params->wep_key[i]) 5150 continue; 5151 5152 nl_key = nla_nest_start(msg, i); 5153 if (!nl_key) 5154 goto nla_put_failure; 5155 5156 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i], 5157 params->wep_key[i]); 5158 if (params->wep_key_len[i] == 5) 5159 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 5160 WLAN_CIPHER_SUITE_WEP40); 5161 else 5162 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 5163 WLAN_CIPHER_SUITE_WEP104); 5164 5165 NLA_PUT_U8(msg, NL80211_KEY_IDX, i); 5166 5167 if (i == params->wep_tx_keyidx) 5168 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); 5169 5170 nla_nest_end(msg, nl_key); 5171 } 5172 nla_nest_end(msg, nl_keys); 5173 5174 return 0; 5175 5176 nla_put_failure: 5177 return -ENOBUFS; 5178 } 5179 5180 5181 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv, 5182 const u8 *addr, int cmd, u16 reason_code, 5183 int local_state_change) 5184 { 5185 int ret = -1; 5186 struct nl_msg *msg; 5187 5188 msg = nlmsg_alloc(); 5189 if (!msg) 5190 return -1; 5191 5192 nl80211_cmd(drv, msg, 0, cmd); 5193 5194 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 5195 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code); 5196 if (addr) 5197 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); 5198 if (local_state_change) 5199 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE); 5200 5201 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5202 msg = NULL; 5203 if (ret) { 5204 wpa_dbg(drv->ctx, MSG_DEBUG, 5205 "nl80211: MLME command failed: reason=%u ret=%d (%s)", 5206 reason_code, ret, strerror(-ret)); 5207 goto nla_put_failure; 5208 } 5209 ret = 0; 5210 5211 nla_put_failure: 5212 nlmsg_free(msg); 5213 return ret; 5214 } 5215 5216 5217 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv, 5218 int reason_code) 5219 { 5220 int ret; 5221 5222 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code); 5223 nl80211_mark_disconnected(drv); 5224 /* Disconnect command doesn't need BSSID - it uses cached value */ 5225 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT, 5226 reason_code, 0); 5227 /* 5228 * For locally generated disconnect, supplicant already generates a 5229 * DEAUTH event, so ignore the event from NL80211. 5230 */ 5231 drv->ignore_next_local_disconnect = ret == 0; 5232 5233 return ret; 5234 } 5235 5236 5237 static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss, 5238 const u8 *addr, int reason_code) 5239 { 5240 struct wpa_driver_nl80211_data *drv = bss->drv; 5241 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) 5242 return wpa_driver_nl80211_disconnect(drv, reason_code); 5243 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)", 5244 __func__, MAC2STR(addr), reason_code); 5245 nl80211_mark_disconnected(drv); 5246 if (drv->nlmode == NL80211_IFTYPE_ADHOC) 5247 return nl80211_leave_ibss(drv); 5248 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE, 5249 reason_code, 0); 5250 } 5251 5252 5253 static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv, 5254 struct wpa_driver_auth_params *params) 5255 { 5256 int i; 5257 5258 drv->auth_freq = params->freq; 5259 drv->auth_alg = params->auth_alg; 5260 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx; 5261 drv->auth_local_state_change = params->local_state_change; 5262 drv->auth_p2p = params->p2p; 5263 5264 if (params->bssid) 5265 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN); 5266 else 5267 os_memset(drv->auth_bssid_, 0, ETH_ALEN); 5268 5269 if (params->ssid) { 5270 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len); 5271 drv->auth_ssid_len = params->ssid_len; 5272 } else 5273 drv->auth_ssid_len = 0; 5274 5275 5276 os_free(drv->auth_ie); 5277 drv->auth_ie = NULL; 5278 drv->auth_ie_len = 0; 5279 if (params->ie) { 5280 drv->auth_ie = os_malloc(params->ie_len); 5281 if (drv->auth_ie) { 5282 os_memcpy(drv->auth_ie, params->ie, params->ie_len); 5283 drv->auth_ie_len = params->ie_len; 5284 } 5285 } 5286 5287 for (i = 0; i < 4; i++) { 5288 if (params->wep_key[i] && params->wep_key_len[i] && 5289 params->wep_key_len[i] <= 16) { 5290 os_memcpy(drv->auth_wep_key[i], params->wep_key[i], 5291 params->wep_key_len[i]); 5292 drv->auth_wep_key_len[i] = params->wep_key_len[i]; 5293 } else 5294 drv->auth_wep_key_len[i] = 0; 5295 } 5296 } 5297 5298 5299 static int wpa_driver_nl80211_authenticate( 5300 struct i802_bss *bss, struct wpa_driver_auth_params *params) 5301 { 5302 struct wpa_driver_nl80211_data *drv = bss->drv; 5303 int ret = -1, i; 5304 struct nl_msg *msg; 5305 enum nl80211_auth_type type; 5306 enum nl80211_iftype nlmode; 5307 int count = 0; 5308 int is_retry; 5309 5310 is_retry = drv->retry_auth; 5311 drv->retry_auth = 0; 5312 5313 nl80211_mark_disconnected(drv); 5314 os_memset(drv->auth_bssid, 0, ETH_ALEN); 5315 if (params->bssid) 5316 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN); 5317 else 5318 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN); 5319 /* FIX: IBSS mode */ 5320 nlmode = params->p2p ? 5321 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION; 5322 if (drv->nlmode != nlmode && 5323 wpa_driver_nl80211_set_mode(bss, nlmode) < 0) 5324 return -1; 5325 5326 retry: 5327 msg = nlmsg_alloc(); 5328 if (!msg) 5329 return -1; 5330 5331 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)", 5332 drv->ifindex); 5333 5334 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE); 5335 5336 for (i = 0; i < 4; i++) { 5337 if (!params->wep_key[i]) 5338 continue; 5339 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP, 5340 NULL, i, 5341 i == params->wep_tx_keyidx, NULL, 0, 5342 params->wep_key[i], 5343 params->wep_key_len[i]); 5344 if (params->wep_tx_keyidx != i) 5345 continue; 5346 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0, 5347 params->wep_key[i], params->wep_key_len[i])) { 5348 nlmsg_free(msg); 5349 return -1; 5350 } 5351 } 5352 5353 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 5354 if (params->bssid) { 5355 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, 5356 MAC2STR(params->bssid)); 5357 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); 5358 } 5359 if (params->freq) { 5360 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); 5361 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); 5362 } 5363 if (params->ssid) { 5364 wpa_hexdump_ascii(MSG_DEBUG, " * SSID", 5365 params->ssid, params->ssid_len); 5366 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, 5367 params->ssid); 5368 } 5369 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len); 5370 if (params->ie) 5371 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie); 5372 if (params->sae_data) { 5373 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data, 5374 params->sae_data_len); 5375 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len, 5376 params->sae_data); 5377 } 5378 if (params->auth_alg & WPA_AUTH_ALG_OPEN) 5379 type = NL80211_AUTHTYPE_OPEN_SYSTEM; 5380 else if (params->auth_alg & WPA_AUTH_ALG_SHARED) 5381 type = NL80211_AUTHTYPE_SHARED_KEY; 5382 else if (params->auth_alg & WPA_AUTH_ALG_LEAP) 5383 type = NL80211_AUTHTYPE_NETWORK_EAP; 5384 else if (params->auth_alg & WPA_AUTH_ALG_FT) 5385 type = NL80211_AUTHTYPE_FT; 5386 else if (params->auth_alg & WPA_AUTH_ALG_SAE) 5387 type = NL80211_AUTHTYPE_SAE; 5388 else 5389 goto nla_put_failure; 5390 wpa_printf(MSG_DEBUG, " * Auth Type %d", type); 5391 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type); 5392 if (params->local_state_change) { 5393 wpa_printf(MSG_DEBUG, " * Local state change only"); 5394 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE); 5395 } 5396 5397 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 5398 msg = NULL; 5399 if (ret) { 5400 wpa_dbg(drv->ctx, MSG_DEBUG, 5401 "nl80211: MLME command failed (auth): ret=%d (%s)", 5402 ret, strerror(-ret)); 5403 count++; 5404 if (ret == -EALREADY && count == 1 && params->bssid && 5405 !params->local_state_change) { 5406 /* 5407 * mac80211 does not currently accept new 5408 * authentication if we are already authenticated. As a 5409 * workaround, force deauthentication and try again. 5410 */ 5411 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication " 5412 "after forced deauthentication"); 5413 wpa_driver_nl80211_deauthenticate( 5414 bss, params->bssid, 5415 WLAN_REASON_PREV_AUTH_NOT_VALID); 5416 nlmsg_free(msg); 5417 goto retry; 5418 } 5419 5420 if (ret == -ENOENT && params->freq && !is_retry) { 5421 /* 5422 * cfg80211 has likely expired the BSS entry even 5423 * though it was previously available in our internal 5424 * BSS table. To recover quickly, start a single 5425 * channel scan on the specified channel. 5426 */ 5427 struct wpa_driver_scan_params scan; 5428 int freqs[2]; 5429 5430 os_memset(&scan, 0, sizeof(scan)); 5431 scan.num_ssids = 1; 5432 if (params->ssid) { 5433 scan.ssids[0].ssid = params->ssid; 5434 scan.ssids[0].ssid_len = params->ssid_len; 5435 } 5436 freqs[0] = params->freq; 5437 freqs[1] = 0; 5438 scan.freqs = freqs; 5439 wpa_printf(MSG_DEBUG, "nl80211: Trigger single " 5440 "channel scan to refresh cfg80211 BSS " 5441 "entry"); 5442 ret = wpa_driver_nl80211_scan(bss, &scan); 5443 if (ret == 0) { 5444 nl80211_copy_auth_params(drv, params); 5445 drv->scan_for_auth = 1; 5446 } 5447 } else if (is_retry) { 5448 /* 5449 * Need to indicate this with an event since the return 5450 * value from the retry is not delivered to core code. 5451 */ 5452 union wpa_event_data event; 5453 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry " 5454 "failed"); 5455 os_memset(&event, 0, sizeof(event)); 5456 os_memcpy(event.timeout_event.addr, drv->auth_bssid_, 5457 ETH_ALEN); 5458 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT, 5459 &event); 5460 } 5461 5462 goto nla_put_failure; 5463 } 5464 ret = 0; 5465 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send " 5466 "successfully"); 5467 5468 nla_put_failure: 5469 nlmsg_free(msg); 5470 return ret; 5471 } 5472 5473 5474 static int wpa_driver_nl80211_authenticate_retry( 5475 struct wpa_driver_nl80211_data *drv) 5476 { 5477 struct wpa_driver_auth_params params; 5478 struct i802_bss *bss = &drv->first_bss; 5479 int i; 5480 5481 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again"); 5482 5483 os_memset(¶ms, 0, sizeof(params)); 5484 params.freq = drv->auth_freq; 5485 params.auth_alg = drv->auth_alg; 5486 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx; 5487 params.local_state_change = drv->auth_local_state_change; 5488 params.p2p = drv->auth_p2p; 5489 5490 if (!is_zero_ether_addr(drv->auth_bssid_)) 5491 params.bssid = drv->auth_bssid_; 5492 5493 if (drv->auth_ssid_len) { 5494 params.ssid = drv->auth_ssid; 5495 params.ssid_len = drv->auth_ssid_len; 5496 } 5497 5498 params.ie = drv->auth_ie; 5499 params.ie_len = drv->auth_ie_len; 5500 5501 for (i = 0; i < 4; i++) { 5502 if (drv->auth_wep_key_len[i]) { 5503 params.wep_key[i] = drv->auth_wep_key[i]; 5504 params.wep_key_len[i] = drv->auth_wep_key_len[i]; 5505 } 5506 } 5507 5508 drv->retry_auth = 1; 5509 return wpa_driver_nl80211_authenticate(bss, ¶ms); 5510 } 5511 5512 5513 struct phy_info_arg { 5514 u16 *num_modes; 5515 struct hostapd_hw_modes *modes; 5516 int last_mode, last_chan_idx; 5517 }; 5518 5519 static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa, 5520 struct nlattr *ampdu_factor, 5521 struct nlattr *ampdu_density, 5522 struct nlattr *mcs_set) 5523 { 5524 if (capa) 5525 mode->ht_capab = nla_get_u16(capa); 5526 5527 if (ampdu_factor) 5528 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03; 5529 5530 if (ampdu_density) 5531 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2; 5532 5533 if (mcs_set && nla_len(mcs_set) >= 16) { 5534 u8 *mcs; 5535 mcs = nla_data(mcs_set); 5536 os_memcpy(mode->mcs_set, mcs, 16); 5537 } 5538 } 5539 5540 5541 static void phy_info_vht_capa(struct hostapd_hw_modes *mode, 5542 struct nlattr *capa, 5543 struct nlattr *mcs_set) 5544 { 5545 if (capa) 5546 mode->vht_capab = nla_get_u32(capa); 5547 5548 if (mcs_set && nla_len(mcs_set) >= 8) { 5549 u8 *mcs; 5550 mcs = nla_data(mcs_set); 5551 os_memcpy(mode->vht_mcs_set, mcs, 8); 5552 } 5553 } 5554 5555 5556 static void phy_info_freq(struct hostapd_hw_modes *mode, 5557 struct hostapd_channel_data *chan, 5558 struct nlattr *tb_freq[]) 5559 { 5560 u8 channel; 5561 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]); 5562 chan->flag = 0; 5563 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES) 5564 chan->chan = channel; 5565 5566 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) 5567 chan->flag |= HOSTAPD_CHAN_DISABLED; 5568 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]) 5569 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN; 5570 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS]) 5571 chan->flag |= HOSTAPD_CHAN_NO_IBSS; 5572 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR]) 5573 chan->flag |= HOSTAPD_CHAN_RADAR; 5574 5575 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] && 5576 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) 5577 chan->max_tx_power = nla_get_u32( 5578 tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100; 5579 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) { 5580 enum nl80211_dfs_state state = 5581 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]); 5582 5583 switch (state) { 5584 case NL80211_DFS_USABLE: 5585 chan->flag |= HOSTAPD_CHAN_DFS_USABLE; 5586 break; 5587 case NL80211_DFS_AVAILABLE: 5588 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE; 5589 break; 5590 case NL80211_DFS_UNAVAILABLE: 5591 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE; 5592 break; 5593 } 5594 } 5595 } 5596 5597 5598 static int phy_info_freqs(struct phy_info_arg *phy_info, 5599 struct hostapd_hw_modes *mode, struct nlattr *tb) 5600 { 5601 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { 5602 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, 5603 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, 5604 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG }, 5605 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG }, 5606 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG }, 5607 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 }, 5608 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 }, 5609 }; 5610 int new_channels = 0; 5611 struct hostapd_channel_data *channel; 5612 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1]; 5613 struct nlattr *nl_freq; 5614 int rem_freq, idx; 5615 5616 if (tb == NULL) 5617 return NL_OK; 5618 5619 nla_for_each_nested(nl_freq, tb, rem_freq) { 5620 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, 5621 nla_data(nl_freq), nla_len(nl_freq), freq_policy); 5622 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) 5623 continue; 5624 new_channels++; 5625 } 5626 5627 channel = os_realloc_array(mode->channels, 5628 mode->num_channels + new_channels, 5629 sizeof(struct hostapd_channel_data)); 5630 if (!channel) 5631 return NL_SKIP; 5632 5633 mode->channels = channel; 5634 mode->num_channels += new_channels; 5635 5636 idx = phy_info->last_chan_idx; 5637 5638 nla_for_each_nested(nl_freq, tb, rem_freq) { 5639 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, 5640 nla_data(nl_freq), nla_len(nl_freq), freq_policy); 5641 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) 5642 continue; 5643 phy_info_freq(mode, &mode->channels[idx], tb_freq); 5644 idx++; 5645 } 5646 phy_info->last_chan_idx = idx; 5647 5648 return NL_OK; 5649 } 5650 5651 5652 static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb) 5653 { 5654 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = { 5655 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 }, 5656 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = 5657 { .type = NLA_FLAG }, 5658 }; 5659 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1]; 5660 struct nlattr *nl_rate; 5661 int rem_rate, idx; 5662 5663 if (tb == NULL) 5664 return NL_OK; 5665 5666 nla_for_each_nested(nl_rate, tb, rem_rate) { 5667 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, 5668 nla_data(nl_rate), nla_len(nl_rate), 5669 rate_policy); 5670 if (!tb_rate[NL80211_BITRATE_ATTR_RATE]) 5671 continue; 5672 mode->num_rates++; 5673 } 5674 5675 mode->rates = os_calloc(mode->num_rates, sizeof(int)); 5676 if (!mode->rates) 5677 return NL_SKIP; 5678 5679 idx = 0; 5680 5681 nla_for_each_nested(nl_rate, tb, rem_rate) { 5682 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, 5683 nla_data(nl_rate), nla_len(nl_rate), 5684 rate_policy); 5685 if (!tb_rate[NL80211_BITRATE_ATTR_RATE]) 5686 continue; 5687 mode->rates[idx] = nla_get_u32( 5688 tb_rate[NL80211_BITRATE_ATTR_RATE]); 5689 idx++; 5690 } 5691 5692 return NL_OK; 5693 } 5694 5695 5696 static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band) 5697 { 5698 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1]; 5699 struct hostapd_hw_modes *mode; 5700 int ret; 5701 5702 if (phy_info->last_mode != nl_band->nla_type) { 5703 mode = os_realloc_array(phy_info->modes, 5704 *phy_info->num_modes + 1, 5705 sizeof(*mode)); 5706 if (!mode) 5707 return NL_SKIP; 5708 phy_info->modes = mode; 5709 5710 mode = &phy_info->modes[*(phy_info->num_modes)]; 5711 os_memset(mode, 0, sizeof(*mode)); 5712 mode->mode = NUM_HOSTAPD_MODES; 5713 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN | 5714 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN; 5715 5716 /* 5717 * Unsupported VHT MCS stream is defined as value 3, so the VHT 5718 * MCS RX/TX map must be initialized with 0xffff to mark all 8 5719 * possible streams as unsupported. This will be overridden if 5720 * driver advertises VHT support. 5721 */ 5722 mode->vht_mcs_set[0] = 0xff; 5723 mode->vht_mcs_set[1] = 0xff; 5724 mode->vht_mcs_set[4] = 0xff; 5725 mode->vht_mcs_set[5] = 0xff; 5726 5727 *(phy_info->num_modes) += 1; 5728 phy_info->last_mode = nl_band->nla_type; 5729 phy_info->last_chan_idx = 0; 5730 } else 5731 mode = &phy_info->modes[*(phy_info->num_modes) - 1]; 5732 5733 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band), 5734 nla_len(nl_band), NULL); 5735 5736 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA], 5737 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR], 5738 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY], 5739 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]); 5740 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA], 5741 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]); 5742 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]); 5743 if (ret != NL_OK) 5744 return ret; 5745 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]); 5746 if (ret != NL_OK) 5747 return ret; 5748 5749 return NL_OK; 5750 } 5751 5752 5753 static int phy_info_handler(struct nl_msg *msg, void *arg) 5754 { 5755 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; 5756 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 5757 struct phy_info_arg *phy_info = arg; 5758 struct nlattr *nl_band; 5759 int rem_band; 5760 5761 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 5762 genlmsg_attrlen(gnlh, 0), NULL); 5763 5764 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS]) 5765 return NL_SKIP; 5766 5767 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) 5768 { 5769 int res = phy_info_band(phy_info, nl_band); 5770 if (res != NL_OK) 5771 return res; 5772 } 5773 5774 return NL_SKIP; 5775 } 5776 5777 5778 static struct hostapd_hw_modes * 5779 wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes, 5780 u16 *num_modes) 5781 { 5782 u16 m; 5783 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode; 5784 int i, mode11g_idx = -1; 5785 5786 /* heuristic to set up modes */ 5787 for (m = 0; m < *num_modes; m++) { 5788 if (!modes[m].num_channels) 5789 continue; 5790 if (modes[m].channels[0].freq < 4000) { 5791 modes[m].mode = HOSTAPD_MODE_IEEE80211B; 5792 for (i = 0; i < modes[m].num_rates; i++) { 5793 if (modes[m].rates[i] > 200) { 5794 modes[m].mode = HOSTAPD_MODE_IEEE80211G; 5795 break; 5796 } 5797 } 5798 } else if (modes[m].channels[0].freq > 50000) 5799 modes[m].mode = HOSTAPD_MODE_IEEE80211AD; 5800 else 5801 modes[m].mode = HOSTAPD_MODE_IEEE80211A; 5802 } 5803 5804 /* If only 802.11g mode is included, use it to construct matching 5805 * 802.11b mode data. */ 5806 5807 for (m = 0; m < *num_modes; m++) { 5808 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B) 5809 return modes; /* 802.11b already included */ 5810 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G) 5811 mode11g_idx = m; 5812 } 5813 5814 if (mode11g_idx < 0) 5815 return modes; /* 2.4 GHz band not supported at all */ 5816 5817 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes)); 5818 if (nmodes == NULL) 5819 return modes; /* Could not add 802.11b mode */ 5820 5821 mode = &nmodes[*num_modes]; 5822 os_memset(mode, 0, sizeof(*mode)); 5823 (*num_modes)++; 5824 modes = nmodes; 5825 5826 mode->mode = HOSTAPD_MODE_IEEE80211B; 5827 5828 mode11g = &modes[mode11g_idx]; 5829 mode->num_channels = mode11g->num_channels; 5830 mode->channels = os_malloc(mode11g->num_channels * 5831 sizeof(struct hostapd_channel_data)); 5832 if (mode->channels == NULL) { 5833 (*num_modes)--; 5834 return modes; /* Could not add 802.11b mode */ 5835 } 5836 os_memcpy(mode->channels, mode11g->channels, 5837 mode11g->num_channels * sizeof(struct hostapd_channel_data)); 5838 5839 mode->num_rates = 0; 5840 mode->rates = os_malloc(4 * sizeof(int)); 5841 if (mode->rates == NULL) { 5842 os_free(mode->channels); 5843 (*num_modes)--; 5844 return modes; /* Could not add 802.11b mode */ 5845 } 5846 5847 for (i = 0; i < mode11g->num_rates; i++) { 5848 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 && 5849 mode11g->rates[i] != 55 && mode11g->rates[i] != 110) 5850 continue; 5851 mode->rates[mode->num_rates] = mode11g->rates[i]; 5852 mode->num_rates++; 5853 if (mode->num_rates == 4) 5854 break; 5855 } 5856 5857 if (mode->num_rates == 0) { 5858 os_free(mode->channels); 5859 os_free(mode->rates); 5860 (*num_modes)--; 5861 return modes; /* No 802.11b rates */ 5862 } 5863 5864 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g " 5865 "information"); 5866 5867 return modes; 5868 } 5869 5870 5871 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start, 5872 int end) 5873 { 5874 int c; 5875 5876 for (c = 0; c < mode->num_channels; c++) { 5877 struct hostapd_channel_data *chan = &mode->channels[c]; 5878 if (chan->freq - 10 >= start && chan->freq + 10 <= end) 5879 chan->flag |= HOSTAPD_CHAN_HT40; 5880 } 5881 } 5882 5883 5884 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start, 5885 int end) 5886 { 5887 int c; 5888 5889 for (c = 0; c < mode->num_channels; c++) { 5890 struct hostapd_channel_data *chan = &mode->channels[c]; 5891 if (!(chan->flag & HOSTAPD_CHAN_HT40)) 5892 continue; 5893 if (chan->freq - 30 >= start && chan->freq - 10 <= end) 5894 chan->flag |= HOSTAPD_CHAN_HT40MINUS; 5895 if (chan->freq + 10 >= start && chan->freq + 30 <= end) 5896 chan->flag |= HOSTAPD_CHAN_HT40PLUS; 5897 } 5898 } 5899 5900 5901 static void nl80211_reg_rule_ht40(struct nlattr *tb[], 5902 struct phy_info_arg *results) 5903 { 5904 u32 start, end, max_bw; 5905 u16 m; 5906 5907 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL || 5908 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL || 5909 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL) 5910 return; 5911 5912 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000; 5913 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000; 5914 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000; 5915 5916 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz", 5917 start, end, max_bw); 5918 if (max_bw < 40) 5919 return; 5920 5921 for (m = 0; m < *results->num_modes; m++) { 5922 if (!(results->modes[m].ht_capab & 5923 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) 5924 continue; 5925 nl80211_set_ht40_mode(&results->modes[m], start, end); 5926 } 5927 } 5928 5929 5930 static void nl80211_reg_rule_sec(struct nlattr *tb[], 5931 struct phy_info_arg *results) 5932 { 5933 u32 start, end, max_bw; 5934 u16 m; 5935 5936 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL || 5937 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL || 5938 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL) 5939 return; 5940 5941 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000; 5942 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000; 5943 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000; 5944 5945 if (max_bw < 20) 5946 return; 5947 5948 for (m = 0; m < *results->num_modes; m++) { 5949 if (!(results->modes[m].ht_capab & 5950 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) 5951 continue; 5952 nl80211_set_ht40_mode_sec(&results->modes[m], start, end); 5953 } 5954 } 5955 5956 5957 static int nl80211_get_reg(struct nl_msg *msg, void *arg) 5958 { 5959 struct phy_info_arg *results = arg; 5960 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; 5961 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 5962 struct nlattr *nl_rule; 5963 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1]; 5964 int rem_rule; 5965 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { 5966 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 }, 5967 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 }, 5968 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 }, 5969 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 }, 5970 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 }, 5971 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 }, 5972 }; 5973 5974 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 5975 genlmsg_attrlen(gnlh, 0), NULL); 5976 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] || 5977 !tb_msg[NL80211_ATTR_REG_RULES]) { 5978 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information " 5979 "available"); 5980 return NL_SKIP; 5981 } 5982 5983 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s", 5984 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2])); 5985 5986 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) 5987 { 5988 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, 5989 nla_data(nl_rule), nla_len(nl_rule), reg_policy); 5990 nl80211_reg_rule_ht40(tb_rule, results); 5991 } 5992 5993 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) 5994 { 5995 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, 5996 nla_data(nl_rule), nla_len(nl_rule), reg_policy); 5997 nl80211_reg_rule_sec(tb_rule, results); 5998 } 5999 6000 return NL_SKIP; 6001 } 6002 6003 6004 static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv, 6005 struct phy_info_arg *results) 6006 { 6007 struct nl_msg *msg; 6008 6009 msg = nlmsg_alloc(); 6010 if (!msg) 6011 return -ENOMEM; 6012 6013 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG); 6014 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results); 6015 } 6016 6017 6018 static struct hostapd_hw_modes * 6019 wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags) 6020 { 6021 u32 feat; 6022 struct i802_bss *bss = priv; 6023 struct wpa_driver_nl80211_data *drv = bss->drv; 6024 struct nl_msg *msg; 6025 struct phy_info_arg result = { 6026 .num_modes = num_modes, 6027 .modes = NULL, 6028 .last_mode = -1, 6029 }; 6030 6031 *num_modes = 0; 6032 *flags = 0; 6033 6034 msg = nlmsg_alloc(); 6035 if (!msg) 6036 return NULL; 6037 6038 feat = get_nl80211_protocol_features(drv); 6039 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP) 6040 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY); 6041 else 6042 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY); 6043 6044 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); 6045 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 6046 6047 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) { 6048 nl80211_set_ht40_flags(drv, &result); 6049 return wpa_driver_nl80211_postprocess_modes(result.modes, 6050 num_modes); 6051 } 6052 msg = NULL; 6053 nla_put_failure: 6054 nlmsg_free(msg); 6055 return NULL; 6056 } 6057 6058 6059 static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv, 6060 const void *data, size_t len, 6061 int encrypt, int noack) 6062 { 6063 __u8 rtap_hdr[] = { 6064 0x00, 0x00, /* radiotap version */ 6065 0x0e, 0x00, /* radiotap length */ 6066 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */ 6067 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */ 6068 0x00, /* padding */ 6069 0x00, 0x00, /* RX and TX flags to indicate that */ 6070 0x00, 0x00, /* this is the injected frame directly */ 6071 }; 6072 struct iovec iov[2] = { 6073 { 6074 .iov_base = &rtap_hdr, 6075 .iov_len = sizeof(rtap_hdr), 6076 }, 6077 { 6078 .iov_base = (void *) data, 6079 .iov_len = len, 6080 } 6081 }; 6082 struct msghdr msg = { 6083 .msg_name = NULL, 6084 .msg_namelen = 0, 6085 .msg_iov = iov, 6086 .msg_iovlen = 2, 6087 .msg_control = NULL, 6088 .msg_controllen = 0, 6089 .msg_flags = 0, 6090 }; 6091 int res; 6092 u16 txflags = 0; 6093 6094 if (encrypt) 6095 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP; 6096 6097 if (drv->monitor_sock < 0) { 6098 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available " 6099 "for %s", __func__); 6100 return -1; 6101 } 6102 6103 if (noack) 6104 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK; 6105 WPA_PUT_LE16(&rtap_hdr[12], txflags); 6106 6107 res = sendmsg(drv->monitor_sock, &msg, 0); 6108 if (res < 0) { 6109 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno)); 6110 return -1; 6111 } 6112 return 0; 6113 } 6114 6115 6116 static int wpa_driver_nl80211_send_frame(struct i802_bss *bss, 6117 const void *data, size_t len, 6118 int encrypt, int noack, 6119 unsigned int freq, int no_cck, 6120 int offchanok, unsigned int wait_time) 6121 { 6122 struct wpa_driver_nl80211_data *drv = bss->drv; 6123 u64 cookie; 6124 6125 if (freq == 0) 6126 freq = bss->freq; 6127 6128 if (drv->use_monitor) 6129 return wpa_driver_nl80211_send_mntr(drv, data, len, 6130 encrypt, noack); 6131 6132 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len, 6133 &cookie, no_cck, noack, offchanok); 6134 } 6135 6136 6137 static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data, 6138 size_t data_len, int noack, 6139 unsigned int freq, int no_cck, 6140 int offchanok, 6141 unsigned int wait_time) 6142 { 6143 struct wpa_driver_nl80211_data *drv = bss->drv; 6144 struct ieee80211_mgmt *mgmt; 6145 int encrypt = 1; 6146 u16 fc; 6147 6148 mgmt = (struct ieee80211_mgmt *) data; 6149 fc = le_to_host16(mgmt->frame_control); 6150 6151 if ((is_sta_interface(drv->nlmode) || 6152 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) && 6153 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && 6154 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) { 6155 /* 6156 * The use of last_mgmt_freq is a bit of a hack, 6157 * but it works due to the single-threaded nature 6158 * of wpa_supplicant. 6159 */ 6160 if (freq == 0) 6161 freq = drv->last_mgmt_freq; 6162 return nl80211_send_frame_cmd(bss, freq, 0, 6163 data, data_len, NULL, 1, noack, 6164 1); 6165 } 6166 6167 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) { 6168 if (freq == 0) 6169 freq = bss->freq; 6170 return nl80211_send_frame_cmd(bss, freq, 6171 (int) freq == bss->freq ? 0 : 6172 wait_time, 6173 data, data_len, 6174 &drv->send_action_cookie, 6175 no_cck, noack, offchanok); 6176 } 6177 6178 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && 6179 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) { 6180 /* 6181 * Only one of the authentication frame types is encrypted. 6182 * In order for static WEP encryption to work properly (i.e., 6183 * to not encrypt the frame), we need to tell mac80211 about 6184 * the frames that must not be encrypted. 6185 */ 6186 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg); 6187 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction); 6188 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3) 6189 encrypt = 0; 6190 } 6191 6192 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 6193 noack, freq, no_cck, offchanok, 6194 wait_time); 6195 } 6196 6197 6198 static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble, 6199 int slot, int ht_opmode, int ap_isolate, 6200 int *basic_rates) 6201 { 6202 struct wpa_driver_nl80211_data *drv = bss->drv; 6203 struct nl_msg *msg; 6204 6205 msg = nlmsg_alloc(); 6206 if (!msg) 6207 return -ENOMEM; 6208 6209 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS); 6210 6211 if (cts >= 0) 6212 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts); 6213 if (preamble >= 0) 6214 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble); 6215 if (slot >= 0) 6216 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot); 6217 if (ht_opmode >= 0) 6218 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode); 6219 if (ap_isolate >= 0) 6220 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate); 6221 6222 if (basic_rates) { 6223 u8 rates[NL80211_MAX_SUPP_RATES]; 6224 u8 rates_len = 0; 6225 int i; 6226 6227 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; 6228 i++) 6229 rates[rates_len++] = basic_rates[i] / 5; 6230 6231 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates); 6232 } 6233 6234 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); 6235 6236 return send_and_recv_msgs(drv, msg, NULL, NULL); 6237 nla_put_failure: 6238 nlmsg_free(msg); 6239 return -ENOBUFS; 6240 } 6241 6242 6243 static int wpa_driver_nl80211_set_acl(void *priv, 6244 struct hostapd_acl_params *params) 6245 { 6246 struct i802_bss *bss = priv; 6247 struct wpa_driver_nl80211_data *drv = bss->drv; 6248 struct nl_msg *msg; 6249 struct nlattr *acl; 6250 unsigned int i; 6251 int ret = 0; 6252 6253 if (!(drv->capa.max_acl_mac_addrs)) 6254 return -ENOTSUP; 6255 6256 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs) 6257 return -ENOTSUP; 6258 6259 msg = nlmsg_alloc(); 6260 if (!msg) 6261 return -ENOMEM; 6262 6263 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)", 6264 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl); 6265 6266 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL); 6267 6268 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); 6269 6270 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ? 6271 NL80211_ACL_POLICY_DENY_UNLESS_LISTED : 6272 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED); 6273 6274 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS); 6275 if (acl == NULL) 6276 goto nla_put_failure; 6277 6278 for (i = 0; i < params->num_mac_acl; i++) 6279 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr); 6280 6281 nla_nest_end(msg, acl); 6282 6283 ret = send_and_recv_msgs(drv, msg, NULL, NULL); 6284 msg = NULL; 6285 if (ret) { 6286 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)", 6287 ret, strerror(-ret)); 6288 } 6289 6290 nla_put_failure: 6291 nlmsg_free(msg); 6292 6293 return ret; 6294 } 6295 6296 6297 static int wpa_driver_nl80211_set_ap(void *priv, 6298 struct wpa_driver_ap_params *params) 6299 { 6300 struct i802_bss *bss = priv; 6301 struct wpa_driver_nl80211_data *drv = bss->drv; 6302 struct nl_msg *msg; 6303 u8 cmd = NL80211_CMD_NEW_BEACON; 6304 int ret; 6305 int beacon_set; 6306 int ifindex = if_nametoindex(bss->ifname); 6307 int num_suites; 6308 u32 suites[10]; 6309 u32 ver; 6310 6311 beacon_set = bss->beacon_set; 6312 6313 msg = nlmsg_alloc(); 6314 if (!msg) 6315 return -ENOMEM; 6316 6317 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)", 6318 beacon_set); 6319 if (beacon_set) 6320 cmd = NL80211_CMD_SET_BEACON; 6321 6322 nl80211_cmd(drv, msg, 0, cmd); 6323 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head", 6324 params->head, params->head_len); 6325 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,