driver_nl80211.c revision 051af73b8f8014eff33330aead0f36944b3403e6
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 enum chan_width convert2width(int width);
2517
2518static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2519				struct nlattr **tb)
2520{
2521	union wpa_event_data data;
2522	enum nl80211_radar_event event_type;
2523
2524	if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2525		return;
2526
2527	os_memset(&data, 0, sizeof(data));
2528	data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2529	event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
2530
2531	/* Check HT params */
2532	if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2533		data.dfs_event.ht_enabled = 1;
2534		data.dfs_event.chan_offset = 0;
2535
2536		switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2537		case NL80211_CHAN_NO_HT:
2538			data.dfs_event.ht_enabled = 0;
2539			break;
2540		case NL80211_CHAN_HT20:
2541			break;
2542		case NL80211_CHAN_HT40PLUS:
2543			data.dfs_event.chan_offset = 1;
2544			break;
2545		case NL80211_CHAN_HT40MINUS:
2546			data.dfs_event.chan_offset = -1;
2547			break;
2548		}
2549	}
2550
2551	/* Get VHT params */
2552	data.dfs_event.chan_width =
2553		convert2width(nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
2554	data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
2555	if (tb[NL80211_ATTR_CENTER_FREQ2])
2556		data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2557
2558	wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2559		   data.dfs_event.freq, data.dfs_event.ht_enabled,
2560		   data.dfs_event.chan_offset, data.dfs_event.chan_width,
2561		   data.dfs_event.cf1, data.dfs_event.cf2);
2562
2563	switch (event_type) {
2564	case NL80211_RADAR_DETECTED:
2565		wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2566		break;
2567	case NL80211_RADAR_CAC_FINISHED:
2568		wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2569		break;
2570	case NL80211_RADAR_CAC_ABORTED:
2571		wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2572		break;
2573	case NL80211_RADAR_NOP_FINISHED:
2574		wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2575		break;
2576	default:
2577		wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2578			   "received", event_type);
2579		break;
2580	}
2581}
2582
2583
2584static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2585				   int wds)
2586{
2587	struct wpa_driver_nl80211_data *drv = bss->drv;
2588	union wpa_event_data event;
2589
2590	if (!tb[NL80211_ATTR_MAC])
2591		return;
2592
2593	os_memset(&event, 0, sizeof(event));
2594	event.rx_from_unknown.bssid = bss->addr;
2595	event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2596	event.rx_from_unknown.wds = wds;
2597
2598	wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2599}
2600
2601
2602static void do_process_drv_event(struct i802_bss *bss, int cmd,
2603				 struct nlattr **tb)
2604{
2605	struct wpa_driver_nl80211_data *drv = bss->drv;
2606
2607	wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2608		   cmd, nl80211_command_to_string(cmd), bss->ifname);
2609
2610	if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2611	    (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2612	     cmd == NL80211_CMD_SCAN_ABORTED)) {
2613		wpa_driver_nl80211_set_mode(&drv->first_bss,
2614					    drv->ap_scan_as_station);
2615		drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
2616	}
2617
2618	switch (cmd) {
2619	case NL80211_CMD_TRIGGER_SCAN:
2620		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
2621		drv->scan_state = SCAN_STARTED;
2622		break;
2623	case NL80211_CMD_START_SCHED_SCAN:
2624		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
2625		drv->scan_state = SCHED_SCAN_STARTED;
2626		break;
2627	case NL80211_CMD_SCHED_SCAN_STOPPED:
2628		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
2629		drv->scan_state = SCHED_SCAN_STOPPED;
2630		wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2631		break;
2632	case NL80211_CMD_NEW_SCAN_RESULTS:
2633		wpa_dbg(drv->ctx, MSG_DEBUG,
2634			"nl80211: New scan results available");
2635		drv->scan_state = SCAN_COMPLETED;
2636		drv->scan_complete_events = 1;
2637		eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2638				     drv->ctx);
2639		send_scan_event(drv, 0, tb);
2640		break;
2641	case NL80211_CMD_SCHED_SCAN_RESULTS:
2642		wpa_dbg(drv->ctx, MSG_DEBUG,
2643			"nl80211: New sched scan results available");
2644		drv->scan_state = SCHED_SCAN_RESULTS;
2645		send_scan_event(drv, 0, tb);
2646		break;
2647	case NL80211_CMD_SCAN_ABORTED:
2648		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
2649		drv->scan_state = SCAN_ABORTED;
2650		/*
2651		 * Need to indicate that scan results are available in order
2652		 * not to make wpa_supplicant stop its scanning.
2653		 */
2654		eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2655				     drv->ctx);
2656		send_scan_event(drv, 1, tb);
2657		break;
2658	case NL80211_CMD_AUTHENTICATE:
2659	case NL80211_CMD_ASSOCIATE:
2660	case NL80211_CMD_DEAUTHENTICATE:
2661	case NL80211_CMD_DISASSOCIATE:
2662	case NL80211_CMD_FRAME_TX_STATUS:
2663	case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2664	case NL80211_CMD_UNPROT_DISASSOCIATE:
2665		mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
2666			   tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2667			   tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2668			   tb[NL80211_ATTR_COOKIE],
2669			   tb[NL80211_ATTR_RX_SIGNAL_DBM]);
2670		break;
2671	case NL80211_CMD_CONNECT:
2672	case NL80211_CMD_ROAM:
2673		mlme_event_connect(drv, cmd,
2674				   tb[NL80211_ATTR_STATUS_CODE],
2675				   tb[NL80211_ATTR_MAC],
2676				   tb[NL80211_ATTR_REQ_IE],
2677				   tb[NL80211_ATTR_RESP_IE]);
2678		break;
2679	case NL80211_CMD_CH_SWITCH_NOTIFY:
2680		mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
2681				     tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2682		break;
2683	case NL80211_CMD_DISCONNECT:
2684		mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
2685				      tb[NL80211_ATTR_MAC],
2686				      tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
2687		break;
2688	case NL80211_CMD_MICHAEL_MIC_FAILURE:
2689		mlme_event_michael_mic_failure(bss, tb);
2690		break;
2691	case NL80211_CMD_JOIN_IBSS:
2692		mlme_event_join_ibss(drv, tb);
2693		break;
2694	case NL80211_CMD_REMAIN_ON_CHANNEL:
2695		mlme_event_remain_on_channel(drv, 0, tb);
2696		break;
2697	case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2698		mlme_event_remain_on_channel(drv, 1, tb);
2699		break;
2700	case NL80211_CMD_NOTIFY_CQM:
2701		nl80211_cqm_event(drv, tb);
2702		break;
2703	case NL80211_CMD_REG_CHANGE:
2704		wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2705		wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2706				     NULL);
2707		break;
2708	case NL80211_CMD_REG_BEACON_HINT:
2709		wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2710		wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2711				     NULL);
2712		break;
2713	case NL80211_CMD_NEW_STATION:
2714		nl80211_new_station_event(drv, tb);
2715		break;
2716	case NL80211_CMD_DEL_STATION:
2717		nl80211_del_station_event(drv, tb);
2718		break;
2719	case NL80211_CMD_SET_REKEY_OFFLOAD:
2720		nl80211_rekey_offload_event(drv, tb);
2721		break;
2722	case NL80211_CMD_PMKSA_CANDIDATE:
2723		nl80211_pmksa_candidate_event(drv, tb);
2724		break;
2725	case NL80211_CMD_PROBE_CLIENT:
2726		nl80211_client_probe_event(drv, tb);
2727		break;
2728	case NL80211_CMD_TDLS_OPER:
2729		nl80211_tdls_oper_event(drv, tb);
2730		break;
2731	case NL80211_CMD_CONN_FAILED:
2732		nl80211_connect_failed_event(drv, tb);
2733		break;
2734	case NL80211_CMD_FT_EVENT:
2735		mlme_event_ft_event(drv, tb);
2736		break;
2737	case NL80211_CMD_RADAR_DETECT:
2738		nl80211_radar_event(drv, tb);
2739		break;
2740	case NL80211_CMD_STOP_AP:
2741		nl80211_stop_ap(drv, tb);
2742		break;
2743	default:
2744		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
2745			"(cmd=%d)", cmd);
2746		break;
2747	}
2748}
2749
2750
2751static int process_drv_event(struct nl_msg *msg, void *arg)
2752{
2753	struct wpa_driver_nl80211_data *drv = arg;
2754	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2755	struct nlattr *tb[NL80211_ATTR_MAX + 1];
2756	struct i802_bss *bss;
2757	int ifidx = -1;
2758
2759	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2760		  genlmsg_attrlen(gnlh, 0), NULL);
2761
2762	if (tb[NL80211_ATTR_IFINDEX]) {
2763		ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2764
2765		for (bss = &drv->first_bss; bss; bss = bss->next)
2766			if (ifidx == -1 || ifidx == bss->ifindex) {
2767				do_process_drv_event(bss, gnlh->cmd, tb);
2768				return NL_SKIP;
2769			}
2770		wpa_printf(MSG_DEBUG,
2771			   "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
2772			   gnlh->cmd, ifidx);
2773	} else if (tb[NL80211_ATTR_WDEV]) {
2774		u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
2775		wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
2776		for (bss = &drv->first_bss; bss; bss = bss->next) {
2777			if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
2778				do_process_drv_event(bss, gnlh->cmd, tb);
2779				return NL_SKIP;
2780			}
2781		}
2782		wpa_printf(MSG_DEBUG,
2783			   "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
2784			   gnlh->cmd, (long long unsigned int) wdev_id);
2785	}
2786
2787	return NL_SKIP;
2788}
2789
2790
2791static int process_global_event(struct nl_msg *msg, void *arg)
2792{
2793	struct nl80211_global *global = arg;
2794	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2795	struct nlattr *tb[NL80211_ATTR_MAX + 1];
2796	struct wpa_driver_nl80211_data *drv, *tmp;
2797	int ifidx = -1;
2798	struct i802_bss *bss;
2799	u64 wdev_id = 0;
2800	int wdev_id_set = 0;
2801
2802	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2803		  genlmsg_attrlen(gnlh, 0), NULL);
2804
2805	if (tb[NL80211_ATTR_IFINDEX])
2806		ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2807	else if (tb[NL80211_ATTR_WDEV]) {
2808		wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
2809		wdev_id_set = 1;
2810	}
2811
2812	dl_list_for_each_safe(drv, tmp, &global->interfaces,
2813			      struct wpa_driver_nl80211_data, list) {
2814		for (bss = &drv->first_bss; bss; bss = bss->next) {
2815			if ((ifidx == -1 && !wdev_id_set) ||
2816			    ifidx == bss->ifindex ||
2817			    (wdev_id_set && bss->wdev_id_set &&
2818			     wdev_id == bss->wdev_id)) {
2819				do_process_drv_event(bss, gnlh->cmd, tb);
2820				return NL_SKIP;
2821			}
2822		}
2823	}
2824
2825	return NL_SKIP;
2826}
2827
2828
2829static int process_bss_event(struct nl_msg *msg, void *arg)
2830{
2831	struct i802_bss *bss = arg;
2832	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2833	struct nlattr *tb[NL80211_ATTR_MAX + 1];
2834
2835	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2836		  genlmsg_attrlen(gnlh, 0), NULL);
2837
2838	wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
2839		   gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
2840		   bss->ifname);
2841
2842	switch (gnlh->cmd) {
2843	case NL80211_CMD_FRAME:
2844	case NL80211_CMD_FRAME_TX_STATUS:
2845		mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
2846			   tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2847			   tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2848			   tb[NL80211_ATTR_COOKIE],
2849			   tb[NL80211_ATTR_RX_SIGNAL_DBM]);
2850		break;
2851	case NL80211_CMD_UNEXPECTED_FRAME:
2852		nl80211_spurious_frame(bss, tb, 0);
2853		break;
2854	case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2855		nl80211_spurious_frame(bss, tb, 1);
2856		break;
2857	default:
2858		wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2859			   "(cmd=%d)", gnlh->cmd);
2860		break;
2861	}
2862
2863	return NL_SKIP;
2864}
2865
2866
2867static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
2868					     void *handle)
2869{
2870	struct nl_cb *cb = eloop_ctx;
2871
2872	wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
2873
2874	nl_recvmsgs(handle, cb);
2875}
2876
2877
2878/**
2879 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2880 * @priv: driver_nl80211 private data
2881 * @alpha2_arg: country to which to switch to
2882 * Returns: 0 on success, -1 on failure
2883 *
2884 * This asks nl80211 to set the regulatory domain for given
2885 * country ISO / IEC alpha2.
2886 */
2887static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2888{
2889	struct i802_bss *bss = priv;
2890	struct wpa_driver_nl80211_data *drv = bss->drv;
2891	char alpha2[3];
2892	struct nl_msg *msg;
2893
2894	msg = nlmsg_alloc();
2895	if (!msg)
2896		return -ENOMEM;
2897
2898	alpha2[0] = alpha2_arg[0];
2899	alpha2[1] = alpha2_arg[1];
2900	alpha2[2] = '\0';
2901
2902	nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
2903
2904	NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2905	if (send_and_recv_msgs(drv, msg, NULL, NULL))
2906		return -EINVAL;
2907	return 0;
2908nla_put_failure:
2909	nlmsg_free(msg);
2910	return -EINVAL;
2911}
2912
2913
2914static int protocol_feature_handler(struct nl_msg *msg, void *arg)
2915{
2916	u32 *feat = arg;
2917	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2918	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2919
2920	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2921		  genlmsg_attrlen(gnlh, 0), NULL);
2922
2923	if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
2924		*feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
2925
2926	return NL_SKIP;
2927}
2928
2929
2930static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
2931{
2932	u32 feat = 0;
2933	struct nl_msg *msg;
2934
2935	msg = nlmsg_alloc();
2936	if (!msg)
2937		goto nla_put_failure;
2938
2939	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
2940	if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
2941		return feat;
2942
2943	msg = NULL;
2944nla_put_failure:
2945	nlmsg_free(msg);
2946	return 0;
2947}
2948
2949
2950struct wiphy_info_data {
2951	struct wpa_driver_nl80211_data *drv;
2952	struct wpa_driver_capa *capa;
2953
2954	unsigned int num_multichan_concurrent;
2955
2956	unsigned int error:1;
2957	unsigned int device_ap_sme:1;
2958	unsigned int poll_command_supported:1;
2959	unsigned int data_tx_status:1;
2960	unsigned int monitor_supported:1;
2961	unsigned int auth_supported:1;
2962	unsigned int connect_supported:1;
2963	unsigned int p2p_go_supported:1;
2964	unsigned int p2p_client_supported:1;
2965	unsigned int p2p_concurrent:1;
2966};
2967
2968
2969static unsigned int probe_resp_offload_support(int supp_protocols)
2970{
2971	unsigned int prot = 0;
2972
2973	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2974		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2975	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2976		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2977	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2978		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2979	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2980		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2981
2982	return prot;
2983}
2984
2985
2986static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
2987					 struct nlattr *tb)
2988{
2989	struct nlattr *nl_mode;
2990	int i;
2991
2992	if (tb == NULL)
2993		return;
2994
2995	nla_for_each_nested(nl_mode, tb, i) {
2996		switch (nla_type(nl_mode)) {
2997		case NL80211_IFTYPE_AP:
2998			info->capa->flags |= WPA_DRIVER_FLAGS_AP;
2999			break;
3000		case NL80211_IFTYPE_ADHOC:
3001			info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3002			break;
3003		case NL80211_IFTYPE_P2P_DEVICE:
3004			info->capa->flags |=
3005				WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3006			break;
3007		case NL80211_IFTYPE_P2P_GO:
3008			info->p2p_go_supported = 1;
3009			break;
3010		case NL80211_IFTYPE_P2P_CLIENT:
3011			info->p2p_client_supported = 1;
3012			break;
3013		case NL80211_IFTYPE_MONITOR:
3014			info->monitor_supported = 1;
3015			break;
3016		}
3017	}
3018}
3019
3020
3021static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3022					 struct nlattr *nl_combi)
3023{
3024	struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3025	struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3026	struct nlattr *nl_limit, *nl_mode;
3027	int err, rem_limit, rem_mode;
3028	int combination_has_p2p = 0, combination_has_mgd = 0;
3029	static struct nla_policy
3030	iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3031		[NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3032		[NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3033		[NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3034		[NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
3035		[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
3036	},
3037	iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3038		[NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3039		[NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3040	};
3041
3042	err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3043			       nl_combi, iface_combination_policy);
3044	if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3045	    !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3046	    !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3047		return 0; /* broken combination */
3048
3049	if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3050		info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3051
3052	nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3053			    rem_limit) {
3054		err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3055				       nl_limit, iface_limit_policy);
3056		if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3057			return 0; /* broken combination */
3058
3059		nla_for_each_nested(nl_mode,
3060				    tb_limit[NL80211_IFACE_LIMIT_TYPES],
3061				    rem_mode) {
3062			int ift = nla_type(nl_mode);
3063			if (ift == NL80211_IFTYPE_P2P_GO ||
3064			    ift == NL80211_IFTYPE_P2P_CLIENT)
3065				combination_has_p2p = 1;
3066			if (ift == NL80211_IFTYPE_STATION)
3067				combination_has_mgd = 1;
3068		}
3069		if (combination_has_p2p && combination_has_mgd)
3070			break;
3071	}
3072
3073	if (combination_has_p2p && combination_has_mgd) {
3074		info->p2p_concurrent = 1;
3075		info->num_multichan_concurrent =
3076			nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
3077		return 1;
3078	}
3079
3080	return 0;
3081}
3082
3083
3084static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3085				  struct nlattr *tb)
3086{
3087	struct nlattr *nl_combi;
3088	int rem_combi;
3089
3090	if (tb == NULL)
3091		return;
3092
3093	nla_for_each_nested(nl_combi, tb, rem_combi) {
3094		if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3095			break;
3096	}
3097}
3098
3099
3100static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3101				 struct nlattr *tb)
3102{
3103	struct nlattr *nl_cmd;
3104	int i;
3105
3106	if (tb == NULL)
3107		return;
3108
3109	nla_for_each_nested(nl_cmd, tb, i) {
3110		switch (nla_get_u32(nl_cmd)) {
3111		case NL80211_CMD_AUTHENTICATE:
3112			info->auth_supported = 1;
3113			break;
3114		case NL80211_CMD_CONNECT:
3115			info->connect_supported = 1;
3116			break;
3117		case NL80211_CMD_START_SCHED_SCAN:
3118			info->capa->sched_scan_supported = 1;
3119			break;
3120		case NL80211_CMD_PROBE_CLIENT:
3121			info->poll_command_supported = 1;
3122			break;
3123		}
3124	}
3125}
3126
3127
3128static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3129			       struct nlattr *tb)
3130{
3131	if (tb)
3132		capa->max_remain_on_chan = nla_get_u32(tb);
3133}
3134
3135
3136static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3137			    struct nlattr *ext_setup)
3138{
3139	if (tdls == NULL)
3140		return;
3141
3142	wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3143	capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3144
3145	if (ext_setup) {
3146		wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3147		capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3148	}
3149}
3150
3151
3152static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3153				     struct nlattr *tb)
3154{
3155	u32 flags;
3156	struct wpa_driver_capa *capa = info->capa;
3157
3158	if (tb == NULL)
3159		return;
3160
3161	flags = nla_get_u32(tb);
3162
3163	if (flags & NL80211_FEATURE_SK_TX_STATUS)
3164		info->data_tx_status = 1;
3165
3166	if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3167		capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3168
3169	if (flags & NL80211_FEATURE_SAE)
3170		capa->flags |= WPA_DRIVER_FLAGS_SAE;
3171
3172	if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3173		capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
3174}
3175
3176
3177static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3178					  struct nlattr *tb)
3179{
3180	u32 protocols;
3181
3182	if (tb == NULL)
3183		return;
3184
3185	protocols = nla_get_u32(tb);
3186	wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3187		   "mode");
3188	capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3189	capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3190}
3191
3192
3193static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3194{
3195	struct nlattr *tb[NL80211_ATTR_MAX + 1];
3196	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3197	struct wiphy_info_data *info = arg;
3198	struct wpa_driver_capa *capa = info->capa;
3199	struct wpa_driver_nl80211_data *drv = info->drv;
3200
3201	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3202		  genlmsg_attrlen(gnlh, 0), NULL);
3203
3204	if (tb[NL80211_ATTR_WIPHY_NAME])
3205		os_strncpy(drv->phyname,
3206			   nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3207			   sizeof(drv->phyname));
3208	if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
3209		capa->max_scan_ssids =
3210			nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3211
3212	if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3213		capa->max_sched_scan_ssids =
3214			nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3215
3216	if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3217		capa->max_match_sets =
3218			nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3219
3220	if (tb[NL80211_ATTR_MAC_ACL_MAX])
3221		capa->max_acl_mac_addrs =
3222			nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3223
3224	wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3225	wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3226	wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
3227
3228	if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3229		wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3230			   "off-channel TX");
3231		capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3232	}
3233
3234	if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3235		wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3236		capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3237	}
3238
3239	wiphy_info_max_roc(capa,
3240			   tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
3241
3242	if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3243		capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
3244
3245	wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3246			tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
3247
3248	if (tb[NL80211_ATTR_DEVICE_AP_SME])
3249		info->device_ap_sme = 1;
3250
3251	wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3252	wiphy_info_probe_resp_offload(capa,
3253				      tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
3254
3255	if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3256	    drv->extended_capa == NULL) {
3257		drv->extended_capa =
3258			os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3259		if (drv->extended_capa) {
3260			os_memcpy(drv->extended_capa,
3261				  nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3262				  nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3263			drv->extended_capa_len =
3264				nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3265		}
3266		drv->extended_capa_mask =
3267			os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3268		if (drv->extended_capa_mask) {
3269			os_memcpy(drv->extended_capa_mask,
3270				  nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3271				  nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3272		} else {
3273			os_free(drv->extended_capa);
3274			drv->extended_capa = NULL;
3275			drv->extended_capa_len = 0;
3276		}
3277	}
3278
3279	return NL_SKIP;
3280}
3281
3282
3283static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3284				       struct wiphy_info_data *info)
3285{
3286	u32 feat;
3287	struct nl_msg *msg;
3288
3289	os_memset(info, 0, sizeof(*info));
3290	info->capa = &drv->capa;
3291	info->drv = drv;
3292
3293	msg = nlmsg_alloc();
3294	if (!msg)
3295		return -1;
3296
3297	feat = get_nl80211_protocol_features(drv);
3298	if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3299		nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3300	else
3301		nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
3302
3303	NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
3304	if (nl80211_set_iface_id(msg, &drv->first_bss) < 0)
3305		goto nla_put_failure;
3306
3307	if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3308		return -1;
3309
3310	if (info->auth_supported)
3311		drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3312	else if (!info->connect_supported) {
3313		wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3314			   "authentication/association or connect commands");
3315		info->error = 1;
3316	}
3317
3318	if (info->p2p_go_supported && info->p2p_client_supported)
3319		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3320	if (info->p2p_concurrent) {
3321		wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3322			   "interface (driver advertised support)");
3323		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3324		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3325	}
3326	if (info->num_multichan_concurrent > 1) {
3327		wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3328			   "concurrent (driver advertised support)");
3329		drv->capa.num_multichan_concurrent =
3330			info->num_multichan_concurrent;
3331	}
3332
3333	/* default to 5000 since early versions of mac80211 don't set it */
3334	if (!drv->capa.max_remain_on_chan)
3335		drv->capa.max_remain_on_chan = 5000;
3336
3337	return 0;
3338nla_put_failure:
3339	nlmsg_free(msg);
3340	return -1;
3341}
3342
3343
3344static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3345{
3346	struct wiphy_info_data info;
3347	if (wpa_driver_nl80211_get_info(drv, &info))
3348		return -1;
3349
3350	if (info.error)
3351		return -1;
3352
3353	drv->has_capability = 1;
3354	/* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
3355	drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3356		WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3357		WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3358		WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
3359	drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
3360		WPA_DRIVER_CAPA_ENC_WEP104 |
3361		WPA_DRIVER_CAPA_ENC_TKIP |
3362		WPA_DRIVER_CAPA_ENC_CCMP;
3363	drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3364		WPA_DRIVER_AUTH_SHARED |
3365		WPA_DRIVER_AUTH_LEAP;
3366
3367	drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3368	drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
3369	drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3370
3371	if (!info.device_ap_sme) {
3372		drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
3373
3374		/*
3375		 * No AP SME is currently assumed to also indicate no AP MLME
3376		 * in the driver/firmware.
3377		 */
3378		drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3379	}
3380
3381	drv->device_ap_sme = info.device_ap_sme;
3382	drv->poll_command_supported = info.poll_command_supported;
3383	drv->data_tx_status = info.data_tx_status;
3384
3385#ifdef ANDROID_P2P
3386	if(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
3387		/* Driver is new enough to support monitorless mode*/
3388		wpa_printf(MSG_DEBUG, "nl80211: Driver is new "
3389			  "enough to support monitor-less mode");
3390		drv->use_monitor = 0;
3391	}
3392#else
3393	/*
3394	 * If poll command and tx status are supported, mac80211 is new enough
3395	 * to have everything we need to not need monitor interfaces.
3396	 */
3397	drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
3398#endif
3399
3400	if (drv->device_ap_sme && drv->use_monitor) {
3401		/*
3402		 * Non-mac80211 drivers may not support monitor interface.
3403		 * Make sure we do not get stuck with incorrect capability here
3404		 * by explicitly testing this.
3405		 */
3406		if (!info.monitor_supported) {
3407			wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3408				   "with device_ap_sme since no monitor mode "
3409				   "support detected");
3410			drv->use_monitor = 0;
3411		}
3412	}
3413
3414	/*
3415	 * If we aren't going to use monitor interfaces, but the
3416	 * driver doesn't support data TX status, we won't get TX
3417	 * status for EAPOL frames.
3418	 */
3419	if (!drv->use_monitor && !info.data_tx_status)
3420		drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3421
3422	return 0;
3423}
3424
3425
3426#ifdef ANDROID
3427static int android_genl_ctrl_resolve(struct nl_handle *handle,
3428				     const char *name)
3429{
3430	/*
3431	 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3432	 * need to work around that.
3433	 */
3434	struct nl_cache *cache = NULL;
3435	struct genl_family *nl80211 = NULL;
3436	int id = -1;
3437
3438	if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3439		wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3440			   "netlink cache");
3441		goto fail;
3442	}
3443
3444	nl80211 = genl_ctrl_search_by_name(cache, name);
3445	if (nl80211 == NULL)
3446		goto fail;
3447
3448	id = genl_family_get_id(nl80211);
3449
3450fail:
3451	if (nl80211)
3452		genl_family_put(nl80211);
3453	if (cache)
3454		nl_cache_free(cache);
3455
3456	return id;
3457}
3458#define genl_ctrl_resolve android_genl_ctrl_resolve
3459#endif /* ANDROID */
3460
3461
3462static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
3463{
3464	int ret;
3465
3466	global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3467	if (global->nl_cb == NULL) {
3468		wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3469			   "callbacks");
3470		return -1;
3471	}
3472
3473	global->nl = nl_create_handle(global->nl_cb, "nl");
3474	if (global->nl == NULL)
3475		goto err;
3476
3477	global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
3478	if (global->nl80211_id < 0) {
3479		wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3480			   "found");
3481		goto err;
3482	}
3483
3484	global->nl_event = nl_create_handle(global->nl_cb, "event");
3485	if (global->nl_event == NULL)
3486		goto err;
3487
3488	ret = nl_get_multicast_id(global, "nl80211", "scan");
3489	if (ret >= 0)
3490		ret = nl_socket_add_membership(global->nl_event, ret);
3491	if (ret < 0) {
3492		wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3493			   "membership for scan events: %d (%s)",
3494			   ret, strerror(-ret));
3495		goto err;
3496	}
3497
3498	ret = nl_get_multicast_id(global, "nl80211", "mlme");
3499	if (ret >= 0)
3500		ret = nl_socket_add_membership(global->nl_event, ret);
3501	if (ret < 0) {
3502		wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3503			   "membership for mlme events: %d (%s)",
3504			   ret, strerror(-ret));
3505		goto err;
3506	}
3507
3508	ret = nl_get_multicast_id(global, "nl80211", "regulatory");
3509	if (ret >= 0)
3510		ret = nl_socket_add_membership(global->nl_event, ret);
3511	if (ret < 0) {
3512		wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3513			   "membership for regulatory events: %d (%s)",
3514			   ret, strerror(-ret));
3515		/* Continue without regulatory events */
3516	}
3517
3518	nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3519		  no_seq_check, NULL);
3520	nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3521		  process_global_event, global);
3522
3523	eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
3524				 wpa_driver_nl80211_event_receive,
3525				 global->nl_cb, global->nl_event);
3526
3527	return 0;
3528
3529err:
3530	nl_destroy_handles(&global->nl_event);
3531	nl_destroy_handles(&global->nl);
3532	nl_cb_put(global->nl_cb);
3533	global->nl_cb = NULL;
3534	return -1;
3535}
3536
3537
3538static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
3539{
3540	drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3541	if (!drv->nl_cb) {
3542		wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
3543		return -1;
3544	}
3545
3546	nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3547		  no_seq_check, NULL);
3548	nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3549		  process_drv_event, drv);
3550
3551	return 0;
3552}
3553
3554
3555static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3556{
3557	wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
3558	/*
3559	 * This may be for any interface; use ifdown event to disable
3560	 * interface.
3561	 */
3562}
3563
3564
3565static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
3566{
3567	struct wpa_driver_nl80211_data *drv = ctx;
3568	wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
3569	if (i802_set_iface_flags(&drv->first_bss, 1)) {
3570		wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
3571			   "after rfkill unblock");
3572		return;
3573	}
3574	/* rtnetlink ifup handler will report interface as enabled */
3575}
3576
3577
3578static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
3579						      void *eloop_ctx,
3580						      void *handle)
3581{
3582	struct wpa_driver_nl80211_data *drv = eloop_ctx;
3583	u8 data[2048];
3584	struct msghdr msg;
3585	struct iovec entry;
3586	u8 control[512];
3587	struct cmsghdr *cmsg;
3588	int res, found_ee = 0, found_wifi = 0, acked = 0;
3589	union wpa_event_data event;
3590
3591	memset(&msg, 0, sizeof(msg));
3592	msg.msg_iov = &entry;
3593	msg.msg_iovlen = 1;
3594	entry.iov_base = data;
3595	entry.iov_len = sizeof(data);
3596	msg.msg_control = &control;
3597	msg.msg_controllen = sizeof(control);
3598
3599	res = recvmsg(sock, &msg, MSG_ERRQUEUE);
3600	/* if error or not fitting 802.3 header, return */
3601	if (res < 14)
3602		return;
3603
3604	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
3605	{
3606		if (cmsg->cmsg_level == SOL_SOCKET &&
3607		    cmsg->cmsg_type == SCM_WIFI_STATUS) {
3608			int *ack;
3609
3610			found_wifi = 1;
3611			ack = (void *)CMSG_DATA(cmsg);
3612			acked = *ack;
3613		}
3614
3615		if (cmsg->cmsg_level == SOL_PACKET &&
3616		    cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
3617			struct sock_extended_err *err =
3618				(struct sock_extended_err *)CMSG_DATA(cmsg);
3619
3620			if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
3621				found_ee = 1;
3622		}
3623	}
3624
3625	if (!found_ee || !found_wifi)
3626		return;
3627
3628	memset(&event, 0, sizeof(event));
3629	event.eapol_tx_status.dst = data;
3630	event.eapol_tx_status.data = data + 14;
3631	event.eapol_tx_status.data_len = res - 14;
3632	event.eapol_tx_status.ack = acked;
3633	wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
3634}
3635
3636
3637static int nl80211_init_bss(struct i802_bss *bss)
3638{
3639	bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3640	if (!bss->nl_cb)
3641		return -1;
3642
3643	nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3644		  no_seq_check, NULL);
3645	nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3646		  process_bss_event, bss);
3647
3648	return 0;
3649}
3650
3651
3652static void nl80211_destroy_bss(struct i802_bss *bss)
3653{
3654	nl_cb_put(bss->nl_cb);
3655	bss->nl_cb = NULL;
3656}
3657
3658
3659/**
3660 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
3661 * @ctx: context to be used when calling wpa_supplicant functions,
3662 * e.g., wpa_supplicant_event()
3663 * @ifname: interface name, e.g., wlan0
3664 * @global_priv: private driver global data from global_init()
3665 * Returns: Pointer to private data, %NULL on failure
3666 */
3667static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
3668				      void *global_priv)
3669{
3670	struct wpa_driver_nl80211_data *drv;
3671	struct rfkill_config *rcfg;
3672	struct i802_bss *bss;
3673
3674	if (global_priv == NULL)
3675		return NULL;
3676	drv = os_zalloc(sizeof(*drv));
3677	if (drv == NULL)
3678		return NULL;
3679	drv->global = global_priv;
3680	drv->ctx = ctx;
3681	bss = &drv->first_bss;
3682	bss->drv = drv;
3683	bss->ctx = ctx;
3684
3685	os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
3686	drv->monitor_ifidx = -1;
3687	drv->monitor_sock = -1;
3688	drv->eapol_tx_sock = -1;
3689	drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
3690
3691	if (wpa_driver_nl80211_init_nl(drv)) {
3692		os_free(drv);
3693		return NULL;
3694	}
3695
3696	if (nl80211_init_bss(bss))
3697		goto failed;
3698
3699	rcfg = os_zalloc(sizeof(*rcfg));
3700	if (rcfg == NULL)
3701		goto failed;
3702	rcfg->ctx = drv;
3703	os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
3704	rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
3705	rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
3706	drv->rfkill = rfkill_init(rcfg);
3707	if (drv->rfkill == NULL) {
3708		wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
3709		os_free(rcfg);
3710	}
3711
3712	if (wpa_driver_nl80211_finish_drv_init(drv))
3713		goto failed;
3714
3715	drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
3716	if (drv->eapol_tx_sock < 0)
3717		goto failed;
3718
3719	if (drv->data_tx_status) {
3720		int enabled = 1;
3721
3722		if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
3723			       &enabled, sizeof(enabled)) < 0) {
3724			wpa_printf(MSG_DEBUG,
3725				"nl80211: wifi status sockopt failed\n");
3726			drv->data_tx_status = 0;
3727			if (!drv->use_monitor)
3728				drv->capa.flags &=
3729					~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3730		} else {
3731			eloop_register_read_sock(drv->eapol_tx_sock,
3732				wpa_driver_nl80211_handle_eapol_tx_status,
3733				drv, NULL);
3734		}
3735	}
3736
3737	if (drv->global) {
3738		dl_list_add(&drv->global->interfaces, &drv->list);
3739		drv->in_interface_list = 1;
3740	}
3741
3742	return bss;
3743
3744failed:
3745	wpa_driver_nl80211_deinit(bss);
3746	return NULL;
3747}
3748
3749
3750static int nl80211_register_frame(struct i802_bss *bss,
3751				  struct nl_handle *nl_handle,
3752				  u16 type, const u8 *match, size_t match_len)
3753{
3754	struct wpa_driver_nl80211_data *drv = bss->drv;
3755	struct nl_msg *msg;
3756	int ret = -1;
3757
3758	msg = nlmsg_alloc();
3759	if (!msg)
3760		return -1;
3761
3762	wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
3763		   type, nl_handle);
3764	wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3765		    match, match_len);
3766
3767	nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
3768
3769	if (nl80211_set_iface_id(msg, bss) < 0)
3770		goto nla_put_failure;
3771
3772	NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
3773	NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3774
3775	ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
3776	msg = NULL;
3777	if (ret) {
3778		wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3779			   "failed (type=%u): ret=%d (%s)",
3780			   type, ret, strerror(-ret));
3781		wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3782			    match, match_len);
3783		goto nla_put_failure;
3784	}
3785	ret = 0;
3786nla_put_failure:
3787	nlmsg_free(msg);
3788	return ret;
3789}
3790
3791
3792static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3793{
3794	struct wpa_driver_nl80211_data *drv = bss->drv;
3795
3796	if (bss->nl_mgmt) {
3797		wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
3798			   "already on! (nl_mgmt=%p)", bss->nl_mgmt);
3799		return -1;
3800	}
3801
3802	bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3803	if (bss->nl_mgmt == NULL)
3804		return -1;
3805
3806	eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
3807				 wpa_driver_nl80211_event_receive, bss->nl_cb,
3808				 bss->nl_mgmt);
3809
3810	return 0;
3811}
3812
3813
3814static int nl80211_register_action_frame(struct i802_bss *bss,
3815					 const u8 *match, size_t match_len)
3816{
3817	u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
3818	return nl80211_register_frame(bss, bss->nl_mgmt,
3819				      type, match, match_len);
3820}
3821
3822
3823static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
3824{
3825	struct wpa_driver_nl80211_data *drv = bss->drv;
3826
3827	if (nl80211_alloc_mgmt_handle(bss))
3828		return -1;
3829	wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3830		   "handle %p", bss->nl_mgmt);
3831
3832#ifdef CONFIG_INTERWORKING
3833	/* QoS Map Configure */
3834	if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
3835		return -1;
3836#endif /* CONFIG_INTERWORKING */
3837#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
3838	/* GAS Initial Request */
3839	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
3840		return -1;
3841	/* GAS Initial Response */
3842	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
3843		return -1;
3844	/* GAS Comeback Request */
3845	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
3846		return -1;
3847	/* GAS Comeback Response */
3848	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
3849		return -1;
3850#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3851#ifdef CONFIG_P2P
3852	/* P2P Public Action */
3853	if (nl80211_register_action_frame(bss,
3854					  (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3855					  6) < 0)
3856		return -1;
3857	/* P2P Action */
3858	if (nl80211_register_action_frame(bss,
3859					  (u8 *) "\x7f\x50\x6f\x9a\x09",
3860					  5) < 0)
3861		return -1;
3862#endif /* CONFIG_P2P */
3863#ifdef CONFIG_IEEE80211W
3864	/* SA Query Response */
3865	if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
3866		return -1;
3867#endif /* CONFIG_IEEE80211W */
3868#ifdef CONFIG_TDLS
3869	if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3870		/* TDLS Discovery Response */
3871		if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
3872		    0)
3873			return -1;
3874	}
3875#endif /* CONFIG_TDLS */
3876
3877	/* FT Action frames */
3878	if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
3879		return -1;
3880	else
3881		drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3882			WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3883
3884	/* WNM - BSS Transition Management Request */
3885	if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3886		return -1;
3887	/* WNM-Sleep Mode Response */
3888	if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
3889		return -1;
3890
3891	return 0;
3892}
3893
3894
3895static int nl80211_register_spurious_class3(struct i802_bss *bss)
3896{
3897	struct wpa_driver_nl80211_data *drv = bss->drv;
3898	struct nl_msg *msg;
3899	int ret = -1;
3900
3901	msg = nlmsg_alloc();
3902	if (!msg)
3903		return -1;
3904
3905	nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3906
3907	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3908
3909	ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
3910	msg = NULL;
3911	if (ret) {
3912		wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3913			   "failed: ret=%d (%s)",
3914			   ret, strerror(-ret));
3915		goto nla_put_failure;
3916	}
3917	ret = 0;
3918nla_put_failure:
3919	nlmsg_free(msg);
3920	return ret;
3921}
3922
3923
3924static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3925{
3926	static const int stypes[] = {
3927		WLAN_FC_STYPE_AUTH,
3928		WLAN_FC_STYPE_ASSOC_REQ,
3929		WLAN_FC_STYPE_REASSOC_REQ,
3930		WLAN_FC_STYPE_DISASSOC,
3931		WLAN_FC_STYPE_DEAUTH,
3932		WLAN_FC_STYPE_ACTION,
3933		WLAN_FC_STYPE_PROBE_REQ,
3934/* Beacon doesn't work as mac80211 doesn't currently allow
3935 * it, but it wouldn't really be the right thing anyway as
3936 * it isn't per interface ... maybe just dump the scan
3937 * results periodically for OLBC?
3938 */
3939//		WLAN_FC_STYPE_BEACON,
3940	};
3941	unsigned int i;
3942
3943	if (nl80211_alloc_mgmt_handle(bss))
3944		return -1;
3945	wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3946		   "handle %p", bss->nl_mgmt);
3947
3948	for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
3949		if (nl80211_register_frame(bss, bss->nl_mgmt,
3950					   (WLAN_FC_TYPE_MGMT << 2) |
3951					   (stypes[i] << 4),
3952					   NULL, 0) < 0) {
3953			goto out_err;
3954		}
3955	}
3956
3957	if (nl80211_register_spurious_class3(bss))
3958		goto out_err;
3959
3960	if (nl80211_get_wiphy_data_ap(bss) == NULL)
3961		goto out_err;
3962
3963	return 0;
3964
3965out_err:
3966	eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3967	nl_destroy_handles(&bss->nl_mgmt);
3968	return -1;
3969}
3970
3971
3972static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3973{
3974	if (nl80211_alloc_mgmt_handle(bss))
3975		return -1;
3976	wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3977		   "handle %p (device SME)", bss->nl_mgmt);
3978
3979	if (nl80211_register_frame(bss, bss->nl_mgmt,
3980				   (WLAN_FC_TYPE_MGMT << 2) |
3981				   (WLAN_FC_STYPE_ACTION << 4),
3982				   NULL, 0) < 0)
3983		goto out_err;
3984
3985	return 0;
3986
3987out_err:
3988	eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3989	nl_destroy_handles(&bss->nl_mgmt);
3990	return -1;
3991}
3992
3993
3994static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
3995{
3996	if (bss->nl_mgmt == NULL)
3997		return;
3998	wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3999		   "(%s)", bss->nl_mgmt, reason);
4000	eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
4001	nl_destroy_handles(&bss->nl_mgmt);
4002
4003	nl80211_put_wiphy_data_ap(bss);
4004}
4005
4006
4007static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4008{
4009	wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4010}
4011
4012
4013static void nl80211_del_p2pdev(struct i802_bss *bss)
4014{
4015	struct wpa_driver_nl80211_data *drv = bss->drv;
4016	struct nl_msg *msg;
4017	int ret;
4018
4019	msg = nlmsg_alloc();
4020	if (!msg)
4021		return;
4022
4023	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4024	NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4025
4026	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4027	msg = NULL;
4028
4029	wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4030		   bss->ifname, (long long unsigned int) bss->wdev_id,
4031		   strerror(ret));
4032
4033nla_put_failure:
4034	nlmsg_free(msg);
4035}
4036
4037
4038static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4039{
4040	struct wpa_driver_nl80211_data *drv = bss->drv;
4041	struct nl_msg *msg;
4042	int ret = -1;
4043
4044	msg = nlmsg_alloc();
4045	if (!msg)
4046		return -1;
4047
4048	if (start)
4049		nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4050	else
4051		nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4052
4053	NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4054
4055	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4056	msg = NULL;
4057
4058	wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4059		   start ? "Start" : "Stop",
4060		   bss->ifname, (long long unsigned int) bss->wdev_id,
4061		   strerror(ret));
4062
4063nla_put_failure:
4064	nlmsg_free(msg);
4065	return ret;
4066}
4067
4068
4069static int i802_set_iface_flags(struct i802_bss *bss, int up)
4070{
4071	enum nl80211_iftype nlmode;
4072
4073	nlmode = nl80211_get_ifmode(bss);
4074	if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4075		return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4076					     bss->ifname, up);
4077	}
4078
4079	/* P2P Device has start/stop which is equivalent */
4080	return nl80211_set_p2pdev(bss, up);
4081}
4082
4083
4084static int
4085wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
4086{
4087#ifndef HOSTAPD
4088	enum nl80211_iftype nlmode = NL80211_IFTYPE_STATION;
4089#endif /* HOSTAPD */
4090	struct i802_bss *bss = &drv->first_bss;
4091	int send_rfkill_event = 0;
4092
4093	drv->ifindex = if_nametoindex(bss->ifname);
4094	bss->ifindex = drv->ifindex;
4095	bss->wdev_id = drv->global->if_add_wdevid;
4096	bss->wdev_id_set = drv->global->if_add_wdevid_set;
4097
4098	bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4099	bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
4100	drv->global->if_add_wdevid_set = 0;
4101
4102	if (wpa_driver_nl80211_capa(drv))
4103		return -1;
4104
4105	wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4106		   bss->ifname, drv->phyname);
4107
4108#ifndef HOSTAPD
4109	if (bss->if_dynamic)
4110		nlmode = nl80211_get_ifmode(bss);
4111
4112	/*
4113	 * Make sure the interface starts up in station mode unless this is a
4114	 * dynamically added interface (e.g., P2P) that was already configured
4115	 * with proper iftype.
4116	 */
4117	if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
4118		wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to use managed mode");
4119		return -1;
4120	}
4121	drv->nlmode = nlmode;
4122
4123	if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
4124		int ret = nl80211_set_p2pdev(bss, 1);
4125		if (ret < 0)
4126			wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device");
4127		nl80211_get_macaddr(bss);
4128		return ret;
4129	}
4130
4131	if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
4132		if (rfkill_is_blocked(drv->rfkill)) {
4133			wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4134				   "interface '%s' due to rfkill",
4135				   bss->ifname);
4136			drv->if_disabled = 1;
4137			send_rfkill_event = 1;
4138		} else {
4139			wpa_printf(MSG_ERROR, "nl80211: Could not set "
4140				   "interface '%s' UP", bss->ifname);
4141			return -1;
4142		}
4143	}
4144
4145	netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4146			       1, IF_OPER_DORMANT);
4147#endif /* HOSTAPD */
4148
4149	if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4150			       bss->addr))
4151		return -1;
4152
4153	if (send_rfkill_event) {
4154		eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4155				       drv, drv->ctx);
4156	}
4157
4158	return 0;
4159}
4160
4161
4162static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4163{
4164	struct nl_msg *msg;
4165
4166	msg = nlmsg_alloc();
4167	if (!msg)
4168		return -ENOMEM;
4169
4170	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
4171	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4172
4173	return send_and_recv_msgs(drv, msg, NULL, NULL);
4174 nla_put_failure:
4175	nlmsg_free(msg);
4176	return -ENOBUFS;
4177}
4178
4179
4180/**
4181 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
4182 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
4183 *
4184 * Shut down driver interface and processing of driver events. Free
4185 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4186 */
4187static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
4188{
4189	struct wpa_driver_nl80211_data *drv = bss->drv;
4190
4191	bss->in_deinit = 1;
4192	if (drv->data_tx_status)
4193		eloop_unregister_read_sock(drv->eapol_tx_sock);
4194	if (drv->eapol_tx_sock >= 0)
4195		close(drv->eapol_tx_sock);
4196
4197	if (bss->nl_preq)
4198		wpa_driver_nl80211_probe_req_report(bss, 0);
4199	if (bss->added_if_into_bridge) {
4200		if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4201				    bss->ifname) < 0)
4202			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4203				   "interface %s from bridge %s: %s",
4204				   bss->ifname, bss->brname, strerror(errno));
4205	}
4206	if (bss->added_bridge) {
4207		if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
4208			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4209				   "bridge %s: %s",
4210				   bss->brname, strerror(errno));
4211	}
4212
4213	nl80211_remove_monitor_interface(drv);
4214
4215	if (is_ap_interface(drv->nlmode))
4216		wpa_driver_nl80211_del_beacon(drv);
4217
4218#ifdef HOSTAPD
4219	if (drv->last_freq_ht) {
4220		/* Clear HT flags from the driver */
4221		struct hostapd_freq_params freq;
4222		os_memset(&freq, 0, sizeof(freq));
4223		freq.freq = drv->last_freq;
4224		wpa_driver_nl80211_set_freq(bss, &freq);
4225	}
4226
4227	if (drv->eapol_sock >= 0) {
4228		eloop_unregister_read_sock(drv->eapol_sock);
4229		close(drv->eapol_sock);
4230	}
4231
4232	if (drv->if_indices != drv->default_if_indices)
4233		os_free(drv->if_indices);
4234#endif /* HOSTAPD */
4235
4236	if (drv->disabled_11b_rates)
4237		nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4238
4239	netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4240			       IF_OPER_UP);
4241	rfkill_deinit(drv->rfkill);
4242
4243	eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4244
4245	(void) i802_set_iface_flags(bss, 0);
4246	if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4247		wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
4248		nl80211_mgmt_unsubscribe(bss, "deinit");
4249	} else {
4250		nl80211_mgmt_unsubscribe(bss, "deinit");
4251		nl80211_del_p2pdev(bss);
4252	}
4253	nl_cb_put(drv->nl_cb);
4254
4255	nl80211_destroy_bss(&drv->first_bss);
4256
4257	os_free(drv->filter_ssids);
4258
4259	os_free(drv->auth_ie);
4260
4261	if (drv->in_interface_list)
4262		dl_list_del(&drv->list);
4263
4264	os_free(drv->extended_capa);
4265	os_free(drv->extended_capa_mask);
4266	os_free(drv);
4267}
4268
4269
4270/**
4271 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4272 * @eloop_ctx: Driver private data
4273 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4274 *
4275 * This function can be used as registered timeout when starting a scan to
4276 * generate a scan completed event if the driver does not report this.
4277 */
4278static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4279{
4280	struct wpa_driver_nl80211_data *drv = eloop_ctx;
4281	if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
4282		wpa_driver_nl80211_set_mode(&drv->first_bss,
4283					    drv->ap_scan_as_station);
4284		drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
4285	}
4286	wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4287	wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4288}
4289
4290
4291static struct nl_msg *
4292nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
4293		    struct wpa_driver_scan_params *params, u64 *wdev_id)
4294{
4295	struct nl_msg *msg;
4296	size_t i;
4297
4298	msg = nlmsg_alloc();
4299	if (!msg)
4300		return NULL;
4301
4302	nl80211_cmd(drv, msg, 0, cmd);
4303
4304	if (!wdev_id)
4305		NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4306	else
4307		NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
4308
4309	if (params->num_ssids) {
4310		struct nlattr *ssids;
4311
4312		ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4313		if (ssids == NULL)
4314			goto fail;
4315		for (i = 0; i < params->num_ssids; i++) {
4316			wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4317					  params->ssids[i].ssid,
4318					  params->ssids[i].ssid_len);
4319			if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4320				    params->ssids[i].ssid) < 0)
4321				goto fail;
4322		}
4323		nla_nest_end(msg, ssids);
4324	}
4325
4326	if (params->extra_ies) {
4327		wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4328			    params->extra_ies, params->extra_ies_len);
4329		if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4330			    params->extra_ies) < 0)
4331			goto fail;
4332	}
4333
4334	if (params->freqs) {
4335		struct nlattr *freqs;
4336		freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4337		if (freqs == NULL)
4338			goto fail;
4339		for (i = 0; params->freqs[i]; i++) {
4340			wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4341				   "MHz", params->freqs[i]);
4342			if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
4343				goto fail;
4344		}
4345		nla_nest_end(msg, freqs);
4346	}
4347
4348	os_free(drv->filter_ssids);
4349	drv->filter_ssids = params->filter_ssids;
4350	params->filter_ssids = NULL;
4351	drv->num_filter_ssids = params->num_filter_ssids;
4352
4353	return msg;
4354
4355fail:
4356nla_put_failure:
4357	nlmsg_free(msg);
4358	return NULL;
4359}
4360
4361
4362/**
4363 * wpa_driver_nl80211_scan - Request the driver to initiate scan
4364 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
4365 * @params: Scan parameters
4366 * Returns: 0 on success, -1 on failure
4367 */
4368static int wpa_driver_nl80211_scan(struct i802_bss *bss,
4369				   struct wpa_driver_scan_params *params)
4370{
4371	struct wpa_driver_nl80211_data *drv = bss->drv;
4372	int ret = -1, timeout;
4373	struct nl_msg *msg = NULL;
4374
4375	wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
4376	drv->scan_for_auth = 0;
4377
4378	msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
4379				  bss->wdev_id_set ? &bss->wdev_id : NULL);
4380	if (!msg)
4381		return -1;
4382
4383	if (params->p2p_probe) {
4384		struct nlattr *rates;
4385
4386		wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
4387
4388		rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
4389		if (rates == NULL)
4390			goto nla_put_failure;
4391
4392		/*
4393		 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
4394		 * by masking out everything else apart from the OFDM rates 6,
4395		 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
4396		 * rates are left enabled.
4397		 */
4398		NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
4399			"\x0c\x12\x18\x24\x30\x48\x60\x6c");
4400		nla_nest_end(msg, rates);
4401
4402		NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
4403	}
4404
4405	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4406	msg = NULL;
4407	if (ret) {
4408		wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
4409			   "(%s)", ret, strerror(-ret));
4410#ifdef HOSTAPD
4411		if (is_ap_interface(drv->nlmode)) {
4412			/*
4413			 * mac80211 does not allow scan requests in AP mode, so
4414			 * try to do this in station mode.
4415			 */
4416			if (wpa_driver_nl80211_set_mode(
4417				    bss, NL80211_IFTYPE_STATION))
4418				goto nla_put_failure;
4419
4420			if (wpa_driver_nl80211_scan(bss, params)) {
4421				wpa_driver_nl80211_set_mode(bss, drv->nlmode);
4422				goto nla_put_failure;
4423			}
4424
4425			/* Restore AP mode when processing scan results */
4426			drv->ap_scan_as_station = drv->nlmode;
4427			ret = 0;
4428		} else
4429			goto nla_put_failure;
4430#else /* HOSTAPD */
4431		goto nla_put_failure;
4432#endif /* HOSTAPD */
4433	}
4434
4435	drv->scan_state = SCAN_REQUESTED;
4436	/* Not all drivers generate "scan completed" wireless event, so try to
4437	 * read results after a timeout. */
4438	timeout = 10;
4439	if (drv->scan_complete_events) {
4440		/*
4441		 * The driver seems to deliver events to notify when scan is
4442		 * complete, so use longer timeout to avoid race conditions
4443		 * with scanning and following association request.
4444		 */
4445		timeout = 30;
4446	}
4447	wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
4448		   "seconds", ret, timeout);
4449	eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4450	eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
4451			       drv, drv->ctx);
4452
4453nla_put_failure:
4454	nlmsg_free(msg);
4455	return ret;
4456}
4457
4458
4459/**
4460 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
4461 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4462 * @params: Scan parameters
4463 * @interval: Interval between scan cycles in milliseconds
4464 * Returns: 0 on success, -1 on failure or if not supported
4465 */
4466static int wpa_driver_nl80211_sched_scan(void *priv,
4467					 struct wpa_driver_scan_params *params,
4468					 u32 interval)
4469{
4470	struct i802_bss *bss = priv;
4471	struct wpa_driver_nl80211_data *drv = bss->drv;
4472	int ret = -1;
4473	struct nl_msg *msg;
4474	size_t i;
4475
4476	wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4477
4478#ifdef ANDROID
4479	if (!drv->capa.sched_scan_supported)
4480		return android_pno_start(bss, params);
4481#endif /* ANDROID */
4482
4483	msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
4484				  bss->wdev_id_set ? &bss->wdev_id : NULL);
4485	if (!msg)
4486		goto nla_put_failure;
4487
4488	NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
4489
4490	if ((drv->num_filter_ssids &&
4491	    (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
4492	    params->filter_rssi) {
4493		struct nlattr *match_sets;
4494		match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
4495		if (match_sets == NULL)
4496			goto nla_put_failure;
4497
4498		for (i = 0; i < drv->num_filter_ssids; i++) {
4499			struct nlattr *match_set_ssid;
4500			wpa_hexdump_ascii(MSG_MSGDUMP,
4501					  "nl80211: Sched scan filter SSID",
4502					  drv->filter_ssids[i].ssid,
4503					  drv->filter_ssids[i].ssid_len);
4504
4505			match_set_ssid = nla_nest_start(msg, i + 1);
4506			if (match_set_ssid == NULL)
4507				goto nla_put_failure;
4508			NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
4509				drv->filter_ssids[i].ssid_len,
4510				drv->filter_ssids[i].ssid);
4511
4512			nla_nest_end(msg, match_set_ssid);
4513		}
4514
4515		if (params->filter_rssi) {
4516			struct nlattr *match_set_rssi;
4517			match_set_rssi = nla_nest_start(msg, 0);
4518			if (match_set_rssi == NULL)
4519				goto nla_put_failure;
4520			NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
4521				    params->filter_rssi);
4522			wpa_printf(MSG_MSGDUMP,
4523				   "nl80211: Sched scan RSSI filter %d dBm",
4524				   params->filter_rssi);
4525			nla_nest_end(msg, match_set_rssi);
4526		}
4527
4528		nla_nest_end(msg, match_sets);
4529	}
4530
4531	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4532
4533	/* TODO: if we get an error here, we should fall back to normal scan */
4534
4535	msg = NULL;
4536	if (ret) {
4537		wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
4538			   "ret=%d (%s)", ret, strerror(-ret));
4539		goto nla_put_failure;
4540	}
4541
4542	wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
4543		   "scan interval %d msec", ret, interval);
4544
4545nla_put_failure:
4546	nlmsg_free(msg);
4547	return ret;
4548}
4549
4550
4551/**
4552 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
4553 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4554 * Returns: 0 on success, -1 on failure or if not supported
4555 */
4556static int wpa_driver_nl80211_stop_sched_scan(void *priv)
4557{
4558	struct i802_bss *bss = priv;
4559	struct wpa_driver_nl80211_data *drv = bss->drv;
4560	int ret = 0;
4561	struct nl_msg *msg;
4562
4563#ifdef ANDROID
4564	if (!drv->capa.sched_scan_supported)
4565		return android_pno_stop(bss);
4566#endif /* ANDROID */
4567
4568	msg = nlmsg_alloc();
4569	if (!msg)
4570		return -1;
4571
4572	nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
4573
4574	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4575
4576	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4577	msg = NULL;
4578	if (ret) {
4579		wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
4580			   "ret=%d (%s)", ret, strerror(-ret));
4581		goto nla_put_failure;
4582	}
4583
4584	wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
4585
4586nla_put_failure:
4587	nlmsg_free(msg);
4588	return ret;
4589}
4590
4591
4592static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
4593{
4594	const u8 *end, *pos;
4595
4596	if (ies == NULL)
4597		return NULL;
4598
4599	pos = ies;
4600	end = ies + ies_len;
4601
4602	while (pos + 1 < end) {
4603		if (pos + 2 + pos[1] > end)
4604			break;
4605		if (pos[0] == ie)
4606			return pos;
4607		pos += 2 + pos[1];
4608	}
4609
4610	return NULL;
4611}
4612
4613
4614static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
4615				 const u8 *ie, size_t ie_len)
4616{
4617	const u8 *ssid;
4618	size_t i;
4619
4620	if (drv->filter_ssids == NULL)
4621		return 0;
4622
4623	ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
4624	if (ssid == NULL)
4625		return 1;
4626
4627	for (i = 0; i < drv->num_filter_ssids; i++) {
4628		if (ssid[1] == drv->filter_ssids[i].ssid_len &&
4629		    os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
4630		    0)
4631			return 0;
4632	}
4633
4634	return 1;
4635}
4636
4637
4638static int bss_info_handler(struct nl_msg *msg, void *arg)
4639{
4640	struct nlattr *tb[NL80211_ATTR_MAX + 1];
4641	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4642	struct nlattr *bss[NL80211_BSS_MAX + 1];
4643	static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
4644		[NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
4645		[NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
4646		[NL80211_BSS_TSF] = { .type = NLA_U64 },
4647		[NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
4648		[NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
4649		[NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
4650		[NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
4651		[NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
4652		[NL80211_BSS_STATUS] = { .type = NLA_U32 },
4653		[NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
4654		[NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
4655	};
4656	struct nl80211_bss_info_arg *_arg = arg;
4657	struct wpa_scan_results *res = _arg->res;
4658	struct wpa_scan_res **tmp;
4659	struct wpa_scan_res *r;
4660	const u8 *ie, *beacon_ie;
4661	size_t ie_len, beacon_ie_len;
4662	u8 *pos;
4663	size_t i;
4664
4665	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4666		  genlmsg_attrlen(gnlh, 0), NULL);
4667	if (!tb[NL80211_ATTR_BSS])
4668		return NL_SKIP;
4669	if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
4670			     bss_policy))
4671		return NL_SKIP;
4672	if (bss[NL80211_BSS_STATUS]) {
4673		enum nl80211_bss_status status;
4674		status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4675		if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4676		    bss[NL80211_BSS_FREQUENCY]) {
4677			_arg->assoc_freq =
4678				nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4679			wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
4680				   _arg->assoc_freq);
4681		}
4682		if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4683		    bss[NL80211_BSS_BSSID]) {
4684			os_memcpy(_arg->assoc_bssid,
4685				  nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
4686			wpa_printf(MSG_DEBUG, "nl80211: Associated with "
4687				   MACSTR, MAC2STR(_arg->assoc_bssid));
4688		}
4689	}
4690	if (!res)
4691		return NL_SKIP;
4692	if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
4693		ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4694		ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4695	} else {
4696		ie = NULL;
4697		ie_len = 0;
4698	}
4699	if (bss[NL80211_BSS_BEACON_IES]) {
4700		beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
4701		beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
4702	} else {
4703		beacon_ie = NULL;
4704		beacon_ie_len = 0;
4705	}
4706
4707	if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
4708				  ie ? ie_len : beacon_ie_len))
4709		return NL_SKIP;
4710
4711	r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
4712	if (r == NULL)
4713		return NL_SKIP;
4714	if (bss[NL80211_BSS_BSSID])
4715		os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
4716			  ETH_ALEN);
4717	if (bss[NL80211_BSS_FREQUENCY])
4718		r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4719	if (bss[NL80211_BSS_BEACON_INTERVAL])
4720		r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
4721	if (bss[NL80211_BSS_CAPABILITY])
4722		r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
4723	r->flags |= WPA_SCAN_NOISE_INVALID;
4724	if (bss[NL80211_BSS_SIGNAL_MBM]) {
4725		r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
4726		r->level /= 100; /* mBm to dBm */
4727		r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
4728	} else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
4729		r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
4730		r->flags |= WPA_SCAN_QUAL_INVALID;
4731	} else
4732		r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
4733	if (bss[NL80211_BSS_TSF])
4734		r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
4735	if (bss[NL80211_BSS_SEEN_MS_AGO])
4736		r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
4737	r->ie_len = ie_len;
4738	pos = (u8 *) (r + 1);
4739	if (ie) {
4740		os_memcpy(pos, ie, ie_len);
4741		pos += ie_len;
4742	}
4743	r->beacon_ie_len = beacon_ie_len;
4744	if (beacon_ie)
4745		os_memcpy(pos, beacon_ie, beacon_ie_len);
4746
4747	if (bss[NL80211_BSS_STATUS]) {
4748		enum nl80211_bss_status status;
4749		status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4750		switch (status) {
4751		case NL80211_BSS_STATUS_AUTHENTICATED:
4752			r->flags |= WPA_SCAN_AUTHENTICATED;
4753			break;
4754		case NL80211_BSS_STATUS_ASSOCIATED:
4755			r->flags |= WPA_SCAN_ASSOCIATED;
4756			break;
4757		default:
4758			break;
4759		}
4760	}
4761
4762	/*
4763	 * cfg80211 maintains separate BSS table entries for APs if the same
4764	 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
4765	 * not use frequency as a separate key in the BSS table, so filter out
4766	 * duplicated entries. Prefer associated BSS entry in such a case in
4767	 * order to get the correct frequency into the BSS table.
4768	 */
4769	for (i = 0; i < res->num; i++) {
4770		const u8 *s1, *s2;
4771		if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
4772			continue;
4773
4774		s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
4775				    res->res[i]->ie_len, WLAN_EID_SSID);
4776		s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
4777		if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
4778		    os_memcmp(s1, s2, 2 + s1[1]) != 0)
4779			continue;
4780
4781		/* Same BSSID,SSID was already included in scan results */
4782		wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
4783			   "for " MACSTR, MAC2STR(r->bssid));
4784
4785		if ((r->flags & WPA_SCAN_ASSOCIATED) &&
4786		    !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
4787			os_free(res->res[i]);
4788			res->res[i] = r;
4789		} else
4790			os_free(r);
4791		return NL_SKIP;
4792	}
4793
4794	tmp = os_realloc_array(res->res, res->num + 1,
4795			       sizeof(struct wpa_scan_res *));
4796	if (tmp == NULL) {
4797		os_free(r);
4798		return NL_SKIP;
4799	}
4800	tmp[res->num++] = r;
4801	res->res = tmp;
4802
4803	return NL_SKIP;
4804}
4805
4806
4807static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
4808				 const u8 *addr)
4809{
4810	if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
4811		wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
4812			   "mismatch (" MACSTR ")", MAC2STR(addr));
4813		wpa_driver_nl80211_mlme(drv, addr,
4814					NL80211_CMD_DEAUTHENTICATE,
4815					WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
4816	}
4817}
4818
4819
4820static void wpa_driver_nl80211_check_bss_status(
4821	struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
4822{
4823	size_t i;
4824
4825	for (i = 0; i < res->num; i++) {
4826		struct wpa_scan_res *r = res->res[i];
4827		if (r->flags & WPA_SCAN_AUTHENTICATED) {
4828			wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4829				   "indicates BSS status with " MACSTR
4830				   " as authenticated",
4831				   MAC2STR(r->bssid));
4832			if (is_sta_interface(drv->nlmode) &&
4833			    os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
4834			    os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
4835			    0) {
4836				wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
4837					   " in local state (auth=" MACSTR
4838					   " assoc=" MACSTR ")",
4839					   MAC2STR(drv->auth_bssid),
4840					   MAC2STR(drv->bssid));
4841				clear_state_mismatch(drv, r->bssid);
4842			}
4843		}
4844
4845		if (r->flags & WPA_SCAN_ASSOCIATED) {
4846			wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4847				   "indicate BSS status with " MACSTR
4848				   " as associated",
4849				   MAC2STR(r->bssid));
4850			if (is_sta_interface(drv->nlmode) &&
4851			    !drv->associated) {
4852				wpa_printf(MSG_DEBUG, "nl80211: Local state "
4853					   "(not associated) does not match "
4854					   "with BSS state");
4855				clear_state_mismatch(drv, r->bssid);
4856			} else if (is_sta_interface(drv->nlmode) &&
4857				   os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
4858				   0) {
4859				wpa_printf(MSG_DEBUG, "nl80211: Local state "
4860					   "(associated with " MACSTR ") does "
4861					   "not match with BSS state",
4862					   MAC2STR(drv->bssid));
4863				clear_state_mismatch(drv, r->bssid);
4864				clear_state_mismatch(drv, drv->bssid);
4865			}
4866		}
4867	}
4868}
4869
4870
4871static struct wpa_scan_results *
4872nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
4873{
4874	struct nl_msg *msg;
4875	struct wpa_scan_results *res;
4876	int ret;
4877	struct nl80211_bss_info_arg arg;
4878
4879	res = os_zalloc(sizeof(*res));
4880	if (res == NULL)
4881		return NULL;
4882	msg = nlmsg_alloc();
4883	if (!msg)
4884		goto nla_put_failure;
4885
4886	nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
4887	if (nl80211_set_iface_id(msg, &drv->first_bss) < 0)
4888		goto nla_put_failure;
4889
4890	arg.drv = drv;
4891	arg.res = res;
4892	ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
4893	msg = NULL;
4894	if (ret == 0) {
4895		wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4896			   "BSSes)", (unsigned long) res->num);
4897		nl80211_get_noise_for_scan_results(drv, res);
4898		return res;
4899	}
4900	wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4901		   "(%s)", ret, strerror(-ret));
4902nla_put_failure:
4903	nlmsg_free(msg);
4904	wpa_scan_results_free(res);
4905	return NULL;
4906}
4907
4908
4909/**
4910 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
4911 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
4912 * Returns: Scan results on success, -1 on failure
4913 */
4914static struct wpa_scan_results *
4915wpa_driver_nl80211_get_scan_results(void *priv)
4916{
4917	struct i802_bss *bss = priv;
4918	struct wpa_driver_nl80211_data *drv = bss->drv;
4919	struct wpa_scan_results *res;
4920
4921	res = nl80211_get_scan_results(drv);
4922	if (res)
4923		wpa_driver_nl80211_check_bss_status(drv, res);
4924	return res;
4925}
4926
4927
4928static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4929{
4930	struct wpa_scan_results *res;
4931	size_t i;
4932
4933	res = nl80211_get_scan_results(drv);
4934	if (res == NULL) {
4935		wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4936		return;
4937	}
4938
4939	wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4940	for (i = 0; i < res->num; i++) {
4941		struct wpa_scan_res *r = res->res[i];
4942		wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4943			   (int) i, (int) res->num, MAC2STR(r->bssid),
4944			   r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4945			   r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4946	}
4947
4948	wpa_scan_results_free(res);
4949}
4950
4951
4952static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
4953				      enum wpa_alg alg, const u8 *addr,
4954				      int key_idx, int set_tx,
4955				      const u8 *seq, size_t seq_len,
4956				      const u8 *key, size_t key_len)
4957{
4958	struct wpa_driver_nl80211_data *drv = bss->drv;
4959	int ifindex;
4960	struct nl_msg *msg;
4961	int ret;
4962	int tdls = 0;
4963
4964	/* Ignore for P2P Device */
4965	if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
4966		return 0;
4967
4968	ifindex = if_nametoindex(ifname);
4969	wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
4970		   "set_tx=%d seq_len=%lu key_len=%lu",
4971		   __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
4972		   (unsigned long) seq_len, (unsigned long) key_len);
4973#ifdef CONFIG_TDLS
4974	if (key_idx == -1) {
4975		key_idx = 0;
4976		tdls = 1;
4977	}
4978#endif /* CONFIG_TDLS */
4979
4980	msg = nlmsg_alloc();
4981	if (!msg)
4982		return -ENOMEM;
4983
4984	if (alg == WPA_ALG_NONE) {
4985		nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
4986	} else {
4987		nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
4988		NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
4989		switch (alg) {
4990		case WPA_ALG_WEP:
4991			if (key_len == 5)
4992				NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4993					    WLAN_CIPHER_SUITE_WEP40);
4994			else
4995				NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4996					    WLAN_CIPHER_SUITE_WEP104);
4997			break;
4998		case WPA_ALG_TKIP:
4999			NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5000				    WLAN_CIPHER_SUITE_TKIP);
5001			break;
5002		case WPA_ALG_CCMP:
5003			NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5004				    WLAN_CIPHER_SUITE_CCMP);
5005			break;
5006		case WPA_ALG_GCMP:
5007			NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5008				    WLAN_CIPHER_SUITE_GCMP);
5009			break;
5010		case WPA_ALG_IGTK:
5011			NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5012				    WLAN_CIPHER_SUITE_AES_CMAC);
5013			break;
5014		case WPA_ALG_SMS4:
5015			NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5016				    WLAN_CIPHER_SUITE_SMS4);
5017			break;
5018		case WPA_ALG_KRK:
5019			NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5020				    WLAN_CIPHER_SUITE_KRK);
5021			break;
5022		default:
5023			wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
5024				   "algorithm %d", __func__, alg);
5025			nlmsg_free(msg);
5026			return -1;
5027		}
5028	}
5029
5030	if (seq && seq_len)
5031		NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
5032
5033	if (addr && !is_broadcast_ether_addr(addr)) {
5034		wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
5035		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5036
5037		if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5038			wpa_printf(MSG_DEBUG, "   RSN IBSS RX GTK");
5039			NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5040				    NL80211_KEYTYPE_GROUP);
5041		}
5042	} else if (addr && is_broadcast_ether_addr(addr)) {
5043		struct nlattr *types;
5044
5045		wpa_printf(MSG_DEBUG, "   broadcast key");
5046
5047		types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
5048		if (!types)
5049			goto nla_put_failure;
5050		NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5051		nla_nest_end(msg, types);
5052	}
5053	NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5054	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5055
5056	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5057	if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5058		ret = 0;
5059	if (ret)
5060		wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5061			   ret, strerror(-ret));
5062
5063	/*
5064	 * If we failed or don't need to set the default TX key (below),
5065	 * we're done here.
5066	 */
5067	if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
5068		return ret;
5069	if (is_ap_interface(drv->nlmode) && addr &&
5070	    !is_broadcast_ether_addr(addr))
5071		return ret;
5072
5073	msg = nlmsg_alloc();
5074	if (!msg)
5075		return -ENOMEM;
5076
5077	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
5078	NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5079	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5080	if (alg == WPA_ALG_IGTK)
5081		NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5082	else
5083		NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5084	if (addr && is_broadcast_ether_addr(addr)) {
5085		struct nlattr *types;
5086
5087		types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
5088		if (!types)
5089			goto nla_put_failure;
5090		NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5091		nla_nest_end(msg, types);
5092	} else if (addr) {
5093		struct nlattr *types;
5094
5095		types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
5096		if (!types)
5097			goto nla_put_failure;
5098		NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5099		nla_nest_end(msg, types);
5100	}
5101
5102	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5103	if (ret == -ENOENT)
5104		ret = 0;
5105	if (ret)
5106		wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5107			   "err=%d %s)", ret, strerror(-ret));
5108	return ret;
5109
5110nla_put_failure:
5111	nlmsg_free(msg);
5112	return -ENOBUFS;
5113}
5114
5115
5116static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5117		      int key_idx, int defkey,
5118		      const u8 *seq, size_t seq_len,
5119		      const u8 *key, size_t key_len)
5120{
5121	struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5122	if (!key_attr)
5123		return -1;
5124
5125	if (defkey && alg == WPA_ALG_IGTK)
5126		NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5127	else if (defkey)
5128		NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5129
5130	NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5131
5132	switch (alg) {
5133	case WPA_ALG_WEP:
5134		if (key_len == 5)
5135			NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5136				    WLAN_CIPHER_SUITE_WEP40);
5137		else
5138			NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5139				    WLAN_CIPHER_SUITE_WEP104);
5140		break;
5141	case WPA_ALG_TKIP:
5142		NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
5143		break;
5144	case WPA_ALG_CCMP:
5145		NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
5146		break;
5147	case WPA_ALG_GCMP:
5148		NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
5149		break;
5150	case WPA_ALG_IGTK:
5151		NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5152			    WLAN_CIPHER_SUITE_AES_CMAC);
5153		break;
5154	default:
5155		wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
5156			   "algorithm %d", __func__, alg);
5157		return -1;
5158	}
5159
5160	if (seq && seq_len)
5161		NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5162
5163	NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5164
5165	nla_nest_end(msg, key_attr);
5166
5167	return 0;
5168 nla_put_failure:
5169	return -1;
5170}
5171
5172
5173static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5174				 struct nl_msg *msg)
5175{
5176	int i, privacy = 0;
5177	struct nlattr *nl_keys, *nl_key;
5178
5179	for (i = 0; i < 4; i++) {
5180		if (!params->wep_key[i])
5181			continue;
5182		privacy = 1;
5183		break;
5184	}
5185	if (params->wps == WPS_MODE_PRIVACY)
5186		privacy = 1;
5187	if (params->pairwise_suite &&
5188	    params->pairwise_suite != WPA_CIPHER_NONE)
5189		privacy = 1;
5190
5191	if (!privacy)
5192		return 0;
5193
5194	NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5195
5196	nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5197	if (!nl_keys)
5198		goto nla_put_failure;
5199
5200	for (i = 0; i < 4; i++) {
5201		if (!params->wep_key[i])
5202			continue;
5203
5204		nl_key = nla_nest_start(msg, i);
5205		if (!nl_key)
5206			goto nla_put_failure;
5207
5208		NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5209			params->wep_key[i]);
5210		if (params->wep_key_len[i] == 5)
5211			NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5212				    WLAN_CIPHER_SUITE_WEP40);
5213		else
5214			NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5215				    WLAN_CIPHER_SUITE_WEP104);
5216
5217		NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5218
5219		if (i == params->wep_tx_keyidx)
5220			NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5221
5222		nla_nest_end(msg, nl_key);
5223	}
5224	nla_nest_end(msg, nl_keys);
5225
5226	return 0;
5227
5228nla_put_failure:
5229	return -ENOBUFS;
5230}
5231
5232
5233static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5234				   const u8 *addr, int cmd, u16 reason_code,
5235				   int local_state_change)
5236{
5237	int ret = -1;
5238	struct nl_msg *msg;
5239
5240	msg = nlmsg_alloc();
5241	if (!msg)
5242		return -1;
5243
5244	nl80211_cmd(drv, msg, 0, cmd);
5245
5246	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5247	NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
5248	if (addr)
5249		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5250	if (local_state_change)
5251		NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5252
5253	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5254	msg = NULL;
5255	if (ret) {
5256		wpa_dbg(drv->ctx, MSG_DEBUG,
5257			"nl80211: MLME command failed: reason=%u ret=%d (%s)",
5258			reason_code, ret, strerror(-ret));
5259		goto nla_put_failure;
5260	}
5261	ret = 0;
5262
5263nla_put_failure:
5264	nlmsg_free(msg);
5265	return ret;
5266}
5267
5268
5269static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
5270					 int reason_code)
5271{
5272	int ret;
5273
5274	wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
5275	nl80211_mark_disconnected(drv);
5276	/* Disconnect command doesn't need BSSID - it uses cached value */
5277	ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5278				      reason_code, 0);
5279	/*
5280	 * For locally generated disconnect, supplicant already generates a
5281	 * DEAUTH event, so ignore the event from NL80211.
5282	 */
5283	drv->ignore_next_local_disconnect = ret == 0;
5284
5285	return ret;
5286}
5287
5288
5289static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5290					     const u8 *addr, int reason_code)
5291{
5292	struct wpa_driver_nl80211_data *drv = bss->drv;
5293	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
5294		return wpa_driver_nl80211_disconnect(drv, reason_code);
5295	wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5296		   __func__, MAC2STR(addr), reason_code);
5297	nl80211_mark_disconnected(drv);
5298	if (drv->nlmode == NL80211_IFTYPE_ADHOC)
5299		return nl80211_leave_ibss(drv);
5300	return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
5301				       reason_code, 0);
5302}
5303
5304
5305static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
5306				     struct wpa_driver_auth_params *params)
5307{
5308	int i;
5309
5310	drv->auth_freq = params->freq;
5311	drv->auth_alg = params->auth_alg;
5312	drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
5313	drv->auth_local_state_change = params->local_state_change;
5314	drv->auth_p2p = params->p2p;
5315
5316	if (params->bssid)
5317		os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
5318	else
5319		os_memset(drv->auth_bssid_, 0, ETH_ALEN);
5320
5321	if (params->ssid) {
5322		os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
5323		drv->auth_ssid_len = params->ssid_len;
5324	} else
5325		drv->auth_ssid_len = 0;
5326
5327
5328	os_free(drv->auth_ie);
5329	drv->auth_ie = NULL;
5330	drv->auth_ie_len = 0;
5331	if (params->ie) {
5332		drv->auth_ie = os_malloc(params->ie_len);
5333		if (drv->auth_ie) {
5334			os_memcpy(drv->auth_ie, params->ie, params->ie_len);
5335			drv->auth_ie_len = params->ie_len;
5336		}
5337	}
5338
5339	for (i = 0; i < 4; i++) {
5340		if (params->wep_key[i] && params->wep_key_len[i] &&
5341		    params->wep_key_len[i] <= 16) {
5342			os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
5343				  params->wep_key_len[i]);
5344			drv->auth_wep_key_len[i] = params->wep_key_len[i];
5345		} else
5346			drv->auth_wep_key_len[i] = 0;
5347	}
5348}
5349
5350
5351static int wpa_driver_nl80211_authenticate(
5352	struct i802_bss *bss, struct wpa_driver_auth_params *params)
5353{
5354	struct wpa_driver_nl80211_data *drv = bss->drv;
5355	int ret = -1, i;
5356	struct nl_msg *msg;
5357	enum nl80211_auth_type type;
5358	enum nl80211_iftype nlmode;
5359	int count = 0;
5360	int is_retry;
5361
5362	is_retry = drv->retry_auth;
5363	drv->retry_auth = 0;
5364
5365	nl80211_mark_disconnected(drv);
5366	os_memset(drv->auth_bssid, 0, ETH_ALEN);
5367	if (params->bssid)
5368		os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5369	else
5370		os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5371	/* FIX: IBSS mode */
5372	nlmode = params->p2p ?
5373		NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5374	if (drv->nlmode != nlmode &&
5375	    wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
5376		return -1;
5377
5378retry:
5379	msg = nlmsg_alloc();
5380	if (!msg)
5381		return -1;
5382
5383	wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
5384		   drv->ifindex);
5385
5386	nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
5387
5388	for (i = 0; i < 4; i++) {
5389		if (!params->wep_key[i])
5390			continue;
5391		wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
5392					   NULL, i,
5393					   i == params->wep_tx_keyidx, NULL, 0,
5394					   params->wep_key[i],
5395					   params->wep_key_len[i]);
5396		if (params->wep_tx_keyidx != i)
5397			continue;
5398		if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
5399			       params->wep_key[i], params->wep_key_len[i])) {
5400			nlmsg_free(msg);
5401			return -1;
5402		}
5403	}
5404
5405	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5406	if (params->bssid) {
5407		wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
5408			   MAC2STR(params->bssid));
5409		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5410	}
5411	if (params->freq) {
5412		wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
5413		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5414	}
5415	if (params->ssid) {
5416		wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
5417				  params->ssid, params->ssid_len);
5418		NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5419			params->ssid);
5420	}
5421	wpa_hexdump(MSG_DEBUG, "  * IEs", params->ie, params->ie_len);
5422	if (params->ie)
5423		NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
5424	if (params->sae_data) {
5425		wpa_hexdump(MSG_DEBUG, "  * SAE data", params->sae_data,
5426			    params->sae_data_len);
5427		NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
5428			params->sae_data);
5429	}
5430	if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5431		type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5432	else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5433		type = NL80211_AUTHTYPE_SHARED_KEY;
5434	else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5435		type = NL80211_AUTHTYPE_NETWORK_EAP;
5436	else if (params->auth_alg & WPA_AUTH_ALG_FT)
5437		type = NL80211_AUTHTYPE_FT;
5438	else if (params->auth_alg & WPA_AUTH_ALG_SAE)
5439		type = NL80211_AUTHTYPE_SAE;
5440	else
5441		goto nla_put_failure;
5442	wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
5443	NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
5444	if (params->local_state_change) {
5445		wpa_printf(MSG_DEBUG, "  * Local state change only");
5446		NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5447	}
5448
5449	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5450	msg = NULL;
5451	if (ret) {
5452		wpa_dbg(drv->ctx, MSG_DEBUG,
5453			"nl80211: MLME command failed (auth): ret=%d (%s)",
5454			ret, strerror(-ret));
5455		count++;
5456		if (ret == -EALREADY && count == 1 && params->bssid &&
5457		    !params->local_state_change) {
5458			/*
5459			 * mac80211 does not currently accept new
5460			 * authentication if we are already authenticated. As a
5461			 * workaround, force deauthentication and try again.
5462			 */
5463			wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
5464				   "after forced deauthentication");
5465			wpa_driver_nl80211_deauthenticate(
5466				bss, params->bssid,
5467				WLAN_REASON_PREV_AUTH_NOT_VALID);
5468			nlmsg_free(msg);
5469			goto retry;
5470		}
5471
5472		if (ret == -ENOENT && params->freq && !is_retry) {
5473			/*
5474			 * cfg80211 has likely expired the BSS entry even
5475			 * though it was previously available in our internal
5476			 * BSS table. To recover quickly, start a single
5477			 * channel scan on the specified channel.
5478			 */
5479			struct wpa_driver_scan_params scan;
5480			int freqs[2];
5481
5482			os_memset(&scan, 0, sizeof(scan));
5483			scan.num_ssids = 1;
5484			if (params->ssid) {
5485				scan.ssids[0].ssid = params->ssid;
5486				scan.ssids[0].ssid_len = params->ssid_len;
5487			}
5488			freqs[0] = params->freq;
5489			freqs[1] = 0;
5490			scan.freqs = freqs;
5491			wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
5492				   "channel scan to refresh cfg80211 BSS "
5493				   "entry");
5494			ret = wpa_driver_nl80211_scan(bss, &scan);
5495			if (ret == 0) {
5496				nl80211_copy_auth_params(drv, params);
5497				drv->scan_for_auth = 1;
5498			}
5499		} else if (is_retry) {
5500			/*
5501			 * Need to indicate this with an event since the return
5502			 * value from the retry is not delivered to core code.
5503			 */
5504			union wpa_event_data event;
5505			wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
5506				   "failed");
5507			os_memset(&event, 0, sizeof(event));
5508			os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
5509				  ETH_ALEN);
5510			wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
5511					     &event);
5512		}
5513
5514		goto nla_put_failure;
5515	}
5516	ret = 0;
5517	wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
5518		   "successfully");
5519
5520nla_put_failure:
5521	nlmsg_free(msg);
5522	return ret;
5523}
5524
5525
5526static int wpa_driver_nl80211_authenticate_retry(
5527	struct wpa_driver_nl80211_data *drv)
5528{
5529	struct wpa_driver_auth_params params;
5530	struct i802_bss *bss = &drv->first_bss;
5531	int i;
5532
5533	wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
5534
5535	os_memset(&params, 0, sizeof(params));
5536	params.freq = drv->auth_freq;
5537	params.auth_alg = drv->auth_alg;
5538	params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
5539	params.local_state_change = drv->auth_local_state_change;
5540	params.p2p = drv->auth_p2p;
5541
5542	if (!is_zero_ether_addr(drv->auth_bssid_))
5543		params.bssid = drv->auth_bssid_;
5544
5545	if (drv->auth_ssid_len) {
5546		params.ssid = drv->auth_ssid;
5547		params.ssid_len = drv->auth_ssid_len;
5548	}
5549
5550	params.ie = drv->auth_ie;
5551	params.ie_len = drv->auth_ie_len;
5552
5553	for (i = 0; i < 4; i++) {
5554		if (drv->auth_wep_key_len[i]) {
5555			params.wep_key[i] = drv->auth_wep_key[i];
5556			params.wep_key_len[i] = drv->auth_wep_key_len[i];
5557		}
5558	}
5559
5560	drv->retry_auth = 1;
5561	return wpa_driver_nl80211_authenticate(bss, &params);
5562}
5563
5564
5565struct phy_info_arg {
5566	u16 *num_modes;
5567	struct hostapd_hw_modes *modes;
5568	int last_mode, last_chan_idx;
5569};
5570
5571static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
5572			     struct nlattr *ampdu_factor,
5573			     struct nlattr *ampdu_density,
5574			     struct nlattr *mcs_set)
5575{
5576	if (capa)
5577		mode->ht_capab = nla_get_u16(capa);
5578
5579	if (ampdu_factor)
5580		mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
5581
5582	if (ampdu_density)
5583		mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
5584
5585	if (mcs_set && nla_len(mcs_set) >= 16) {
5586		u8 *mcs;
5587		mcs = nla_data(mcs_set);
5588		os_memcpy(mode->mcs_set, mcs, 16);
5589	}
5590}
5591
5592
5593static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
5594			      struct nlattr *capa,
5595			      struct nlattr *mcs_set)
5596{
5597	if (capa)
5598		mode->vht_capab = nla_get_u32(capa);
5599
5600	if (mcs_set && nla_len(mcs_set) >= 8) {
5601		u8 *mcs;
5602		mcs = nla_data(mcs_set);
5603		os_memcpy(mode->vht_mcs_set, mcs, 8);
5604	}
5605}
5606
5607
5608static void phy_info_freq(struct hostapd_hw_modes *mode,
5609			  struct hostapd_channel_data *chan,
5610			  struct nlattr *tb_freq[])
5611{
5612	u8 channel;
5613	chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
5614	chan->flag = 0;
5615	if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
5616		chan->chan = channel;
5617
5618	if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5619		chan->flag |= HOSTAPD_CHAN_DISABLED;
5620	if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
5621		chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN;
5622	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
5623		chan->flag |= HOSTAPD_CHAN_NO_IBSS;
5624	if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
5625		chan->flag |= HOSTAPD_CHAN_RADAR;
5626
5627	if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
5628	    !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5629		chan->max_tx_power = nla_get_u32(
5630			tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
5631	if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
5632		enum nl80211_dfs_state state =
5633			nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
5634
5635		switch (state) {
5636		case NL80211_DFS_USABLE:
5637			chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
5638			break;
5639		case NL80211_DFS_AVAILABLE:
5640			chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
5641			break;
5642		case NL80211_DFS_UNAVAILABLE:
5643			chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
5644			break;
5645		}
5646	}
5647}
5648
5649
5650static int phy_info_freqs(struct phy_info_arg *phy_info,
5651			  struct hostapd_hw_modes *mode, struct nlattr *tb)
5652{
5653	static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5654		[NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
5655		[NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
5656		[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
5657		[NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
5658		[NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
5659		[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
5660		[NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
5661	};
5662	int new_channels = 0;
5663	struct hostapd_channel_data *channel;
5664	struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
5665	struct nlattr *nl_freq;
5666	int rem_freq, idx;
5667
5668	if (tb == NULL)
5669		return NL_OK;
5670
5671	nla_for_each_nested(nl_freq, tb, rem_freq) {
5672		nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5673			  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5674		if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5675			continue;
5676		new_channels++;
5677	}
5678
5679	channel = os_realloc_array(mode->channels,
5680				   mode->num_channels + new_channels,
5681				   sizeof(struct hostapd_channel_data));
5682	if (!channel)
5683		return NL_SKIP;
5684
5685	mode->channels = channel;
5686	mode->num_channels += new_channels;
5687
5688	idx = phy_info->last_chan_idx;
5689
5690	nla_for_each_nested(nl_freq, tb, rem_freq) {
5691		nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5692			  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5693		if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5694			continue;
5695		phy_info_freq(mode, &mode->channels[idx], tb_freq);
5696		idx++;
5697	}
5698	phy_info->last_chan_idx = idx;
5699
5700	return NL_OK;
5701}
5702
5703
5704static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
5705{
5706	static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
5707		[NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
5708		[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
5709		{ .type = NLA_FLAG },
5710	};
5711	struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
5712	struct nlattr *nl_rate;
5713	int rem_rate, idx;
5714
5715	if (tb == NULL)
5716		return NL_OK;
5717
5718	nla_for_each_nested(nl_rate, tb, rem_rate) {
5719		nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5720			  nla_data(nl_rate), nla_len(nl_rate),
5721			  rate_policy);
5722		if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5723			continue;
5724		mode->num_rates++;
5725	}
5726
5727	mode->rates = os_calloc(mode->num_rates, sizeof(int));
5728	if (!mode->rates)
5729		return NL_SKIP;
5730
5731	idx = 0;
5732
5733	nla_for_each_nested(nl_rate, tb, rem_rate) {
5734		nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5735			  nla_data(nl_rate), nla_len(nl_rate),
5736			  rate_policy);
5737		if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5738			continue;
5739		mode->rates[idx] = nla_get_u32(
5740			tb_rate[NL80211_BITRATE_ATTR_RATE]);
5741		idx++;
5742	}
5743
5744	return NL_OK;
5745}
5746
5747
5748static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
5749{
5750	struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
5751	struct hostapd_hw_modes *mode;
5752	int ret;
5753
5754	if (phy_info->last_mode != nl_band->nla_type) {
5755		mode = os_realloc_array(phy_info->modes,
5756					*phy_info->num_modes + 1,
5757					sizeof(*mode));
5758		if (!mode)
5759			return NL_SKIP;
5760		phy_info->modes = mode;
5761
5762		mode = &phy_info->modes[*(phy_info->num_modes)];
5763		os_memset(mode, 0, sizeof(*mode));
5764		mode->mode = NUM_HOSTAPD_MODES;
5765		mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
5766			HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
5767
5768		/*
5769		 * Unsupported VHT MCS stream is defined as value 3, so the VHT
5770		 * MCS RX/TX map must be initialized with 0xffff to mark all 8
5771		 * possible streams as unsupported. This will be overridden if
5772		 * driver advertises VHT support.
5773		 */
5774		mode->vht_mcs_set[0] = 0xff;
5775		mode->vht_mcs_set[1] = 0xff;
5776		mode->vht_mcs_set[4] = 0xff;
5777		mode->vht_mcs_set[5] = 0xff;
5778
5779		*(phy_info->num_modes) += 1;
5780		phy_info->last_mode = nl_band->nla_type;
5781		phy_info->last_chan_idx = 0;
5782	} else
5783		mode = &phy_info->modes[*(phy_info->num_modes) - 1];
5784
5785	nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
5786		  nla_len(nl_band), NULL);
5787
5788	phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
5789			 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
5790			 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
5791			 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
5792	phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
5793			  tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
5794	ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
5795	if (ret != NL_OK)
5796		return ret;
5797	ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
5798	if (ret != NL_OK)
5799		return ret;
5800
5801	return NL_OK;
5802}
5803
5804
5805static int phy_info_handler(struct nl_msg *msg, void *arg)
5806{
5807	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5808	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5809	struct phy_info_arg *phy_info = arg;
5810	struct nlattr *nl_band;
5811	int rem_band;
5812
5813	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5814		  genlmsg_attrlen(gnlh, 0), NULL);
5815
5816	if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
5817		return NL_SKIP;
5818
5819	nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
5820	{
5821		int res = phy_info_band(phy_info, nl_band);
5822		if (res != NL_OK)
5823			return res;
5824	}
5825
5826	return NL_SKIP;
5827}
5828
5829
5830static struct hostapd_hw_modes *
5831wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
5832				     u16 *num_modes)
5833{
5834	u16 m;
5835	struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
5836	int i, mode11g_idx = -1;
5837
5838	/* heuristic to set up modes */
5839	for (m = 0; m < *num_modes; m++) {
5840		if (!modes[m].num_channels)
5841			continue;
5842		if (modes[m].channels[0].freq < 4000) {
5843			modes[m].mode = HOSTAPD_MODE_IEEE80211B;
5844			for (i = 0; i < modes[m].num_rates; i++) {
5845				if (modes[m].rates[i] > 200) {
5846					modes[m].mode = HOSTAPD_MODE_IEEE80211G;
5847					break;
5848				}
5849			}
5850		} else if (modes[m].channels[0].freq > 50000)
5851			modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
5852		else
5853			modes[m].mode = HOSTAPD_MODE_IEEE80211A;
5854	}
5855
5856	/* If only 802.11g mode is included, use it to construct matching
5857	 * 802.11b mode data. */
5858
5859	for (m = 0; m < *num_modes; m++) {
5860		if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
5861			return modes; /* 802.11b already included */
5862		if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
5863			mode11g_idx = m;
5864	}
5865
5866	if (mode11g_idx < 0)
5867		return modes; /* 2.4 GHz band not supported at all */
5868
5869	nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
5870	if (nmodes == NULL)
5871		return modes; /* Could not add 802.11b mode */
5872
5873	mode = &nmodes[*num_modes];
5874	os_memset(mode, 0, sizeof(*mode));
5875	(*num_modes)++;
5876	modes = nmodes;
5877
5878	mode->mode = HOSTAPD_MODE_IEEE80211B;
5879
5880	mode11g = &modes[mode11g_idx];
5881	mode->num_channels = mode11g->num_channels;
5882	mode->channels = os_malloc(mode11g->num_channels *
5883				   sizeof(struct hostapd_channel_data));
5884	if (mode->channels == NULL) {
5885		(*num_modes)--;
5886		return modes; /* Could not add 802.11b mode */
5887	}
5888	os_memcpy(mode->channels, mode11g->channels,
5889		  mode11g->num_channels * sizeof(struct hostapd_channel_data));
5890
5891	mode->num_rates = 0;
5892	mode->rates = os_malloc(4 * sizeof(int));
5893	if (mode->rates == NULL) {
5894		os_free(mode->channels);
5895		(*num_modes)--;
5896		return modes; /* Could not add 802.11b mode */
5897	}
5898
5899	for (i = 0; i < mode11g->num_rates; i++) {
5900		if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
5901		    mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
5902			continue;
5903		mode->rates[mode->num_rates] = mode11g->rates[i];
5904		mode->num_rates++;
5905		if (mode->num_rates == 4)
5906			break;
5907	}
5908
5909	if (mode->num_rates == 0) {
5910		os_free(mode->channels);
5911		os_free(mode->rates);
5912		(*num_modes)--;
5913		return modes; /* No 802.11b rates */
5914	}
5915
5916	wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
5917		   "information");
5918
5919	return modes;
5920}
5921
5922
5923static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
5924				  int end)
5925{
5926	int c;
5927
5928	for (c = 0; c < mode->num_channels; c++) {
5929		struct hostapd_channel_data *chan = &mode->channels[c];
5930		if (chan->freq - 10 >= start && chan->freq + 10 <= end)
5931			chan->flag |= HOSTAPD_CHAN_HT40;
5932	}
5933}
5934
5935
5936static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
5937				      int end)
5938{
5939	int c;
5940
5941	for (c = 0; c < mode->num_channels; c++) {
5942		struct hostapd_channel_data *chan = &mode->channels[c];
5943		if (!(chan->flag & HOSTAPD_CHAN_HT40))
5944			continue;
5945		if (chan->freq - 30 >= start && chan->freq - 10 <= end)
5946			chan->flag |= HOSTAPD_CHAN_HT40MINUS;
5947		if (chan->freq + 10 >= start && chan->freq + 30 <= end)
5948			chan->flag |= HOSTAPD_CHAN_HT40PLUS;
5949	}
5950}
5951
5952
5953static void nl80211_reg_rule_ht40(struct nlattr *tb[],
5954				  struct phy_info_arg *results)
5955{
5956	u32 start, end, max_bw;
5957	u16 m;
5958
5959	if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5960	    tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5961	    tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5962		return;
5963
5964	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5965	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5966	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5967
5968	wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
5969		   start, end, max_bw);
5970	if (max_bw < 40)
5971		return;
5972
5973	for (m = 0; m < *results->num_modes; m++) {
5974		if (!(results->modes[m].ht_capab &
5975		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5976			continue;
5977		nl80211_set_ht40_mode(&results->modes[m], start, end);
5978	}
5979}
5980
5981
5982static void nl80211_reg_rule_sec(struct nlattr *tb[],
5983				 struct phy_info_arg *results)
5984{
5985	u32 start, end, max_bw;
5986	u16 m;
5987
5988	if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5989	    tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5990	    tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5991		return;
5992
5993	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5994	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5995	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5996
5997	if (max_bw < 20)
5998		return;
5999
6000	for (m = 0; m < *results->num_modes; m++) {
6001		if (!(results->modes[m].ht_capab &
6002		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6003			continue;
6004		nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6005	}
6006}
6007
6008
6009static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6010{
6011	struct phy_info_arg *results = arg;
6012	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6013	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6014	struct nlattr *nl_rule;
6015	struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6016	int rem_rule;
6017	static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6018		[NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6019		[NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6020		[NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6021		[NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6022		[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6023		[NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6024	};
6025
6026	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6027		  genlmsg_attrlen(gnlh, 0), NULL);
6028	if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6029	    !tb_msg[NL80211_ATTR_REG_RULES]) {
6030		wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6031			   "available");
6032		return NL_SKIP;
6033	}
6034
6035	wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6036		   (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6037
6038	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6039	{
6040		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6041			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6042		nl80211_reg_rule_ht40(tb_rule, results);
6043	}
6044
6045	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6046	{
6047		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6048			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6049		nl80211_reg_rule_sec(tb_rule, results);
6050	}
6051
6052	return NL_SKIP;
6053}
6054
6055
6056static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
6057				  struct phy_info_arg *results)
6058{
6059	struct nl_msg *msg;
6060
6061	msg = nlmsg_alloc();
6062	if (!msg)
6063		return -ENOMEM;
6064
6065	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
6066	return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6067}
6068
6069
6070static struct hostapd_hw_modes *
6071wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6072{
6073	u32 feat;
6074	struct i802_bss *bss = priv;
6075	struct wpa_driver_nl80211_data *drv = bss->drv;
6076	struct nl_msg *msg;
6077	struct phy_info_arg result = {
6078		.num_modes = num_modes,
6079		.modes = NULL,
6080		.last_mode = -1,
6081	};
6082
6083	*num_modes = 0;
6084	*flags = 0;
6085
6086	msg = nlmsg_alloc();
6087	if (!msg)
6088		return NULL;
6089
6090	feat = get_nl80211_protocol_features(drv);
6091	if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6092		nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6093	else
6094		nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
6095
6096	NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
6097	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6098
6099	if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
6100		nl80211_set_ht40_flags(drv, &result);
6101		return wpa_driver_nl80211_postprocess_modes(result.modes,
6102							    num_modes);
6103	}
6104	msg = NULL;
6105 nla_put_failure:
6106	nlmsg_free(msg);
6107	return NULL;
6108}
6109
6110
6111static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6112					const void *data, size_t len,
6113					int encrypt, int noack)
6114{
6115	__u8 rtap_hdr[] = {
6116		0x00, 0x00, /* radiotap version */
6117		0x0e, 0x00, /* radiotap length */
6118		0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6119		IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6120		0x00,       /* padding */
6121		0x00, 0x00, /* RX and TX flags to indicate that */
6122		0x00, 0x00, /* this is the injected frame directly */
6123	};
6124	struct iovec iov[2] = {
6125		{
6126			.iov_base = &rtap_hdr,
6127			.iov_len = sizeof(rtap_hdr),
6128		},
6129		{
6130			.iov_base = (void *) data,
6131			.iov_len = len,
6132		}
6133	};
6134	struct msghdr msg = {
6135		.msg_name = NULL,
6136		.msg_namelen = 0,
6137		.msg_iov = iov,
6138		.msg_iovlen = 2,
6139		.msg_control = NULL,
6140		.msg_controllen = 0,
6141		.msg_flags = 0,
6142	};
6143	int res;
6144	u16 txflags = 0;
6145
6146	if (encrypt)
6147		rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6148
6149	if (drv->monitor_sock < 0) {
6150		wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6151			   "for %s", __func__);
6152		return -1;
6153	}
6154
6155	if (noack)
6156		txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
6157	WPA_PUT_LE16(&rtap_hdr[12], txflags);
6158
6159	res = sendmsg(drv->monitor_sock, &msg, 0);
6160	if (res < 0) {
6161		wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
6162		return -1;
6163	}
6164	return 0;
6165}
6166
6167
6168static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
6169					 const void *data, size_t len,
6170					 int encrypt, int noack,
6171					 unsigned int freq, int no_cck,
6172					 int offchanok, unsigned int wait_time)
6173{
6174	struct wpa_driver_nl80211_data *drv = bss->drv;
6175	u64 cookie;
6176	int res;
6177
6178	if (freq == 0) {
6179		wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
6180			   bss->freq);
6181		freq = bss->freq;
6182	}
6183
6184	if (drv->use_monitor) {
6185		wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
6186			   freq, bss->freq);
6187		return wpa_driver_nl80211_send_mntr(drv, data, len,
6188						    encrypt, noack);
6189	}
6190
6191	wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
6192	res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
6193				     &cookie, no_cck, noack, offchanok);
6194	if (res == 0 && !noack) {
6195		const struct ieee80211_mgmt *mgmt;
6196		u16 fc;
6197
6198		mgmt = (const struct ieee80211_mgmt *) data;
6199		fc = le_to_host16(mgmt->frame_control);
6200		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6201		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
6202			wpa_printf(MSG_MSGDUMP,
6203				   "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
6204				   (long long unsigned int)
6205				   drv->send_action_cookie,
6206				   (long long unsigned int) cookie);
6207			drv->send_action_cookie = cookie;
6208		}
6209	}
6210
6211	return res;
6212}
6213
6214
6215static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
6216					size_t data_len, int noack,
6217					unsigned int freq, int no_cck,
6218					int offchanok,
6219					unsigned int wait_time)
6220{
6221	struct wpa_driver_nl80211_data *drv = bss->drv;
6222	struct ieee80211_mgmt *mgmt;
6223	int encrypt = 1;
6224	u16 fc;
6225
6226	mgmt = (struct ieee80211_mgmt *) data;
6227	fc = le_to_host16(mgmt->frame_control);
6228	wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
6229		   noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
6230
6231	if ((is_sta_interface(drv->nlmode) ||
6232	     drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
6233	    WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6234	    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
6235		/*
6236		 * The use of last_mgmt_freq is a bit of a hack,
6237		 * but it works due to the single-threaded nature
6238		 * of wpa_supplicant.
6239		 */
6240		if (freq == 0) {
6241			wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
6242				   drv->last_mgmt_freq);
6243			freq = drv->last_mgmt_freq;
6244		}
6245		return nl80211_send_frame_cmd(bss, freq, 0,
6246					      data, data_len, NULL, 1, noack,
6247					      1);
6248	}
6249
6250	if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
6251		if (freq == 0) {
6252			wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
6253				   bss->freq);
6254			freq = bss->freq;
6255		}
6256		return nl80211_send_frame_cmd(bss, freq,
6257					      (int) freq == bss->freq ? 0 :
6258					      wait_time,
6259					      data, data_len,
6260					      &drv->send_action_cookie,
6261					      no_cck, noack, offchanok);
6262	}
6263
6264	if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6265	    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
6266		/*
6267		 * Only one of the authentication frame types is encrypted.
6268		 * In order for static WEP encryption to work properly (i.e.,
6269		 * to not encrypt the frame), we need to tell mac80211 about
6270		 * the frames that must not be encrypted.
6271		 */
6272		u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
6273		u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
6274		if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
6275			encrypt = 0;
6276	}
6277
6278	wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
6279	return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
6280					     noack, freq, no_cck, offchanok,
6281					     wait_time);
6282}
6283
6284
6285static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
6286			   int slot, int ht_opmode, int ap_isolate,
6287			   int *basic_rates)
6288{
6289	struct wpa_driver_nl80211_data *drv = bss->drv;
6290	struct nl_msg *msg;
6291
6292	msg = nlmsg_alloc();
6293	if (!msg)
6294		return -ENOMEM;
6295
6296	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
6297
6298	if (cts >= 0)
6299		NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
6300	if (preamble >= 0)
6301		NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
6302	if (slot >= 0)
6303		NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
6304	if (ht_opmode >= 0)
6305		NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
6306	if (ap_isolate >= 0)
6307		NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
6308
6309	if (basic_rates) {
6310		u8 rates[NL80211_MAX_SUPP_RATES];
6311		u8 rates_len = 0;
6312		int i;
6313
6314		for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
6315		     i++)
6316			rates[rates_len++] = basic_rates[i] / 5;
6317
6318		NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
6319	}
6320
6321	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6322
6323	return send_and_recv_msgs(drv, msg, NULL, NULL);
6324 nla_put_failure:
6325	nlmsg_free(msg);
6326	return -ENOBUFS;
6327}
6328
6329
6330static int wpa_driver_nl80211_set_acl(void *priv,
6331				      struct hostapd_acl_params *params)
6332{
6333	struct i802_bss *bss = priv;
6334	struct wpa_driver_nl80211_data *drv = bss->drv;
6335	struct nl_msg *msg;
6336	struct nlattr *acl;
6337	unsigned int i;
6338	int ret = 0;
6339
6340	if (!(drv->capa.max_acl_mac_addrs))
6341		return -ENOTSUP;
6342
6343	if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
6344		return -ENOTSUP;
6345
6346	msg = nlmsg_alloc();
6347	if (!msg)
6348		return -ENOMEM;
6349
6350	wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
6351		   params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
6352
6353	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
6354
6355	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6356
6357	NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
6358		    NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
6359		    NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
6360
6361	acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
6362	if (acl == NULL)
6363		goto nla_put_failure;
6364
6365	for (i = 0; i < params->num_mac_acl; i++)
6366		NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
6367
6368	nla_nest_end(msg, acl);
6369
6370	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6371	msg = NULL;
6372	if (ret) {
6373		wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
6374			   ret, strerror(-ret));
6375	}
6376
6377nla_put_failure:
6378	nlmsg_free(msg);
6379
6380	return ret;
6381}
6382
6383
6384static int wpa_driver_nl80211_set_ap(void *priv,
6385				     struct wpa_driver_ap_params *params)
6386{
6387	struct i802_bss *bss = priv;
6388	struct wpa_driver_nl80211_data *drv = bss->drv;
6389	struct nl_msg *msg;
6390	u8 cmd = NL80211_CMD_NEW_BEACON;
6391	int ret;
6392	int beacon_set;
6393	int ifindex = if_nametoindex(bss->ifname);
6394	int num_suites;
6395	u32 suites[10];
6396	u32 ver;
6397
6398	beacon_set = bss->beacon_set;
6399
6400	msg = nlmsg_alloc();
6401	if (!msg)
6402		return -ENOMEM;
6403
6404	wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
6405		   beacon_set);
6406	if (beacon_set)
6407		cmd = NL80211_CMD_SET_BEACON;
6408
6409	nl80211_cmd(drv, msg, 0, cmd);
6410	wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
6411		    params->head, params->head_len);
6412	NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
6413	wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
6414		    params->tail, params->tail_len);
6415	NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
6416	wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
6417	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
6418	wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
6419	NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
6420	wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
6421	NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
6422	wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
6423			  params->ssid, params->ssid_len);
6424	NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6425		params->ssid);
6426	if (params->proberesp && params->proberesp_len) {
6427		wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
6428			    params->proberesp, params->proberesp_len);
6429		NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
6430			params->proberesp);
6431	}
6432	switch (params->hide_ssid) {
6433	case NO_SSID_HIDING:
6434		wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
6435		NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6436			    NL80211_HIDDEN_SSID_NOT_IN_USE);
6437		break;
6438	case HIDDEN_SSID_ZERO_LEN:
6439		wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
6440		NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6441			    NL80211_HIDDEN_SSID_ZERO_LEN);
6442		break;
6443	case HIDDEN_SSID_ZERO_CONTENTS:
6444		wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
6445		NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6446			    NL80211_HIDDEN_SSID_ZERO_CONTENTS);
6447		break;
6448	}
6449	wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
6450	if (params->privacy)
6451		NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
6452	wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
6453	if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
6454	    (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
6455		/* Leave out the attribute */
6456	} else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
6457		NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
6458			    NL80211_AUTHTYPE_SHARED_KEY);
6459	else
6460		NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
6461			    NL80211_AUTHTYPE_OPEN_SYSTEM);
6462
6463	wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
6464	ver = 0;
6465	if (params->wpa_version & WPA_PROTO_WPA)
6466		ver |= NL80211_WPA_VERSION_1;
6467	if (params->wpa_version & WPA_PROTO_RSN)
6468		ver |= NL80211_WPA_VERSION_2;
6469	if (ver)
6470		NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
6471
6472	wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
6473		   params->key_mgmt_suites);
6474	num_suites = 0;
6475	if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
6476		suites[num_suites++] = WLAN_AKM_SUITE_8021X;
6477	if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
6478		suites[num_suites++] = WLAN_AKM_SUITE_PSK;
6479	if (num_suites) {
6480		NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
6481			num_suites * sizeof(u32), suites);
6482	}
6483
6484	if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
6485	    params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
6486		NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
6487
6488	wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
6489		   params->pairwise_ciphers);
6490	num_suites = 0;
6491	if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
6492		suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
6493	if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
6494		suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
6495	if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
6496		suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
6497	if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
6498		suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
6499	if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
6500		suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
6501	if (num_suites) {
6502		NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
6503			num_suites * sizeof(u32), suites);
6504	}
6505
6506	wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
6507		   params->group_cipher);
6508	switch (params->group_cipher) {
6509	case WPA_CIPHER_CCMP:
6510		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6511			    WLAN_CIPHER_SUITE_CCMP);
6512		break;
6513	case WPA_CIPHER_GCMP:
6514		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6515			    WLAN_CIPHER_SUITE_GCMP);
6516		break;
6517	case WPA_CIPHER_TKIP:
6518		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6519			    WLAN_CIPHER_SUITE_TKIP);
6520		break;
6521	case WPA_CIPHER_WEP104:
6522		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6523			    WLAN_CIPHER_SUITE_WEP104);
6524		break;
6525	case WPA_CIPHER_WEP40:
6526		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6527			    WLAN_CIPHER_SUITE_WEP40);
6528		break;
6529	}
6530
6531	if (params->beacon_ies) {
6532		wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
6533				params->beacon_ies);
6534		NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
6535			wpabuf_head(params->beacon_ies));
6536	}
6537	if (params->proberesp_ies) {
6538		wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
6539				params->proberesp_ies);
6540		NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
6541			wpabuf_len(params->proberesp_ies),
6542			wpabuf_head(params->proberesp_ies));
6543	}
6544	if (params->assocresp_ies) {
6545		wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
6546				params->assocresp_ies);
6547		NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
6548			wpabuf_len(params->assocresp_ies),
6549			wpabuf_head(params->assocresp_ies));
6550	}
6551
6552	if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)  {
6553		wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
6554			   params->ap_max_inactivity);
6555		NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
6556			    params->ap_max_inactivity);
6557	}
6558
6559	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6560	if (ret) {
6561		wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
6562			   ret, strerror(-ret));
6563	} else {
6564		bss->beacon_set = 1;
6565		nl80211_set_bss(bss, params->cts_protect, params->preamble,
6566				params->short_slot_time, params->ht_opmode,
6567				params->isolate, params->basic_rates);
6568	}
6569	return ret;
6570 nla_put_failure:
6571	nlmsg_free(msg);
6572	return -ENOBUFS;
6573}
6574
6575
6576static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
6577				       struct hostapd_freq_params *freq)
6578{
6579	struct wpa_driver_nl80211_data *drv = bss->drv;
6580	struct nl_msg *msg;
6581	int ret;
6582
6583	wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d,"
6584		   " bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
6585		   freq->freq, freq->ht_enabled, freq->vht_enabled,
6586		   freq->bandwidth, freq->center_freq1, freq->center_freq2);
6587	msg = nlmsg_alloc();
6588	if (!msg)
6589		return -1;
6590
6591	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
6592
6593	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6594	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
6595	if (freq->vht_enabled) {
6596		switch (freq->bandwidth) {
6597		case 20:
6598			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6599				    NL80211_CHAN_WIDTH_20);
6600			break;
6601		case 40:
6602			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6603				    NL80211_CHAN_WIDTH_40);
6604			break;
6605		case 80:
6606			if (freq->center_freq2)
6607				NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6608					    NL80211_CHAN_WIDTH_80P80);
6609			else
6610				NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6611					    NL80211_CHAN_WIDTH_80);
6612			break;
6613		case 160:
6614			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6615				    NL80211_CHAN_WIDTH_160);
6616			break;
6617		default:
6618			return -1;
6619		}
6620		NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
6621		if (freq->center_freq2)
6622			NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
6623				    freq->center_freq2);
6624	} else if (freq->ht_enabled) {
6625		switch (freq->sec_channel_offset) {
6626		case -1:
6627			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6628				    NL80211_CHAN_HT40MINUS);
6629			break;
6630		case 1:
6631			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6632				    NL80211_CHAN_HT40PLUS);
6633			break;
6634		default:
6635			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6636				    NL80211_CHAN_HT20);
6637			break;
6638		}
6639	}
6640
6641	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6642	msg = NULL;
6643	if (ret == 0) {
6644		bss->freq = freq->freq;
6645		return 0;
6646	}
6647	wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
6648		   "%d (%s)", freq->freq, ret, strerror(-ret));
6649nla_put_failure:
6650	nlmsg_free(msg);
6651	return -1;
6652}
6653
6654
6655static u32 sta_flags_nl80211(int flags)
6656{
6657	u32 f = 0;
6658
6659	if (flags & WPA_STA_AUTHORIZED)
6660		f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
6661	if (flags & WPA_STA_WMM)
6662		f |= BIT(NL80211_STA_FLAG_WME);
6663	if (flags & WPA_STA_SHORT_PREAMBLE)
6664		f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
6665	if (flags & WPA_STA_MFP)
6666		f |= BIT(NL80211_STA_FLAG_MFP);
6667	if (flags & WPA_STA_TDLS_PEER)
6668		f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
6669
6670	return f;
6671}
6672
6673
6674static int wpa_driver_nl80211_sta_add(void *priv,
6675				      struct hostapd_sta_add_params *params)
6676{
6677	struct i802_bss *bss = priv;
6678	struct wpa_driver_nl80211_data *drv = bss->drv;
6679	struct nl_msg *msg;
6680	struct nl80211_sta_flag_update upd;
6681	int ret = -ENOBUFS;
6682
6683	if ((params->flags & WPA_STA_TDLS_PEER) &&
6684	    !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6685		return -EOPNOTSUPP;
6686
6687	msg = nlmsg_alloc();
6688	if (!msg)
6689		return -ENOMEM;
6690
6691	wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
6692		   params->set ? "Set" : "Add", MAC2STR(params->addr));
6693	nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
6694		    NL80211_CMD_NEW_STATION);
6695
6696	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6697	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
6698	NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
6699		params->supp_rates);
6700	wpa_hexdump(MSG_DEBUG, "  * supported rates", params->supp_rates,
6701		    params->supp_rates_len);
6702	if (!params->set) {
6703		if (params->aid) {
6704			wpa_printf(MSG_DEBUG, "  * aid=%u", params->aid);
6705			NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
6706		} else {
6707			/*
6708			 * cfg80211 validates that AID is non-zero, so we have
6709			 * to make this a non-zero value for the TDLS case where
6710			 * a dummy STA entry is used for now.
6711			 */
6712			wpa_printf(MSG_DEBUG, "  * aid=1 (TDLS workaround)");
6713			NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
6714		}
6715		wpa_printf(MSG_DEBUG, "  * listen_interval=%u",
6716			   params->listen_interval);
6717		NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
6718			    params->listen_interval);
6719	} else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
6720		wpa_printf(MSG_DEBUG, "  * peer_aid=%u", params->aid);
6721		NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
6722	}
6723	if (params->ht_capabilities) {
6724		wpa_hexdump(MSG_DEBUG, "  * ht_capabilities",
6725			    (u8 *) params->ht_capabilities,
6726			    sizeof(*params->ht_capabilities));
6727		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
6728			sizeof(*params->ht_capabilities),
6729			params->ht_capabilities);
6730	}
6731
6732	if (params->vht_capabilities) {
6733		wpa_hexdump(MSG_DEBUG, "  * vht_capabilities",
6734			    (u8 *) params->vht_capabilities,
6735			    sizeof(*params->vht_capabilities));
6736		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
6737			sizeof(*params->vht_capabilities),
6738			params->vht_capabilities);
6739	}
6740
6741	wpa_printf(MSG_DEBUG, "  * capability=0x%x", params->capability);
6742	NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
6743
6744	if (params->ext_capab) {
6745		wpa_hexdump(MSG_DEBUG, "  * ext_capab",
6746			    params->ext_capab, params->ext_capab_len);
6747		NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
6748			params->ext_capab_len, params->ext_capab);
6749	}
6750
6751	os_memset(&upd, 0, sizeof(upd));
6752	upd.mask = sta_flags_nl80211(params->flags);
6753	upd.set = upd.mask;
6754	wpa_printf(MSG_DEBUG, "  * flags set=0x%x mask=0x%x",
6755		   upd.set, upd.mask);
6756	NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6757
6758	if (params->flags & WPA_STA_WMM) {
6759		struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
6760
6761		if (!wme)
6762			goto nla_put_failure;
6763
6764		wpa_printf(MSG_DEBUG, "  * qosinfo=0x%x", params->qosinfo);
6765		NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
6766				params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
6767		NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
6768				(params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
6769				WMM_QOSINFO_STA_SP_MASK);
6770		nla_nest_end(msg, wme);
6771	}
6772
6773	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6774	msg = NULL;
6775	if (ret)
6776		wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
6777			   "result: %d (%s)", params->set ? "SET" : "NEW", ret,
6778			   strerror(-ret));
6779	if (ret == -EEXIST)
6780		ret = 0;
6781 nla_put_failure:
6782	nlmsg_free(msg);
6783	return ret;
6784}
6785
6786
6787static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
6788{
6789	struct wpa_driver_nl80211_data *drv = bss->drv;
6790	struct nl_msg *msg;
6791	int ret;
6792
6793	msg = nlmsg_alloc();
6794	if (!msg)
6795		return -ENOMEM;
6796
6797	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
6798
6799	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6800		    if_nametoindex(bss->ifname));
6801	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6802
6803	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6804	wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
6805		   " --> %d (%s)",
6806		   bss->ifname, MAC2STR(addr), ret, strerror(-ret));
6807	if (ret == -ENOENT)
6808		return 0;
6809	return ret;
6810 nla_put_failure:
6811	nlmsg_free(msg);
6812	return -ENOBUFS;
6813}
6814
6815
6816static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
6817				 int ifidx)
6818{
6819	struct nl_msg *msg;
6820
6821	wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
6822
6823	/* stop listening for EAPOL on this interface */
6824	del_ifidx(drv, ifidx);
6825
6826	msg = nlmsg_alloc();
6827	if (!msg)
6828		goto nla_put_failure;
6829
6830	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
6831	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
6832
6833	if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6834		return;
6835	msg = NULL;
6836 nla_put_failure:
6837	nlmsg_free(msg);
6838	wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
6839}
6840
6841
6842static const char * nl80211_iftype_str(enum nl80211_iftype mode)
6843{
6844	switch (mode) {
6845	case NL80211_IFTYPE_ADHOC:
6846		return "ADHOC";
6847	case NL80211_IFTYPE_STATION:
6848		return "STATION";
6849	case NL80211_IFTYPE_AP:
6850		return "AP";
6851	case NL80211_IFTYPE_AP_VLAN:
6852		return "AP_VLAN";
6853	case NL80211_IFTYPE_WDS:
6854		return "WDS";
6855	case NL80211_IFTYPE_MONITOR:
6856		return "MONITOR";
6857	case NL80211_IFTYPE_MESH_POINT:
6858		return "MESH_POINT";
6859	case NL80211_IFTYPE_P2P_CLIENT:
6860		return "P2P_CLIENT";
6861	case NL80211_IFTYPE_P2P_GO:
6862		return "P2P_GO";
6863	case NL80211_IFTYPE_P2P_DEVICE:
6864		return "P2P_DEVICE";
6865	default:
6866		return "unknown";
6867	}
6868}
6869
6870
6871static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6872				     const char *ifname,
6873				     enum nl80211_iftype iftype,
6874				     const u8 *addr, int wds,
6875				     int (*handler)(struct nl_msg *, void *),
6876				     void *arg)
6877{
6878	struct nl_msg *msg;
6879	int ifidx;
6880	int ret = -ENOBUFS;
6881
6882	wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6883		   iftype, nl80211_iftype_str(iftype));
6884
6885	msg = nlmsg_alloc();
6886	if (!msg)
6887		return -1;
6888
6889	nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
6890	if (nl80211_set_iface_id(msg, &drv->first_bss) < 0)
6891		goto nla_put_failure;
6892	NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
6893	NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
6894
6895	if (iftype == NL80211_IFTYPE_MONITOR) {
6896		struct nlattr *flags;
6897
6898		flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
6899		if (!flags)
6900			goto nla_put_failure;
6901
6902		NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
6903
6904		nla_nest_end(msg, flags);
6905	} else if (wds) {
6906		NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
6907	}
6908
6909	ret = send_and_recv_msgs(drv, msg, handler, arg);
6910	msg = NULL;
6911	if (ret) {
6912 nla_put_failure:
6913		nlmsg_free(msg);
6914		wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6915			   ifname, ret, strerror(-ret));
6916		return ret;
6917	}
6918
6919	if (iftype == NL80211_IFTYPE_P2P_DEVICE)
6920		return 0;
6921
6922	ifidx = if_nametoindex(ifname);
6923	wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6924		   ifname, ifidx);
6925
6926	if (ifidx <= 0)
6927		return -1;
6928
6929	/* start listening for EAPOL on this interface */
6930	add_ifidx(drv, ifidx);
6931
6932	if (addr && iftype != NL80211_IFTYPE_MONITOR &&
6933	    linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
6934		nl80211_remove_iface(drv, ifidx);
6935		return -1;
6936	}
6937
6938	return ifidx;
6939}
6940
6941
6942static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6943				const char *ifname, enum nl80211_iftype iftype,
6944				const u8 *addr, int wds,
6945				int (*handler)(struct nl_msg *, void *),
6946				void *arg)
6947{
6948	int ret;
6949
6950	ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
6951					arg);
6952
6953	/* if error occurred and interface exists already */
6954	if (ret == -ENFILE && if_nametoindex(ifname)) {
6955		wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6956
6957		/* Try to remove the interface that was already there. */
6958		nl80211_remove_iface(drv, if_nametoindex(ifname));
6959
6960		/* Try to create the interface again */
6961		ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
6962						wds, handler, arg);
6963	}
6964
6965	if (ret >= 0 && is_p2p_net_interface(iftype))
6966		nl80211_disable_11b_rates(drv, ret, 1);
6967
6968	return ret;
6969}
6970
6971
6972static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
6973{
6974	struct ieee80211_hdr *hdr;
6975	u16 fc;
6976	union wpa_event_data event;
6977
6978	hdr = (struct ieee80211_hdr *) buf;
6979	fc = le_to_host16(hdr->frame_control);
6980
6981	os_memset(&event, 0, sizeof(event));
6982	event.tx_status.type = WLAN_FC_GET_TYPE(fc);
6983	event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
6984	event.tx_status.dst = hdr->addr1;
6985	event.tx_status.data = buf;
6986	event.tx_status.data_len = len;
6987	event.tx_status.ack = ok;
6988	wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
6989}
6990
6991
6992static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
6993			     u8 *buf, size_t len)
6994{
6995	struct ieee80211_hdr *hdr = (void *)buf;
6996	u16 fc;
6997	union wpa_event_data event;
6998
6999	if (len < sizeof(*hdr))
7000		return;
7001
7002	fc = le_to_host16(hdr->frame_control);
7003
7004	os_memset(&event, 0, sizeof(event));
7005	event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7006	event.rx_from_unknown.addr = hdr->addr2;
7007	event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7008		(WLAN_FC_FROMDS | WLAN_FC_TODS);
7009	wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
7010}
7011
7012
7013static void handle_frame(struct wpa_driver_nl80211_data *drv,
7014			 u8 *buf, size_t len, int datarate, int ssi_signal)
7015{
7016	struct ieee80211_hdr *hdr;
7017	u16 fc;
7018	union wpa_event_data event;
7019
7020	hdr = (struct ieee80211_hdr *) buf;
7021	fc = le_to_host16(hdr->frame_control);
7022
7023	switch (WLAN_FC_GET_TYPE(fc)) {
7024	case WLAN_FC_TYPE_MGMT:
7025		os_memset(&event, 0, sizeof(event));
7026		event.rx_mgmt.frame = buf;
7027		event.rx_mgmt.frame_len = len;
7028		event.rx_mgmt.datarate = datarate;
7029		event.rx_mgmt.ssi_signal = ssi_signal;
7030		wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
7031		break;
7032	case WLAN_FC_TYPE_CTRL:
7033		/* can only get here with PS-Poll frames */
7034		wpa_printf(MSG_DEBUG, "CTRL");
7035		from_unknown_sta(drv, buf, len);
7036		break;
7037	case WLAN_FC_TYPE_DATA:
7038		from_unknown_sta(drv, buf, len);
7039		break;
7040	}
7041}
7042
7043
7044static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7045{
7046	struct wpa_driver_nl80211_data *drv = eloop_ctx;
7047	int len;
7048	unsigned char buf[3000];
7049	struct ieee80211_radiotap_iterator iter;
7050	int ret;
7051	int datarate = 0, ssi_signal = 0;
7052	int injected = 0, failed = 0, rxflags = 0;
7053
7054	len = recv(sock, buf, sizeof(buf), 0);
7055	if (len < 0) {
7056		perror("recv");
7057		return;
7058	}
7059
7060	if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
7061		printf("received invalid radiotap frame\n");
7062		return;
7063	}
7064
7065	while (1) {
7066		ret = ieee80211_radiotap_iterator_next(&iter);
7067		if (ret == -ENOENT)
7068			break;
7069		if (ret) {
7070			printf("received invalid radiotap frame (%d)\n", ret);
7071			return;
7072		}
7073		switch (iter.this_arg_index) {
7074		case IEEE80211_RADIOTAP_FLAGS:
7075			if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7076				len -= 4;
7077			break;
7078		case IEEE80211_RADIOTAP_RX_FLAGS:
7079			rxflags = 1;
7080			break;
7081		case IEEE80211_RADIOTAP_TX_FLAGS:
7082			injected = 1;
7083			failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7084					IEEE80211_RADIOTAP_F_TX_FAIL;
7085			break;
7086		case IEEE80211_RADIOTAP_DATA_RETRIES:
7087			break;
7088		case IEEE80211_RADIOTAP_CHANNEL:
7089			/* TODO: convert from freq/flags to channel number */
7090			break;
7091		case IEEE80211_RADIOTAP_RATE:
7092			datarate = *iter.this_arg * 5;
7093			break;
7094		case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
7095			ssi_signal = (s8) *iter.this_arg;
7096			break;
7097		}
7098	}
7099
7100	if (rxflags && injected)
7101		return;
7102
7103	if (!injected)
7104		handle_frame(drv, buf + iter.max_length,
7105			     len - iter.max_length, datarate, ssi_signal);
7106	else
7107		handle_tx_callback(drv->ctx, buf + iter.max_length,
7108				   len - iter.max_length, !failed);
7109}
7110
7111
7112/*
7113 * we post-process the filter code later and rewrite
7114 * this to the offset to the last instruction
7115 */
7116#define PASS	0xFF
7117#define FAIL	0xFE
7118
7119static struct sock_filter msock_filter_insns[] = {
7120	/*
7121	 * do a little-endian load of the radiotap length field
7122	 */
7123	/* load lower byte into A */
7124	BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
7125	/* put it into X (== index register) */
7126	BPF_STMT(BPF_MISC| BPF_TAX, 0),
7127	/* load upper byte into A */
7128	BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
7129	/* left-shift it by 8 */
7130	BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
7131	/* or with X */
7132	BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
7133	/* put result into X */
7134	BPF_STMT(BPF_MISC| BPF_TAX, 0),
7135
7136	/*
7137	 * Allow management frames through, this also gives us those
7138	 * management frames that we sent ourselves with status
7139	 */
7140	/* load the lower byte of the IEEE 802.11 frame control field */
7141	BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
7142	/* mask off frame type and version */
7143	BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
7144	/* accept frame if it's both 0, fall through otherwise */
7145	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
7146
7147	/*
7148	 * TODO: add a bit to radiotap RX flags that indicates
7149	 * that the sending station is not associated, then
7150	 * add a filter here that filters on our DA and that flag
7151	 * to allow us to deauth frames to that bad station.
7152	 *
7153	 * For now allow all To DS data frames through.
7154	 */
7155	/* load the IEEE 802.11 frame control field */
7156	BPF_STMT(BPF_LD  | BPF_H | BPF_IND, 0),
7157	/* mask off frame type, version and DS status */
7158	BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
7159	/* accept frame if version 0, type 2 and To DS, fall through otherwise
7160	 */
7161	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
7162
7163#if 0
7164	/*
7165	 * drop non-data frames
7166	 */
7167	/* load the lower byte of the frame control field */
7168	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
7169	/* mask off QoS bit */
7170	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
7171	/* drop non-data frames */
7172	BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
7173#endif
7174	/* load the upper byte of the frame control field */
7175	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 1),
7176	/* mask off toDS/fromDS */
7177	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
7178	/* accept WDS frames */
7179	BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, PASS, 0),
7180
7181	/*
7182	 * add header length to index
7183	 */
7184	/* load the lower byte of the frame control field */
7185	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
7186	/* mask off QoS bit */
7187	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
7188	/* right shift it by 6 to give 0 or 2 */
7189	BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
7190	/* add data frame header length */
7191	BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
7192	/* add index, was start of 802.11 header */
7193	BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
7194	/* move to index, now start of LL header */
7195	BPF_STMT(BPF_MISC | BPF_TAX, 0),
7196
7197	/*
7198	 * Accept empty data frames, we use those for
7199	 * polling activity.
7200	 */
7201	BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
7202	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
7203
7204	/*
7205	 * Accept EAPOL frames
7206	 */
7207	BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
7208	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
7209	BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
7210	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
7211
7212	/* keep these last two statements or change the code below */
7213	/* return 0 == "DROP" */
7214	BPF_STMT(BPF_RET | BPF_K, 0),
7215	/* return ~0 == "keep all" */
7216	BPF_STMT(BPF_RET | BPF_K, ~0),
7217};
7218
7219static struct sock_fprog msock_filter = {
7220	.len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
7221	.filter = msock_filter_insns,
7222};
7223
7224
7225static int add_monitor_filter(int s)
7226{
7227	int idx;
7228
7229	/* rewrite all PASS/FAIL jump offsets */
7230	for (idx = 0; idx < msock_filter.len; idx++) {
7231		struct sock_filter *insn = &msock_filter_insns[idx];
7232
7233		if (BPF_CLASS(insn->code) == BPF_JMP) {
7234			if (insn->code == (BPF_JMP|BPF_JA)) {
7235				if (insn->k == PASS)
7236					insn->k = msock_filter.len - idx - 2;
7237				else if (insn->k == FAIL)
7238					insn->k = msock_filter.len - idx - 3;
7239			}
7240
7241			if (insn->jt == PASS)
7242				insn->jt = msock_filter.len - idx - 2;
7243			else if (insn->jt == FAIL)
7244				insn->jt = msock_filter.len - idx - 3;
7245
7246			if (insn->jf == PASS)
7247				insn->jf = msock_filter.len - idx - 2;
7248			else if (insn->jf == FAIL)
7249				insn->jf = msock_filter.len - idx - 3;
7250		}
7251	}
7252
7253	if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
7254		       &msock_filter, sizeof(msock_filter))) {
7255		perror("SO_ATTACH_FILTER");
7256		return -1;
7257	}
7258
7259	return 0;
7260}
7261
7262
7263static void nl80211_remove_monitor_interface(
7264	struct wpa_driver_nl80211_data *drv)
7265{
7266	drv->monitor_refcount--;
7267	if (drv->monitor_refcount > 0)
7268		return;
7269
7270	if (drv->monitor_ifidx >= 0) {
7271		nl80211_remove_iface(drv, drv->monitor_ifidx);
7272		drv->monitor_ifidx = -1;
7273	}
7274	if (drv->monitor_sock >= 0) {
7275		eloop_unregister_read_sock(drv->monitor_sock);
7276		close(drv->monitor_sock);
7277		drv->monitor_sock = -1;
7278	}
7279}
7280
7281
7282static int
7283nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
7284{
7285	char buf[IFNAMSIZ];
7286	struct sockaddr_ll ll;
7287	int optval;
7288	socklen_t optlen;
7289
7290	if (drv->monitor_ifidx >= 0) {
7291		drv->monitor_refcount++;
7292		return 0;
7293	}
7294
7295	if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
7296		/*
7297		 * P2P interface name is of the format p2p-%s-%d. For monitor
7298		 * interface name corresponding to P2P GO, replace "p2p-" with
7299		 * "mon-" to retain the same interface name length and to
7300		 * indicate that it is a monitor interface.
7301		 */
7302		snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
7303	} else {
7304		/* Non-P2P interface with AP functionality. */
7305		snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
7306	}
7307
7308	buf[IFNAMSIZ - 1] = '\0';
7309
7310	drv->monitor_ifidx =
7311		nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
7312				     0, NULL, NULL);
7313
7314	if (drv->monitor_ifidx == -EOPNOTSUPP) {
7315		/*
7316		 * This is backward compatibility for a few versions of
7317		 * the kernel only that didn't advertise the right
7318		 * attributes for the only driver that then supported
7319		 * AP mode w/o monitor -- ath6kl.
7320		 */
7321		wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
7322			   "monitor interface type - try to run without it");
7323		drv->device_ap_sme = 1;
7324	}
7325
7326	if (drv->monitor_ifidx < 0)
7327		return -1;
7328
7329	if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
7330		goto error;
7331
7332	memset(&ll, 0, sizeof(ll));
7333	ll.sll_family = AF_PACKET;
7334	ll.sll_ifindex = drv->monitor_ifidx;
7335	drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
7336	if (drv->monitor_sock < 0) {
7337		perror("socket[PF_PACKET,SOCK_RAW]");
7338		goto error;
7339	}
7340
7341	if (add_monitor_filter(drv->monitor_sock)) {
7342		wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
7343			   "interface; do filtering in user space");
7344		/* This works, but will cost in performance. */
7345	}
7346
7347	if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
7348		perror("monitor socket bind");
7349		goto error;
7350	}
7351
7352	optlen = sizeof(optval);
7353	optval = 20;
7354	if (setsockopt
7355	    (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
7356		perror("Failed to set socket priority");
7357		goto error;
7358	}
7359
7360	if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
7361				     drv, NULL)) {
7362		printf("Could not register monitor read socket\n");
7363		goto error;
7364	}
7365
7366	return 0;
7367 error:
7368	nl80211_remove_monitor_interface(drv);
7369	return -1;
7370}
7371
7372
7373static int nl80211_setup_ap(struct i802_bss *bss)
7374{
7375	struct wpa_driver_nl80211_data *drv = bss->drv;
7376
7377	wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
7378		   "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
7379
7380	/*
7381	 * Disable Probe Request reporting unless we need it in this way for
7382	 * devices that include the AP SME, in the other case (unless using
7383	 * monitor iface) we'll get it through the nl_mgmt socket instead.
7384	 */
7385	if (!drv->device_ap_sme)
7386		wpa_driver_nl80211_probe_req_report(bss, 0);
7387
7388	if (!drv->device_ap_sme && !drv->use_monitor)
7389		if (nl80211_mgmt_subscribe_ap(bss))
7390			return -1;
7391
7392	if (drv->device_ap_sme && !drv->use_monitor)
7393		if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
7394			return -1;
7395
7396	if (!drv->device_ap_sme && drv->use_monitor &&
7397	    nl80211_create_monitor_interface(drv) &&
7398	    !drv->device_ap_sme)
7399		return -1;
7400
7401#ifdef ANDROID_P2P
7402	if (drv->device_ap_sme && drv->use_monitor)
7403		if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
7404			return -1;
7405
7406	if (drv->use_monitor &&
7407	    nl80211_create_monitor_interface(drv))
7408		return -1;
7409#endif
7410
7411	if (drv->device_ap_sme &&
7412	    wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
7413		wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
7414			   "Probe Request frame reporting in AP mode");
7415		/* Try to survive without this */
7416	}
7417
7418	return 0;
7419}
7420
7421
7422static void nl80211_teardown_ap(struct i802_bss *bss)
7423{
7424	struct wpa_driver_nl80211_data *drv = bss->drv;
7425
7426	if (drv->device_ap_sme) {
7427		wpa_driver_nl80211_probe_req_report(bss, 0);
7428		if (!drv->use_monitor)
7429			nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
7430	} else if (drv->use_monitor)
7431		nl80211_remove_monitor_interface(drv);
7432	else
7433		nl80211_mgmt_unsubscribe(bss, "AP teardown");
7434
7435	bss->beacon_set = 0;
7436}
7437
7438
7439static int nl80211_send_eapol_data(struct i802_bss *bss,
7440				   const u8 *addr, const u8 *data,
7441				   size_t data_len)
7442{
7443	struct sockaddr_ll ll;
7444	int ret;
7445
7446	if (bss->drv->eapol_tx_sock < 0) {
7447		wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
7448		return -1;
7449	}
7450
7451	os_memset(&ll, 0, sizeof(ll));
7452	ll.sll_family = AF_PACKET;
7453	ll.sll_ifindex = bss->ifindex;
7454	ll.sll_protocol = htons(ETH_P_PAE);
7455	ll.sll_halen = ETH_ALEN;
7456	os_memcpy(ll.sll_addr, addr, ETH_ALEN);
7457	ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
7458		     (struct sockaddr *) &ll, sizeof(ll));
7459	if (ret < 0)
7460		wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
7461			   strerror(errno));
7462
7463	return ret;
7464}
7465
7466
7467static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
7468
7469static int wpa_driver_nl80211_hapd_send_eapol(
7470	void *priv, const u8 *addr, const u8 *data,
7471	size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
7472{
7473	struct i802_bss *bss = priv;
7474	struct wpa_driver_nl80211_data *drv = bss->drv;
7475	struct ieee80211_hdr *hdr;
7476	size_t len;
7477	u8 *pos;
7478	int res;
7479	int qos = flags & WPA_STA_WMM;
7480#ifndef ANDROID_P2P
7481	if (drv->device_ap_sme || !drv->use_monitor)
7482#else
7483	if (drv->device_ap_sme && !drv->use_monitor)
7484#endif
7485		return nl80211_send_eapol_data(bss, addr, data, data_len);
7486
7487	len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
7488		data_len;
7489	hdr = os_zalloc(len);
7490	if (hdr == NULL) {
7491		printf("malloc() failed for i802_send_data(len=%lu)\n",
7492		       (unsigned long) len);
7493		return -1;
7494	}
7495
7496	hdr->frame_control =
7497		IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
7498	hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
7499	if (encrypt)
7500		hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
7501	if (qos) {
7502		hdr->frame_control |=
7503			host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
7504	}
7505
7506	memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7507	memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7508	memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7509	pos = (u8 *) (hdr + 1);
7510
7511	if (qos) {
7512		/* Set highest priority in QoS header */
7513		pos[0] = 7;
7514		pos[1] = 0;
7515		pos += 2;
7516	}
7517
7518	memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
7519	pos += sizeof(rfc1042_header);
7520	WPA_PUT_BE16(pos, ETH_P_PAE);
7521	pos += 2;
7522	memcpy(pos, data, data_len);
7523
7524	res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
7525					    0, 0, 0, 0);
7526	if (res < 0) {
7527		wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
7528			   "failed: %d (%s)",
7529			   (unsigned long) len, errno, strerror(errno));
7530	}
7531	os_free(hdr);
7532
7533	return res;
7534}
7535
7536
7537static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
7538					    int total_flags,
7539					    int flags_or, int flags_and)
7540{
7541	struct i802_bss *bss = priv;
7542	struct wpa_driver_nl80211_data *drv = bss->drv;
7543	struct nl_msg *msg;
7544	struct nlattr *flags;
7545	struct nl80211_sta_flag_update upd;
7546
7547	msg = nlmsg_alloc();
7548	if (!msg)
7549		return -ENOMEM;
7550
7551	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
7552
7553	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7554		    if_nametoindex(bss->ifname));
7555	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7556
7557	/*
7558	 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
7559	 * can be removed eventually.
7560	 */
7561	flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
7562	if (!flags)
7563		goto nla_put_failure;
7564	if (total_flags & WPA_STA_AUTHORIZED)
7565		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
7566
7567	if (total_flags & WPA_STA_WMM)
7568		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
7569
7570	if (total_flags & WPA_STA_SHORT_PREAMBLE)
7571		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
7572
7573	if (total_flags & WPA_STA_MFP)
7574		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
7575
7576	if (total_flags & WPA_STA_TDLS_PEER)
7577		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
7578
7579	nla_nest_end(msg, flags);
7580
7581	os_memset(&upd, 0, sizeof(upd));
7582	upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
7583	upd.set = sta_flags_nl80211(flags_or);
7584	NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7585
7586	return send_and_recv_msgs(drv, msg, NULL, NULL);
7587 nla_put_failure:
7588	nlmsg_free(msg);
7589	return -ENOBUFS;
7590}
7591
7592
7593static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
7594				 struct wpa_driver_associate_params *params)
7595{
7596	enum nl80211_iftype nlmode, old_mode;
7597	struct hostapd_freq_params freq = {
7598		.freq = params->freq,
7599	};
7600
7601	if (params->p2p) {
7602		wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
7603			   "group (GO)");
7604		nlmode = NL80211_IFTYPE_P2P_GO;
7605	} else
7606		nlmode = NL80211_IFTYPE_AP;
7607
7608	old_mode = drv->nlmode;
7609	if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode)) {
7610		nl80211_remove_monitor_interface(drv);
7611		return -1;
7612	}
7613
7614	if (wpa_driver_nl80211_set_freq(&drv->first_bss, &freq)) {
7615		if (old_mode != nlmode)
7616			wpa_driver_nl80211_set_mode(&drv->first_bss, old_mode);
7617		nl80211_remove_monitor_interface(drv);
7618		return -1;
7619	}
7620
7621	return 0;
7622}
7623
7624
7625static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
7626{
7627	struct nl_msg *msg;
7628	int ret = -1;
7629
7630	msg = nlmsg_alloc();
7631	if (!msg)
7632		return -1;
7633
7634	nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
7635	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7636	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7637	msg = NULL;
7638	if (ret) {
7639		wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
7640			   "(%s)", ret, strerror(-ret));
7641		goto nla_put_failure;
7642	}
7643
7644	ret = 0;
7645	wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
7646
7647nla_put_failure:
7648	if (wpa_driver_nl80211_set_mode(&drv->first_bss,
7649					NL80211_IFTYPE_STATION)) {
7650		wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7651			   "station mode");
7652	}
7653
7654	nlmsg_free(msg);
7655	return ret;
7656}
7657
7658
7659static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
7660				   struct wpa_driver_associate_params *params)
7661{
7662	struct nl_msg *msg;
7663	int ret = -1;
7664	int count = 0;
7665
7666	wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
7667
7668	if (wpa_driver_nl80211_set_mode(&drv->first_bss,
7669					NL80211_IFTYPE_ADHOC)) {
7670		wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7671			   "IBSS mode");
7672		return -1;
7673	}
7674
7675retry:
7676	msg = nlmsg_alloc();
7677	if (!msg)
7678		return -1;
7679
7680	nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
7681	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7682
7683	if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
7684		goto nla_put_failure;
7685
7686	wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
7687			  params->ssid, params->ssid_len);
7688	NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7689		params->ssid);
7690	os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7691	drv->ssid_len = params->ssid_len;
7692
7693	wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
7694	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7695
7696	ret = nl80211_set_conn_keys(params, msg);
7697	if (ret)
7698		goto nla_put_failure;
7699
7700	if (params->bssid && params->fixed_bssid) {
7701		wpa_printf(MSG_DEBUG, "  * BSSID=" MACSTR,
7702			   MAC2STR(params->bssid));
7703		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7704	}
7705
7706	if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7707	    params->key_mgmt_suite == KEY_MGMT_PSK ||
7708	    params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
7709	    params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
7710		wpa_printf(MSG_DEBUG, "  * control port");
7711		NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7712	}
7713
7714	if (params->wpa_ie) {
7715		wpa_hexdump(MSG_DEBUG,
7716			    "  * Extra IEs for Beacon/Probe Response frames",
7717			    params->wpa_ie, params->wpa_ie_len);
7718		NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7719			params->wpa_ie);
7720	}
7721
7722	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7723	msg = NULL;
7724	if (ret) {
7725		wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
7726			   ret, strerror(-ret));
7727		count++;
7728		if (ret == -EALREADY && count == 1) {
7729			wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
7730				   "forced leave");
7731			nl80211_leave_ibss(drv);
7732			nlmsg_free(msg);
7733			goto retry;
7734		}
7735
7736		goto nla_put_failure;
7737	}
7738	ret = 0;
7739	wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
7740
7741nla_put_failure:
7742	nlmsg_free(msg);
7743	return ret;
7744}
7745
7746
7747static int wpa_driver_nl80211_try_connect(
7748	struct wpa_driver_nl80211_data *drv,
7749	struct wpa_driver_associate_params *params)
7750{
7751	struct nl_msg *msg;
7752	enum nl80211_auth_type type;
7753	int ret = 0;
7754	int algs;
7755
7756	msg = nlmsg_alloc();
7757	if (!msg)
7758		return -1;
7759
7760	wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
7761	nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
7762
7763	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7764	if (params->bssid) {
7765		wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
7766			   MAC2STR(params->bssid));
7767		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7768	}
7769	if (params->freq) {
7770		wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
7771		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7772		drv->assoc_freq = params->freq;
7773	} else
7774		drv->assoc_freq = 0;
7775	if (params->bg_scan_period >= 0) {
7776		wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
7777			   params->bg_scan_period);
7778		NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7779			    params->bg_scan_period);
7780	}
7781	if (params->ssid) {
7782		wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
7783				  params->ssid, params->ssid_len);
7784		NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7785			params->ssid);
7786		if (params->ssid_len > sizeof(drv->ssid))
7787			goto nla_put_failure;
7788		os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7789		drv->ssid_len = params->ssid_len;
7790	}
7791	wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
7792	if (params->wpa_ie)
7793		NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7794			params->wpa_ie);
7795
7796	algs = 0;
7797	if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7798		algs++;
7799	if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7800		algs++;
7801	if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7802		algs++;
7803	if (algs > 1) {
7804		wpa_printf(MSG_DEBUG, "  * Leave out Auth Type for automatic "
7805			   "selection");
7806		goto skip_auth_type;
7807	}
7808
7809	if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7810		type = NL80211_AUTHTYPE_OPEN_SYSTEM;
7811	else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7812		type = NL80211_AUTHTYPE_SHARED_KEY;
7813	else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7814		type = NL80211_AUTHTYPE_NETWORK_EAP;
7815	else if (params->auth_alg & WPA_AUTH_ALG_FT)
7816		type = NL80211_AUTHTYPE_FT;
7817	else
7818		goto nla_put_failure;
7819
7820	wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
7821	NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
7822
7823skip_auth_type:
7824	if (params->wpa_proto) {
7825		enum nl80211_wpa_versions ver = 0;
7826
7827		if (params->wpa_proto & WPA_PROTO_WPA)
7828			ver |= NL80211_WPA_VERSION_1;
7829		if (params->wpa_proto & WPA_PROTO_RSN)
7830			ver |= NL80211_WPA_VERSION_2;
7831
7832		wpa_printf(MSG_DEBUG, "  * WPA Versions 0x%x", ver);
7833		NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7834	}
7835
7836	if (params->pairwise_suite != CIPHER_NONE) {
7837		int cipher;
7838
7839		switch (params->pairwise_suite) {
7840		case CIPHER_SMS4:
7841			cipher = WLAN_CIPHER_SUITE_SMS4;
7842			break;
7843		case CIPHER_WEP40:
7844			cipher = WLAN_CIPHER_SUITE_WEP40;
7845			break;
7846		case CIPHER_WEP104:
7847			cipher = WLAN_CIPHER_SUITE_WEP104;
7848			break;
7849		case CIPHER_CCMP:
7850			cipher = WLAN_CIPHER_SUITE_CCMP;
7851			break;
7852		case CIPHER_GCMP:
7853			cipher = WLAN_CIPHER_SUITE_GCMP;
7854			break;
7855		case CIPHER_TKIP:
7856		default:
7857			cipher = WLAN_CIPHER_SUITE_TKIP;
7858			break;
7859		}
7860		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7861	}
7862
7863	if (params->group_suite != CIPHER_NONE) {
7864		int cipher;
7865
7866		switch (params->group_suite) {
7867		case CIPHER_SMS4:
7868			cipher = WLAN_CIPHER_SUITE_SMS4;
7869			break;
7870		case CIPHER_WEP40:
7871			cipher = WLAN_CIPHER_SUITE_WEP40;
7872			break;
7873		case CIPHER_WEP104:
7874			cipher = WLAN_CIPHER_SUITE_WEP104;
7875			break;
7876		case CIPHER_CCMP:
7877			cipher = WLAN_CIPHER_SUITE_CCMP;
7878			break;
7879		case CIPHER_GCMP:
7880			cipher = WLAN_CIPHER_SUITE_GCMP;
7881			break;
7882		case CIPHER_TKIP:
7883		default:
7884			cipher = WLAN_CIPHER_SUITE_TKIP;
7885			break;
7886		}
7887		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7888	}
7889
7890	if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7891	    params->key_mgmt_suite == KEY_MGMT_PSK ||
7892	    params->key_mgmt_suite == KEY_MGMT_FT_802_1X ||
7893	    params->key_mgmt_suite == KEY_MGMT_FT_PSK ||
7894	    params->key_mgmt_suite == KEY_MGMT_CCKM) {
7895		int mgmt = WLAN_AKM_SUITE_PSK;
7896
7897		switch (params->key_mgmt_suite) {
7898		case KEY_MGMT_CCKM:
7899			mgmt = WLAN_AKM_SUITE_CCKM;
7900			break;
7901		case KEY_MGMT_802_1X:
7902			mgmt = WLAN_AKM_SUITE_8021X;
7903			break;
7904		case KEY_MGMT_FT_802_1X:
7905			mgmt = WLAN_AKM_SUITE_FT_8021X;
7906			break;
7907		case KEY_MGMT_FT_PSK:
7908			mgmt = WLAN_AKM_SUITE_FT_PSK;
7909			break;
7910		case KEY_MGMT_PSK:
7911		default:
7912			mgmt = WLAN_AKM_SUITE_PSK;
7913			break;
7914		}
7915		NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
7916	}
7917
7918#ifdef CONFIG_IEEE80211W
7919	if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7920		NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7921#endif /* CONFIG_IEEE80211W */
7922
7923	if (params->disable_ht)
7924		NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7925
7926	if (params->htcaps && params->htcaps_mask) {
7927		int sz = sizeof(struct ieee80211_ht_capabilities);
7928		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7929		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7930			params->htcaps_mask);
7931	}
7932
7933#ifdef CONFIG_VHT_OVERRIDES
7934	if (params->disable_vht) {
7935		wpa_printf(MSG_DEBUG, "  * VHT disabled");
7936		NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7937	}
7938
7939	if (params->vhtcaps && params->vhtcaps_mask) {
7940		int sz = sizeof(struct ieee80211_vht_capabilities);
7941		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7942		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7943			params->vhtcaps_mask);
7944	}
7945#endif /* CONFIG_VHT_OVERRIDES */
7946
7947	ret = nl80211_set_conn_keys(params, msg);
7948	if (ret)
7949		goto nla_put_failure;
7950
7951	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7952	msg = NULL;
7953	if (ret) {
7954		wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7955			   "(%s)", ret, strerror(-ret));
7956		goto nla_put_failure;
7957	}
7958	ret = 0;
7959	wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
7960
7961nla_put_failure:
7962	nlmsg_free(msg);
7963	return ret;
7964
7965}
7966
7967
7968static int wpa_driver_nl80211_connect(
7969	struct wpa_driver_nl80211_data *drv,
7970	struct wpa_driver_associate_params *params)
7971{
7972	int ret = wpa_driver_nl80211_try_connect(drv, params);
7973	if (ret == -EALREADY) {
7974		/*
7975		 * cfg80211 does not currently accept new connections if
7976		 * we are already connected. As a workaround, force
7977		 * disconnection and try again.
7978		 */
7979		wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7980			   "disconnecting before reassociation "
7981			   "attempt");
7982		if (wpa_driver_nl80211_disconnect(
7983			    drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
7984			return -1;
7985		ret = wpa_driver_nl80211_try_connect(drv, params);
7986	}
7987	return ret;
7988}
7989
7990
7991static int wpa_driver_nl80211_associate(
7992	void *priv, struct wpa_driver_associate_params *params)
7993{
7994	struct i802_bss *bss = priv;
7995	struct wpa_driver_nl80211_data *drv = bss->drv;
7996	int ret = -1;
7997	struct nl_msg *msg;
7998
7999	if (params->mode == IEEE80211_MODE_AP)
8000		return wpa_driver_nl80211_ap(drv, params);
8001
8002	if (params->mode == IEEE80211_MODE_IBSS)
8003		return wpa_driver_nl80211_ibss(drv, params);
8004
8005	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
8006		enum nl80211_iftype nlmode = params->p2p ?
8007			NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8008
8009		if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
8010			return -1;
8011		return wpa_driver_nl80211_connect(drv, params);
8012	}
8013
8014	nl80211_mark_disconnected(drv);
8015
8016	msg = nlmsg_alloc();
8017	if (!msg)
8018		return -1;
8019
8020	wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8021		   drv->ifindex);
8022	nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
8023
8024	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8025	if (params->bssid) {
8026		wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
8027			   MAC2STR(params->bssid));
8028		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8029	}
8030	if (params->freq) {
8031		wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
8032		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8033		drv->assoc_freq = params->freq;
8034	} else
8035		drv->assoc_freq = 0;
8036	if (params->bg_scan_period >= 0) {
8037		wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
8038			   params->bg_scan_period);
8039		NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8040			    params->bg_scan_period);
8041	}
8042	if (params->ssid) {
8043		wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
8044				  params->ssid, params->ssid_len);
8045		NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8046			params->ssid);
8047		if (params->ssid_len > sizeof(drv->ssid))
8048			goto nla_put_failure;
8049		os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8050		drv->ssid_len = params->ssid_len;
8051	}
8052	wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
8053	if (params->wpa_ie)
8054		NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8055			params->wpa_ie);
8056
8057	if (params->pairwise_suite != CIPHER_NONE) {
8058		int cipher;
8059
8060		switch (params->pairwise_suite) {
8061		case CIPHER_WEP40:
8062			cipher = WLAN_CIPHER_SUITE_WEP40;
8063			break;
8064		case CIPHER_WEP104:
8065			cipher = WLAN_CIPHER_SUITE_WEP104;
8066			break;
8067		case CIPHER_CCMP:
8068			cipher = WLAN_CIPHER_SUITE_CCMP;
8069			break;
8070		case CIPHER_GCMP:
8071			cipher = WLAN_CIPHER_SUITE_GCMP;
8072			break;
8073		case CIPHER_TKIP:
8074		default:
8075			cipher = WLAN_CIPHER_SUITE_TKIP;
8076			break;
8077		}
8078		wpa_printf(MSG_DEBUG, "  * pairwise=0x%x", cipher);
8079		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8080	}
8081
8082	if (params->group_suite != CIPHER_NONE) {
8083		int cipher;
8084
8085		switch (params->group_suite) {
8086		case CIPHER_WEP40:
8087			cipher = WLAN_CIPHER_SUITE_WEP40;
8088			break;
8089		case CIPHER_WEP104:
8090			cipher = WLAN_CIPHER_SUITE_WEP104;
8091			break;
8092		case CIPHER_CCMP:
8093			cipher = WLAN_CIPHER_SUITE_CCMP;
8094			break;
8095		case CIPHER_GCMP:
8096			cipher = WLAN_CIPHER_SUITE_GCMP;
8097			break;
8098		case CIPHER_TKIP:
8099		default:
8100			cipher = WLAN_CIPHER_SUITE_TKIP;
8101			break;
8102		}
8103		wpa_printf(MSG_DEBUG, "  * group=0x%x", cipher);
8104		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8105	}
8106
8107#ifdef CONFIG_IEEE80211W
8108	if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8109		NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8110#endif /* CONFIG_IEEE80211W */
8111
8112	NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8113
8114	if (params->prev_bssid) {
8115		wpa_printf(MSG_DEBUG, "  * prev_bssid=" MACSTR,
8116			   MAC2STR(params->prev_bssid));
8117		NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8118			params->prev_bssid);
8119	}
8120
8121	if (params->disable_ht)
8122		NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8123
8124	if (params->htcaps && params->htcaps_mask) {
8125		int sz = sizeof(struct ieee80211_ht_capabilities);
8126		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
8127		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8128			params->htcaps_mask);
8129	}
8130
8131#ifdef CONFIG_VHT_OVERRIDES
8132	if (params->disable_vht) {
8133		wpa_printf(MSG_DEBUG, "  * VHT disabled");
8134		NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8135	}
8136
8137	if (params->vhtcaps && params->vhtcaps_mask) {
8138		int sz = sizeof(struct ieee80211_vht_capabilities);
8139		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
8140		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8141			params->vhtcaps_mask);
8142	}
8143#endif /* CONFIG_VHT_OVERRIDES */
8144
8145	if (params->p2p)
8146		wpa_printf(MSG_DEBUG, "  * P2P group");
8147
8148	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8149	msg = NULL;
8150	if (ret) {
8151		wpa_dbg(drv->ctx, MSG_DEBUG,
8152			"nl80211: MLME command failed (assoc): ret=%d (%s)",
8153			ret, strerror(-ret));
8154		nl80211_dump_scan(drv);
8155		goto nla_put_failure;
8156	}
8157	ret = 0;
8158	wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8159		   "successfully");
8160
8161nla_put_failure:
8162	nlmsg_free(msg);
8163	return ret;
8164}
8165
8166
8167static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
8168			    int ifindex, enum nl80211_iftype mode)
8169{
8170	struct nl_msg *msg;
8171	int ret = -ENOBUFS;
8172
8173	wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8174		   ifindex, mode, nl80211_iftype_str(mode));
8175
8176	msg = nlmsg_alloc();
8177	if (!msg)
8178		return -ENOMEM;
8179
8180	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
8181	if (nl80211_set_iface_id(msg, &drv->first_bss) < 0)
8182		goto nla_put_failure;
8183	NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
8184
8185	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8186	msg = NULL;
8187	if (!ret)
8188		return 0;
8189nla_put_failure:
8190	nlmsg_free(msg);
8191	wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
8192		   " %d (%s)", ifindex, mode, ret, strerror(-ret));
8193	return ret;
8194}
8195
8196
8197static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
8198				       enum nl80211_iftype nlmode)
8199{
8200	struct wpa_driver_nl80211_data *drv = bss->drv;
8201	int ret = -1;
8202	int i;
8203	int was_ap = is_ap_interface(drv->nlmode);
8204	int res;
8205
8206	res = nl80211_set_mode(drv, drv->ifindex, nlmode);
8207	if (res && nlmode == nl80211_get_ifmode(bss))
8208		res = 0;
8209
8210	if (res == 0) {
8211		drv->nlmode = nlmode;
8212		ret = 0;
8213		goto done;
8214	}
8215
8216	if (res == -ENODEV)
8217		return -1;
8218
8219	if (nlmode == drv->nlmode) {
8220		wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
8221			   "requested mode - ignore error");
8222		ret = 0;
8223		goto done; /* Already in the requested mode */
8224	}
8225
8226	/* mac80211 doesn't allow mode changes while the device is up, so
8227	 * take the device down, try to set the mode again, and bring the
8228	 * device back up.
8229	 */
8230	wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
8231		   "interface down");
8232	for (i = 0; i < 10; i++) {
8233		res = i802_set_iface_flags(bss, 0);
8234		if (res == -EACCES || res == -ENODEV)
8235			break;
8236		if (res == 0) {
8237			/* Try to set the mode again while the interface is
8238			 * down */
8239			ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
8240			if (ret == -EACCES)
8241				break;
8242			res = i802_set_iface_flags(bss, 1);
8243			if (res && !ret)
8244				ret = -1;
8245			else if (ret != -EBUSY)
8246				break;
8247		} else
8248			wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
8249				   "interface down");
8250		os_sleep(0, 100000);
8251	}
8252
8253	if (!ret) {
8254		wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
8255			   "interface is down");
8256		drv->nlmode = nlmode;
8257		drv->ignore_if_down_event = 1;
8258	}
8259
8260done:
8261	if (ret) {
8262		wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
8263			   "from %d failed", nlmode, drv->nlmode);
8264		return ret;
8265	}
8266
8267	if (is_p2p_net_interface(nlmode))
8268		nl80211_disable_11b_rates(drv, drv->ifindex, 1);
8269	else if (drv->disabled_11b_rates)
8270		nl80211_disable_11b_rates(drv, drv->ifindex, 0);
8271
8272	if (is_ap_interface(nlmode)) {
8273		nl80211_mgmt_unsubscribe(bss, "start AP");
8274		/* Setup additional AP mode functionality if needed */
8275		if (nl80211_setup_ap(bss))
8276			return -1;
8277	} else if (was_ap) {
8278		/* Remove additional AP mode functionality */
8279		nl80211_teardown_ap(bss);
8280	} else {
8281		nl80211_mgmt_unsubscribe(bss, "mode change");
8282	}
8283
8284	if (!bss->in_deinit && !is_ap_interface(nlmode) &&
8285	    nl80211_mgmt_subscribe_non_ap(bss) < 0)
8286		wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
8287			   "frame processing - ignore for now");
8288
8289	return 0;
8290}
8291
8292
8293static int wpa_driver_nl80211_get_capa(void *priv,
8294				       struct wpa_driver_capa *capa)
8295{
8296	struct i802_bss *bss = priv;
8297	struct wpa_driver_nl80211_data *drv = bss->drv;
8298	if (!drv->has_capability)
8299		return -1;
8300	os_memcpy(capa, &drv->capa, sizeof(*capa));
8301	if (drv->extended_capa && drv->extended_capa_mask) {
8302		capa->extended_capa = drv->extended_capa;
8303		capa->extended_capa_mask = drv->extended_capa_mask;
8304		capa->extended_capa_len = drv->extended_capa_len;
8305	}
8306
8307	if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
8308	    !drv->allow_p2p_device) {
8309		wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
8310		capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
8311	}
8312
8313	return 0;
8314}
8315
8316
8317static int wpa_driver_nl80211_set_operstate(void *priv, int state)
8318{
8319	struct i802_bss *bss = priv;
8320	struct wpa_driver_nl80211_data *drv = bss->drv;
8321
8322	wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
8323		   __func__, drv->operstate, state, state ? "UP" : "DORMANT");
8324	drv->operstate = state;
8325	return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
8326				      state ? IF_OPER_UP : IF_OPER_DORMANT);
8327}
8328
8329
8330static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
8331{
8332	struct i802_bss *bss = priv;
8333	struct wpa_driver_nl80211_data *drv = bss->drv;
8334	struct nl_msg *msg;
8335	struct nl80211_sta_flag_update upd;
8336
8337	wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
8338		   MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
8339
8340	msg = nlmsg_alloc();
8341	if (!msg)
8342		return -ENOMEM;
8343
8344	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
8345
8346	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8347		    if_nametoindex(bss->ifname));
8348	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
8349
8350	os_memset(&upd, 0, sizeof(upd));
8351	upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
8352	if (authorized)
8353		upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
8354	NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8355
8356	return send_and_recv_msgs(drv, msg, NULL, NULL);
8357 nla_put_failure:
8358	nlmsg_free(msg);
8359	return -ENOBUFS;
8360}
8361
8362
8363/* Set kernel driver on given frequency (MHz) */
8364static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
8365{
8366	struct i802_bss *bss = priv;
8367	return wpa_driver_nl80211_set_freq(bss, freq);
8368}
8369
8370
8371#if defined(HOSTAPD) || defined(CONFIG_AP)
8372
8373static inline int min_int(int a, int b)
8374{
8375	if (a < b)
8376		return a;
8377	return b;
8378}
8379
8380
8381static int get_key_handler(struct nl_msg *msg, void *arg)
8382{
8383	struct nlattr *tb[NL80211_ATTR_MAX + 1];
8384	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8385
8386	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8387		  genlmsg_attrlen(gnlh, 0), NULL);
8388
8389	/*
8390	 * TODO: validate the key index and mac address!
8391	 * Otherwise, there's a race condition as soon as
8392	 * the kernel starts sending key notifications.
8393	 */
8394
8395	if (tb[NL80211_ATTR_KEY_SEQ])
8396		memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
8397		       min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
8398	return NL_SKIP;
8399}
8400
8401
8402static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
8403			   int idx, u8 *seq)
8404{
8405	struct i802_bss *bss = priv;
8406	struct wpa_driver_nl80211_data *drv = bss->drv;
8407	struct nl_msg *msg;
8408
8409	msg = nlmsg_alloc();
8410	if (!msg)
8411		return -ENOMEM;
8412
8413	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
8414
8415	if (addr)
8416		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8417	NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
8418	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
8419
8420	memset(seq, 0, 6);
8421
8422	return send_and_recv_msgs(drv, msg, get_key_handler, seq);
8423 nla_put_failure:
8424	nlmsg_free(msg);
8425	return -ENOBUFS;
8426}
8427
8428
8429static int i802_set_rts(void *priv, int rts)
8430{
8431	struct i802_bss *bss = priv;
8432	struct wpa_driver_nl80211_data *drv = bss->drv;
8433	struct nl_msg *msg;
8434	int ret = -ENOBUFS;
8435	u32 val;
8436
8437	msg = nlmsg_alloc();
8438	if (!msg)
8439		return -ENOMEM;
8440
8441	if (rts >= 2347)
8442		val = (u32) -1;
8443	else
8444		val = rts;
8445
8446	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
8447	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8448	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
8449
8450	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8451	msg = NULL;
8452	if (!ret)
8453		return 0;
8454nla_put_failure:
8455	nlmsg_free(msg);
8456	wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
8457		   "%d (%s)", rts, ret, strerror(-ret));
8458	return ret;
8459}
8460
8461
8462static int i802_set_frag(void *priv, int frag)
8463{
8464	struct i802_bss *bss = priv;
8465	struct wpa_driver_nl80211_data *drv = bss->drv;
8466	struct nl_msg *msg;
8467	int ret = -ENOBUFS;
8468	u32 val;
8469
8470	msg = nlmsg_alloc();
8471	if (!msg)
8472		return -ENOMEM;
8473
8474	if (frag >= 2346)
8475		val = (u32) -1;
8476	else
8477		val = frag;
8478
8479	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
8480	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8481	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
8482
8483	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8484	msg = NULL;
8485	if (!ret)
8486		return 0;
8487nla_put_failure:
8488	nlmsg_free(msg);
8489	wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
8490		   "%d: %d (%s)", frag, ret, strerror(-ret));
8491	return ret;
8492}
8493
8494
8495static int i802_flush(void *priv)
8496{
8497	struct i802_bss *bss = priv;
8498	struct wpa_driver_nl80211_data *drv = bss->drv;
8499	struct nl_msg *msg;
8500	int res;
8501
8502	msg = nlmsg_alloc();
8503	if (!msg)
8504		return -1;
8505
8506	wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
8507		   bss->ifname);
8508	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
8509
8510	/*
8511	 * XXX: FIX! this needs to flush all VLANs too
8512	 */
8513	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8514		    if_nametoindex(bss->ifname));
8515
8516	res = send_and_recv_msgs(drv, msg, NULL, NULL);
8517	if (res) {
8518		wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
8519			   "(%s)", res, strerror(-res));
8520	}
8521	return res;
8522 nla_put_failure:
8523	nlmsg_free(msg);
8524	return -ENOBUFS;
8525}
8526
8527#endif /* HOSTAPD || CONFIG_AP */
8528
8529
8530static int get_sta_handler(struct nl_msg *msg, void *arg)
8531{
8532	struct nlattr *tb[NL80211_ATTR_MAX + 1];
8533	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8534	struct hostap_sta_driver_data *data = arg;
8535	struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
8536	static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
8537		[NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
8538		[NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
8539		[NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
8540		[NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
8541		[NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
8542		[NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
8543	};
8544
8545	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8546		  genlmsg_attrlen(gnlh, 0), NULL);
8547
8548	/*
8549	 * TODO: validate the interface and mac address!
8550	 * Otherwise, there's a race condition as soon as
8551	 * the kernel starts sending station notifications.
8552	 */
8553
8554	if (!tb[NL80211_ATTR_STA_INFO]) {
8555		wpa_printf(MSG_DEBUG, "sta stats missing!");
8556		return NL_SKIP;
8557	}
8558	if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
8559			     tb[NL80211_ATTR_STA_INFO],
8560			     stats_policy)) {
8561		wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
8562		return NL_SKIP;
8563	}
8564
8565	if (stats[NL80211_STA_INFO_INACTIVE_TIME])
8566		data->inactive_msec =
8567			nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
8568	if (stats[NL80211_STA_INFO_RX_BYTES])
8569		data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
8570	if (stats[NL80211_STA_INFO_TX_BYTES])
8571		data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
8572	if (stats[NL80211_STA_INFO_RX_PACKETS])
8573		data->rx_packets =
8574			nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
8575	if (stats[NL80211_STA_INFO_TX_PACKETS])
8576		data->tx_packets =
8577			nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
8578	if (stats[NL80211_STA_INFO_TX_FAILED])
8579		data->tx_retry_failed =
8580			nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
8581
8582	return NL_SKIP;
8583}
8584
8585static int i802_read_sta_data(struct i802_bss *bss,
8586			      struct hostap_sta_driver_data *data,
8587			      const u8 *addr)
8588{
8589	struct wpa_driver_nl80211_data *drv = bss->drv;
8590	struct nl_msg *msg;
8591
8592	os_memset(data, 0, sizeof(*data));
8593	msg = nlmsg_alloc();
8594	if (!msg)
8595		return -ENOMEM;
8596
8597	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
8598
8599	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8600	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8601
8602	return send_and_recv_msgs(drv, msg, get_sta_handler, data);
8603 nla_put_failure:
8604	nlmsg_free(msg);
8605	return -ENOBUFS;
8606}
8607
8608
8609#if defined(HOSTAPD) || defined(CONFIG_AP)
8610
8611static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
8612				    int cw_min, int cw_max, int burst_time)
8613{
8614	struct i802_bss *bss = priv;
8615	struct wpa_driver_nl80211_data *drv = bss->drv;
8616	struct nl_msg *msg;
8617	struct nlattr *txq, *params;
8618
8619	msg = nlmsg_alloc();
8620	if (!msg)
8621		return -1;
8622
8623	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
8624
8625	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8626
8627	txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
8628	if (!txq)
8629		goto nla_put_failure;
8630
8631	/* We are only sending parameters for a single TXQ at a time */
8632	params = nla_nest_start(msg, 1);
8633	if (!params)
8634		goto nla_put_failure;
8635
8636	switch (queue) {
8637	case 0:
8638		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
8639		break;
8640	case 1:
8641		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
8642		break;
8643	case 2:
8644		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
8645		break;
8646	case 3:
8647		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
8648		break;
8649	}
8650	/* Burst time is configured in units of 0.1 msec and TXOP parameter in
8651	 * 32 usec, so need to convert the value here. */
8652	NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
8653	NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
8654	NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
8655	NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
8656
8657	nla_nest_end(msg, params);
8658
8659	nla_nest_end(msg, txq);
8660
8661	if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8662		return 0;
8663	msg = NULL;
8664 nla_put_failure:
8665	nlmsg_free(msg);
8666	return -1;
8667}
8668
8669
8670static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
8671			     const char *ifname, int vlan_id)
8672{
8673	struct wpa_driver_nl80211_data *drv = bss->drv;
8674	struct nl_msg *msg;
8675	int ret = -ENOBUFS;
8676
8677	msg = nlmsg_alloc();
8678	if (!msg)
8679		return -ENOMEM;
8680
8681	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
8682
8683	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8684		    if_nametoindex(bss->ifname));
8685	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8686	NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
8687		    if_nametoindex(ifname));
8688
8689	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8690	msg = NULL;
8691	if (ret < 0) {
8692		wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8693			   MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8694			   MAC2STR(addr), ifname, vlan_id, ret,
8695			   strerror(-ret));
8696	}
8697 nla_put_failure:
8698	nlmsg_free(msg);
8699	return ret;
8700}
8701
8702
8703static int i802_get_inact_sec(void *priv, const u8 *addr)
8704{
8705	struct hostap_sta_driver_data data;
8706	int ret;
8707
8708	data.inactive_msec = (unsigned long) -1;
8709	ret = i802_read_sta_data(priv, &data, addr);
8710	if (ret || data.inactive_msec == (unsigned long) -1)
8711		return -1;
8712	return data.inactive_msec / 1000;
8713}
8714
8715
8716static int i802_sta_clear_stats(void *priv, const u8 *addr)
8717{
8718#if 0
8719	/* TODO */
8720#endif
8721	return 0;
8722}
8723
8724
8725static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
8726			   int reason)
8727{
8728	struct i802_bss *bss = priv;
8729	struct wpa_driver_nl80211_data *drv = bss->drv;
8730	struct ieee80211_mgmt mgmt;
8731
8732	if (drv->device_ap_sme)
8733		return wpa_driver_nl80211_sta_remove(bss, addr);
8734
8735	memset(&mgmt, 0, sizeof(mgmt));
8736	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8737					  WLAN_FC_STYPE_DEAUTH);
8738	memcpy(mgmt.da, addr, ETH_ALEN);
8739	memcpy(mgmt.sa, own_addr, ETH_ALEN);
8740	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8741	mgmt.u.deauth.reason_code = host_to_le16(reason);
8742	return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8743					    IEEE80211_HDRLEN +
8744					    sizeof(mgmt.u.deauth), 0, 0, 0, 0,
8745					    0);
8746}
8747
8748
8749static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
8750			     int reason)
8751{
8752	struct i802_bss *bss = priv;
8753	struct wpa_driver_nl80211_data *drv = bss->drv;
8754	struct ieee80211_mgmt mgmt;
8755
8756	if (drv->device_ap_sme)
8757		return wpa_driver_nl80211_sta_remove(bss, addr);
8758
8759	memset(&mgmt, 0, sizeof(mgmt));
8760	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8761					  WLAN_FC_STYPE_DISASSOC);
8762	memcpy(mgmt.da, addr, ETH_ALEN);
8763	memcpy(mgmt.sa, own_addr, ETH_ALEN);
8764	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8765	mgmt.u.disassoc.reason_code = host_to_le16(reason);
8766	return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8767					    IEEE80211_HDRLEN +
8768					    sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
8769					    0);
8770}
8771
8772#endif /* HOSTAPD || CONFIG_AP */
8773
8774#ifdef HOSTAPD
8775
8776static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8777{
8778	int i;
8779	int *old;
8780
8781	wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
8782		   ifidx);
8783	for (i = 0; i < drv->num_if_indices; i++) {
8784		if (drv->if_indices[i] == 0) {
8785			drv->if_indices[i] = ifidx;
8786			return;
8787		}
8788	}
8789
8790	if (drv->if_indices != drv->default_if_indices)
8791		old = drv->if_indices;
8792	else
8793		old = NULL;
8794
8795	drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
8796					   sizeof(int));
8797	if (!drv->if_indices) {
8798		if (!old)
8799			drv->if_indices = drv->default_if_indices;
8800		else
8801			drv->if_indices = old;
8802		wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8803			   "interfaces");
8804		wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
8805		return;
8806	} else if (!old)
8807		os_memcpy(drv->if_indices, drv->default_if_indices,
8808			  sizeof(drv->default_if_indices));
8809	drv->if_indices[drv->num_if_indices] = ifidx;
8810	drv->num_if_indices++;
8811}
8812
8813
8814static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8815{
8816	int i;
8817
8818	for (i = 0; i < drv->num_if_indices; i++) {
8819		if (drv->if_indices[i] == ifidx) {
8820			drv->if_indices[i] = 0;
8821			break;
8822		}
8823	}
8824}
8825
8826
8827static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8828{
8829	int i;
8830
8831	for (i = 0; i < drv->num_if_indices; i++)
8832		if (drv->if_indices[i] == ifidx)
8833			return 1;
8834
8835	return 0;
8836}
8837
8838
8839static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
8840                            const char *bridge_ifname, char *ifname_wds)
8841{
8842	struct i802_bss *bss = priv;
8843	struct wpa_driver_nl80211_data *drv = bss->drv;
8844	char name[IFNAMSIZ + 1];
8845
8846	os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
8847	if (ifname_wds)
8848		os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
8849
8850	wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
8851		   " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
8852	if (val) {
8853		if (!if_nametoindex(name)) {
8854			if (nl80211_create_iface(drv, name,
8855						 NL80211_IFTYPE_AP_VLAN,
8856						 bss->addr, 1, NULL, NULL) < 0)
8857				return -1;
8858			if (bridge_ifname &&
8859			    linux_br_add_if(drv->global->ioctl_sock,
8860					    bridge_ifname, name) < 0)
8861				return -1;
8862		}
8863		if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
8864			wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
8865				   "interface %s up", name);
8866		}
8867		return i802_set_sta_vlan(priv, addr, name, 0);
8868	} else {
8869		if (bridge_ifname)
8870			linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
8871					name);
8872
8873		i802_set_sta_vlan(priv, addr, bss->ifname, 0);
8874		return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
8875						    name);
8876	}
8877}
8878
8879
8880static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
8881{
8882	struct wpa_driver_nl80211_data *drv = eloop_ctx;
8883	struct sockaddr_ll lladdr;
8884	unsigned char buf[3000];
8885	int len;
8886	socklen_t fromlen = sizeof(lladdr);
8887
8888	len = recvfrom(sock, buf, sizeof(buf), 0,
8889		       (struct sockaddr *)&lladdr, &fromlen);
8890	if (len < 0) {
8891		perror("recv");
8892		return;
8893	}
8894
8895	if (have_ifidx(drv, lladdr.sll_ifindex))
8896		drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
8897}
8898
8899
8900static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
8901			     struct i802_bss *bss,
8902			     const char *brname, const char *ifname)
8903{
8904	int ifindex;
8905	char in_br[IFNAMSIZ];
8906
8907	os_strlcpy(bss->brname, brname, IFNAMSIZ);
8908	ifindex = if_nametoindex(brname);
8909	if (ifindex == 0) {
8910		/*
8911		 * Bridge was configured, but the bridge device does
8912		 * not exist. Try to add it now.
8913		 */
8914		if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
8915			wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8916				   "bridge interface %s: %s",
8917				   brname, strerror(errno));
8918			return -1;
8919		}
8920		bss->added_bridge = 1;
8921		add_ifidx(drv, if_nametoindex(brname));
8922	}
8923
8924	if (linux_br_get(in_br, ifname) == 0) {
8925		if (os_strcmp(in_br, brname) == 0)
8926			return 0; /* already in the bridge */
8927
8928		wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8929			   "bridge %s", ifname, in_br);
8930		if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8931		    0) {
8932			wpa_printf(MSG_ERROR, "nl80211: Failed to "
8933				   "remove interface %s from bridge "
8934				   "%s: %s",
8935				   ifname, brname, strerror(errno));
8936			return -1;
8937		}
8938	}
8939
8940	wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8941		   ifname, brname);
8942	if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
8943		wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
8944			   "into bridge %s: %s",
8945			   ifname, brname, strerror(errno));
8946		return -1;
8947	}
8948	bss->added_if_into_bridge = 1;
8949
8950	return 0;
8951}
8952
8953
8954static void *i802_init(struct hostapd_data *hapd,
8955		       struct wpa_init_params *params)
8956{
8957	struct wpa_driver_nl80211_data *drv;
8958	struct i802_bss *bss;
8959	size_t i;
8960	char brname[IFNAMSIZ];
8961	int ifindex, br_ifindex;
8962	int br_added = 0;
8963
8964	bss = wpa_driver_nl80211_init(hapd, params->ifname,
8965				      params->global_priv);
8966	if (bss == NULL)
8967		return NULL;
8968
8969	drv = bss->drv;
8970	drv->nlmode = NL80211_IFTYPE_AP;
8971	drv->eapol_sock = -1;
8972
8973	if (linux_br_get(brname, params->ifname) == 0) {
8974		wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
8975			   params->ifname, brname);
8976		br_ifindex = if_nametoindex(brname);
8977	} else {
8978		brname[0] = '\0';
8979		br_ifindex = 0;
8980	}
8981
8982	drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
8983	drv->if_indices = drv->default_if_indices;
8984	for (i = 0; i < params->num_bridge; i++) {
8985		if (params->bridge[i]) {
8986			ifindex = if_nametoindex(params->bridge[i]);
8987			if (ifindex)
8988				add_ifidx(drv, ifindex);
8989			if (ifindex == br_ifindex)
8990				br_added = 1;
8991		}
8992	}
8993	if (!br_added && br_ifindex &&
8994	    (params->num_bridge == 0 || !params->bridge[0]))
8995		add_ifidx(drv, br_ifindex);
8996
8997	/* start listening for EAPOL on the default AP interface */
8998	add_ifidx(drv, drv->ifindex);
8999
9000	if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
9001		goto failed;
9002
9003	if (params->bssid) {
9004		if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9005				       params->bssid))
9006			goto failed;
9007	}
9008
9009	if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
9010		wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
9011			   "into AP mode", bss->ifname);
9012		goto failed;
9013	}
9014
9015	if (params->num_bridge && params->bridge[0] &&
9016	    i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
9017		goto failed;
9018
9019	if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
9020		goto failed;
9021
9022	drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9023	if (drv->eapol_sock < 0) {
9024		perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
9025		goto failed;
9026	}
9027
9028	if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9029	{
9030		printf("Could not register read socket for eapol\n");
9031		goto failed;
9032	}
9033
9034	if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9035			       params->own_addr))
9036		goto failed;
9037
9038	memcpy(bss->addr, params->own_addr, ETH_ALEN);
9039
9040	return bss;
9041
9042failed:
9043	wpa_driver_nl80211_deinit(bss);
9044	return NULL;
9045}
9046
9047
9048static void i802_deinit(void *priv)
9049{
9050	struct i802_bss *bss = priv;
9051	wpa_driver_nl80211_deinit(bss);
9052}
9053
9054#endif /* HOSTAPD */
9055
9056
9057static enum nl80211_iftype wpa_driver_nl80211_if_type(
9058	enum wpa_driver_if_type type)
9059{
9060	switch (type) {
9061	case WPA_IF_STATION:
9062		return NL80211_IFTYPE_STATION;
9063	case WPA_IF_P2P_CLIENT:
9064	case WPA_IF_P2P_GROUP:
9065		return NL80211_IFTYPE_P2P_CLIENT;
9066	case WPA_IF_AP_VLAN:
9067		return NL80211_IFTYPE_AP_VLAN;
9068	case WPA_IF_AP_BSS:
9069		return NL80211_IFTYPE_AP;
9070	case WPA_IF_P2P_GO:
9071		return NL80211_IFTYPE_P2P_GO;
9072	case WPA_IF_P2P_DEVICE:
9073		return NL80211_IFTYPE_P2P_DEVICE;
9074	}
9075	return -1;
9076}
9077
9078
9079#ifdef CONFIG_P2P
9080
9081static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9082{
9083	struct wpa_driver_nl80211_data *drv;
9084	dl_list_for_each(drv, &global->interfaces,
9085			 struct wpa_driver_nl80211_data, list) {
9086		if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
9087			return 1;
9088	}
9089	return 0;
9090}
9091
9092
9093static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9094				      u8 *new_addr)
9095{
9096	unsigned int idx;
9097
9098	if (!drv->global)
9099		return -1;
9100
9101	os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
9102	for (idx = 0; idx < 64; idx++) {
9103		new_addr[0] = drv->first_bss.addr[0] | 0x02;
9104		new_addr[0] ^= idx << 2;
9105		if (!nl80211_addr_in_use(drv->global, new_addr))
9106			break;
9107	}
9108	if (idx == 64)
9109		return -1;
9110
9111	wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
9112		   MACSTR, MAC2STR(new_addr));
9113
9114	return 0;
9115}
9116
9117#endif /* CONFIG_P2P */
9118
9119
9120struct wdev_info {
9121	u64 wdev_id;
9122	int wdev_id_set;
9123	u8 macaddr[ETH_ALEN];
9124};
9125
9126static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
9127{
9128	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9129	struct nlattr *tb[NL80211_ATTR_MAX + 1];
9130	struct wdev_info *wi = arg;
9131
9132	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9133		  genlmsg_attrlen(gnlh, 0), NULL);
9134	if (tb[NL80211_ATTR_WDEV]) {
9135		wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9136		wi->wdev_id_set = 1;
9137	}
9138
9139	if (tb[NL80211_ATTR_MAC])
9140		os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
9141			  ETH_ALEN);
9142
9143	return NL_SKIP;
9144}
9145
9146
9147static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
9148				     const char *ifname, const u8 *addr,
9149				     void *bss_ctx, void **drv_priv,
9150				     char *force_ifname, u8 *if_addr,
9151				     const char *bridge)
9152{
9153	enum nl80211_iftype nlmode;
9154	struct i802_bss *bss = priv;
9155	struct wpa_driver_nl80211_data *drv = bss->drv;
9156	int ifidx;
9157#ifdef HOSTAPD
9158	struct i802_bss *new_bss = NULL;
9159
9160	if (type == WPA_IF_AP_BSS) {
9161		new_bss = os_zalloc(sizeof(*new_bss));
9162		if (new_bss == NULL)
9163			return -1;
9164	}
9165#endif /* HOSTAPD */
9166
9167	if (addr)
9168		os_memcpy(if_addr, addr, ETH_ALEN);
9169	nlmode = wpa_driver_nl80211_if_type(type);
9170	if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
9171		struct wdev_info p2pdev_info;
9172
9173		os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
9174		ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
9175					     0, nl80211_wdev_handler,
9176					     &p2pdev_info);
9177		if (!p2pdev_info.wdev_id_set || ifidx != 0) {
9178			wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
9179				   ifname);
9180			return -1;
9181		}
9182
9183		drv->global->if_add_wdevid = p2pdev_info.wdev_id;
9184		drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
9185		if (!is_zero_ether_addr(p2pdev_info.macaddr))
9186			os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
9187		wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
9188			   ifname,
9189			   (long long unsigned int) p2pdev_info.wdev_id);
9190	} else {
9191		ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
9192					     0, NULL, NULL);
9193		if (ifidx < 0) {
9194#ifdef HOSTAPD
9195			os_free(new_bss);
9196#endif /* HOSTAPD */
9197			return -1;
9198		}
9199	}
9200
9201	if (!addr) {
9202		if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
9203			os_memcpy(if_addr, bss->addr, ETH_ALEN);
9204		else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
9205					    bss->ifname, if_addr) < 0) {
9206			nl80211_remove_iface(drv, ifidx);
9207			return -1;
9208		}
9209	}
9210
9211#ifdef CONFIG_P2P
9212	if (!addr &&
9213	    (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
9214	     type == WPA_IF_P2P_GO)) {
9215		/* Enforce unique P2P Interface Address */
9216		u8 new_addr[ETH_ALEN];
9217
9218		if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
9219				       new_addr) < 0) {
9220			nl80211_remove_iface(drv, ifidx);
9221			return -1;
9222		}
9223		if (nl80211_addr_in_use(drv->global, new_addr)) {
9224			wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
9225				   "for P2P group interface");
9226			if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
9227				nl80211_remove_iface(drv, ifidx);
9228				return -1;
9229			}
9230			if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
9231					       new_addr) < 0) {
9232				nl80211_remove_iface(drv, ifidx);
9233				return -1;
9234			}
9235		}
9236		os_memcpy(if_addr, new_addr, ETH_ALEN);
9237	}
9238#endif /* CONFIG_P2P */
9239
9240#ifdef HOSTAPD
9241	if (bridge &&
9242	    i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
9243		wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
9244			   "interface %s to a bridge %s", ifname, bridge);
9245		nl80211_remove_iface(drv, ifidx);
9246		os_free(new_bss);
9247		return -1;
9248	}
9249
9250	if (type == WPA_IF_AP_BSS) {
9251		if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
9252		{
9253			nl80211_remove_iface(drv, ifidx);
9254			os_free(new_bss);
9255			return -1;
9256		}
9257		os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
9258		os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
9259		new_bss->ifindex = ifidx;
9260		new_bss->drv = drv;
9261		new_bss->next = drv->first_bss.next;
9262		new_bss->freq = drv->first_bss.freq;
9263		new_bss->ctx = bss_ctx;
9264		drv->first_bss.next = new_bss;
9265		if (drv_priv)
9266			*drv_priv = new_bss;
9267		nl80211_init_bss(new_bss);
9268
9269		/* Subscribe management frames for this WPA_IF_AP_BSS */
9270		if (nl80211_setup_ap(new_bss))
9271			return -1;
9272	}
9273#endif /* HOSTAPD */
9274
9275	if (drv->global)
9276		drv->global->if_add_ifindex = ifidx;
9277
9278	return 0;
9279}
9280
9281
9282static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
9283					enum wpa_driver_if_type type,
9284					const char *ifname)
9285{
9286	struct wpa_driver_nl80211_data *drv = bss->drv;
9287	int ifindex = if_nametoindex(ifname);
9288
9289	wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
9290		   __func__, type, ifname, ifindex);
9291	if (ifindex > 0)
9292		nl80211_remove_iface(drv, ifindex);
9293
9294#ifdef HOSTAPD
9295	if (type != WPA_IF_AP_BSS)
9296		return 0;
9297
9298	if (bss->added_if_into_bridge) {
9299		if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9300				    bss->ifname) < 0)
9301			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9302				   "interface %s from bridge %s: %s",
9303				   bss->ifname, bss->brname, strerror(errno));
9304	}
9305	if (bss->added_bridge) {
9306		if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
9307			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9308				   "bridge %s: %s",
9309				   bss->brname, strerror(errno));
9310	}
9311
9312	if (bss != &drv->first_bss) {
9313		struct i802_bss *tbss;
9314
9315		for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
9316			if (tbss->next == bss) {
9317				tbss->next = bss->next;
9318				/* Unsubscribe management frames */
9319				nl80211_teardown_ap(bss);
9320				nl80211_destroy_bss(bss);
9321				os_free(bss);
9322				bss = NULL;
9323				break;
9324			}
9325		}
9326		if (bss)
9327			wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9328				   "BSS %p in the list", __func__, bss);
9329	}
9330#endif /* HOSTAPD */
9331
9332	return 0;
9333}
9334
9335
9336static int cookie_handler(struct nl_msg *msg, void *arg)
9337{
9338	struct nlattr *tb[NL80211_ATTR_MAX + 1];
9339	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9340	u64 *cookie = arg;
9341	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9342		  genlmsg_attrlen(gnlh, 0), NULL);
9343	if (tb[NL80211_ATTR_COOKIE])
9344		*cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9345	return NL_SKIP;
9346}
9347
9348
9349static int nl80211_send_frame_cmd(struct i802_bss *bss,
9350				  unsigned int freq, unsigned int wait,
9351				  const u8 *buf, size_t buf_len,
9352				  u64 *cookie_out, int no_cck, int no_ack,
9353				  int offchanok)
9354{
9355	struct wpa_driver_nl80211_data *drv = bss->drv;
9356	struct nl_msg *msg;
9357	u64 cookie;
9358	int ret = -1;
9359
9360	msg = nlmsg_alloc();
9361	if (!msg)
9362		return -1;
9363
9364	wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
9365		   "no_ack=%d offchanok=%d",
9366		   freq, wait, no_cck, no_ack, offchanok);
9367	wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
9368	nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
9369
9370	if (nl80211_set_iface_id(msg, bss) < 0)
9371		goto nla_put_failure;
9372	if (freq)
9373		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9374	if (wait)
9375		NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
9376	if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
9377		NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
9378	if (no_cck)
9379		NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
9380	if (no_ack)
9381		NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
9382
9383	NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
9384
9385	cookie = 0;
9386	ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
9387	msg = NULL;
9388	if (ret) {
9389		wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
9390			   "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9391			   freq, wait);
9392		goto nla_put_failure;
9393	}
9394	wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
9395		   "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9396		   (long long unsigned int) cookie);
9397
9398	if (cookie_out)
9399		*cookie_out = no_ack ? (u64) -1 : cookie;
9400
9401nla_put_failure:
9402	nlmsg_free(msg);
9403	return ret;
9404}
9405
9406
9407static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
9408					  unsigned int freq,
9409					  unsigned int wait_time,
9410					  const u8 *dst, const u8 *src,
9411					  const u8 *bssid,
9412					  const u8 *data, size_t data_len,
9413					  int no_cck)
9414{
9415	struct wpa_driver_nl80211_data *drv = bss->drv;
9416	int ret = -1;
9417	u8 *buf;
9418	struct ieee80211_hdr *hdr;
9419
9420	wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
9421		   "freq=%u MHz wait=%d ms no_cck=%d)",
9422		   drv->ifindex, freq, wait_time, no_cck);
9423
9424	buf = os_zalloc(24 + data_len);
9425	if (buf == NULL)
9426		return ret;
9427	os_memcpy(buf + 24, data, data_len);
9428	hdr = (struct ieee80211_hdr *) buf;
9429	hdr->frame_control =
9430		IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
9431	os_memcpy(hdr->addr1, dst, ETH_ALEN);
9432	os_memcpy(hdr->addr2, src, ETH_ALEN);
9433	os_memcpy(hdr->addr3, bssid, ETH_ALEN);
9434
9435	if (is_ap_interface(drv->nlmode) &&
9436	    (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9437	     (int) freq == bss->freq || drv->device_ap_sme ||
9438	     !drv->use_monitor))
9439		ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
9440						   0, freq, no_cck, 1,
9441						   wait_time);
9442	else
9443		ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
9444					     24 + data_len,
9445					     &drv->send_action_cookie,
9446					     no_cck, 0, 1);
9447
9448	os_free(buf);
9449	return ret;
9450}
9451
9452
9453static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
9454{
9455	struct i802_bss *bss = priv;
9456	struct wpa_driver_nl80211_data *drv = bss->drv;
9457	struct nl_msg *msg;
9458	int ret;
9459
9460	msg = nlmsg_alloc();
9461	if (!msg)
9462		return;
9463
9464	wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
9465		   (long long unsigned int) drv->send_action_cookie);
9466	nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
9467
9468	if (nl80211_set_iface_id(msg, bss) < 0)
9469		goto nla_put_failure;
9470	NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
9471
9472	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9473	msg = NULL;
9474	if (ret)
9475		wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
9476			   "(%s)", ret, strerror(-ret));
9477
9478 nla_put_failure:
9479	nlmsg_free(msg);
9480}
9481
9482
9483static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
9484						unsigned int duration)
9485{
9486	struct i802_bss *bss = priv;
9487	struct wpa_driver_nl80211_data *drv = bss->drv;
9488	struct nl_msg *msg;
9489	int ret;
9490	u64 cookie;
9491
9492	msg = nlmsg_alloc();
9493	if (!msg)
9494		return -1;
9495
9496	nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
9497
9498	if (nl80211_set_iface_id(msg, bss) < 0)
9499		goto nla_put_failure;
9500
9501	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9502	NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
9503
9504	cookie = 0;
9505	ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
9506	msg = NULL;
9507	if (ret == 0) {
9508		wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
9509			   "0x%llx for freq=%u MHz duration=%u",
9510			   (long long unsigned int) cookie, freq, duration);
9511		drv->remain_on_chan_cookie = cookie;
9512		drv->pending_remain_on_chan = 1;
9513		return 0;
9514	}
9515	wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
9516		   "(freq=%d duration=%u): %d (%s)",
9517		   freq, duration, ret, strerror(-ret));
9518nla_put_failure:
9519	nlmsg_free(msg);
9520	return -1;
9521}
9522
9523
9524static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
9525{
9526	struct i802_bss *bss = priv;
9527	struct wpa_driver_nl80211_data *drv = bss->drv;
9528	struct nl_msg *msg;
9529	int ret;
9530
9531	if (!drv->pending_remain_on_chan) {
9532		wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
9533			   "to cancel");
9534		return -1;
9535	}
9536
9537	wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
9538		   "0x%llx",
9539		   (long long unsigned int) drv->remain_on_chan_cookie);
9540
9541	msg = nlmsg_alloc();
9542	if (!msg)
9543		return -1;
9544
9545	nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
9546
9547	if (nl80211_set_iface_id(msg, bss) < 0)
9548		goto nla_put_failure;
9549
9550	NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
9551
9552	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9553	msg = NULL;
9554	if (ret == 0)
9555		return 0;
9556	wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
9557		   "%d (%s)", ret, strerror(-ret));
9558nla_put_failure:
9559	nlmsg_free(msg);
9560	return -1;
9561}
9562
9563
9564static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
9565{
9566	struct wpa_driver_nl80211_data *drv = bss->drv;
9567
9568	if (!report) {
9569		if (bss->nl_preq && drv->device_ap_sme &&
9570		    is_ap_interface(drv->nlmode)) {
9571			/*
9572			 * Do not disable Probe Request reporting that was
9573			 * enabled in nl80211_setup_ap().
9574			 */
9575			wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
9576				   "Probe Request reporting nl_preq=%p while "
9577				   "in AP mode", bss->nl_preq);
9578		} else if (bss->nl_preq) {
9579			wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
9580				   "reporting nl_preq=%p", bss->nl_preq);
9581			eloop_unregister_read_sock(
9582				nl_socket_get_fd(bss->nl_preq));
9583			nl_destroy_handles(&bss->nl_preq);
9584		}
9585		return 0;
9586	}
9587
9588	if (bss->nl_preq) {
9589		wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
9590			   "already on! nl_preq=%p", bss->nl_preq);
9591		return 0;
9592	}
9593
9594	bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
9595	if (bss->nl_preq == NULL)
9596		return -1;
9597	wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
9598		   "reporting nl_preq=%p", bss->nl_preq);
9599
9600	if (nl80211_register_frame(bss, bss->nl_preq,
9601				   (WLAN_FC_TYPE_MGMT << 2) |
9602				   (WLAN_FC_STYPE_PROBE_REQ << 4),
9603				   NULL, 0) < 0)
9604		goto out_err;
9605
9606	eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
9607				 wpa_driver_nl80211_event_receive, bss->nl_cb,
9608				 bss->nl_preq);
9609
9610	return 0;
9611
9612 out_err:
9613	nl_destroy_handles(&bss->nl_preq);
9614	return -1;
9615}
9616
9617
9618static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
9619				     int ifindex, int disabled)
9620{
9621	struct nl_msg *msg;
9622	struct nlattr *bands, *band;
9623	int ret;
9624
9625	msg = nlmsg_alloc();
9626	if (!msg)
9627		return -1;
9628
9629	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
9630	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
9631
9632	bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
9633	if (!bands)
9634		goto nla_put_failure;
9635
9636	/*
9637	 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
9638	 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
9639	 * rates. All 5 GHz rates are left enabled.
9640	 */
9641	band = nla_nest_start(msg, NL80211_BAND_2GHZ);
9642	if (!band)
9643		goto nla_put_failure;
9644	if (disabled) {
9645		NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
9646			"\x0c\x12\x18\x24\x30\x48\x60\x6c");
9647	}
9648	nla_nest_end(msg, band);
9649
9650	nla_nest_end(msg, bands);
9651
9652	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9653	msg = NULL;
9654	if (ret) {
9655		wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
9656			   "(%s)", ret, strerror(-ret));
9657	} else
9658		drv->disabled_11b_rates = disabled;
9659
9660	return ret;
9661
9662nla_put_failure:
9663	nlmsg_free(msg);
9664	return -1;
9665}
9666
9667
9668static int wpa_driver_nl80211_deinit_ap(void *priv)
9669{
9670	struct i802_bss *bss = priv;
9671	struct wpa_driver_nl80211_data *drv = bss->drv;
9672	if (!is_ap_interface(drv->nlmode))
9673		return -1;
9674	wpa_driver_nl80211_del_beacon(drv);
9675
9676	/*
9677	 * If the P2P GO interface was dynamically added, then it is
9678	 * possible that the interface change to station is not possible.
9679	 */
9680	if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
9681		return 0;
9682
9683	return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9684}
9685
9686
9687static int wpa_driver_nl80211_stop_ap(void *priv)
9688{
9689	struct i802_bss *bss = priv;
9690	struct wpa_driver_nl80211_data *drv = bss->drv;
9691	if (!is_ap_interface(drv->nlmode))
9692		return -1;
9693	wpa_driver_nl80211_del_beacon(drv);
9694	bss->beacon_set = 0;
9695	return 0;
9696}
9697
9698
9699static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
9700{
9701	struct i802_bss *bss = priv;
9702	struct wpa_driver_nl80211_data *drv = bss->drv;
9703	if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
9704		return -1;
9705
9706	/*
9707	 * If the P2P Client interface was dynamically added, then it is
9708	 * possible that the interface change to station is not possible.
9709	 */
9710	if (bss->if_dynamic)
9711		return 0;
9712
9713	return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9714}
9715
9716
9717static void wpa_driver_nl80211_resume(void *priv)
9718{
9719	struct i802_bss *bss = priv;
9720
9721	if (i802_set_iface_flags(bss, 1))
9722		wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
9723}
9724
9725
9726static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
9727				  const u8 *ies, size_t ies_len)
9728{
9729	struct i802_bss *bss = priv;
9730	struct wpa_driver_nl80211_data *drv = bss->drv;
9731	int ret;
9732	u8 *data, *pos;
9733	size_t data_len;
9734	const u8 *own_addr = bss->addr;
9735
9736	if (action != 1) {
9737		wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
9738			   "action %d", action);
9739		return -1;
9740	}
9741
9742	/*
9743	 * Action frame payload:
9744	 * Category[1] = 6 (Fast BSS Transition)
9745	 * Action[1] = 1 (Fast BSS Transition Request)
9746	 * STA Address
9747	 * Target AP Address
9748	 * FT IEs
9749	 */
9750
9751	data_len = 2 + 2 * ETH_ALEN + ies_len;
9752	data = os_malloc(data_len);
9753	if (data == NULL)
9754		return -1;
9755	pos = data;
9756	*pos++ = 0x06; /* FT Action category */
9757	*pos++ = action;
9758	os_memcpy(pos, own_addr, ETH_ALEN);
9759	pos += ETH_ALEN;
9760	os_memcpy(pos, target_ap, ETH_ALEN);
9761	pos += ETH_ALEN;
9762	os_memcpy(pos, ies, ies_len);
9763
9764	ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
9765					     drv->bssid, own_addr, drv->bssid,
9766					     data, data_len, 0);
9767	os_free(data);
9768
9769	return ret;
9770}
9771
9772
9773static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9774{
9775	struct i802_bss *bss = priv;
9776	struct wpa_driver_nl80211_data *drv = bss->drv;
9777	struct nl_msg *msg;
9778	struct nlattr *cqm;
9779	int ret = -1;
9780
9781	wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9782		   "hysteresis=%d", threshold, hysteresis);
9783
9784	msg = nlmsg_alloc();
9785	if (!msg)
9786		return -1;
9787
9788	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
9789
9790	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9791
9792	cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
9793	if (cqm == NULL)
9794		goto nla_put_failure;
9795
9796	NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
9797	NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
9798	nla_nest_end(msg, cqm);
9799
9800	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9801	msg = NULL;
9802
9803nla_put_failure:
9804	nlmsg_free(msg);
9805	return ret;
9806}
9807
9808
9809/* Converts nl80211_chan_width to a common format */
9810static enum chan_width convert2width(int width)
9811{
9812	switch (width) {
9813	case NL80211_CHAN_WIDTH_20_NOHT:
9814		return CHAN_WIDTH_20_NOHT;
9815	case NL80211_CHAN_WIDTH_20:
9816		return CHAN_WIDTH_20;
9817	case NL80211_CHAN_WIDTH_40:
9818		return CHAN_WIDTH_40;
9819	case NL80211_CHAN_WIDTH_80:
9820		return CHAN_WIDTH_80;
9821	case NL80211_CHAN_WIDTH_80P80:
9822		return CHAN_WIDTH_80P80;
9823	case NL80211_CHAN_WIDTH_160:
9824		return CHAN_WIDTH_160;
9825	}
9826	return CHAN_WIDTH_UNKNOWN;
9827}
9828
9829
9830static int get_channel_width(struct nl_msg *msg, void *arg)
9831{
9832	struct nlattr *tb[NL80211_ATTR_MAX + 1];
9833	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9834	struct wpa_signal_info *sig_change = arg;
9835
9836	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9837		  genlmsg_attrlen(gnlh, 0), NULL);
9838
9839	sig_change->center_frq1 = -1;
9840	sig_change->center_frq2 = -1;
9841	sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
9842
9843	if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
9844		sig_change->chanwidth = convert2width(
9845			nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
9846		if (tb[NL80211_ATTR_CENTER_FREQ1])
9847			sig_change->center_frq1 =
9848				nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
9849		if (tb[NL80211_ATTR_CENTER_FREQ2])
9850			sig_change->center_frq2 =
9851				nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
9852	}
9853
9854	return NL_SKIP;
9855}
9856
9857
9858static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
9859				     struct wpa_signal_info *sig)
9860{
9861	struct nl_msg *msg;
9862
9863	msg = nlmsg_alloc();
9864	if (!msg)
9865		return -ENOMEM;
9866
9867	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
9868	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9869
9870	return send_and_recv_msgs(drv, msg, get_channel_width, sig);
9871
9872nla_put_failure:
9873	nlmsg_free(msg);
9874	return -ENOBUFS;
9875}
9876
9877
9878static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
9879{
9880	struct i802_bss *bss = priv;
9881	struct wpa_driver_nl80211_data *drv = bss->drv;
9882	int res;
9883
9884	os_memset(si, 0, sizeof(*si));
9885	res = nl80211_get_link_signal(drv, si);
9886	if (res != 0)
9887		return res;
9888
9889	res = nl80211_get_channel_width(drv, si);
9890	if (res != 0)
9891		return res;
9892
9893	return nl80211_get_link_noise(drv, si);
9894}
9895
9896
9897static int wpa_driver_nl80211_shared_freq(void *priv)
9898{
9899	struct i802_bss *bss = priv;
9900	struct wpa_driver_nl80211_data *drv = bss->drv;
9901	struct wpa_driver_nl80211_data *driver;
9902	int freq = 0;
9903
9904	/*
9905	 * If the same PHY is in connected state with some other interface,
9906	 * then retrieve the assoc freq.
9907	 */
9908	wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
9909		   drv->phyname);
9910
9911	dl_list_for_each(driver, &drv->global->interfaces,
9912			 struct wpa_driver_nl80211_data, list) {
9913		if (drv == driver ||
9914		    os_strcmp(drv->phyname, driver->phyname) != 0 ||
9915#ifdef ANDROID_P2P
9916		    (!driver->associated && !is_ap_interface(driver->nlmode)))
9917#else
9918		    !driver->associated)
9919#endif
9920			continue;
9921
9922		wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
9923			   MACSTR,
9924			   driver->phyname, driver->first_bss.ifname,
9925			   MAC2STR(driver->first_bss.addr));
9926		if (is_ap_interface(driver->nlmode))
9927			freq = driver->first_bss.freq;
9928		else
9929			freq = nl80211_get_assoc_freq(driver);
9930		wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
9931			   drv->phyname, freq);
9932	}
9933
9934	if (!freq)
9935		wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
9936			   "PHY (%s) in associated state", drv->phyname);
9937
9938	return freq;
9939}
9940
9941
9942static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
9943			      int encrypt)
9944{
9945	struct i802_bss *bss = priv;
9946	return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
9947					     0, 0, 0, 0);
9948}
9949
9950
9951static int nl80211_set_param(void *priv, const char *param)
9952{
9953	wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
9954	if (param == NULL)
9955		return 0;
9956
9957#ifdef CONFIG_P2P
9958	if (os_strstr(param, "use_p2p_group_interface=1")) {
9959		struct i802_bss *bss = priv;
9960		struct wpa_driver_nl80211_data *drv = bss->drv;
9961
9962		wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
9963			   "interface");
9964		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
9965		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
9966	}
9967
9968	if (os_strstr(param, "p2p_device=1")) {
9969		struct i802_bss *bss = priv;
9970		struct wpa_driver_nl80211_data *drv = bss->drv;
9971		drv->allow_p2p_device = 1;
9972	}
9973
9974#ifdef ANDROID_P2P
9975	if(os_strstr(param, "use_multi_chan_concurrent=1")) {
9976		struct i802_bss *bss = priv;
9977		struct wpa_driver_nl80211_data *drv = bss->drv;
9978		wpa_printf(MSG_DEBUG, "nl80211: Use Multi channel "
9979			   "concurrency");
9980		drv->capa.num_multichan_concurrent = 2;
9981	}
9982#endif
9983#endif /* CONFIG_P2P */
9984
9985	return 0;
9986}
9987
9988
9989static void * nl80211_global_init(void)
9990{
9991	struct nl80211_global *global;
9992	struct netlink_config *cfg;
9993
9994	global = os_zalloc(sizeof(*global));
9995	if (global == NULL)
9996		return NULL;
9997	global->ioctl_sock = -1;
9998	dl_list_init(&global->interfaces);
9999	global->if_add_ifindex = -1;
10000
10001	cfg = os_zalloc(sizeof(*cfg));
10002	if (cfg == NULL)
10003		goto err;
10004
10005	cfg->ctx = global;
10006	cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10007	cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10008	global->netlink = netlink_init(cfg);
10009	if (global->netlink == NULL) {
10010		os_free(cfg);
10011		goto err;
10012	}
10013
10014	if (wpa_driver_nl80211_init_nl_global(global) < 0)
10015		goto err;
10016
10017	global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10018	if (global->ioctl_sock < 0) {
10019		perror("socket(PF_INET,SOCK_DGRAM)");
10020		goto err;
10021	}
10022
10023	return global;
10024
10025err:
10026	nl80211_global_deinit(global);
10027	return NULL;
10028}
10029
10030
10031static void nl80211_global_deinit(void *priv)
10032{
10033	struct nl80211_global *global = priv;
10034	if (global == NULL)
10035		return;
10036	if (!dl_list_empty(&global->interfaces)) {
10037		wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10038			   "nl80211_global_deinit",
10039			   dl_list_len(&global->interfaces));
10040	}
10041
10042	if (global->netlink)
10043		netlink_deinit(global->netlink);
10044
10045	nl_destroy_handles(&global->nl);
10046
10047	if (global->nl_event) {
10048		eloop_unregister_read_sock(
10049			nl_socket_get_fd(global->nl_event));
10050		nl_destroy_handles(&global->nl_event);
10051	}
10052
10053	nl_cb_put(global->nl_cb);
10054
10055	if (global->ioctl_sock >= 0)
10056		close(global->ioctl_sock);
10057
10058	os_free(global);
10059}
10060
10061
10062static const char * nl80211_get_radio_name(void *priv)
10063{
10064	struct i802_bss *bss = priv;
10065	struct wpa_driver_nl80211_data *drv = bss->drv;
10066	return drv->phyname;
10067}
10068
10069
10070static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10071			 const u8 *pmkid)
10072{
10073	struct nl_msg *msg;
10074
10075	msg = nlmsg_alloc();
10076	if (!msg)
10077		return -ENOMEM;
10078
10079	nl80211_cmd(bss->drv, msg, 0, cmd);
10080
10081	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10082	if (pmkid)
10083		NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
10084	if (bssid)
10085		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
10086
10087	return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10088 nla_put_failure:
10089	nlmsg_free(msg);
10090	return -ENOBUFS;
10091}
10092
10093
10094static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10095{
10096	struct i802_bss *bss = priv;
10097	wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
10098	return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
10099}
10100
10101
10102static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10103{
10104	struct i802_bss *bss = priv;
10105	wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10106		   MAC2STR(bssid));
10107	return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
10108}
10109
10110
10111static int nl80211_flush_pmkid(void *priv)
10112{
10113	struct i802_bss *bss = priv;
10114	wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
10115	return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
10116}
10117
10118
10119static void clean_survey_results(struct survey_results *survey_results)
10120{
10121	struct freq_survey *survey, *tmp;
10122
10123	if (dl_list_empty(&survey_results->survey_list))
10124		return;
10125
10126	dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10127			      struct freq_survey, list) {
10128		dl_list_del(&survey->list);
10129		os_free(survey);
10130	}
10131}
10132
10133
10134static void add_survey(struct nlattr **sinfo, u32 ifidx,
10135		       struct dl_list *survey_list)
10136{
10137	struct freq_survey *survey;
10138
10139	survey = os_zalloc(sizeof(struct freq_survey));
10140	if  (!survey)
10141		return;
10142
10143	survey->ifidx = ifidx;
10144	survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10145	survey->filled = 0;
10146
10147	if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10148		survey->nf = (int8_t)
10149			nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10150		survey->filled |= SURVEY_HAS_NF;
10151	}
10152
10153	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10154		survey->channel_time =
10155			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10156		survey->filled |= SURVEY_HAS_CHAN_TIME;
10157	}
10158
10159	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10160		survey->channel_time_busy =
10161			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10162		survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10163	}
10164
10165	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10166		survey->channel_time_rx =
10167			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10168		survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10169	}
10170
10171	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10172		survey->channel_time_tx =
10173			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10174		survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10175	}
10176
10177	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)",
10178		   survey->freq,
10179		   survey->nf,
10180		   (unsigned long int) survey->channel_time,
10181		   (unsigned long int) survey->channel_time_busy,
10182		   (unsigned long int) survey->channel_time_tx,
10183		   (unsigned long int) survey->channel_time_rx,
10184		   survey->filled);
10185
10186	dl_list_add_tail(survey_list, &survey->list);
10187}
10188
10189
10190static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10191			   unsigned int freq_filter)
10192{
10193	if (!freq_filter)
10194		return 1;
10195
10196	return freq_filter == surveyed_freq;
10197}
10198
10199
10200static int survey_handler(struct nl_msg *msg, void *arg)
10201{
10202	struct nlattr *tb[NL80211_ATTR_MAX + 1];
10203	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10204	struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10205	struct survey_results *survey_results;
10206	u32 surveyed_freq = 0;
10207	u32 ifidx;
10208
10209	static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10210		[NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10211		[NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10212	};
10213
10214	survey_results = (struct survey_results *) arg;
10215
10216	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10217		  genlmsg_attrlen(gnlh, 0), NULL);
10218
10219	ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10220
10221	if (!tb[NL80211_ATTR_SURVEY_INFO])
10222		return NL_SKIP;
10223
10224	if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10225			     tb[NL80211_ATTR_SURVEY_INFO],
10226			     survey_policy))
10227		return NL_SKIP;
10228
10229	if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10230		wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10231		return NL_SKIP;
10232	}
10233
10234	surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10235
10236	if (!check_survey_ok(sinfo, surveyed_freq,
10237			     survey_results->freq_filter))
10238		return NL_SKIP;
10239
10240	if (survey_results->freq_filter &&
10241	    survey_results->freq_filter != surveyed_freq) {
10242		wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10243			   surveyed_freq);
10244		return NL_SKIP;
10245	}
10246
10247	add_survey(sinfo, ifidx, &survey_results->survey_list);
10248
10249	return NL_SKIP;
10250}
10251
10252
10253static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10254{
10255	struct i802_bss *bss = priv;
10256	struct wpa_driver_nl80211_data *drv = bss->drv;
10257	struct nl_msg *msg;
10258	int err = -ENOBUFS;
10259	union wpa_event_data data;
10260	struct survey_results *survey_results;
10261
10262	os_memset(&data, 0, sizeof(data));
10263	survey_results = &data.survey_results;
10264
10265	dl_list_init(&survey_results->survey_list);
10266
10267	msg = nlmsg_alloc();
10268	if (!msg)
10269		goto nla_put_failure;
10270
10271	nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
10272
10273	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10274
10275	if (freq)
10276		data.survey_results.freq_filter = freq;
10277
10278	do {
10279		wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
10280		err = send_and_recv_msgs(drv, msg, survey_handler,
10281					 survey_results);
10282	} while (err > 0);
10283
10284	if (err) {
10285		wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
10286		goto out_clean;
10287	}
10288
10289	wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
10290
10291out_clean:
10292	clean_survey_results(survey_results);
10293nla_put_failure:
10294	return err;
10295}
10296
10297
10298static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
10299				   const u8 *replay_ctr)
10300{
10301	struct i802_bss *bss = priv;
10302	struct wpa_driver_nl80211_data *drv = bss->drv;
10303	struct nlattr *replay_nested;
10304	struct nl_msg *msg;
10305
10306	msg = nlmsg_alloc();
10307	if (!msg)
10308		return;
10309
10310	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10311
10312	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10313
10314	replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10315	if (!replay_nested)
10316		goto nla_put_failure;
10317
10318	NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
10319	NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
10320	NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
10321		replay_ctr);
10322
10323	nla_nest_end(msg, replay_nested);
10324
10325	send_and_recv_msgs(drv, msg, NULL, NULL);
10326	return;
10327 nla_put_failure:
10328	nlmsg_free(msg);
10329}
10330
10331
10332static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
10333				    const u8 *addr, int qos)
10334{
10335	/* send data frame to poll STA and check whether
10336	 * this frame is ACKed */
10337	struct {
10338		struct ieee80211_hdr hdr;
10339		u16 qos_ctl;
10340	} STRUCT_PACKED nulldata;
10341	size_t size;
10342
10343	/* Send data frame to poll STA and check whether this frame is ACKed */
10344
10345	os_memset(&nulldata, 0, sizeof(nulldata));
10346
10347	if (qos) {
10348		nulldata.hdr.frame_control =
10349			IEEE80211_FC(WLAN_FC_TYPE_DATA,
10350				     WLAN_FC_STYPE_QOS_NULL);
10351		size = sizeof(nulldata);
10352	} else {
10353		nulldata.hdr.frame_control =
10354			IEEE80211_FC(WLAN_FC_TYPE_DATA,
10355				     WLAN_FC_STYPE_NULLFUNC);
10356		size = sizeof(struct ieee80211_hdr);
10357	}
10358
10359	nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
10360	os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
10361	os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
10362	os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
10363
10364	if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
10365					 0, 0) < 0)
10366		wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
10367			   "send poll frame");
10368}
10369
10370static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
10371				int qos)
10372{
10373	struct i802_bss *bss = priv;
10374	struct wpa_driver_nl80211_data *drv = bss->drv;
10375	struct nl_msg *msg;
10376
10377	if (!drv->poll_command_supported) {
10378		nl80211_send_null_frame(bss, own_addr, addr, qos);
10379		return;
10380	}
10381
10382	msg = nlmsg_alloc();
10383	if (!msg)
10384		return;
10385
10386	nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
10387
10388	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10389	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
10390
10391	send_and_recv_msgs(drv, msg, NULL, NULL);
10392	return;
10393 nla_put_failure:
10394	nlmsg_free(msg);
10395}
10396
10397
10398static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
10399{
10400	struct nl_msg *msg;
10401
10402	msg = nlmsg_alloc();
10403	if (!msg)
10404		return -ENOMEM;
10405
10406	nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
10407	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10408	NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
10409		    enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
10410	return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10411nla_put_failure:
10412	nlmsg_free(msg);
10413	return -ENOBUFS;
10414}
10415
10416
10417static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
10418				     int ctwindow)
10419{
10420	struct i802_bss *bss = priv;
10421
10422	wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
10423		   "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
10424
10425	if (opp_ps != -1 || ctwindow != -1)
10426#ifdef ANDROID_P2P
10427		wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
10428#else
10429		return -1; /* Not yet supported */
10430#endif
10431
10432	if (legacy_ps == -1)
10433		return 0;
10434	if (legacy_ps != 0 && legacy_ps != 1)
10435		return -1; /* Not yet supported */
10436
10437	return nl80211_set_power_save(bss, legacy_ps);
10438}
10439
10440
10441static int nl80211_start_radar_detection(void *priv,
10442					 struct hostapd_freq_params *freq)
10443{
10444	struct i802_bss *bss = priv;
10445	struct wpa_driver_nl80211_data *drv = bss->drv;
10446	struct nl_msg *msg;
10447	int ret;
10448
10449	wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
10450		   freq->freq, freq->ht_enabled, freq->vht_enabled,
10451		   freq->bandwidth, freq->center_freq1, freq->center_freq2);
10452
10453	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
10454		wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
10455			   "detection");
10456		return -1;
10457	}
10458
10459	msg = nlmsg_alloc();
10460	if (!msg)
10461		return -1;
10462
10463	nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
10464	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10465	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
10466
10467	if (freq->vht_enabled) {
10468		switch (freq->bandwidth) {
10469		case 20:
10470			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10471				    NL80211_CHAN_WIDTH_20);
10472			break;
10473		case 40:
10474			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10475				    NL80211_CHAN_WIDTH_40);
10476			break;
10477		case 80:
10478			if (freq->center_freq2)
10479				NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10480					    NL80211_CHAN_WIDTH_80P80);
10481			else
10482				NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10483					    NL80211_CHAN_WIDTH_80);
10484			break;
10485		case 160:
10486			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10487				    NL80211_CHAN_WIDTH_160);
10488			break;
10489		default:
10490			return -1;
10491		}
10492		NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
10493		if (freq->center_freq2)
10494			NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
10495				    freq->center_freq2);
10496	} else if (freq->ht_enabled) {
10497		switch (freq->sec_channel_offset) {
10498		case -1:
10499			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10500				    NL80211_CHAN_HT40MINUS);
10501			break;
10502		case 1:
10503			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10504				    NL80211_CHAN_HT40PLUS);
10505			break;
10506		default:
10507			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10508				    NL80211_CHAN_HT20);
10509			break;
10510		}
10511	}
10512
10513	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10514	if (ret == 0)
10515		return 0;
10516	wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
10517		   "%d (%s)", ret, strerror(-ret));
10518nla_put_failure:
10519	return -1;
10520}
10521
10522#ifdef CONFIG_TDLS
10523
10524static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
10525				  u8 dialog_token, u16 status_code,
10526				  const u8 *buf, size_t len)
10527{
10528	struct i802_bss *bss = priv;
10529	struct wpa_driver_nl80211_data *drv = bss->drv;
10530	struct nl_msg *msg;
10531
10532	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10533		return -EOPNOTSUPP;
10534
10535	if (!dst)
10536		return -EINVAL;
10537
10538	msg = nlmsg_alloc();
10539	if (!msg)
10540		return -ENOMEM;
10541
10542	nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
10543	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10544	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
10545	NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
10546	NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
10547	NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
10548	NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
10549
10550	return send_and_recv_msgs(drv, msg, NULL, NULL);
10551
10552nla_put_failure:
10553	nlmsg_free(msg);
10554	return -ENOBUFS;
10555}
10556
10557
10558static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
10559{
10560	struct i802_bss *bss = priv;
10561	struct wpa_driver_nl80211_data *drv = bss->drv;
10562	struct nl_msg *msg;
10563	enum nl80211_tdls_operation nl80211_oper;
10564
10565	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10566		return -EOPNOTSUPP;
10567
10568	switch (oper) {
10569	case TDLS_DISCOVERY_REQ:
10570		nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
10571		break;
10572	case TDLS_SETUP:
10573		nl80211_oper = NL80211_TDLS_SETUP;
10574		break;
10575	case TDLS_TEARDOWN:
10576		nl80211_oper = NL80211_TDLS_TEARDOWN;
10577		break;
10578	case TDLS_ENABLE_LINK:
10579		nl80211_oper = NL80211_TDLS_ENABLE_LINK;
10580		break;
10581	case TDLS_DISABLE_LINK:
10582		nl80211_oper = NL80211_TDLS_DISABLE_LINK;
10583		break;
10584	case TDLS_ENABLE:
10585		return 0;
10586	case TDLS_DISABLE:
10587		return 0;
10588	default:
10589		return -EINVAL;
10590	}
10591
10592	msg = nlmsg_alloc();
10593	if (!msg)
10594		return -ENOMEM;
10595
10596	nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
10597	NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
10598	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10599	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
10600
10601	return send_and_recv_msgs(drv, msg, NULL, NULL);
10602
10603nla_put_failure:
10604	nlmsg_free(msg);
10605	return -ENOBUFS;
10606}
10607
10608#endif /* CONFIG TDLS */
10609
10610
10611#ifdef ANDROID
10612
10613typedef struct android_wifi_priv_cmd {
10614	char *buf;
10615	int used_len;
10616	int total_len;
10617} android_wifi_priv_cmd;
10618
10619static int drv_errors = 0;
10620
10621static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
10622{
10623	drv_errors++;
10624	if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
10625		drv_errors = 0;
10626		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
10627	}
10628}
10629
10630
10631static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
10632{
10633	struct wpa_driver_nl80211_data *drv = bss->drv;
10634	struct ifreq ifr;
10635	android_wifi_priv_cmd priv_cmd;
10636	char buf[MAX_DRV_CMD_SIZE];
10637	int ret;
10638
10639	os_memset(&ifr, 0, sizeof(ifr));
10640	os_memset(&priv_cmd, 0, sizeof(priv_cmd));
10641	os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
10642
10643	os_memset(buf, 0, sizeof(buf));
10644	os_strlcpy(buf, cmd, sizeof(buf));
10645
10646	priv_cmd.buf = buf;
10647	priv_cmd.used_len = sizeof(buf);
10648	priv_cmd.total_len = sizeof(buf);
10649	ifr.ifr_data = &priv_cmd;
10650
10651	ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
10652	if (ret < 0) {
10653		wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
10654			   __func__);
10655		wpa_driver_send_hang_msg(drv);
10656		return ret;
10657	}
10658
10659	drv_errors = 0;
10660	return 0;
10661}
10662
10663
10664static int android_pno_start(struct i802_bss *bss,
10665			     struct wpa_driver_scan_params *params)
10666{
10667	struct wpa_driver_nl80211_data *drv = bss->drv;
10668	struct ifreq ifr;
10669	android_wifi_priv_cmd priv_cmd;
10670	int ret = 0, i = 0, bp;
10671	char buf[WEXT_PNO_MAX_COMMAND_SIZE];
10672
10673	bp = WEXT_PNOSETUP_HEADER_SIZE;
10674	os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
10675	buf[bp++] = WEXT_PNO_TLV_PREFIX;
10676	buf[bp++] = WEXT_PNO_TLV_VERSION;
10677	buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
10678	buf[bp++] = WEXT_PNO_TLV_RESERVED;
10679
10680	while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
10681		/* Check that there is enough space needed for 1 more SSID, the
10682		 * other sections and null termination */
10683		if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
10684		     WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
10685			break;
10686		wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
10687				  params->ssids[i].ssid,
10688				  params->ssids[i].ssid_len);
10689		buf[bp++] = WEXT_PNO_SSID_SECTION;
10690		buf[bp++] = params->ssids[i].ssid_len;
10691		os_memcpy(&buf[bp], params->ssids[i].ssid,
10692			  params->ssids[i].ssid_len);
10693		bp += params->ssids[i].ssid_len;
10694		i++;
10695	}
10696
10697	buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
10698	os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
10699		    WEXT_PNO_SCAN_INTERVAL);
10700	bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
10701
10702	buf[bp++] = WEXT_PNO_REPEAT_SECTION;
10703	os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
10704		    WEXT_PNO_REPEAT);
10705	bp += WEXT_PNO_REPEAT_LENGTH;
10706
10707	buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
10708	os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
10709		    WEXT_PNO_MAX_REPEAT);
10710	bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
10711
10712	memset(&ifr, 0, sizeof(ifr));
10713	memset(&priv_cmd, 0, sizeof(priv_cmd));
10714	os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
10715
10716	priv_cmd.buf = buf;
10717	priv_cmd.used_len = bp;
10718	priv_cmd.total_len = bp;
10719	ifr.ifr_data = &priv_cmd;
10720
10721	ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
10722
10723	if (ret < 0) {
10724		wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
10725			   ret);
10726		wpa_driver_send_hang_msg(drv);
10727		return ret;
10728	}
10729
10730	drv_errors = 0;
10731
10732	return android_priv_cmd(bss, "PNOFORCE 1");
10733}
10734
10735
10736static int android_pno_stop(struct i802_bss *bss)
10737{
10738	return android_priv_cmd(bss, "PNOFORCE 0");
10739}
10740
10741#endif /* ANDROID */
10742
10743
10744static int driver_nl80211_set_key(const char *ifname, void *priv,
10745				  enum wpa_alg alg, const u8 *addr,
10746				  int key_idx, int set_tx,
10747				  const u8 *seq, size_t seq_len,
10748				  const u8 *key, size_t key_len)
10749{
10750	struct i802_bss *bss = priv;
10751	return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
10752					  set_tx, seq, seq_len, key, key_len);
10753}
10754
10755
10756static int driver_nl80211_scan2(void *priv,
10757				struct wpa_driver_scan_params *params)
10758{
10759	struct i802_bss *bss = priv;
10760	return wpa_driver_nl80211_scan(bss, params);
10761}
10762
10763
10764static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
10765					 int reason_code)
10766{
10767	struct i802_bss *bss = priv;
10768	return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
10769}
10770
10771
10772static int driver_nl80211_authenticate(void *priv,
10773				       struct wpa_driver_auth_params *params)
10774{
10775	struct i802_bss *bss = priv;
10776	return wpa_driver_nl80211_authenticate(bss, params);
10777}
10778
10779
10780static void driver_nl80211_deinit(void *priv)
10781{
10782	struct i802_bss *bss = priv;
10783	wpa_driver_nl80211_deinit(bss);
10784}
10785
10786
10787static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
10788				    const char *ifname)
10789{
10790	struct i802_bss *bss = priv;
10791	return wpa_driver_nl80211_if_remove(bss, type, ifname);
10792}
10793
10794
10795static int driver_nl80211_send_mlme(void *priv, const u8 *data,
10796				    size_t data_len, int noack)
10797{
10798	struct i802_bss *bss = priv;
10799	return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
10800					    0, 0, 0, 0);
10801}
10802
10803
10804static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
10805{
10806	struct i802_bss *bss = priv;
10807	return wpa_driver_nl80211_sta_remove(bss, addr);
10808}
10809
10810
10811#if defined(HOSTAPD) || defined(CONFIG_AP)
10812static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
10813				       const char *ifname, int vlan_id)
10814{
10815	struct i802_bss *bss = priv;
10816	return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
10817}
10818#endif /* HOSTAPD || CONFIG_AP */
10819
10820
10821static int driver_nl80211_read_sta_data(void *priv,
10822					struct hostap_sta_driver_data *data,
10823					const u8 *addr)
10824{
10825	struct i802_bss *bss = priv;
10826	return i802_read_sta_data(bss, data, addr);
10827}
10828
10829
10830static int driver_nl80211_send_action(void *priv, unsigned int freq,
10831				      unsigned int wait_time,
10832				      const u8 *dst, const u8 *src,
10833				      const u8 *bssid,
10834				      const u8 *data, size_t data_len,
10835				      int no_cck)
10836{
10837	struct i802_bss *bss = priv;
10838	return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
10839					      bssid, data, data_len, no_cck);
10840}
10841
10842
10843static int driver_nl80211_probe_req_report(void *priv, int report)
10844{
10845	struct i802_bss *bss = priv;
10846	return wpa_driver_nl80211_probe_req_report(bss, report);
10847}
10848
10849
10850static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
10851					    const u8 *ies, size_t ies_len)
10852{
10853	int ret;
10854	struct nl_msg *msg;
10855	struct i802_bss *bss = priv;
10856	struct wpa_driver_nl80211_data *drv = bss->drv;
10857	u16 mdid = WPA_GET_LE16(md);
10858
10859	msg = nlmsg_alloc();
10860	if (!msg)
10861		return -ENOMEM;
10862
10863	wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
10864	nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
10865	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10866	NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
10867	NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
10868
10869	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10870	if (ret) {
10871		wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
10872			   "err=%d (%s)", ret, strerror(-ret));
10873	}
10874
10875	return ret;
10876
10877nla_put_failure:
10878	nlmsg_free(msg);
10879	return -ENOBUFS;
10880}
10881
10882
10883const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
10884{
10885	struct i802_bss *bss = priv;
10886	struct wpa_driver_nl80211_data *drv = bss->drv;
10887
10888	if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
10889		return NULL;
10890
10891	return bss->addr;
10892}
10893
10894
10895static const char * scan_state_str(enum scan_states scan_state)
10896{
10897	switch (scan_state) {
10898	case NO_SCAN:
10899		return "NO_SCAN";
10900	case SCAN_REQUESTED:
10901		return "SCAN_REQUESTED";
10902	case SCAN_STARTED:
10903		return "SCAN_STARTED";
10904	case SCAN_COMPLETED:
10905		return "SCAN_COMPLETED";
10906	case SCAN_ABORTED:
10907		return "SCAN_ABORTED";
10908	case SCHED_SCAN_STARTED:
10909		return "SCHED_SCAN_STARTED";
10910	case SCHED_SCAN_STOPPED:
10911		return "SCHED_SCAN_STOPPED";
10912	case SCHED_SCAN_RESULTS:
10913		return "SCHED_SCAN_RESULTS";
10914	}
10915
10916	return "??";
10917}
10918
10919
10920static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
10921{
10922	struct i802_bss *bss = priv;
10923	struct wpa_driver_nl80211_data *drv = bss->drv;
10924	int res;
10925	char *pos, *end;
10926
10927	pos = buf;
10928	end = buf + buflen;
10929
10930	res = os_snprintf(pos, end - pos,
10931			  "ifindex=%d\n"
10932			  "ifname=%s\n"
10933			  "brname=%s\n"
10934			  "addr=" MACSTR "\n"
10935			  "freq=%d\n"
10936			  "%s%s%s%s%s",
10937			  bss->ifindex,
10938			  bss->ifname,
10939			  bss->brname,
10940			  MAC2STR(bss->addr),
10941			  bss->freq,
10942			  bss->beacon_set ? "beacon_set=1\n" : "",
10943			  bss->added_if_into_bridge ?
10944			  "added_if_into_bridge=1\n" : "",
10945			  bss->added_bridge ? "added_bridge=1\n" : "",
10946			  bss->in_deinit ? "in_deinit=1\n" : "",
10947			  bss->if_dynamic ? "if_dynamic=1\n" : "");
10948	if (res < 0 || res >= end - pos)
10949		return pos - buf;
10950	pos += res;
10951
10952	if (bss->wdev_id_set) {
10953		res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
10954				  (unsigned long long) bss->wdev_id);
10955		if (res < 0 || res >= end - pos)
10956			return pos - buf;
10957		pos += res;
10958	}
10959
10960	res = os_snprintf(pos, end - pos,
10961			  "phyname=%s\n"
10962			  "drv_ifindex=%d\n"
10963			  "operstate=%d\n"
10964			  "scan_state=%s\n"
10965			  "auth_bssid=" MACSTR "\n"
10966			  "auth_attempt_bssid=" MACSTR "\n"
10967			  "bssid=" MACSTR "\n"
10968			  "prev_bssid=" MACSTR "\n"
10969			  "associated=%d\n"
10970			  "assoc_freq=%u\n"
10971			  "monitor_sock=%d\n"
10972			  "monitor_ifidx=%d\n"
10973			  "monitor_refcount=%d\n"
10974			  "last_mgmt_freq=%u\n"
10975			  "eapol_tx_sock=%d\n"
10976			  "%s%s%s%s%s%s%s%s%s%s%s%s%s",
10977			  drv->phyname,
10978			  drv->ifindex,
10979			  drv->operstate,
10980			  scan_state_str(drv->scan_state),
10981			  MAC2STR(drv->auth_bssid),
10982			  MAC2STR(drv->auth_attempt_bssid),
10983			  MAC2STR(drv->bssid),
10984			  MAC2STR(drv->prev_bssid),
10985			  drv->associated,
10986			  drv->assoc_freq,
10987			  drv->monitor_sock,
10988			  drv->monitor_ifidx,
10989			  drv->monitor_refcount,
10990			  drv->last_mgmt_freq,
10991			  drv->eapol_tx_sock,
10992			  drv->ignore_if_down_event ?
10993			  "ignore_if_down_event=1\n" : "",
10994			  drv->scan_complete_events ?
10995			  "scan_complete_events=1\n" : "",
10996			  drv->disabled_11b_rates ?
10997			  "disabled_11b_rates=1\n" : "",
10998			  drv->pending_remain_on_chan ?
10999			  "pending_remain_on_chan=1\n" : "",
11000			  drv->in_interface_list ? "in_interface_list=1\n" : "",
11001			  drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11002			  drv->poll_command_supported ?
11003			  "poll_command_supported=1\n" : "",
11004			  drv->data_tx_status ? "data_tx_status=1\n" : "",
11005			  drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11006			  drv->retry_auth ? "retry_auth=1\n" : "",
11007			  drv->use_monitor ? "use_monitor=1\n" : "",
11008			  drv->ignore_next_local_disconnect ?
11009			  "ignore_next_local_disconnect=1\n" : "",
11010			  drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11011	if (res < 0 || res >= end - pos)
11012		return pos - buf;
11013	pos += res;
11014
11015	if (drv->has_capability) {
11016		res = os_snprintf(pos, end - pos,
11017				  "capa.key_mgmt=0x%x\n"
11018				  "capa.enc=0x%x\n"
11019				  "capa.auth=0x%x\n"
11020				  "capa.flags=0x%x\n"
11021				  "capa.max_scan_ssids=%d\n"
11022				  "capa.max_sched_scan_ssids=%d\n"
11023				  "capa.sched_scan_supported=%d\n"
11024				  "capa.max_match_sets=%d\n"
11025				  "capa.max_remain_on_chan=%u\n"
11026				  "capa.max_stations=%u\n"
11027				  "capa.probe_resp_offloads=0x%x\n"
11028				  "capa.max_acl_mac_addrs=%u\n"
11029				  "capa.num_multichan_concurrent=%u\n",
11030				  drv->capa.key_mgmt,
11031				  drv->capa.enc,
11032				  drv->capa.auth,
11033				  drv->capa.flags,
11034				  drv->capa.max_scan_ssids,
11035				  drv->capa.max_sched_scan_ssids,
11036				  drv->capa.sched_scan_supported,
11037				  drv->capa.max_match_sets,
11038				  drv->capa.max_remain_on_chan,
11039				  drv->capa.max_stations,
11040				  drv->capa.probe_resp_offloads,
11041				  drv->capa.max_acl_mac_addrs,
11042				  drv->capa.num_multichan_concurrent);
11043		if (res < 0 || res >= end - pos)
11044			return pos - buf;
11045		pos += res;
11046	}
11047
11048	return pos - buf;
11049}
11050
11051
11052const struct wpa_driver_ops wpa_driver_nl80211_ops = {
11053	.name = "nl80211",
11054	.desc = "Linux nl80211/cfg80211",
11055	.get_bssid = wpa_driver_nl80211_get_bssid,
11056	.get_ssid = wpa_driver_nl80211_get_ssid,
11057	.set_key = driver_nl80211_set_key,
11058	.scan2 = driver_nl80211_scan2,
11059	.sched_scan = wpa_driver_nl80211_sched_scan,
11060	.stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
11061	.get_scan_results2 = wpa_driver_nl80211_get_scan_results,
11062	.deauthenticate = driver_nl80211_deauthenticate,
11063	.authenticate = driver_nl80211_authenticate,
11064	.associate = wpa_driver_nl80211_associate,
11065	.global_init = nl80211_global_init,
11066	.global_deinit = nl80211_global_deinit,
11067	.init2 = wpa_driver_nl80211_init,
11068	.deinit = driver_nl80211_deinit,
11069	.get_capa = wpa_driver_nl80211_get_capa,
11070	.set_operstate = wpa_driver_nl80211_set_operstate,
11071	.set_supp_port = wpa_driver_nl80211_set_supp_port,
11072	.set_country = wpa_driver_nl80211_set_country,
11073	.set_ap = wpa_driver_nl80211_set_ap,
11074	.set_acl = wpa_driver_nl80211_set_acl,
11075	.if_add = wpa_driver_nl80211_if_add,
11076	.if_remove = driver_nl80211_if_remove,
11077	.send_mlme = driver_nl80211_send_mlme,
11078	.get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
11079	.sta_add = wpa_driver_nl80211_sta_add,
11080	.sta_remove = driver_nl80211_sta_remove,
11081	.hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
11082	.sta_set_flags = wpa_driver_nl80211_sta_set_flags,
11083#ifdef HOSTAPD
11084	.hapd_init = i802_init,
11085	.hapd_deinit = i802_deinit,
11086	.set_wds_sta = i802_set_wds_sta,
11087#endif /* HOSTAPD */
11088#if defined(HOSTAPD) || defined(CONFIG_AP)
11089	.get_seqnum = i802_get_seqnum,
11090	.flush = i802_flush,
11091	.get_inact_sec = i802_get_inact_sec,
11092	.sta_clear_stats = i802_sta_clear_stats,
11093	.set_rts = i802_set_rts,
11094	.set_frag = i802_set_frag,
11095	.set_tx_queue_params = i802_set_tx_queue_params,
11096	.set_sta_vlan = driver_nl80211_set_sta_vlan,
11097	.sta_deauth = i802_sta_deauth,
11098	.sta_disassoc = i802_sta_disassoc,
11099#endif /* HOSTAPD || CONFIG_AP */
11100	.read_sta_data = driver_nl80211_read_sta_data,
11101	.set_freq = i802_set_freq,
11102	.send_action = driver_nl80211_send_action,
11103	.send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
11104	.remain_on_channel = wpa_driver_nl80211_remain_on_channel,
11105	.cancel_remain_on_channel =
11106	wpa_driver_nl80211_cancel_remain_on_channel,
11107	.probe_req_report = driver_nl80211_probe_req_report,
11108	.deinit_ap = wpa_driver_nl80211_deinit_ap,
11109	.deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
11110	.resume = wpa_driver_nl80211_resume,
11111	.send_ft_action = nl80211_send_ft_action,
11112	.signal_monitor = nl80211_signal_monitor,
11113	.signal_poll = nl80211_signal_poll,
11114	.send_frame = nl80211_send_frame,
11115	.shared_freq = wpa_driver_nl80211_shared_freq,
11116	.set_param = nl80211_set_param,
11117	.get_radio_name = nl80211_get_radio_name,
11118	.add_pmkid = nl80211_add_pmkid,
11119	.remove_pmkid = nl80211_remove_pmkid,
11120	.flush_pmkid = nl80211_flush_pmkid,
11121	.set_rekey_info = nl80211_set_rekey_info,
11122	.poll_client = nl80211_poll_client,
11123	.set_p2p_powersave = nl80211_set_p2p_powersave,
11124	.start_dfs_cac = nl80211_start_radar_detection,
11125	.stop_ap = wpa_driver_nl80211_stop_ap,
11126#ifdef CONFIG_TDLS
11127	.send_tdls_mgmt = nl80211_send_tdls_mgmt,
11128	.tdls_oper = nl80211_tdls_oper,
11129#endif /* CONFIG_TDLS */
11130	.update_ft_ies = wpa_driver_nl80211_update_ft_ies,
11131	.get_mac_addr = wpa_driver_nl80211_get_macaddr,
11132	.get_survey = wpa_driver_nl80211_get_survey,
11133	.status = wpa_driver_nl80211_status,
11134#ifdef ANDROID_P2P
11135	.set_noa = wpa_driver_set_p2p_noa,
11136	.get_noa = wpa_driver_get_p2p_noa,
11137	.set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
11138#endif
11139#ifdef ANDROID
11140	.driver_cmd = wpa_driver_nl80211_driver_cmd,
11141#endif
11142};
11143