driver_nl80211.c revision f7e0a9905988e62e4f70fed8b795722abeab719b
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_AP_VLAN:
6302		return "AP_VLAN";
6303	case NL80211_IFTYPE_WDS:
6304		return "WDS";
6305	case NL80211_IFTYPE_MONITOR:
6306		return "MONITOR";
6307	case NL80211_IFTYPE_MESH_POINT:
6308		return "MESH_POINT";
6309	case NL80211_IFTYPE_P2P_CLIENT:
6310		return "P2P_CLIENT";
6311	case NL80211_IFTYPE_P2P_GO:
6312		return "P2P_GO";
6313	default:
6314		return "unknown";
6315	}
6316}
6317
6318
6319static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6320				     const char *ifname,
6321				     enum nl80211_iftype iftype,
6322				     const u8 *addr, int wds)
6323{
6324	struct nl_msg *msg;
6325	int ifidx;
6326	int ret = -ENOBUFS;
6327
6328	wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6329		   iftype, nl80211_iftype_str(iftype));
6330
6331	msg = nlmsg_alloc();
6332	if (!msg)
6333		return -1;
6334
6335	nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
6336	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6337	NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
6338	NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
6339
6340	if (iftype == NL80211_IFTYPE_MONITOR) {
6341		struct nlattr *flags;
6342
6343		flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
6344		if (!flags)
6345			goto nla_put_failure;
6346
6347		NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
6348
6349		nla_nest_end(msg, flags);
6350	} else if (wds) {
6351		NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
6352	}
6353
6354	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6355	msg = NULL;
6356	if (ret) {
6357 nla_put_failure:
6358		nlmsg_free(msg);
6359		wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6360			   ifname, ret, strerror(-ret));
6361		return ret;
6362	}
6363
6364	ifidx = if_nametoindex(ifname);
6365	wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6366		   ifname, ifidx);
6367
6368	if (ifidx <= 0)
6369		return -1;
6370
6371	/* start listening for EAPOL on this interface */
6372	add_ifidx(drv, ifidx);
6373
6374	if (addr && iftype != NL80211_IFTYPE_MONITOR &&
6375	    linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
6376		nl80211_remove_iface(drv, ifidx);
6377		return -1;
6378	}
6379
6380	return ifidx;
6381}
6382
6383
6384static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6385				const char *ifname, enum nl80211_iftype iftype,
6386				const u8 *addr, int wds)
6387{
6388	int ret;
6389
6390	ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
6391
6392	/* if error occurred and interface exists already */
6393	if (ret == -ENFILE && if_nametoindex(ifname)) {
6394		wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6395
6396		/* Try to remove the interface that was already there. */
6397		nl80211_remove_iface(drv, if_nametoindex(ifname));
6398
6399		/* Try to create the interface again */
6400		ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
6401						wds);
6402	}
6403
6404	if (ret >= 0 && is_p2p_interface(iftype))
6405		nl80211_disable_11b_rates(drv, ret, 1);
6406
6407	return ret;
6408}
6409
6410
6411static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
6412{
6413	struct ieee80211_hdr *hdr;
6414	u16 fc;
6415	union wpa_event_data event;
6416
6417	hdr = (struct ieee80211_hdr *) buf;
6418	fc = le_to_host16(hdr->frame_control);
6419
6420	os_memset(&event, 0, sizeof(event));
6421	event.tx_status.type = WLAN_FC_GET_TYPE(fc);
6422	event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
6423	event.tx_status.dst = hdr->addr1;
6424	event.tx_status.data = buf;
6425	event.tx_status.data_len = len;
6426	event.tx_status.ack = ok;
6427	wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
6428}
6429
6430
6431static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
6432			     u8 *buf, size_t len)
6433{
6434	struct ieee80211_hdr *hdr = (void *)buf;
6435	u16 fc;
6436	union wpa_event_data event;
6437
6438	if (len < sizeof(*hdr))
6439		return;
6440
6441	fc = le_to_host16(hdr->frame_control);
6442
6443	os_memset(&event, 0, sizeof(event));
6444	event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
6445	event.rx_from_unknown.addr = hdr->addr2;
6446	event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
6447		(WLAN_FC_FROMDS | WLAN_FC_TODS);
6448	wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
6449}
6450
6451
6452static void handle_frame(struct wpa_driver_nl80211_data *drv,
6453			 u8 *buf, size_t len, int datarate, int ssi_signal)
6454{
6455	struct ieee80211_hdr *hdr;
6456	u16 fc;
6457	union wpa_event_data event;
6458
6459	hdr = (struct ieee80211_hdr *) buf;
6460	fc = le_to_host16(hdr->frame_control);
6461
6462	switch (WLAN_FC_GET_TYPE(fc)) {
6463	case WLAN_FC_TYPE_MGMT:
6464		os_memset(&event, 0, sizeof(event));
6465		event.rx_mgmt.frame = buf;
6466		event.rx_mgmt.frame_len = len;
6467		event.rx_mgmt.datarate = datarate;
6468		event.rx_mgmt.ssi_signal = ssi_signal;
6469		wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
6470		break;
6471	case WLAN_FC_TYPE_CTRL:
6472		/* can only get here with PS-Poll frames */
6473		wpa_printf(MSG_DEBUG, "CTRL");
6474		from_unknown_sta(drv, buf, len);
6475		break;
6476	case WLAN_FC_TYPE_DATA:
6477		from_unknown_sta(drv, buf, len);
6478		break;
6479	}
6480}
6481
6482
6483static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
6484{
6485	struct wpa_driver_nl80211_data *drv = eloop_ctx;
6486	int len;
6487	unsigned char buf[3000];
6488	struct ieee80211_radiotap_iterator iter;
6489	int ret;
6490	int datarate = 0, ssi_signal = 0;
6491	int injected = 0, failed = 0, rxflags = 0;
6492
6493	len = recv(sock, buf, sizeof(buf), 0);
6494	if (len < 0) {
6495		perror("recv");
6496		return;
6497	}
6498
6499	if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
6500		printf("received invalid radiotap frame\n");
6501		return;
6502	}
6503
6504	while (1) {
6505		ret = ieee80211_radiotap_iterator_next(&iter);
6506		if (ret == -ENOENT)
6507			break;
6508		if (ret) {
6509			printf("received invalid radiotap frame (%d)\n", ret);
6510			return;
6511		}
6512		switch (iter.this_arg_index) {
6513		case IEEE80211_RADIOTAP_FLAGS:
6514			if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
6515				len -= 4;
6516			break;
6517		case IEEE80211_RADIOTAP_RX_FLAGS:
6518			rxflags = 1;
6519			break;
6520		case IEEE80211_RADIOTAP_TX_FLAGS:
6521			injected = 1;
6522			failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
6523					IEEE80211_RADIOTAP_F_TX_FAIL;
6524			break;
6525		case IEEE80211_RADIOTAP_DATA_RETRIES:
6526			break;
6527		case IEEE80211_RADIOTAP_CHANNEL:
6528			/* TODO: convert from freq/flags to channel number */
6529			break;
6530		case IEEE80211_RADIOTAP_RATE:
6531			datarate = *iter.this_arg * 5;
6532			break;
6533		case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
6534			ssi_signal = (s8) *iter.this_arg;
6535			break;
6536		}
6537	}
6538
6539	if (rxflags && injected)
6540		return;
6541
6542	if (!injected)
6543		handle_frame(drv, buf + iter.max_length,
6544			     len - iter.max_length, datarate, ssi_signal);
6545	else
6546		handle_tx_callback(drv->ctx, buf + iter.max_length,
6547				   len - iter.max_length, !failed);
6548}
6549
6550
6551/*
6552 * we post-process the filter code later and rewrite
6553 * this to the offset to the last instruction
6554 */
6555#define PASS	0xFF
6556#define FAIL	0xFE
6557
6558static struct sock_filter msock_filter_insns[] = {
6559	/*
6560	 * do a little-endian load of the radiotap length field
6561	 */
6562	/* load lower byte into A */
6563	BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
6564	/* put it into X (== index register) */
6565	BPF_STMT(BPF_MISC| BPF_TAX, 0),
6566	/* load upper byte into A */
6567	BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
6568	/* left-shift it by 8 */
6569	BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
6570	/* or with X */
6571	BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
6572	/* put result into X */
6573	BPF_STMT(BPF_MISC| BPF_TAX, 0),
6574
6575	/*
6576	 * Allow management frames through, this also gives us those
6577	 * management frames that we sent ourselves with status
6578	 */
6579	/* load the lower byte of the IEEE 802.11 frame control field */
6580	BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
6581	/* mask off frame type and version */
6582	BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
6583	/* accept frame if it's both 0, fall through otherwise */
6584	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
6585
6586	/*
6587	 * TODO: add a bit to radiotap RX flags that indicates
6588	 * that the sending station is not associated, then
6589	 * add a filter here that filters on our DA and that flag
6590	 * to allow us to deauth frames to that bad station.
6591	 *
6592	 * For now allow all To DS data frames through.
6593	 */
6594	/* load the IEEE 802.11 frame control field */
6595	BPF_STMT(BPF_LD  | BPF_H | BPF_IND, 0),
6596	/* mask off frame type, version and DS status */
6597	BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
6598	/* accept frame if version 0, type 2 and To DS, fall through otherwise
6599	 */
6600	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
6601
6602#if 0
6603	/*
6604	 * drop non-data frames
6605	 */
6606	/* load the lower byte of the frame control field */
6607	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
6608	/* mask off QoS bit */
6609	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
6610	/* drop non-data frames */
6611	BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
6612#endif
6613	/* load the upper byte of the frame control field */
6614	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 1),
6615	/* mask off toDS/fromDS */
6616	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
6617	/* accept WDS frames */
6618	BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, PASS, 0),
6619
6620	/*
6621	 * add header length to index
6622	 */
6623	/* load the lower byte of the frame control field */
6624	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
6625	/* mask off QoS bit */
6626	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
6627	/* right shift it by 6 to give 0 or 2 */
6628	BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
6629	/* add data frame header length */
6630	BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
6631	/* add index, was start of 802.11 header */
6632	BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
6633	/* move to index, now start of LL header */
6634	BPF_STMT(BPF_MISC | BPF_TAX, 0),
6635
6636	/*
6637	 * Accept empty data frames, we use those for
6638	 * polling activity.
6639	 */
6640	BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
6641	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
6642
6643	/*
6644	 * Accept EAPOL frames
6645	 */
6646	BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
6647	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
6648	BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
6649	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
6650
6651	/* keep these last two statements or change the code below */
6652	/* return 0 == "DROP" */
6653	BPF_STMT(BPF_RET | BPF_K, 0),
6654	/* return ~0 == "keep all" */
6655	BPF_STMT(BPF_RET | BPF_K, ~0),
6656};
6657
6658static struct sock_fprog msock_filter = {
6659	.len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
6660	.filter = msock_filter_insns,
6661};
6662
6663
6664static int add_monitor_filter(int s)
6665{
6666	int idx;
6667
6668	/* rewrite all PASS/FAIL jump offsets */
6669	for (idx = 0; idx < msock_filter.len; idx++) {
6670		struct sock_filter *insn = &msock_filter_insns[idx];
6671
6672		if (BPF_CLASS(insn->code) == BPF_JMP) {
6673			if (insn->code == (BPF_JMP|BPF_JA)) {
6674				if (insn->k == PASS)
6675					insn->k = msock_filter.len - idx - 2;
6676				else if (insn->k == FAIL)
6677					insn->k = msock_filter.len - idx - 3;
6678			}
6679
6680			if (insn->jt == PASS)
6681				insn->jt = msock_filter.len - idx - 2;
6682			else if (insn->jt == FAIL)
6683				insn->jt = msock_filter.len - idx - 3;
6684
6685			if (insn->jf == PASS)
6686				insn->jf = msock_filter.len - idx - 2;
6687			else if (insn->jf == FAIL)
6688				insn->jf = msock_filter.len - idx - 3;
6689		}
6690	}
6691
6692	if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
6693		       &msock_filter, sizeof(msock_filter))) {
6694		perror("SO_ATTACH_FILTER");
6695		return -1;
6696	}
6697
6698	return 0;
6699}
6700
6701
6702static void nl80211_remove_monitor_interface(
6703	struct wpa_driver_nl80211_data *drv)
6704{
6705	drv->monitor_refcount--;
6706	if (drv->monitor_refcount > 0)
6707		return;
6708
6709	if (drv->monitor_ifidx >= 0) {
6710		nl80211_remove_iface(drv, drv->monitor_ifidx);
6711		drv->monitor_ifidx = -1;
6712	}
6713	if (drv->monitor_sock >= 0) {
6714		eloop_unregister_read_sock(drv->monitor_sock);
6715		close(drv->monitor_sock);
6716		drv->monitor_sock = -1;
6717	}
6718}
6719
6720
6721static int
6722nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
6723{
6724	char buf[IFNAMSIZ];
6725	struct sockaddr_ll ll;
6726	int optval;
6727	socklen_t optlen;
6728
6729	if (drv->monitor_ifidx >= 0) {
6730		drv->monitor_refcount++;
6731		return 0;
6732	}
6733
6734	if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
6735		/*
6736		 * P2P interface name is of the format p2p-%s-%d. For monitor
6737		 * interface name corresponding to P2P GO, replace "p2p-" with
6738		 * "mon-" to retain the same interface name length and to
6739		 * indicate that it is a monitor interface.
6740		 */
6741		snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
6742	} else {
6743		/* Non-P2P interface with AP functionality. */
6744		snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
6745	}
6746
6747	buf[IFNAMSIZ - 1] = '\0';
6748
6749	drv->monitor_ifidx =
6750		nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
6751				     0);
6752
6753	if (drv->monitor_ifidx == -EOPNOTSUPP) {
6754		/*
6755		 * This is backward compatibility for a few versions of
6756		 * the kernel only that didn't advertise the right
6757		 * attributes for the only driver that then supported
6758		 * AP mode w/o monitor -- ath6kl.
6759		 */
6760		wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
6761			   "monitor interface type - try to run without it");
6762		drv->device_ap_sme = 1;
6763	}
6764
6765	if (drv->monitor_ifidx < 0)
6766		return -1;
6767
6768	if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
6769		goto error;
6770
6771	memset(&ll, 0, sizeof(ll));
6772	ll.sll_family = AF_PACKET;
6773	ll.sll_ifindex = drv->monitor_ifidx;
6774	drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6775	if (drv->monitor_sock < 0) {
6776		perror("socket[PF_PACKET,SOCK_RAW]");
6777		goto error;
6778	}
6779
6780	if (add_monitor_filter(drv->monitor_sock)) {
6781		wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
6782			   "interface; do filtering in user space");
6783		/* This works, but will cost in performance. */
6784	}
6785
6786	if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
6787		perror("monitor socket bind");
6788		goto error;
6789	}
6790
6791	optlen = sizeof(optval);
6792	optval = 20;
6793	if (setsockopt
6794	    (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
6795		perror("Failed to set socket priority");
6796		goto error;
6797	}
6798
6799	if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
6800				     drv, NULL)) {
6801		printf("Could not register monitor read socket\n");
6802		goto error;
6803	}
6804
6805	return 0;
6806 error:
6807	nl80211_remove_monitor_interface(drv);
6808	return -1;
6809}
6810
6811
6812static int nl80211_setup_ap(struct i802_bss *bss)
6813{
6814	struct wpa_driver_nl80211_data *drv = bss->drv;
6815
6816	wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
6817		   "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
6818
6819	/*
6820	 * Disable Probe Request reporting unless we need it in this way for
6821	 * devices that include the AP SME, in the other case (unless using
6822	 * monitor iface) we'll get it through the nl_mgmt socket instead.
6823	 */
6824	if (!drv->device_ap_sme)
6825		wpa_driver_nl80211_probe_req_report(bss, 0);
6826
6827	if (!drv->device_ap_sme && !drv->use_monitor)
6828		if (nl80211_mgmt_subscribe_ap(bss))
6829			return -1;
6830
6831	if (drv->device_ap_sme && !drv->use_monitor)
6832		if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6833			return -1;
6834
6835	if (!drv->device_ap_sme && drv->use_monitor &&
6836	    nl80211_create_monitor_interface(drv) &&
6837	    !drv->device_ap_sme)
6838		return -1;
6839
6840#ifdef ANDROID_P2P
6841	if (drv->device_ap_sme && drv->use_monitor)
6842		if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6843			return -1;
6844
6845	if (drv->use_monitor &&
6846	    nl80211_create_monitor_interface(drv))
6847		return -1;
6848#endif
6849
6850	if (drv->device_ap_sme &&
6851	    wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6852		wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6853			   "Probe Request frame reporting in AP mode");
6854		/* Try to survive without this */
6855	}
6856
6857	return 0;
6858}
6859
6860
6861static void nl80211_teardown_ap(struct i802_bss *bss)
6862{
6863	struct wpa_driver_nl80211_data *drv = bss->drv;
6864
6865	if (drv->device_ap_sme) {
6866		wpa_driver_nl80211_probe_req_report(bss, 0);
6867		if (!drv->use_monitor)
6868			nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6869	} else if (drv->use_monitor)
6870		nl80211_remove_monitor_interface(drv);
6871	else
6872		nl80211_mgmt_unsubscribe(bss, "AP teardown");
6873
6874	bss->beacon_set = 0;
6875}
6876
6877
6878static int nl80211_send_eapol_data(struct i802_bss *bss,
6879				   const u8 *addr, const u8 *data,
6880				   size_t data_len)
6881{
6882	struct sockaddr_ll ll;
6883	int ret;
6884
6885	if (bss->drv->eapol_tx_sock < 0) {
6886		wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
6887		return -1;
6888	}
6889
6890	os_memset(&ll, 0, sizeof(ll));
6891	ll.sll_family = AF_PACKET;
6892	ll.sll_ifindex = bss->ifindex;
6893	ll.sll_protocol = htons(ETH_P_PAE);
6894	ll.sll_halen = ETH_ALEN;
6895	os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6896	ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6897		     (struct sockaddr *) &ll, sizeof(ll));
6898	if (ret < 0)
6899		wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6900			   strerror(errno));
6901
6902	return ret;
6903}
6904
6905
6906static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6907
6908static int wpa_driver_nl80211_hapd_send_eapol(
6909	void *priv, const u8 *addr, const u8 *data,
6910	size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
6911{
6912	struct i802_bss *bss = priv;
6913	struct wpa_driver_nl80211_data *drv = bss->drv;
6914	struct ieee80211_hdr *hdr;
6915	size_t len;
6916	u8 *pos;
6917	int res;
6918	int qos = flags & WPA_STA_WMM;
6919#ifndef ANDROID_P2P
6920	if (drv->device_ap_sme || !drv->use_monitor)
6921#else
6922	if (drv->device_ap_sme && !drv->use_monitor)
6923#endif
6924		return nl80211_send_eapol_data(bss, addr, data, data_len);
6925
6926	len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6927		data_len;
6928	hdr = os_zalloc(len);
6929	if (hdr == NULL) {
6930		printf("malloc() failed for i802_send_data(len=%lu)\n",
6931		       (unsigned long) len);
6932		return -1;
6933	}
6934
6935	hdr->frame_control =
6936		IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6937	hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6938	if (encrypt)
6939		hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
6940	if (qos) {
6941		hdr->frame_control |=
6942			host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6943	}
6944
6945	memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6946	memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6947	memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6948	pos = (u8 *) (hdr + 1);
6949
6950	if (qos) {
6951		/* Set highest priority in QoS header */
6952		pos[0] = 7;
6953		pos[1] = 0;
6954		pos += 2;
6955	}
6956
6957	memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6958	pos += sizeof(rfc1042_header);
6959	WPA_PUT_BE16(pos, ETH_P_PAE);
6960	pos += 2;
6961	memcpy(pos, data, data_len);
6962
6963	res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
6964					    0, 0, 0, 0);
6965	if (res < 0) {
6966		wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
6967			   "failed: %d (%s)",
6968			   (unsigned long) len, errno, strerror(errno));
6969	}
6970	os_free(hdr);
6971
6972	return res;
6973}
6974
6975
6976static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
6977					    int total_flags,
6978					    int flags_or, int flags_and)
6979{
6980	struct i802_bss *bss = priv;
6981	struct wpa_driver_nl80211_data *drv = bss->drv;
6982	struct nl_msg *msg;
6983	struct nlattr *flags;
6984	struct nl80211_sta_flag_update upd;
6985
6986	msg = nlmsg_alloc();
6987	if (!msg)
6988		return -ENOMEM;
6989
6990	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
6991
6992	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6993		    if_nametoindex(bss->ifname));
6994	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6995
6996	/*
6997	 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6998	 * can be removed eventually.
6999	 */
7000	flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
7001	if (!flags)
7002		goto nla_put_failure;
7003	if (total_flags & WPA_STA_AUTHORIZED)
7004		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
7005
7006	if (total_flags & WPA_STA_WMM)
7007		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
7008
7009	if (total_flags & WPA_STA_SHORT_PREAMBLE)
7010		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
7011
7012	if (total_flags & WPA_STA_MFP)
7013		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
7014
7015	if (total_flags & WPA_STA_TDLS_PEER)
7016		NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
7017
7018	nla_nest_end(msg, flags);
7019
7020	os_memset(&upd, 0, sizeof(upd));
7021	upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
7022	upd.set = sta_flags_nl80211(flags_or);
7023	NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7024
7025	return send_and_recv_msgs(drv, msg, NULL, NULL);
7026 nla_put_failure:
7027	nlmsg_free(msg);
7028	return -ENOBUFS;
7029}
7030
7031
7032static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
7033				 struct wpa_driver_associate_params *params)
7034{
7035	enum nl80211_iftype nlmode, old_mode;
7036	struct hostapd_freq_params freq = {
7037		.freq = params->freq,
7038	};
7039
7040	if (params->p2p) {
7041		wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
7042			   "group (GO)");
7043		nlmode = NL80211_IFTYPE_P2P_GO;
7044	} else
7045		nlmode = NL80211_IFTYPE_AP;
7046
7047	old_mode = drv->nlmode;
7048	if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode)) {
7049		nl80211_remove_monitor_interface(drv);
7050		return -1;
7051	}
7052
7053	if (wpa_driver_nl80211_set_freq(&drv->first_bss, &freq)) {
7054		if (old_mode != nlmode)
7055			wpa_driver_nl80211_set_mode(&drv->first_bss, old_mode);
7056		nl80211_remove_monitor_interface(drv);
7057		return -1;
7058	}
7059
7060	return 0;
7061}
7062
7063
7064static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
7065{
7066	struct nl_msg *msg;
7067	int ret = -1;
7068
7069	msg = nlmsg_alloc();
7070	if (!msg)
7071		return -1;
7072
7073	nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
7074	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7075	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7076	msg = NULL;
7077	if (ret) {
7078		wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
7079			   "(%s)", ret, strerror(-ret));
7080		goto nla_put_failure;
7081	}
7082
7083	ret = 0;
7084	wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
7085
7086nla_put_failure:
7087	nlmsg_free(msg);
7088	return ret;
7089}
7090
7091
7092static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
7093				   struct wpa_driver_associate_params *params)
7094{
7095	struct nl_msg *msg;
7096	int ret = -1;
7097	int count = 0;
7098
7099	wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
7100
7101	if (wpa_driver_nl80211_set_mode(&drv->first_bss,
7102					NL80211_IFTYPE_ADHOC)) {
7103		wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7104			   "IBSS mode");
7105		return -1;
7106	}
7107
7108retry:
7109	msg = nlmsg_alloc();
7110	if (!msg)
7111		return -1;
7112
7113	nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
7114	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7115
7116	if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
7117		goto nla_put_failure;
7118
7119	wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
7120			  params->ssid, params->ssid_len);
7121	NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7122		params->ssid);
7123	os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7124	drv->ssid_len = params->ssid_len;
7125
7126	wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
7127	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7128
7129	ret = nl80211_set_conn_keys(params, msg);
7130	if (ret)
7131		goto nla_put_failure;
7132
7133	if (params->bssid && params->fixed_bssid) {
7134		wpa_printf(MSG_DEBUG, "  * BSSID=" MACSTR,
7135			   MAC2STR(params->bssid));
7136		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7137	}
7138
7139	if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7140	    params->key_mgmt_suite == KEY_MGMT_PSK ||
7141	    params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
7142	    params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
7143		wpa_printf(MSG_DEBUG, "  * control port");
7144		NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7145	}
7146
7147	if (params->wpa_ie) {
7148		wpa_hexdump(MSG_DEBUG,
7149			    "  * Extra IEs for Beacon/Probe Response frames",
7150			    params->wpa_ie, params->wpa_ie_len);
7151		NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7152			params->wpa_ie);
7153	}
7154
7155	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7156	msg = NULL;
7157	if (ret) {
7158		wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
7159			   ret, strerror(-ret));
7160		count++;
7161		if (ret == -EALREADY && count == 1) {
7162			wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
7163				   "forced leave");
7164			nl80211_leave_ibss(drv);
7165			nlmsg_free(msg);
7166			goto retry;
7167		}
7168
7169		goto nla_put_failure;
7170	}
7171	ret = 0;
7172	wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
7173
7174nla_put_failure:
7175	nlmsg_free(msg);
7176	return ret;
7177}
7178
7179
7180static int wpa_driver_nl80211_try_connect(
7181	struct wpa_driver_nl80211_data *drv,
7182	struct wpa_driver_associate_params *params)
7183{
7184	struct nl_msg *msg;
7185	enum nl80211_auth_type type;
7186	int ret = 0;
7187	int algs;
7188
7189	msg = nlmsg_alloc();
7190	if (!msg)
7191		return -1;
7192
7193	wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
7194	nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
7195
7196	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7197	if (params->bssid) {
7198		wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
7199			   MAC2STR(params->bssid));
7200		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7201	}
7202	if (params->freq) {
7203		wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
7204		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7205	}
7206	if (params->bg_scan_period >= 0) {
7207		wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
7208			   params->bg_scan_period);
7209		NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7210			    params->bg_scan_period);
7211	}
7212	if (params->ssid) {
7213		wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
7214				  params->ssid, params->ssid_len);
7215		NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7216			params->ssid);
7217		if (params->ssid_len > sizeof(drv->ssid))
7218			goto nla_put_failure;
7219		os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7220		drv->ssid_len = params->ssid_len;
7221	}
7222	wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
7223	if (params->wpa_ie)
7224		NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7225			params->wpa_ie);
7226
7227	algs = 0;
7228	if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7229		algs++;
7230	if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7231		algs++;
7232	if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7233		algs++;
7234	if (algs > 1) {
7235		wpa_printf(MSG_DEBUG, "  * Leave out Auth Type for automatic "
7236			   "selection");
7237		goto skip_auth_type;
7238	}
7239
7240	if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7241		type = NL80211_AUTHTYPE_OPEN_SYSTEM;
7242	else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7243		type = NL80211_AUTHTYPE_SHARED_KEY;
7244	else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7245		type = NL80211_AUTHTYPE_NETWORK_EAP;
7246	else if (params->auth_alg & WPA_AUTH_ALG_FT)
7247		type = NL80211_AUTHTYPE_FT;
7248	else
7249		goto nla_put_failure;
7250
7251	wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
7252	NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
7253
7254skip_auth_type:
7255	if (params->wpa_proto) {
7256		enum nl80211_wpa_versions ver = 0;
7257
7258		if (params->wpa_proto & WPA_PROTO_WPA)
7259			ver |= NL80211_WPA_VERSION_1;
7260		if (params->wpa_proto & WPA_PROTO_RSN)
7261			ver |= NL80211_WPA_VERSION_2;
7262
7263		wpa_printf(MSG_DEBUG, "  * WPA Versions 0x%x", ver);
7264		NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7265	}
7266
7267	if (params->pairwise_suite != CIPHER_NONE) {
7268		int cipher;
7269
7270		switch (params->pairwise_suite) {
7271		case CIPHER_SMS4:
7272			cipher = WLAN_CIPHER_SUITE_SMS4;
7273			break;
7274		case CIPHER_WEP40:
7275			cipher = WLAN_CIPHER_SUITE_WEP40;
7276			break;
7277		case CIPHER_WEP104:
7278			cipher = WLAN_CIPHER_SUITE_WEP104;
7279			break;
7280		case CIPHER_CCMP:
7281			cipher = WLAN_CIPHER_SUITE_CCMP;
7282			break;
7283		case CIPHER_GCMP:
7284			cipher = WLAN_CIPHER_SUITE_GCMP;
7285			break;
7286		case CIPHER_TKIP:
7287		default:
7288			cipher = WLAN_CIPHER_SUITE_TKIP;
7289			break;
7290		}
7291		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7292	}
7293
7294	if (params->group_suite != CIPHER_NONE) {
7295		int cipher;
7296
7297		switch (params->group_suite) {
7298		case CIPHER_SMS4:
7299			cipher = WLAN_CIPHER_SUITE_SMS4;
7300			break;
7301		case CIPHER_WEP40:
7302			cipher = WLAN_CIPHER_SUITE_WEP40;
7303			break;
7304		case CIPHER_WEP104:
7305			cipher = WLAN_CIPHER_SUITE_WEP104;
7306			break;
7307		case CIPHER_CCMP:
7308			cipher = WLAN_CIPHER_SUITE_CCMP;
7309			break;
7310		case CIPHER_GCMP:
7311			cipher = WLAN_CIPHER_SUITE_GCMP;
7312			break;
7313		case CIPHER_TKIP:
7314		default:
7315			cipher = WLAN_CIPHER_SUITE_TKIP;
7316			break;
7317		}
7318		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7319	}
7320
7321	if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7322	    params->key_mgmt_suite == KEY_MGMT_PSK ||
7323	    params->key_mgmt_suite == KEY_MGMT_FT_802_1X ||
7324	    params->key_mgmt_suite == KEY_MGMT_FT_PSK ||
7325	    params->key_mgmt_suite == KEY_MGMT_CCKM) {
7326		int mgmt = WLAN_AKM_SUITE_PSK;
7327
7328		switch (params->key_mgmt_suite) {
7329		case KEY_MGMT_CCKM:
7330			mgmt = WLAN_AKM_SUITE_CCKM;
7331			break;
7332		case KEY_MGMT_802_1X:
7333			mgmt = WLAN_AKM_SUITE_8021X;
7334			break;
7335		case KEY_MGMT_FT_802_1X:
7336			mgmt = WLAN_AKM_SUITE_FT_8021X;
7337			break;
7338		case KEY_MGMT_FT_PSK:
7339			mgmt = WLAN_AKM_SUITE_FT_PSK;
7340			break;
7341		case KEY_MGMT_PSK:
7342		default:
7343			mgmt = WLAN_AKM_SUITE_PSK;
7344			break;
7345		}
7346		NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
7347	}
7348
7349#ifdef CONFIG_IEEE80211W
7350	if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7351		NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7352#endif /* CONFIG_IEEE80211W */
7353
7354	if (params->disable_ht)
7355		NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7356
7357	if (params->htcaps && params->htcaps_mask) {
7358		int sz = sizeof(struct ieee80211_ht_capabilities);
7359		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7360		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7361			params->htcaps_mask);
7362	}
7363
7364#ifdef CONFIG_VHT_OVERRIDES
7365	if (params->disable_vht) {
7366		wpa_printf(MSG_DEBUG, "  * VHT disabled");
7367		NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7368	}
7369
7370	if (params->vhtcaps && params->vhtcaps_mask) {
7371		int sz = sizeof(struct ieee80211_vht_capabilities);
7372		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7373		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7374			params->vhtcaps_mask);
7375	}
7376#endif /* CONFIG_VHT_OVERRIDES */
7377
7378	ret = nl80211_set_conn_keys(params, msg);
7379	if (ret)
7380		goto nla_put_failure;
7381
7382	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7383	msg = NULL;
7384	if (ret) {
7385		wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7386			   "(%s)", ret, strerror(-ret));
7387		goto nla_put_failure;
7388	}
7389	ret = 0;
7390	wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
7391
7392nla_put_failure:
7393	nlmsg_free(msg);
7394	return ret;
7395
7396}
7397
7398
7399static int wpa_driver_nl80211_connect(
7400	struct wpa_driver_nl80211_data *drv,
7401	struct wpa_driver_associate_params *params)
7402{
7403	int ret = wpa_driver_nl80211_try_connect(drv, params);
7404	if (ret == -EALREADY) {
7405		/*
7406		 * cfg80211 does not currently accept new connections if
7407		 * we are already connected. As a workaround, force
7408		 * disconnection and try again.
7409		 */
7410		wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7411			   "disconnecting before reassociation "
7412			   "attempt");
7413		if (wpa_driver_nl80211_disconnect(
7414			    drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
7415			return -1;
7416		/* Ignore the next local disconnect message. */
7417		drv->ignore_next_local_disconnect = 1;
7418		ret = wpa_driver_nl80211_try_connect(drv, params);
7419	}
7420	return ret;
7421}
7422
7423
7424static int wpa_driver_nl80211_associate(
7425	void *priv, struct wpa_driver_associate_params *params)
7426{
7427	struct i802_bss *bss = priv;
7428	struct wpa_driver_nl80211_data *drv = bss->drv;
7429	int ret = -1;
7430	struct nl_msg *msg;
7431
7432	if (params->mode == IEEE80211_MODE_AP)
7433		return wpa_driver_nl80211_ap(drv, params);
7434
7435	if (params->mode == IEEE80211_MODE_IBSS)
7436		return wpa_driver_nl80211_ibss(drv, params);
7437
7438	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
7439		enum nl80211_iftype nlmode = params->p2p ?
7440			NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
7441
7442		if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
7443			return -1;
7444		return wpa_driver_nl80211_connect(drv, params);
7445	}
7446
7447	drv->associated = 0;
7448
7449	msg = nlmsg_alloc();
7450	if (!msg)
7451		return -1;
7452
7453	wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
7454		   drv->ifindex);
7455	nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
7456
7457	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7458	if (params->bssid) {
7459		wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
7460			   MAC2STR(params->bssid));
7461		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7462	}
7463	if (params->freq) {
7464		wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
7465		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7466		drv->assoc_freq = params->freq;
7467	} else
7468		drv->assoc_freq = 0;
7469	if (params->bg_scan_period >= 0) {
7470		wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
7471			   params->bg_scan_period);
7472		NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7473			    params->bg_scan_period);
7474	}
7475	if (params->ssid) {
7476		wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
7477				  params->ssid, params->ssid_len);
7478		NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7479			params->ssid);
7480		if (params->ssid_len > sizeof(drv->ssid))
7481			goto nla_put_failure;
7482		os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7483		drv->ssid_len = params->ssid_len;
7484	}
7485	wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
7486	if (params->wpa_ie)
7487		NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7488			params->wpa_ie);
7489
7490	if (params->pairwise_suite != CIPHER_NONE) {
7491		int cipher;
7492
7493		switch (params->pairwise_suite) {
7494		case CIPHER_WEP40:
7495			cipher = WLAN_CIPHER_SUITE_WEP40;
7496			break;
7497		case CIPHER_WEP104:
7498			cipher = WLAN_CIPHER_SUITE_WEP104;
7499			break;
7500		case CIPHER_CCMP:
7501			cipher = WLAN_CIPHER_SUITE_CCMP;
7502			break;
7503		case CIPHER_GCMP:
7504			cipher = WLAN_CIPHER_SUITE_GCMP;
7505			break;
7506		case CIPHER_TKIP:
7507		default:
7508			cipher = WLAN_CIPHER_SUITE_TKIP;
7509			break;
7510		}
7511		wpa_printf(MSG_DEBUG, "  * pairwise=0x%x", cipher);
7512		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7513	}
7514
7515	if (params->group_suite != CIPHER_NONE) {
7516		int cipher;
7517
7518		switch (params->group_suite) {
7519		case CIPHER_WEP40:
7520			cipher = WLAN_CIPHER_SUITE_WEP40;
7521			break;
7522		case CIPHER_WEP104:
7523			cipher = WLAN_CIPHER_SUITE_WEP104;
7524			break;
7525		case CIPHER_CCMP:
7526			cipher = WLAN_CIPHER_SUITE_CCMP;
7527			break;
7528		case CIPHER_GCMP:
7529			cipher = WLAN_CIPHER_SUITE_GCMP;
7530			break;
7531		case CIPHER_TKIP:
7532		default:
7533			cipher = WLAN_CIPHER_SUITE_TKIP;
7534			break;
7535		}
7536		wpa_printf(MSG_DEBUG, "  * group=0x%x", cipher);
7537		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7538	}
7539
7540#ifdef CONFIG_IEEE80211W
7541	if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7542		NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7543#endif /* CONFIG_IEEE80211W */
7544
7545	NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7546
7547	if (params->prev_bssid) {
7548		wpa_printf(MSG_DEBUG, "  * prev_bssid=" MACSTR,
7549			   MAC2STR(params->prev_bssid));
7550		NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
7551			params->prev_bssid);
7552	}
7553
7554	if (params->disable_ht)
7555		NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7556
7557	if (params->htcaps && params->htcaps_mask) {
7558		int sz = sizeof(struct ieee80211_ht_capabilities);
7559		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7560		NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7561			params->htcaps_mask);
7562	}
7563
7564#ifdef CONFIG_VHT_OVERRIDES
7565	if (params->disable_vht) {
7566		wpa_printf(MSG_DEBUG, "  * VHT disabled");
7567		NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7568	}
7569
7570	if (params->vhtcaps && params->vhtcaps_mask) {
7571		int sz = sizeof(struct ieee80211_vht_capabilities);
7572		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7573		NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7574			params->vhtcaps_mask);
7575	}
7576#endif /* CONFIG_VHT_OVERRIDES */
7577
7578	if (params->p2p)
7579		wpa_printf(MSG_DEBUG, "  * P2P group");
7580
7581	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7582	msg = NULL;
7583	if (ret) {
7584		wpa_dbg(drv->ctx, MSG_DEBUG,
7585			"nl80211: MLME command failed (assoc): ret=%d (%s)",
7586			ret, strerror(-ret));
7587		nl80211_dump_scan(drv);
7588		goto nla_put_failure;
7589	}
7590	ret = 0;
7591	wpa_printf(MSG_DEBUG, "nl80211: Association request send "
7592		   "successfully");
7593
7594nla_put_failure:
7595	nlmsg_free(msg);
7596	return ret;
7597}
7598
7599
7600static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
7601			    int ifindex, enum nl80211_iftype mode)
7602{
7603	struct nl_msg *msg;
7604	int ret = -ENOBUFS;
7605
7606	wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
7607		   ifindex, mode, nl80211_iftype_str(mode));
7608
7609	msg = nlmsg_alloc();
7610	if (!msg)
7611		return -ENOMEM;
7612
7613	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
7614	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
7615	NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
7616
7617	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7618	msg = NULL;
7619	if (!ret)
7620		return 0;
7621nla_put_failure:
7622	nlmsg_free(msg);
7623	wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7624		   " %d (%s)", ifindex, mode, ret, strerror(-ret));
7625	return ret;
7626}
7627
7628
7629static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7630				       enum nl80211_iftype nlmode)
7631{
7632	struct wpa_driver_nl80211_data *drv = bss->drv;
7633	int ret = -1;
7634	int i;
7635	int was_ap = is_ap_interface(drv->nlmode);
7636	int res;
7637
7638	res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7639	if (res == 0) {
7640		drv->nlmode = nlmode;
7641		ret = 0;
7642		goto done;
7643	}
7644
7645	if (res == -ENODEV)
7646		return -1;
7647
7648	if (nlmode == drv->nlmode) {
7649		wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7650			   "requested mode - ignore error");
7651		ret = 0;
7652		goto done; /* Already in the requested mode */
7653	}
7654
7655	/* mac80211 doesn't allow mode changes while the device is up, so
7656	 * take the device down, try to set the mode again, and bring the
7657	 * device back up.
7658	 */
7659	wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7660		   "interface down");
7661	for (i = 0; i < 10; i++) {
7662		res = linux_set_iface_flags(drv->global->ioctl_sock,
7663					    bss->ifname, 0);
7664		if (res == -EACCES || res == -ENODEV)
7665			break;
7666		if (res == 0) {
7667			/* Try to set the mode again while the interface is
7668			 * down */
7669			ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
7670			if (ret == -EACCES)
7671				break;
7672			res = linux_set_iface_flags(drv->global->ioctl_sock,
7673						    bss->ifname, 1);
7674			if (res && !ret)
7675				ret = -1;
7676			else if (ret != -EBUSY)
7677				break;
7678		} else
7679			wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7680				   "interface down");
7681		os_sleep(0, 100000);
7682	}
7683
7684	if (!ret) {
7685		wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7686			   "interface is down");
7687		drv->nlmode = nlmode;
7688		drv->ignore_if_down_event = 1;
7689	}
7690
7691done:
7692	if (ret) {
7693		wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7694			   "from %d failed", nlmode, drv->nlmode);
7695		return ret;
7696	}
7697
7698	if (is_p2p_interface(nlmode))
7699		nl80211_disable_11b_rates(drv, drv->ifindex, 1);
7700	else if (drv->disabled_11b_rates)
7701		nl80211_disable_11b_rates(drv, drv->ifindex, 0);
7702
7703	if (is_ap_interface(nlmode)) {
7704		nl80211_mgmt_unsubscribe(bss, "start AP");
7705		/* Setup additional AP mode functionality if needed */
7706		if (nl80211_setup_ap(bss))
7707			return -1;
7708	} else if (was_ap) {
7709		/* Remove additional AP mode functionality */
7710		nl80211_teardown_ap(bss);
7711	} else {
7712		nl80211_mgmt_unsubscribe(bss, "mode change");
7713	}
7714
7715	if (!bss->in_deinit && !is_ap_interface(nlmode) &&
7716	    nl80211_mgmt_subscribe_non_ap(bss) < 0)
7717		wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7718			   "frame processing - ignore for now");
7719
7720	return 0;
7721}
7722
7723
7724static int wpa_driver_nl80211_get_capa(void *priv,
7725				       struct wpa_driver_capa *capa)
7726{
7727	struct i802_bss *bss = priv;
7728	struct wpa_driver_nl80211_data *drv = bss->drv;
7729	if (!drv->has_capability)
7730		return -1;
7731	os_memcpy(capa, &drv->capa, sizeof(*capa));
7732	if (drv->extended_capa && drv->extended_capa_mask) {
7733		capa->extended_capa = drv->extended_capa;
7734		capa->extended_capa_mask = drv->extended_capa_mask;
7735		capa->extended_capa_len = drv->extended_capa_len;
7736	}
7737	return 0;
7738}
7739
7740
7741static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7742{
7743	struct i802_bss *bss = priv;
7744	struct wpa_driver_nl80211_data *drv = bss->drv;
7745
7746	wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
7747		   __func__, drv->operstate, state, state ? "UP" : "DORMANT");
7748	drv->operstate = state;
7749	return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
7750				      state ? IF_OPER_UP : IF_OPER_DORMANT);
7751}
7752
7753
7754static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7755{
7756	struct i802_bss *bss = priv;
7757	struct wpa_driver_nl80211_data *drv = bss->drv;
7758	struct nl_msg *msg;
7759	struct nl80211_sta_flag_update upd;
7760
7761	msg = nlmsg_alloc();
7762	if (!msg)
7763		return -ENOMEM;
7764
7765	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
7766
7767	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7768		    if_nametoindex(bss->ifname));
7769	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
7770
7771	os_memset(&upd, 0, sizeof(upd));
7772	upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7773	if (authorized)
7774		upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
7775	NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7776
7777	return send_and_recv_msgs(drv, msg, NULL, NULL);
7778 nla_put_failure:
7779	nlmsg_free(msg);
7780	return -ENOBUFS;
7781}
7782
7783
7784/* Set kernel driver on given frequency (MHz) */
7785static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
7786{
7787	struct i802_bss *bss = priv;
7788	return wpa_driver_nl80211_set_freq(bss, freq);
7789}
7790
7791
7792#if defined(HOSTAPD) || defined(CONFIG_AP)
7793
7794static inline int min_int(int a, int b)
7795{
7796	if (a < b)
7797		return a;
7798	return b;
7799}
7800
7801
7802static int get_key_handler(struct nl_msg *msg, void *arg)
7803{
7804	struct nlattr *tb[NL80211_ATTR_MAX + 1];
7805	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7806
7807	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7808		  genlmsg_attrlen(gnlh, 0), NULL);
7809
7810	/*
7811	 * TODO: validate the key index and mac address!
7812	 * Otherwise, there's a race condition as soon as
7813	 * the kernel starts sending key notifications.
7814	 */
7815
7816	if (tb[NL80211_ATTR_KEY_SEQ])
7817		memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7818		       min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
7819	return NL_SKIP;
7820}
7821
7822
7823static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
7824			   int idx, u8 *seq)
7825{
7826	struct i802_bss *bss = priv;
7827	struct wpa_driver_nl80211_data *drv = bss->drv;
7828	struct nl_msg *msg;
7829
7830	msg = nlmsg_alloc();
7831	if (!msg)
7832		return -ENOMEM;
7833
7834	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
7835
7836	if (addr)
7837		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7838	NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
7839	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
7840
7841	memset(seq, 0, 6);
7842
7843	return send_and_recv_msgs(drv, msg, get_key_handler, seq);
7844 nla_put_failure:
7845	nlmsg_free(msg);
7846	return -ENOBUFS;
7847}
7848
7849
7850static int i802_set_rts(void *priv, int rts)
7851{
7852	struct i802_bss *bss = priv;
7853	struct wpa_driver_nl80211_data *drv = bss->drv;
7854	struct nl_msg *msg;
7855	int ret = -ENOBUFS;
7856	u32 val;
7857
7858	msg = nlmsg_alloc();
7859	if (!msg)
7860		return -ENOMEM;
7861
7862	if (rts >= 2347)
7863		val = (u32) -1;
7864	else
7865		val = rts;
7866
7867	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7868	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7869	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
7870
7871	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7872	msg = NULL;
7873	if (!ret)
7874		return 0;
7875nla_put_failure:
7876	nlmsg_free(msg);
7877	wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7878		   "%d (%s)", rts, ret, strerror(-ret));
7879	return ret;
7880}
7881
7882
7883static int i802_set_frag(void *priv, int frag)
7884{
7885	struct i802_bss *bss = priv;
7886	struct wpa_driver_nl80211_data *drv = bss->drv;
7887	struct nl_msg *msg;
7888	int ret = -ENOBUFS;
7889	u32 val;
7890
7891	msg = nlmsg_alloc();
7892	if (!msg)
7893		return -ENOMEM;
7894
7895	if (frag >= 2346)
7896		val = (u32) -1;
7897	else
7898		val = frag;
7899
7900	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7901	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7902	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
7903
7904	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7905	msg = NULL;
7906	if (!ret)
7907		return 0;
7908nla_put_failure:
7909	nlmsg_free(msg);
7910	wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7911		   "%d: %d (%s)", frag, ret, strerror(-ret));
7912	return ret;
7913}
7914
7915
7916static int i802_flush(void *priv)
7917{
7918	struct i802_bss *bss = priv;
7919	struct wpa_driver_nl80211_data *drv = bss->drv;
7920	struct nl_msg *msg;
7921	int res;
7922
7923	msg = nlmsg_alloc();
7924	if (!msg)
7925		return -1;
7926
7927	nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
7928
7929	/*
7930	 * XXX: FIX! this needs to flush all VLANs too
7931	 */
7932	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7933		    if_nametoindex(bss->ifname));
7934
7935	res = send_and_recv_msgs(drv, msg, NULL, NULL);
7936	if (res) {
7937		wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7938			   "(%s)", res, strerror(-res));
7939	}
7940	return res;
7941 nla_put_failure:
7942	nlmsg_free(msg);
7943	return -ENOBUFS;
7944}
7945
7946#endif /* HOSTAPD || CONFIG_AP */
7947
7948
7949static int get_sta_handler(struct nl_msg *msg, void *arg)
7950{
7951	struct nlattr *tb[NL80211_ATTR_MAX + 1];
7952	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7953	struct hostap_sta_driver_data *data = arg;
7954	struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7955	static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7956		[NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7957		[NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7958		[NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
7959		[NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7960		[NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
7961		[NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
7962	};
7963
7964	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7965		  genlmsg_attrlen(gnlh, 0), NULL);
7966
7967	/*
7968	 * TODO: validate the interface and mac address!
7969	 * Otherwise, there's a race condition as soon as
7970	 * the kernel starts sending station notifications.
7971	 */
7972
7973	if (!tb[NL80211_ATTR_STA_INFO]) {
7974		wpa_printf(MSG_DEBUG, "sta stats missing!");
7975		return NL_SKIP;
7976	}
7977	if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7978			     tb[NL80211_ATTR_STA_INFO],
7979			     stats_policy)) {
7980		wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7981		return NL_SKIP;
7982	}
7983
7984	if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7985		data->inactive_msec =
7986			nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
7987	if (stats[NL80211_STA_INFO_RX_BYTES])
7988		data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7989	if (stats[NL80211_STA_INFO_TX_BYTES])
7990		data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
7991	if (stats[NL80211_STA_INFO_RX_PACKETS])
7992		data->rx_packets =
7993			nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7994	if (stats[NL80211_STA_INFO_TX_PACKETS])
7995		data->tx_packets =
7996			nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
7997	if (stats[NL80211_STA_INFO_TX_FAILED])
7998		data->tx_retry_failed =
7999			nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
8000
8001	return NL_SKIP;
8002}
8003
8004static int i802_read_sta_data(struct i802_bss *bss,
8005			      struct hostap_sta_driver_data *data,
8006			      const u8 *addr)
8007{
8008	struct wpa_driver_nl80211_data *drv = bss->drv;
8009	struct nl_msg *msg;
8010
8011	os_memset(data, 0, sizeof(*data));
8012	msg = nlmsg_alloc();
8013	if (!msg)
8014		return -ENOMEM;
8015
8016	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
8017
8018	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8019	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8020
8021	return send_and_recv_msgs(drv, msg, get_sta_handler, data);
8022 nla_put_failure:
8023	nlmsg_free(msg);
8024	return -ENOBUFS;
8025}
8026
8027
8028#if defined(HOSTAPD) || defined(CONFIG_AP)
8029
8030static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
8031				    int cw_min, int cw_max, int burst_time)
8032{
8033	struct i802_bss *bss = priv;
8034	struct wpa_driver_nl80211_data *drv = bss->drv;
8035	struct nl_msg *msg;
8036	struct nlattr *txq, *params;
8037
8038	msg = nlmsg_alloc();
8039	if (!msg)
8040		return -1;
8041
8042	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
8043
8044	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8045
8046	txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
8047	if (!txq)
8048		goto nla_put_failure;
8049
8050	/* We are only sending parameters for a single TXQ at a time */
8051	params = nla_nest_start(msg, 1);
8052	if (!params)
8053		goto nla_put_failure;
8054
8055	switch (queue) {
8056	case 0:
8057		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
8058		break;
8059	case 1:
8060		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
8061		break;
8062	case 2:
8063		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
8064		break;
8065	case 3:
8066		NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
8067		break;
8068	}
8069	/* Burst time is configured in units of 0.1 msec and TXOP parameter in
8070	 * 32 usec, so need to convert the value here. */
8071	NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
8072	NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
8073	NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
8074	NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
8075
8076	nla_nest_end(msg, params);
8077
8078	nla_nest_end(msg, txq);
8079
8080	if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8081		return 0;
8082	msg = NULL;
8083 nla_put_failure:
8084	nlmsg_free(msg);
8085	return -1;
8086}
8087
8088
8089static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
8090			     const char *ifname, int vlan_id)
8091{
8092	struct wpa_driver_nl80211_data *drv = bss->drv;
8093	struct nl_msg *msg;
8094	int ret = -ENOBUFS;
8095
8096	msg = nlmsg_alloc();
8097	if (!msg)
8098		return -ENOMEM;
8099
8100	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
8101
8102	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8103		    if_nametoindex(bss->ifname));
8104	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8105	NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
8106		    if_nametoindex(ifname));
8107
8108	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8109	msg = NULL;
8110	if (ret < 0) {
8111		wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8112			   MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8113			   MAC2STR(addr), ifname, vlan_id, ret,
8114			   strerror(-ret));
8115	}
8116 nla_put_failure:
8117	nlmsg_free(msg);
8118	return ret;
8119}
8120
8121
8122static int i802_get_inact_sec(void *priv, const u8 *addr)
8123{
8124	struct hostap_sta_driver_data data;
8125	int ret;
8126
8127	data.inactive_msec = (unsigned long) -1;
8128	ret = i802_read_sta_data(priv, &data, addr);
8129	if (ret || data.inactive_msec == (unsigned long) -1)
8130		return -1;
8131	return data.inactive_msec / 1000;
8132}
8133
8134
8135static int i802_sta_clear_stats(void *priv, const u8 *addr)
8136{
8137#if 0
8138	/* TODO */
8139#endif
8140	return 0;
8141}
8142
8143
8144static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
8145			   int reason)
8146{
8147	struct i802_bss *bss = priv;
8148	struct wpa_driver_nl80211_data *drv = bss->drv;
8149	struct ieee80211_mgmt mgmt;
8150
8151	if (drv->device_ap_sme)
8152		return wpa_driver_nl80211_sta_remove(bss, addr);
8153
8154	memset(&mgmt, 0, sizeof(mgmt));
8155	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8156					  WLAN_FC_STYPE_DEAUTH);
8157	memcpy(mgmt.da, addr, ETH_ALEN);
8158	memcpy(mgmt.sa, own_addr, ETH_ALEN);
8159	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8160	mgmt.u.deauth.reason_code = host_to_le16(reason);
8161	return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8162					    IEEE80211_HDRLEN +
8163					    sizeof(mgmt.u.deauth), 0, 0, 0, 0,
8164					    0);
8165}
8166
8167
8168static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
8169			     int reason)
8170{
8171	struct i802_bss *bss = priv;
8172	struct wpa_driver_nl80211_data *drv = bss->drv;
8173	struct ieee80211_mgmt mgmt;
8174
8175	if (drv->device_ap_sme)
8176		return wpa_driver_nl80211_sta_remove(bss, addr);
8177
8178	memset(&mgmt, 0, sizeof(mgmt));
8179	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8180					  WLAN_FC_STYPE_DISASSOC);
8181	memcpy(mgmt.da, addr, ETH_ALEN);
8182	memcpy(mgmt.sa, own_addr, ETH_ALEN);
8183	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8184	mgmt.u.disassoc.reason_code = host_to_le16(reason);
8185	return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8186					    IEEE80211_HDRLEN +
8187					    sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
8188					    0);
8189}
8190
8191#endif /* HOSTAPD || CONFIG_AP */
8192
8193#ifdef HOSTAPD
8194
8195static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8196{
8197	int i;
8198	int *old;
8199
8200	wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
8201		   ifidx);
8202	for (i = 0; i < drv->num_if_indices; i++) {
8203		if (drv->if_indices[i] == 0) {
8204			drv->if_indices[i] = ifidx;
8205			return;
8206		}
8207	}
8208
8209	if (drv->if_indices != drv->default_if_indices)
8210		old = drv->if_indices;
8211	else
8212		old = NULL;
8213
8214	drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
8215					   sizeof(int));
8216	if (!drv->if_indices) {
8217		if (!old)
8218			drv->if_indices = drv->default_if_indices;
8219		else
8220			drv->if_indices = old;
8221		wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8222			   "interfaces");
8223		wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
8224		return;
8225	} else if (!old)
8226		os_memcpy(drv->if_indices, drv->default_if_indices,
8227			  sizeof(drv->default_if_indices));
8228	drv->if_indices[drv->num_if_indices] = ifidx;
8229	drv->num_if_indices++;
8230}
8231
8232
8233static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8234{
8235	int i;
8236
8237	for (i = 0; i < drv->num_if_indices; i++) {
8238		if (drv->if_indices[i] == ifidx) {
8239			drv->if_indices[i] = 0;
8240			break;
8241		}
8242	}
8243}
8244
8245
8246static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8247{
8248	int i;
8249
8250	for (i = 0; i < drv->num_if_indices; i++)
8251		if (drv->if_indices[i] == ifidx)
8252			return 1;
8253
8254	return 0;
8255}
8256
8257
8258static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
8259                            const char *bridge_ifname)
8260{
8261	struct i802_bss *bss = priv;
8262	struct wpa_driver_nl80211_data *drv = bss->drv;
8263	char name[IFNAMSIZ + 1];
8264
8265	os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
8266	wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
8267		   " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
8268	if (val) {
8269		if (!if_nametoindex(name)) {
8270			if (nl80211_create_iface(drv, name,
8271						 NL80211_IFTYPE_AP_VLAN,
8272						 bss->addr, 1) < 0)
8273				return -1;
8274			if (bridge_ifname &&
8275			    linux_br_add_if(drv->global->ioctl_sock,
8276					    bridge_ifname, name) < 0)
8277				return -1;
8278		}
8279		if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
8280			wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
8281				   "interface %s up", name);
8282		}
8283		return i802_set_sta_vlan(priv, addr, name, 0);
8284	} else {
8285		if (bridge_ifname)
8286			linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
8287					name);
8288
8289		i802_set_sta_vlan(priv, addr, bss->ifname, 0);
8290		return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
8291						    name);
8292	}
8293}
8294
8295
8296static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
8297{
8298	struct wpa_driver_nl80211_data *drv = eloop_ctx;
8299	struct sockaddr_ll lladdr;
8300	unsigned char buf[3000];
8301	int len;
8302	socklen_t fromlen = sizeof(lladdr);
8303
8304	len = recvfrom(sock, buf, sizeof(buf), 0,
8305		       (struct sockaddr *)&lladdr, &fromlen);
8306	if (len < 0) {
8307		perror("recv");
8308		return;
8309	}
8310
8311	if (have_ifidx(drv, lladdr.sll_ifindex))
8312		drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
8313}
8314
8315
8316static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
8317			     struct i802_bss *bss,
8318			     const char *brname, const char *ifname)
8319{
8320	int ifindex;
8321	char in_br[IFNAMSIZ];
8322
8323	os_strlcpy(bss->brname, brname, IFNAMSIZ);
8324	ifindex = if_nametoindex(brname);
8325	if (ifindex == 0) {
8326		/*
8327		 * Bridge was configured, but the bridge device does
8328		 * not exist. Try to add it now.
8329		 */
8330		if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
8331			wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8332				   "bridge interface %s: %s",
8333				   brname, strerror(errno));
8334			return -1;
8335		}
8336		bss->added_bridge = 1;
8337		add_ifidx(drv, if_nametoindex(brname));
8338	}
8339
8340	if (linux_br_get(in_br, ifname) == 0) {
8341		if (os_strcmp(in_br, brname) == 0)
8342			return 0; /* already in the bridge */
8343
8344		wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8345			   "bridge %s", ifname, in_br);
8346		if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8347		    0) {
8348			wpa_printf(MSG_ERROR, "nl80211: Failed to "
8349				   "remove interface %s from bridge "
8350				   "%s: %s",
8351				   ifname, brname, strerror(errno));
8352			return -1;
8353		}
8354	}
8355
8356	wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8357		   ifname, brname);
8358	if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
8359		wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
8360			   "into bridge %s: %s",
8361			   ifname, brname, strerror(errno));
8362		return -1;
8363	}
8364	bss->added_if_into_bridge = 1;
8365
8366	return 0;
8367}
8368
8369
8370static void *i802_init(struct hostapd_data *hapd,
8371		       struct wpa_init_params *params)
8372{
8373	struct wpa_driver_nl80211_data *drv;
8374	struct i802_bss *bss;
8375	size_t i;
8376	char brname[IFNAMSIZ];
8377	int ifindex, br_ifindex;
8378	int br_added = 0;
8379
8380	bss = wpa_driver_nl80211_init(hapd, params->ifname,
8381				      params->global_priv);
8382	if (bss == NULL)
8383		return NULL;
8384
8385	drv = bss->drv;
8386	drv->nlmode = NL80211_IFTYPE_AP;
8387	drv->eapol_sock = -1;
8388
8389	if (linux_br_get(brname, params->ifname) == 0) {
8390		wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
8391			   params->ifname, brname);
8392		br_ifindex = if_nametoindex(brname);
8393	} else {
8394		brname[0] = '\0';
8395		br_ifindex = 0;
8396	}
8397
8398	drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
8399	drv->if_indices = drv->default_if_indices;
8400	for (i = 0; i < params->num_bridge; i++) {
8401		if (params->bridge[i]) {
8402			ifindex = if_nametoindex(params->bridge[i]);
8403			if (ifindex)
8404				add_ifidx(drv, ifindex);
8405			if (ifindex == br_ifindex)
8406				br_added = 1;
8407		}
8408	}
8409	if (!br_added && br_ifindex &&
8410	    (params->num_bridge == 0 || !params->bridge[0]))
8411		add_ifidx(drv, br_ifindex);
8412
8413	/* start listening for EAPOL on the default AP interface */
8414	add_ifidx(drv, drv->ifindex);
8415
8416	if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
8417		goto failed;
8418
8419	if (params->bssid) {
8420		if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8421				       params->bssid))
8422			goto failed;
8423	}
8424
8425	if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
8426		wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
8427			   "into AP mode", bss->ifname);
8428		goto failed;
8429	}
8430
8431	if (params->num_bridge && params->bridge[0] &&
8432	    i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
8433		goto failed;
8434
8435	if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
8436		goto failed;
8437
8438	drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
8439	if (drv->eapol_sock < 0) {
8440		perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
8441		goto failed;
8442	}
8443
8444	if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
8445	{
8446		printf("Could not register read socket for eapol\n");
8447		goto failed;
8448	}
8449
8450	if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8451			       params->own_addr))
8452		goto failed;
8453
8454	memcpy(bss->addr, params->own_addr, ETH_ALEN);
8455
8456	return bss;
8457
8458failed:
8459	wpa_driver_nl80211_deinit(bss);
8460	return NULL;
8461}
8462
8463
8464static void i802_deinit(void *priv)
8465{
8466	struct i802_bss *bss = priv;
8467	wpa_driver_nl80211_deinit(bss);
8468}
8469
8470#endif /* HOSTAPD */
8471
8472
8473static enum nl80211_iftype wpa_driver_nl80211_if_type(
8474	enum wpa_driver_if_type type)
8475{
8476	switch (type) {
8477	case WPA_IF_STATION:
8478		return NL80211_IFTYPE_STATION;
8479	case WPA_IF_P2P_CLIENT:
8480	case WPA_IF_P2P_GROUP:
8481		return NL80211_IFTYPE_P2P_CLIENT;
8482	case WPA_IF_AP_VLAN:
8483		return NL80211_IFTYPE_AP_VLAN;
8484	case WPA_IF_AP_BSS:
8485		return NL80211_IFTYPE_AP;
8486	case WPA_IF_P2P_GO:
8487		return NL80211_IFTYPE_P2P_GO;
8488	}
8489	return -1;
8490}
8491
8492
8493#ifdef CONFIG_P2P
8494
8495static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
8496{
8497	struct wpa_driver_nl80211_data *drv;
8498	dl_list_for_each(drv, &global->interfaces,
8499			 struct wpa_driver_nl80211_data, list) {
8500		if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
8501			return 1;
8502	}
8503	return 0;
8504}
8505
8506
8507static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
8508				      u8 *new_addr)
8509{
8510	unsigned int idx;
8511
8512	if (!drv->global)
8513		return -1;
8514
8515	os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
8516	for (idx = 0; idx < 64; idx++) {
8517		new_addr[0] = drv->first_bss.addr[0] | 0x02;
8518		new_addr[0] ^= idx << 2;
8519		if (!nl80211_addr_in_use(drv->global, new_addr))
8520			break;
8521	}
8522	if (idx == 64)
8523		return -1;
8524
8525	wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
8526		   MACSTR, MAC2STR(new_addr));
8527
8528	return 0;
8529}
8530
8531#endif /* CONFIG_P2P */
8532
8533
8534static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8535				     const char *ifname, const u8 *addr,
8536				     void *bss_ctx, void **drv_priv,
8537				     char *force_ifname, u8 *if_addr,
8538				     const char *bridge)
8539{
8540	struct i802_bss *bss = priv;
8541	struct wpa_driver_nl80211_data *drv = bss->drv;
8542	int ifidx;
8543#ifdef HOSTAPD
8544	struct i802_bss *new_bss = NULL;
8545
8546	if (type == WPA_IF_AP_BSS) {
8547		new_bss = os_zalloc(sizeof(*new_bss));
8548		if (new_bss == NULL)
8549			return -1;
8550	}
8551#endif /* HOSTAPD */
8552
8553	if (addr)
8554		os_memcpy(if_addr, addr, ETH_ALEN);
8555	ifidx = nl80211_create_iface(drv, ifname,
8556				     wpa_driver_nl80211_if_type(type), addr,
8557				     0);
8558	if (ifidx < 0) {
8559#ifdef HOSTAPD
8560		os_free(new_bss);
8561#endif /* HOSTAPD */
8562		return -1;
8563	}
8564
8565	if (!addr &&
8566	    linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8567			       if_addr) < 0) {
8568		nl80211_remove_iface(drv, ifidx);
8569		return -1;
8570	}
8571
8572#ifdef CONFIG_P2P
8573	if (!addr &&
8574	    (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
8575	     type == WPA_IF_P2P_GO)) {
8576		/* Enforce unique P2P Interface Address */
8577		u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
8578
8579		if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8580				       own_addr) < 0 ||
8581		    linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
8582				       new_addr) < 0) {
8583			nl80211_remove_iface(drv, ifidx);
8584			return -1;
8585		}
8586		if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
8587			wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
8588				   "for P2P group interface");
8589			if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
8590				nl80211_remove_iface(drv, ifidx);
8591				return -1;
8592			}
8593			if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
8594					       new_addr) < 0) {
8595				nl80211_remove_iface(drv, ifidx);
8596				return -1;
8597			}
8598		}
8599		os_memcpy(if_addr, new_addr, ETH_ALEN);
8600	}
8601#endif /* CONFIG_P2P */
8602
8603#ifdef HOSTAPD
8604	if (bridge &&
8605	    i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
8606		wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
8607			   "interface %s to a bridge %s", ifname, bridge);
8608		nl80211_remove_iface(drv, ifidx);
8609		os_free(new_bss);
8610		return -1;
8611	}
8612
8613	if (type == WPA_IF_AP_BSS) {
8614		if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
8615		{
8616			nl80211_remove_iface(drv, ifidx);
8617			os_free(new_bss);
8618			return -1;
8619		}
8620		os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
8621		os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
8622		new_bss->ifindex = ifidx;
8623		new_bss->drv = drv;
8624		new_bss->next = drv->first_bss.next;
8625		new_bss->freq = drv->first_bss.freq;
8626		new_bss->ctx = bss_ctx;
8627		drv->first_bss.next = new_bss;
8628		if (drv_priv)
8629			*drv_priv = new_bss;
8630		nl80211_init_bss(new_bss);
8631
8632		/* Subscribe management frames for this WPA_IF_AP_BSS */
8633		if (nl80211_setup_ap(new_bss))
8634			return -1;
8635	}
8636#endif /* HOSTAPD */
8637
8638	if (drv->global)
8639		drv->global->if_add_ifindex = ifidx;
8640
8641	return 0;
8642}
8643
8644
8645static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
8646					enum wpa_driver_if_type type,
8647					const char *ifname)
8648{
8649	struct wpa_driver_nl80211_data *drv = bss->drv;
8650	int ifindex = if_nametoindex(ifname);
8651
8652	wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
8653		   __func__, type, ifname, ifindex);
8654	if (ifindex <= 0)
8655		return -1;
8656
8657	nl80211_remove_iface(drv, ifindex);
8658
8659#ifdef HOSTAPD
8660	if (type != WPA_IF_AP_BSS)
8661		return 0;
8662
8663	if (bss->added_if_into_bridge) {
8664		if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
8665				    bss->ifname) < 0)
8666			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8667				   "interface %s from bridge %s: %s",
8668				   bss->ifname, bss->brname, strerror(errno));
8669	}
8670	if (bss->added_bridge) {
8671		if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
8672			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8673				   "bridge %s: %s",
8674				   bss->brname, strerror(errno));
8675	}
8676
8677	if (bss != &drv->first_bss) {
8678		struct i802_bss *tbss;
8679
8680		for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
8681			if (tbss->next == bss) {
8682				tbss->next = bss->next;
8683				/* Unsubscribe management frames */
8684				nl80211_teardown_ap(bss);
8685				nl80211_destroy_bss(bss);
8686				os_free(bss);
8687				bss = NULL;
8688				break;
8689			}
8690		}
8691		if (bss)
8692			wpa_printf(MSG_INFO, "nl80211: %s - could not find "
8693				   "BSS %p in the list", __func__, bss);
8694	}
8695#endif /* HOSTAPD */
8696
8697	return 0;
8698}
8699
8700
8701static int cookie_handler(struct nl_msg *msg, void *arg)
8702{
8703	struct nlattr *tb[NL80211_ATTR_MAX + 1];
8704	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8705	u64 *cookie = arg;
8706	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8707		  genlmsg_attrlen(gnlh, 0), NULL);
8708	if (tb[NL80211_ATTR_COOKIE])
8709		*cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
8710	return NL_SKIP;
8711}
8712
8713
8714static int nl80211_send_frame_cmd(struct i802_bss *bss,
8715				  unsigned int freq, unsigned int wait,
8716				  const u8 *buf, size_t buf_len,
8717				  u64 *cookie_out, int no_cck, int no_ack,
8718				  int offchanok)
8719{
8720	struct wpa_driver_nl80211_data *drv = bss->drv;
8721	struct nl_msg *msg;
8722	u64 cookie;
8723	int ret = -1;
8724
8725	msg = nlmsg_alloc();
8726	if (!msg)
8727		return -1;
8728
8729	wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
8730		   "no_ack=%d offchanok=%d",
8731		   freq, wait, no_cck, no_ack, offchanok);
8732	nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
8733
8734	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8735	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8736	if (wait)
8737		NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
8738	if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
8739		NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
8740	if (no_cck)
8741		NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
8742	if (no_ack)
8743		NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
8744
8745	NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
8746
8747	cookie = 0;
8748	ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8749	msg = NULL;
8750	if (ret) {
8751		wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
8752			   "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
8753			   freq, wait);
8754		goto nla_put_failure;
8755	}
8756	wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
8757		   "cookie 0x%llx", no_ack ? " (no ACK)" : "",
8758		   (long long unsigned int) cookie);
8759
8760	if (cookie_out)
8761		*cookie_out = no_ack ? (u64) -1 : cookie;
8762
8763nla_put_failure:
8764	nlmsg_free(msg);
8765	return ret;
8766}
8767
8768
8769static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
8770					  unsigned int freq,
8771					  unsigned int wait_time,
8772					  const u8 *dst, const u8 *src,
8773					  const u8 *bssid,
8774					  const u8 *data, size_t data_len,
8775					  int no_cck)
8776{
8777	struct wpa_driver_nl80211_data *drv = bss->drv;
8778	int ret = -1;
8779	u8 *buf;
8780	struct ieee80211_hdr *hdr;
8781
8782	wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
8783		   "freq=%u MHz wait=%d ms no_cck=%d)",
8784		   drv->ifindex, freq, wait_time, no_cck);
8785
8786	buf = os_zalloc(24 + data_len);
8787	if (buf == NULL)
8788		return ret;
8789	os_memcpy(buf + 24, data, data_len);
8790	hdr = (struct ieee80211_hdr *) buf;
8791	hdr->frame_control =
8792		IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
8793	os_memcpy(hdr->addr1, dst, ETH_ALEN);
8794	os_memcpy(hdr->addr2, src, ETH_ALEN);
8795	os_memcpy(hdr->addr3, bssid, ETH_ALEN);
8796
8797	if (is_ap_interface(drv->nlmode))
8798		ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
8799						   0, freq, no_cck, 1,
8800						   wait_time);
8801	else
8802		ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
8803					     24 + data_len,
8804					     &drv->send_action_cookie,
8805					     no_cck, 0, 1);
8806
8807	os_free(buf);
8808	return ret;
8809}
8810
8811
8812static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
8813{
8814	struct i802_bss *bss = priv;
8815	struct wpa_driver_nl80211_data *drv = bss->drv;
8816	struct nl_msg *msg;
8817	int ret;
8818
8819	msg = nlmsg_alloc();
8820	if (!msg)
8821		return;
8822
8823	wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
8824		   (long long unsigned int) drv->send_action_cookie);
8825	nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
8826
8827	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8828	NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
8829
8830	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8831	msg = NULL;
8832	if (ret)
8833		wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
8834			   "(%s)", ret, strerror(-ret));
8835
8836 nla_put_failure:
8837	nlmsg_free(msg);
8838}
8839
8840
8841static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
8842						unsigned int duration)
8843{
8844	struct i802_bss *bss = priv;
8845	struct wpa_driver_nl80211_data *drv = bss->drv;
8846	struct nl_msg *msg;
8847	int ret;
8848	u64 cookie;
8849
8850	msg = nlmsg_alloc();
8851	if (!msg)
8852		return -1;
8853
8854	nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
8855
8856	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8857	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8858	NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
8859
8860	cookie = 0;
8861	ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8862	msg = NULL;
8863	if (ret == 0) {
8864		wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
8865			   "0x%llx for freq=%u MHz duration=%u",
8866			   (long long unsigned int) cookie, freq, duration);
8867		drv->remain_on_chan_cookie = cookie;
8868		drv->pending_remain_on_chan = 1;
8869		return 0;
8870	}
8871	wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
8872		   "(freq=%d duration=%u): %d (%s)",
8873		   freq, duration, ret, strerror(-ret));
8874nla_put_failure:
8875	nlmsg_free(msg);
8876	return -1;
8877}
8878
8879
8880static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
8881{
8882	struct i802_bss *bss = priv;
8883	struct wpa_driver_nl80211_data *drv = bss->drv;
8884	struct nl_msg *msg;
8885	int ret;
8886
8887	if (!drv->pending_remain_on_chan) {
8888		wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
8889			   "to cancel");
8890		return -1;
8891	}
8892
8893	wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
8894		   "0x%llx",
8895		   (long long unsigned int) drv->remain_on_chan_cookie);
8896
8897	msg = nlmsg_alloc();
8898	if (!msg)
8899		return -1;
8900
8901	nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
8902
8903	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8904	NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
8905
8906	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8907	msg = NULL;
8908	if (ret == 0)
8909		return 0;
8910	wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
8911		   "%d (%s)", ret, strerror(-ret));
8912nla_put_failure:
8913	nlmsg_free(msg);
8914	return -1;
8915}
8916
8917
8918static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
8919{
8920	struct wpa_driver_nl80211_data *drv = bss->drv;
8921
8922	if (!report) {
8923		if (bss->nl_preq && drv->device_ap_sme &&
8924		    is_ap_interface(drv->nlmode)) {
8925			/*
8926			 * Do not disable Probe Request reporting that was
8927			 * enabled in nl80211_setup_ap().
8928			 */
8929			wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
8930				   "Probe Request reporting nl_preq=%p while "
8931				   "in AP mode", bss->nl_preq);
8932		} else if (bss->nl_preq) {
8933			wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
8934				   "reporting nl_preq=%p", bss->nl_preq);
8935			eloop_unregister_read_sock(
8936				nl_socket_get_fd(bss->nl_preq));
8937			nl_destroy_handles(&bss->nl_preq);
8938		}
8939		return 0;
8940	}
8941
8942	if (bss->nl_preq) {
8943		wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
8944			   "already on! nl_preq=%p", bss->nl_preq);
8945		return 0;
8946	}
8947
8948	bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
8949	if (bss->nl_preq == NULL)
8950		return -1;
8951	wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
8952		   "reporting nl_preq=%p", bss->nl_preq);
8953
8954	if (nl80211_register_frame(bss, bss->nl_preq,
8955				   (WLAN_FC_TYPE_MGMT << 2) |
8956				   (WLAN_FC_STYPE_PROBE_REQ << 4),
8957				   NULL, 0) < 0)
8958		goto out_err;
8959
8960	eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
8961				 wpa_driver_nl80211_event_receive, bss->nl_cb,
8962				 bss->nl_preq);
8963
8964	return 0;
8965
8966 out_err:
8967	nl_destroy_handles(&bss->nl_preq);
8968	return -1;
8969}
8970
8971
8972static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8973				     int ifindex, int disabled)
8974{
8975	struct nl_msg *msg;
8976	struct nlattr *bands, *band;
8977	int ret;
8978
8979	msg = nlmsg_alloc();
8980	if (!msg)
8981		return -1;
8982
8983	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
8984	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
8985
8986	bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8987	if (!bands)
8988		goto nla_put_failure;
8989
8990	/*
8991	 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8992	 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8993	 * rates. All 5 GHz rates are left enabled.
8994	 */
8995	band = nla_nest_start(msg, NL80211_BAND_2GHZ);
8996	if (!band)
8997		goto nla_put_failure;
8998	if (disabled) {
8999		NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
9000			"\x0c\x12\x18\x24\x30\x48\x60\x6c");
9001	}
9002	nla_nest_end(msg, band);
9003
9004	nla_nest_end(msg, bands);
9005
9006	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9007	msg = NULL;
9008	if (ret) {
9009		wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
9010			   "(%s)", ret, strerror(-ret));
9011	} else
9012		drv->disabled_11b_rates = disabled;
9013
9014	return ret;
9015
9016nla_put_failure:
9017	nlmsg_free(msg);
9018	return -1;
9019}
9020
9021
9022static int wpa_driver_nl80211_deinit_ap(void *priv)
9023{
9024	struct i802_bss *bss = priv;
9025	struct wpa_driver_nl80211_data *drv = bss->drv;
9026	if (!is_ap_interface(drv->nlmode))
9027		return -1;
9028	wpa_driver_nl80211_del_beacon(drv);
9029	return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9030}
9031
9032
9033static int wpa_driver_nl80211_stop_ap(void *priv)
9034{
9035	struct i802_bss *bss = priv;
9036	struct wpa_driver_nl80211_data *drv = bss->drv;
9037	if (!is_ap_interface(drv->nlmode))
9038		return -1;
9039	wpa_driver_nl80211_del_beacon(drv);
9040	bss->beacon_set = 0;
9041	return 0;
9042}
9043
9044
9045static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
9046{
9047	struct i802_bss *bss = priv;
9048	struct wpa_driver_nl80211_data *drv = bss->drv;
9049	if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
9050		return -1;
9051	return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9052}
9053
9054
9055static void wpa_driver_nl80211_resume(void *priv)
9056{
9057	struct i802_bss *bss = priv;
9058	struct wpa_driver_nl80211_data *drv = bss->drv;
9059	if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
9060		wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
9061			   "resume event");
9062	}
9063}
9064
9065
9066static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
9067				  const u8 *ies, size_t ies_len)
9068{
9069	struct i802_bss *bss = priv;
9070	struct wpa_driver_nl80211_data *drv = bss->drv;
9071	int ret;
9072	u8 *data, *pos;
9073	size_t data_len;
9074	const u8 *own_addr = bss->addr;
9075
9076	if (action != 1) {
9077		wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
9078			   "action %d", action);
9079		return -1;
9080	}
9081
9082	/*
9083	 * Action frame payload:
9084	 * Category[1] = 6 (Fast BSS Transition)
9085	 * Action[1] = 1 (Fast BSS Transition Request)
9086	 * STA Address
9087	 * Target AP Address
9088	 * FT IEs
9089	 */
9090
9091	data_len = 2 + 2 * ETH_ALEN + ies_len;
9092	data = os_malloc(data_len);
9093	if (data == NULL)
9094		return -1;
9095	pos = data;
9096	*pos++ = 0x06; /* FT Action category */
9097	*pos++ = action;
9098	os_memcpy(pos, own_addr, ETH_ALEN);
9099	pos += ETH_ALEN;
9100	os_memcpy(pos, target_ap, ETH_ALEN);
9101	pos += ETH_ALEN;
9102	os_memcpy(pos, ies, ies_len);
9103
9104	ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
9105					     drv->bssid, own_addr, drv->bssid,
9106					     data, data_len, 0);
9107	os_free(data);
9108
9109	return ret;
9110}
9111
9112
9113static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9114{
9115	struct i802_bss *bss = priv;
9116	struct wpa_driver_nl80211_data *drv = bss->drv;
9117	struct nl_msg *msg;
9118	struct nlattr *cqm;
9119	int ret = -1;
9120
9121	wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9122		   "hysteresis=%d", threshold, hysteresis);
9123
9124	msg = nlmsg_alloc();
9125	if (!msg)
9126		return -1;
9127
9128	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
9129
9130	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9131
9132	cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
9133	if (cqm == NULL)
9134		goto nla_put_failure;
9135
9136	NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
9137	NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
9138	nla_nest_end(msg, cqm);
9139
9140	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9141	msg = NULL;
9142
9143nla_put_failure:
9144	nlmsg_free(msg);
9145	return ret;
9146}
9147
9148
9149static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
9150{
9151	struct i802_bss *bss = priv;
9152	struct wpa_driver_nl80211_data *drv = bss->drv;
9153	int res;
9154
9155	os_memset(si, 0, sizeof(*si));
9156	res = nl80211_get_link_signal(drv, si);
9157	if (res != 0)
9158		return res;
9159
9160	return nl80211_get_link_noise(drv, si);
9161}
9162
9163
9164static int wpa_driver_nl80211_shared_freq(void *priv)
9165{
9166	struct i802_bss *bss = priv;
9167	struct wpa_driver_nl80211_data *drv = bss->drv;
9168	struct wpa_driver_nl80211_data *driver;
9169	int freq = 0;
9170
9171	/*
9172	 * If the same PHY is in connected state with some other interface,
9173	 * then retrieve the assoc freq.
9174	 */
9175	wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
9176		   drv->phyname);
9177
9178	dl_list_for_each(driver, &drv->global->interfaces,
9179			 struct wpa_driver_nl80211_data, list) {
9180		if (drv == driver ||
9181		    os_strcmp(drv->phyname, driver->phyname) != 0 ||
9182#ifdef ANDROID_P2P
9183		    (!driver->associated && !is_ap_interface(driver->nlmode)))
9184#else
9185		    !driver->associated)
9186#endif
9187			continue;
9188
9189		wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
9190			   MACSTR,
9191			   driver->phyname, driver->first_bss.ifname,
9192			   MAC2STR(driver->first_bss.addr));
9193		if (is_ap_interface(driver->nlmode))
9194			freq = driver->first_bss.freq;
9195		else
9196			freq = nl80211_get_assoc_freq(driver);
9197		wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
9198			   drv->phyname, freq);
9199	}
9200
9201	if (!freq)
9202		wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
9203			   "PHY (%s) in associated state", drv->phyname);
9204
9205	return freq;
9206}
9207
9208
9209static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
9210			      int encrypt)
9211{
9212	struct i802_bss *bss = priv;
9213	return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
9214					     0, 0, 0, 0);
9215}
9216
9217
9218static int nl80211_set_param(void *priv, const char *param)
9219{
9220	wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
9221	if (param == NULL)
9222		return 0;
9223
9224#ifdef CONFIG_P2P
9225	if (os_strstr(param, "use_p2p_group_interface=1")) {
9226		struct i802_bss *bss = priv;
9227		struct wpa_driver_nl80211_data *drv = bss->drv;
9228
9229		wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
9230			   "interface");
9231		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
9232		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
9233	}
9234#ifdef ANDROID_P2P
9235	if(os_strstr(param, "use_multi_chan_concurrent=1")) {
9236		struct i802_bss *bss = priv;
9237		struct wpa_driver_nl80211_data *drv = bss->drv;
9238		wpa_printf(MSG_DEBUG, "nl80211: Use Multi channel "
9239			   "concurrency");
9240		drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
9241	}
9242#endif
9243#endif /* CONFIG_P2P */
9244
9245	return 0;
9246}
9247
9248
9249static void * nl80211_global_init(void)
9250{
9251	struct nl80211_global *global;
9252	struct netlink_config *cfg;
9253
9254	global = os_zalloc(sizeof(*global));
9255	if (global == NULL)
9256		return NULL;
9257	global->ioctl_sock = -1;
9258	dl_list_init(&global->interfaces);
9259	global->if_add_ifindex = -1;
9260
9261	cfg = os_zalloc(sizeof(*cfg));
9262	if (cfg == NULL)
9263		goto err;
9264
9265	cfg->ctx = global;
9266	cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
9267	cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
9268	global->netlink = netlink_init(cfg);
9269	if (global->netlink == NULL) {
9270		os_free(cfg);
9271		goto err;
9272	}
9273
9274	if (wpa_driver_nl80211_init_nl_global(global) < 0)
9275		goto err;
9276
9277	global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
9278	if (global->ioctl_sock < 0) {
9279		perror("socket(PF_INET,SOCK_DGRAM)");
9280		goto err;
9281	}
9282
9283	return global;
9284
9285err:
9286	nl80211_global_deinit(global);
9287	return NULL;
9288}
9289
9290
9291static void nl80211_global_deinit(void *priv)
9292{
9293	struct nl80211_global *global = priv;
9294	if (global == NULL)
9295		return;
9296	if (!dl_list_empty(&global->interfaces)) {
9297		wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
9298			   "nl80211_global_deinit",
9299			   dl_list_len(&global->interfaces));
9300	}
9301
9302	if (global->netlink)
9303		netlink_deinit(global->netlink);
9304
9305	nl_destroy_handles(&global->nl);
9306
9307	if (global->nl_event) {
9308		eloop_unregister_read_sock(
9309			nl_socket_get_fd(global->nl_event));
9310		nl_destroy_handles(&global->nl_event);
9311	}
9312
9313	nl_cb_put(global->nl_cb);
9314
9315	if (global->ioctl_sock >= 0)
9316		close(global->ioctl_sock);
9317
9318	os_free(global);
9319}
9320
9321
9322static const char * nl80211_get_radio_name(void *priv)
9323{
9324	struct i802_bss *bss = priv;
9325	struct wpa_driver_nl80211_data *drv = bss->drv;
9326	return drv->phyname;
9327}
9328
9329
9330static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
9331			 const u8 *pmkid)
9332{
9333	struct nl_msg *msg;
9334
9335	msg = nlmsg_alloc();
9336	if (!msg)
9337		return -ENOMEM;
9338
9339	nl80211_cmd(bss->drv, msg, 0, cmd);
9340
9341	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9342	if (pmkid)
9343		NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
9344	if (bssid)
9345		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
9346
9347	return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9348 nla_put_failure:
9349	nlmsg_free(msg);
9350	return -ENOBUFS;
9351}
9352
9353
9354static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9355{
9356	struct i802_bss *bss = priv;
9357	wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
9358	return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
9359}
9360
9361
9362static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9363{
9364	struct i802_bss *bss = priv;
9365	wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
9366		   MAC2STR(bssid));
9367	return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
9368}
9369
9370
9371static int nl80211_flush_pmkid(void *priv)
9372{
9373	struct i802_bss *bss = priv;
9374	wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
9375	return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
9376}
9377
9378
9379static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
9380				   const u8 *replay_ctr)
9381{
9382	struct i802_bss *bss = priv;
9383	struct wpa_driver_nl80211_data *drv = bss->drv;
9384	struct nlattr *replay_nested;
9385	struct nl_msg *msg;
9386
9387	msg = nlmsg_alloc();
9388	if (!msg)
9389		return;
9390
9391	nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9392
9393	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9394
9395	replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9396	if (!replay_nested)
9397		goto nla_put_failure;
9398
9399	NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
9400	NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
9401	NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
9402		replay_ctr);
9403
9404	nla_nest_end(msg, replay_nested);
9405
9406	send_and_recv_msgs(drv, msg, NULL, NULL);
9407	return;
9408 nla_put_failure:
9409	nlmsg_free(msg);
9410}
9411
9412
9413static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
9414				    const u8 *addr, int qos)
9415{
9416	/* send data frame to poll STA and check whether
9417	 * this frame is ACKed */
9418	struct {
9419		struct ieee80211_hdr hdr;
9420		u16 qos_ctl;
9421	} STRUCT_PACKED nulldata;
9422	size_t size;
9423
9424	/* Send data frame to poll STA and check whether this frame is ACKed */
9425
9426	os_memset(&nulldata, 0, sizeof(nulldata));
9427
9428	if (qos) {
9429		nulldata.hdr.frame_control =
9430			IEEE80211_FC(WLAN_FC_TYPE_DATA,
9431				     WLAN_FC_STYPE_QOS_NULL);
9432		size = sizeof(nulldata);
9433	} else {
9434		nulldata.hdr.frame_control =
9435			IEEE80211_FC(WLAN_FC_TYPE_DATA,
9436				     WLAN_FC_STYPE_NULLFUNC);
9437		size = sizeof(struct ieee80211_hdr);
9438	}
9439
9440	nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
9441	os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
9442	os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
9443	os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
9444
9445	if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
9446					 0, 0) < 0)
9447		wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
9448			   "send poll frame");
9449}
9450
9451static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
9452				int qos)
9453{
9454	struct i802_bss *bss = priv;
9455	struct wpa_driver_nl80211_data *drv = bss->drv;
9456	struct nl_msg *msg;
9457
9458	if (!drv->poll_command_supported) {
9459		nl80211_send_null_frame(bss, own_addr, addr, qos);
9460		return;
9461	}
9462
9463	msg = nlmsg_alloc();
9464	if (!msg)
9465		return;
9466
9467	nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
9468
9469	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9470	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9471
9472	send_and_recv_msgs(drv, msg, NULL, NULL);
9473	return;
9474 nla_put_failure:
9475	nlmsg_free(msg);
9476}
9477
9478
9479static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
9480{
9481	struct nl_msg *msg;
9482
9483	msg = nlmsg_alloc();
9484	if (!msg)
9485		return -ENOMEM;
9486
9487	nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
9488	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9489	NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
9490		    enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
9491	return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9492nla_put_failure:
9493	nlmsg_free(msg);
9494	return -ENOBUFS;
9495}
9496
9497
9498static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
9499				     int ctwindow)
9500{
9501	struct i802_bss *bss = priv;
9502
9503	wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
9504		   "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
9505
9506	if (opp_ps != -1 || ctwindow != -1)
9507#ifdef ANDROID_P2P
9508		wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
9509#else
9510		return -1; /* Not yet supported */
9511#endif
9512
9513	if (legacy_ps == -1)
9514		return 0;
9515	if (legacy_ps != 0 && legacy_ps != 1)
9516		return -1; /* Not yet supported */
9517
9518	return nl80211_set_power_save(bss, legacy_ps);
9519}
9520
9521
9522static int nl80211_start_radar_detection(void *priv, int freq)
9523{
9524	struct i802_bss *bss = priv;
9525	struct wpa_driver_nl80211_data *drv = bss->drv;
9526	struct nl_msg *msg;
9527	int ret;
9528
9529	wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC)");
9530	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
9531		wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
9532			   "detection");
9533		return -1;
9534	}
9535
9536	msg = nlmsg_alloc();
9537	if (!msg)
9538		return -1;
9539
9540	nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
9541	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9542	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9543
9544	/* only HT20 is supported at this point */
9545	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, NL80211_CHAN_HT20);
9546
9547	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9548	if (ret == 0)
9549		return 0;
9550	wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
9551		   "%d (%s)", ret, strerror(-ret));
9552nla_put_failure:
9553	return -1;
9554}
9555
9556#ifdef CONFIG_TDLS
9557
9558static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
9559				  u8 dialog_token, u16 status_code,
9560				  const u8 *buf, size_t len)
9561{
9562	struct i802_bss *bss = priv;
9563	struct wpa_driver_nl80211_data *drv = bss->drv;
9564	struct nl_msg *msg;
9565
9566	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9567		return -EOPNOTSUPP;
9568
9569	if (!dst)
9570		return -EINVAL;
9571
9572	msg = nlmsg_alloc();
9573	if (!msg)
9574		return -ENOMEM;
9575
9576	nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
9577	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9578	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
9579	NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
9580	NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
9581	NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
9582	NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
9583
9584	return send_and_recv_msgs(drv, msg, NULL, NULL);
9585
9586nla_put_failure:
9587	nlmsg_free(msg);
9588	return -ENOBUFS;
9589}
9590
9591
9592static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
9593{
9594	struct i802_bss *bss = priv;
9595	struct wpa_driver_nl80211_data *drv = bss->drv;
9596	struct nl_msg *msg;
9597	enum nl80211_tdls_operation nl80211_oper;
9598
9599	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9600		return -EOPNOTSUPP;
9601
9602	switch (oper) {
9603	case TDLS_DISCOVERY_REQ:
9604		nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
9605		break;
9606	case TDLS_SETUP:
9607		nl80211_oper = NL80211_TDLS_SETUP;
9608		break;
9609	case TDLS_TEARDOWN:
9610		nl80211_oper = NL80211_TDLS_TEARDOWN;
9611		break;
9612	case TDLS_ENABLE_LINK:
9613		nl80211_oper = NL80211_TDLS_ENABLE_LINK;
9614		break;
9615	case TDLS_DISABLE_LINK:
9616		nl80211_oper = NL80211_TDLS_DISABLE_LINK;
9617		break;
9618	case TDLS_ENABLE:
9619		return 0;
9620	case TDLS_DISABLE:
9621		return 0;
9622	default:
9623		return -EINVAL;
9624	}
9625
9626	msg = nlmsg_alloc();
9627	if (!msg)
9628		return -ENOMEM;
9629
9630	nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
9631	NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
9632	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9633	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
9634
9635	return send_and_recv_msgs(drv, msg, NULL, NULL);
9636
9637nla_put_failure:
9638	nlmsg_free(msg);
9639	return -ENOBUFS;
9640}
9641
9642#endif /* CONFIG TDLS */
9643
9644
9645#ifdef ANDROID
9646
9647typedef struct android_wifi_priv_cmd {
9648	char *buf;
9649	int used_len;
9650	int total_len;
9651} android_wifi_priv_cmd;
9652
9653static int drv_errors = 0;
9654
9655static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
9656{
9657	drv_errors++;
9658	if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
9659		drv_errors = 0;
9660		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
9661	}
9662}
9663
9664
9665static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
9666{
9667	struct wpa_driver_nl80211_data *drv = bss->drv;
9668	struct ifreq ifr;
9669	android_wifi_priv_cmd priv_cmd;
9670	char buf[MAX_DRV_CMD_SIZE];
9671	int ret;
9672
9673	os_memset(&ifr, 0, sizeof(ifr));
9674	os_memset(&priv_cmd, 0, sizeof(priv_cmd));
9675	os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9676
9677	os_memset(buf, 0, sizeof(buf));
9678	os_strlcpy(buf, cmd, sizeof(buf));
9679
9680	priv_cmd.buf = buf;
9681	priv_cmd.used_len = sizeof(buf);
9682	priv_cmd.total_len = sizeof(buf);
9683	ifr.ifr_data = &priv_cmd;
9684
9685	ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9686	if (ret < 0) {
9687		wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
9688			   __func__);
9689		wpa_driver_send_hang_msg(drv);
9690		return ret;
9691	}
9692
9693	drv_errors = 0;
9694	return 0;
9695}
9696
9697
9698static int android_pno_start(struct i802_bss *bss,
9699			     struct wpa_driver_scan_params *params)
9700{
9701	struct wpa_driver_nl80211_data *drv = bss->drv;
9702	struct ifreq ifr;
9703	android_wifi_priv_cmd priv_cmd;
9704	int ret = 0, i = 0, bp;
9705	char buf[WEXT_PNO_MAX_COMMAND_SIZE];
9706
9707	bp = WEXT_PNOSETUP_HEADER_SIZE;
9708	os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
9709	buf[bp++] = WEXT_PNO_TLV_PREFIX;
9710	buf[bp++] = WEXT_PNO_TLV_VERSION;
9711	buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
9712	buf[bp++] = WEXT_PNO_TLV_RESERVED;
9713
9714	while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
9715		/* Check that there is enough space needed for 1 more SSID, the
9716		 * other sections and null termination */
9717		if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
9718		     WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
9719			break;
9720		wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
9721				  params->ssids[i].ssid,
9722				  params->ssids[i].ssid_len);
9723		buf[bp++] = WEXT_PNO_SSID_SECTION;
9724		buf[bp++] = params->ssids[i].ssid_len;
9725		os_memcpy(&buf[bp], params->ssids[i].ssid,
9726			  params->ssids[i].ssid_len);
9727		bp += params->ssids[i].ssid_len;
9728		i++;
9729	}
9730
9731	buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
9732	os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
9733		    WEXT_PNO_SCAN_INTERVAL);
9734	bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
9735
9736	buf[bp++] = WEXT_PNO_REPEAT_SECTION;
9737	os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
9738		    WEXT_PNO_REPEAT);
9739	bp += WEXT_PNO_REPEAT_LENGTH;
9740
9741	buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
9742	os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
9743		    WEXT_PNO_MAX_REPEAT);
9744	bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
9745
9746	memset(&ifr, 0, sizeof(ifr));
9747	memset(&priv_cmd, 0, sizeof(priv_cmd));
9748	os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9749
9750	priv_cmd.buf = buf;
9751	priv_cmd.used_len = bp;
9752	priv_cmd.total_len = bp;
9753	ifr.ifr_data = &priv_cmd;
9754
9755	ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9756
9757	if (ret < 0) {
9758		wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
9759			   ret);
9760		wpa_driver_send_hang_msg(drv);
9761		return ret;
9762	}
9763
9764	drv_errors = 0;
9765
9766	return android_priv_cmd(bss, "PNOFORCE 1");
9767}
9768
9769
9770static int android_pno_stop(struct i802_bss *bss)
9771{
9772	return android_priv_cmd(bss, "PNOFORCE 0");
9773}
9774
9775#endif /* ANDROID */
9776
9777
9778static int driver_nl80211_set_key(const char *ifname, void *priv,
9779				  enum wpa_alg alg, const u8 *addr,
9780				  int key_idx, int set_tx,
9781				  const u8 *seq, size_t seq_len,
9782				  const u8 *key, size_t key_len)
9783{
9784	struct i802_bss *bss = priv;
9785	return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
9786					  set_tx, seq, seq_len, key, key_len);
9787}
9788
9789
9790static int driver_nl80211_scan2(void *priv,
9791				struct wpa_driver_scan_params *params)
9792{
9793	struct i802_bss *bss = priv;
9794	return wpa_driver_nl80211_scan(bss, params);
9795}
9796
9797
9798static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
9799					 int reason_code)
9800{
9801	struct i802_bss *bss = priv;
9802	return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
9803}
9804
9805
9806static int driver_nl80211_authenticate(void *priv,
9807				       struct wpa_driver_auth_params *params)
9808{
9809	struct i802_bss *bss = priv;
9810	return wpa_driver_nl80211_authenticate(bss, params);
9811}
9812
9813
9814static void driver_nl80211_deinit(void *priv)
9815{
9816	struct i802_bss *bss = priv;
9817	wpa_driver_nl80211_deinit(bss);
9818}
9819
9820
9821static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
9822				    const char *ifname)
9823{
9824	struct i802_bss *bss = priv;
9825	return wpa_driver_nl80211_if_remove(bss, type, ifname);
9826}
9827
9828
9829static int driver_nl80211_send_mlme(void *priv, const u8 *data,
9830				    size_t data_len, int noack)
9831{
9832	struct i802_bss *bss = priv;
9833	return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
9834					    0, 0, 0, 0);
9835}
9836
9837
9838static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
9839{
9840	struct i802_bss *bss = priv;
9841	return wpa_driver_nl80211_sta_remove(bss, addr);
9842}
9843
9844
9845#if defined(HOSTAPD) || defined(CONFIG_AP)
9846static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
9847				       const char *ifname, int vlan_id)
9848{
9849	struct i802_bss *bss = priv;
9850	return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
9851}
9852#endif /* HOSTAPD || CONFIG_AP */
9853
9854
9855static int driver_nl80211_read_sta_data(void *priv,
9856					struct hostap_sta_driver_data *data,
9857					const u8 *addr)
9858{
9859	struct i802_bss *bss = priv;
9860	return i802_read_sta_data(bss, data, addr);
9861}
9862
9863
9864static int driver_nl80211_send_action(void *priv, unsigned int freq,
9865				      unsigned int wait_time,
9866				      const u8 *dst, const u8 *src,
9867				      const u8 *bssid,
9868				      const u8 *data, size_t data_len,
9869				      int no_cck)
9870{
9871	struct i802_bss *bss = priv;
9872	return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
9873					      bssid, data, data_len, no_cck);
9874}
9875
9876
9877static int driver_nl80211_probe_req_report(void *priv, int report)
9878{
9879	struct i802_bss *bss = priv;
9880	return wpa_driver_nl80211_probe_req_report(bss, report);
9881}
9882
9883
9884static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
9885					    const u8 *ies, size_t ies_len)
9886{
9887	int ret;
9888	struct nl_msg *msg;
9889	struct i802_bss *bss = priv;
9890	struct wpa_driver_nl80211_data *drv = bss->drv;
9891	u16 mdid = WPA_GET_LE16(md);
9892
9893	msg = nlmsg_alloc();
9894	if (!msg)
9895		return -ENOMEM;
9896
9897	wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
9898	nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
9899	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9900	NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
9901	NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
9902
9903	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9904	if (ret) {
9905		wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
9906			   "err=%d (%s)", ret, strerror(-ret));
9907	}
9908
9909	return ret;
9910
9911nla_put_failure:
9912	nlmsg_free(msg);
9913	return -ENOBUFS;
9914}
9915
9916
9917const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9918	.name = "nl80211",
9919	.desc = "Linux nl80211/cfg80211",
9920	.get_bssid = wpa_driver_nl80211_get_bssid,
9921	.get_ssid = wpa_driver_nl80211_get_ssid,
9922	.set_key = driver_nl80211_set_key,
9923	.scan2 = driver_nl80211_scan2,
9924	.sched_scan = wpa_driver_nl80211_sched_scan,
9925	.stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
9926	.get_scan_results2 = wpa_driver_nl80211_get_scan_results,
9927	.deauthenticate = driver_nl80211_deauthenticate,
9928	.authenticate = driver_nl80211_authenticate,
9929	.associate = wpa_driver_nl80211_associate,
9930	.global_init = nl80211_global_init,
9931	.global_deinit = nl80211_global_deinit,
9932	.init2 = wpa_driver_nl80211_init,
9933	.deinit = driver_nl80211_deinit,
9934	.get_capa = wpa_driver_nl80211_get_capa,
9935	.set_operstate = wpa_driver_nl80211_set_operstate,
9936	.set_supp_port = wpa_driver_nl80211_set_supp_port,
9937	.set_country = wpa_driver_nl80211_set_country,
9938	.set_ap = wpa_driver_nl80211_set_ap,
9939	.if_add = wpa_driver_nl80211_if_add,
9940	.if_remove = driver_nl80211_if_remove,
9941	.send_mlme = driver_nl80211_send_mlme,
9942	.get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
9943	.sta_add = wpa_driver_nl80211_sta_add,
9944	.sta_remove = driver_nl80211_sta_remove,
9945	.hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9946	.sta_set_flags = wpa_driver_nl80211_sta_set_flags,
9947#ifdef HOSTAPD
9948	.hapd_init = i802_init,
9949	.hapd_deinit = i802_deinit,
9950	.set_wds_sta = i802_set_wds_sta,
9951#endif /* HOSTAPD */
9952#if defined(HOSTAPD) || defined(CONFIG_AP)
9953	.get_seqnum = i802_get_seqnum,
9954	.flush = i802_flush,
9955	.get_inact_sec = i802_get_inact_sec,
9956	.sta_clear_stats = i802_sta_clear_stats,
9957	.set_rts = i802_set_rts,
9958	.set_frag = i802_set_frag,
9959	.set_tx_queue_params = i802_set_tx_queue_params,
9960	.set_sta_vlan = driver_nl80211_set_sta_vlan,
9961	.sta_deauth = i802_sta_deauth,
9962	.sta_disassoc = i802_sta_disassoc,
9963#endif /* HOSTAPD || CONFIG_AP */
9964	.read_sta_data = driver_nl80211_read_sta_data,
9965	.set_freq = i802_set_freq,
9966	.send_action = driver_nl80211_send_action,
9967	.send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9968	.remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9969	.cancel_remain_on_channel =
9970	wpa_driver_nl80211_cancel_remain_on_channel,
9971	.probe_req_report = driver_nl80211_probe_req_report,
9972	.deinit_ap = wpa_driver_nl80211_deinit_ap,
9973	.deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
9974	.resume = wpa_driver_nl80211_resume,
9975	.send_ft_action = nl80211_send_ft_action,
9976	.signal_monitor = nl80211_signal_monitor,
9977	.signal_poll = nl80211_signal_poll,
9978	.send_frame = nl80211_send_frame,
9979	.shared_freq = wpa_driver_nl80211_shared_freq,
9980	.set_param = nl80211_set_param,
9981	.get_radio_name = nl80211_get_radio_name,
9982	.add_pmkid = nl80211_add_pmkid,
9983	.remove_pmkid = nl80211_remove_pmkid,
9984	.flush_pmkid = nl80211_flush_pmkid,
9985	.set_rekey_info = nl80211_set_rekey_info,
9986	.poll_client = nl80211_poll_client,
9987	.set_p2p_powersave = nl80211_set_p2p_powersave,
9988	.start_dfs_cac = nl80211_start_radar_detection,
9989	.stop_ap = wpa_driver_nl80211_stop_ap,
9990#ifdef CONFIG_TDLS
9991	.send_tdls_mgmt = nl80211_send_tdls_mgmt,
9992	.tdls_oper = nl80211_tdls_oper,
9993#endif /* CONFIG_TDLS */
9994	.update_ft_ies = wpa_driver_nl80211_update_ft_ies,
9995#ifdef ANDROID_P2P
9996	.set_noa = wpa_driver_set_p2p_noa,
9997	.get_noa = wpa_driver_get_p2p_noa,
9998	.set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
9999#endif
10000#ifdef ANDROID
10001	.driver_cmd = wpa_driver_nl80211_driver_cmd,
10002#endif
10003};
10004