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