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