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