driver_nl80211.c revision f21452aea786ac056eb01f1cbba4f553bd502747
18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/*
28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Driver interaction with Linux nl80211/cfg80211
3fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2003-2004, Instant802 Networks, Inc.
58d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2005-2006, Devicescape Software, Inc.
68d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2009-2010, Atheros Communications
88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
9c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * This software may be distributed under the terms of the BSD license.
10c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * See README for more details.
118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "includes.h"
148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <sys/ioctl.h>
158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <sys/types.h>
168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <sys/stat.h>
178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <fcntl.h>
188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <net/if.h>
198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <netlink/genl/genl.h>
208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <netlink/genl/family.h>
218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <netlink/genl/ctrl.h>
228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <linux/rtnetlink.h>
238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <netpacket/packet.h>
248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <linux/filter.h>
251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include <linux/errqueue.h>
268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "nl80211_copy.h"
278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common.h"
298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "eloop.h"
308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/list.h"
31cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt#include "common/qca-vendor.h"
328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common/ieee802_11_defs.h"
331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include "common/ieee802_11_common.h"
341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include "l2_packet/l2_packet.h"
358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "netlink.h"
368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "linux_ioctl.h"
378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "radiotap.h"
388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "radiotap_iter.h"
398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "rfkill.h"
408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "driver.h"
41c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifndef SO_WIFI_STATUS
431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt# if defined(__sparc__)
441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#  define SO_WIFI_STATUS	0x0025
451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt# elif defined(__parisc__)
461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#  define SO_WIFI_STATUS	0x4022
471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt# else
481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#  define SO_WIFI_STATUS	41
491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt# endif
501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt# define SCM_WIFI_STATUS	SO_WIFI_STATUS
521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif
531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifndef SO_EE_ORIGIN_TXSTATUS
551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#define SO_EE_ORIGIN_TXSTATUS	4
561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif
571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifndef PACKET_TX_TIMESTAMP
591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#define PACKET_TX_TIMESTAMP	16
601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif
611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef ANDROID
631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include "android_drv.h"
641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* ANDROID */
658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_LIBNL20
668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* libnl 2.0 compatibility code */
678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define nl_handle nl_sock
688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define nl80211_handle_alloc nl_socket_alloc_cb
698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define nl80211_handle_destroy nl_socket_free
708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#else
718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/*
728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * libnl 1.1 has a bug, it tries to allocate socket numbers densely
738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * but when you free a socket again it will mess up its bitmap and
748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * and use the wrong number the next time it needs a socket ID.
758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Therefore, we wrap the handle alloc/destroy and add our own pid
768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * accounting.
778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic uint32_t port_bitmap[32] = { 0 };
798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct nl_handle *nl80211_handle_alloc(void *cb)
818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_handle *handle;
838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	uint32_t pid = getpid() & 0x3FFFFF;
848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int i;
858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	handle = nl_handle_alloc_cb(cb);
878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < 1024; i++) {
898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (port_bitmap[i / 32] & (1 << (i % 32)))
908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		port_bitmap[i / 32] |= 1 << (i % 32);
928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pid += i << 22;
938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nl_socket_set_local_port(handle, pid);
978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return handle;
998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_handle_destroy(struct nl_handle *handle)
1028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	uint32_t port = nl_socket_get_local_port(handle);
1048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	port >>= 22;
1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	port_bitmap[port / 32] &= ~(1 << (port % 32));
1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nl_handle_destroy(handle);
1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_LIBNL20 */
1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1135460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidt#ifdef ANDROID
1145460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidt/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
1155460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidtstatic int android_nl_socket_set_nonblocking(struct nl_handle *handle)
1165460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidt{
1175460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidt	return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
1185460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidt}
1195460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidt#undef nl_socket_set_nonblocking
1205460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidt#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
1215460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidt#endif /* ANDROID */
1225460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidt
1235460547a121207cf7a99eac45e05fcdd83be3161Dmitry Shmidt
1241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
1251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
1261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_handle *handle;
1271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	handle = nl80211_handle_alloc(cb);
1291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (handle == NULL) {
1301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "callbacks (%s)", dbg);
1321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
1331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
1341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (genl_connect(handle)) {
1361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
1371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "netlink (%s)", dbg);
1381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_handle_destroy(handle);
1391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
1401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
1411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return handle;
1431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
1441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl_destroy_handles(struct nl_handle **handle)
1471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
1481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (*handle == NULL)
1491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
1501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_handle_destroy(*handle);
1511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	*handle = NULL;
1521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
1531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
15568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt#if __WORDSIZE == 64
15668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt#define ELOOP_SOCKET_INVALID	(intptr_t) 0x8888888888888889ULL
15768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt#else
15868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt#define ELOOP_SOCKET_INVALID	(intptr_t) 0x88888889ULL
15968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt#endif
16068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
16168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidtstatic void nl80211_register_eloop_read(struct nl_handle **handle,
16268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt					eloop_sock_handler handler,
16368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt					void *eloop_data)
16468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt{
16568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl_socket_set_nonblocking(*handle);
16668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
16768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				 eloop_data, *handle);
16868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	*handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
16968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt}
17068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
17168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
17268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidtstatic void nl80211_destroy_eloop_handle(struct nl_handle **handle)
17368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt{
17468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	*handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
17568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	eloop_unregister_read_sock(nl_socket_get_fd(*handle));
17668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl_destroy_handles(handle);
17768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt}
17868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
17968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef IFF_LOWER_UP
1818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define IFF_LOWER_UP   0x10000         /* driver signals L1 up         */
1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif
1838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef IFF_DORMANT
1848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define IFF_DORMANT    0x20000         /* driver signals dormant       */
1858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif
1868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef IF_OPER_DORMANT
1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define IF_OPER_DORMANT 5
1898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif
1908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef IF_OPER_UP
1918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define IF_OPER_UP 6
1928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif
1938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct nl80211_global {
1958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct dl_list interfaces;
1961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int if_add_ifindex;
19734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	u64 if_add_wdevid;
19834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	int if_add_wdevid_set;
1991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct netlink_data *netlink;
2001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_cb *nl_cb;
2011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_handle *nl;
2021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int nl80211_id;
2031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ioctl_sock; /* socket for ioctl() use */
2041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_handle *nl_event;
2061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt};
2071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstruct nl80211_wiphy_data {
2091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct dl_list list;
2101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct dl_list bsss;
2111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct dl_list drvs;
2121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_handle *nl_beacons;
2141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_cb *nl_cb;
2151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int wiphy_idx;
2178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
2188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_global_deinit(void *priv);
2201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct i802_bss {
2228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv;
2238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *next;
2248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ifindex;
22534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	u64 wdev_id;
2268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char ifname[IFNAMSIZ + 1];
2278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char brname[IFNAMSIZ];
2288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	unsigned int beacon_set:1;
2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	unsigned int added_if_into_bridge:1;
2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	unsigned int added_bridge:1;
23104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	unsigned int in_deinit:1;
23234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	unsigned int wdev_id_set:1;
233cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	unsigned int added_if:1;
2341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u8 addr[ETH_ALEN];
2361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int freq;
238b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	int if_dynamic;
2391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
240a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	void *ctx;
2411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_handle *nl_preq, *nl_mgmt;
2421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_cb *nl_cb;
2431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl80211_wiphy_data *wiphy_data;
2451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct dl_list wiphy_list;
2468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
2478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpa_driver_nl80211_data {
2498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl80211_global *global;
2508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct dl_list list;
2511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct dl_list wiphy_list;
2528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char phyname[32];
2538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	void *ctx;
2548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ifindex;
2558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int if_removed;
2568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int if_disabled;
2571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ignore_if_down_event;
2588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rfkill_data *rfkill;
2598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_capa capa;
260444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	u8 *extended_capa, *extended_capa_mask;
261444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	unsigned int extended_capa_len;
2628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int has_capability;
2638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int operstate;
2658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int scan_complete_events;
2675605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	enum scan_states {
2685605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
2695605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
2705605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		SCHED_SCAN_RESULTS
2715605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	} scan_state;
2728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_cb *nl_cb;
2748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 auth_bssid[ETH_ALEN];
2768bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	u8 auth_attempt_bssid[ETH_ALEN];
2778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 bssid[ETH_ALEN];
2788bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	u8 prev_bssid[ETH_ALEN];
2798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int associated;
2808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 ssid[32];
2818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t ssid_len;
2821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	enum nl80211_iftype nlmode;
2831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	enum nl80211_iftype ap_scan_as_station;
2848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	unsigned int assoc_freq;
2858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int monitor_sock;
2878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int monitor_ifidx;
2881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int monitor_refcount;
2898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int disabled_11b_rates:1;
2918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	unsigned int pending_remain_on_chan:1;
2921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int in_interface_list:1;
2931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int device_ap_sme:1;
2941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int poll_command_supported:1;
2951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int data_tx_status:1;
2961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int scan_for_auth:1;
2971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int retry_auth:1;
2981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int use_monitor:1;
299d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	unsigned int ignore_next_local_disconnect:1;
30034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	unsigned int allow_p2p_device:1;
301e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	unsigned int hostapd:1;
302e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	unsigned int start_mode_ap:1;
303e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	unsigned int start_iface_up:1;
3048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u64 remain_on_chan_cookie;
3068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u64 send_action_cookie;
3078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	unsigned int last_mgmt_freq;
3098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_scan_filter *filter_ssids;
3118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t num_filter_ssids;
3128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
313cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	struct i802_bss *first_bss;
3148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int eapol_tx_sock;
3161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
3178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int eapol_sock; /* socket for EAPOL frames */
3188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int default_if_indices[16];
3208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int *if_indices;
3218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int num_if_indices;
3228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* From failed authentication command */
3241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int auth_freq;
3251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u8 auth_bssid_[ETH_ALEN];
3261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u8 auth_ssid[32];
3271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	size_t auth_ssid_len;
3281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int auth_alg;
3291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u8 *auth_ie;
3301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	size_t auth_ie_len;
3311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u8 auth_wep_key[4][16];
3321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	size_t auth_wep_key_len[4];
3331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int auth_wep_tx_keyidx;
3341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int auth_local_state_change;
3351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int auth_p2p;
3368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
3378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3394b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic void wpa_driver_nl80211_deinit(struct i802_bss *bss);
3408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
3418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					    void *timeout_ctx);
3421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
3431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				       enum nl80211_iftype nlmode);
3448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int
345e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtwpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
346e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				   const u8 *set_addr, int first);
3478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   const u8 *addr, int cmd, u16 reason_code,
3498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   int local_state_change);
3508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_remove_monitor_interface(
3518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv);
3521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_send_frame_cmd(struct i802_bss *bss,
3538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  unsigned int freq, unsigned int wait,
3541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  const u8 *buf, size_t buf_len, u64 *cookie,
3551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  int no_cck, int no_ack, int offchanok);
356c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidtstatic int nl80211_register_frame(struct i802_bss *bss,
357c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt				  struct nl_handle *hl_handle,
358c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt				  u16 type, const u8 *match, size_t match_len);
3594b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
3604b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					       int report);
3611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef ANDROID
3621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int android_pno_start(struct i802_bss *bss,
3631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			     struct wpa_driver_scan_params *params);
3641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int android_pno_stop(struct i802_bss *bss);
365292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidtextern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
366292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidt					 size_t buf_len);
3671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* ANDROID */
3681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef ANDROID_P2P
3696e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidtint wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
3701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtint wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
3716e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidtint wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
3726e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidtint wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
373292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidt				 const struct wpabuf *proberesp,
374292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidt				 const struct wpabuf *assocresp);
375292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidt#endif /* ANDROID_P2P */
3768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
3788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
3798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
3804b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
3818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					enum wpa_driver_if_type type,
3828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					const char *ifname);
383738a26e9e2087b0d43eba3534535632b27b49947Dmitry Shmidt
3844b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
3854b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				       struct hostapd_freq_params *freq);
3868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
3878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     int ifindex, int disabled);
3888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
3901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_authenticate_retry(
3911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv);
3921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
393b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidtstatic int i802_set_iface_flags(struct i802_bss *bss, int up);
394b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
3951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
39634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic const char * nl80211_command_to_string(enum nl80211_commands cmd)
39734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
39834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt#define C2S(x) case x: return #x;
39934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	switch (cmd) {
40034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_UNSPEC)
40134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_WIPHY)
40234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_WIPHY)
40334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_NEW_WIPHY)
40434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_DEL_WIPHY)
40534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_INTERFACE)
40634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_INTERFACE)
40734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_NEW_INTERFACE)
40834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_DEL_INTERFACE)
40934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_KEY)
41034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_KEY)
41134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_NEW_KEY)
41234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_DEL_KEY)
41334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_BEACON)
41434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_BEACON)
41534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_START_AP)
41634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_STOP_AP)
41734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_STATION)
41834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_STATION)
41934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_NEW_STATION)
42034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_DEL_STATION)
42134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_MPATH)
42234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_MPATH)
42334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_NEW_MPATH)
42434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_DEL_MPATH)
42534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_BSS)
42634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_REG)
42734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_REQ_SET_REG)
42834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_MESH_CONFIG)
42934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_MESH_CONFIG)
43034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
43134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_REG)
43234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_SCAN)
43334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_TRIGGER_SCAN)
43434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_NEW_SCAN_RESULTS)
43534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SCAN_ABORTED)
43634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_REG_CHANGE)
43734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_AUTHENTICATE)
43834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_ASSOCIATE)
43934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_DEAUTHENTICATE)
44034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_DISASSOCIATE)
44134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
44234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_REG_BEACON_HINT)
44334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_JOIN_IBSS)
44434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_LEAVE_IBSS)
44534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_TESTMODE)
44634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_CONNECT)
44734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_ROAM)
44834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_DISCONNECT)
44934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_WIPHY_NETNS)
45034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_SURVEY)
45134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
45234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_PMKSA)
45334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_DEL_PMKSA)
45434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_FLUSH_PMKSA)
45534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
45634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
45734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
45834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_REGISTER_FRAME)
45934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_FRAME)
46034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_FRAME_TX_STATUS)
46134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_POWER_SAVE)
46234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_POWER_SAVE)
46334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_CQM)
46434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_NOTIFY_CQM)
46534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_CHANNEL)
46634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_WDS_PEER)
46734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
46834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_JOIN_MESH)
46934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_LEAVE_MESH)
47034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
47134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
47234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
47334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_WOWLAN)
47434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_WOWLAN)
47534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_START_SCHED_SCAN)
47634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_STOP_SCHED_SCAN)
47734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
47834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
47934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
48034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_PMKSA_CANDIDATE)
48134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_TDLS_OPER)
48234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_TDLS_MGMT)
48334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_UNEXPECTED_FRAME)
48434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_PROBE_CLIENT)
48534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_REGISTER_BEACONS)
48634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
48734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_NOACK_MAP)
48834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
48934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_START_P2P_DEVICE)
49034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_STOP_P2P_DEVICE)
49134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_CONN_FAILED)
49234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_MCAST_RATE)
49334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_SET_MAC_ACL)
49434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_RADAR_DETECT)
49534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
49634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_UPDATE_FT_IES)
49734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_FT_EVENT)
49834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_CRIT_PROTOCOL_START)
49934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
500fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	C2S(NL80211_CMD_GET_COALESCE)
501fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	C2S(NL80211_CMD_SET_COALESCE)
502fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	C2S(NL80211_CMD_CHANNEL_SWITCH)
503fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	C2S(NL80211_CMD_VENDOR)
504fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	C2S(NL80211_CMD_SET_QOS_MAP)
50534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	default:
50634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return "NL80211_CMD_UNKNOWN";
50734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	}
50834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt#undef C2S
50934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
51034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
51134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
51204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt/* Converts nl80211_chan_width to a common format */
51304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidtstatic enum chan_width convert2width(int width)
51404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt{
51504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	switch (width) {
51604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	case NL80211_CHAN_WIDTH_20_NOHT:
51704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		return CHAN_WIDTH_20_NOHT;
51804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	case NL80211_CHAN_WIDTH_20:
51904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		return CHAN_WIDTH_20;
52004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	case NL80211_CHAN_WIDTH_40:
52104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		return CHAN_WIDTH_40;
52204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	case NL80211_CHAN_WIDTH_80:
52304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		return CHAN_WIDTH_80;
52404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	case NL80211_CHAN_WIDTH_80P80:
52504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		return CHAN_WIDTH_80P80;
52604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	case NL80211_CHAN_WIDTH_160:
52704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		return CHAN_WIDTH_160;
52804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	}
52904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	return CHAN_WIDTH_UNKNOWN;
53004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt}
53104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
53204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
5331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int is_ap_interface(enum nl80211_iftype nlmode)
5341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
5351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return (nlmode == NL80211_IFTYPE_AP ||
5361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nlmode == NL80211_IFTYPE_P2P_GO);
5371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
5381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
5391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
5401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int is_sta_interface(enum nl80211_iftype nlmode)
5411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
5421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return (nlmode == NL80211_IFTYPE_STATION ||
5431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nlmode == NL80211_IFTYPE_P2P_CLIENT);
5441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
5451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
5461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
54734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic int is_p2p_net_interface(enum nl80211_iftype nlmode)
5481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
5491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
5501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nlmode == NL80211_IFTYPE_P2P_GO);
5511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
5528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5548bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidtstatic void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
5558bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt{
5568bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	if (drv->associated)
5578bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
5588bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	drv->associated = 0;
5598bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	os_memset(drv->bssid, 0, ETH_ALEN);
5608bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt}
5618bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
5628bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
56387fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinenstruct nl80211_bss_info_arg {
56487fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	struct wpa_driver_nl80211_data *drv;
56587fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	struct wpa_scan_results *res;
56687fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	unsigned int assoc_freq;
5671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u8 assoc_bssid[ETH_ALEN];
56887fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen};
56987fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
57087fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinenstatic int bss_info_handler(struct nl_msg *msg, void *arg);
57187fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
57287fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
5738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* nl80211 code */
5748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int ack_handler(struct nl_msg *msg, void *arg)
5758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int *err = arg;
5778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*err = 0;
5788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_STOP;
5798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int finish_handler(struct nl_msg *msg, void *arg)
5828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int *ret = arg;
5848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*ret = 0;
5858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
5868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
5898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 void *arg)
5908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int *ret = arg;
5928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*ret = err->error;
5938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
5948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int no_seq_check(struct nl_msg *msg, void *arg)
5988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_OK;
6008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
6018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int send_and_recv(struct nl80211_global *global,
6048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 struct nl_handle *nl_handle, struct nl_msg *msg,
6058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 int (*valid_handler)(struct nl_msg *, void *),
6068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 void *valid_data)
6078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
6088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_cb *cb;
6098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int err = -ENOMEM;
6108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	cb = nl_cb_clone(global->nl_cb);
6128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!cb)
6138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto out;
6148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	err = nl_send_auto_complete(nl_handle, msg);
6168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (err < 0)
6178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto out;
6188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	err = 1;
6208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
6228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
6238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
6248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (valid_handler)
6268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
6278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  valid_handler, valid_data);
6288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	while (err > 0) {
63068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		int res = nl_recvmsgs(nl_handle, cb);
63168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		if (res) {
63268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			wpa_printf(MSG_INFO,
63368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				   "nl80211: %s->nl_recvmsgs failed: %d",
63468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				   __func__, res);
63568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		}
63668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	}
6378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt out:
6388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nl_cb_put(cb);
6398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
6408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return err;
6418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
6428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int send_and_recv_msgs_global(struct nl80211_global *global,
6451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     struct nl_msg *msg,
6461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     int (*valid_handler)(struct nl_msg *, void *),
6471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     void *valid_data)
6481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
6491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return send_and_recv(global, global->nl, msg, valid_handler,
6501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			     valid_data);
6511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
6521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
65304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
654641185ebd73d9607756c87b22f6c520ab1846325Dmitry Shmidtstatic int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
6558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			      struct nl_msg *msg,
6568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			      int (*valid_handler)(struct nl_msg *, void *),
6578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			      void *valid_data)
6588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
6591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return send_and_recv(drv->global, drv->global->nl, msg,
6601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			     valid_handler, valid_data);
6618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
6628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct family_data {
6658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const char *group;
6668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int id;
6678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
6688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
67134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
67234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (bss->wdev_id_set)
67334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
67434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	else
67534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
67634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return 0;
67734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
67834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtnla_put_failure:
67934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return -1;
68034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
68134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
68234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
6838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int family_handler(struct nl_msg *msg, void *arg)
6848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
6858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct family_data *res = arg;
6868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *tb[CTRL_ATTR_MAX + 1];
6878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *mcgrp;
6898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int i;
6908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
6938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!tb[CTRL_ATTR_MCAST_GROUPS])
6948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
6958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
6978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
6988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
6998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  nla_len(mcgrp), NULL);
7008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
7018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
7028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
7038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       res->group,
7048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
7058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
7068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
7078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
7088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
7098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
7118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
7128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl_get_multicast_id(struct nl80211_global *global,
7158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       const char *family, const char *group)
7168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
7178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
7188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -1;
7198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct family_data res = { group, -ENOENT };
7208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
7228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
7238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
7241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
7258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    0, 0, CTRL_CMD_GETFAMILY, 0);
7268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
7278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
7298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
7308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret == 0)
7318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = res.id;
7328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
7348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
7358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
7368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
7378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
7401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			  struct nl_msg *msg, int flags, uint8_t cmd)
7411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
7421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
7431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   0, flags, cmd, 0);
7441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
7451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstruct wiphy_idx_data {
7481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int wiphy_idx;
74934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	enum nl80211_iftype nlmode;
75034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	u8 *macaddr;
7511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt};
7521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int netdev_info_handler(struct nl_msg *msg, void *arg)
7551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
7561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
7571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wiphy_idx_data *info = arg;
7591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
7621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (tb[NL80211_ATTR_WIPHY])
7641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
7651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
76634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (tb[NL80211_ATTR_IFTYPE])
76734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
76834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
76934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (tb[NL80211_ATTR_MAC] && info->macaddr)
77034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
77134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			  ETH_ALEN);
77234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
7731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return NL_SKIP;
7741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
7751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_get_wiphy_index(struct i802_bss *bss)
7781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
7791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
7801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wiphy_idx_data data = {
7811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		.wiphy_idx = -1,
78234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		.macaddr = NULL,
7831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	};
7841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
7861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
78734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return NL80211_IFTYPE_UNSPECIFIED;
7881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
7901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
79134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nl80211_set_iface_id(msg, bss) < 0)
79234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
7931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
7951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return data.wiphy_idx;
7961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
7971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtnla_put_failure:
7981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
7991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return -1;
8001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
8011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
80334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
80434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
80534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct nl_msg *msg;
80634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct wiphy_idx_data data = {
80734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		.nlmode = NL80211_IFTYPE_UNSPECIFIED,
80834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		.macaddr = NULL,
80934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	};
81034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
81134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	msg = nlmsg_alloc();
81234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (!msg)
81334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return -1;
81434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
81534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
81634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
81734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nl80211_set_iface_id(msg, bss) < 0)
81834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
81934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
82034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
82134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return data.nlmode;
82234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	msg = NULL;
82334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtnla_put_failure:
82434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nlmsg_free(msg);
82534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return NL80211_IFTYPE_UNSPECIFIED;
82634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
82734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
82834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
82934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic int nl80211_get_macaddr(struct i802_bss *bss)
83034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
83134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct nl_msg *msg;
83234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct wiphy_idx_data data = {
83334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		.macaddr = bss->addr,
83434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	};
83534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
83634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	msg = nlmsg_alloc();
83734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (!msg)
83834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return NL80211_IFTYPE_UNSPECIFIED;
83934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
84034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
84134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nl80211_set_iface_id(msg, bss) < 0)
84234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
84334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
84434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
84534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
84634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtnla_put_failure:
84734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nlmsg_free(msg);
84834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return NL80211_IFTYPE_UNSPECIFIED;
84934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
85034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
85134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
8521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
8531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				    struct nl80211_wiphy_data *w)
8541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
8551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
8561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ret = -1;
8571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
8591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
8601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
8611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
8631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
8651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
8671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
8681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ret) {
8691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
8701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "failed: ret=%d (%s)",
8711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   ret, strerror(-ret));
8721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto nla_put_failure;
8731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
8741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = 0;
8751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtnla_put_failure:
8761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
8771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return ret;
8781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
8791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
8821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
8831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl80211_wiphy_data *w = eloop_ctx;
88468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	int res;
8851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
886c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
8871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
88868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	res = nl_recvmsgs(handle, w->nl_cb);
88968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	if (res) {
89068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
89168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   __func__, res);
89268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	}
8931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
8941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int process_beacon_event(struct nl_msg *msg, void *arg)
8971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
8981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl80211_wiphy_data *w = arg;
8991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv;
9001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
9021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	union wpa_event_data event;
9031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
9061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (gnlh->cmd != NL80211_CMD_FRAME) {
9081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
9091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   gnlh->cmd);
9101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NL_SKIP;
9111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
9121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!tb[NL80211_ATTR_FRAME])
9141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NL_SKIP;
9151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
9171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 wiphy_list) {
9181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		os_memset(&event, 0, sizeof(event));
9191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
9201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
9211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
9221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
9231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return NL_SKIP;
9251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
9261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic struct nl80211_wiphy_data *
9291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtnl80211_get_wiphy_data_ap(struct i802_bss *bss)
9301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
9311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	static DEFINE_DL_LIST(nl80211_wiphys);
9321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl80211_wiphy_data *w;
9331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int wiphy_idx, found = 0;
9341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *tmp_bss;
9351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (bss->wiphy_data != NULL)
9371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return bss->wiphy_data;
9381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wiphy_idx = nl80211_get_wiphy_index(bss);
9401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
9421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (w->wiphy_idx == wiphy_idx)
9431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			goto add;
9441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
9451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* alloc new one */
9471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	w = os_zalloc(sizeof(*w));
9481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (w == NULL)
9491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
9501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	w->wiphy_idx = wiphy_idx;
9511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_init(&w->bsss);
9521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_init(&w->drvs);
9531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
9551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!w->nl_cb) {
9561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		os_free(w);
9571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
9581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
9591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
9601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
9611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  w);
9621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
9641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					 "wiphy beacons");
9651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (w->nl_beacons == NULL) {
9661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		os_free(w);
9671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
9681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
9691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_beacons(bss->drv, w)) {
9711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl_destroy_handles(&w->nl_beacons);
9721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		os_free(w);
9731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
9741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
9751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
97668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
9771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_add(&nl80211_wiphys, &w->list);
9791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtadd:
9811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* drv entry for this bss already there? */
9821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
9831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (tmp_bss->drv == bss->drv) {
9841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			found = 1;
9851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			break;
9861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
9871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
9881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* if not add it */
9891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!found)
9901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		dl_list_add(&w->drvs, &bss->drv->wiphy_list);
9911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_add(&w->bsss, &bss->wiphy_list);
9931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bss->wiphy_data = w;
9941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return w;
9951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
9961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
9981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
9991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
10001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl80211_wiphy_data *w = bss->wiphy_data;
10011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *tmp_bss;
10021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int found = 0;
10031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
10041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (w == NULL)
10051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
10061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bss->wiphy_data = NULL;
10071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_del(&bss->wiphy_list);
10081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
10091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* still any for this drv present? */
10101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
10111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (tmp_bss->drv == bss->drv) {
10121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			found = 1;
10131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			break;
10141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
10151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
10161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* if not remove it */
10171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!found)
10181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		dl_list_del(&bss->drv->wiphy_list);
10191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
10201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!dl_list_empty(&w->bsss))
10211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
10221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
102368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl80211_destroy_eloop_handle(&w->nl_beacons);
10241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
10251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_put(w->nl_cb);
10261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_del(&w->list);
10271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_free(w);
10281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
10291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
10301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
10318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
10328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
10338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
10348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
10358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!drv->associated)
10368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
10378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(bssid, drv->bssid, ETH_ALEN);
10388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
10398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
10408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
10438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
10448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
10458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
10468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!drv->associated)
10478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
10488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(ssid, drv->ssid, drv->ssid_len);
10498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return drv->ssid_len;
10508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
10518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1053fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic void wpa_driver_nl80211_event_newlink(
1054fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct wpa_driver_nl80211_data *drv, char *ifname)
10558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
10568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
10578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1058fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1059fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		if (if_nametoindex(drv->first_bss->ifname) == 0) {
1060fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1061fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				   drv->first_bss->ifname);
1062fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			return;
1063fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		}
1064fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		if (!drv->if_removed)
1065fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			return;
1066fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1067fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   drv->first_bss->ifname);
1068fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		drv->if_removed = 0;
1069fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
1070fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
10718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
1072fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	os_strlcpy(event.interface_status.ifname, ifname,
1073fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		   sizeof(event.interface_status.ifname));
1074fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1075fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1076fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt}
1077fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
1078fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
1079fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic void wpa_driver_nl80211_event_dellink(
1080fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct wpa_driver_nl80211_data *drv, char *ifname)
1081fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt{
1082fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	union wpa_event_data event;
1083fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
1084fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1085fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		if (drv->if_removed) {
1086fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1087fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				   ifname);
1088fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			return;
108904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		}
1090fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1091fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   ifname);
1092fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		drv->if_removed = 1;
1093fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	} else {
1094fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1095fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   ifname);
10968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
10978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1098fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
1099fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	os_strlcpy(event.interface_status.ifname, ifname,
1100fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		   sizeof(event.interface_status.ifname));
1101fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
11028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
11038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
11048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
11078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					 u8 *buf, size_t len)
11088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
11098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int attrlen, rta_len;
11108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rtattr *attr;
11118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	attrlen = len;
11138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	attr = (struct rtattr *) buf;
11148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rta_len = RTA_ALIGN(sizeof(struct rtattr));
11168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (RTA_OK(attr, attrlen)) {
11178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (attr->rta_type == IFLA_IFNAME) {
1118cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			if (os_strcmp(((char *) attr) + rta_len,
1119cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				      drv->first_bss->ifname) == 0)
11208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				return 1;
11218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			else
11228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				break;
11238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
11248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		attr = RTA_NEXT(attr, attrlen);
11258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
11288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
11298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
11328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  int ifindex, u8 *buf, size_t len)
11338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
11348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->ifindex == ifindex)
11358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 1;
11368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
11388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
11398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "interface");
1140e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
11418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 1;
11428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
11458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
11468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic struct wpa_driver_nl80211_data *
11491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtnl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
11501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
11511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv;
11521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_for_each(drv, &global->interfaces,
11531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 struct wpa_driver_nl80211_data, list) {
11541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
11551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    have_ifidx(drv, idx))
11561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			return drv;
11571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
11581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return NULL;
11591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
11601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
11611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
11628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
11638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						 struct ifinfomsg *ifi,
11648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						 u8 *buf, size_t len)
11658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
11661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl80211_global *global = ctx;
11671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv;
1168fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int attrlen;
11698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rtattr *attr;
11708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 brid = 0;
11711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	char namebuf[IFNAMSIZ];
1172fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	char ifname[IFNAMSIZ + 1];
1173fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	char extra[100], *pos, *end;
11748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
11761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!drv) {
1177fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1178fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   ifi->ifi_index);
11798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
11808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1182fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	extra[0] = '\0';
1183fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	pos = extra;
1184fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	end = pos + sizeof(extra);
1185fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	ifname[0] = '\0';
1186fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
1187fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	attrlen = len;
1188fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	attr = (struct rtattr *) buf;
1189fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	while (RTA_OK(attr, attrlen)) {
1190fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		switch (attr->rta_type) {
1191fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case IFLA_IFNAME:
1192fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1193fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				break;
1194fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1195fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			ifname[RTA_PAYLOAD(attr)] = '\0';
1196fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
1197fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case IFLA_MASTER:
1198fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			brid = nla_get_u32((struct nlattr *) attr);
1199fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			pos += os_snprintf(pos, end - pos, " master=%u", brid);
1200fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
1201fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case IFLA_WIRELESS:
1202fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			pos += os_snprintf(pos, end - pos, " wext");
1203fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
1204fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case IFLA_OPERSTATE:
1205fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			pos += os_snprintf(pos, end - pos, " operstate=%u",
1206fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt					   nla_get_u32((struct nlattr *) attr));
1207fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
1208fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case IFLA_LINKMODE:
1209fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			pos += os_snprintf(pos, end - pos, " linkmode=%u",
1210fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt					   nla_get_u32((struct nlattr *) attr));
1211fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
1212fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		}
1213fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		attr = RTA_NEXT(attr, attrlen);
1214fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
1215fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	extra[sizeof(extra) - 1] = '\0';
1216fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
1217fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
1218fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		   ifi->ifi_index, ifname, extra, ifi->ifi_flags,
12198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
12208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
12218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
12228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
12238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
12251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (if_indextoname(ifi->ifi_index, namebuf) &&
12261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    linux_iface_up(drv->global->ioctl_sock,
1227cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				   drv->first_bss->ifname) > 0) {
12281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
12291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "event since interface %s is up", namebuf);
12301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			return;
12311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
12328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Interface down");
12331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (drv->ignore_if_down_event) {
12341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
12351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "event generated by mode change");
12361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			drv->ignore_if_down_event = 0;
12371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		} else {
12381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			drv->if_disabled = 1;
12391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_supplicant_event(drv->ctx,
12401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					     EVENT_INTERFACE_DISABLED, NULL);
12411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
12428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
12451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (if_indextoname(ifi->ifi_index, namebuf) &&
12461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    linux_iface_up(drv->global->ioctl_sock,
1247cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				   drv->first_bss->ifname) == 0) {
12481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
12491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "event since interface %s is down",
12501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   namebuf);
1251cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		} else if (if_nametoindex(drv->first_bss->ifname) == 0) {
125204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
125304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				   "event since interface %s does not exist",
1254cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				   drv->first_bss->ifname);
125504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		} else if (drv->if_removed) {
125604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
125704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				   "event since interface %s is marked "
1258cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				   "removed", drv->first_bss->ifname);
12591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		} else {
12601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Interface up");
12611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			drv->if_disabled = 0;
12621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
12631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					     NULL);
12641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
12658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
12688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Some drivers send the association event before the operup event--in
12698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
12708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * fails. This will hit us when wpa_supplicant does not need to do
12718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * IEEE 802.1X authentication
12728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
12738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->operstate == 1 &&
12748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
1275fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	    !(ifi->ifi_flags & IFF_RUNNING)) {
1276fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
12771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
12788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       -1, IF_OPER_UP);
12798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1281fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (ifname[0])
1282fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_driver_nl80211_event_newlink(drv, ifname);
1283fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
12848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ifi->ifi_family == AF_BRIDGE && brid) {
12858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* device has been added to bridge */
12868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if_indextoname(brid, namebuf);
12878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
12888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   brid, namebuf);
12898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		add_ifidx(drv, brid);
12908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
12928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
12958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						 struct ifinfomsg *ifi,
12968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						 u8 *buf, size_t len)
12978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
12981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl80211_global *global = ctx;
12991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv;
1300fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int attrlen;
13018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rtattr *attr;
13028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 brid = 0;
1303fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	char ifname[IFNAMSIZ + 1];
13048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
13061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!drv) {
1307fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1308fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   ifi->ifi_index);
13091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
13101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
13111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1312fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	ifname[0] = '\0';
1313fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
13148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	attrlen = len;
13158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	attr = (struct rtattr *) buf;
13168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (RTA_OK(attr, attrlen)) {
1317fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		switch (attr->rta_type) {
1318fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case IFLA_IFNAME:
1319fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1320fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				break;
1321fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1322fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			ifname[RTA_PAYLOAD(attr)] = '\0';
1323fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
1324fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case IFLA_MASTER:
13258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			brid = nla_get_u32((struct nlattr *) attr);
1326fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
1327fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		}
13288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		attr = RTA_NEXT(attr, attrlen);
13298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
13308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1331fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (ifname[0])
1332fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_driver_nl80211_event_dellink(drv, ifname);
1333fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
13348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ifi->ifi_family == AF_BRIDGE && brid) {
13358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* device has been removed from bridge */
13368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		char namebuf[IFNAMSIZ];
13378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if_indextoname(brid, namebuf);
13388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
13398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "%s", brid, namebuf);
13408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		del_ifidx(drv, brid);
13418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
13428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
13438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
13468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    const u8 *frame, size_t len)
13478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
13488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const struct ieee80211_mgmt *mgmt;
13498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
13508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1351d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
13528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mgmt = (const struct ieee80211_mgmt *) frame;
13538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len < 24 + sizeof(mgmt->u.auth)) {
13548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
13558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "frame");
13568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
13578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
13588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
13608bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
13618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
13628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
13638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
1364d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	event.auth.auth_transaction =
1365d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		le_to_host16(mgmt->u.auth.auth_transaction);
13668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
13678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len > 24 + sizeof(mgmt->u.auth)) {
13688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.auth.ies = mgmt->u.auth.variable;
13698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
13708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
13718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
13738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
13748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
137687fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinenstatic unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
137787fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen{
137887fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	struct nl_msg *msg;
137987fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	int ret;
138087fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	struct nl80211_bss_info_arg arg;
138187fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
138287fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	os_memset(&arg, 0, sizeof(arg));
138387fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	msg = nlmsg_alloc();
138487fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	if (!msg)
138587fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		goto nla_put_failure;
138687fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
13871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
138887fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
138987fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
139087fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	arg.drv = drv;
139187fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
139287fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	msg = NULL;
139387fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	if (ret == 0) {
139487fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
139587fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen			   "associated BSS from scan results: %u MHz",
139687fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen			   arg.assoc_freq);
1397b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		if (arg.assoc_freq)
1398b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			drv->assoc_freq = arg.assoc_freq;
1399b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return drv->assoc_freq;
140087fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	}
140187fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
140287fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		   "(%s)", ret, strerror(-ret));
140387fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinennla_put_failure:
140487fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	nlmsg_free(msg);
140587fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	return drv->assoc_freq;
140687fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen}
140787fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
140887fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
14098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
14108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    const u8 *frame, size_t len)
14118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
14128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const struct ieee80211_mgmt *mgmt;
14138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
14148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 status;
14158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1416d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Associate event");
14178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mgmt = (const struct ieee80211_mgmt *) frame;
14188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
14198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
14208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "frame");
14218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
14228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
14238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	status = le_to_host16(mgmt->u.assoc_resp.status_code);
14258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (status != WLAN_STATUS_SUCCESS) {
14268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memset(&event, 0, sizeof(event));
14278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.assoc_reject.bssid = mgmt->bssid;
14288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
14298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			event.assoc_reject.resp_ies =
14308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				(u8 *) mgmt->u.assoc_resp.variable;
14318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			event.assoc_reject.resp_ies_len =
14328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				len - 24 - sizeof(mgmt->u.assoc_resp);
14338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
14348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.assoc_reject.status_code = status;
14358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
14378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
14388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
14398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->associated = 1;
14418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
14428bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
14438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
14458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
14468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
14478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.assoc_info.resp_ies_len =
14488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			len - 24 - sizeof(mgmt->u.assoc_resp);
14498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
14508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.assoc_info.freq = drv->assoc_freq;
145204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
14538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
14548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
14558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
14588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       enum nl80211_commands cmd, struct nlattr *status,
14598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       struct nlattr *addr, struct nlattr *req_ie,
14608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       struct nlattr *resp_ie)
14618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
14628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
14638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
14658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
14668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Avoid reporting two association events that would confuse
14678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * the core code.
14688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
14698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
14708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "when using userspace SME", cmd);
14718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
14728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
14738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1474d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (cmd == NL80211_CMD_CONNECT)
1475d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1476d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	else if (cmd == NL80211_CMD_ROAM)
1477d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1478d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
14798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
14808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cmd == NL80211_CMD_CONNECT &&
14818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
14828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (addr)
14838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			event.assoc_reject.bssid = nla_data(addr);
14848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (resp_ie) {
14858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			event.assoc_reject.resp_ies = nla_data(resp_ie);
14868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			event.assoc_reject.resp_ies_len = nla_len(resp_ie);
14878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
14888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.assoc_reject.status_code = nla_get_u16(status);
14898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
14908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
14918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
14928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->associated = 1;
14948bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	if (addr) {
14958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
14968bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
14978bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	}
14988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (req_ie) {
15008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.assoc_info.req_ies = nla_data(req_ie);
15018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.assoc_info.req_ies_len = nla_len(req_ie);
15028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (resp_ie) {
15048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.assoc_info.resp_ies = nla_data(resp_ie);
15058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.assoc_info.resp_ies_len = nla_len(resp_ie);
15068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
150887fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	event.assoc_info.freq = nl80211_get_assoc_freq(drv);
150987fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
15108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
15118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
15128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
1515c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt				  struct nlattr *reason, struct nlattr *addr,
1516c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt				  struct nlattr *by_ap)
15171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
15181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	union wpa_event_data data;
1519d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	unsigned int locally_generated = by_ap == NULL;
15201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
15211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
15221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/*
15231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * Avoid reporting two disassociation events that could
15241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * confuse the core code.
15251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 */
15261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
15271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "event when using userspace SME");
15281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
15291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
15301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1531d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (drv->ignore_next_local_disconnect) {
1532d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		drv->ignore_next_local_disconnect = 0;
1533d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		if (locally_generated) {
1534d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1535d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt				   "event triggered during reassociation");
1536d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			return;
1537d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		}
1538d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1539d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			   "disconnect but got another disconnect "
1540d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			   "event first");
1541d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	}
1542d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
1543d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
15448bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	nl80211_mark_disconnected(drv);
15451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&data, 0, sizeof(data));
15461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (reason)
154704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		data.deauth_info.reason_code = nla_get_u16(reason);
154804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	data.deauth_info.locally_generated = by_ap == NULL;
154904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
155004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt}
155104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
155204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
15539767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidtstatic int calculate_chan_offset(int width, int freq, int cf1, int cf2)
15549767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt{
15559767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	int freq1 = 0;
15569767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt
15579767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	switch (convert2width(width)) {
15589767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case CHAN_WIDTH_20_NOHT:
15599767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case CHAN_WIDTH_20:
15609767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		return 0;
15619767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case CHAN_WIDTH_40:
15629767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		freq1 = cf1 - 10;
15639767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		break;
15649767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case CHAN_WIDTH_80:
15659767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		freq1 = cf1 - 30;
15669767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		break;
15679767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case CHAN_WIDTH_160:
15689767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		freq1 = cf1 - 70;
15699767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		break;
15709767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case CHAN_WIDTH_UNKNOWN:
15719767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case CHAN_WIDTH_80P80:
15729767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		/* FIXME: implement this */
15739767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		return 0;
15749767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	}
15759767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt
15769767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
15779767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt}
15789767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt
15799767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt
158004949598a23f501be6eec21697465fd46a28840aDmitry Shmidtstatic void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
158104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt				 struct nlattr *ifindex, struct nlattr *freq,
158204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt				 struct nlattr *type, struct nlattr *bw,
158304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt				 struct nlattr *cf1, struct nlattr *cf2)
158404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt{
158504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	struct i802_bss *bss;
158604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	union wpa_event_data data;
158704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	int ht_enabled = 1;
158804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	int chan_offset = 0;
158904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	int ifidx;
159004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
159104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
159204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
159304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	if (!freq)
159404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		return;
159504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
159604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	ifidx = nla_get_u32(ifindex);
159704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	for (bss = drv->first_bss; bss; bss = bss->next)
159804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		if (bss->ifindex == ifidx)
159904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt			break;
160004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
160104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	if (bss == NULL) {
160204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
160304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt			   ifidx);
160404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		return;
160504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	}
160604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
160704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	if (type) {
160804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		switch (nla_get_u32(type)) {
160904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		case NL80211_CHAN_NO_HT:
161004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt			ht_enabled = 0;
161104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt			break;
161204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		case NL80211_CHAN_HT20:
161304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt			break;
161404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		case NL80211_CHAN_HT40PLUS:
161504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt			chan_offset = 1;
161604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt			break;
161704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		case NL80211_CHAN_HT40MINUS:
161804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt			chan_offset = -1;
161904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt			break;
162004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		}
16219767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	} else if (bw && cf1) {
16229767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		/* This can happen for example with VHT80 ch switch */
16239767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		chan_offset = calculate_chan_offset(nla_get_u32(bw),
16249767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt						    nla_get_u32(freq),
16259767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt						    nla_get_u32(cf1),
16269767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt						    cf2 ? nla_get_u32(cf2) : 0);
16279767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	} else {
16289767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
162904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	}
163004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
163104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	os_memset(&data, 0, sizeof(data));
163204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	data.ch_switch.freq = nla_get_u32(freq);
163304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	data.ch_switch.ht_enabled = ht_enabled;
163404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	data.ch_switch.ch_offset = chan_offset;
163504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	if (bw)
163604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
163704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	if (cf1)
163804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		data.ch_switch.cf1 = nla_get_u32(cf1);
163904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	if (cf2)
164004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		data.ch_switch.cf2 = nla_get_u32(cf2);
164104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
164204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	bss->freq = data.ch_switch.freq;
1643e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
164404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
16451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
16461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
16471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
16488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
16498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       enum nl80211_commands cmd, struct nlattr *addr)
16508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
16518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
16528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	enum wpa_event_type ev;
16538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (nla_len(addr) != ETH_ALEN)
16558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
16568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
16588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   cmd, MAC2STR((u8 *) nla_data(addr)));
16598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cmd == NL80211_CMD_AUTHENTICATE)
16618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ev = EVENT_AUTH_TIMED_OUT;
16628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else if (cmd == NL80211_CMD_ASSOCIATE)
16638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ev = EVENT_ASSOC_TIMED_OUT;
16648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
16658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
16668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
16688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
16698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, ev, &event);
16708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
16718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
167404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			    struct nlattr *freq, struct nlattr *sig,
167504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			    const u8 *frame, size_t len)
16768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
16778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const struct ieee80211_mgmt *mgmt;
16788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
16798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 fc, stype;
168004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	int ssi_signal = 0;
16815605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	int rx_freq = 0;
16828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16838da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
16848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mgmt = (const struct ieee80211_mgmt *) frame;
16858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len < 24) {
1686fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
16878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
16888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	fc = le_to_host16(mgmt->frame_control);
16918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	stype = WLAN_FC_GET_STYPE(fc);
16928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
169304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (sig)
169404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		ssi_signal = (s32) nla_get_u32(sig);
169504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
16968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
16978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (freq) {
1698fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		event.rx_mgmt.freq = nla_get_u32(freq);
1699fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
17008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
17015605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	wpa_printf(MSG_DEBUG,
17025605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		   "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
17035605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		   rx_freq, ssi_signal, stype, (unsigned int) len);
1704fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	event.rx_mgmt.frame = frame;
1705fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	event.rx_mgmt.frame_len = len;
1706fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	event.rx_mgmt.ssi_signal = ssi_signal;
1707fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
17088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
17098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
17121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				      struct nlattr *cookie, const u8 *frame,
17131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				      size_t len, struct nlattr *ack)
17148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
17158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
17168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const struct ieee80211_hdr *hdr;
17178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 fc;
17188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1719d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
17201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!is_ap_interface(drv->nlmode)) {
17211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		u64 cookie_val;
17228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (!cookie)
17241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			return;
17251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
17261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		cookie_val = nla_get_u64(cookie);
17271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
17281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   " cookie=0%llx%s (ack=%d)",
17291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   (long long unsigned int) cookie_val,
17301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   cookie_val == drv->send_action_cookie ?
17311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   " (match)" : " (unknown)", ack != NULL);
17321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (cookie_val != drv->send_action_cookie)
17331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			return;
17341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
17358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr = (const struct ieee80211_hdr *) frame;
17378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	fc = le_to_host16(hdr->frame_control);
17388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
17408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.type = WLAN_FC_GET_TYPE(fc);
17418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
17428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.dst = hdr->addr1;
17438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.data = frame;
17448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.data_len = len;
17458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.ack = ack != NULL;
17468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
17478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
17488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
17518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       enum wpa_event_type type,
17528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       const u8 *frame, size_t len)
17538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
17548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const struct ieee80211_mgmt *mgmt;
17558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
17568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *bssid = NULL;
17578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 reason_code = 0;
17588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1759d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (type == EVENT_DEAUTH)
1760d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1761d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	else
1762d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1763d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
17648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mgmt = (const struct ieee80211_mgmt *) frame;
17658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len >= 24) {
17668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		bssid = mgmt->bssid;
17678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17688bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
17698bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		    !drv->associated &&
17708bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		    os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
17718bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		    os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
17728bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		    os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
17738bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt			/*
17748bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt			 * Avoid issues with some roaming cases where
17758bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt			 * disconnection event for the old AP may show up after
17768bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt			 * we have started connection with the new AP.
17778bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt			 */
17788bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
17798bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt				   MAC2STR(bssid),
17808bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt				   MAC2STR(drv->auth_attempt_bssid));
17818bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt			return;
17828bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		}
17838bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
17848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (drv->associated != 0 &&
17858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
17868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
17878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			/*
17888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 * We have presumably received this deauth as a
17898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 * response to a clear_state_mismatch() outgoing
17908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 * deauth.  Don't let it take us offline!
17918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 */
17928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
17938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "from Unknown BSSID " MACSTR " -- ignoring",
17948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   MAC2STR(bssid));
17958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return;
17968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
17978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
17988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17998bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	nl80211_mark_disconnected(drv);
18008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
18018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Note: Same offset for Reason Code in both frame subtypes */
18038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len >= 24 + sizeof(mgmt->u.deauth))
18048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		reason_code = le_to_host16(mgmt->u.deauth.reason_code);
18058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (type == EVENT_DISASSOC) {
1807c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		event.disassoc_info.locally_generated =
1808cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			!os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
18098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.disassoc_info.addr = bssid;
18108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.disassoc_info.reason_code = reason_code;
18118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (frame + len > mgmt->u.disassoc.variable) {
18128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			event.disassoc_info.ie = mgmt->u.disassoc.variable;
18138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			event.disassoc_info.ie_len = frame + len -
18148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				mgmt->u.disassoc.variable;
18158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
18168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
1817c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		event.deauth_info.locally_generated =
1818cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			!os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
18198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.deauth_info.addr = bssid;
18208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.deauth_info.reason_code = reason_code;
18218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (frame + len > mgmt->u.deauth.variable) {
18228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			event.deauth_info.ie = mgmt->u.deauth.variable;
18238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			event.deauth_info.ie_len = frame + len -
18248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				mgmt->u.deauth.variable;
18258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
18268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, type, &event);
18298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
18308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
18338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					 enum wpa_event_type type,
18348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					 const u8 *frame, size_t len)
18358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
18368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const struct ieee80211_mgmt *mgmt;
18378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
18388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 reason_code = 0;
18398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1840d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (type == EVENT_UNPROT_DEAUTH)
1841d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1842d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	else
1843d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1844d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
18458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len < 24)
18468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
18478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mgmt = (const struct ieee80211_mgmt *) frame;
18498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
18518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Note: Same offset for Reason Code in both frame subtypes */
18528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len >= 24 + sizeof(mgmt->u.deauth))
18538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		reason_code = le_to_host16(mgmt->u.deauth.reason_code);
18548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (type == EVENT_UNPROT_DISASSOC) {
18568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.unprot_disassoc.sa = mgmt->sa;
18578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.unprot_disassoc.da = mgmt->da;
18588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.unprot_disassoc.reason_code = reason_code;
18598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
18608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.unprot_deauth.sa = mgmt->sa;
18618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.unprot_deauth.da = mgmt->da;
18628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.unprot_deauth.reason_code = reason_code;
18638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, type, &event);
18668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
18678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18698da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidtstatic void mlme_event(struct i802_bss *bss,
18708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       enum nl80211_commands cmd, struct nlattr *frame,
18718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       struct nlattr *addr, struct nlattr *timed_out,
18728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       struct nlattr *freq, struct nlattr *ack,
187304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		       struct nlattr *cookie, struct nlattr *sig)
18748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
18758da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
18768da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	const u8 *data;
18778da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	size_t len;
18788da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt
18798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (timed_out && addr) {
18808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mlme_timeout_event(drv, cmd, addr);
18818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
18828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (frame == NULL) {
188534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wpa_printf(MSG_DEBUG,
188634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   "nl80211: MLME event %d (%s) without frame data",
188734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   cmd, nl80211_command_to_string(cmd));
18888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
18898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18918da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	data = nla_data(frame);
18928da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	len = nla_len(frame);
1893ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	if (len < 4 + 2 * ETH_ALEN) {
189434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
189534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   MACSTR ") - too short",
189634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   cmd, nl80211_command_to_string(cmd), bss->ifname,
189734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   MAC2STR(bss->addr));
18988da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		return;
18998da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	}
190034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
190134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   ") A1=" MACSTR " A2=" MACSTR, cmd,
190234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   nl80211_command_to_string(cmd), bss->ifname,
190334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   MAC2STR(bss->addr), MAC2STR(data + 4),
190434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   MAC2STR(data + 4 + ETH_ALEN));
19058da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
1906ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	    os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1907ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	    os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
19088da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
19098da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			   "for foreign address", bss->ifname);
19108da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		return;
19118da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	}
19128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
19138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    nla_data(frame), nla_len(frame));
19148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (cmd) {
19168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_AUTHENTICATE:
19178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mlme_event_auth(drv, nla_data(frame), nla_len(frame));
19188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
19198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_ASSOCIATE:
19208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
19218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
19228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_DEAUTHENTICATE:
19238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
19248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   nla_data(frame), nla_len(frame));
19258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
19268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_DISASSOCIATE:
19278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
19288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   nla_data(frame), nla_len(frame));
19298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
19308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_FRAME:
193104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		mlme_event_mgmt(drv, freq, sig, nla_data(frame),
193204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				nla_len(frame));
19338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
19348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_FRAME_TX_STATUS:
19351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
19361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					  nla_len(frame), ack);
19378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
19388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_UNPROT_DEAUTHENTICATE:
19398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
19408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     nla_data(frame), nla_len(frame));
19418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
19428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_UNPROT_DISASSOCIATE:
19438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
19448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     nla_data(frame), nla_len(frame));
19458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
19468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
19478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
19488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
19508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1952a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidtstatic void mlme_event_michael_mic_failure(struct i802_bss *bss,
19538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   struct nlattr *tb[])
19548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
19558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data data;
19568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
19588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&data, 0, sizeof(data));
19598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_MAC]) {
19608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
19618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    nla_data(tb[NL80211_ATTR_MAC]),
19628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    nla_len(tb[NL80211_ATTR_MAC]));
19638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
19648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_KEY_SEQ]) {
19668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
19678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    nla_data(tb[NL80211_ATTR_KEY_SEQ]),
19688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    nla_len(tb[NL80211_ATTR_KEY_SEQ]));
19698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_KEY_TYPE]) {
19718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		enum nl80211_key_type key_type =
19728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
19738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
19748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (key_type == NL80211_KEYTYPE_PAIRWISE)
19758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			data.michael_mic_failure.unicast = 1;
19768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else
19778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data.michael_mic_failure.unicast = 1;
19788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_KEY_IDX]) {
19808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
19818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
19828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1984a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
19858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
19868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
19898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 struct nlattr *tb[])
19908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
19918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_MAC] == NULL) {
19928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
19938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "event");
19948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
19958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1997c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt
19988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->associated = 1;
19998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
20008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(drv->bssid));
20018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
20038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
20048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
20078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					 int cancel_event, struct nlattr *tb[])
20088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
20098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	unsigned int freq, chan_type, duration;
20108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data data;
20118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u64 cookie;
20128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_WIPHY_FREQ])
20148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
20158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
20168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		freq = 0;
20178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
20198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
20208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
20218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		chan_type = 0;
20228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_DURATION])
20248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
20258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
20268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		duration = 0;
20278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_COOKIE])
20298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
20308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
20318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cookie = 0;
20328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
20348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
20358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   cancel_event, freq, chan_type, duration,
20368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (long long unsigned int) cookie,
20378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
20388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cookie != drv->remain_on_chan_cookie)
20408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return; /* not for us */
20418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (cancel_event)
20431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->pending_remain_on_chan = 0;
20448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&data, 0, sizeof(data));
20468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	data.remain_on_channel.freq = freq;
20478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	data.remain_on_channel.duration = duration;
20488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, cancel_event ?
20498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     EVENT_CANCEL_REMAIN_ON_CHANNEL :
20508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     EVENT_REMAIN_ON_CHANNEL, &data);
20518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
20528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2054700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidtstatic void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2055700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt				struct nlattr *tb[])
2056700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt{
2057700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	union wpa_event_data data;
2058700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
2059700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	os_memset(&data, 0, sizeof(data));
2060700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
2061700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	if (tb[NL80211_ATTR_IE]) {
2062700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2063700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2064700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	}
2065700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
2066700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	if (tb[NL80211_ATTR_IE_RIC]) {
2067700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2068700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2069700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	}
2070700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
2071700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	if (tb[NL80211_ATTR_MAC])
2072700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		os_memcpy(data.ft_ies.target_ap,
2073700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			  nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2074700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
2075700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2076700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		   MAC2STR(data.ft_ies.target_ap));
2077700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
2078700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2079700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt}
2080700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
2081700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
20828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
20838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    struct nlattr *tb[])
20848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
20858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
20868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *nl;
20878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int rem;
20888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct scan_info *info;
20898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define MAX_REPORT_FREQS 50
20908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int freqs[MAX_REPORT_FREQS];
20918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int num_freqs = 0;
20928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->scan_for_auth) {
20941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->scan_for_auth = 0;
20951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
20961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "cfg80211 BSS entry");
20971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_driver_nl80211_authenticate_retry(drv);
20981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
20991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
21001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
21018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
21028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	info = &event.scan_info;
21038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	info->aborted = aborted;
21048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_SCAN_SSIDS]) {
21068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
21078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			struct wpa_driver_scan_ssid *s =
21088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				&info->ssids[info->num_ssids];
21098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			s->ssid = nla_data(nl);
21108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			s->ssid_len = nla_len(nl);
2111fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2112fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				   wpa_ssid_txt(s->ssid, s->ssid_len));
21138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			info->num_ssids++;
21148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
21158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				break;
21168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
21178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
2119fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		char msg[200], *pos, *end;
2120fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		int res;
2121fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
2122fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		pos = msg;
2123fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		end = pos + sizeof(msg);
2124fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		*pos = '\0';
2125fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
21268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
21278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		{
21288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			freqs[num_freqs] = nla_get_u32(nl);
2129fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			res = os_snprintf(pos, end - pos, " %d",
2130fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt					  freqs[num_freqs]);
2131fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			if (res > 0 && end - pos > res)
2132fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				pos += res;
21338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			num_freqs++;
21348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (num_freqs == MAX_REPORT_FREQS - 1)
21358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				break;
21368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
21378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		info->freqs = freqs;
21388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		info->num_freqs = num_freqs;
2139fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2140fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   msg);
21418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
21438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
21448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int get_link_signal(struct nl_msg *msg, void *arg)
21478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
21488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
21498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
21508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
21518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
21528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
215334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		[NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
21548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
21558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
21568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
21578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
21588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
21598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
21608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
21618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
21628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_signal_info *sig_change = arg;
21638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
21658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
21668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!tb[NL80211_ATTR_STA_INFO] ||
21678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
21688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     tb[NL80211_ATTR_STA_INFO], policy))
21698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
21708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!sinfo[NL80211_STA_INFO_SIGNAL])
21718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
21728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sig_change->current_signal =
21748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		(s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
21758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
217634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
217734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		sig_change->avg_signal =
217834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			(s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
217934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	else
218034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		sig_change->avg_signal = 0;
218134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
21828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
21838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
21848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     sinfo[NL80211_STA_INFO_TX_BITRATE],
21858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     rate_policy)) {
21868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			sig_change->current_txrate = 0;
21878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		} else {
21888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (rinfo[NL80211_RATE_INFO_BITRATE]) {
21898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				sig_change->current_txrate =
21908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					nla_get_u16(rinfo[
21918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     NL80211_RATE_INFO_BITRATE]) * 100;
21928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
21938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
21948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
21978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
21988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
22018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   struct wpa_signal_info *sig)
22028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
22038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
22048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sig->current_signal = -9999;
22068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sig->current_txrate = 0;
22078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
22098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
22108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
22118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
22138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
22158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
22168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return send_and_recv_msgs(drv, msg, get_link_signal, sig);
22188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
22191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
22208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
22218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
22228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int get_link_noise(struct nl_msg *msg, void *arg)
22258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
22268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
22278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
22288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
22298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
22308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
22318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
22328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
22338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_signal_info *sig_change = arg;
22348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
22368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
22378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!tb[NL80211_ATTR_SURVEY_INFO]) {
22398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
22408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
22418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
22428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
22448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     tb[NL80211_ATTR_SURVEY_INFO],
22458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     survey_policy)) {
22468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
22478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "attributes!");
22488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
22498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
22508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
22528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
22538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
22558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    sig_change->frequency)
22568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
22578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!sinfo[NL80211_SURVEY_INFO_NOISE])
22598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
22608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sig_change->current_noise =
22628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		(s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
22638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
22658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
22668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
22698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  struct wpa_signal_info *sig_change)
22708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
22718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
22728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sig_change->current_noise = 9999;
22748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sig_change->frequency = drv->assoc_freq;
22758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
22778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
22788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
22798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
22818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
22838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
22858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
22861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
22871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return -ENOBUFS;
22881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
22891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
22901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
22911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
22921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
22931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
22941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
22951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
22961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
22971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
22981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
22991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	};
23001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_scan_results *scan_results = arg;
23011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_scan_res *scan_res;
23021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	size_t i;
23031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
23051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
23061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!tb[NL80211_ATTR_SURVEY_INFO]) {
23081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
23091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NL_SKIP;
23101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
23111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
23131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			     tb[NL80211_ATTR_SURVEY_INFO],
23141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			     survey_policy)) {
23151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
23161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "attributes");
23171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NL_SKIP;
23181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
23191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!sinfo[NL80211_SURVEY_INFO_NOISE])
23211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NL_SKIP;
23221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
23241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NL_SKIP;
23251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	for (i = 0; i < scan_results->num; ++i) {
23271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		scan_res = scan_results->res[i];
23281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (!scan_res)
23291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			continue;
23301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
23311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    scan_res->freq)
23321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			continue;
23331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
23341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			continue;
23351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		scan_res->noise = (s8)
23361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
23371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
23381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
23391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return NL_SKIP;
23411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
23421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_get_noise_for_scan_results(
23451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv,
23461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_scan_results *scan_res)
23471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
23481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
23491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
23511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
23521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -ENOMEM;
23531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
23551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
23571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
23581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
23591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  scan_res);
23601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt nla_put_failure:
23611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
23628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
23638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
23648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
23678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			      struct nlattr *tb[])
23688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
23698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
23708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
23718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
23728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
23738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
23748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
23758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
23768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	enum nl80211_cqm_rssi_threshold_event event;
23778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data ed;
23788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_signal_info sig;
23798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int res;
23808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_CQM] == NULL ||
23828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
23838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     cqm_policy)) {
23848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
23858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
23868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&ed, 0, sizeof(ed));
23898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
23918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!tb[NL80211_ATTR_MAC])
23928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return;
23938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
23948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  ETH_ALEN);
23958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
23968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
23978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
24008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
24018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
24028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
24048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
24058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "event: RSSI high");
24068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ed.signal_change.above_threshold = 1;
24078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
24088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
24098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "event: RSSI low");
24108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ed.signal_change.above_threshold = 0;
24118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else
24128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
24138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	res = nl80211_get_link_signal(drv, &sig);
24158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (res == 0) {
24168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ed.signal_change.current_signal = sig.current_signal;
24178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ed.signal_change.current_txrate = sig.current_txrate;
24188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm  txrate: %d",
24198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   sig.current_signal, sig.current_txrate);
24208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
24218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	res = nl80211_get_link_noise(drv, &sig);
24238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (res == 0) {
24248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ed.signal_change.current_noise = sig.current_noise;
24258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
24268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   sig.current_noise);
24278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
24288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
24308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
24348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      struct nlattr **tb)
24358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *addr;
24378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data data;
24388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_MAC] == NULL)
24408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
24418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr = nla_data(tb[NL80211_ATTR_MAC]);
24428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
2443c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt
24441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
2445c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		u8 *ies = NULL;
2446c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		size_t ies_len = 0;
2447c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		if (tb[NL80211_ATTR_IE]) {
2448c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt			ies = nla_data(tb[NL80211_ATTR_IE]);
2449c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt			ies_len = nla_len(tb[NL80211_ATTR_IE]);
2450c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		}
2451c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2452c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2453c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		return;
2454c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt	}
2455c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt
24568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->nlmode != NL80211_IFTYPE_ADHOC)
24578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
24588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&data, 0, sizeof(data));
24608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
24618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
24628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
24668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      struct nlattr **tb)
24678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *addr;
24698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data data;
24708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_MAC] == NULL)
24728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
24738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr = nla_data(tb[NL80211_ATTR_MAC]);
24748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
24758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(addr));
2476c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt
24771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
2478c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		drv_event_disassoc(drv->ctx, addr);
2479c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		return;
2480c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt	}
2481c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt
24828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->nlmode != NL80211_IFTYPE_ADHOC)
24838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
24848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&data, 0, sizeof(data));
24868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
24878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
24888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
24921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					struct nlattr **tb)
24938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
24951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
24961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_REKEY_DATA_KEK] = {
24971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			.minlen = NL80211_KEK_LEN,
24981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			.maxlen = NL80211_KEK_LEN,
24991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		},
25001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_REKEY_DATA_KCK] = {
25011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			.minlen = NL80211_KCK_LEN,
25021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			.maxlen = NL80211_KCK_LEN,
25031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		},
25041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_REKEY_DATA_REPLAY_CTR] = {
25051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			.minlen = NL80211_REPLAY_CTR_LEN,
25061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			.maxlen = NL80211_REPLAY_CTR_LEN,
25071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		},
25081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	};
25098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data data;
25108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
25111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!tb[NL80211_ATTR_MAC])
25121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
25131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!tb[NL80211_ATTR_REKEY_DATA])
25141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
25151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
25161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			     tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
25171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
25181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
25191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
25208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
25211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&data, 0, sizeof(data));
25221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
25231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
25241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   MAC2STR(data.driver_gtk_rekey.bssid));
25251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	data.driver_gtk_rekey.replay_ctr =
25261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
25271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
25281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
25291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
25301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
25311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
25321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
25331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
25341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					  struct nlattr **tb)
25351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
25361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
25371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
25381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
25391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_PMKSA_CANDIDATE_BSSID] = {
25401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			.minlen = ETH_ALEN,
25411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			.maxlen = ETH_ALEN,
25421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		},
25431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
25441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	};
25451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	union wpa_event_data data;
25461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2547d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2548d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
25491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
25501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
25511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
25521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			     tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
25531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
25541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
25551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    !cand[NL80211_PMKSA_CANDIDATE_BSSID])
25561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
25571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
25581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&data, 0, sizeof(data));
25591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memcpy(data.pmkid_candidate.bssid,
25601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
25611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	data.pmkid_candidate.index =
25621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
25631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	data.pmkid_candidate.preauth =
25641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
25651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
25661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
25671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
25681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
25691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
25701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				       struct nlattr **tb)
25711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
25721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	union wpa_event_data data;
25731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2574d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2575d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
25761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
25771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
25781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
25791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&data, 0, sizeof(data));
25801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memcpy(data.client_poll.addr,
25811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
25821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
25831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
25841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
25851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
25861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2587d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidtstatic void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2588d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt				    struct nlattr **tb)
2589d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt{
2590d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	union wpa_event_data data;
2591d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
2592d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2593d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
2594d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2595d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		return;
2596d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
2597d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	os_memset(&data, 0, sizeof(data));
2598d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2599d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2600d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	case NL80211_TDLS_SETUP:
2601d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2602d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			   MACSTR, MAC2STR(data.tdls.peer));
2603d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		data.tdls.oper = TDLS_REQUEST_SETUP;
2604d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		break;
2605d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	case NL80211_TDLS_TEARDOWN:
2606d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2607d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			   MACSTR, MAC2STR(data.tdls.peer));
2608d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2609d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		break;
2610d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	default:
2611d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2612d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			   "event");
2613d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		return;
2614d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	}
2615d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (tb[NL80211_ATTR_REASON_CODE]) {
2616d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		data.tdls.reason_code =
2617d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2618d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	}
2619d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
2620d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2621d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt}
2622d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
2623d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
26245393a0f77c5281735888bd9dcf8d8abeb5961461Dmitry Shmidtstatic void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
26255393a0f77c5281735888bd9dcf8d8abeb5961461Dmitry Shmidt			    struct nlattr **tb)
26265393a0f77c5281735888bd9dcf8d8abeb5961461Dmitry Shmidt{
26275393a0f77c5281735888bd9dcf8d8abeb5961461Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
26285393a0f77c5281735888bd9dcf8d8abeb5961461Dmitry Shmidt}
26295393a0f77c5281735888bd9dcf8d8abeb5961461Dmitry Shmidt
26305393a0f77c5281735888bd9dcf8d8abeb5961461Dmitry Shmidt
2631f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidtstatic void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2632f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt					 struct nlattr **tb)
2633f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt{
2634f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	union wpa_event_data data;
2635f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	u32 reason;
2636f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2637f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2638f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2639f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2640f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		return;
2641f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2642f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	os_memset(&data, 0, sizeof(data));
2643f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	os_memcpy(data.connect_failed_reason.addr,
2644f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		  nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2645f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2646f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2647f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	switch (reason) {
2648f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	case NL80211_CONN_FAIL_MAX_CLIENTS:
2649f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2650f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2651f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		break;
2652f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2653f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2654f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			   " tried to connect",
2655f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			   MAC2STR(data.connect_failed_reason.addr));
2656f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		data.connect_failed_reason.code = BLOCKED_CLIENT;
2657f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		break;
2658f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	default:
2659f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2660f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			   "%u", reason);
2661f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		return;
2662f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	}
2663f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2664f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2665f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt}
2666f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2667f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
2668ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidtstatic void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2669ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt				struct nlattr **tb)
2670ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt{
2671ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	union wpa_event_data data;
2672ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	enum nl80211_radar_event event_type;
2673ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
2674ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2675ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		return;
2676ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
2677ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	os_memset(&data, 0, sizeof(data));
2678051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2679051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
2680051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
2681051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	/* Check HT params */
2682051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2683051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		data.dfs_event.ht_enabled = 1;
2684051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		data.dfs_event.chan_offset = 0;
2685051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
2686051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2687051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		case NL80211_CHAN_NO_HT:
2688051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			data.dfs_event.ht_enabled = 0;
2689051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
2690051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		case NL80211_CHAN_HT20:
2691051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
2692051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		case NL80211_CHAN_HT40PLUS:
2693051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			data.dfs_event.chan_offset = 1;
2694051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
2695051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		case NL80211_CHAN_HT40MINUS:
2696051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			data.dfs_event.chan_offset = -1;
2697051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
2698051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		}
2699051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	}
2700ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
2701051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	/* Get VHT params */
2702cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2703cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		data.dfs_event.chan_width =
2704cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			convert2width(nla_get_u32(
2705cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt					      tb[NL80211_ATTR_CHANNEL_WIDTH]));
2706cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (tb[NL80211_ATTR_CENTER_FREQ1])
2707cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
2708051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	if (tb[NL80211_ATTR_CENTER_FREQ2])
2709051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2710051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
2711051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2712051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		   data.dfs_event.freq, data.dfs_event.ht_enabled,
2713051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		   data.dfs_event.chan_offset, data.dfs_event.chan_width,
2714051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		   data.dfs_event.cf1, data.dfs_event.cf2);
2715ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
2716ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	switch (event_type) {
2717ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	case NL80211_RADAR_DETECTED:
2718ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2719ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		break;
2720ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	case NL80211_RADAR_CAC_FINISHED:
2721ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2722ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		break;
2723ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	case NL80211_RADAR_CAC_ABORTED:
2724ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2725ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		break;
2726ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	case NL80211_RADAR_NOP_FINISHED:
2727ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2728ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		break;
2729ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	default:
2730ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2731ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt			   "received", event_type);
2732ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		break;
2733ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	}
2734ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt}
2735ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
2736ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
27371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
27381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   int wds)
27391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
27401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
27411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	union wpa_event_data event;
27421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
27431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!tb[NL80211_ATTR_MAC])
27441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
27451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
27461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&event, 0, sizeof(event));
27471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	event.rx_from_unknown.bssid = bss->addr;
27481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
27491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	event.rx_from_unknown.wds = wds;
27508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
27521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
27531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
27541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2755cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidtstatic void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2756cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt				   const u8 *data, size_t len)
2757cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt{
2758cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	u32 i, count;
2759cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	union wpa_event_data event;
2760cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	struct wpa_freq_range *range = NULL;
2761cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	const struct qca_avoid_freq_list *freq_range;
2762cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt
2763cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	freq_range = (const struct qca_avoid_freq_list *) data;
2764cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	if (len < sizeof(freq_range->count))
2765cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		return;
2766cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt
2767cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	count = freq_range->count;
2768cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	if (len < sizeof(freq_range->count) +
2769cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	    count * sizeof(struct qca_avoid_freq_range)) {
2770cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2771cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt			   (unsigned int) len);
2772cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		return;
2773cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	}
2774cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt
2775cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	if (count > 0) {
2776cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		range = os_calloc(count, sizeof(struct wpa_freq_range));
2777cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		if (range == NULL)
2778cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt			return;
2779cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	}
2780cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt
2781cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	os_memset(&event, 0, sizeof(event));
2782cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	for (i = 0; i < count; i++) {
2783cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		unsigned int idx = event.freq_range.num;
2784cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		range[idx].min = freq_range->range[i].start_freq;
2785cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		range[idx].max = freq_range->range[i].end_freq;
2786cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2787cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt			   range[idx].min, range[idx].max);
2788cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		if (range[idx].min > range[idx].max) {
2789cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2790cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt			continue;
2791cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		}
2792cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		event.freq_range.num++;
2793cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	}
2794cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	event.freq_range.range = range;
2795cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt
2796cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2797cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt
2798cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	os_free(range);
2799cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt}
2800cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt
2801cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt
2802cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidtstatic void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2803cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt				     u32 subcmd, u8 *data, size_t len)
2804cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt{
2805cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	switch (subcmd) {
2806cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2807cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		qca_nl80211_avoid_freq(drv, data, len);
2808cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		break;
2809cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	default:
2810cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		wpa_printf(MSG_DEBUG,
2811cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt			   "nl80211: Ignore unsupported QCA vendor event %u",
2812cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt			   subcmd);
2813cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		break;
2814cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	}
2815cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt}
2816cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt
2817cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt
2818fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2819fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				 struct nlattr **tb)
2820fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt{
2821fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	u32 vendor_id, subcmd, wiphy = 0;
2822fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int wiphy_idx;
2823fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	u8 *data = NULL;
2824fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	size_t len = 0;
2825fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
2826fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (!tb[NL80211_ATTR_VENDOR_ID] ||
2827fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	    !tb[NL80211_ATTR_VENDOR_SUBCMD])
2828fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return;
2829fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
2830fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2831fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2832fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
2833fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (tb[NL80211_ATTR_WIPHY])
2834fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2835fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
2836fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2837fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		   wiphy, vendor_id, subcmd);
2838fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
2839fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (tb[NL80211_ATTR_VENDOR_DATA]) {
2840fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2841fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2842fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2843fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
2844fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
2845fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2846fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2847fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2848fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   wiphy, wiphy_idx);
2849fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return;
2850fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
2851fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
2852fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	switch (vendor_id) {
2853cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt	case OUI_QCA:
2854cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		nl80211_vendor_event_qca(drv, subcmd, data, len);
2855cf32e60fa7e0d33fe1551a6dba8dcbbec47ea50eDmitry Shmidt		break;
2856fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	default:
2857fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2858fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		break;
2859fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
2860fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt}
2861fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
2862fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
2863a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidtstatic void do_process_drv_event(struct i802_bss *bss, int cmd,
2864a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt				 struct nlattr **tb)
28651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
2866a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
2867e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	union wpa_event_data data;
2868a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt
286934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
287034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   cmd, nl80211_command_to_string(cmd), bss->ifname);
287134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
28721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
28731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
28741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	     cmd == NL80211_CMD_SCAN_ABORTED)) {
2875cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		wpa_driver_nl80211_set_mode(drv->first_bss,
28761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					    drv->ap_scan_as_station);
28771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
28788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
28798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	switch (cmd) {
28818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_TRIGGER_SCAN:
2882700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
28835605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		drv->scan_state = SCAN_STARTED;
2884fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
28858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
28861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_CMD_START_SCHED_SCAN:
2887700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
28885605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		drv->scan_state = SCHED_SCAN_STARTED;
28891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
28901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_CMD_SCHED_SCAN_STOPPED:
2891700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
28925605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		drv->scan_state = SCHED_SCAN_STOPPED;
28931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
28941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
28958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_NEW_SCAN_RESULTS:
2896700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		wpa_dbg(drv->ctx, MSG_DEBUG,
2897700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			"nl80211: New scan results available");
28985605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		drv->scan_state = SCAN_COMPLETED;
28998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		drv->scan_complete_events = 1;
29008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
29018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     drv->ctx);
29028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		send_scan_event(drv, 0, tb);
29038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_CMD_SCHED_SCAN_RESULTS:
2905700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		wpa_dbg(drv->ctx, MSG_DEBUG,
2906700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			"nl80211: New sched scan results available");
29075605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		drv->scan_state = SCHED_SCAN_RESULTS;
29081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		send_scan_event(drv, 0, tb);
29091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
29108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_SCAN_ABORTED:
2911700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
29125605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		drv->scan_state = SCAN_ABORTED;
29138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
29148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Need to indicate that scan results are available in order
29158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * not to make wpa_supplicant stop its scanning.
29168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
29178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
29188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     drv->ctx);
29198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		send_scan_event(drv, 1, tb);
29208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_AUTHENTICATE:
29228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_ASSOCIATE:
29238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_DEAUTHENTICATE:
29248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_DISASSOCIATE:
29258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_FRAME_TX_STATUS:
29268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_UNPROT_DEAUTHENTICATE:
29278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_UNPROT_DISASSOCIATE:
29288da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
29298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
29308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
293104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			   tb[NL80211_ATTR_COOKIE],
293204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			   tb[NL80211_ATTR_RX_SIGNAL_DBM]);
29338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_CONNECT:
29358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_ROAM:
29361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		mlme_event_connect(drv, cmd,
29378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   tb[NL80211_ATTR_STATUS_CODE],
29388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   tb[NL80211_ATTR_MAC],
29398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   tb[NL80211_ATTR_REQ_IE],
29408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   tb[NL80211_ATTR_RESP_IE]);
29418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
294204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	case NL80211_CMD_CH_SWITCH_NOTIFY:
294304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		mlme_event_ch_switch(drv,
294404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt				     tb[NL80211_ATTR_IFINDEX],
294504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt				     tb[NL80211_ATTR_WIPHY_FREQ],
294604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt				     tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
294704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt				     tb[NL80211_ATTR_CHANNEL_WIDTH],
294804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt				     tb[NL80211_ATTR_CENTER_FREQ1],
294904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt				     tb[NL80211_ATTR_CENTER_FREQ2]);
295004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		break;
29518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_DISCONNECT:
29521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
2953c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt				      tb[NL80211_ATTR_MAC],
2954c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt				      tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
29558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_MICHAEL_MIC_FAILURE:
2957a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		mlme_event_michael_mic_failure(bss, tb);
29588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_JOIN_IBSS:
29608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mlme_event_join_ibss(drv, tb);
29618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_REMAIN_ON_CHANNEL:
29638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mlme_event_remain_on_channel(drv, 0, tb);
29648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
29668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mlme_event_remain_on_channel(drv, 1, tb);
29678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_NOTIFY_CQM:
29698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_cqm_event(drv, tb);
29708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_REG_CHANGE:
29728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2973e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
2974e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			break;
2975e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		os_memset(&data, 0, sizeof(data));
2976e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		switch (nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])) {
2977e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		case NL80211_REGDOM_SET_BY_CORE:
2978e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			data.channel_list_changed.initiator =
2979e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				REGDOM_SET_BY_CORE;
2980e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			break;
2981e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		case NL80211_REGDOM_SET_BY_USER:
2982e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			data.channel_list_changed.initiator =
2983e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				REGDOM_SET_BY_USER;
2984e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			break;
2985e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		case NL80211_REGDOM_SET_BY_DRIVER:
2986e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			data.channel_list_changed.initiator =
2987e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				REGDOM_SET_BY_DRIVER;
2988e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			break;
2989e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2990e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			data.channel_list_changed.initiator =
2991e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				REGDOM_SET_BY_COUNTRY_IE;
2992e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			break;
2993e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		default:
2994e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Unknown reg change initiator %d received",
2995e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				   nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]));
2996e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			break;
2997e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		}
29988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2999e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				     &data);
30008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
30018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_REG_BEACON_HINT:
30028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
30039767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		os_memset(&data, 0, sizeof(data));
30049767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
30058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
30069767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt				     &data);
30078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
30088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_NEW_STATION:
30098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_new_station_event(drv, tb);
30108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
30118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case NL80211_CMD_DEL_STATION:
30128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_del_station_event(drv, tb);
30138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
30141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_CMD_SET_REKEY_OFFLOAD:
30151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_rekey_offload_event(drv, tb);
30161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
30171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_CMD_PMKSA_CANDIDATE:
30181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_pmksa_candidate_event(drv, tb);
30191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
30201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_CMD_PROBE_CLIENT:
30211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_client_probe_event(drv, tb);
30221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
3023d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	case NL80211_CMD_TDLS_OPER:
3024d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		nl80211_tdls_oper_event(drv, tb);
3025d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		break;
3026f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	case NL80211_CMD_CONN_FAILED:
3027f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		nl80211_connect_failed_event(drv, tb);
3028f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		break;
3029700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	case NL80211_CMD_FT_EVENT:
3030700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		mlme_event_ft_event(drv, tb);
3031700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		break;
3032ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	case NL80211_CMD_RADAR_DETECT:
3033ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		nl80211_radar_event(drv, tb);
3034ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		break;
30355393a0f77c5281735888bd9dcf8d8abeb5961461Dmitry Shmidt	case NL80211_CMD_STOP_AP:
30365393a0f77c5281735888bd9dcf8d8abeb5961461Dmitry Shmidt		nl80211_stop_ap(drv, tb);
30375393a0f77c5281735888bd9dcf8d8abeb5961461Dmitry Shmidt		break;
3038fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case NL80211_CMD_VENDOR:
3039fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		nl80211_vendor_event(drv, tb);
3040fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		break;
30411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	default:
3042700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3043700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			"(cmd=%d)", cmd);
30441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
30451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
30461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
30471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
30481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
30491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int process_drv_event(struct nl_msg *msg, void *arg)
30501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
30511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = arg;
30521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
30531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
3054a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	struct i802_bss *bss;
3055a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	int ifidx = -1;
30561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
30571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
30581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
30591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
306034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (tb[NL80211_ATTR_IFINDEX]) {
3061a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3062a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt
3063cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		for (bss = drv->first_bss; bss; bss = bss->next)
306434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			if (ifidx == -1 || ifidx == bss->ifindex) {
306534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				do_process_drv_event(bss, gnlh->cmd, tb);
306634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				return NL_SKIP;
306734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			}
306834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wpa_printf(MSG_DEBUG,
306934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
307034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   gnlh->cmd, ifidx);
307134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	} else if (tb[NL80211_ATTR_WDEV]) {
307234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
307334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
3074cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		for (bss = drv->first_bss; bss; bss = bss->next) {
307534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
307634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				do_process_drv_event(bss, gnlh->cmd, tb);
307734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				return NL_SKIP;
307834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			}
30791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
308034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wpa_printf(MSG_DEBUG,
308134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
308234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   gnlh->cmd, (long long unsigned int) wdev_id);
30831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
30841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
30851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return NL_SKIP;
30861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
30871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
30881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
30891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int process_global_event(struct nl_msg *msg, void *arg)
30901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
30911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl80211_global *global = arg;
30921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
30931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
309404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	struct wpa_driver_nl80211_data *drv, *tmp;
30951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ifidx = -1;
3096a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	struct i802_bss *bss;
309734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	u64 wdev_id = 0;
309834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	int wdev_id_set = 0;
30991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
31001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
31011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
31021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
31031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (tb[NL80211_ATTR_IFINDEX])
31041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
310534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	else if (tb[NL80211_ATTR_WDEV]) {
310634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
310734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wdev_id_set = 1;
310834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	}
31091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
311004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	dl_list_for_each_safe(drv, tmp, &global->interfaces,
311104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			      struct wpa_driver_nl80211_data, list) {
3112cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		for (bss = drv->first_bss; bss; bss = bss->next) {
311334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			if ((ifidx == -1 && !wdev_id_set) ||
311434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			    ifidx == bss->ifindex ||
311534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			    (wdev_id_set && bss->wdev_id_set &&
311634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			     wdev_id == bss->wdev_id)) {
3117a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt				do_process_drv_event(bss, gnlh->cmd, tb);
3118a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt				return NL_SKIP;
3119a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			}
3120a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		}
31211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
31221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
31231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return NL_SKIP;
31241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
31251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
31261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
31271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int process_bss_event(struct nl_msg *msg, void *arg)
31281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
31291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *bss = arg;
31301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
31311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
31321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
31331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
31341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
31351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
313634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
313734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
313834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   bss->ifname);
313934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
31401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	switch (gnlh->cmd) {
31411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_CMD_FRAME:
31421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_CMD_FRAME_TX_STATUS:
31438da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
31441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
31451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
314604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			   tb[NL80211_ATTR_COOKIE],
314704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			   tb[NL80211_ATTR_RX_SIGNAL_DBM]);
31481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
31491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_CMD_UNEXPECTED_FRAME:
31501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_spurious_frame(bss, tb, 0);
31511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
31521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
31531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_spurious_frame(bss, tb, 1);
31541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
31558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
31568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
31578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(cmd=%d)", gnlh->cmd);
31588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
31598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
31608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
31628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
31638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
31668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     void *handle)
31678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
31681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_cb *cb = eloop_ctx;
316968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	int res;
31708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31718da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
31728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
317368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	res = nl_recvmsgs(handle, cb);
317468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	if (res) {
317568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
317668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   __func__, res);
317768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	}
31788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
31798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
31828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
31838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @priv: driver_nl80211 private data
31848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @alpha2_arg: country to which to switch to
31858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 0 on success, -1 on failure
31868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
31878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This asks nl80211 to set the regulatory domain for given
31888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * country ISO / IEC alpha2.
31898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
31908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
31918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
31928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
31938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
31948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char alpha2[3];
31958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
31968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
31988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
31998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
32008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	alpha2[0] = alpha2_arg[0];
32028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	alpha2[1] = alpha2_arg[1];
32038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	alpha2[2] = '\0';
32048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
32068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
32088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (send_and_recv_msgs(drv, msg, NULL, NULL))
32098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -EINVAL;
32108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
32118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
32121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
32138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -EINVAL;
32148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
32158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3217cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtstatic int nl80211_get_country(struct nl_msg *msg, void *arg)
3218cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt{
3219cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	char *alpha2 = arg;
3220cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3221cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3222cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
3223cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3224cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
3225cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3226cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3227cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		return NL_SKIP;
3228cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	}
3229cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3230cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	return NL_SKIP;
3231cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
3232cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
3233cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
3234cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtstatic int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3235cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt{
3236cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	struct i802_bss *bss = priv;
3237cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
3238cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	struct nl_msg *msg;
3239cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	int ret;
3240cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
3241cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	msg = nlmsg_alloc();
3242cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (!msg)
3243cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		return -ENOMEM;
3244cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
3245cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3246cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	alpha2[0] = '\0';
3247cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3248cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (!alpha2[0])
3249cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		ret = -1;
3250cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
3251cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	return ret;
3252cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
3253cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
3254cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
32552f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic int protocol_feature_handler(struct nl_msg *msg, void *arg)
32562f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
32572f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	u32 *feat = arg;
32582f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
32592f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
32602f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
32612f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
32622f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
32632f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
32642f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
32652f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		*feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
32662f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
32672f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	return NL_SKIP;
32682f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
32692f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
32702f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
32712f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
32722f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
32732f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	u32 feat = 0;
32742f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nl_msg *msg;
32752f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
32762f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	msg = nlmsg_alloc();
32772f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (!msg)
32782f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		goto nla_put_failure;
32792f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
32802f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
32812f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
32822f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return feat;
32832f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
32842f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	msg = NULL;
32852f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtnla_put_failure:
32862f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nlmsg_free(msg);
32872f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	return 0;
32882f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
32892f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
32902f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
32918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wiphy_info_data {
3292444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	struct wpa_driver_nl80211_data *drv;
32931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_capa *capa;
32941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
3295c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	unsigned int num_multichan_concurrent;
3296c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt
32971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int error:1;
32981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int device_ap_sme:1;
32991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int poll_command_supported:1;
33001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int data_tx_status:1;
33011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int monitor_supported:1;
33022f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	unsigned int auth_supported:1;
33032f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	unsigned int connect_supported:1;
33042f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	unsigned int p2p_go_supported:1;
33052f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	unsigned int p2p_client_supported:1;
33062f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	unsigned int p2p_concurrent:1;
3307e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	unsigned int channel_switch_supported:1;
3308fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	unsigned int set_qos_map_supported:1;
33098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
33108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic unsigned int probe_resp_offload_support(int supp_protocols)
33131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
33141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int prot = 0;
33151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
33161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
33171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
33181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
33191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
33201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
33211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
33221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
33231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
33241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
33251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return prot;
33261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
33271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
33281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
33292f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
33302f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt					 struct nlattr *tb)
33318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
33322f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *nl_mode;
33332f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int i;
33342f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
33352f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb == NULL)
33362f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return;
33372f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
33382f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_for_each_nested(nl_mode, tb, i) {
33392f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		switch (nla_type(nl_mode)) {
33402f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		case NL80211_IFTYPE_AP:
33412f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			info->capa->flags |= WPA_DRIVER_FLAGS_AP;
33422f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			break;
3343700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		case NL80211_IFTYPE_ADHOC:
3344700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3345700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			break;
334634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		case NL80211_IFTYPE_P2P_DEVICE:
334734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			info->capa->flags |=
334834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
334934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			break;
33502f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		case NL80211_IFTYPE_P2P_GO:
33512f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			info->p2p_go_supported = 1;
33522f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			break;
33532f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		case NL80211_IFTYPE_P2P_CLIENT:
33542f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			info->p2p_client_supported = 1;
33552f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			break;
33562f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		case NL80211_IFTYPE_MONITOR:
33572f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			info->monitor_supported = 1;
33582f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			break;
33592f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		}
33602f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
33612f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
33622f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
33632f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
33642f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
33652f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt					 struct nlattr *nl_combi)
33662f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
33672f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
33682f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
33692f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *nl_limit, *nl_mode;
33702f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int err, rem_limit, rem_mode;
33712f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int combination_has_p2p = 0, combination_has_mgd = 0;
33721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	static struct nla_policy
33731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
33741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
33751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
33761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
33771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
3378ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
33791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	},
33801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
33811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
33821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		[NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
33831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	};
33848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33852f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
33862f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			       nl_combi, iface_combination_policy);
33872f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
33882f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	    !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
33892f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	    !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
33902f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return 0; /* broken combination */
33912f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
3392ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3393ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3394ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
33952f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
33962f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			    rem_limit) {
33972f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
33982f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				       nl_limit, iface_limit_policy);
33992f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
34002f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			return 0; /* broken combination */
34012f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34022f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		nla_for_each_nested(nl_mode,
34032f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				    tb_limit[NL80211_IFACE_LIMIT_TYPES],
34042f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				    rem_mode) {
34052f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			int ift = nla_type(nl_mode);
34062f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			if (ift == NL80211_IFTYPE_P2P_GO ||
34072f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			    ift == NL80211_IFTYPE_P2P_CLIENT)
34082f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				combination_has_p2p = 1;
34092f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			if (ift == NL80211_IFTYPE_STATION)
34102f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				combination_has_mgd = 1;
34112f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		}
34122f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		if (combination_has_p2p && combination_has_mgd)
34132f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			break;
34142f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
34152f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34162f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (combination_has_p2p && combination_has_mgd) {
34172f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		info->p2p_concurrent = 1;
3418c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		info->num_multichan_concurrent =
3419c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt			nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
34202f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return 1;
34212f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
34222f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34232f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	return 0;
34242f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
34252f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34262f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34272f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic void wiphy_info_iface_comb(struct wiphy_info_data *info,
34282f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				  struct nlattr *tb)
34292f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
34302f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *nl_combi;
34312f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int rem_combi;
34322f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34332f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb == NULL)
34342f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return;
34352f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34362f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_for_each_nested(nl_combi, tb, rem_combi) {
34372f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
34382f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			break;
34392f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
34402f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
34412f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34422f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34432f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic void wiphy_info_supp_cmds(struct wiphy_info_data *info,
34442f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				 struct nlattr *tb)
34452f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
34462f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *nl_cmd;
34472f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int i;
34482f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34492f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb == NULL)
34502f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return;
34512f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
34522f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_for_each_nested(nl_cmd, tb, i) {
34532f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		switch (nla_get_u32(nl_cmd)) {
34542f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		case NL80211_CMD_AUTHENTICATE:
34552f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			info->auth_supported = 1;
34562f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			break;
34572f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		case NL80211_CMD_CONNECT:
34582f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			info->connect_supported = 1;
34592f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			break;
34602f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		case NL80211_CMD_START_SCHED_SCAN:
34612f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			info->capa->sched_scan_supported = 1;
34622f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			break;
34632f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		case NL80211_CMD_PROBE_CLIENT:
34642f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			info->poll_command_supported = 1;
34652f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			break;
3466e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		case NL80211_CMD_CHANNEL_SWITCH:
3467e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			info->channel_switch_supported = 1;
3468e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			break;
3469fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case NL80211_CMD_SET_QOS_MAP:
3470fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->set_qos_map_supported = 1;
3471fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3472fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		}
3473fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
3474fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt}
3475fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
3476fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
3477fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3478fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				     struct nlattr *tb)
3479fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt{
3480fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int i, num;
3481fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	u32 *ciphers;
3482fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
3483fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (tb == NULL)
3484fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return;
3485fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
3486fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	num = nla_len(tb) / sizeof(u32);
3487fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	ciphers = nla_data(tb);
3488fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	for (i = 0; i < num; i++) {
3489fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		u32 c = ciphers[i];
3490fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
3491fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3492fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   c >> 24, (c >> 16) & 0xff,
3493fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   (c >> 8) & 0xff, c & 0xff);
3494fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		switch (c) {
3495fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_CCMP_256:
3496fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3497fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3498fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_GCMP_256:
3499fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3500fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3501fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_CCMP:
3502fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3503fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3504fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_GCMP:
3505fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3506fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3507fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_TKIP:
3508fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3509fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3510fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_WEP104:
3511fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3512fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3513fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_WEP40:
3514fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3515fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3516fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_AES_CMAC:
3517fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3518fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3519fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3520fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3521fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3522fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3523fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3524fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3525fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3526fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3527fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			break;
3528f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3529f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3530f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			break;
35312f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		}
35322f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
35332f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
35342f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35352f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35362f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic void wiphy_info_max_roc(struct wpa_driver_capa *capa,
35372f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			       struct nlattr *tb)
35382f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
35392f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb)
35402f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		capa->max_remain_on_chan = nla_get_u32(tb);
35412f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
35422f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35432f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35442f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
35452f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			    struct nlattr *ext_setup)
35462f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
35472f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tdls == NULL)
35482f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return;
35492f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35502f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
35512f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
35522f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35532f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (ext_setup) {
35542f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
35552f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
35562f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
35572f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
35582f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35592f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35602f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic void wiphy_info_feature_flags(struct wiphy_info_data *info,
35612f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				     struct nlattr *tb)
35622f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
35632f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	u32 flags;
35642f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct wpa_driver_capa *capa = info->capa;
35652f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35662f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb == NULL)
35672f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return;
35682f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35692f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	flags = nla_get_u32(tb);
35702f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35712f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (flags & NL80211_FEATURE_SK_TX_STATUS)
35722f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		info->data_tx_status = 1;
35732f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35742f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
35752f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
35762f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35772f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (flags & NL80211_FEATURE_SAE)
35782f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		capa->flags |= WPA_DRIVER_FLAGS_SAE;
35792f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35802f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
35812f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
35822f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
35832f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35842f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35852f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
35862f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt					  struct nlattr *tb)
35872f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
35882f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	u32 protocols;
35892f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35902f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb == NULL)
35912f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return;
35922f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
35932f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	protocols = nla_get_u32(tb);
35942f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
35952f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		   "mode");
35962f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
35972f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	capa->probe_resp_offloads = probe_resp_offload_support(protocols);
35982f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
35992f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
36002f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
36012f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic int wiphy_info_handler(struct nl_msg *msg, void *arg)
36022f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
36032f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
36042f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
36052f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct wiphy_info_data *info = arg;
36062f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct wpa_driver_capa *capa = info->capa;
3607444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = info->drv;
36082f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
36098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
36108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
36118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
361234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (tb[NL80211_ATTR_WIPHY_NAME])
361368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		os_strlcpy(drv->phyname,
361434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
361534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   sizeof(drv->phyname));
36168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
36171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		capa->max_scan_ssids =
36188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
36198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
36201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
36211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		capa->max_sched_scan_ssids =
36221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
36231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
36241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (tb[NL80211_ATTR_MAX_MATCH_SETS])
36251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		capa->max_match_sets =
36261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
36271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
36288bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	if (tb[NL80211_ATTR_MAC_ACL_MAX])
36298bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		capa->max_acl_mac_addrs =
36308bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt			nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
36318bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
36322f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
36332f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
36342f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
3635fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
36368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
36371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
36381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
36391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "off-channel TX");
36401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
36411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
36421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
36431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
36441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
36451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
36461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
36471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
36482f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	wiphy_info_max_roc(capa,
36492f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			   tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
36501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
36511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
36521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
36538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
36542f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
36552f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
3656ad266fb3da6083126e7619e525153839b918aa44Dmitry Shmidt
36571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (tb[NL80211_ATTR_DEVICE_AP_SME])
36581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		info->device_ap_sme = 1;
36591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
36602f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
36612f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	wiphy_info_probe_resp_offload(capa,
36622f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				      tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
36631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
3664444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3665444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	    drv->extended_capa == NULL) {
3666444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt		drv->extended_capa =
3667444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt			os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3668444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt		if (drv->extended_capa) {
3669444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt			os_memcpy(drv->extended_capa,
3670444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt				  nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3671444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt				  nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3672444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt			drv->extended_capa_len =
3673444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt				nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3674444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt		}
3675444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt		drv->extended_capa_mask =
3676444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt			os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3677444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt		if (drv->extended_capa_mask) {
3678444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt			os_memcpy(drv->extended_capa_mask,
3679444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt				  nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3680444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt				  nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3681444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt		} else {
3682444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt			os_free(drv->extended_capa);
3683444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt			drv->extended_capa = NULL;
3684444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt			drv->extended_capa_len = 0;
3685444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt		}
3686444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	}
3687444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt
3688fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (tb[NL80211_ATTR_VENDOR_DATA]) {
3689fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		struct nlattr *nl;
3690fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		int rem;
3691fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
3692fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3693fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			struct nl80211_vendor_cmd_info *vinfo;
36941846323989242844f0e857458a8939fa5836429cDmitry Shmidt			if (nla_len(nl) != sizeof(*vinfo)) {
3695fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3696fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				continue;
3697fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			}
3698fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			vinfo = nla_data(nl);
3699fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3700fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				   vinfo->vendor_id, vinfo->subcmd);
3701fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		}
3702fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
3703fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
3704fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3705fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		struct nlattr *nl;
3706fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		int rem;
3707fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
3708fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3709fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			struct nl80211_vendor_cmd_info *vinfo;
37101846323989242844f0e857458a8939fa5836429cDmitry Shmidt			if (nla_len(nl) != sizeof(*vinfo)) {
3711fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3712fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				continue;
3713fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			}
3714fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			vinfo = nla_data(nl);
3715fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3716fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				   vinfo->vendor_id, vinfo->subcmd);
3717fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		}
3718fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
3719fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
37208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
37218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
37228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
37238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
37248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
37258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       struct wiphy_info_data *info)
37268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
37272f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	u32 feat;
37288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
37298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
37308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(info, 0, sizeof(*info));
37311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	info->capa = &drv->capa;
3732444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	info->drv = drv;
37338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
37348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
37358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
37368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
37378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
37382f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	feat = get_nl80211_protocol_features(drv);
37392f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
37402f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
37412f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	else
37422f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
37438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
37442f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
3745cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
374634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
37478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
37482f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
37492f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return -1;
37502f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
37512f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (info->auth_supported)
37522f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
37532f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	else if (!info->connect_supported) {
37542f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		wpa_printf(MSG_INFO, "nl80211: Driver does not support "
37552f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			   "authentication/association or connect commands");
37562f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		info->error = 1;
37572f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
37582f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
37592f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (info->p2p_go_supported && info->p2p_client_supported)
37602f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
37612f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (info->p2p_concurrent) {
37622f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
37632f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			   "interface (driver advertised support)");
37642f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
37652f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
37662f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
3767c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	if (info->num_multichan_concurrent > 1) {
37682f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
37692f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			   "concurrent (driver advertised support)");
3770c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		drv->capa.num_multichan_concurrent =
3771c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt			info->num_multichan_concurrent;
37722f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
377351b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt
377451b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt	/* default to 5000 since early versions of mac80211 don't set it */
377551b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt	if (!drv->capa.max_remain_on_chan)
377651b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt		drv->capa.max_remain_on_chan = 5000;
377751b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt
3778fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (info->channel_switch_supported)
3779fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
3780fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
37812f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	return 0;
37828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
37838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
37848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
37858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
37868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
37878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
37888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
37898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
37908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wiphy_info_data info;
37918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_driver_nl80211_get_info(drv, &info))
37928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
37931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
37941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (info.error)
37951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
37961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
37978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->has_capability = 1;
37988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
37998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
38008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
38018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
38028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
38038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		WPA_DRIVER_AUTH_SHARED |
38048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		WPA_DRIVER_AUTH_LEAP;
38058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
38071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
38081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3809ad266fb3da6083126e7619e525153839b918aa44Dmitry Shmidt
381061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (!info.device_ap_sme) {
381104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
38128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
381361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		/*
381461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		 * No AP SME is currently assumed to also indicate no AP MLME
381561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		 * in the driver/firmware.
381661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		 */
381761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
381861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	}
381961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
38201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->device_ap_sme = info.device_ap_sme;
38211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->poll_command_supported = info.poll_command_supported;
38221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->data_tx_status = info.data_tx_status;
3823fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (info.set_qos_map_supported)
3824fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
38258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/*
3827aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt	 * If poll command and tx status are supported, mac80211 is new enough
3828aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt	 * to have everything we need to not need monitor interfaces.
38291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 */
3830aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt	drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
38311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
38321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->device_ap_sme && drv->use_monitor) {
38331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/*
38341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * Non-mac80211 drivers may not support monitor interface.
38351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * Make sure we do not get stuck with incorrect capability here
38361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * by explicitly testing this.
38371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 */
38381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (!info.monitor_supported) {
38391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
38401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "with device_ap_sme since no monitor mode "
38411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "support detected");
38421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			drv->use_monitor = 0;
38431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
38448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
38458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/*
38471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * If we aren't going to use monitor interfaces, but the
38481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * driver doesn't support data TX status, we won't get TX
38491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * status for EAPOL frames.
38501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 */
38511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!drv->use_monitor && !info.data_tx_status)
38521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
38538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
38558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
38568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef ANDROID
38591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int android_genl_ctrl_resolve(struct nl_handle *handle,
38601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     const char *name)
38618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
38621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/*
38631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
38641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * need to work around that.
38651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 */
38661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_cache *cache = NULL;
38671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct genl_family *nl80211 = NULL;
38681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int id = -1;
38698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
38711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
38721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "netlink cache");
38731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto fail;
38748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
38758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211 = genl_ctrl_search_by_name(cache, name);
38771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211 == NULL)
38781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto fail;
38798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	id = genl_family_get_id(nl80211);
38818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtfail:
38831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211)
38841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		genl_family_put(nl80211);
38851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (cache)
38861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl_cache_free(cache);
38878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return id;
38891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
38901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#define genl_ctrl_resolve android_genl_ctrl_resolve
38911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* ANDROID */
38928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
38931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
38941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
38951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
38961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ret;
38971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
38981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
38991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (global->nl_cb == NULL) {
39001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
39011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "callbacks");
39021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
39038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
39048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
39051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	global->nl = nl_create_handle(global->nl_cb, "nl");
39061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (global->nl == NULL)
39071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto err;
39081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
39091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
39101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (global->nl80211_id < 0) {
39118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
39128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "found");
39131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto err;
39148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
39158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
39161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	global->nl_event = nl_create_handle(global->nl_cb, "event");
39171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (global->nl_event == NULL)
39181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto err;
39191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
39201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = nl_get_multicast_id(global, "nl80211", "scan");
39218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret >= 0)
39221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		ret = nl_socket_add_membership(global->nl_event, ret);
39238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret < 0) {
39248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
39258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "membership for scan events: %d (%s)",
39268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   ret, strerror(-ret));
39271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto err;
39288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
39298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
39301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = nl_get_multicast_id(global, "nl80211", "mlme");
39318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret >= 0)
39321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		ret = nl_socket_add_membership(global->nl_event, ret);
39338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret < 0) {
39348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
39358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "membership for mlme events: %d (%s)",
39368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   ret, strerror(-ret));
39371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto err;
39388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
39398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
39401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = nl_get_multicast_id(global, "nl80211", "regulatory");
39418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret >= 0)
39421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		ret = nl_socket_add_membership(global->nl_event, ret);
39438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret < 0) {
39448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
39458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "membership for regulatory events: %d (%s)",
39468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   ret, strerror(-ret));
39478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Continue without regulatory events */
39488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
39498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3950fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	ret = nl_get_multicast_id(global, "nl80211", "vendor");
3951fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (ret >= 0)
3952fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = nl_socket_add_membership(global->nl_event, ret);
3953fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (ret < 0) {
3954fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3955fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   "membership for vendor events: %d (%s)",
3956fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   ret, strerror(-ret));
3957fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		/* Continue without vendor events */
3958fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
3959fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
39601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
39611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  no_seq_check, NULL);
39621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
39631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  process_global_event, global);
39641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
396568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl80211_register_eloop_read(&global->nl_event,
396668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				    wpa_driver_nl80211_event_receive,
396768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				    global->nl_cb);
39688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
39698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
39708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
39711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidterr:
39721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_destroy_handles(&global->nl_event);
39731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_destroy_handles(&global->nl);
39741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_put(global->nl_cb);
39751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	global->nl_cb = NULL;
39768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
39778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
39788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
39798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
39801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
39811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
39821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
39831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!drv->nl_cb) {
39841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
39851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
39861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
39871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
39881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
39891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  no_seq_check, NULL);
39901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
39911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  process_drv_event, drv);
39921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
39931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return 0;
39941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
39951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
39961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
39978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_rfkill_blocked(void *ctx)
39988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
39998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
40008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
40018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * This may be for any interface; use ifdown event to disable
40028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * interface.
40038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
40048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
40058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
40068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
40078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
40088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
40098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = ctx;
40108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
4011cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (i802_set_iface_flags(drv->first_bss, 1)) {
40128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
40138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "after rfkill unblock");
40148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
40158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
40168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* rtnetlink ifup handler will report interface as enabled */
40178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
40188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
40198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
40201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
40211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt						      void *eloop_ctx,
40221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt						      void *handle)
40231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
40241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = eloop_ctx;
40251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u8 data[2048];
40261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct msghdr msg;
40271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct iovec entry;
402804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	u8 control[512];
40291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct cmsghdr *cmsg;
40301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int res, found_ee = 0, found_wifi = 0, acked = 0;
40311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	union wpa_event_data event;
40321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	memset(&msg, 0, sizeof(msg));
40341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg.msg_iov = &entry;
40351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg.msg_iovlen = 1;
40361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	entry.iov_base = data;
40371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	entry.iov_len = sizeof(data);
40381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg.msg_control = &control;
40391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg.msg_controllen = sizeof(control);
40401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	res = recvmsg(sock, &msg, MSG_ERRQUEUE);
40421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* if error or not fitting 802.3 header, return */
40431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (res < 14)
40441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
40451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
40471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	{
40481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (cmsg->cmsg_level == SOL_SOCKET &&
40491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    cmsg->cmsg_type == SCM_WIFI_STATUS) {
40501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			int *ack;
40511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			found_wifi = 1;
40531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			ack = (void *)CMSG_DATA(cmsg);
40541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			acked = *ack;
40551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
40561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (cmsg->cmsg_level == SOL_PACKET &&
40581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
40591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			struct sock_extended_err *err =
40601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				(struct sock_extended_err *)CMSG_DATA(cmsg);
40611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
40631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				found_ee = 1;
40641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
40651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
40661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!found_ee || !found_wifi)
40681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
40691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	memset(&event, 0, sizeof(event));
40711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	event.eapol_tx_status.dst = data;
40721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	event.eapol_tx_status.data = data + 14;
40731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	event.eapol_tx_status.data_len = res - 14;
40741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	event.eapol_tx_status.ack = acked;
40751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
40761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
40771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_init_bss(struct i802_bss *bss)
40801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
40811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
40821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!bss->nl_cb)
40831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
40841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
40861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  no_seq_check, NULL);
40871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
40881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		  process_bss_event, bss);
40891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return 0;
40911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
40921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
40941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_destroy_bss(struct i802_bss *bss)
40951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
40961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_put(bss->nl_cb);
40971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bss->nl_cb = NULL;
40981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
40991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
41001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
4101e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtstatic void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4102e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt					  void *global_priv, int hostapd,
4103e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt					  const u8 *set_addr)
41048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
41058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv;
41068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rfkill_config *rcfg;
41078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss;
41088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
41091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (global_priv == NULL)
41101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
41118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv = os_zalloc(sizeof(*drv));
41128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv == NULL)
41138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
41148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->global = global_priv;
41158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->ctx = ctx;
4116e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	drv->hostapd = !!hostapd;
4117e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	drv->eapol_sock = -1;
4118e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4119e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	drv->if_indices = drv->default_if_indices;
4120cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
4121cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4122cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (!drv->first_bss) {
4123cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		os_free(drv);
4124cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		return NULL;
4125cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	}
4126cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	bss = drv->first_bss;
41278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	bss->drv = drv;
4128a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	bss->ctx = ctx;
4129a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt
41308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
41318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->monitor_ifidx = -1;
41328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->monitor_sock = -1;
41331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->eapol_tx_sock = -1;
41341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
41358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
41368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wpa_driver_nl80211_init_nl(drv)) {
41378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(drv);
41388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
41398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
41408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
41411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_init_bss(bss))
41428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto failed;
41438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
41448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rcfg = os_zalloc(sizeof(*rcfg));
41458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (rcfg == NULL)
41468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto failed;
41478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rcfg->ctx = drv;
41488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
41498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
41508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
41518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->rfkill = rfkill_init(rcfg);
41528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->rfkill == NULL) {
41538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
41548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(rcfg);
41558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
41568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4157e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4158e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		drv->start_iface_up = 1;
4159e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
4160e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
41618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto failed;
41628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
41631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
41641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->eapol_tx_sock < 0)
41651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto failed;
41661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
41671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->data_tx_status) {
41681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		int enabled = 1;
41691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
41701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
41711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			       &enabled, sizeof(enabled)) < 0) {
41721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG,
41731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				"nl80211: wifi status sockopt failed\n");
41741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			drv->data_tx_status = 0;
41751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (!drv->use_monitor)
41761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				drv->capa.flags &=
41771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
41781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		} else {
41791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			eloop_register_read_sock(drv->eapol_tx_sock,
41801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				wpa_driver_nl80211_handle_eapol_tx_status,
41811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				drv, NULL);
41821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
41831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
41841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
41851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->global) {
41868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		dl_list_add(&drv->global->interfaces, &drv->list);
41871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->in_interface_list = 1;
41881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
41898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
41908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return bss;
41918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
41928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtfailed:
41931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_driver_nl80211_deinit(bss);
41948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NULL;
41958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
41968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
41978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4198e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt/**
4199e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4200e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt * @ctx: context to be used when calling wpa_supplicant functions,
4201e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt * e.g., wpa_supplicant_event()
4202e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt * @ifname: interface name, e.g., wlan0
4203e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt * @global_priv: private driver global data from global_init()
4204e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt * Returns: Pointer to private data, %NULL on failure
4205e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt */
4206e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtstatic void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4207e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				      void *global_priv)
4208e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt{
4209e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4210e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt}
4211e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
4212e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
42131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_register_frame(struct i802_bss *bss,
42148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  struct nl_handle *nl_handle,
42158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  u16 type, const u8 *match, size_t match_len)
42168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
42171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
42188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
42198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -1;
4220fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	char buf[30];
42218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
42228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
42238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
42248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
42258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4226fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	buf[0] = '\0';
4227fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
4228fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
4229fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		   type, nl_handle, buf);
42308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
42311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
42321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
423334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nl80211_set_iface_id(msg, bss) < 0)
423434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
423534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
42368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
42378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
42388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
42391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
42408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
42418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
42428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
42438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "failed (type=%u): ret=%d (%s)",
42448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   type, ret, strerror(-ret));
42458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
42468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    match, match_len);
42478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
42488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
42498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = 0;
42508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
42518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
42528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
42538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
42548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
42558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
42561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
42571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
42581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
42591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
42601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (bss->nl_mgmt) {
42611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
42621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "already on! (nl_mgmt=%p)", bss->nl_mgmt);
42631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
42641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
42651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
42661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
42671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (bss->nl_mgmt == NULL)
42681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
42691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
42701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return 0;
42711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
42721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
42731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
427468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidtstatic void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
427568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt{
427668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl80211_register_eloop_read(&bss->nl_mgmt,
427768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				    wpa_driver_nl80211_event_receive,
427868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				    bss->nl_cb);
427968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt}
428068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
428168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
42821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_register_action_frame(struct i802_bss *bss,
42838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					 const u8 *match, size_t match_len)
42848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
42858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
42861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return nl80211_register_frame(bss, bss->nl_mgmt,
42878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      type, match, match_len);
42888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
42898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
42908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
42911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
42928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
42931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
4294fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int ret = 0;
42951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
42961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_alloc_mgmt_handle(bss))
42971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
42981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
42991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   "handle %p", bss->nl_mgmt);
43001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
430168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
430268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
430368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
430468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		/* register for any AUTH message */
430568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
430668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	}
430768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
4308051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#ifdef CONFIG_INTERWORKING
4309051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	/* QoS Map Configure */
4310051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
4311fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
4312051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#endif /* CONFIG_INTERWORKING */
43131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
43148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* GAS Initial Request */
43151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
4316fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
43178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* GAS Initial Response */
43181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
4319fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
43208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* GAS Comeback Request */
43211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
4322fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
43238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* GAS Comeback Response */
43241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
4325fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
43261846323989242844f0e857458a8939fa5836429cDmitry Shmidt	/* Protected GAS Initial Request */
43271846323989242844f0e857458a8939fa5836429cDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
43281846323989242844f0e857458a8939fa5836429cDmitry Shmidt		ret = -1;
43291846323989242844f0e857458a8939fa5836429cDmitry Shmidt	/* Protected GAS Initial Response */
43301846323989242844f0e857458a8939fa5836429cDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
43311846323989242844f0e857458a8939fa5836429cDmitry Shmidt		ret = -1;
43321846323989242844f0e857458a8939fa5836429cDmitry Shmidt	/* Protected GAS Comeback Request */
43331846323989242844f0e857458a8939fa5836429cDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
43341846323989242844f0e857458a8939fa5836429cDmitry Shmidt		ret = -1;
43351846323989242844f0e857458a8939fa5836429cDmitry Shmidt	/* Protected GAS Comeback Response */
43361846323989242844f0e857458a8939fa5836429cDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
43371846323989242844f0e857458a8939fa5836429cDmitry Shmidt		ret = -1;
43381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
43391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef CONFIG_P2P
43408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* P2P Public Action */
43411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_action_frame(bss,
43428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  (u8 *) "\x04\x09\x50\x6f\x9a\x09",
43438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  6) < 0)
4344fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
43458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* P2P Action */
43461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_action_frame(bss,
43478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  (u8 *) "\x7f\x50\x6f\x9a\x09",
43488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  5) < 0)
4349fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
43508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_P2P */
43518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_IEEE80211W
43528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* SA Query Response */
43531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
4354fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
43558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_IEEE80211W */
43561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef CONFIG_TDLS
43571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
43581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/* TDLS Discovery Response */
43591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
43601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    0)
4361fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			ret = -1;
43621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
43631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* CONFIG_TDLS */
43648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
43658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* FT Action frames */
43661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
4367fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
43688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
43698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
43708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
43718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
43721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* WNM - BSS Transition Management Request */
43731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
4374fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
4375a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	/* WNM-Sleep Mode Response */
4376a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
4377fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		ret = -1;
43781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
4379f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_HS20
4380f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt	/* WNM-Notification */
4381f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt	if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
4382f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		return -1;
4383f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_HS20 */
4384f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt
438568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl80211_mgmt_handle_register_eloop(bss);
438668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
4387fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	return ret;
43881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
43891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
43901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
43911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_register_spurious_class3(struct i802_bss *bss)
43921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
43931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
43941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
43951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ret = -1;
43961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
43971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
43981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
43991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
44001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
44021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
44041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
44061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
44071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ret) {
44081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
44091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "failed: ret=%d (%s)",
44101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   ret, strerror(-ret));
44111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto nla_put_failure;
44121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
44131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = 0;
44141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtnla_put_failure:
44151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
44161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return ret;
44171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
44181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
44211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
44221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	static const int stypes[] = {
44231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		WLAN_FC_STYPE_AUTH,
44241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		WLAN_FC_STYPE_ASSOC_REQ,
44251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		WLAN_FC_STYPE_REASSOC_REQ,
44261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		WLAN_FC_STYPE_DISASSOC,
44271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		WLAN_FC_STYPE_DEAUTH,
44281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		WLAN_FC_STYPE_ACTION,
44291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		WLAN_FC_STYPE_PROBE_REQ,
44301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt/* Beacon doesn't work as mac80211 doesn't currently allow
44311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * it, but it wouldn't really be the right thing anyway as
44321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * it isn't per interface ... maybe just dump the scan
44331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * results periodically for OLBC?
44341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt */
44351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt//		WLAN_FC_STYPE_BEACON,
44361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	};
44371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	unsigned int i;
44381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_alloc_mgmt_handle(bss))
44401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
44411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
44421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   "handle %p", bss->nl_mgmt);
44431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
444468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	for (i = 0; i < ARRAY_SIZE(stypes); i++) {
44451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (nl80211_register_frame(bss, bss->nl_mgmt,
44461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					   (WLAN_FC_TYPE_MGMT << 2) |
44471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					   (stypes[i] << 4),
44481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					   NULL, 0) < 0) {
44491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			goto out_err;
44501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
44511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
44521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_spurious_class3(bss))
44541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto out_err;
44551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_get_wiphy_data_ap(bss) == NULL)
44571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto out_err;
44581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
445968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl80211_mgmt_handle_register_eloop(bss);
44601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return 0;
44611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtout_err:
44631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_destroy_handles(&bss->nl_mgmt);
44641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return -1;
44651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
44661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
44691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
44701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_alloc_mgmt_handle(bss))
44711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
44721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
44731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   "handle %p (device SME)", bss->nl_mgmt);
44741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_frame(bss, bss->nl_mgmt,
44761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   (WLAN_FC_TYPE_MGMT << 2) |
44771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   (WLAN_FC_STYPE_ACTION << 4),
44781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   NULL, 0) < 0)
44791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto out_err;
44801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
448168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl80211_mgmt_handle_register_eloop(bss);
44828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
44831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtout_err:
44851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_destroy_handles(&bss->nl_mgmt);
44861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return -1;
44871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
44881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
44911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
44921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (bss->nl_mgmt == NULL)
44931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
44941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
44951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   "(%s)", bss->nl_mgmt, reason);
449668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl80211_destroy_eloop_handle(&bss->nl_mgmt);
44971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
44981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_put_wiphy_data_ap(bss);
44998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
45008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
45018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
45028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
45038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
45048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
45058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
45068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
45078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
450834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic void nl80211_del_p2pdev(struct i802_bss *bss)
450934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
451034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
451134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct nl_msg *msg;
451234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	int ret;
451334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
451434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	msg = nlmsg_alloc();
451534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (!msg)
451634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return;
451734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
451834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
451934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
452034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
452134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
452234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	msg = NULL;
452334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
452434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
452534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   bss->ifname, (long long unsigned int) bss->wdev_id,
452668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		   strerror(-ret));
452734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
452834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtnla_put_failure:
452934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nlmsg_free(msg);
453034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
453134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
453234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
453334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic int nl80211_set_p2pdev(struct i802_bss *bss, int start)
453434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
453534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
453634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct nl_msg *msg;
453734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	int ret = -1;
453834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
453934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	msg = nlmsg_alloc();
454034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (!msg)
454134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return -1;
454234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
454334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (start)
454434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
454534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	else
454634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
454734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
454834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
454934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
455034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
455134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	msg = NULL;
455234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
455334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
455434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   start ? "Start" : "Stop",
455534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   bss->ifname, (long long unsigned int) bss->wdev_id,
455668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		   strerror(-ret));
455734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
455834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtnla_put_failure:
455934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nlmsg_free(msg);
456034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return ret;
456134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
456234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
456334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
456434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic int i802_set_iface_flags(struct i802_bss *bss, int up)
456534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
456634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	enum nl80211_iftype nlmode;
456734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
456834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nlmode = nl80211_get_ifmode(bss);
456934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
457034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return linux_set_iface_flags(bss->drv->global->ioctl_sock,
457134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt					     bss->ifname, up);
457234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	}
457334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
457434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	/* P2P Device has start/stop which is equivalent */
457534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return nl80211_set_p2pdev(bss, up);
457634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
457734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
457834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
45798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int
4580e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtwpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4581e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				   const u8 *set_addr, int first)
45828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4583cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	struct i802_bss *bss = drv->first_bss;
45848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int send_rfkill_event = 0;
4585e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	enum nl80211_iftype nlmode;
45868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
45878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->ifindex = if_nametoindex(bss->ifname);
458834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	bss->ifindex = drv->ifindex;
458934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	bss->wdev_id = drv->global->if_add_wdevid;
459034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	bss->wdev_id_set = drv->global->if_add_wdevid_set;
459134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
4592b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4593b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
459434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	drv->global->if_add_wdevid_set = 0;
459534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
459634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (wpa_driver_nl80211_capa(drv))
459734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return -1;
459834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
459934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
460034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   bss->ifname, drv->phyname);
46018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4602e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (set_addr &&
4603e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	    (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4604e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	     linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4605e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				set_addr)))
4606e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		return -1;
4607e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
4608e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4609e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		drv->start_mode_ap = 1;
4610e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
4611e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (drv->hostapd)
4612e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		nlmode = NL80211_IFTYPE_AP;
4613e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	else if (bss->if_dynamic)
461434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		nlmode = nl80211_get_ifmode(bss);
4615e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	else
4616e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		nlmode = NL80211_IFTYPE_STATION;
461734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
461834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
4619e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
46201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
46218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
462234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
462334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
462434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		int ret = nl80211_set_p2pdev(bss, 1);
462534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		if (ret < 0)
462634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device");
462734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		nl80211_get_macaddr(bss);
462834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return ret;
462934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	}
46308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
46311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
46328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (rfkill_is_blocked(drv->rfkill)) {
46338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
46348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "interface '%s' due to rfkill",
46358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   bss->ifname);
46368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			drv->if_disabled = 1;
46378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			send_rfkill_event = 1;
46388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		} else {
46398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_ERROR, "nl80211: Could not set "
46408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "interface '%s' UP", bss->ifname);
46418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
46428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
46438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
46448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4645e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (!drv->hostapd)
4646e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4647e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				       1, IF_OPER_DORMANT);
46488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
46491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
46501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			       bss->addr))
46518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
46528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
46538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (send_rfkill_event) {
46548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
46558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       drv, drv->ctx);
46568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
46578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
46588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
46598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
46608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
46618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
46628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
46638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
46648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
46658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
46668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
46678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
46688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
46698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4670cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4671cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		   drv->ifindex);
46721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
46738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
46748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
46758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return send_and_recv_msgs(drv, msg, NULL, NULL);
46768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
46771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
46788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
46798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
46808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
46818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
46828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
46838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
46844b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
46858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
46868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Shut down driver interface and processing of driver events. Free
46878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * private data buffer if one was allocated in wpa_driver_nl80211_init().
46888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
46894b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic void wpa_driver_nl80211_deinit(struct i802_bss *bss)
46908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
46918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
46928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
469304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	bss->in_deinit = 1;
46941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->data_tx_status)
46951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		eloop_unregister_read_sock(drv->eapol_tx_sock);
46961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->eapol_tx_sock >= 0)
46971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		close(drv->eapol_tx_sock);
46981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
46991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (bss->nl_preq)
47008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_driver_nl80211_probe_req_report(bss, 0);
47018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss->added_if_into_bridge) {
47021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
47031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				    bss->ifname) < 0)
47048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
47058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "interface %s from bridge %s: %s",
47068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   bss->ifname, bss->brname, strerror(errno));
47078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
47088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss->added_bridge) {
47091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
47108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
47118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "bridge %s: %s",
47128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   bss->brname, strerror(errno));
47138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
47148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nl80211_remove_monitor_interface(drv);
47168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (is_ap_interface(drv->nlmode))
47188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_driver_nl80211_del_beacon(drv);
47198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->eapol_sock >= 0) {
47218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_unregister_read_sock(drv->eapol_sock);
47228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		close(drv->eapol_sock);
47238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
47248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->if_indices != drv->default_if_indices)
47268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(drv->if_indices);
47278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->disabled_11b_rates)
47298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_disable_11b_rates(drv, drv->ifindex, 0);
47308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
47321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			       IF_OPER_UP);
47338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	rfkill_deinit(drv->rfkill);
47348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
47368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4737e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (!drv->start_iface_up)
4738e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		(void) i802_set_iface_flags(bss, 0);
473934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4740e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		if (!drv->hostapd || !drv->start_mode_ap)
4741e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			wpa_driver_nl80211_set_mode(bss,
4742e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt						    NL80211_IFTYPE_STATION);
4743b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		nl80211_mgmt_unsubscribe(bss, "deinit");
474434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	} else {
474534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		nl80211_mgmt_unsubscribe(bss, "deinit");
474634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		nl80211_del_p2pdev(bss);
474734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	}
47488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nl_cb_put(drv->nl_cb);
47498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4750cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	nl80211_destroy_bss(drv->first_bss);
47511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
47528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(drv->filter_ssids);
47538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_free(drv->auth_ie);
47551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
47561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->in_interface_list)
47578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		dl_list_del(&drv->list);
47588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4759444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	os_free(drv->extended_capa);
4760444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	os_free(drv->extended_capa_mask);
4761cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	os_free(drv->first_bss);
47628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(drv);
47638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
47648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
47678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
47688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @eloop_ctx: Driver private data
47698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
47708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
47718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function can be used as registered timeout when starting a scan to
47728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * generate a scan completed event if the driver does not report this.
47738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
47748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
47758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
47768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = eloop_ctx;
47771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
4778cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		wpa_driver_nl80211_set_mode(drv->first_bss,
47791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					    drv->ap_scan_as_station);
47801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
47818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
47828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
47838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
47848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
47858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
478761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtstatic struct nl_msg *
478861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtnl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
478934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		    struct wpa_driver_scan_params *params, u64 *wdev_id)
47908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
479161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	struct nl_msg *msg;
47928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t i;
47938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
47948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
479561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (!msg)
479661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		return NULL;
47978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
479861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	nl80211_cmd(drv, msg, 0, cmd);
47998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
480034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (!wdev_id)
480134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
480234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	else
480334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
48048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
480561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (params->num_ssids) {
48068da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		struct nlattr *ssids;
48078da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt
48088da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
480961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (ssids == NULL)
481061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			goto fail;
481161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		for (i = 0; i < params->num_ssids; i++) {
481261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
481361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt					  params->ssids[i].ssid,
481461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt					  params->ssids[i].ssid_len);
48158da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
48168da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt				    params->ssids[i].ssid) < 0)
481761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				goto fail;
481861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		}
48198da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		nla_nest_end(msg, ssids);
48208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
48218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
48228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->extra_ies) {
48231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
48241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			    params->extra_ies, params->extra_ies_len);
482561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
482661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			    params->extra_ies) < 0)
482761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			goto fail;
48288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
48298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
48308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->freqs) {
48318da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		struct nlattr *freqs;
48328da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
483361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (freqs == NULL)
483461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			goto fail;
48358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		for (i = 0; params->freqs[i]; i++) {
48368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
48378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "MHz", params->freqs[i]);
48388da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
483961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				goto fail;
48408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
48418da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		nla_nest_end(msg, freqs);
48428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
48438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
484461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	os_free(drv->filter_ssids);
484561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	drv->filter_ssids = params->filter_ssids;
484661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	params->filter_ssids = NULL;
484761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	drv->num_filter_ssids = params->num_filter_ssids;
484861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
4849fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (params->only_new_results) {
4850fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
4851fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
4852fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			    NL80211_SCAN_FLAG_FLUSH);
4853fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
4854fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
485561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	return msg;
485661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
485761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtfail:
485834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtnla_put_failure:
485961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	nlmsg_free(msg);
486061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	return NULL;
486161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt}
486261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
486361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
486461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt/**
486561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * wpa_driver_nl80211_scan - Request the driver to initiate scan
48664b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
486761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * @params: Scan parameters
486861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * Returns: 0 on success, -1 on failure
486961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt */
48704b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_scan(struct i802_bss *bss,
487161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				   struct wpa_driver_scan_params *params)
487261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt{
487361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
487461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	int ret = -1, timeout;
48758da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	struct nl_msg *msg = NULL;
487661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
4877700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
487861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	drv->scan_for_auth = 0;
487961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
488034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
488134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				  bss->wdev_id_set ? &bss->wdev_id : NULL);
488261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (!msg)
488361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		return -1;
488461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
48851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->p2p_probe) {
48868da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		struct nlattr *rates;
48878da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt
488804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
488904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
48908da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
489161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (rates == NULL)
489261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			goto nla_put_failure;
489361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
48941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/*
48951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
48961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * by masking out everything else apart from the OFDM rates 6,
48971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
48981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * rates are left enabled.
48991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 */
49008da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
49011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			"\x0c\x12\x18\x24\x30\x48\x60\x6c");
49028da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		nla_nest_end(msg, rates);
49031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
49041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
49051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
49061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
49078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
49088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
49098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
49108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
49118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(%s)", ret, strerror(-ret));
4912e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		if (drv->hostapd && is_ap_interface(drv->nlmode)) {
4913ed003d2a93648e6201272d2fb8212cbc01085928Dmitry Shmidt			enum nl80211_iftype old_mode = drv->nlmode;
4914ed003d2a93648e6201272d2fb8212cbc01085928Dmitry Shmidt
49158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			/*
49168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 * mac80211 does not allow scan requests in AP mode, so
49178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 * try to do this in station mode.
49188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 */
49191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (wpa_driver_nl80211_set_mode(
49201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				    bss, NL80211_IFTYPE_STATION))
49218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				goto nla_put_failure;
49228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
49234b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt			if (wpa_driver_nl80211_scan(bss, params)) {
49241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				wpa_driver_nl80211_set_mode(bss, drv->nlmode);
49258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				goto nla_put_failure;
49268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
49278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
49288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			/* Restore AP mode when processing scan results */
4929ed003d2a93648e6201272d2fb8212cbc01085928Dmitry Shmidt			drv->ap_scan_as_station = old_mode;
49308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			ret = 0;
49318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		} else
49328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			goto nla_put_failure;
49338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
49348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
49355605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	drv->scan_state = SCAN_REQUESTED;
49368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Not all drivers generate "scan completed" wireless event, so try to
49378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * read results after a timeout. */
49388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	timeout = 10;
49398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->scan_complete_events) {
49408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
49418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * The driver seems to deliver events to notify when scan is
49428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * complete, so use longer timeout to avoid race conditions
49438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * with scanning and following association request.
49448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
49458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		timeout = 30;
49468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
49478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
49488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "seconds", ret, timeout);
49498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
49508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
49518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       drv, drv->ctx);
49528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
49538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
49548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
49558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
49568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
49578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
49588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
49591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt/**
49601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
49611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
49621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * @params: Scan parameters
49631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * @interval: Interval between scan cycles in milliseconds
49641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * Returns: 0 on success, -1 on failure or if not supported
49651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt */
49661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_sched_scan(void *priv,
49671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					 struct wpa_driver_scan_params *params,
49681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					 u32 interval)
49698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
49701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *bss = priv;
49711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
497261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	int ret = -1;
497361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	struct nl_msg *msg;
49741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	size_t i;
49758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4976700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4977700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
49781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef ANDROID
49791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!drv->capa.sched_scan_supported)
49801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return android_pno_start(bss, params);
49811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* ANDROID */
49821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
498334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
498434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				  bss->wdev_id_set ? &bss->wdev_id : NULL);
498561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (!msg)
498661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		goto nla_put_failure;
49871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
49881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
49891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
499061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if ((drv->num_filter_ssids &&
499161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	    (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
499261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	    params->filter_rssi) {
49938da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		struct nlattr *match_sets;
49948da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
499561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (match_sets == NULL)
499661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			goto nla_put_failure;
49971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
49981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		for (i = 0; i < drv->num_filter_ssids; i++) {
49998da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			struct nlattr *match_set_ssid;
50001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_hexdump_ascii(MSG_MSGDUMP,
50011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					  "nl80211: Sched scan filter SSID",
50021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					  drv->filter_ssids[i].ssid,
50031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					  drv->filter_ssids[i].ssid_len);
50041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50058da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			match_set_ssid = nla_nest_start(msg, i + 1);
500661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			if (match_set_ssid == NULL)
500761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				goto nla_put_failure;
50088da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
50091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				drv->filter_ssids[i].ssid_len,
50101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				drv->filter_ssids[i].ssid);
50119767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt			if (params->filter_rssi)
50129767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt				NLA_PUT_U32(msg,
50139767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt					    NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
50149767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt					    params->filter_rssi);
50151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50168da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			nla_nest_end(msg, match_set_ssid);
50171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
50181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50199767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		/*
50209767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		 * Due to backward compatibility code, newer kernels treat this
50219767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		 * matchset (with only an RSSI filter) as the default for all
50229767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		 * other matchsets, unless it's the only one, in which case the
50239767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		 * matchset will actually allow all SSIDs above the RSSI.
50249767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		 */
502561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (params->filter_rssi) {
50268da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			struct nlattr *match_set_rssi;
50278da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			match_set_rssi = nla_nest_start(msg, 0);
502861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			if (match_set_rssi == NULL)
502961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				goto nla_put_failure;
50308da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
503161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				    params->filter_rssi);
503261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			wpa_printf(MSG_MSGDUMP,
503361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				   "nl80211: Sched scan RSSI filter %d dBm",
503461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				   params->filter_rssi);
50358da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt			nla_nest_end(msg, match_set_rssi);
50361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
503761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
50388da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		nla_nest_end(msg, match_sets);
50391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
50401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
50421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* TODO: if we get an error here, we should fall back to normal scan */
50441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
50461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ret) {
50471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
50481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "ret=%d (%s)", ret, strerror(-ret));
50491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto nla_put_failure;
50501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
50511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
50531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   "scan interval %d msec", ret, interval);
50541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtnla_put_failure:
50561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
50571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return ret;
50581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
50591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt/**
50621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
50631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
50641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * Returns: 0 on success, -1 on failure or if not supported
50651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt */
50661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_stop_sched_scan(void *priv)
50671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
50681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *bss = priv;
50691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
50701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ret = 0;
50711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
50721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef ANDROID
50741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!drv->capa.sched_scan_supported)
50751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return android_pno_stop(bss);
50761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* ANDROID */
50771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
50791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
50801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
50811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
50831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
50851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
50871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
50881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ret) {
50891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
50901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "ret=%d (%s)", ret, strerror(-ret));
50911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto nla_put_failure;
50921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
50931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
50951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
50961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtnla_put_failure:
50971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
50981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return ret;
50991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
51001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
51011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
51021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
51031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
51041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	const u8 *end, *pos;
51051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
51061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ies == NULL)
51071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
51081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
51091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	pos = ies;
51101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	end = ies + ies_len;
51118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (pos + 1 < end) {
51138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (pos + 2 + pos[1] > end)
51148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
51158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (pos[0] == ie)
51168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos;
51178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += 2 + pos[1];
51188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
51198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NULL;
51218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
51228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
51258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 const u8 *ie, size_t ie_len)
51268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
51278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *ssid;
51288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t i;
51298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->filter_ssids == NULL)
51318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
51328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
51348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ssid == NULL)
51358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 1;
51368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < drv->num_filter_ssids; i++) {
51388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ssid[1] == drv->filter_ssids[i].ssid_len &&
51398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
51408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    0)
51418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return 0;
51428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
51438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 1;
51458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
51468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int bss_info_handler(struct nl_msg *msg, void *arg)
51498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
51508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
51518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
51528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *bss[NL80211_BSS_MAX + 1];
51538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
51548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
51558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
51568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_TSF] = { .type = NLA_U64 },
51578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
51588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
51598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
51608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
51618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
51628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_STATUS] = { .type = NLA_U32 },
51638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
51648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
51658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
51668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl80211_bss_info_arg *_arg = arg;
51678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_scan_results *res = _arg->res;
51688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_scan_res **tmp;
51698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_scan_res *r;
51708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *ie, *beacon_ie;
51718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t ie_len, beacon_ie_len;
51728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *pos;
517387fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	size_t i;
51748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
51758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
51768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
51778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!tb[NL80211_ATTR_BSS])
51788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
51798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
51808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     bss_policy))
51818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
518287fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	if (bss[NL80211_BSS_STATUS]) {
518387fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		enum nl80211_bss_status status;
518487fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		status = nla_get_u32(bss[NL80211_BSS_STATUS]);
518587fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		if (status == NL80211_BSS_STATUS_ASSOCIATED &&
518687fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		    bss[NL80211_BSS_FREQUENCY]) {
518787fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen			_arg->assoc_freq =
518887fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen				nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
518987fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen			wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
519087fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen				   _arg->assoc_freq);
519187fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		}
51921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (status == NL80211_BSS_STATUS_ASSOCIATED &&
51931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    bss[NL80211_BSS_BSSID]) {
51941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			os_memcpy(_arg->assoc_bssid,
51951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
51961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Associated with "
51971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   MACSTR, MAC2STR(_arg->assoc_bssid));
51981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
519987fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	}
520087fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	if (!res)
520187fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		return NL_SKIP;
52028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
52038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
52048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
52058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
52068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ie = NULL;
52078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ie_len = 0;
52088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
52098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss[NL80211_BSS_BEACON_IES]) {
52108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
52118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
52128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
52138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		beacon_ie = NULL;
52148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		beacon_ie_len = 0;
52158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
52168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
52178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
52188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  ie ? ie_len : beacon_ie_len))
52198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
52208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
52218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
52228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (r == NULL)
52238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
52248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss[NL80211_BSS_BSSID])
52258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
52268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  ETH_ALEN);
52278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss[NL80211_BSS_FREQUENCY])
52288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
52298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss[NL80211_BSS_BEACON_INTERVAL])
52308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
52318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss[NL80211_BSS_CAPABILITY])
52328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
52338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	r->flags |= WPA_SCAN_NOISE_INVALID;
52348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss[NL80211_BSS_SIGNAL_MBM]) {
52358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
52368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		r->level /= 100; /* mBm to dBm */
52378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
52388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
52398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
52401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		r->flags |= WPA_SCAN_QUAL_INVALID;
52418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else
52428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
52438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss[NL80211_BSS_TSF])
52448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
52458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss[NL80211_BSS_SEEN_MS_AGO])
52468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
52478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	r->ie_len = ie_len;
52488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = (u8 *) (r + 1);
52498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ie) {
52508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(pos, ie, ie_len);
52518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ie_len;
52528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
52538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	r->beacon_ie_len = beacon_ie_len;
52548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (beacon_ie)
52558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(pos, beacon_ie, beacon_ie_len);
52568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
52578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss[NL80211_BSS_STATUS]) {
52588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		enum nl80211_bss_status status;
52598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		status = nla_get_u32(bss[NL80211_BSS_STATUS]);
52608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		switch (status) {
52618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		case NL80211_BSS_STATUS_AUTHENTICATED:
52628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			r->flags |= WPA_SCAN_AUTHENTICATED;
52638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
52648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		case NL80211_BSS_STATUS_ASSOCIATED:
52658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			r->flags |= WPA_SCAN_ASSOCIATED;
52668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
52678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		default:
52688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
52698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
52708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
52718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
527287fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	/*
527387fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	 * cfg80211 maintains separate BSS table entries for APs if the same
527487fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
527587fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	 * not use frequency as a separate key in the BSS table, so filter out
527687fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	 * duplicated entries. Prefer associated BSS entry in such a case in
5277fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	 * order to get the correct frequency into the BSS table. Similarly,
5278fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	 * prefer newer entries over older.
527987fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	 */
528087fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	for (i = 0; i < res->num; i++) {
528187fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		const u8 *s1, *s2;
528287fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
528387fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen			continue;
528487fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
528587fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
528687fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen				    res->res[i]->ie_len, WLAN_EID_SSID);
528787fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
528887fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
528987fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		    os_memcmp(s1, s2, 2 + s1[1]) != 0)
529087fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen			continue;
529187fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
529287fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		/* Same BSSID,SSID was already included in scan results */
529387fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
529487fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen			   "for " MACSTR, MAC2STR(r->bssid));
529587fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
5296fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5297fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		     !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5298fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		    r->age < res->res[i]->age) {
529987fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen			os_free(res->res[i]);
530087fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen			res->res[i] = r;
530187fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		} else
530287fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen			os_free(r);
530387fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen		return NL_SKIP;
530487fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	}
530587fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen
530661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	tmp = os_realloc_array(res->res, res->num + 1,
530761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			       sizeof(struct wpa_scan_res *));
53088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tmp == NULL) {
53098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(r);
53108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
53118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
53128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	tmp[res->num++] = r;
53138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	res->res = tmp;
53148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
53168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
53178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
53208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 const u8 *addr)
53218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
53228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
53238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
53248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "mismatch (" MACSTR ")", MAC2STR(addr));
53258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_driver_nl80211_mlme(drv, addr,
53268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					NL80211_CMD_DEAUTHENTICATE,
53278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
53288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
53298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
53308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_check_bss_status(
53338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
53348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
53358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t i;
53368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < res->num; i++) {
53388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct wpa_scan_res *r = res->res[i];
53398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (r->flags & WPA_SCAN_AUTHENTICATED) {
53408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Scan results "
53418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "indicates BSS status with " MACSTR
53428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   " as authenticated",
53438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   MAC2STR(r->bssid));
53441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (is_sta_interface(drv->nlmode) &&
53458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
53468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
53478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    0) {
53488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
53498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   " in local state (auth=" MACSTR
53508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   " assoc=" MACSTR ")",
53518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   MAC2STR(drv->auth_bssid),
53528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   MAC2STR(drv->bssid));
53538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				clear_state_mismatch(drv, r->bssid);
53548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
53558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
53568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (r->flags & WPA_SCAN_ASSOCIATED) {
53588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Scan results "
53598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "indicate BSS status with " MACSTR
53608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   " as associated",
53618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   MAC2STR(r->bssid));
53621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (is_sta_interface(drv->nlmode) &&
53638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    !drv->associated) {
53648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wpa_printf(MSG_DEBUG, "nl80211: Local state "
53658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   "(not associated) does not match "
53668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   "with BSS state");
53678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				clear_state_mismatch(drv, r->bssid);
53681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			} else if (is_sta_interface(drv->nlmode) &&
53698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
53708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   0) {
53718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wpa_printf(MSG_DEBUG, "nl80211: Local state "
53728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   "(associated with " MACSTR ") does "
53738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   "not match with BSS state",
53748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   MAC2STR(drv->bssid));
53758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				clear_state_mismatch(drv, r->bssid);
53768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				clear_state_mismatch(drv, drv->bssid);
53778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
53788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
53798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
53808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
53818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct wpa_scan_results *
53848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
53858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
53868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
53878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_scan_results *res;
53888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
53898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl80211_bss_info_arg arg;
53908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	res = os_zalloc(sizeof(*res));
53928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (res == NULL)
53938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
53948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
53958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
53968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
53978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
53981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
5399cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
540034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
54018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	arg.drv = drv;
54038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	arg.res = res;
54048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
54058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
54068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret == 0) {
54071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
54081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "BSSes)", (unsigned long) res->num);
54091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_get_noise_for_scan_results(drv, res);
54108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return res;
54118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
54128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
54138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "(%s)", ret, strerror(-ret));
54148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
54158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
54168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_scan_results_free(res);
54178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NULL;
54188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
54198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
54228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
54238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
54248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: Scan results on success, -1 on failure
54258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
54268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct wpa_scan_results *
54278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtwpa_driver_nl80211_get_scan_results(void *priv)
54288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
54298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
54308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
54318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_scan_results *res;
54328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	res = nl80211_get_scan_results(drv);
54348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (res)
54358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_driver_nl80211_check_bss_status(drv, res);
54368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return res;
54378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
54388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
54418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
54428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_scan_results *res;
54438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t i;
54448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	res = nl80211_get_scan_results(drv);
54468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (res == NULL) {
54478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
54488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
54498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
54508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
54528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < res->num; i++) {
54538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct wpa_scan_res *r = res->res[i];
54548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
54558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   (int) i, (int) res->num, MAC2STR(r->bssid),
54568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
54578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
54588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
54598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_scan_results_free(res);
54618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
54628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5464fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5465fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt{
5466fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	switch (alg) {
5467fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_WEP:
5468fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		if (key_len == 5)
5469fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			return WLAN_CIPHER_SUITE_WEP40;
5470fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_WEP104;
5471fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_TKIP:
5472fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_TKIP;
5473fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_CCMP:
5474fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_CCMP;
5475fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_GCMP:
5476fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_GCMP;
5477fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_CCMP_256:
5478fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_CCMP_256;
5479fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_GCMP_256:
5480fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_GCMP_256;
5481fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_IGTK:
5482fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_AES_CMAC;
5483fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_BIP_GMAC_128:
5484fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5485fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_BIP_GMAC_256:
5486fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5487fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_BIP_CMAC_256:
5488fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5489fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_SMS4:
5490fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_SMS4;
5491fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_KRK:
5492fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_KRK;
5493fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_NONE:
5494fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_ALG_PMK:
5495fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5496fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   alg);
5497fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return 0;
5498fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
5499fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
5500fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5501fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		   alg);
5502fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	return 0;
5503fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt}
5504fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
5505fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
5506fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5507fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt{
5508fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	switch (cipher) {
5509fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_CIPHER_CCMP_256:
5510fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_CCMP_256;
5511fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_CIPHER_GCMP_256:
5512fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_GCMP_256;
5513fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_CIPHER_CCMP:
5514fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_CCMP;
5515fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_CIPHER_GCMP:
5516fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_GCMP;
5517fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_CIPHER_TKIP:
5518fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_TKIP;
5519fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_CIPHER_WEP104:
5520fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_WEP104;
5521fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	case WPA_CIPHER_WEP40:
5522fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return WLAN_CIPHER_SUITE_WEP40;
5523f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt	case WPA_CIPHER_GTK_NOT_USED:
5524f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
5525fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
5526fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
5527fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	return 0;
5528fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt}
5529fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
5530fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
5531fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5532fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				       int max_suites)
5533fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt{
5534fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int num_suites = 0;
5535fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
5536fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5537fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5538fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5539fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5540fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5541fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5542fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5543fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5544fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5545fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5546fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5547fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5548fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5549fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5550fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
5551fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	return num_suites;
5552fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt}
5553fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
5554fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
55554b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
55568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      enum wpa_alg alg, const u8 *addr,
55578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      int key_idx, int set_tx,
55588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      const u8 *seq, size_t seq_len,
55598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      const u8 *key, size_t key_len)
55608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
55618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
556234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	int ifindex;
55638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
55648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
5565d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	int tdls = 0;
55668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
556734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	/* Ignore for P2P Device */
556834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
556934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return 0;
557034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
557134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	ifindex = if_nametoindex(ifname);
557234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
55738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "set_tx=%d seq_len=%lu key_len=%lu",
557434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		   __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
55758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (unsigned long) seq_len, (unsigned long) key_len);
55761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef CONFIG_TDLS
5577d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	if (key_idx == -1) {
55781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		key_idx = 0;
5579d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt		tdls = 1;
5580d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	}
55811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* CONFIG_TDLS */
55828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
55838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
55848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
55858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
55868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
55878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (alg == WPA_ALG_NONE) {
55881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
55898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
55901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
55918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
5592fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5593fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			    wpa_alg_to_cipher_suite(alg, key_len));
55948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
55958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
55968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (seq && seq_len)
55978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
55988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
55998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (addr && !is_broadcast_ether_addr(addr)) {
56008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
56018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
56028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
56048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "   RSN IBSS RX GTK");
56058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
56068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    NL80211_KEYTYPE_GROUP);
56078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
56088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else if (addr && is_broadcast_ether_addr(addr)) {
56098da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		struct nlattr *types;
56108da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt
56118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "   broadcast key");
56128da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt
56138da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
56148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!types)
56158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			goto nla_put_failure;
56168da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
56178da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		nla_nest_end(msg, types);
56188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
56198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
56208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
56218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
56238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
56248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = 0;
56258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret)
56268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
56278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   ret, strerror(-ret));
56288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
56308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * If we failed or don't need to set the default TX key (below),
56318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * we're done here.
56328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
5633d5c075b0c218277d0f926daf1f9eff974b9656dcDmitry Shmidt	if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
56348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return ret;
56351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (is_ap_interface(drv->nlmode) && addr &&
56368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    !is_broadcast_ether_addr(addr))
56378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return ret;
56388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
56408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
56418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
56428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
56448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
56458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
56468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (alg == WPA_ALG_IGTK)
56478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
56488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
56498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
56508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (addr && is_broadcast_ether_addr(addr)) {
56518da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		struct nlattr *types;
56528da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt
56538da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
56548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!types)
56558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			goto nla_put_failure;
56568da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
56578da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		nla_nest_end(msg, types);
56588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else if (addr) {
56598da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		struct nlattr *types;
56608da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt
56618da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
56628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!types)
56638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			goto nla_put_failure;
56648da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
56658da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		nla_nest_end(msg, types);
56668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
56678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
56698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret == -ENOENT)
56708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = 0;
56718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret)
56728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
56738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "err=%d %s)", ret, strerror(-ret));
56748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
56758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
56771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
56788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
56798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
56808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
56838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		      int key_idx, int defkey,
56848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		      const u8 *seq, size_t seq_len,
56858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		      const u8 *key, size_t key_len)
56868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
56878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
56888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!key_attr)
56898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
56908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (defkey && alg == WPA_ALG_IGTK)
56928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
56938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else if (defkey)
56948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
56958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
56968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
56978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5698fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5699fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		    wpa_alg_to_cipher_suite(alg, key_len));
57008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (seq && seq_len)
57028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
57038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
57058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_nest_end(msg, key_attr);
57078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
57098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
57108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
57118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
57128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
57158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 struct nl_msg *msg)
57168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
57178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int i, privacy = 0;
57188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *nl_keys, *nl_key;
57198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < 4; i++) {
57218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!params->wep_key[i])
57228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
57238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		privacy = 1;
57248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
57258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
57268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->wps == WPS_MODE_PRIVACY)
57278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		privacy = 1;
57288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->pairwise_suite &&
57298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    params->pairwise_suite != WPA_CIPHER_NONE)
57308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		privacy = 1;
57318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!privacy)
57338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
57348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
57368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
57388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!nl_keys)
57398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
57408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < 4; i++) {
57428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!params->wep_key[i])
57438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
57448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl_key = nla_nest_start(msg, i);
57468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!nl_key)
57478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			goto nla_put_failure;
57488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
57508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			params->wep_key[i]);
57518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (params->wep_key_len[i] == 5)
57528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
57538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    WLAN_CIPHER_SUITE_WEP40);
57548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		else
57558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
57568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    WLAN_CIPHER_SUITE_WEP104);
57578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
57598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (i == params->wep_tx_keyidx)
57618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
57628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nla_nest_end(msg, nl_key);
57648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
57658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_nest_end(msg, nl_keys);
57668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
57688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
57708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
57718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
57728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
57758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   const u8 *addr, int cmd, u16 reason_code,
57768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   int local_state_change)
57778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
57788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -1;
57798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
57808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
57828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
57838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
57848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, cmd);
57868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
57888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
5789d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (addr)
5790d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
57918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (local_state_change)
57928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
57938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
57948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
57958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
57968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
57971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_dbg(drv->ctx, MSG_DEBUG,
57981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			"nl80211: MLME command failed: reason=%u ret=%d (%s)",
57991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			reason_code, ret, strerror(-ret));
58008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
58018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
58028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = 0;
58038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
58048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
58058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
58068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
58078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
58088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
58098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
58108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
5811d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt					 int reason_code)
58128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5813c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	int ret;
5814c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt
5815d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
58168bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	nl80211_mark_disconnected(drv);
5817d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	/* Disconnect command doesn't need BSSID - it uses cached value */
5818c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5819c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt				      reason_code, 0);
5820c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	/*
5821c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	 * For locally generated disconnect, supplicant already generates a
5822c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	 * DEAUTH event, so ignore the event from NL80211.
5823c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	 */
5824c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	drv->ignore_next_local_disconnect = ret == 0;
5825c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt
5826c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	return ret;
58278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
58288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
58298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
58304b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
58314b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					     const u8 *addr, int reason_code)
58328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
58338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
58348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
5835d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		return wpa_driver_nl80211_disconnect(drv, reason_code);
58368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
58378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   __func__, MAC2STR(addr), reason_code);
58388bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	nl80211_mark_disconnected(drv);
58398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->nlmode == NL80211_IFTYPE_ADHOC)
58408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return nl80211_leave_ibss(drv);
58418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
58428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       reason_code, 0);
58438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
58448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
58458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
58461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
58471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     struct wpa_driver_auth_params *params)
58481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
58491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int i;
58501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
58511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->auth_freq = params->freq;
58521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->auth_alg = params->auth_alg;
58531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
58541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->auth_local_state_change = params->local_state_change;
58551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->auth_p2p = params->p2p;
58561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
58571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->bssid)
58581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
58591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	else
58601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		os_memset(drv->auth_bssid_, 0, ETH_ALEN);
58611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
58621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->ssid) {
58631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
58641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->auth_ssid_len = params->ssid_len;
58651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	} else
58661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->auth_ssid_len = 0;
58671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
58681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
58691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_free(drv->auth_ie);
58701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->auth_ie = NULL;
58711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->auth_ie_len = 0;
58721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->ie) {
58731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->auth_ie = os_malloc(params->ie_len);
58741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (drv->auth_ie) {
58751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			os_memcpy(drv->auth_ie, params->ie, params->ie_len);
58761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			drv->auth_ie_len = params->ie_len;
58771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
58781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
58791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
58801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	for (i = 0; i < 4; i++) {
58811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (params->wep_key[i] && params->wep_key_len[i] &&
58821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    params->wep_key_len[i] <= 16) {
58831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
58841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  params->wep_key_len[i]);
58851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			drv->auth_wep_key_len[i] = params->wep_key_len[i];
58861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		} else
58871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			drv->auth_wep_key_len[i] = 0;
58881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
58891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
58901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
58911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
58928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_authenticate(
58934b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss, struct wpa_driver_auth_params *params)
58948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
58958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
58968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -1, i;
58978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
58988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	enum nl80211_auth_type type;
58991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	enum nl80211_iftype nlmode;
59008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int count = 0;
59011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int is_retry;
59021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
59031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	is_retry = drv->retry_auth;
59041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->retry_auth = 0;
59058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
59068bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	nl80211_mark_disconnected(drv);
59078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(drv->auth_bssid, 0, ETH_ALEN);
59088bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	if (params->bssid)
59098bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
59108bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	else
59118bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
59128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* FIX: IBSS mode */
59131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmode = params->p2p ?
59141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
59151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->nlmode != nlmode &&
59164b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	    wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
59178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
59188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
59198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtretry:
59208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
59218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
59228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
59238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
59248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
59258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   drv->ifindex);
59268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
59271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
59288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
59298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < 4; i++) {
59308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!params->wep_key[i])
59318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
59324b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
59338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   NULL, i,
59348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   i == params->wep_tx_keyidx, NULL, 0,
59358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   params->wep_key[i],
59368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   params->wep_key_len[i]);
59378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (params->wep_tx_keyidx != i)
59388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
59398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
59408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       params->wep_key[i], params->wep_key_len[i])) {
59418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nlmsg_free(msg);
59428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
59438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
59448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
59458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
59468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
59478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->bssid) {
59488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
59498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MAC2STR(params->bssid));
59508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
59518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
59528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->freq) {
59538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
59548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
59558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
59568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->ssid) {
59578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
59588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  params->ssid, params->ssid_len);
59598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
59608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			params->ssid);
59618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
59628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "  * IEs", params->ie, params->ie_len);
59638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->ie)
59648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
5965d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (params->sae_data) {
5966d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_hexdump(MSG_DEBUG, "  * SAE data", params->sae_data,
5967d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			    params->sae_data_len);
5968d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
5969d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			params->sae_data);
5970d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	}
59718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->auth_alg & WPA_AUTH_ALG_OPEN)
59728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		type = NL80211_AUTHTYPE_OPEN_SYSTEM;
59738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
59748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		type = NL80211_AUTHTYPE_SHARED_KEY;
59758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
59768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		type = NL80211_AUTHTYPE_NETWORK_EAP;
59778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else if (params->auth_alg & WPA_AUTH_ALG_FT)
59788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		type = NL80211_AUTHTYPE_FT;
5979d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	else if (params->auth_alg & WPA_AUTH_ALG_SAE)
5980d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		type = NL80211_AUTHTYPE_SAE;
59818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
59828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
59838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
59848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
59858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->local_state_change) {
59868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * Local state change only");
59878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
59888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
59898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
59908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
59918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
59928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
59931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_dbg(drv->ctx, MSG_DEBUG,
59941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			"nl80211: MLME command failed (auth): ret=%d (%s)",
59951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			ret, strerror(-ret));
59968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		count++;
59978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret == -EALREADY && count == 1 && params->bssid &&
59988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    !params->local_state_change) {
59998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			/*
60008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 * mac80211 does not currently accept new
60018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 * authentication if we are already authenticated. As a
60028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 * workaround, force deauthentication and try again.
60038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 */
60048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
60058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "after forced deauthentication");
60068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_driver_nl80211_deauthenticate(
60078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				bss, params->bssid,
60088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				WLAN_REASON_PREV_AUTH_NOT_VALID);
60098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nlmsg_free(msg);
60108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			goto retry;
60118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
60121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
60131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (ret == -ENOENT && params->freq && !is_retry) {
60141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			/*
60151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * cfg80211 has likely expired the BSS entry even
60161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * though it was previously available in our internal
60171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * BSS table. To recover quickly, start a single
60181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * channel scan on the specified channel.
60191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 */
60201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			struct wpa_driver_scan_params scan;
60211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			int freqs[2];
60221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
60231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			os_memset(&scan, 0, sizeof(scan));
60241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			scan.num_ssids = 1;
60251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (params->ssid) {
60261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				scan.ssids[0].ssid = params->ssid;
60271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				scan.ssids[0].ssid_len = params->ssid_len;
60281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			}
60291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			freqs[0] = params->freq;
60301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			freqs[1] = 0;
60311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			scan.freqs = freqs;
60321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
60331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "channel scan to refresh cfg80211 BSS "
60341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "entry");
60351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			ret = wpa_driver_nl80211_scan(bss, &scan);
60361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (ret == 0) {
60371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				nl80211_copy_auth_params(drv, params);
60381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				drv->scan_for_auth = 1;
60391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			}
60401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		} else if (is_retry) {
60411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			/*
60421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * Need to indicate this with an event since the return
60431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * value from the retry is not delivered to core code.
60441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 */
60451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			union wpa_event_data event;
60461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
60471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "failed");
60481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			os_memset(&event, 0, sizeof(event));
60491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
60501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  ETH_ALEN);
60511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
60521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					     &event);
60531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
60541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
60558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
60568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
60578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = 0;
60588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
60598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "successfully");
60608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
60618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
60628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
60638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
60648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
60658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
60668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
60671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_authenticate_retry(
60681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv)
60691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
60701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_auth_params params;
6071cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	struct i802_bss *bss = drv->first_bss;
60721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int i;
60731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
60741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
60751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
60761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&params, 0, sizeof(params));
60771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	params.freq = drv->auth_freq;
60781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	params.auth_alg = drv->auth_alg;
60791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
60801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	params.local_state_change = drv->auth_local_state_change;
60811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	params.p2p = drv->auth_p2p;
60821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
60831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!is_zero_ether_addr(drv->auth_bssid_))
60841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		params.bssid = drv->auth_bssid_;
60851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
60861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->auth_ssid_len) {
60871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		params.ssid = drv->auth_ssid;
60881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		params.ssid_len = drv->auth_ssid_len;
60891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
60901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
60911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	params.ie = drv->auth_ie;
60921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	params.ie_len = drv->auth_ie_len;
60931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
60941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	for (i = 0; i < 4; i++) {
60951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (drv->auth_wep_key_len[i]) {
60961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			params.wep_key[i] = drv->auth_wep_key[i];
60971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			params.wep_key_len[i] = drv->auth_wep_key_len[i];
60981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
60991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
61001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
61011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv->retry_auth = 1;
61021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return wpa_driver_nl80211_authenticate(bss, &params);
61031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
61041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
61051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
61068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct phy_info_arg {
61078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 *num_modes;
61088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct hostapd_hw_modes *modes;
61092f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int last_mode, last_chan_idx;
61108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
61118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
61122f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
61132f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			     struct nlattr *ampdu_factor,
61142f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			     struct nlattr *ampdu_density,
61152f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			     struct nlattr *mcs_set)
61168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
61172f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (capa)
61182f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mode->ht_capab = nla_get_u16(capa);
61198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
61202f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (ampdu_factor)
61212f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
61228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
61232f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (ampdu_density)
61242f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
61252f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
61262f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (mcs_set && nla_len(mcs_set) >= 16) {
61272f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		u8 *mcs;
61282f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mcs = nla_data(mcs_set);
61292f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		os_memcpy(mode->mcs_set, mcs, 16);
61302f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
61312f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
61322f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
61332f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
61342f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic void phy_info_vht_capa(struct hostapd_hw_modes *mode,
61352f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			      struct nlattr *capa,
61362f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			      struct nlattr *mcs_set)
61372f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
61382f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (capa)
61392f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mode->vht_capab = nla_get_u32(capa);
61402f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
61412f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (mcs_set && nla_len(mcs_set) >= 8) {
61422f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		u8 *mcs;
61432f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mcs = nla_data(mcs_set);
61442f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		os_memcpy(mode->vht_mcs_set, mcs, 8);
61452f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
61462f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
61472f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
61482f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
61492f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic void phy_info_freq(struct hostapd_hw_modes *mode,
61502f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			  struct hostapd_channel_data *chan,
61512f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			  struct nlattr *tb_freq[])
61522f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
61534b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	u8 channel;
61542f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
61552f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	chan->flag = 0;
61564b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
61574b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt		chan->chan = channel;
61582f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
61592f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
61602f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		chan->flag |= HOSTAPD_CHAN_DISABLED;
6161fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6162fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
61632f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
61642f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		chan->flag |= HOSTAPD_CHAN_RADAR;
61652f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
6166ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6167ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		enum nl80211_dfs_state state =
6168ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt			nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6169ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
6170ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		switch (state) {
6171ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		case NL80211_DFS_USABLE:
6172ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt			chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6173ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt			break;
6174ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		case NL80211_DFS_AVAILABLE:
6175ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt			chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6176ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt			break;
6177ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		case NL80211_DFS_UNAVAILABLE:
6178ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt			chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6179ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt			break;
6180ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		}
6181ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	}
61822f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
61832f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
61842f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
61852f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic int phy_info_freqs(struct phy_info_arg *phy_info,
61862f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			  struct hostapd_hw_modes *mode, struct nlattr *tb)
61872f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
61888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
61898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
61908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
6191fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		[NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
61928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
61938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
6194ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		[NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
61958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
61962f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int new_channels = 0;
61972f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct hostapd_channel_data *channel;
61982f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
61998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *nl_freq;
62002f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int rem_freq, idx;
62018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62022f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb == NULL)
62032f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return NL_OK;
62048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62052f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_for_each_nested(nl_freq, tb, rem_freq) {
62062f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
62072f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
62082f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
62092f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			continue;
62102f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		new_channels++;
62112f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
62122f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
62132f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	channel = os_realloc_array(mode->channels,
62142f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				   mode->num_channels + new_channels,
62152f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt				   sizeof(struct hostapd_channel_data));
62162f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (!channel)
62178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
62188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62192f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	mode->channels = channel;
62202f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	mode->num_channels += new_channels;
62218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62222f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	idx = phy_info->last_chan_idx;
62238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62242f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_for_each_nested(nl_freq, tb, rem_freq) {
62252f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
62262f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
62272f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
62282f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			continue;
62292f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		phy_info_freq(mode, &mode->channels[idx], tb_freq);
62302f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		idx++;
62312f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
62322f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	phy_info->last_chan_idx = idx;
62338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62342f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	return NL_OK;
62352f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
62368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62382f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
62392f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
62402f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
62412f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		[NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
62422f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
62432f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		{ .type = NLA_FLAG },
62442f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	};
62452f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
62462f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *nl_rate;
62472f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int rem_rate, idx;
62488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62492f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (tb == NULL)
62502f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return NL_OK;
62518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62522f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_for_each_nested(nl_rate, tb, rem_rate) {
62532f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
62542f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			  nla_data(nl_rate), nla_len(nl_rate),
62552f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			  rate_policy);
62562f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
62572f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			continue;
62582f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mode->num_rates++;
62592f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
62608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62612f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	mode->rates = os_calloc(mode->num_rates, sizeof(int));
62622f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (!mode->rates)
62632f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return NL_SKIP;
626404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
62652f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	idx = 0;
626604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
62672f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_for_each_nested(nl_rate, tb, rem_rate) {
62682f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
62692f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			  nla_data(nl_rate), nla_len(nl_rate),
62702f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			  rate_policy);
62712f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
62722f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			continue;
62732f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mode->rates[idx] = nla_get_u32(
62742f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			tb_rate[NL80211_BITRATE_ATTR_RATE]);
62752f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		idx++;
62762f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
62778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62782f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	return NL_OK;
62792f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
62808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62822f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
62832f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
62842f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
62852f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct hostapd_hw_modes *mode;
62862f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int ret;
62878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62882f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (phy_info->last_mode != nl_band->nla_type) {
62892f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mode = os_realloc_array(phy_info->modes,
62902f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt					*phy_info->num_modes + 1,
62912f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt					sizeof(*mode));
62922f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		if (!mode)
62938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return NL_SKIP;
62942f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		phy_info->modes = mode;
62958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
62962f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mode = &phy_info->modes[*(phy_info->num_modes)];
62972f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		os_memset(mode, 0, sizeof(*mode));
62982f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mode->mode = NUM_HOSTAPD_MODES;
6299c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6300c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt			HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6301c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt
6302c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		/*
6303c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6304c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6305c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		 * possible streams as unsupported. This will be overridden if
6306c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		 * driver advertises VHT support.
6307c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		 */
6308c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		mode->vht_mcs_set[0] = 0xff;
6309c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		mode->vht_mcs_set[1] = 0xff;
6310c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		mode->vht_mcs_set[4] = 0xff;
6311c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		mode->vht_mcs_set[5] = 0xff;
6312c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt
63132f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		*(phy_info->num_modes) += 1;
63142f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		phy_info->last_mode = nl_band->nla_type;
63152f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		phy_info->last_chan_idx = 0;
63162f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	} else
63172f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		mode = &phy_info->modes[*(phy_info->num_modes) - 1];
63182f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
63192f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
63202f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		  nla_len(nl_band), NULL);
63212f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
63222f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
63232f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
63242f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
63252f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
63262f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
63272f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			  tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
63282f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
63292f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (ret != NL_OK)
63302f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return ret;
63312f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
63322f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (ret != NL_OK)
63332f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return ret;
63348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
63352f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	return NL_OK;
63362f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt}
63378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
63388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
63392f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidtstatic int phy_info_handler(struct nl_msg *msg, void *arg)
63402f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt{
63412f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
63422f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
63432f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct phy_info_arg *phy_info = arg;
63442f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	struct nlattr *nl_band;
63452f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	int rem_band;
63462f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
63472f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
63482f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
63492f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
63502f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
63512f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		return NL_SKIP;
63522f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
63532f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
63542f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	{
63552f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		int res = phy_info_band(phy_info, nl_band);
63562f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		if (res != NL_OK)
63572f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			return res;
63588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
63598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
63608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
63618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
63628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
63632f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
63648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct hostapd_hw_modes *
63650ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidtwpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
63660ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				     u16 *num_modes)
63678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
63688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 m;
63698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
63708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int i, mode11g_idx = -1;
63718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
63720ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	/* heuristic to set up modes */
63730ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	for (m = 0; m < *num_modes; m++) {
63740ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		if (!modes[m].num_channels)
63750ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			continue;
63760ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		if (modes[m].channels[0].freq < 4000) {
63770ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			modes[m].mode = HOSTAPD_MODE_IEEE80211B;
63780ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			for (i = 0; i < modes[m].num_rates; i++) {
63790ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				if (modes[m].rates[i] > 200) {
63800ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt					modes[m].mode = HOSTAPD_MODE_IEEE80211G;
63810ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt					break;
63820ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				}
63830ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			}
63840ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		} else if (modes[m].channels[0].freq > 50000)
63850ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
63860ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		else
63870ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			modes[m].mode = HOSTAPD_MODE_IEEE80211A;
63880ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	}
63890ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt
63908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* If only 802.11g mode is included, use it to construct matching
63918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * 802.11b mode data. */
63928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
63938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (m = 0; m < *num_modes; m++) {
63948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
63958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return modes; /* 802.11b already included */
63968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
63978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			mode11g_idx = m;
63988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
63998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (mode11g_idx < 0)
64018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return modes; /* 2.4 GHz band not supported at all */
64028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
640361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
64048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (nmodes == NULL)
64058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return modes; /* Could not add 802.11b mode */
64068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mode = &nmodes[*num_modes];
64088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(mode, 0, sizeof(*mode));
64098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	(*num_modes)++;
64108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	modes = nmodes;
64118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mode->mode = HOSTAPD_MODE_IEEE80211B;
64138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mode11g = &modes[mode11g_idx];
64158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mode->num_channels = mode11g->num_channels;
64168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mode->channels = os_malloc(mode11g->num_channels *
64178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   sizeof(struct hostapd_channel_data));
64188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (mode->channels == NULL) {
64198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		(*num_modes)--;
64208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return modes; /* Could not add 802.11b mode */
64218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
64228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(mode->channels, mode11g->channels,
64238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  mode11g->num_channels * sizeof(struct hostapd_channel_data));
64248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mode->num_rates = 0;
64268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mode->rates = os_malloc(4 * sizeof(int));
64278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (mode->rates == NULL) {
64288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(mode->channels);
64298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		(*num_modes)--;
64308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return modes; /* Could not add 802.11b mode */
64318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
64328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < mode11g->num_rates; i++) {
64348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
64358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
64368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
64378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mode->rates[mode->num_rates] = mode11g->rates[i];
64388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		mode->num_rates++;
64398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (mode->num_rates == 4)
64408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
64418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
64428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (mode->num_rates == 0) {
64448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(mode->channels);
64458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(mode->rates);
64468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		(*num_modes)--;
64478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return modes; /* No 802.11b rates */
64488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
64498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
64518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "information");
64528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return modes;
64548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
64558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
64588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  int end)
64598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
64608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int c;
64618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (c = 0; c < mode->num_channels; c++) {
64638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct hostapd_channel_data *chan = &mode->channels[c];
64648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (chan->freq - 10 >= start && chan->freq + 10 <= end)
64658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			chan->flag |= HOSTAPD_CHAN_HT40;
64668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
64678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
64688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
64718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      int end)
64728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
64738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int c;
64748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (c = 0; c < mode->num_channels; c++) {
64768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct hostapd_channel_data *chan = &mode->channels[c];
64778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!(chan->flag & HOSTAPD_CHAN_HT40))
64788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
64798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (chan->freq - 30 >= start && chan->freq - 10 <= end)
64808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			chan->flag |= HOSTAPD_CHAN_HT40MINUS;
64818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (chan->freq + 10 >= start && chan->freq + 30 <= end)
64828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			chan->flag |= HOSTAPD_CHAN_HT40PLUS;
64838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
64848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
64858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
64868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6487fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
648868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				      struct phy_info_arg *results)
648968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt{
649068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	u16 m;
649168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
649268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	for (m = 0; m < *results->num_modes; m++) {
649368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		int c;
649468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		struct hostapd_hw_modes *mode = &results->modes[m];
649568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
649668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		for (c = 0; c < mode->num_channels; c++) {
649768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			struct hostapd_channel_data *chan = &mode->channels[c];
649868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			if ((u32) chan->freq - 10 >= start &&
649968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			    (u32) chan->freq + 10 <= end)
650068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				chan->max_tx_power = max_eirp;
650168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		}
650268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	}
650368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt}
650468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
650568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
6506fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic void nl80211_reg_rule_ht40(u32 start, u32 end,
65078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  struct phy_info_arg *results)
65088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
65098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 m;
65108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
65118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (m = 0; m < *results->num_modes; m++) {
65128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!(results->modes[m].ht_capab &
65138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
65148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
65158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_set_ht40_mode(&results->modes[m], start, end);
65168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
65178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
65188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
65198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
65208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_reg_rule_sec(struct nlattr *tb[],
65218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 struct phy_info_arg *results)
65228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
65238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 start, end, max_bw;
65248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 m;
65258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
65268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
65278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
65288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
65298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
65308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
65318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
65328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
65338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
65348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
65358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (max_bw < 20)
65368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
65378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
65388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (m = 0; m < *results->num_modes; m++) {
65398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!(results->modes[m].ht_capab &
65408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
65418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
65428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
65438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
65448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
65458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
65468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
654768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidtstatic void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
654868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				 int end)
654968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt{
655068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	int c;
655168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
655268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	for (c = 0; c < mode->num_channels; c++) {
655368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		struct hostapd_channel_data *chan = &mode->channels[c];
655468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		if (chan->freq - 10 >= start && chan->freq + 70 <= end)
655568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			chan->flag |= HOSTAPD_CHAN_VHT_10_70;
655668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
655768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		if (chan->freq - 30 >= start && chan->freq + 50 <= end)
655868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			chan->flag |= HOSTAPD_CHAN_VHT_30_50;
655968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
656068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		if (chan->freq - 50 >= start && chan->freq + 30 <= end)
656168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			chan->flag |= HOSTAPD_CHAN_VHT_50_30;
656268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
656368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		if (chan->freq - 70 >= start && chan->freq + 10 <= end)
656468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			chan->flag |= HOSTAPD_CHAN_VHT_70_10;
656568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	}
656668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt}
656768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
656868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
656968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidtstatic void nl80211_reg_rule_vht(struct nlattr *tb[],
657068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				 struct phy_info_arg *results)
657168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt{
657268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	u32 start, end, max_bw;
657368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	u16 m;
657468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
657568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
657668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	    tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
657768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	    tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
657868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		return;
657968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
658068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
658168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
658268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
658368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
658468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	if (max_bw < 80)
658568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		return;
658668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
658768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	for (m = 0; m < *results->num_modes; m++) {
658868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		if (!(results->modes[m].ht_capab &
658968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
659068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			continue;
659168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		/* TODO: use a real VHT support indication */
659268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		if (!results->modes[m].vht_capab)
659368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			continue;
659468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
659568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		nl80211_set_vht_mode(&results->modes[m], start, end);
659668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	}
659768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt}
659868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
659968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
66009767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidtstatic const char * dfs_domain_name(enum nl80211_dfs_regions region)
66019767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt{
66029767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	switch (region) {
66039767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case NL80211_DFS_UNSET:
66049767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		return "DFS-UNSET";
66059767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case NL80211_DFS_FCC:
66069767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		return "DFS-FCC";
66079767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case NL80211_DFS_ETSI:
66089767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		return "DFS-ETSI";
66099767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	case NL80211_DFS_JP:
66109767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		return "DFS-JP";
66119767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	default:
66129767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		return "DFS-invalid";
66139767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	}
66149767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt}
66159767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt
66169767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt
66178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_get_reg(struct nl_msg *msg, void *arg)
66188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
66198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct phy_info_arg *results = arg;
66208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
66218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
66228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *nl_rule;
66238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
66248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int rem_rule;
66258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
66268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
66278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
66288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
66298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
66308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
66318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
66328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
66338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
66348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
66358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
66368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
66378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    !tb_msg[NL80211_ATTR_REG_RULES]) {
66388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
66398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "available");
66408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
66418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
66428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
66439767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	if (tb_msg[NL80211_ATTR_DFS_REGION]) {
66449767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		enum nl80211_dfs_regions dfs_domain;
66459767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
66469767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
66479767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt			   (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
66489767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt			   dfs_domain_name(dfs_domain));
66499767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	} else {
66509767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
66519767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt			   (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
66529767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	}
66538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
66548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
66558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	{
6656f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
66578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
66588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6659fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6660fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		    tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6661fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			continue;
6662fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6663fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6664fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6665fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6666fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6667fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6668f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
6669f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
6670f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt
6671f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
6672f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			   start, end, max_bw, max_eirp,
6673f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			   flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
6674f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			   flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
6675f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			   flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
6676f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			   flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
6677f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			   "",
6678f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			   flags & NL80211_RRF_DFS ? " (DFS)" : "",
6679f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			   flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
6680f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			   flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
6681f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt			   flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
6682fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		if (max_bw >= 40)
6683fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			nl80211_reg_rule_ht40(start, end, results);
6684fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6685fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			nl80211_reg_rule_max_eirp(start, end, max_eirp,
6686fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt						  results);
66878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
66888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
66898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
66908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	{
66918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
66928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
66938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_reg_rule_sec(tb_rule, results);
66948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
66958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
669668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
669768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	{
669868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
669968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
670068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		nl80211_reg_rule_vht(tb_rule, results);
670168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	}
670268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
67038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
67048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
67058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
670768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidtstatic int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
670868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt					struct phy_info_arg *results)
67098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
67108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
67118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
67138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
67148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
67158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
67178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
67188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
67198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct hostapd_hw_modes *
67228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtwpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
67238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
67242f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	u32 feat;
67258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
67268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
67278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
67288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct phy_info_arg result = {
67298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		.num_modes = num_modes,
67308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		.modes = NULL,
67312f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		.last_mode = -1,
67328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
67338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*num_modes = 0;
67358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*flags = 0;
67368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
67388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
67398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
67408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67412f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	feat = get_nl80211_protocol_features(drv);
67422f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
67432f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
67442f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	else
67452f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
67468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67472f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
6748bd14a57187b024f49f5b9ace55ef457d8d04650aDmitry Shmidt	if (nl80211_set_iface_id(msg, bss) < 0)
6749bd14a57187b024f49f5b9ace55ef457d8d04650aDmitry Shmidt		goto nla_put_failure;
67508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
675268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		nl80211_set_regulatory_flags(drv, &result);
67530ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		return wpa_driver_nl80211_postprocess_modes(result.modes,
67540ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt							    num_modes);
67558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
67561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
67578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
67581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
67598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NULL;
67608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
67618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
67641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					const void *data, size_t len,
67651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					int encrypt, int noack)
67668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
67678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	__u8 rtap_hdr[] = {
67688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		0x00, 0x00, /* radiotap version */
67698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		0x0e, 0x00, /* radiotap length */
67708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
67718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
67728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		0x00,       /* padding */
67738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		0x00, 0x00, /* RX and TX flags to indicate that */
67748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		0x00, 0x00, /* this is the injected frame directly */
67758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
67768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct iovec iov[2] = {
67778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		{
67788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			.iov_base = &rtap_hdr,
67798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			.iov_len = sizeof(rtap_hdr),
67808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		},
67818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		{
67828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			.iov_base = (void *) data,
67838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			.iov_len = len,
67848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
67858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
67868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct msghdr msg = {
67878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		.msg_name = NULL,
67888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		.msg_namelen = 0,
67898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		.msg_iov = iov,
67908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		.msg_iovlen = 2,
67918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		.msg_control = NULL,
67928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		.msg_controllen = 0,
67938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		.msg_flags = 0,
67948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
67958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int res;
67961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u16 txflags = 0;
67978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
67988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (encrypt)
67998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
68008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6801c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt	if (drv->monitor_sock < 0) {
6802c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6803c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt			   "for %s", __func__);
6804c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		return -1;
6805c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt	}
6806c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt
68071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (noack)
68081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
6809d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	WPA_PUT_LE16(&rtap_hdr[12], txflags);
68101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
68118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	res = sendmsg(drv->monitor_sock, &msg, 0);
68128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (res < 0) {
68138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
68148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
68158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
68168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
68178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
68188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
68198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
68201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
68211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					 const void *data, size_t len,
6822c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					 int encrypt, int noack,
6823c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					 unsigned int freq, int no_cck,
6824c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					 int offchanok, unsigned int wait_time)
68251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
68261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
68271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u64 cookie;
6828051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	int res;
68291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
68305605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	if (freq == 0) {
68315605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
68325605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			   bss->freq);
6833c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		freq = bss->freq;
68345605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	}
6835c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
68365605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	if (drv->use_monitor) {
68375605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
68385605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			   freq, bss->freq);
68391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return wpa_driver_nl80211_send_mntr(drv, data, len,
68401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt						    encrypt, noack);
68415605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	}
68421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
68435605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
6844051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
6845051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				     &cookie, no_cck, noack, offchanok);
6846051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	if (res == 0 && !noack) {
6847051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		const struct ieee80211_mgmt *mgmt;
6848051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		u16 fc;
6849051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
6850051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		mgmt = (const struct ieee80211_mgmt *) data;
6851051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		fc = le_to_host16(mgmt->frame_control);
6852051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6853051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
6854051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			wpa_printf(MSG_MSGDUMP,
6855051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				   "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
6856051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				   (long long unsigned int)
6857051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				   drv->send_action_cookie,
6858051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				   (long long unsigned int) cookie);
6859051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			drv->send_action_cookie = cookie;
6860051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		}
6861051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	}
6862051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
6863051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	return res;
68641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
68651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
68661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
68674b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
68684b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					size_t data_len, int noack,
68694b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					unsigned int freq, int no_cck,
68704b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					int offchanok,
68714b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					unsigned int wait_time)
68728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
68738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
68748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct ieee80211_mgmt *mgmt;
68758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int encrypt = 1;
68768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 fc;
68778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
68788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mgmt = (struct ieee80211_mgmt *) data;
68798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	fc = le_to_host16(mgmt->frame_control);
68805605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
68815605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		   noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
68821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
688334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if ((is_sta_interface(drv->nlmode) ||
688434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	     drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
68858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
68868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
68878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
68888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * The use of last_mgmt_freq is a bit of a hack,
68898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * but it works due to the single-threaded nature
68908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * of wpa_supplicant.
68918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
68925605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		if (freq == 0) {
68935605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
68945605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				   drv->last_mgmt_freq);
6895c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt			freq = drv->last_mgmt_freq;
68965605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		}
6897c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		return nl80211_send_frame_cmd(bss, freq, 0,
68981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					      data, data_len, NULL, 1, noack,
68991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					      1);
69008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
69011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
69021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
69035605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		if (freq == 0) {
69045605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
69055605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				   bss->freq);
6906c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt			freq = bss->freq;
69075605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		}
690804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		return nl80211_send_frame_cmd(bss, freq,
690904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt					      (int) freq == bss->freq ? 0 :
691004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt					      wait_time,
6911c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					      data, data_len,
6912c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					      &drv->send_action_cookie,
6913c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					      no_cck, noack, offchanok);
69146e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidt	}
6915b638fe75d3cb9d21c67386173f10afe65053cc4dDmitry Shmidt
69168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
69178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
69188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
69198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Only one of the authentication frame types is encrypted.
69208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * In order for static WEP encryption to work properly (i.e.,
69218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * to not encrypt the frame), we need to tell mac80211 about
69228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * the frames that must not be encrypted.
69238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
69248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
69258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
69268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
69278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			encrypt = 0;
69288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
69291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
69305605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
69311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
6932c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					     noack, freq, no_cck, offchanok,
6933c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					     wait_time);
6934c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt}
6935c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
6936c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
69371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
69381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   int slot, int ht_opmode, int ap_isolate,
69391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   int *basic_rates)
69401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
69411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
69421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
69431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
69441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
69451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
69461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -ENOMEM;
69471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
69481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
69491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
69501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (cts >= 0)
69511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
69521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (preamble >= 0)
69531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
69541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (slot >= 0)
69551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
69561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ht_opmode >= 0)
69571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
69581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ap_isolate >= 0)
69591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
69601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
69611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (basic_rates) {
69621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		u8 rates[NL80211_MAX_SUPP_RATES];
69631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		u8 rates_len = 0;
69641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		int i;
69651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
69661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
69671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		     i++)
69681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			rates[rates_len++] = basic_rates[i] / 5;
69691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
69701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
69711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
69721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
69731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
69741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
69751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return send_and_recv_msgs(drv, msg, NULL, NULL);
69761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt nla_put_failure:
69771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
69781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return -ENOBUFS;
69798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
69808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
69818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
69828bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidtstatic int wpa_driver_nl80211_set_acl(void *priv,
69838bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt				      struct hostapd_acl_params *params)
69848bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt{
69858bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	struct i802_bss *bss = priv;
69868bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
69878bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	struct nl_msg *msg;
69888bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	struct nlattr *acl;
69898bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	unsigned int i;
69908bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	int ret = 0;
69918bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
69928bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	if (!(drv->capa.max_acl_mac_addrs))
69938bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		return -ENOTSUP;
69948bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
69958bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
69968bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		return -ENOTSUP;
69978bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
69988bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	msg = nlmsg_alloc();
69998bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	if (!msg)
70008bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		return -ENOMEM;
70018bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70028bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
70038bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		   params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
70048bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70058bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
70068bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70078bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
70088bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70098bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
70108bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		    NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
70118bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		    NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
70128bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70138bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
70148bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	if (acl == NULL)
70158bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		goto nla_put_failure;
70168bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70178bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	for (i = 0; i < params->num_mac_acl; i++)
70188bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
70198bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70208bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	nla_nest_end(msg, acl);
70218bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70228bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
70238bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	msg = NULL;
70248bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	if (ret) {
70258bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
70268bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt			   ret, strerror(-ret));
70278bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	}
70288bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70298bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidtnla_put_failure:
70308bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	nlmsg_free(msg);
70318bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70328bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	return ret;
70338bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt}
70348bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70358bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
70361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_set_ap(void *priv,
70371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     struct wpa_driver_ap_params *params)
70388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
70398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
70408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
70418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
70428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 cmd = NL80211_CMD_NEW_BEACON;
70438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
70448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int beacon_set;
70458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ifindex = if_nametoindex(bss->ifname);
70461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int num_suites;
7047fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	u32 suites[10], suite;
70481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u32 ver;
70491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
70506e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidt	beacon_set = bss->beacon_set;
70518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
70528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
70538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
70548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
70558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
70568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
70578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   beacon_set);
70588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (beacon_set)
70598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cmd = NL80211_CMD_SET_BEACON;
70608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
70611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, cmd);
70620ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
70630ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		    params->head, params->head_len);
70641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
70650ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
70660ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		    params->tail, params->tail_len);
70671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
70680ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
70698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
70700ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
70711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
70720ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
70731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
70740ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
70750ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			  params->ssid, params->ssid_len);
70761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
70771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		params->ssid);
70780ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	if (params->proberesp && params->proberesp_len) {
70790ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
70800ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			    params->proberesp, params->proberesp_len);
70811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
70821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			params->proberesp);
70830ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	}
70841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	switch (params->hide_ssid) {
70851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NO_SSID_HIDING:
70860ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
70871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
70881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			    NL80211_HIDDEN_SSID_NOT_IN_USE);
70891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
70901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case HIDDEN_SSID_ZERO_LEN:
70910ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
70921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
70931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			    NL80211_HIDDEN_SSID_ZERO_LEN);
70941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
70951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case HIDDEN_SSID_ZERO_CONTENTS:
70960ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
70971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
70981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			    NL80211_HIDDEN_SSID_ZERO_CONTENTS);
70991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
71001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
71010ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
71021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->privacy)
71031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
71040ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
71051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
71061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
71071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/* Leave out the attribute */
71081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	} else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
71091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
71101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			    NL80211_AUTHTYPE_SHARED_KEY);
71111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	else
71121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
71131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			    NL80211_AUTHTYPE_OPEN_SYSTEM);
71141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
71150ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
71161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ver = 0;
71171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->wpa_version & WPA_PROTO_WPA)
71181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		ver |= NL80211_WPA_VERSION_1;
71191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->wpa_version & WPA_PROTO_RSN)
71201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		ver |= NL80211_WPA_VERSION_2;
71211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ver)
71221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
71231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
71240ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
71250ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		   params->key_mgmt_suites);
71261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	num_suites = 0;
71271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
71281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		suites[num_suites++] = WLAN_AKM_SUITE_8021X;
71291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
71301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		suites[num_suites++] = WLAN_AKM_SUITE_PSK;
71311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (num_suites) {
71321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
71331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			num_suites * sizeof(u32), suites);
71341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
71351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
71361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
71371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
71381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
71391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
71400ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
71410ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		   params->pairwise_ciphers);
7142fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7143fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt						 suites, ARRAY_SIZE(suites));
71441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (num_suites) {
71451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
71461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			num_suites * sizeof(u32), suites);
71471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
71481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
71490ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
71500ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		   params->group_cipher);
7151fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7152fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (suite)
7153fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
71541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
71551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->beacon_ies) {
71560ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
71570ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				params->beacon_ies);
71581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
71591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpabuf_head(params->beacon_ies));
71601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
71611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->proberesp_ies) {
71620ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
71630ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				params->proberesp_ies);
71641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
71651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpabuf_len(params->proberesp_ies),
71661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpabuf_head(params->proberesp_ies));
71671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
71681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->assocresp_ies) {
71690ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
71700ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt				params->assocresp_ies);
71711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
71721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpabuf_len(params->assocresp_ies),
71731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpabuf_head(params->assocresp_ies));
71741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
71758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
717604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)  {
71770ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
71780ccb66edb8d2a0a397320ace3ec2a03fb0d00d5fDmitry Shmidt			   params->ap_max_inactivity);
717904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
718004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			    params->ap_max_inactivity);
718104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	}
718204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
71838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
71848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
71858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
71868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   ret, strerror(-ret));
71878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
71888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		bss->beacon_set = 1;
71891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_set_bss(bss, params->cts_protect, params->preamble,
71901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				params->short_slot_time, params->ht_opmode,
71911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				params->isolate, params->basic_rates);
71928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
71938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
71948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
71951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
71968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
71978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
71988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
71998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7200e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtstatic int nl80211_put_freq_params(struct nl_msg *msg,
7201e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				   struct hostapd_freq_params *freq)
72028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
7203a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7204a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	if (freq->vht_enabled) {
7205a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		switch (freq->bandwidth) {
7206a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		case 20:
7207a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7208a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt				    NL80211_CHAN_WIDTH_20);
7209a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			break;
7210a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		case 40:
7211a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7212a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt				    NL80211_CHAN_WIDTH_40);
7213a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			break;
7214a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		case 80:
7215a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			if (freq->center_freq2)
7216a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt				NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7217a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt					    NL80211_CHAN_WIDTH_80P80);
7218a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			else
7219a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt				NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7220a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt					    NL80211_CHAN_WIDTH_80);
7221a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			break;
7222a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		case 160:
7223a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7224a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt				    NL80211_CHAN_WIDTH_160);
7225a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			break;
7226a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		default:
7227e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			return -EINVAL;
7228a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		}
7229a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7230a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		if (freq->center_freq2)
7231a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7232a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt				    freq->center_freq2);
7233a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	} else if (freq->ht_enabled) {
7234a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		switch (freq->sec_channel_offset) {
72358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		case -1:
72368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
72378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    NL80211_CHAN_HT40MINUS);
72388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
72398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		case 1:
72408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
72418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    NL80211_CHAN_HT40PLUS);
72428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
72438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		default:
72448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
72458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    NL80211_CHAN_HT20);
72468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
72478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
72488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
7249e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	return 0;
7250e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
7251e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtnla_put_failure:
7252e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	return -ENOBUFS;
7253e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt}
7254e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
7255e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
7256e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtstatic int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
7257e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt				       struct hostapd_freq_params *freq)
7258e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt{
7259e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
7260e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	struct nl_msg *msg;
7261e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	int ret;
7262e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
7263e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	wpa_printf(MSG_DEBUG,
7264e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		   "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7265e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		   freq->freq, freq->ht_enabled, freq->vht_enabled,
7266e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		   freq->bandwidth, freq->center_freq1, freq->center_freq2);
7267e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	msg = nlmsg_alloc();
7268e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (!msg)
7269e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		return -1;
7270e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
7271e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7272e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
7273e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7274e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (nl80211_put_freq_params(msg, freq) < 0)
7275e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		goto nla_put_failure;
72768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
72778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
72781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
72791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ret == 0) {
7280a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		bss->freq = freq->freq;
72818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
72821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
72838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
7284a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		   "%d (%s)", freq->freq, ret, strerror(-ret));
72858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
72861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
72878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
72888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
72898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
72908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
72911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic u32 sta_flags_nl80211(int flags)
72928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
72931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u32 f = 0;
72948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
72951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (flags & WPA_STA_AUTHORIZED)
72961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
72971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (flags & WPA_STA_WMM)
72981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		f |= BIT(NL80211_STA_FLAG_WME);
72991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (flags & WPA_STA_SHORT_PREAMBLE)
73001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
73011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (flags & WPA_STA_MFP)
73021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		f |= BIT(NL80211_STA_FLAG_MFP);
73031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (flags & WPA_STA_TDLS_PEER)
73041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
73051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
73061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return f;
73071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
73081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
73091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
73101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_sta_add(void *priv,
73111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				      struct hostapd_sta_add_params *params)
73121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
73131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *bss = priv;
73141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
73158da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	struct nl_msg *msg;
73161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl80211_sta_flag_update upd;
73171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ret = -ENOBUFS;
73181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
73191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if ((params->flags & WPA_STA_TDLS_PEER) &&
73201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
73211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -EOPNOTSUPP;
73221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
73231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
73241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
73251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -ENOMEM;
73261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7327f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7328f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		   params->set ? "Set" : "Add", MAC2STR(params->addr));
73291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
73301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    NL80211_CMD_NEW_STATION);
73318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
73328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
73338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
73348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
73358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		params->supp_rates);
7336f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "  * supported rates", params->supp_rates,
7337f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		    params->supp_rates_len);
73381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!params->set) {
733951b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt		if (params->aid) {
734051b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt			wpa_printf(MSG_DEBUG, "  * aid=%u", params->aid);
734151b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt			NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
734251b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt		} else {
734351b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt			/*
734451b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt			 * cfg80211 validates that AID is non-zero, so we have
734551b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt			 * to make this a non-zero value for the TDLS case where
734651b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt			 * a dummy STA entry is used for now.
734751b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt			 */
734851b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt			wpa_printf(MSG_DEBUG, "  * aid=1 (TDLS workaround)");
734951b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt			NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
735051b6ea882f234c14cd1fe1332a3840cf61fafccaDmitry Shmidt		}
7351f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * listen_interval=%u",
7352f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			   params->listen_interval);
73531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
73541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			    params->listen_interval);
73558bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	} else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
73568bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * peer_aid=%u", params->aid);
73578bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
73581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
73598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->ht_capabilities) {
7360f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "  * ht_capabilities",
7361f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			    (u8 *) params->ht_capabilities,
7362f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			    sizeof(*params->ht_capabilities));
73638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
73648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			sizeof(*params->ht_capabilities),
73658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			params->ht_capabilities);
73668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
73678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7368a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	if (params->vht_capabilities) {
7369f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "  * vht_capabilities",
7370f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			    (u8 *) params->vht_capabilities,
7371f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			    sizeof(*params->vht_capabilities));
7372a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7373a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			sizeof(*params->vht_capabilities),
7374a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt			params->vht_capabilities);
7375a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	}
7376a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt
7377bd14a57187b024f49f5b9ace55ef457d8d04650aDmitry Shmidt	if (params->vht_opmode_enabled) {
7378bd14a57187b024f49f5b9ace55ef457d8d04650aDmitry Shmidt		wpa_printf(MSG_DEBUG, "  * opmode=%u", params->vht_opmode);
7379bd14a57187b024f49f5b9ace55ef457d8d04650aDmitry Shmidt		NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7380bd14a57187b024f49f5b9ace55ef457d8d04650aDmitry Shmidt			   params->vht_opmode);
7381bd14a57187b024f49f5b9ace55ef457d8d04650aDmitry Shmidt	}
7382bd14a57187b024f49f5b9ace55ef457d8d04650aDmitry Shmidt
7383f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	wpa_printf(MSG_DEBUG, "  * capability=0x%x", params->capability);
7384f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7385f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
7386f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	if (params->ext_capab) {
7387f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "  * ext_capab",
7388f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			    params->ext_capab, params->ext_capab_len);
7389f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7390f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			params->ext_capab_len, params->ext_capab);
7391f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	}
7392f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
7393344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt	if (params->supp_channels) {
7394344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "  * supported channels",
7395344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt			    params->supp_channels, params->supp_channels_len);
7396344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7397344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt			params->supp_channels_len, params->supp_channels);
7398344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt	}
7399344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt
7400344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt	if (params->supp_oper_classes) {
7401344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "  * supported operating classes",
7402344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt			    params->supp_oper_classes,
7403344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt			    params->supp_oper_classes_len);
7404344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7405344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt			params->supp_oper_classes_len,
7406344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt			params->supp_oper_classes);
7407344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt	}
7408344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt
74091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&upd, 0, sizeof(upd));
74101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	upd.mask = sta_flags_nl80211(params->flags);
74111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	upd.set = upd.mask;
7412f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	wpa_printf(MSG_DEBUG, "  * flags set=0x%x mask=0x%x",
7413f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		   upd.set, upd.mask);
74141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
74151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
74161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->flags & WPA_STA_WMM) {
74178da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
74188da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt
74191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (!wme)
74201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			goto nla_put_failure;
74211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7422f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * qosinfo=0x%x", params->qosinfo);
74238da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
74241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
74258da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
7426f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt				(params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
74271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				WMM_QOSINFO_STA_SP_MASK);
74288da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		nla_nest_end(msg, wme);
74291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
74301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
74318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
74321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
74338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret)
74341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
74351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "result: %d (%s)", params->set ? "SET" : "NEW", ret,
74361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   strerror(-ret));
74378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret == -EEXIST)
74388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = 0;
74398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
74401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
74418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
74428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
74438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74454b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
74468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
74478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
74488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
74498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
74508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
74528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
74538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
74548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
74568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
74588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    if_nametoindex(bss->ifname));
74598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
74608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7462b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7463b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		   " --> %d (%s)",
7464b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		   bss->ifname, MAC2STR(addr), ret, strerror(-ret));
74658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret == -ENOENT)
74668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
74678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
74688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
74691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
74708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
74718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
74728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
74758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 int ifidx)
74768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
74778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
74788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
74808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* stop listening for EAPOL on this interface */
74828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	del_ifidx(drv, ifidx);
74838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
74858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
74868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
74878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
74898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
74908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
74928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
74931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
74948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
74951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
74968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
74978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
74988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
74998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic const char * nl80211_iftype_str(enum nl80211_iftype mode)
75011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
75021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	switch (mode) {
75031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_IFTYPE_ADHOC:
75041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return "ADHOC";
75051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_IFTYPE_STATION:
75061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return "STATION";
75071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_IFTYPE_AP:
75081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return "AP";
7509f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt	case NL80211_IFTYPE_AP_VLAN:
7510f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		return "AP_VLAN";
7511f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt	case NL80211_IFTYPE_WDS:
7512f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		return "WDS";
75131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_IFTYPE_MONITOR:
75141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return "MONITOR";
7515f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt	case NL80211_IFTYPE_MESH_POINT:
7516f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		return "MESH_POINT";
75171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_IFTYPE_P2P_CLIENT:
75181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return "P2P_CLIENT";
75191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case NL80211_IFTYPE_P2P_GO:
75201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return "P2P_GO";
752134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	case NL80211_IFTYPE_P2P_DEVICE:
752234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return "P2P_DEVICE";
75231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	default:
75241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return "unknown";
75251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
75261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
75271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
75281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
75298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
75308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     const char *ifname,
75318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     enum nl80211_iftype iftype,
753234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				     const u8 *addr, int wds,
753334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				     int (*handler)(struct nl_msg *, void *),
753434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				     void *arg)
75358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
75368da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	struct nl_msg *msg;
75378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ifidx;
75388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -ENOBUFS;
75398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
75411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   iftype, nl80211_iftype_str(iftype));
75421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
75438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
75448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
75458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
75468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
7548cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
754934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
75508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
75518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
75528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (iftype == NL80211_IFTYPE_MONITOR) {
75548da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		struct nlattr *flags;
75558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75568da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
75578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!flags)
75588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			goto nla_put_failure;
75598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75608da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
75618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75628da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		nla_nest_end(msg, flags);
75638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else if (wds) {
75648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
75658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
75668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
756734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, handler, arg);
75681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
75698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
75708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
75711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nlmsg_free(msg);
75728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
75738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   ifname, ret, strerror(-ret));
75748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return ret;
75758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
75768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
757734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (iftype == NL80211_IFTYPE_P2P_DEVICE)
757834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return 0;
757934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
75808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ifidx = if_nametoindex(ifname);
75818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
75828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   ifname, ifidx);
75838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ifidx <= 0)
75858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
75868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* start listening for EAPOL on this interface */
75888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	add_ifidx(drv, ifidx);
75898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (addr && iftype != NL80211_IFTYPE_MONITOR &&
75911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
75928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_remove_iface(drv, ifidx);
75938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
75948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
75958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ifidx;
75978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
75988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
75998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
76018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				const char *ifname, enum nl80211_iftype iftype,
760234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				const u8 *addr, int wds,
760334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				int (*handler)(struct nl_msg *, void *),
7604cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				void *arg, int use_existing)
76058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
76068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
76078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
760834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
760934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt					arg);
76108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* if error occurred and interface exists already */
76128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret == -ENFILE && if_nametoindex(ifname)) {
7613cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		if (use_existing) {
7614cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7615cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				   ifname);
7616cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			return -ENFILE;
7617cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		}
76188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
76198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Try to remove the interface that was already there. */
76218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_remove_iface(drv, if_nametoindex(ifname));
76228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Try to create the interface again */
76248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
762534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt						wds, handler, arg);
76268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
76278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
762834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (ret >= 0 && is_p2p_net_interface(iftype))
76298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_disable_11b_rates(drv, ret, 1);
76308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
76328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
76338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
76368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
76378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct ieee80211_hdr *hdr;
76388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 fc;
76398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
76408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr = (struct ieee80211_hdr *) buf;
76428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	fc = le_to_host16(hdr->frame_control);
76438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
76458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.type = WLAN_FC_GET_TYPE(fc);
76468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
76478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.dst = hdr->addr1;
76488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.data = buf;
76498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.data_len = len;
76508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	event.tx_status.ack = ok;
76518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
76528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
76538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
76568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     u8 *buf, size_t len)
76578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
76581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct ieee80211_hdr *hdr = (void *)buf;
76591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	u16 fc;
76608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
76611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
76621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (len < sizeof(*hdr))
76631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
76641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
76651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	fc = le_to_host16(hdr->frame_control);
76661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
76678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&event, 0, sizeof(event));
76681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
76691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	event.rx_from_unknown.addr = hdr->addr2;
76701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
76711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		(WLAN_FC_FROMDS | WLAN_FC_TODS);
76728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
76738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
76748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void handle_frame(struct wpa_driver_nl80211_data *drv,
76778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 u8 *buf, size_t len, int datarate, int ssi_signal)
76788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
76798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct ieee80211_hdr *hdr;
76808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 fc;
76818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union wpa_event_data event;
76828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr = (struct ieee80211_hdr *) buf;
76848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	fc = le_to_host16(hdr->frame_control);
76858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
76868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (WLAN_FC_GET_TYPE(fc)) {
76878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WLAN_FC_TYPE_MGMT:
76888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memset(&event, 0, sizeof(event));
76898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.rx_mgmt.frame = buf;
76908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.rx_mgmt.frame_len = len;
76918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.rx_mgmt.datarate = datarate;
76928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		event.rx_mgmt.ssi_signal = ssi_signal;
76938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
76948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
76958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WLAN_FC_TYPE_CTRL:
76968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* can only get here with PS-Poll frames */
76978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "CTRL");
76988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		from_unknown_sta(drv, buf, len);
76998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
77008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WLAN_FC_TYPE_DATA:
77018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		from_unknown_sta(drv, buf, len);
77028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
77038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
77048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
77058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
77068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
77078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
77088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
77098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = eloop_ctx;
77108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int len;
77118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	unsigned char buf[3000];
77128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct ieee80211_radiotap_iterator iter;
77138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
77148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int datarate = 0, ssi_signal = 0;
77158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int injected = 0, failed = 0, rxflags = 0;
77168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
77178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len = recv(sock, buf, sizeof(buf), 0);
77188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (len < 0) {
771968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
772068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   strerror(errno));
77218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
77228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
77238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
77248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
772568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
77268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
77278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
77288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
77298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (1) {
77308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = ieee80211_radiotap_iterator_next(&iter);
77318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret == -ENOENT)
77328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
77338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret) {
773468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
773568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				   ret);
77368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return;
77378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
77388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		switch (iter.this_arg_index) {
77398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		case IEEE80211_RADIOTAP_FLAGS:
77408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
77418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				len -= 4;
77428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
77438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		case IEEE80211_RADIOTAP_RX_FLAGS:
77448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			rxflags = 1;
77458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
77468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		case IEEE80211_RADIOTAP_TX_FLAGS:
77478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			injected = 1;
77488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
77498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					IEEE80211_RADIOTAP_F_TX_FAIL;
77508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
77518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		case IEEE80211_RADIOTAP_DATA_RETRIES:
77528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
77538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		case IEEE80211_RADIOTAP_CHANNEL:
77548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			/* TODO: convert from freq/flags to channel number */
77558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
77568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		case IEEE80211_RADIOTAP_RATE:
77578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			datarate = *iter.this_arg * 5;
77588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
775904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
776004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			ssi_signal = (s8) *iter.this_arg;
77618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
77628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
77638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
77648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
77658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (rxflags && injected)
77668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
77678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
77688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!injected)
77698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		handle_frame(drv, buf + iter.max_length,
77708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     len - iter.max_length, datarate, ssi_signal);
77718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
77728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		handle_tx_callback(drv->ctx, buf + iter.max_length,
77738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   len - iter.max_length, !failed);
77748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
77758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
77768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
77778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/*
77788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * we post-process the filter code later and rewrite
77798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * this to the offset to the last instruction
77808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
77818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define PASS	0xFF
77828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define FAIL	0xFE
77838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
77848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct sock_filter msock_filter_insns[] = {
77858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
77868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * do a little-endian load of the radiotap length field
77878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
77888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* load lower byte into A */
77898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
77908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* put it into X (== index register) */
77918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_MISC| BPF_TAX, 0),
77928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* load upper byte into A */
77938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
77948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* left-shift it by 8 */
77958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
77968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* or with X */
77978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
77988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* put result into X */
77998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_MISC| BPF_TAX, 0),
78008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
78028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Allow management frames through, this also gives us those
78038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * management frames that we sent ourselves with status
78048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
78058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* load the lower byte of the IEEE 802.11 frame control field */
78068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
78078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* mask off frame type and version */
78088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
78098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* accept frame if it's both 0, fall through otherwise */
78108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
78118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
78138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * TODO: add a bit to radiotap RX flags that indicates
78148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * that the sending station is not associated, then
78158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * add a filter here that filters on our DA and that flag
78168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * to allow us to deauth frames to that bad station.
78178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 *
78188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * For now allow all To DS data frames through.
78198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
78208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* load the IEEE 802.11 frame control field */
78218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_LD  | BPF_H | BPF_IND, 0),
78228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* mask off frame type, version and DS status */
78238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
78248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* accept frame if version 0, type 2 and To DS, fall through otherwise
78258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
78268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
78278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#if 0
78298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
78308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * drop non-data frames
78318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
78328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* load the lower byte of the frame control field */
78338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
78348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* mask off QoS bit */
78358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
78368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* drop non-data frames */
78378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
78388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif
78398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* load the upper byte of the frame control field */
78408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 1),
78418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* mask off toDS/fromDS */
78428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
78438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* accept WDS frames */
78448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, PASS, 0),
78458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
78478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * add header length to index
78488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
78498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* load the lower byte of the frame control field */
78508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
78518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* mask off QoS bit */
78528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
78538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* right shift it by 6 to give 0 or 2 */
78548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
78558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* add data frame header length */
78568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
78578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* add index, was start of 802.11 header */
78588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
78598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* move to index, now start of LL header */
78608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_MISC | BPF_TAX, 0),
78618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
78638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Accept empty data frames, we use those for
78648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * polling activity.
78658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
78668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
78678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
78688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
78708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Accept EAPOL frames
78718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
78728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
78738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
78748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
78758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
78768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* keep these last two statements or change the code below */
78788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* return 0 == "DROP" */
78798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_RET | BPF_K, 0),
78808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* return ~0 == "keep all" */
78818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	BPF_STMT(BPF_RET | BPF_K, ~0),
78828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
78838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct sock_fprog msock_filter = {
788568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	.len = ARRAY_SIZE(msock_filter_insns),
78868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.filter = msock_filter_insns,
78878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
78888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int add_monitor_filter(int s)
78918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
78928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int idx;
78938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* rewrite all PASS/FAIL jump offsets */
78958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (idx = 0; idx < msock_filter.len; idx++) {
78968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct sock_filter *insn = &msock_filter_insns[idx];
78978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
78988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (BPF_CLASS(insn->code) == BPF_JMP) {
78998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (insn->code == (BPF_JMP|BPF_JA)) {
79008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				if (insn->k == PASS)
79018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					insn->k = msock_filter.len - idx - 2;
79028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				else if (insn->k == FAIL)
79038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					insn->k = msock_filter.len - idx - 3;
79048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
79058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
79068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (insn->jt == PASS)
79078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				insn->jt = msock_filter.len - idx - 2;
79088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			else if (insn->jt == FAIL)
79098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				insn->jt = msock_filter.len - idx - 3;
79108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
79118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (insn->jf == PASS)
79128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				insn->jf = msock_filter.len - idx - 2;
79138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			else if (insn->jf == FAIL)
79148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				insn->jf = msock_filter.len - idx - 3;
79158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
79168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
79178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
79188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
79198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       &msock_filter, sizeof(msock_filter))) {
792068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
792168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   strerror(errno));
79228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
79238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
79248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
79258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
79268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
79278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
79288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
79298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_remove_monitor_interface(
79308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv)
79318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
7932cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (drv->monitor_refcount > 0)
7933cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		drv->monitor_refcount--;
7934cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
7935cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		   drv->monitor_refcount);
79361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->monitor_refcount > 0)
79371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
79381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
79398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->monitor_ifidx >= 0) {
79408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_remove_iface(drv, drv->monitor_ifidx);
79418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		drv->monitor_ifidx = -1;
79428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
79438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->monitor_sock >= 0) {
79448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_unregister_read_sock(drv->monitor_sock);
79458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		close(drv->monitor_sock);
79468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		drv->monitor_sock = -1;
79478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
79488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
79498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
79508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
79518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int
79528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
79538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
79548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char buf[IFNAMSIZ];
79558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct sockaddr_ll ll;
79568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int optval;
79578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	socklen_t optlen;
79581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
79591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->monitor_ifidx >= 0) {
79601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->monitor_refcount++;
7961cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
7962cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			   drv->monitor_refcount);
79631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return 0;
79641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
79651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
7966cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
79671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/*
79681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * P2P interface name is of the format p2p-%s-%d. For monitor
79691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * interface name corresponding to P2P GO, replace "p2p-" with
79701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * "mon-" to retain the same interface name length and to
79711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * indicate that it is a monitor interface.
79721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 */
7973cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
79741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	} else {
79751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/* Non-P2P interface with AP functionality. */
7976cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
79771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
79781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
79798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	buf[IFNAMSIZ - 1] = '\0';
79808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
79818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->monitor_ifidx =
79828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
7983cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				     0, NULL, NULL, 0);
79848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7985c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt	if (drv->monitor_ifidx == -EOPNOTSUPP) {
79861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/*
79871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * This is backward compatibility for a few versions of
79881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * the kernel only that didn't advertise the right
79891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * attributes for the only driver that then supported
79901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * AP mode w/o monitor -- ath6kl.
79911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 */
7992c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
7993c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt			   "monitor interface type - try to run without it");
79941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->device_ap_sme = 1;
7995c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt	}
7996c55524ad84d13014e8019491c2b17e5dcf13545aDmitry Shmidt
79978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->monitor_ifidx < 0)
79988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
79998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
80001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
80018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
80028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
80038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memset(&ll, 0, sizeof(ll));
80048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ll.sll_family = AF_PACKET;
80058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ll.sll_ifindex = drv->monitor_ifidx;
80068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
80078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->monitor_sock < 0) {
800868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
800968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   strerror(errno));
80108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
80118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
80128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
80138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (add_monitor_filter(drv->monitor_sock)) {
80148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
80158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "interface; do filtering in user space");
80168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* This works, but will cost in performance. */
80178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
80188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
80198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
802068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
802168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   strerror(errno));
80228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
80238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
80248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
80258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	optlen = sizeof(optval);
80268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	optval = 20;
80278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (setsockopt
80288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
802968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
803068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   strerror(errno));
80318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
80328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
80338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
80348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
80358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     drv, NULL)) {
803668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
80378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto error;
80388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
80398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8040cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	drv->monitor_refcount++;
80418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
80428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt error:
80438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nl80211_remove_monitor_interface(drv);
80448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
80458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
80468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
80478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
80481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_setup_ap(struct i802_bss *bss)
80491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
80501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
80511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8052cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8053cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		   bss->ifname, drv->device_ap_sme, drv->use_monitor);
80541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
80551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/*
80561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * Disable Probe Request reporting unless we need it in this way for
80571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * devices that include the AP SME, in the other case (unless using
80581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * monitor iface) we'll get it through the nl_mgmt socket instead.
80591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 */
80601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!drv->device_ap_sme)
80611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_driver_nl80211_probe_req_report(bss, 0);
80621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
80631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!drv->device_ap_sme && !drv->use_monitor)
80641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (nl80211_mgmt_subscribe_ap(bss))
80651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			return -1;
80661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
80671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->device_ap_sme && !drv->use_monitor)
80681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
80691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			return -1;
80701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
80711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!drv->device_ap_sme && drv->use_monitor &&
80721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    nl80211_create_monitor_interface(drv) &&
80731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    !drv->device_ap_sme)
807404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		return -1;
807504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
80761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->device_ap_sme &&
80771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
80781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
80791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "Probe Request frame reporting in AP mode");
80801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/* Try to survive without this */
80811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
80821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
80831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return 0;
80841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
80851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
80861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
80871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_teardown_ap(struct i802_bss *bss)
80881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
80891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
80901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8091cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8092cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		   bss->ifname, drv->device_ap_sme, drv->use_monitor);
80931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->device_ap_sme) {
80941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_driver_nl80211_probe_req_report(bss, 0);
80951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (!drv->use_monitor)
80961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
80971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	} else if (drv->use_monitor)
80981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_remove_monitor_interface(drv);
80991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	else
81001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_mgmt_unsubscribe(bss, "AP teardown");
81011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
81021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bss->beacon_set = 0;
81031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
81041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
81051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
81061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_send_eapol_data(struct i802_bss *bss,
81071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   const u8 *addr, const u8 *data,
81081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   size_t data_len)
81091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
81101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct sockaddr_ll ll;
81111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ret;
81121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
81131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (bss->drv->eapol_tx_sock < 0) {
81141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
81151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
81161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
81171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
81181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&ll, 0, sizeof(ll));
81191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ll.sll_family = AF_PACKET;
81201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ll.sll_ifindex = bss->ifindex;
81211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ll.sll_protocol = htons(ETH_P_PAE);
81221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ll.sll_halen = ETH_ALEN;
81231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memcpy(ll.sll_addr, addr, ETH_ALEN);
81241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
81251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		     (struct sockaddr *) &ll, sizeof(ll));
81261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ret < 0)
81271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
81281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   strerror(errno));
81291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
81301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return ret;
81311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
81321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
81331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
81348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
81358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
81368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_hapd_send_eapol(
81378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	void *priv, const u8 *addr, const u8 *data,
81388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
81398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
81408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
81418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
81428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct ieee80211_hdr *hdr;
81438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t len;
81448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *pos;
81458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int res;
81468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int qos = flags & WPA_STA_WMM;
8147641185ebd73d9607756c87b22f6c520ab1846325Dmitry Shmidt
81481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->device_ap_sme || !drv->use_monitor)
81491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return nl80211_send_eapol_data(bss, addr, data, data_len);
81501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
81518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
81528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data_len;
81538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr = os_zalloc(len);
81548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (hdr == NULL) {
815568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
815668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   (unsigned long) len);
81578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
81588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
81598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
81608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr->frame_control =
81618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
81628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
81638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (encrypt)
81648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
81658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (qos) {
81668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		hdr->frame_control |=
81678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
81688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
81698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
81708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
81718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
81728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
81738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = (u8 *) (hdr + 1);
81748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
81758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (qos) {
8176aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt		/* Set highest priority in QoS header */
8177aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt		pos[0] = 7;
81788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos[1] = 0;
81798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += 2;
81808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
81818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
81828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
81838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += sizeof(rfc1042_header);
81848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPA_PUT_BE16(pos, ETH_P_PAE);
81858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += 2;
81868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(pos, data, data_len);
81878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8188c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8189c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					    0, 0, 0, 0);
81908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (res < 0) {
81918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
81928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "failed: %d (%s)",
81938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   (unsigned long) len, errno, strerror(errno));
81948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
81958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(hdr);
81968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
81978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return res;
81988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
81998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
82028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					    int total_flags,
82038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					    int flags_or, int flags_and)
82048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
82058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
82068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
82078da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	struct nl_msg *msg;
82088da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	struct nlattr *flags;
82098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl80211_sta_flag_update upd;
82108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
82128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
82138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
82148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
82168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
82188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    if_nametoindex(bss->ifname));
82198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
82208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
82228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
82238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * can be removed eventually.
82248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
82258da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
82268da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	if (!flags)
82278da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		goto nla_put_failure;
82288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (total_flags & WPA_STA_AUTHORIZED)
82298da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
82308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (total_flags & WPA_STA_WMM)
82328da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
82338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (total_flags & WPA_STA_SHORT_PREAMBLE)
82358da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
82368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (total_flags & WPA_STA_MFP)
82388da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
82398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (total_flags & WPA_STA_TDLS_PEER)
82418da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
82421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
82438da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	nla_nest_end(msg, flags);
82448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&upd, 0, sizeof(upd));
82468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
82478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	upd.set = sta_flags_nl80211(flags_or);
82488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
82498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return send_and_recv_msgs(drv, msg, NULL, NULL);
82518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
82521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
82538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
82548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
82558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
82588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 struct wpa_driver_associate_params *params)
82598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
8260a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	enum nl80211_iftype nlmode, old_mode;
8261a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	struct hostapd_freq_params freq = {
8262a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		.freq = params->freq,
8263a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	};
82641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
82651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->p2p) {
82668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
82678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "group (GO)");
82681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nlmode = NL80211_IFTYPE_P2P_GO;
82691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	} else
82701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nlmode = NL80211_IFTYPE_AP;
82711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
8272a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	old_mode = drv->nlmode;
8273cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
8274a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		nl80211_remove_monitor_interface(drv);
8275a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		return -1;
8276a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	}
8277a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt
8278cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
8279a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		if (old_mode != nlmode)
8280cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
82818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_remove_monitor_interface(drv);
82828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
82838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
82848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
82868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
82878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
82908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
82918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
82928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -1;
82938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
82958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
82968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
82978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
82981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
82998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
83008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
83018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
83028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
83038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
83048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(%s)", ret, strerror(-ret));
83058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
83068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
83078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = 0;
83098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
83108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
8312cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (wpa_driver_nl80211_set_mode(drv->first_bss,
83135605286c30e1701491bd3af974ae423727750eddDmitry Shmidt					NL80211_IFTYPE_STATION)) {
83145605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
83155605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			   "station mode");
83165605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	}
83175605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
83188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
83198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
83208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
83218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
83248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   struct wpa_driver_associate_params *params)
83258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
83268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
83278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -1;
83288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int count = 0;
83298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
83318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8332cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (wpa_driver_nl80211_set_mode(drv->first_bss,
83331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					NL80211_IFTYPE_ADHOC)) {
83348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
83358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "IBSS mode");
83368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
83378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
83388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtretry:
83408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
83418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
83428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
83438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
83458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
83468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
83488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
83498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
83518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  params->ssid, params->ssid_len);
83528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
83538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		params->ssid);
83548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(drv->ssid, params->ssid, params->ssid_len);
83558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->ssid_len = params->ssid_len;
83568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
83588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
83598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = nl80211_set_conn_keys(params, msg);
83618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret)
83628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
83638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8364c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	if (params->bssid && params->fixed_bssid) {
8365c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * BSSID=" MACSTR,
8366c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt			   MAC2STR(params->bssid));
8367c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8368c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	}
8369c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
8370fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8371fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	    params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8372fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	    params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8373fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	    params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
8374c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * control port");
8375c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8376c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	}
8377c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
83788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->wpa_ie) {
83798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump(MSG_DEBUG,
83808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    "  * Extra IEs for Beacon/Probe Response frames",
83818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    params->wpa_ie, params->wpa_ie_len);
83828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
83838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			params->wpa_ie);
83848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
83858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
83868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
83878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
83888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
83898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
83908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   ret, strerror(-ret));
83918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		count++;
83928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret == -EALREADY && count == 1) {
83938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
83948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "forced leave");
83958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nl80211_leave_ibss(drv);
83968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nlmsg_free(msg);
83978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			goto retry;
83988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
83998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
84008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
84018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
84028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = 0;
84038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
84048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
84058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
84068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
84078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
84088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
84098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
84108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8411fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8412fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				  struct wpa_driver_associate_params *params,
8413fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt				  struct nl_msg *msg)
84148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
84158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8416fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
84178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->bssid) {
84188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
84198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MAC2STR(params->bssid));
84208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
84218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8422fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
842396be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt	if (params->bssid_hint) {
842496be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt		wpa_printf(MSG_DEBUG, "  * bssid_hint=" MACSTR,
842596be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt			   MAC2STR(params->bssid_hint));
842696be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
842796be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt			params->bssid_hint);
842896be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt	}
842996be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt
84308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->freq) {
84318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
84328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8433b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		drv->assoc_freq = params->freq;
8434b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	} else
8435b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		drv->assoc_freq = 0;
8436fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
843796be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt	if (params->freq_hint) {
843896be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt		wpa_printf(MSG_DEBUG, "  * freq_hint=%d", params->freq_hint);
843996be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
844096be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt			    params->freq_hint);
844196be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt	}
844296be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt
844304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (params->bg_scan_period >= 0) {
844404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
844504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			   params->bg_scan_period);
844604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
844704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			    params->bg_scan_period);
844804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	}
8449fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
84508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->ssid) {
84518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
84528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  params->ssid, params->ssid_len);
84538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
84548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			params->ssid);
84558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (params->ssid_len > sizeof(drv->ssid))
84568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			goto nla_put_failure;
84578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(drv->ssid, params->ssid, params->ssid_len);
84588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		drv->ssid_len = params->ssid_len;
84598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8460fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
84618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
84628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->wpa_ie)
84638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
84648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			params->wpa_ie);
84658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
84661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (params->wpa_proto) {
84671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		enum nl80211_wpa_versions ver = 0;
84688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
84691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (params->wpa_proto & WPA_PROTO_WPA)
84701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			ver |= NL80211_WPA_VERSION_1;
84711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (params->wpa_proto & WPA_PROTO_RSN)
84721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			ver |= NL80211_WPA_VERSION_2;
84738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
84741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "  * WPA Versions 0x%x", ver);
84758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
84768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
84778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8478fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (params->pairwise_suite != WPA_CIPHER_NONE) {
8479fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8480fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * pairwise=0x%x", cipher);
84818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
84828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
84838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8484f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt	if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8485f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt	    !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8486f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		/*
8487f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		 * This is likely to work even though many drivers do not
8488f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		 * advertise support for operations without GTK.
8489f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		 */
8490f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8491f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt	} else if (params->group_suite != WPA_CIPHER_NONE) {
8492fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8493fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * group=0x%x", cipher);
84948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
84958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
84968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8497fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8498fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	    params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8499fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	    params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8500fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	    params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
8501fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	    params->key_mgmt_suite == WPA_KEY_MGMT_CCKM) {
85028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		int mgmt = WLAN_AKM_SUITE_PSK;
85038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
85048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		switch (params->key_mgmt_suite) {
8505fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WPA_KEY_MGMT_CCKM:
8506d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			mgmt = WLAN_AKM_SUITE_CCKM;
8507d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			break;
8508fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WPA_KEY_MGMT_IEEE8021X:
85098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			mgmt = WLAN_AKM_SUITE_8021X;
85108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
8511fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WPA_KEY_MGMT_FT_IEEE8021X:
8512700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			mgmt = WLAN_AKM_SUITE_FT_8021X;
8513700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			break;
8514fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WPA_KEY_MGMT_FT_PSK:
8515700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			mgmt = WLAN_AKM_SUITE_FT_PSK;
8516700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			break;
8517fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		case WPA_KEY_MGMT_PSK:
85188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		default:
85198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			mgmt = WLAN_AKM_SUITE_PSK;
85208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
85218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
85228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
85238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
85248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8525fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8526fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8527a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8528a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8529a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt
8530c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	if (params->disable_ht)
8531c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8532c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
8533c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	if (params->htcaps && params->htcaps_mask) {
8534c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		int sz = sizeof(struct ieee80211_ht_capabilities);
8535c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
8536c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8537c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt			params->htcaps_mask);
8538c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	}
8539c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
85402f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt#ifdef CONFIG_VHT_OVERRIDES
85412f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (params->disable_vht) {
85422f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		wpa_printf(MSG_DEBUG, "  * VHT disabled");
85432f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
85442f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
85452f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
85462f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	if (params->vhtcaps && params->vhtcaps_mask) {
85472f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		int sz = sizeof(struct ieee80211_vht_capabilities);
85482f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
85492f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
85502f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt			params->vhtcaps_mask);
85512f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt	}
85522f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt#endif /* CONFIG_VHT_OVERRIDES */
85532f023193a0fd630eb82ce6381b80911ad5a3462fDmitry Shmidt
8554fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (params->p2p)
8555fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * P2P group");
8556fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8557fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	return 0;
8558fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtnla_put_failure:
8559fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	return -1;
8560fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt}
8561fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8562fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8563fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic int wpa_driver_nl80211_try_connect(
8564fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct wpa_driver_nl80211_data *drv,
8565fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct wpa_driver_associate_params *params)
8566fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt{
8567fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct nl_msg *msg;
8568fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	enum nl80211_auth_type type;
8569fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int ret;
8570fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int algs;
8571fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8572fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	msg = nlmsg_alloc();
8573fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (!msg)
8574fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return -1;
8575fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8576fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
8577fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
8578fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8579fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	ret = nl80211_connect_common(drv, params, msg);
8580fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (ret)
8581fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		goto nla_put_failure;
8582fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8583fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	algs = 0;
8584fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8585fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		algs++;
8586fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8587fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		algs++;
8588fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8589fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		algs++;
8590fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (algs > 1) {
8591fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * Leave out Auth Type for automatic "
8592fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			   "selection");
8593fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		goto skip_auth_type;
8594fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
8595fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8596fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8597fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		type = NL80211_AUTHTYPE_OPEN_SYSTEM;
8598fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8599fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		type = NL80211_AUTHTYPE_SHARED_KEY;
8600fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8601fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		type = NL80211_AUTHTYPE_NETWORK_EAP;
8602fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	else if (params->auth_alg & WPA_AUTH_ALG_FT)
8603fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		type = NL80211_AUTHTYPE_FT;
8604fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	else
8605fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		goto nla_put_failure;
8606fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8607fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
8608fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8609fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8610fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtskip_auth_type:
86118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = nl80211_set_conn_keys(params, msg);
86128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret)
86138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
86148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
86168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
86178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
86188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
86198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(%s)", ret, strerror(-ret));
86208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
86218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
86228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = 0;
86238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
86248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
86268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
86278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
86288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
86308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8632d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidtstatic int wpa_driver_nl80211_connect(
8633d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	struct wpa_driver_nl80211_data *drv,
8634d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	struct wpa_driver_associate_params *params)
8635d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt{
8636d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	int ret = wpa_driver_nl80211_try_connect(drv, params);
8637d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (ret == -EALREADY) {
8638d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		/*
8639d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		 * cfg80211 does not currently accept new connections if
8640d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		 * we are already connected. As a workaround, force
8641d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		 * disconnection and try again.
8642d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		 */
8643d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8644d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			   "disconnecting before reassociation "
8645d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			   "attempt");
8646d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		if (wpa_driver_nl80211_disconnect(
8647d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			    drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8648d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			return -1;
8649d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		ret = wpa_driver_nl80211_try_connect(drv, params);
8650d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	}
8651d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	return ret;
8652d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt}
8653d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
8654d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
86558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_associate(
86568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	void *priv, struct wpa_driver_associate_params *params)
86578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
86588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
86598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
8660fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int ret;
86618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
86628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->mode == IEEE80211_MODE_AP)
86648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return wpa_driver_nl80211_ap(drv, params);
86658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->mode == IEEE80211_MODE_IBSS)
86678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return wpa_driver_nl80211_ibss(drv, params);
86688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
86701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		enum nl80211_iftype nlmode = params->p2p ?
86711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
86721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
86731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
86748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
86758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return wpa_driver_nl80211_connect(drv, params);
86768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
86778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86788bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	nl80211_mark_disconnected(drv);
86798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
86818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
86828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
86838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
86858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   drv->ifindex);
86861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
86878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8688fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	ret = nl80211_connect_common(drv, params, msg);
8689fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (ret)
8690fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		goto nla_put_failure;
86918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->prev_bssid) {
86938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "  * prev_bssid=" MACSTR,
86948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MAC2STR(params->prev_bssid));
86958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
86968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			params->prev_bssid);
86978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
86988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
86998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
87008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
87018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
87021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_dbg(drv->ctx, MSG_DEBUG,
87031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			"nl80211: MLME command failed (assoc): ret=%d (%s)",
87041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			ret, strerror(-ret));
87058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		nl80211_dump_scan(drv);
87068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
87078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
87088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = 0;
87098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Association request send "
87108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "successfully");
87118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
87138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
87148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
87158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
87168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
87191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			    int ifindex, enum nl80211_iftype mode)
87208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
87218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
87228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -ENOBUFS;
87238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
87251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   ifindex, mode, nl80211_iftype_str(mode));
87261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
87278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
87288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
87298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
87308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
8732cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
873334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
87348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
87358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
87371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
87388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!ret)
87398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
87408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
87411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
87428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
87438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   " %d (%s)", ifindex, mode, ret, strerror(-ret));
87448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
87458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
87468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
87491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				       enum nl80211_iftype nlmode)
87508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
87518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
87528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -1;
87538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int i;
87541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int was_ap = is_ap_interface(drv->nlmode);
87551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int res;
87568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	res = nl80211_set_mode(drv, drv->ifindex, nlmode);
875834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (res && nlmode == nl80211_get_ifmode(bss))
875934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		res = 0;
876034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
87611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (res == 0) {
87628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		drv->nlmode = nlmode;
87638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = 0;
87648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto done;
87658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
87668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (res == -ENODEV)
87681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
87691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
87708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (nlmode == drv->nlmode) {
87718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
87728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "requested mode - ignore error");
87738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = 0;
87748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto done; /* Already in the requested mode */
87758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
87768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
87778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* mac80211 doesn't allow mode changes while the device is up, so
87788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * take the device down, try to set the mode again, and bring the
87798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * device back up.
87808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
87818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
87828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "interface down");
87838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < 10; i++) {
878434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		res = i802_set_iface_flags(bss, 0);
87851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (res == -EACCES || res == -ENODEV)
87861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			break;
87871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (res == 0) {
87888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			/* Try to set the mode again while the interface is
87898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 * down */
87908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
87911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (ret == -EACCES)
87921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				break;
879334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			res = i802_set_iface_flags(bss, 1);
87941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (res && !ret)
87958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				ret = -1;
87961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			else if (ret != -EBUSY)
87978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				break;
87988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		} else
87998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
88008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "interface down");
88018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_sleep(0, 100000);
88028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
88038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
88048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!ret) {
88058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
88068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "interface is down");
88078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		drv->nlmode = nlmode;
88081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->ignore_if_down_event = 1;
88098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
88108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
88118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtdone:
88121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ret) {
88131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
88141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "from %d failed", nlmode, drv->nlmode);
88151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return ret;
88161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
88171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
881834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (is_p2p_net_interface(nlmode))
881961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		nl80211_disable_11b_rates(drv, drv->ifindex, 1);
882061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	else if (drv->disabled_11b_rates)
882161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		nl80211_disable_11b_rates(drv, drv->ifindex, 0);
882261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
88231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (is_ap_interface(nlmode)) {
88241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_mgmt_unsubscribe(bss, "start AP");
88258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Setup additional AP mode functionality if needed */
88261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (nl80211_setup_ap(bss))
88278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
88281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	} else if (was_ap) {
88298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Remove additional AP mode functionality */
88301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_teardown_ap(bss);
88311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	} else {
88321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_mgmt_unsubscribe(bss, "mode change");
88338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
88348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
883504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (!bss->in_deinit && !is_ap_interface(nlmode) &&
88361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    nl80211_mgmt_subscribe_non_ap(bss) < 0)
88371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
88381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "frame processing - ignore for now");
88398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
88401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return 0;
88418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
88428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
88438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
88448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_get_capa(void *priv,
88458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       struct wpa_driver_capa *capa)
88468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
88478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
88488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
88498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!drv->has_capability)
88508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
88518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(capa, &drv->capa, sizeof(*capa));
8852444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	if (drv->extended_capa && drv->extended_capa_mask) {
8853444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt		capa->extended_capa = drv->extended_capa;
8854444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt		capa->extended_capa_mask = drv->extended_capa_mask;
8855444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt		capa->extended_capa_len = drv->extended_capa_len;
8856444d567b27731d8572ef37697dd12fd1c37c2f24Dmitry Shmidt	}
885734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
885834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
885934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	    !drv->allow_p2p_device) {
886034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
886134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
886234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	}
886334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
88648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
88658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
88668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
88678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
88688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_set_operstate(void *priv, int state)
88698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
88708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
88718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
88728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8873fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
8874fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		   bss->ifname, drv->operstate, state,
8875fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		   state ? "UP" : "DORMANT");
88768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->operstate = state;
88771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
88788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      state ? IF_OPER_UP : IF_OPER_DORMANT);
88798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
88808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
88818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
88828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
88838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
88848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
88858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
88868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
88878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl80211_sta_flag_update upd;
8888fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int ret = -ENOBUFS;
8889fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
8890fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
8891fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
8892fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return 0;
8893fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
88948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
88958bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
88968bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt		   MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
88978bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt
88988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
88998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
89008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
89018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
89038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
89058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    if_nametoindex(bss->ifname));
89068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
89078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&upd, 0, sizeof(upd));
89098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
89108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (authorized)
89118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
89128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
89138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8914fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8915fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	msg = NULL;
8916fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (!ret)
8917fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return 0;
89188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
89191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
8920fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
8921fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		   ret, strerror(-ret));
8922fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	return ret;
89238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
89248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
892675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen/* Set kernel driver on given frequency (MHz) */
892775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinenstatic int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
89288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
892975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	struct i802_bss *bss = priv;
8930a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt	return wpa_driver_nl80211_set_freq(bss, freq);
89318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
89328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic inline int min_int(int a, int b)
89358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
89368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (a < b)
89378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return a;
89388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return b;
89398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
89408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int get_key_handler(struct nl_msg *msg, void *arg)
89438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
89448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
89458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
89468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
89488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
89498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
89518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * TODO: validate the key index and mac address!
89528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Otherwise, there's a race condition as soon as
89538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * the kernel starts sending key notifications.
89548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
89558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_KEY_SEQ])
89578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
89588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
89598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
89608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
89618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
89648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   int idx, u8 *seq)
89658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
89668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
89678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
89688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
89698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
89718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
89728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
89738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
89758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (addr)
89778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
89788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
89798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
89808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memset(seq, 0, 6);
89828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return send_and_recv_msgs(drv, msg, get_key_handler, seq);
89848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
89851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
89868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
89878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
89888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int i802_set_rts(void *priv, int rts)
89918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
89928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
89938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
89948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
89958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -ENOBUFS;
89968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 val;
89978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
89988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
89998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
90008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
90018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (rts >= 2347)
90038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		val = (u32) -1;
90048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
90058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		val = rts;
90068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
90088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
90098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
90108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
90121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
90138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!ret)
90148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
90158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
90161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
90178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
90188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "%d (%s)", rts, ret, strerror(-ret));
90198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
90208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
90218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int i802_set_frag(void *priv, int frag)
90248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
90258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
90268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
90278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
90288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -ENOBUFS;
90298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u32 val;
90308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
90328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
90338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
90348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (frag >= 2346)
90368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		val = (u32) -1;
90378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
90388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		val = frag;
90398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
90418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
90428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
90438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
90451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
90468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!ret)
90478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
90488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
90491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
90508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
90518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "%d: %d (%s)", frag, ret, strerror(-ret));
90528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
90538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
90548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int i802_flush(void *priv)
90578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
90588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
90598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
90608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
90611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int res;
90628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
90648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
90658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
90668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9067b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9068b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		   bss->ifname);
90691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
90708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
90728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * XXX: FIX! this needs to flush all VLANs too
90738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
90748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
90758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    if_nametoindex(bss->ifname));
90768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	res = send_and_recv_msgs(drv, msg, NULL, NULL);
90781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (res) {
90791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
90801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "(%s)", res, strerror(-res));
90811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
90821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return res;
90831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt nla_put_failure:
90841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
90858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
90868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
90878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
90898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int get_sta_handler(struct nl_msg *msg, void *arg)
90908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
90918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
90928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
90938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct hostap_sta_driver_data *data = arg;
90948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
90958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
90968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
90978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
90988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
90998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
91008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		[NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
91011e6c57fee4a56b421cc20f6dc0785c9138b21337Jouni Malinen		[NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
91028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	};
91038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
91058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
91068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
91088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * TODO: validate the interface and mac address!
91098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Otherwise, there's a race condition as soon as
91108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * the kernel starts sending station notifications.
91118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
91128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!tb[NL80211_ATTR_STA_INFO]) {
91148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "sta stats missing!");
91158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
91168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
91178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
91188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     tb[NL80211_ATTR_STA_INFO],
91198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     stats_policy)) {
91208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
91218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL_SKIP;
91228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
91238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (stats[NL80211_STA_INFO_INACTIVE_TIME])
91258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data->inactive_msec =
91268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
91278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (stats[NL80211_STA_INFO_RX_BYTES])
91288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
91298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (stats[NL80211_STA_INFO_TX_BYTES])
91308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
91318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (stats[NL80211_STA_INFO_RX_PACKETS])
91328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data->rx_packets =
91338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
91348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (stats[NL80211_STA_INFO_TX_PACKETS])
91358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data->tx_packets =
91368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
91371e6c57fee4a56b421cc20f6dc0785c9138b21337Jouni Malinen	if (stats[NL80211_STA_INFO_TX_FAILED])
91381e6c57fee4a56b421cc20f6dc0785c9138b21337Jouni Malinen		data->tx_retry_failed =
91391e6c57fee4a56b421cc20f6dc0785c9138b21337Jouni Malinen			nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
91408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
91428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
91438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91444b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int i802_read_sta_data(struct i802_bss *bss,
91454b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt			      struct hostap_sta_driver_data *data,
91468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			      const u8 *addr)
91478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
91488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
91498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
91508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(data, 0, sizeof(*data));
91528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
91538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
91548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
91558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
91578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
91598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
91608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return send_and_recv_msgs(drv, msg, get_sta_handler, data);
91628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
91631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
91648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -ENOBUFS;
91658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
91668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int i802_set_tx_queue_params(void *priv, int queue, int aifs,
91698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    int cw_min, int cw_max, int burst_time)
91708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
91718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
91728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
91738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
91748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *txq, *params;
91758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
91778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
91788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
91798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
91818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
91838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
91858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!txq)
91868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
91878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* We are only sending parameters for a single TXQ at a time */
91898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	params = nla_nest_start(msg, 1);
91908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!params)
91918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
91928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
91938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (queue) {
91948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case 0:
91958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
91968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
91978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case 1:
91988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
91998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
92008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case 2:
92018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
92028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
92038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case 3:
92048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
92058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
92068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
92078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Burst time is configured in units of 0.1 msec and TXOP parameter in
92088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * 32 usec, so need to convert the value here. */
92098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
92108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
92118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
92128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
92138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_nest_end(msg, params);
92158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_nest_end(msg, txq);
92178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
92198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
92201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
92218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
92221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
92238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
92248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
92258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92274b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
92288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     const char *ifname, int vlan_id)
92298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
92308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
92318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
92328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -ENOBUFS;
92338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
92358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
92368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -ENOMEM;
92378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9238cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9239cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		   ", ifname=%s[%d], vlan_id=%d)",
9240cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		   bss->ifname, if_nametoindex(bss->ifname),
9241cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		   MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
92421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
92438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
92458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    if_nametoindex(bss->ifname));
92468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
92478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
92488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    if_nametoindex(ifname));
92498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
92511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
92528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret < 0) {
92538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
92548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
92558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MAC2STR(addr), ifname, vlan_id, ret,
92568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   strerror(-ret));
92578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
92588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
92591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
92608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
92618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
92628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int i802_get_inact_sec(void *priv, const u8 *addr)
92658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
92668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct hostap_sta_driver_data data;
92678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
92688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	data.inactive_msec = (unsigned long) -1;
92708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = i802_read_sta_data(priv, &data, addr);
92718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret || data.inactive_msec == (unsigned long) -1)
92728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
92738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return data.inactive_msec / 1000;
92748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
92758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int i802_sta_clear_stats(void *priv, const u8 *addr)
92788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
92798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#if 0
92808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* TODO */
92818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif
92828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
92838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
92848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
92868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
92878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   int reason)
92888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
92898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
929004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
92918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct ieee80211_mgmt mgmt;
92928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
929304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (drv->device_ap_sme)
929404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		return wpa_driver_nl80211_sta_remove(bss, addr);
929504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
92968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memset(&mgmt, 0, sizeof(mgmt));
92978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
92988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  WLAN_FC_STYPE_DEAUTH);
92998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(mgmt.da, addr, ETH_ALEN);
93008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(mgmt.sa, own_addr, ETH_ALEN);
93018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
93028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mgmt.u.deauth.reason_code = host_to_le16(reason);
93038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
93048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					    IEEE80211_HDRLEN +
93054b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					    sizeof(mgmt.u.deauth), 0, 0, 0, 0,
93064b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					    0);
93078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
93088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
93098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
93108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
93118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     int reason)
93128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
93138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
931404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
93158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct ieee80211_mgmt mgmt;
93168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
931704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (drv->device_ap_sme)
931804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		return wpa_driver_nl80211_sta_remove(bss, addr);
931904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
93208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memset(&mgmt, 0, sizeof(mgmt));
93218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
93228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  WLAN_FC_STYPE_DISASSOC);
93238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(mgmt.da, addr, ETH_ALEN);
93248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(mgmt.sa, own_addr, ETH_ALEN);
93258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
93268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	mgmt.u.disassoc.reason_code = host_to_le16(reason);
93278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
93288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					    IEEE80211_HDRLEN +
93294b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					    sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
93304b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					    0);
93318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
93328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
93338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
933475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinenstatic void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
933575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen{
933675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	int i;
933775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	int *old;
933875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
933975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
934075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		   ifidx);
934175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	for (i = 0; i < drv->num_if_indices; i++) {
934275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		if (drv->if_indices[i] == 0) {
934375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			drv->if_indices[i] = ifidx;
934475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			return;
934575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		}
934675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	}
934775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
934875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	if (drv->if_indices != drv->default_if_indices)
934975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		old = drv->if_indices;
935075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	else
935175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		old = NULL;
935275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
935361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
935461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt					   sizeof(int));
935575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	if (!drv->if_indices) {
935675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		if (!old)
935775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			drv->if_indices = drv->default_if_indices;
935875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		else
935975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			drv->if_indices = old;
936075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
936175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			   "interfaces");
936275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
936375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		return;
936475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	} else if (!old)
936575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		os_memcpy(drv->if_indices, drv->default_if_indices,
936675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			  sizeof(drv->default_if_indices));
936775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	drv->if_indices[drv->num_if_indices] = ifidx;
936875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	drv->num_if_indices++;
936975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen}
937075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
937175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
937275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinenstatic void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
937375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen{
937475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	int i;
937575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
937675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	for (i = 0; i < drv->num_if_indices; i++) {
937775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		if (drv->if_indices[i] == ifidx) {
937875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			drv->if_indices[i] = 0;
937975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			break;
938075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		}
938175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	}
938275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen}
938375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
938475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
938575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinenstatic int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
938675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen{
938775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	int i;
938875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
938975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	for (i = 0; i < drv->num_if_indices; i++)
939075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		if (drv->if_indices[i] == ifidx)
939175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			return 1;
939275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
939375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	return 0;
939475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen}
939575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
939675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
939775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinenstatic int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
9398c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt                            const char *bridge_ifname, char *ifname_wds)
939975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen{
940075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	struct i802_bss *bss = priv;
940175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	struct wpa_driver_nl80211_data *drv = bss->drv;
940275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	char name[IFNAMSIZ + 1];
940375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
940475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
9405c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	if (ifname_wds)
9406c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9407c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt
940875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
940975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		   " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
941075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	if (val) {
941175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		if (!if_nametoindex(name)) {
941275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			if (nl80211_create_iface(drv, name,
941375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen						 NL80211_IFTYPE_AP_VLAN,
9414cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt						 bss->addr, 1, NULL, NULL, 0) <
9415cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			    0)
941675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen				return -1;
941775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			if (bridge_ifname &&
94181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			    linux_br_add_if(drv->global->ioctl_sock,
94191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					    bridge_ifname, name) < 0)
942075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen				return -1;
942175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		}
942261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
942361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
942461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				   "interface %s up", name);
942561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		}
942675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		return i802_set_sta_vlan(priv, addr, name, 0);
942775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	} else {
9428aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt		if (bridge_ifname)
9429aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt			linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9430aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt					name);
9431aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt
943275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		i802_set_sta_vlan(priv, addr, bss->ifname, 0);
943375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
943475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen						    name);
943575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	}
943675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen}
943775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
943875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
943975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinenstatic void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
944075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen{
944175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	struct wpa_driver_nl80211_data *drv = eloop_ctx;
944275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	struct sockaddr_ll lladdr;
944375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	unsigned char buf[3000];
944475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	int len;
944575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	socklen_t fromlen = sizeof(lladdr);
944675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
944775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	len = recvfrom(sock, buf, sizeof(buf), 0,
944875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		       (struct sockaddr *)&lladdr, &fromlen);
944975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	if (len < 0) {
945068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
945168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   strerror(errno));
945275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		return;
945375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	}
945475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
945575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	if (have_ifidx(drv, lladdr.sll_ifindex))
945675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
945775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen}
945875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
945975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
94608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
94618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     struct i802_bss *bss,
94628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     const char *brname, const char *ifname)
94638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
94648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ifindex;
94658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char in_br[IFNAMSIZ];
94668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
94678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_strlcpy(bss->brname, brname, IFNAMSIZ);
94688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ifindex = if_nametoindex(brname);
94698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ifindex == 0) {
94708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
94718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Bridge was configured, but the bridge device does
94728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * not exist. Try to add it now.
94738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
94741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
94758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
94768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "bridge interface %s: %s",
94778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   brname, strerror(errno));
94788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
94798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
94808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		bss->added_bridge = 1;
94818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		add_ifidx(drv, if_nametoindex(brname));
94828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
94838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
94848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (linux_br_get(in_br, ifname) == 0) {
94858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_strcmp(in_br, brname) == 0)
94868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return 0; /* already in the bridge */
94878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
94888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
94898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "bridge %s", ifname, in_br);
94901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
94911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    0) {
94928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_ERROR, "nl80211: Failed to "
94938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "remove interface %s from bridge "
94948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "%s: %s",
94958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   ifname, brname, strerror(errno));
94968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
94978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
94988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
94998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
95018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   ifname, brname);
95021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
95038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
95048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "into bridge %s: %s",
95058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   ifname, brname, strerror(errno));
95068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
95078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
95088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	bss->added_if_into_bridge = 1;
95098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
95118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
95128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void *i802_init(struct hostapd_data *hapd,
95158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       struct wpa_init_params *params)
95168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
95178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv;
95188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss;
95198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t i;
95208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char brname[IFNAMSIZ];
95218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ifindex, br_ifindex;
95228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int br_added = 0;
95238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9524e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
9525e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt					  params->global_priv, 1,
9526e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt					  params->bssid);
95278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss == NULL)
95288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
95298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv = bss->drv;
95311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
95328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (linux_br_get(brname, params->ifname) == 0) {
95338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
95348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   params->ifname, brname);
95358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		br_ifindex = if_nametoindex(brname);
95368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
95378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		brname[0] = '\0';
95388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		br_ifindex = 0;
95398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
95408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < params->num_bridge; i++) {
95428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (params->bridge[i]) {
95438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			ifindex = if_nametoindex(params->bridge[i]);
95448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (ifindex)
95458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				add_ifidx(drv, ifindex);
95468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (ifindex == br_ifindex)
95478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				br_added = 1;
95488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
95498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
95508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!br_added && br_ifindex &&
95518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    (params->num_bridge == 0 || !params->bridge[0]))
95528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		add_ifidx(drv, br_ifindex);
95538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* start listening for EAPOL on the default AP interface */
95558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	add_ifidx(drv, drv->ifindex);
95568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (params->num_bridge && params->bridge[0] &&
95588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
95598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto failed;
95608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
95628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (drv->eapol_sock < 0) {
956368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
956468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   strerror(errno));
95658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto failed;
95668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
95678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
95698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	{
957068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
95718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto failed;
95728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
95738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
95751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			       params->own_addr))
95768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto failed;
95778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	memcpy(bss->addr, params->own_addr, ETH_ALEN);
95791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
95808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return bss;
95818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtfailed:
95831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_driver_nl80211_deinit(bss);
95848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NULL;
95858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
95868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void i802_deinit(void *priv)
95898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
95904b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
95914b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	wpa_driver_nl80211_deinit(bss);
95928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
95938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
95958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic enum nl80211_iftype wpa_driver_nl80211_if_type(
95968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	enum wpa_driver_if_type type)
95978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
95988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (type) {
95998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPA_IF_STATION:
96008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL80211_IFTYPE_STATION;
96018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPA_IF_P2P_CLIENT:
96028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPA_IF_P2P_GROUP:
96038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL80211_IFTYPE_P2P_CLIENT;
96048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPA_IF_AP_VLAN:
96058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL80211_IFTYPE_AP_VLAN;
96068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPA_IF_AP_BSS:
96078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL80211_IFTYPE_AP;
96088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPA_IF_P2P_GO:
96098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NL80211_IFTYPE_P2P_GO;
961034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	case WPA_IF_P2P_DEVICE:
961134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return NL80211_IFTYPE_P2P_DEVICE;
96128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
96138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
96148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
96158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_P2P
96188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
96208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
96218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv;
96228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_for_each(drv, &global->interfaces,
96238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 struct wpa_driver_nl80211_data, list) {
9624cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
96258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return 1;
96268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
96278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
96288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
96298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
96328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      u8 *new_addr)
96338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
96348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	unsigned int idx;
96358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!drv->global)
96378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
96388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9639cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
96408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (idx = 0; idx < 64; idx++) {
9641cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		new_addr[0] = drv->first_bss->addr[0] | 0x02;
96428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		new_addr[0] ^= idx << 2;
96438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!nl80211_addr_in_use(drv->global, new_addr))
96448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
96458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
96468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (idx == 64)
96478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
96488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
96508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MACSTR, MAC2STR(new_addr));
96518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
96538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
96548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_P2P */
96568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
965834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstruct wdev_info {
965934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	u64 wdev_id;
966034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	int wdev_id_set;
966134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	u8 macaddr[ETH_ALEN];
966234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt};
966334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
966434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
966534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
966634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
966734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
966834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct wdev_info *wi = arg;
966934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
967034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
967134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
967234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (tb[NL80211_ATTR_WDEV]) {
967334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
967434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wi->wdev_id_set = 1;
967534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	}
967634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
967734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (tb[NL80211_ATTR_MAC])
967834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
967934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			  ETH_ALEN);
968034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
968134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return NL_SKIP;
968234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
968334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
968434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
96858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
96868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     const char *ifname, const u8 *addr,
96878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     void *bss_ctx, void **drv_priv,
96888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     char *force_ifname, u8 *if_addr,
9689cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				     const char *bridge, int use_existing)
96908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
969134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	enum nl80211_iftype nlmode;
96928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
96938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
96948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ifidx;
9695cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	int added = 1;
96968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
96978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (addr)
96988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(if_addr, addr, ETH_ALEN);
969934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nlmode = wpa_driver_nl80211_if_type(type);
970034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
970134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		struct wdev_info p2pdev_info;
970234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
970334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
970434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
970534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt					     0, nl80211_wdev_handler,
9706cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt					     &p2pdev_info, use_existing);
970734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		if (!p2pdev_info.wdev_id_set || ifidx != 0) {
970834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
970934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				   ifname);
971034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			return -1;
971134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		}
971234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
971334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		drv->global->if_add_wdevid = p2pdev_info.wdev_id;
971434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
971534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		if (!is_zero_ether_addr(p2pdev_info.macaddr))
971634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
971734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
971834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   ifname,
971934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			   (long long unsigned int) p2pdev_info.wdev_id);
972034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	} else {
972134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
9722cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt					     0, NULL, NULL, use_existing);
9723cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		if (use_existing && ifidx == -ENFILE) {
9724cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			added = 0;
9725cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			ifidx = if_nametoindex(ifname);
9726cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		} else if (ifidx < 0) {
972734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			return -1;
972834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		}
97298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
97308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
973134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (!addr) {
973234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
973334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			os_memcpy(if_addr, bss->addr, ETH_ALEN);
973434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
973534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt					    bss->ifname, if_addr) < 0) {
9736cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			if (added)
9737cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				nl80211_remove_iface(drv, ifidx);
973834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			return -1;
973934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		}
97408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
97418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
97428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_P2P
97438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!addr &&
97448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
97458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	     type == WPA_IF_P2P_GO)) {
97468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Enforce unique P2P Interface Address */
974734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		u8 new_addr[ETH_ALEN];
97488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
974934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
97501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				       new_addr) < 0) {
97518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nl80211_remove_iface(drv, ifidx);
97528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
97538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
975434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		if (nl80211_addr_in_use(drv->global, new_addr)) {
97558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
97568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "for P2P group interface");
97578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
97588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				nl80211_remove_iface(drv, ifidx);
97598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				return -1;
97608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
97611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
97628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					       new_addr) < 0) {
97638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				nl80211_remove_iface(drv, ifidx);
97648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				return -1;
97658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
97668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
97676e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidt		os_memcpy(if_addr, new_addr, ETH_ALEN);
97688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
97698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_P2P */
97708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
97718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (type == WPA_IF_AP_BSS) {
977268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
977368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		if (new_bss == NULL) {
9774cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			if (added)
9775cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				nl80211_remove_iface(drv, ifidx);
977668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			return -1;
977768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		}
977868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
977968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		if (bridge &&
978068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		    i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
978168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
978268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				   "interface %s to a bridge %s",
978368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				   ifname, bridge);
9784cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			if (added)
9785cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt				nl80211_remove_iface(drv, ifidx);
978668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			os_free(new_bss);
978768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			return -1;
978868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		}
978968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
97901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
97911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		{
97928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			nl80211_remove_iface(drv, ifidx);
97938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(new_bss);
97948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
97958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
97968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
97971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
97988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		new_bss->ifindex = ifidx;
97998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		new_bss->drv = drv;
9800cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		new_bss->next = drv->first_bss->next;
9801cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		new_bss->freq = drv->first_bss->freq;
9802a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt		new_bss->ctx = bss_ctx;
9803cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		new_bss->added_if = added;
9804cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		drv->first_bss->next = new_bss;
98058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (drv_priv)
98068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			*drv_priv = new_bss;
98071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_init_bss(new_bss);
9808c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
9809c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		/* Subscribe management frames for this WPA_IF_AP_BSS */
9810c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		if (nl80211_setup_ap(new_bss))
9811c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt			return -1;
98128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
98138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv->global)
98151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->global->if_add_ifindex = ifidx;
98161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
98178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
98188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
98198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98214b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
98228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					enum wpa_driver_if_type type,
98238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					const char *ifname)
98248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
98258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
98268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ifindex = if_nametoindex(ifname);
98278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9828cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
9829cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		   __func__, type, ifname, ifindex, bss->added_if);
983001904cfafd75a70b9f29c0220b90bdef45595491Dmitry Shmidt	if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
9831051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		nl80211_remove_iface(drv, ifindex);
9832aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt
9833aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt	if (type != WPA_IF_AP_BSS)
9834aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt		return 0;
9835aa532510a7b8c4da2d7d6e2c11dda5db840894e4Dmitry Shmidt
98368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss->added_if_into_bridge) {
98371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
98381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				    bss->ifname) < 0)
98398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
98408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "interface %s from bridge %s: %s",
98418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   bss->ifname, bss->brname, strerror(errno));
98428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
98438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (bss->added_bridge) {
98441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
98458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
98468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "bridge %s: %s",
98478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   bss->brname, strerror(errno));
98488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
98498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9850cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	if (bss != drv->first_bss) {
98518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct i802_bss *tbss;
98528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9853cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
9854cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
98558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (tbss->next == bss) {
98568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				tbss->next = bss->next;
9857c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt				/* Unsubscribe management frames */
9858c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt				nl80211_teardown_ap(bss);
98591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				nl80211_destroy_bss(bss);
98608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				os_free(bss);
98618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				bss = NULL;
98628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				break;
98638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
98648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
98658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (bss)
98668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "nl80211: %s - could not find "
98678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "BSS %p in the list", __func__, bss);
9868cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	} else {
9869cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
9870cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		nl80211_teardown_ap(bss);
9871cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		if (!bss->added_if && !drv->first_bss->next)
9872cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			wpa_driver_nl80211_del_beacon(drv);
9873cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		nl80211_destroy_bss(bss);
9874cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		if (!bss->added_if)
9875cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			i802_set_iface_flags(bss, 0);
9876cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		if (drv->first_bss->next) {
9877cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			drv->first_bss = drv->first_bss->next;
9878cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			drv->ctx = drv->first_bss->ctx;
9879cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			os_free(bss);
9880cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		} else {
9881cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
9882cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt		}
98838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
98848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
98868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
98878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int cookie_handler(struct nl_msg *msg, void *arg)
98908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
98918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
98928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
98938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u64 *cookie = arg;
98948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
98958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
98968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (tb[NL80211_ATTR_COOKIE])
98978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
98988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NL_SKIP;
98998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
99008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_send_frame_cmd(struct i802_bss *bss,
99038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  unsigned int freq, unsigned int wait,
99048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  const u8 *buf, size_t buf_len,
99051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  u64 *cookie_out, int no_cck, int no_ack,
99061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  int offchanok)
99078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
99081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
99098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
99108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u64 cookie;
99118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -1;
99128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
99148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
99158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
99168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99178da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
991804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		   "no_ack=%d offchanok=%d",
991904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		   freq, wait, no_cck, no_ack, offchanok);
9920c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
99211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
99228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
992334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nl80211_set_iface_id(msg, bss) < 0)
992434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
9925c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt	if (freq)
9926c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
99276e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidt	if (wait)
99286e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
9929c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
99301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
99311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (no_cck)
99321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
99331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (no_ack)
99341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
99351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
99368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
99378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	cookie = 0;
99398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
99408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
99418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
99428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
99436e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidt			   "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
99446e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidt			   freq, wait);
99458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
99468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
99478da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
99481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   "cookie 0x%llx", no_ack ? " (no ACK)" : "",
99491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   (long long unsigned int) cookie);
99508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cookie_out)
99521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		*cookie_out = no_ack ? (u64) -1 : cookie;
99538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
99558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
99568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
99578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
99588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99604b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_send_action(struct i802_bss *bss,
99614b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					  unsigned int freq,
99628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  unsigned int wait_time,
99638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  const u8 *dst, const u8 *src,
99648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  const u8 *bssid,
99651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					  const u8 *data, size_t data_len,
99661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					  int no_cck)
99678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
99688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
99698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret = -1;
99708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *buf;
99718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct ieee80211_hdr *hdr;
99728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
9974c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		   "freq=%u MHz wait=%d ms no_cck=%d)",
9975c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		   drv->ifindex, freq, wait_time, no_cck);
99768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	buf = os_zalloc(24 + data_len);
99788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (buf == NULL)
99798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return ret;
99808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(buf + 24, data, data_len);
99818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr = (struct ieee80211_hdr *) buf;
99828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hdr->frame_control =
99838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
99848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(hdr->addr1, dst, ETH_ALEN);
99858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(hdr->addr2, src, ETH_ALEN);
99868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(hdr->addr3, bssid, ETH_ALEN);
99878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
99885605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	if (is_ap_interface(drv->nlmode) &&
99895605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	    (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
99905605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	     (int) freq == bss->freq || drv->device_ap_sme ||
99915605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	     !drv->use_monitor))
99924b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt		ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
99934b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt						   0, freq, no_cck, 1,
99944b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt						   wait_time);
99958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
99961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
99978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     24 + data_len,
99981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					     &drv->send_action_cookie,
99991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					     no_cck, 0, 1);
100008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(buf);
100028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
100038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
100048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
100078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
100088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
100098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
100108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
100118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
100128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
100148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
100158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
100168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100172f3b8dec59373945c5feef8a78ced8967a80cc66Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
100182f3b8dec59373945c5feef8a78ced8967a80cc66Dmitry Shmidt		   (long long unsigned int) drv->send_action_cookie);
100191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
100208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1002134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nl80211_set_iface_id(msg, bss) < 0)
1002234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
100238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
100248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
100268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
100278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret)
100288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
100298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(%s)", ret, strerror(-ret));
100308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt nla_put_failure:
100328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
100338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
100348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
100378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						unsigned int duration)
100388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
100398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
100408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
100418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
100428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
100438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u64 cookie;
100448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
100468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
100478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
100488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
100508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1005134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nl80211_set_iface_id(msg, bss) < 0)
1005234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
1005334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
100548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
100558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
100568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	cookie = 0;
100588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
100591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
100608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret == 0) {
100618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
100628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "0x%llx for freq=%u MHz duration=%u",
100638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   (long long unsigned int) cookie, freq, duration);
100648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		drv->remain_on_chan_cookie = cookie;
100651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv->pending_remain_on_chan = 1;
100668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
100678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
100688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
100698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "(freq=%d duration=%u): %d (%s)",
100708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   freq, duration, ret, strerror(-ret));
100718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
100721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
100738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
100748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
100758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
100788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
100798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
100808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
100818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
100828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
100838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!drv->pending_remain_on_chan) {
100858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
100868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "to cancel");
100878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
100888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
100898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
100918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "0x%llx",
100928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (long long unsigned int) drv->remain_on_chan_cookie);
100938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
100958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
100968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
100978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
100981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
100998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1010034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (nl80211_set_iface_id(msg, bss) < 0)
1010134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		goto nla_put_failure;
1010234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
101038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
101048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
101061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = NULL;
101078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret == 0)
101088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
101098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
101108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "%d (%s)", ret, strerror(-ret));
101118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
101121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
101138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
101148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
101158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101174b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
101188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
101198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
101206e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidt
101218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!report) {
101221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (bss->nl_preq && drv->device_ap_sme &&
101231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    is_ap_interface(drv->nlmode)) {
101241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			/*
101251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * Do not disable Probe Request reporting that was
101261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * enabled in nl80211_setup_ap().
101271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 */
101281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
101291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "Probe Request reporting nl_preq=%p while "
101301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "in AP mode", bss->nl_preq);
101311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		} else if (bss->nl_preq) {
101321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
101331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "reporting nl_preq=%p", bss->nl_preq);
1013468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			nl80211_destroy_eloop_handle(&bss->nl_preq);
101358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
101368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
101378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
101388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (bss->nl_preq) {
101408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
101411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "already on! nl_preq=%p", bss->nl_preq);
101428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
101438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
101448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
101461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (bss->nl_preq == NULL)
101478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
101481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
101491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   "reporting nl_preq=%p", bss->nl_preq);
101508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (nl80211_register_frame(bss, bss->nl_preq,
101528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   (WLAN_FC_TYPE_MGMT << 2) |
101538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   (WLAN_FC_STYPE_PROBE_REQ << 4),
101541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   NULL, 0) < 0)
101551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto out_err;
10156497c1d5e50162d6b3c1cce5dbd9c5fd9da69aaefDmitry Shmidt
1015768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	nl80211_register_eloop_read(&bss->nl_preq,
1015868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				    wpa_driver_nl80211_event_receive,
1015968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt				    bss->nl_cb);
101608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
101628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt out_err:
101641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_destroy_handles(&bss->nl_preq);
101658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
101668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
101678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
101708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     int ifindex, int disabled)
101718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
101728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl_msg *msg;
101738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nlattr *bands, *band;
101748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
101758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
101778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
101788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
101798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
101818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
101828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
101848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!bands)
101858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
101868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
101878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
101888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
101898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
101908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * rates. All 5 GHz rates are left enabled.
101918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
101928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	band = nla_nest_start(msg, NL80211_BAND_2GHZ);
101938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!band)
101948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto nla_put_failure;
101951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (disabled) {
101961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
101971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			"\x0c\x12\x18\x24\x30\x48\x60\x6c");
101981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
101998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_nest_end(msg, band);
102008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
102018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nla_nest_end(msg, bands);
102028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
102038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
102048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
102058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret) {
102068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
102078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(%s)", ret, strerror(-ret));
1020861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	} else
1020961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		drv->disabled_11b_rates = disabled;
1021061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
102118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
102128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
102138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
102148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
102158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
102168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
102178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
102188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
102198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_driver_nl80211_deinit_ap(void *priv)
102208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
102218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
102228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
102231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!is_ap_interface(drv->nlmode))
102248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
102258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_driver_nl80211_del_beacon(drv);
10226b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10227b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	/*
10228b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	 * If the P2P GO interface was dynamically added, then it is
10229b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	 * possible that the interface change to station is not possible.
10230b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	 */
10231b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10232b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return 0;
10233b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
102341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
102358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
102368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
102378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10238ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidtstatic int wpa_driver_nl80211_stop_ap(void *priv)
10239ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt{
10240ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	struct i802_bss *bss = priv;
10241ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
10242ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	if (!is_ap_interface(drv->nlmode))
10243ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		return -1;
10244ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	wpa_driver_nl80211_del_beacon(drv);
10245ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	bss->beacon_set = 0;
10246ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	return 0;
10247ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt}
10248ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
10249ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
1025004949598a23f501be6eec21697465fd46a28840aDmitry Shmidtstatic int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
1025104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt{
1025204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	struct i802_bss *bss = priv;
1025304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
1025404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
1025504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		return -1;
10256b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10257b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	/*
10258b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	 * If the P2P Client interface was dynamically added, then it is
10259b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	 * possible that the interface change to station is not possible.
10260b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	 */
10261b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (bss->if_dynamic)
10262b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return 0;
10263b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
1026404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
1026504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt}
1026604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
1026704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
102688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wpa_driver_nl80211_resume(void *priv)
102698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
102708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
1027134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1027234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (i802_set_iface_flags(bss, 1))
1027334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
102748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
102758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
102768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
102778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
102788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  const u8 *ies, size_t ies_len)
102798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
102808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
102818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
102828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
102838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *data, *pos;
102848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t data_len;
102851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	const u8 *own_addr = bss->addr;
102868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
102878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (action != 1) {
102888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
102898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "action %d", action);
102908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
102918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
102928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
102938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
102948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Action frame payload:
102958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Category[1] = 6 (Fast BSS Transition)
102968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Action[1] = 1 (Fast BSS Transition Request)
102978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * STA Address
102988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Target AP Address
102998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * FT IEs
103008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
103018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	data_len = 2 + 2 * ETH_ALEN + ies_len;
103038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	data = os_malloc(data_len);
103048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (data == NULL)
103058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
103068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = data;
103078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*pos++ = 0x06; /* FT Action category */
103088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*pos++ = action;
103098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, own_addr, ETH_ALEN);
103108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += ETH_ALEN;
103118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, target_ap, ETH_ALEN);
103128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += ETH_ALEN;
103138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(pos, ies, ies_len);
103148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
103168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     drv->bssid, own_addr, drv->bssid,
103171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					     data, data_len, 0);
103188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(data);
103198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
103218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
103228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
103258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
103268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
103278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
103288da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	struct nl_msg *msg;
103298da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	struct nlattr *cqm;
1033061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	int ret = -1;
103318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
103338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "hysteresis=%d", threshold, hysteresis);
103348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = nlmsg_alloc();
103368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!msg)
103378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
103388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
103408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
103428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103438da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
103448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cqm == NULL)
1034561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		goto nla_put_failure;
103468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103478da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
103488da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
103498da800a193fb6f8832218715f82a7b4e2d2ad338Dmitry Shmidt	nla_nest_end(msg, cqm);
103508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1035161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
103528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = NULL;
103538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnla_put_failure:
103558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	nlmsg_free(msg);
1035661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	return ret;
103578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
103588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1036034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic int get_channel_width(struct nl_msg *msg, void *arg)
1036134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
1036234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
1036334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1036434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct wpa_signal_info *sig_change = arg;
1036534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1036634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1036734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
1036834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1036934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	sig_change->center_frq1 = -1;
1037034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	sig_change->center_frq2 = -1;
1037134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
1037234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1037334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
1037434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		sig_change->chanwidth = convert2width(
1037534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
1037634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		if (tb[NL80211_ATTR_CENTER_FREQ1])
1037734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			sig_change->center_frq1 =
1037834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
1037934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		if (tb[NL80211_ATTR_CENTER_FREQ2])
1038034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt			sig_change->center_frq2 =
1038134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
1038234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	}
1038334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1038434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return NL_SKIP;
1038534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
1038634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1038734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1038834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtstatic int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
1038934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt				     struct wpa_signal_info *sig)
1039034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
1039134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct nl_msg *msg;
1039234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1039334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	msg = nlmsg_alloc();
1039434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (!msg)
1039534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return -ENOMEM;
1039634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1039734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
1039834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1039934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1040034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return send_and_recv_msgs(drv, msg, get_channel_width, sig);
1040134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1040234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtnla_put_failure:
1040334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	nlmsg_free(msg);
1040434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return -ENOBUFS;
1040534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
1040634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1040734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
104088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
104098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
104108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
104118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
104128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int res;
104138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(si, 0, sizeof(*si));
104158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	res = nl80211_get_link_signal(drv, si);
104168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (res != 0)
104178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return res;
104188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1041934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	res = nl80211_get_channel_width(drv, si);
1042034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (res != 0)
1042134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return res;
1042234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
104238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return nl80211_get_link_noise(drv, si);
104248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
104258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int wpa_driver_nl80211_shared_freq(void *priv)
104288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
104298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
104308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
104311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *driver;
104321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int freq = 0;
104338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/*
104351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * If the same PHY is in connected state with some other interface,
104361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * then retrieve the assoc freq.
104371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 */
104381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
104391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   drv->phyname);
104408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	dl_list_for_each(driver, &drv->global->interfaces,
104421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 struct wpa_driver_nl80211_data, list) {
104431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (drv == driver ||
104441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    os_strcmp(drv->phyname, driver->phyname) != 0 ||
104451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    !driver->associated)
104461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			continue;
104478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
104491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   MACSTR,
10450cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			   driver->phyname, driver->first_bss->ifname,
10451cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			   MAC2STR(driver->first_bss->addr));
1045204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		if (is_ap_interface(driver->nlmode))
10453cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt			freq = driver->first_bss->freq;
104541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		else
104551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			freq = nl80211_get_assoc_freq(driver);
104561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
104571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   drv->phyname, freq);
104581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
104598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!freq)
104611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
104621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "PHY (%s) in associated state", drv->phyname);
104638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return freq;
104651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
104668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
104681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
104691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			      int encrypt)
104701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
104711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *bss = priv;
10472c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10473c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					     0, 0, 0, 0);
104748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
104758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int nl80211_set_param(void *priv, const char *param)
104788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
104798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
104808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (param == NULL)
104818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
104828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_P2P
104848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_strstr(param, "use_p2p_group_interface=1")) {
104858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct i802_bss *bss = priv;
104868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct wpa_driver_nl80211_data *drv = bss->drv;
104878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
104888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
104898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "interface");
104908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
104918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
104928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
1049334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1049434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (os_strstr(param, "p2p_device=1")) {
1049534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		struct i802_bss *bss = priv;
1049634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		struct wpa_driver_nl80211_data *drv = bss->drv;
1049734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		drv->allow_p2p_device = 1;
1049834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	}
104998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_P2P */
105008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10501fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (os_strstr(param, "use_monitor=1")) {
10502fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		struct i802_bss *bss = priv;
10503fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		struct wpa_driver_nl80211_data *drv = bss->drv;
10504fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		drv->use_monitor = 1;
10505fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
10506fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
10507fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (os_strstr(param, "force_connect_cmd=1")) {
10508fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		struct i802_bss *bss = priv;
10509fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		struct wpa_driver_nl80211_data *drv = bss->drv;
10510fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
10511fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	}
10512fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
105138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
105148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
105158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
105168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
105178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void * nl80211_global_init(void)
105188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
105198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl80211_global *global;
105201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct netlink_config *cfg;
105211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	global = os_zalloc(sizeof(*global));
105238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (global == NULL)
105248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
105251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	global->ioctl_sock = -1;
105268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_init(&global->interfaces);
105271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	global->if_add_ifindex = -1;
105281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	cfg = os_zalloc(sizeof(*cfg));
105301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (cfg == NULL)
105311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto err;
105321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	cfg->ctx = global;
105341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
105351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
105361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	global->netlink = netlink_init(cfg);
105371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (global->netlink == NULL) {
105381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		os_free(cfg);
105391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto err;
105401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
105411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (wpa_driver_nl80211_init_nl_global(global) < 0)
105431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto err;
105441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
105461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (global->ioctl_sock < 0) {
1054768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
1054868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt			   strerror(errno));
105491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto err;
105501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
105511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return global;
105531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidterr:
105551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_global_deinit(global);
105561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return NULL;
105578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
105588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
105598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
105608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void nl80211_global_deinit(void *priv)
105618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
105628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct nl80211_global *global = priv;
105638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (global == NULL)
105648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
105658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!dl_list_empty(&global->interfaces)) {
105668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
105678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "nl80211_global_deinit",
105688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   dl_list_len(&global->interfaces));
105698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
105701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (global->netlink)
105721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		netlink_deinit(global->netlink);
105731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_destroy_handles(&global->nl);
105751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1057668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	if (global->nl_event)
1057768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt		nl80211_destroy_eloop_handle(&global->nl_event);
105781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl_cb_put(global->nl_cb);
105801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (global->ioctl_sock >= 0)
105821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		close(global->ioctl_sock);
105831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
105848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(global);
105858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
105868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
105878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
105888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const char * nl80211_get_radio_name(void *priv)
105898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
105908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct i802_bss *bss = priv;
105918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
105928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return drv->phyname;
105938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
105948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
105958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1059675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinenstatic int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
1059775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen			 const u8 *pmkid)
1059875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen{
1059975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	struct nl_msg *msg;
1060075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
1060175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	msg = nlmsg_alloc();
1060275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	if (!msg)
1060375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		return -ENOMEM;
1060475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
106051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(bss->drv, msg, 0, cmd);
1060675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
1060775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
1060875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	if (pmkid)
1060975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
1061075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	if (bssid)
1061175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
1061275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
1061375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
1061475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen nla_put_failure:
106151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
1061675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	return -ENOBUFS;
1061775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen}
1061875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
1061975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
1062075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinenstatic int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
1062175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen{
1062275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	struct i802_bss *bss = priv;
1062375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
1062475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
1062575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen}
1062675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
1062775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
1062875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinenstatic int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
1062975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen{
1063075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	struct i802_bss *bss = priv;
1063175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
1063275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen		   MAC2STR(bssid));
1063375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
1063475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen}
1063575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
1063675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
1063775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinenstatic int nl80211_flush_pmkid(void *priv)
1063875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen{
1063975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	struct i802_bss *bss = priv;
1064075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
1064175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
1064275ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen}
1064375ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
1064475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
10645b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidtstatic void clean_survey_results(struct survey_results *survey_results)
10646b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt{
10647b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	struct freq_survey *survey, *tmp;
10648b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10649b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (dl_list_empty(&survey_results->survey_list))
10650b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return;
10651b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10652b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10653b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			      struct freq_survey, list) {
10654b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		dl_list_del(&survey->list);
10655b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		os_free(survey);
10656b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	}
10657b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt}
10658b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10659b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10660b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidtstatic void add_survey(struct nlattr **sinfo, u32 ifidx,
10661b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		       struct dl_list *survey_list)
10662b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt{
10663b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	struct freq_survey *survey;
10664b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10665b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	survey = os_zalloc(sizeof(struct freq_survey));
10666b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if  (!survey)
10667b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return;
10668b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10669b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	survey->ifidx = ifidx;
10670b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10671b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	survey->filled = 0;
10672b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10673b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10674b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		survey->nf = (int8_t)
10675b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10676b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		survey->filled |= SURVEY_HAS_NF;
10677b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	}
10678b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10679b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10680b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		survey->channel_time =
10681b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10682b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		survey->filled |= SURVEY_HAS_CHAN_TIME;
10683b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	}
10684b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10685b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10686b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		survey->channel_time_busy =
10687b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10688b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10689b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	}
10690b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10691b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10692b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		survey->channel_time_rx =
10693b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10694b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10695b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	}
10696b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10697b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10698b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		survey->channel_time_tx =
10699b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10700b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10701b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	}
10702b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10703b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Freq survey dump event (freq=%d MHz noise=%d channel_time=%ld busy_time=%ld tx_time=%ld rx_time=%ld filled=%04x)",
10704b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		   survey->freq,
10705b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		   survey->nf,
10706b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		   (unsigned long int) survey->channel_time,
10707b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		   (unsigned long int) survey->channel_time_busy,
10708b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		   (unsigned long int) survey->channel_time_tx,
10709b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		   (unsigned long int) survey->channel_time_rx,
10710b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		   survey->filled);
10711b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10712b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	dl_list_add_tail(survey_list, &survey->list);
10713b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt}
10714b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10715b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10716b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidtstatic int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10717b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			   unsigned int freq_filter)
10718b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt{
10719b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (!freq_filter)
10720b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return 1;
10721b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10722b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	return freq_filter == surveyed_freq;
10723b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt}
10724b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10725b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10726b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidtstatic int survey_handler(struct nl_msg *msg, void *arg)
10727b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt{
10728b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	struct nlattr *tb[NL80211_ATTR_MAX + 1];
10729b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10730b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10731b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	struct survey_results *survey_results;
10732b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	u32 surveyed_freq = 0;
10733b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	u32 ifidx;
10734b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10735b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10736b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		[NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10737b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		[NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10738b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	};
10739b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10740b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	survey_results = (struct survey_results *) arg;
10741b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10742b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10743b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		  genlmsg_attrlen(gnlh, 0), NULL);
10744b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
107459767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt	if (!tb[NL80211_ATTR_IFINDEX])
107469767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt		return NL_SKIP;
107479767226d8e6a1adaa33beb9f517ef40dddfa460cDmitry Shmidt
10748b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10749b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10750b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (!tb[NL80211_ATTR_SURVEY_INFO])
10751b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return NL_SKIP;
10752b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10753b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10754b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			     tb[NL80211_ATTR_SURVEY_INFO],
10755b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			     survey_policy))
10756b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return NL_SKIP;
10757b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10758b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10759b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10760b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return NL_SKIP;
10761b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	}
10762b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10763b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10764b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10765b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (!check_survey_ok(sinfo, surveyed_freq,
10766b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			     survey_results->freq_filter))
10767b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return NL_SKIP;
10768b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10769b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (survey_results->freq_filter &&
10770b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	    survey_results->freq_filter != surveyed_freq) {
10771b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10772b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			   surveyed_freq);
10773b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return NL_SKIP;
10774b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	}
10775b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10776b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	add_survey(sinfo, ifidx, &survey_results->survey_list);
10777b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10778b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	return NL_SKIP;
10779b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt}
10780b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10781b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10782b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidtstatic int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10783b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt{
10784b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	struct i802_bss *bss = priv;
10785b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
10786b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	struct nl_msg *msg;
10787b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	int err = -ENOBUFS;
10788b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	union wpa_event_data data;
10789b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	struct survey_results *survey_results;
10790b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10791b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	os_memset(&data, 0, sizeof(data));
10792b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	survey_results = &data.survey_results;
10793b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10794b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	dl_list_init(&survey_results->survey_list);
10795b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10796b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	msg = nlmsg_alloc();
10797b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (!msg)
10798b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		goto nla_put_failure;
10799b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10800b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
10801b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10802b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10803b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10804b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (freq)
10805b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		data.survey_results.freq_filter = freq;
10806b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10807b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	do {
10808b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
10809b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		err = send_and_recv_msgs(drv, msg, survey_handler,
10810b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt					 survey_results);
10811b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	} while (err > 0);
10812b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10813b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	if (err) {
10814b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
10815b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		goto out_clean;
10816b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	}
10817b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10818b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
10819b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10820b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidtout_clean:
10821b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	clean_survey_results(survey_results);
10822b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidtnla_put_failure:
10823b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	return err;
10824b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt}
10825b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
10826b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
108271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
108281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   const u8 *replay_ctr)
108291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
108301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *bss = priv;
108311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
108321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nlattr *replay_nested;
108331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
108341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
108361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
108371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
108381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
108401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
108421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
108441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!replay_nested)
108451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		goto nla_put_failure;
108461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
108481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
108491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
108501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		replay_ctr);
108511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nla_nest_end(msg, replay_nested);
108531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	send_and_recv_msgs(drv, msg, NULL, NULL);
108551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return;
108561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt nla_put_failure:
108571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
108581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
108591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
108621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				    const u8 *addr, int qos)
108631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
108641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* send data frame to poll STA and check whether
108651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	 * this frame is ACKed */
108661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct {
108671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		struct ieee80211_hdr hdr;
108681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		u16 qos_ctl;
108691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	} STRUCT_PACKED nulldata;
108701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	size_t size;
108711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	/* Send data frame to poll STA and check whether this frame is ACKed */
108731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&nulldata, 0, sizeof(nulldata));
108751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (qos) {
108771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nulldata.hdr.frame_control =
108781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			IEEE80211_FC(WLAN_FC_TYPE_DATA,
108791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     WLAN_FC_STYPE_QOS_NULL);
108801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		size = sizeof(nulldata);
108811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	} else {
108821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nulldata.hdr.frame_control =
108831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			IEEE80211_FC(WLAN_FC_TYPE_DATA,
108841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     WLAN_FC_STYPE_NULLFUNC);
108851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		size = sizeof(struct ieee80211_hdr);
108861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
108871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
108891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
108901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
108911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
108921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108934b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
108944b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					 0, 0) < 0)
108951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
108961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   "send poll frame");
108971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
108981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
108991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
109001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				int qos)
109011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
109021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *bss = priv;
109031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
109041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
109051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!drv->poll_command_supported) {
109071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_send_null_frame(bss, own_addr, addr, qos);
109081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
109091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
109101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
109121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
109131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return;
109141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
109161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
109181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
109191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	send_and_recv_msgs(drv, msg, NULL, NULL);
109211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return;
109221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt nla_put_failure:
109231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
109241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
109251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_set_power_save(struct i802_bss *bss, int enabled)
109281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
109291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
109301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
109321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
109331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -ENOMEM;
109341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
109361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
109371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
109381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
109391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
109401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtnla_put_failure:
109411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
109421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return -ENOBUFS;
109431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
109441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
109471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				     int ctwindow)
109481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
109491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *bss = priv;
109501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
109521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		   "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
109531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
10954292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidt	if (opp_ps != -1 || ctwindow != -1) {
10955c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt#ifdef ANDROID_P2P
10956c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
10957292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidt#else /* ANDROID_P2P */
109581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1; /* Not yet supported */
10959292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidt#endif /* ANDROID_P2P */
10960292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidt	}
109611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (legacy_ps == -1)
109631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return 0;
109641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (legacy_ps != 0 && legacy_ps != 1)
109651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1; /* Not yet supported */
109661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return nl80211_set_power_save(bss, legacy_ps);
109681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
109691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
109701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
10971051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtstatic int nl80211_start_radar_detection(void *priv,
10972051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt					 struct hostapd_freq_params *freq)
10973ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt{
10974ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	struct i802_bss *bss = priv;
10975ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
10976ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	struct nl_msg *msg;
10977ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	int ret;
10978ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
10979051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
10980051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		   freq->freq, freq->ht_enabled, freq->vht_enabled,
10981051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		   freq->bandwidth, freq->center_freq1, freq->center_freq2);
10982051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
10983ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
10984ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
10985ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt			   "detection");
10986ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		return -1;
10987ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	}
10988ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
10989ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	msg = nlmsg_alloc();
10990ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	if (!msg)
10991ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		return -1;
10992ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
10993ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
10994ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10995051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
10996ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
10997051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	if (freq->vht_enabled) {
10998051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		switch (freq->bandwidth) {
10999051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		case 20:
11000051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11001051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				    NL80211_CHAN_WIDTH_20);
11002051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
11003051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		case 40:
11004051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11005051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				    NL80211_CHAN_WIDTH_40);
11006051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
11007051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		case 80:
11008051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			if (freq->center_freq2)
11009051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11010051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt					    NL80211_CHAN_WIDTH_80P80);
11011051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			else
11012051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11013051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt					    NL80211_CHAN_WIDTH_80);
11014051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
11015051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		case 160:
11016051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11017051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				    NL80211_CHAN_WIDTH_160);
11018051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
11019051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		default:
11020051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			return -1;
11021051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		}
11022051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
11023051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		if (freq->center_freq2)
11024051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
11025051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				    freq->center_freq2);
11026051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	} else if (freq->ht_enabled) {
11027051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		switch (freq->sec_channel_offset) {
11028051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		case -1:
11029051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11030051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				    NL80211_CHAN_HT40MINUS);
11031051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
11032051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		case 1:
11033051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11034051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				    NL80211_CHAN_HT40PLUS);
11035051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
11036051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		default:
11037051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11038051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt				    NL80211_CHAN_HT20);
11039051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt			break;
11040051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt		}
11041051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	}
11042ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
11043ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11044ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	if (ret == 0)
11045ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		return 0;
11046ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11047ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt		   "%d (%s)", ret, strerror(-ret));
11048ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidtnla_put_failure:
11049ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	return -1;
11050ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt}
11051ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt
110521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef CONFIG_TDLS
110531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
110551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  u8 dialog_token, u16 status_code,
110561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  const u8 *buf, size_t len)
110571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
110581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *bss = priv;
110591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
110601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
110611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
110631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -EOPNOTSUPP;
110641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!dst)
110661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -EINVAL;
110671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
110691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
110701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -ENOMEM;
110711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
110731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
110741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
110751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
110761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
110771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
110781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
110791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return send_and_recv_msgs(drv, msg, NULL, NULL);
110811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtnla_put_failure:
110831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
110841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return -ENOBUFS;
110851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
110861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
110891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
110901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct i802_bss *bss = priv;
110911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
110921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct nl_msg *msg;
110931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	enum nl80211_tdls_operation nl80211_oper;
110941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
110961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -EOPNOTSUPP;
110971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
110981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	switch (oper) {
110991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case TDLS_DISCOVERY_REQ:
111001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
111011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
111021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case TDLS_SETUP:
111031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_oper = NL80211_TDLS_SETUP;
111041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
111051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case TDLS_TEARDOWN:
111061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_oper = NL80211_TDLS_TEARDOWN;
111071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
111081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case TDLS_ENABLE_LINK:
111091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_oper = NL80211_TDLS_ENABLE_LINK;
111101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
111111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case TDLS_DISABLE_LINK:
111121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		nl80211_oper = NL80211_TDLS_DISABLE_LINK;
111131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		break;
111141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case TDLS_ENABLE:
111151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return 0;
111161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	case TDLS_DISABLE:
111171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return 0;
111181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	default:
111191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -EINVAL;
111201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
111211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = nlmsg_alloc();
111231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (!msg)
111241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -ENOMEM;
111251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
111271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
111281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
111291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
111301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return send_and_recv_msgs(drv, msg, NULL, NULL);
111321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtnla_put_failure:
111341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	nlmsg_free(msg);
111351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return -ENOBUFS;
111361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
111371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* CONFIG TDLS */
111391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef ANDROID
111421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidttypedef struct android_wifi_priv_cmd {
111441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	char *buf;
111451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int used_len;
111461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int total_len;
111471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt} android_wifi_priv_cmd;
111481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int drv_errors = 0;
111501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
111521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
111531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv_errors++;
111541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
111551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		drv_errors = 0;
111561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
111571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
111581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
111591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int android_priv_cmd(struct i802_bss *bss, const char *cmd)
111621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
111631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
111641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct ifreq ifr;
111651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	android_wifi_priv_cmd priv_cmd;
111661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	char buf[MAX_DRV_CMD_SIZE];
111671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ret;
111681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&ifr, 0, sizeof(ifr));
111701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(&priv_cmd, 0, sizeof(priv_cmd));
111711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
111721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memset(buf, 0, sizeof(buf));
111741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_strlcpy(buf, cmd, sizeof(buf));
111751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	priv_cmd.buf = buf;
111771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	priv_cmd.used_len = sizeof(buf);
111781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	priv_cmd.total_len = sizeof(buf);
111791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ifr.ifr_data = &priv_cmd;
111801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
111821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ret < 0) {
111831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
111841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   __func__);
111851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_driver_send_hang_msg(drv);
111861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return ret;
111871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
111881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv_errors = 0;
111901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return 0;
111911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
111921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
111941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int android_pno_start(struct i802_bss *bss,
111951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			     struct wpa_driver_scan_params *params)
111961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
111971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
111981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct ifreq ifr;
111991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	android_wifi_priv_cmd priv_cmd;
112001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int ret = 0, i = 0, bp;
112011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	char buf[WEXT_PNO_MAX_COMMAND_SIZE];
112021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bp = WEXT_PNOSETUP_HEADER_SIZE;
112041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
112051f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	buf[bp++] = WEXT_PNO_TLV_PREFIX;
112061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	buf[bp++] = WEXT_PNO_TLV_VERSION;
112071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
112081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	buf[bp++] = WEXT_PNO_TLV_RESERVED;
112091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112101f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
112111f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		/* Check that there is enough space needed for 1 more SSID, the
112121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		 * other sections and null termination */
112131f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
112141f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		     WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
112151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			break;
112161f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
112171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  params->ssids[i].ssid,
112181f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				  params->ssids[i].ssid_len);
112191f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		buf[bp++] = WEXT_PNO_SSID_SECTION;
112201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		buf[bp++] = params->ssids[i].ssid_len;
112211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		os_memcpy(&buf[bp], params->ssids[i].ssid,
112221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			  params->ssids[i].ssid_len);
112231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		bp += params->ssids[i].ssid_len;
112241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		i++;
112251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
112261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112271f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
112281f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
112291f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    WEXT_PNO_SCAN_INTERVAL);
112301f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
112311f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	buf[bp++] = WEXT_PNO_REPEAT_SECTION;
112331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
112341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    WEXT_PNO_REPEAT);
112351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bp += WEXT_PNO_REPEAT_LENGTH;
112361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
112381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
112391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    WEXT_PNO_MAX_REPEAT);
112401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
112411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	memset(&ifr, 0, sizeof(ifr));
112431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	memset(&priv_cmd, 0, sizeof(priv_cmd));
1124468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt	os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
112451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	priv_cmd.buf = buf;
112471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	priv_cmd.used_len = bp;
112481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	priv_cmd.total_len = bp;
112491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ifr.ifr_data = &priv_cmd;
112501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
112521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (ret < 0) {
112541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
112551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			   ret);
112561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpa_driver_send_hang_msg(drv);
112571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return ret;
112581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
112591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	drv_errors = 0;
112611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return android_priv_cmd(bss, "PNOFORCE 1");
112631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
112641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic int android_pno_stop(struct i802_bss *bss)
112671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
112681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return android_priv_cmd(bss, "PNOFORCE 0");
112691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
112701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* ANDROID */
112721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
112744b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_set_key(const char *ifname, void *priv,
112754b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				  enum wpa_alg alg, const u8 *addr,
112764b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				  int key_idx, int set_tx,
112774b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				  const u8 *seq, size_t seq_len,
112784b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				  const u8 *key, size_t key_len)
112794b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
112804b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
112814b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
112824b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					  set_tx, seq, seq_len, key, key_len);
112834b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
112844b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
112854b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
112864b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_scan2(void *priv,
112874b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				struct wpa_driver_scan_params *params)
112884b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
112894b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
112904b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return wpa_driver_nl80211_scan(bss, params);
112914b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
112924b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
112934b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
112944b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
112954b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					 int reason_code)
112964b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
112974b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
112984b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
112994b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
113004b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113014b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113024b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_authenticate(void *priv,
113034b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				       struct wpa_driver_auth_params *params)
113044b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
113054b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
113064b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return wpa_driver_nl80211_authenticate(bss, params);
113074b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
113084b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113094b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113104b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic void driver_nl80211_deinit(void *priv)
113114b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
113124b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
113134b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	wpa_driver_nl80211_deinit(bss);
113144b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
113154b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113164b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113174b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
113184b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				    const char *ifname)
113194b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
113204b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
113214b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return wpa_driver_nl80211_if_remove(bss, type, ifname);
113224b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
113234b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113244b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113254b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_send_mlme(void *priv, const u8 *data,
113264b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				    size_t data_len, int noack)
113274b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
113284b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
113294b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
113304b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					    0, 0, 0, 0);
113314b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
113324b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113334b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113344b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_sta_remove(void *priv, const u8 *addr)
113354b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
113364b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
113374b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return wpa_driver_nl80211_sta_remove(bss, addr);
113384b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
113394b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113404b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113414b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
113424b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				       const char *ifname, int vlan_id)
113434b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
113444b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
113454b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
113464b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
113474b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113484b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113494b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_read_sta_data(void *priv,
113504b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					struct hostap_sta_driver_data *data,
113514b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					const u8 *addr)
113524b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
113534b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
113544b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return i802_read_sta_data(bss, data, addr);
113554b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
113564b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113574b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113584b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_send_action(void *priv, unsigned int freq,
113594b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				      unsigned int wait_time,
113604b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				      const u8 *dst, const u8 *src,
113614b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				      const u8 *bssid,
113624b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				      const u8 *data, size_t data_len,
113634b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt				      int no_cck)
113644b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
113654b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
113664b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
113674b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt					      bssid, data, data_len, no_cck);
113684b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
113694b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113704b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113714b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidtstatic int driver_nl80211_probe_req_report(void *priv, int report)
113724b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt{
113734b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	struct i802_bss *bss = priv;
113744b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	return wpa_driver_nl80211_probe_req_report(bss, report);
113754b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt}
113764b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
113774b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt
11378700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidtstatic int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11379700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt					    const u8 *ies, size_t ies_len)
11380700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt{
11381700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	int ret;
11382700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	struct nl_msg *msg;
11383700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	struct i802_bss *bss = priv;
11384700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
11385700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	u16 mdid = WPA_GET_LE16(md);
11386700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
11387700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	msg = nlmsg_alloc();
11388700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	if (!msg)
11389700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		return -ENOMEM;
11390700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
11391700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11392700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11393700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11394700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11395700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11396700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
11397700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11398700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	if (ret) {
11399700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11400700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt			   "err=%d (%s)", ret, strerror(-ret));
11401700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	}
11402700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
11403700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	return ret;
11404700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
11405700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidtnla_put_failure:
11406700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	nlmsg_free(msg);
11407700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	return -ENOBUFS;
11408700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt}
11409700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
11410700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt
1141134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidtconst u8 * wpa_driver_nl80211_get_macaddr(void *priv)
1141234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt{
1141334af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct i802_bss *bss = priv;
1141434af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
1141534af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1141634af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
1141734af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt		return NULL;
1141834af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1141934af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	return bss->addr;
1142034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt}
1142134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
1142234af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt
114235605286c30e1701491bd3af974ae423727750eddDmitry Shmidtstatic const char * scan_state_str(enum scan_states scan_state)
114245605286c30e1701491bd3af974ae423727750eddDmitry Shmidt{
114255605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	switch (scan_state) {
114265605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	case NO_SCAN:
114275605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		return "NO_SCAN";
114285605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	case SCAN_REQUESTED:
114295605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		return "SCAN_REQUESTED";
114305605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	case SCAN_STARTED:
114315605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		return "SCAN_STARTED";
114325605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	case SCAN_COMPLETED:
114335605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		return "SCAN_COMPLETED";
114345605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	case SCAN_ABORTED:
114355605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		return "SCAN_ABORTED";
114365605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	case SCHED_SCAN_STARTED:
114375605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		return "SCHED_SCAN_STARTED";
114385605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	case SCHED_SCAN_STOPPED:
114395605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		return "SCHED_SCAN_STOPPED";
114405605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	case SCHED_SCAN_RESULTS:
114415605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		return "SCHED_SCAN_RESULTS";
114425605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	}
114435605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
114445605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	return "??";
114455605286c30e1701491bd3af974ae423727750eddDmitry Shmidt}
114465605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
114475605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
114485605286c30e1701491bd3af974ae423727750eddDmitry Shmidtstatic int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
114495605286c30e1701491bd3af974ae423727750eddDmitry Shmidt{
114505605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	struct i802_bss *bss = priv;
114515605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
114525605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	int res;
114535605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	char *pos, *end;
114545605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
114555605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	pos = buf;
114565605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	end = buf + buflen;
114575605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
114585605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	res = os_snprintf(pos, end - pos,
114595605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "ifindex=%d\n"
114605605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "ifname=%s\n"
114615605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "brname=%s\n"
114625605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "addr=" MACSTR "\n"
114635605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "freq=%d\n"
114645605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "%s%s%s%s%s",
114655605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  bss->ifindex,
114665605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  bss->ifname,
114675605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  bss->brname,
114685605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  MAC2STR(bss->addr),
114695605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  bss->freq,
114705605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  bss->beacon_set ? "beacon_set=1\n" : "",
114715605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  bss->added_if_into_bridge ?
114725605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "added_if_into_bridge=1\n" : "",
114735605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  bss->added_bridge ? "added_bridge=1\n" : "",
114745605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  bss->in_deinit ? "in_deinit=1\n" : "",
114755605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  bss->if_dynamic ? "if_dynamic=1\n" : "");
114765605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	if (res < 0 || res >= end - pos)
114775605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		return pos - buf;
114785605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	pos += res;
114795605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
114805605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	if (bss->wdev_id_set) {
114815605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
114825605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  (unsigned long long) bss->wdev_id);
114835605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		if (res < 0 || res >= end - pos)
114845605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			return pos - buf;
114855605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		pos += res;
114865605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	}
114875605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
114885605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	res = os_snprintf(pos, end - pos,
114895605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "phyname=%s\n"
114905605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "drv_ifindex=%d\n"
114915605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "operstate=%d\n"
114925605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "scan_state=%s\n"
114935605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "auth_bssid=" MACSTR "\n"
114945605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "auth_attempt_bssid=" MACSTR "\n"
114955605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "bssid=" MACSTR "\n"
114965605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "prev_bssid=" MACSTR "\n"
114975605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "associated=%d\n"
114985605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "assoc_freq=%u\n"
114995605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "monitor_sock=%d\n"
115005605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "monitor_ifidx=%d\n"
115015605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "monitor_refcount=%d\n"
115025605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "last_mgmt_freq=%u\n"
115035605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "eapol_tx_sock=%d\n"
115045605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "%s%s%s%s%s%s%s%s%s%s%s%s%s",
115055605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->phyname,
115065605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->ifindex,
115075605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->operstate,
115085605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  scan_state_str(drv->scan_state),
115095605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  MAC2STR(drv->auth_bssid),
115105605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  MAC2STR(drv->auth_attempt_bssid),
115115605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  MAC2STR(drv->bssid),
115125605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  MAC2STR(drv->prev_bssid),
115135605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->associated,
115145605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->assoc_freq,
115155605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->monitor_sock,
115165605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->monitor_ifidx,
115175605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->monitor_refcount,
115185605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->last_mgmt_freq,
115195605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->eapol_tx_sock,
115205605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->ignore_if_down_event ?
115215605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "ignore_if_down_event=1\n" : "",
115225605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->scan_complete_events ?
115235605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "scan_complete_events=1\n" : "",
115245605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->disabled_11b_rates ?
115255605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "disabled_11b_rates=1\n" : "",
115265605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->pending_remain_on_chan ?
115275605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "pending_remain_on_chan=1\n" : "",
115285605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->in_interface_list ? "in_interface_list=1\n" : "",
115295605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->device_ap_sme ? "device_ap_sme=1\n" : "",
115305605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->poll_command_supported ?
115315605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "poll_command_supported=1\n" : "",
115325605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->data_tx_status ? "data_tx_status=1\n" : "",
115335605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->scan_for_auth ? "scan_for_auth=1\n" : "",
115345605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->retry_auth ? "retry_auth=1\n" : "",
115355605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->use_monitor ? "use_monitor=1\n" : "",
115365605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->ignore_next_local_disconnect ?
115375605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  "ignore_next_local_disconnect=1\n" : "",
115385605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			  drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
115395605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	if (res < 0 || res >= end - pos)
115405605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		return pos - buf;
115415605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	pos += res;
115425605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
115435605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	if (drv->has_capability) {
115445605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		res = os_snprintf(pos, end - pos,
115455605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.key_mgmt=0x%x\n"
115465605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.enc=0x%x\n"
115475605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.auth=0x%x\n"
115485605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.flags=0x%x\n"
115495605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.max_scan_ssids=%d\n"
115505605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.max_sched_scan_ssids=%d\n"
115515605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.sched_scan_supported=%d\n"
115525605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.max_match_sets=%d\n"
115535605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.max_remain_on_chan=%u\n"
115545605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.max_stations=%u\n"
115555605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.probe_resp_offloads=0x%x\n"
115565605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.max_acl_mac_addrs=%u\n"
115575605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  "capa.num_multichan_concurrent=%u\n",
115585605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.key_mgmt,
115595605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.enc,
115605605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.auth,
115615605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.flags,
115625605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.max_scan_ssids,
115635605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.max_sched_scan_ssids,
115645605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.sched_scan_supported,
115655605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.max_match_sets,
115665605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.max_remain_on_chan,
115675605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.max_stations,
115685605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.probe_resp_offloads,
115695605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.max_acl_mac_addrs,
115705605286c30e1701491bd3af974ae423727750eddDmitry Shmidt				  drv->capa.num_multichan_concurrent);
115715605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		if (res < 0 || res >= end - pos)
115725605286c30e1701491bd3af974ae423727750eddDmitry Shmidt			return pos - buf;
115735605286c30e1701491bd3af974ae423727750eddDmitry Shmidt		pos += res;
115745605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	}
115755605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
115765605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	return pos - buf;
115775605286c30e1701491bd3af974ae423727750eddDmitry Shmidt}
115785605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
115795605286c30e1701491bd3af974ae423727750eddDmitry Shmidt
11580e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtstatic int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11581e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt{
11582e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (settings->head)
11583e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
11584e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			settings->head_len, settings->head);
11585e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11586e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (settings->tail)
11587e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
11588e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			settings->tail_len, settings->tail);
11589e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11590e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (settings->beacon_ies)
11591e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_IE,
11592e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			settings->beacon_ies_len, settings->beacon_ies);
11593e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11594e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (settings->proberesp_ies)
11595e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
11596e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			settings->proberesp_ies_len, settings->proberesp_ies);
11597e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11598e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (settings->assocresp_ies)
11599e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		NLA_PUT(msg,
11600e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			NL80211_ATTR_IE_ASSOC_RESP,
11601e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			settings->assocresp_ies_len, settings->assocresp_ies);
11602e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11603e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (settings->probe_resp)
11604e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
11605e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			settings->probe_resp_len, settings->probe_resp);
11606e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11607e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	return 0;
11608e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11609e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtnla_put_failure:
11610e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	return -ENOBUFS;
11611e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt}
11612e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11613e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11614e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtstatic int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11615e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt{
11616e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	struct nl_msg *msg;
11617e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	struct i802_bss *bss = priv;
11618e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
11619e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	struct nlattr *beacon_csa;
11620e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	int ret = -ENOBUFS;
11621e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
1162204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
11623e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		   settings->cs_count, settings->block_tx,
1162404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		   settings->freq_params.freq, settings->freq_params.bandwidth,
1162504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		   settings->freq_params.center_freq1,
1162604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt		   settings->freq_params.center_freq2);
11627e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11628fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
11629e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11630e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		return -EOPNOTSUPP;
11631e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	}
11632e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11633e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if ((drv->nlmode != NL80211_IFTYPE_AP) &&
11634e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	    (drv->nlmode != NL80211_IFTYPE_P2P_GO))
11635e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		return -EOPNOTSUPP;
11636e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11637e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	/* check settings validity */
11638e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (!settings->beacon_csa.tail ||
11639e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	    ((settings->beacon_csa.tail_len <=
11640e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	      settings->counter_offset_beacon) ||
11641e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	     (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
11642e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	      settings->cs_count)))
11643e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		return -EINVAL;
11644e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11645e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (settings->beacon_csa.probe_resp &&
11646e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	    ((settings->beacon_csa.probe_resp_len <=
11647e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	      settings->counter_offset_presp) ||
11648e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	     (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
11649e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	      settings->cs_count)))
11650e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		return -EINVAL;
11651e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11652e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	msg = nlmsg_alloc();
11653e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (!msg)
11654e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		return -ENOMEM;
11655e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11656e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
11657e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11658e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
11659e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	ret = nl80211_put_freq_params(msg, &settings->freq_params);
11660e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (ret)
11661e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		goto error;
11662e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11663e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (settings->block_tx)
11664e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
11665e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11666e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	/* beacon_after params */
11667e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	ret = set_beacon_data(msg, &settings->beacon_after);
11668e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (ret)
11669e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		goto error;
11670e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11671e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	/* beacon_csa params */
11672e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
11673e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (!beacon_csa)
11674e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		goto nla_put_failure;
11675e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11676e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	ret = set_beacon_data(msg, &settings->beacon_csa);
11677e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (ret)
11678e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		goto error;
11679e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11680e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
11681e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		    settings->counter_offset_beacon);
11682e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11683e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (settings->beacon_csa.probe_resp)
11684e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
11685e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			    settings->counter_offset_presp);
11686e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11687e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	nla_nest_end(msg, beacon_csa);
11688e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11689e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	if (ret) {
11690e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
11691e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt			   ret, strerror(-ret));
11692e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	}
11693e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	return ret;
11694e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11695e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidtnla_put_failure:
11696e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	ret = -ENOBUFS;
11697e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidterror:
11698e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	nlmsg_free(msg);
11699e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
11700e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	return ret;
11701e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt}
11702e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11703e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt
11704fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtstatic int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
11705fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt			       u8 qos_map_set_len)
11706fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt{
11707fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct i802_bss *bss = priv;
11708fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct wpa_driver_nl80211_data *drv = bss->drv;
11709fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct nl_msg *msg;
11710fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	int ret;
11711fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
11712fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	msg = nlmsg_alloc();
11713fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (!msg)
11714fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		return -ENOMEM;
11715fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
11716fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
11717fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		    qos_map_set, qos_map_set_len);
11718fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
11719fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
11720fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11721fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
11722fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
11723fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11724fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	if (ret)
11725fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt		wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
11726fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
11727fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	return ret;
11728fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
11729fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidtnla_put_failure:
11730fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	nlmsg_free(msg);
11731fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	return -ENOBUFS;
11732fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt}
11733fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
11734fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
117358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtconst struct wpa_driver_ops wpa_driver_nl80211_ops = {
117368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.name = "nl80211",
117378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.desc = "Linux nl80211/cfg80211",
117388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.get_bssid = wpa_driver_nl80211_get_bssid,
117398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.get_ssid = wpa_driver_nl80211_get_ssid,
117404b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.set_key = driver_nl80211_set_key,
117414b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.scan2 = driver_nl80211_scan2,
117421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	.sched_scan = wpa_driver_nl80211_sched_scan,
117431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	.stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
117448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.get_scan_results2 = wpa_driver_nl80211_get_scan_results,
117454b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.deauthenticate = driver_nl80211_deauthenticate,
117464b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.authenticate = driver_nl80211_authenticate,
117478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.associate = wpa_driver_nl80211_associate,
117488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.global_init = nl80211_global_init,
117498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.global_deinit = nl80211_global_deinit,
117508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.init2 = wpa_driver_nl80211_init,
117514b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.deinit = driver_nl80211_deinit,
117528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.get_capa = wpa_driver_nl80211_get_capa,
117538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.set_operstate = wpa_driver_nl80211_set_operstate,
117548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.set_supp_port = wpa_driver_nl80211_set_supp_port,
117558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.set_country = wpa_driver_nl80211_set_country,
11756cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt	.get_country = wpa_driver_nl80211_get_country,
117571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	.set_ap = wpa_driver_nl80211_set_ap,
117588bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	.set_acl = wpa_driver_nl80211_set_acl,
117598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.if_add = wpa_driver_nl80211_if_add,
117604b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.if_remove = driver_nl80211_if_remove,
117614b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.send_mlme = driver_nl80211_send_mlme,
117628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
117638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.sta_add = wpa_driver_nl80211_sta_add,
117644b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.sta_remove = driver_nl80211_sta_remove,
117658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
117668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.sta_set_flags = wpa_driver_nl80211_sta_set_flags,
117678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.hapd_init = i802_init,
117688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.hapd_deinit = i802_deinit,
1176975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	.set_wds_sta = i802_set_wds_sta,
117708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.get_seqnum = i802_get_seqnum,
117718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.flush = i802_flush,
117728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.get_inact_sec = i802_get_inact_sec,
117738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.sta_clear_stats = i802_sta_clear_stats,
117748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.set_rts = i802_set_rts,
117758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.set_frag = i802_set_frag,
117768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.set_tx_queue_params = i802_set_tx_queue_params,
117774b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.set_sta_vlan = driver_nl80211_set_sta_vlan,
117788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.sta_deauth = i802_sta_deauth,
117798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.sta_disassoc = i802_sta_disassoc,
117804b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.read_sta_data = driver_nl80211_read_sta_data,
117818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.set_freq = i802_set_freq,
117824b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.send_action = driver_nl80211_send_action,
117838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
117848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.remain_on_channel = wpa_driver_nl80211_remain_on_channel,
117858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.cancel_remain_on_channel =
117868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_driver_nl80211_cancel_remain_on_channel,
117874b9d52f502481b258fec743c03a5e957e5605afcDmitry Shmidt	.probe_req_report = driver_nl80211_probe_req_report,
117888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.deinit_ap = wpa_driver_nl80211_deinit_ap,
1178904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	.deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
117908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.resume = wpa_driver_nl80211_resume,
117918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.send_ft_action = nl80211_send_ft_action,
117928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.signal_monitor = nl80211_signal_monitor,
117938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.signal_poll = nl80211_signal_poll,
117948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.send_frame = nl80211_send_frame,
117951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	.shared_freq = wpa_driver_nl80211_shared_freq,
117968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.set_param = nl80211_set_param,
117978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	.get_radio_name = nl80211_get_radio_name,
1179875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	.add_pmkid = nl80211_add_pmkid,
1179975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	.remove_pmkid = nl80211_remove_pmkid,
1180075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	.flush_pmkid = nl80211_flush_pmkid,
118011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	.set_rekey_info = nl80211_set_rekey_info,
118021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	.poll_client = nl80211_poll_client,
118031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	.set_p2p_powersave = nl80211_set_p2p_powersave,
11804ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	.start_dfs_cac = nl80211_start_radar_detection,
11805ea69e84a6f4455c59348485895d3d5e3af77a65bDmitry Shmidt	.stop_ap = wpa_driver_nl80211_stop_ap,
118061f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef CONFIG_TDLS
118071f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	.send_tdls_mgmt = nl80211_send_tdls_mgmt,
118081f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	.tdls_oper = nl80211_tdls_oper,
118091f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* CONFIG_TDLS */
11810700a137ab366edc72e371da68ba187b4717ee660Dmitry Shmidt	.update_ft_ies = wpa_driver_nl80211_update_ft_ies,
1181134af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt	.get_mac_addr = wpa_driver_nl80211_get_macaddr,
11812b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	.get_survey = wpa_driver_nl80211_get_survey,
118135605286c30e1701491bd3af974ae423727750eddDmitry Shmidt	.status = wpa_driver_nl80211_status,
11814e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	.switch_channel = nl80211_switch_channel,
118151f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef ANDROID_P2P
118166e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidt	.set_noa = wpa_driver_set_p2p_noa,
118171f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	.get_noa = wpa_driver_get_p2p_noa,
118186e933c1e09094a8972ef1e782c57f8b3c55c91d0Dmitry Shmidt	.set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
11819292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidt#endif /* ANDROID_P2P */
11820738a26e9e2087b0d43eba3534535632b27b49947Dmitry Shmidt#ifdef ANDROID
11821738a26e9e2087b0d43eba3534535632b27b49947Dmitry Shmidt	.driver_cmd = wpa_driver_nl80211_driver_cmd,
11822292b0c3a742226c295f8db76eaef9e90c90e7513Dmitry Shmidt#endif /* ANDROID */
11823fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	.set_qos_map = nl80211_set_qos_map,
118248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
11825