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