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