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