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