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