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