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