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