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