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