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