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