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