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