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