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