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