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