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