xref: /netbsd-src/external/bsd/wpa/dist/src/drivers/driver_nl80211.c (revision 8ecbf5f02b752fcb7debe1a8fab1dc82602bc760)
1 /*
2  * Driver interaction with Linux nl80211/cfg80211
3  * Copyright (c) 2002-2015, 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/types.h>
15 #include <fcntl.h>
16 #include <net/if.h>
17 #include <netlink/genl/genl.h>
18 #include <netlink/genl/ctrl.h>
19 #ifdef CONFIG_LIBNL3_ROUTE
20 #include <netlink/route/neighbour.h>
21 #endif /* CONFIG_LIBNL3_ROUTE */
22 #include <linux/rtnetlink.h>
23 #include <netpacket/packet.h>
24 #include <linux/errqueue.h>
25 
26 #include "common.h"
27 #include "eloop.h"
28 #include "common/qca-vendor.h"
29 #include "common/qca-vendor-attr.h"
30 #include "common/ieee802_11_defs.h"
31 #include "common/ieee802_11_common.h"
32 #include "common/wpa_common.h"
33 #include "l2_packet/l2_packet.h"
34 #include "netlink.h"
35 #include "linux_defines.h"
36 #include "linux_ioctl.h"
37 #include "radiotap.h"
38 #include "radiotap_iter.h"
39 #include "rfkill.h"
40 #include "driver_nl80211.h"
41 
42 
43 /* support for extack if compilation headers are too old */
44 #ifndef NETLINK_EXT_ACK
45 #define NETLINK_EXT_ACK 11
46 enum nlmsgerr_attrs {
47 	NLMSGERR_ATTR_UNUSED,
48 	NLMSGERR_ATTR_MSG,
49 	NLMSGERR_ATTR_OFFS,
50 	NLMSGERR_ATTR_COOKIE,
51 
52 	__NLMSGERR_ATTR_MAX,
53 	NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1
54 };
55 #endif
56 #ifndef NLM_F_CAPPED
57 #define NLM_F_CAPPED 0x100
58 #endif
59 #ifndef NLM_F_ACK_TLVS
60 #define NLM_F_ACK_TLVS 0x200
61 #endif
62 #ifndef SOL_NETLINK
63 #define SOL_NETLINK 270
64 #endif
65 
66 #ifndef CONFIG_LIBNL20
67 /*
68  * libnl 1.1 has a bug, it tries to allocate socket numbers densely
69  * but when you free a socket again it will mess up its bitmap and
70  * and use the wrong number the next time it needs a socket ID.
71  * Therefore, we wrap the handle alloc/destroy and add our own pid
72  * accounting.
73  */
74 static uint32_t port_bitmap[32] = { 0 };
75 
76 static struct nl_handle *nl80211_handle_alloc(void *cb)
77 {
78 	struct nl_handle *handle;
79 	uint32_t pid = getpid() & 0x3FFFFF;
80 	int i;
81 
82 	handle = nl_handle_alloc_cb(cb);
83 
84 	for (i = 0; i < 1024; i++) {
85 		if (port_bitmap[i / 32] & (1 << (i % 32)))
86 			continue;
87 		port_bitmap[i / 32] |= 1 << (i % 32);
88 		pid += i << 22;
89 		break;
90 	}
91 
92 	nl_socket_set_local_port(handle, pid);
93 
94 	return handle;
95 }
96 
97 static void nl80211_handle_destroy(struct nl_handle *handle)
98 {
99 	uint32_t port = nl_socket_get_local_port(handle);
100 
101 	port >>= 22;
102 	port_bitmap[port / 32] &= ~(1 << (port % 32));
103 
104 	nl_handle_destroy(handle);
105 }
106 #endif /* CONFIG_LIBNL20 */
107 
108 
109 #ifdef ANDROID
110 /* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
111 #undef nl_socket_set_nonblocking
112 #define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
113 
114 #endif /* ANDROID */
115 
116 
117 static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
118 {
119 	struct nl_handle *handle;
120 
121 	handle = nl80211_handle_alloc(cb);
122 	if (handle == NULL) {
123 		wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
124 			   "callbacks (%s)", dbg);
125 		return NULL;
126 	}
127 
128 	if (genl_connect(handle)) {
129 		wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
130 			   "netlink (%s)", dbg);
131 		nl80211_handle_destroy(handle);
132 		return NULL;
133 	}
134 
135 	return handle;
136 }
137 
138 
139 static void nl_destroy_handles(struct nl_handle **handle)
140 {
141 	if (*handle == NULL)
142 		return;
143 	nl80211_handle_destroy(*handle);
144 	*handle = NULL;
145 }
146 
147 
148 #if __WORDSIZE == 64
149 #define ELOOP_SOCKET_INVALID	(intptr_t) 0x8888888888888889ULL
150 #else
151 #define ELOOP_SOCKET_INVALID	(intptr_t) 0x88888889ULL
152 #endif
153 
154 static void nl80211_register_eloop_read(struct nl_handle **handle,
155 					eloop_sock_handler handler,
156 					void *eloop_data, int persist)
157 {
158 #ifdef CONFIG_LIBNL20
159 	/*
160 	 * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
161 	 * by default. It is possible to hit that limit in some cases where
162 	 * operations are blocked, e.g., with a burst of Deauthentication frames
163 	 * to hostapd and STA entry deletion. Try to increase the buffer to make
164 	 * this less likely to occur.
165 	 */
166 	if (nl_socket_set_buffer_size(*handle, 262144, 0) < 0) {
167 		wpa_printf(MSG_DEBUG,
168 			   "nl80211: Could not set nl_socket RX buffer size: %s",
169 			   strerror(errno));
170 		/* continue anyway with the default (smaller) buffer */
171 	}
172 #endif /* CONFIG_LIBNL20 */
173 
174 	nl_socket_set_nonblocking(*handle);
175 	eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
176 				 eloop_data, *handle);
177 	if (!persist)
178 		*handle = (void *) (((intptr_t) *handle) ^
179 				    ELOOP_SOCKET_INVALID);
180 }
181 
182 
183 static void nl80211_destroy_eloop_handle(struct nl_handle **handle, int persist)
184 {
185 	if (!persist)
186 		*handle = (void *) (((intptr_t) *handle) ^
187 				    ELOOP_SOCKET_INVALID);
188 	eloop_unregister_read_sock(nl_socket_get_fd(*handle));
189 	nl_destroy_handles(handle);
190 }
191 
192 
193 static void nl80211_global_deinit(void *priv);
194 static void nl80211_check_global(struct nl80211_global *global);
195 
196 static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
197 static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
198 					    struct hostapd_freq_params *freq);
199 
200 static int
201 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
202 				   const u8 *set_addr, int first,
203 				   const char *driver_params);
204 static int nl80211_send_frame_cmd(struct i802_bss *bss,
205 				  unsigned int freq, unsigned int wait,
206 				  const u8 *buf, size_t buf_len, u64 *cookie,
207 				  int no_cck, int no_ack, int offchanok,
208 				  const u16 *csa_offs, size_t csa_offs_len);
209 static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
210 					       int report);
211 
212 #define IFIDX_ANY -1
213 
214 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
215 		      int ifidx_reason);
216 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
217 		      int ifidx_reason);
218 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
219 		      int ifidx_reason);
220 
221 static int nl80211_set_channel(struct i802_bss *bss,
222 			       struct hostapd_freq_params *freq, int set_chan);
223 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
224 				     int ifindex, int disabled);
225 
226 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
227 			      int reset_mode);
228 
229 static int i802_set_iface_flags(struct i802_bss *bss, int up);
230 static int nl80211_set_param(void *priv, const char *param);
231 #ifdef CONFIG_MESH
232 static int nl80211_put_mesh_config(struct nl_msg *msg,
233 				   struct wpa_driver_mesh_bss_params *params);
234 #endif /* CONFIG_MESH */
235 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
236 			     int reason);
237 
238 
239 /* Converts nl80211_chan_width to a common format */
240 enum chan_width convert2width(int width)
241 {
242 	switch (width) {
243 	case NL80211_CHAN_WIDTH_20_NOHT:
244 		return CHAN_WIDTH_20_NOHT;
245 	case NL80211_CHAN_WIDTH_20:
246 		return CHAN_WIDTH_20;
247 	case NL80211_CHAN_WIDTH_40:
248 		return CHAN_WIDTH_40;
249 	case NL80211_CHAN_WIDTH_80:
250 		return CHAN_WIDTH_80;
251 	case NL80211_CHAN_WIDTH_80P80:
252 		return CHAN_WIDTH_80P80;
253 	case NL80211_CHAN_WIDTH_160:
254 		return CHAN_WIDTH_160;
255 	}
256 	return CHAN_WIDTH_UNKNOWN;
257 }
258 
259 
260 int is_ap_interface(enum nl80211_iftype nlmode)
261 {
262 	return nlmode == NL80211_IFTYPE_AP ||
263 		nlmode == NL80211_IFTYPE_P2P_GO;
264 }
265 
266 
267 int is_sta_interface(enum nl80211_iftype nlmode)
268 {
269 	return nlmode == NL80211_IFTYPE_STATION ||
270 		nlmode == NL80211_IFTYPE_P2P_CLIENT;
271 }
272 
273 
274 static int is_p2p_net_interface(enum nl80211_iftype nlmode)
275 {
276 	return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
277 		nlmode == NL80211_IFTYPE_P2P_GO;
278 }
279 
280 
281 struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
282 				  int ifindex)
283 {
284 	struct i802_bss *bss;
285 
286 	for (bss = drv->first_bss; bss; bss = bss->next) {
287 		if (bss->ifindex == ifindex)
288 			return bss;
289 	}
290 
291 	return NULL;
292 }
293 
294 
295 static int is_mesh_interface(enum nl80211_iftype nlmode)
296 {
297 	return nlmode == NL80211_IFTYPE_MESH_POINT;
298 }
299 
300 
301 void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
302 {
303 	if (drv->associated)
304 		os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
305 	drv->associated = 0;
306 	os_memset(drv->bssid, 0, ETH_ALEN);
307 }
308 
309 
310 /* nl80211 code */
311 static int ack_handler(struct nl_msg *msg, void *arg)
312 {
313 	int *err = arg;
314 	*err = 0;
315 	return NL_STOP;
316 }
317 
318 static int finish_handler(struct nl_msg *msg, void *arg)
319 {
320 	int *ret = arg;
321 	*ret = 0;
322 	return NL_SKIP;
323 }
324 
325 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
326 			 void *arg)
327 {
328 	struct nlmsghdr *nlh = (struct nlmsghdr *) err - 1;
329 	int len = nlh->nlmsg_len;
330 	struct nlattr *attrs;
331 	struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
332 	int *ret = arg;
333 	int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh);
334 
335 	*ret = err->error;
336 
337 	if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
338 		return NL_SKIP;
339 
340 	if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
341 		ack_len += err->msg.nlmsg_len - sizeof(*nlh);
342 
343 	if (len <= ack_len)
344 		return NL_STOP;
345 
346 	attrs = (void *) ((unsigned char *) nlh + ack_len);
347 	len -= ack_len;
348 
349 	nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, len, NULL);
350 	if (tb[NLMSGERR_ATTR_MSG]) {
351 		len = strnlen((char *) nla_data(tb[NLMSGERR_ATTR_MSG]),
352 			      nla_len(tb[NLMSGERR_ATTR_MSG]));
353 		wpa_printf(MSG_ERROR, "nl80211: kernel reports: %*s",
354 			   len, (char *) nla_data(tb[NLMSGERR_ATTR_MSG]));
355 	}
356 
357 	return NL_SKIP;
358 }
359 
360 
361 static int no_seq_check(struct nl_msg *msg, void *arg)
362 {
363 	return NL_OK;
364 }
365 
366 
367 static void nl80211_nlmsg_clear(struct nl_msg *msg)
368 {
369 	/*
370 	 * Clear nlmsg data, e.g., to make sure key material is not left in
371 	 * heap memory for unnecessarily long time.
372 	 */
373 	if (msg) {
374 		struct nlmsghdr *hdr = nlmsg_hdr(msg);
375 		void *data = nlmsg_data(hdr);
376 		/*
377 		 * This would use nlmsg_datalen() or the older nlmsg_len() if
378 		 * only libnl were to maintain a stable API.. Neither will work
379 		 * with all released versions, so just calculate the length
380 		 * here.
381 		 */
382 		int len = hdr->nlmsg_len - NLMSG_HDRLEN;
383 
384 		os_memset(data, 0, len);
385 	}
386 }
387 
388 
389 static int send_and_recv(struct nl80211_global *global,
390 			 struct nl_handle *nl_handle, struct nl_msg *msg,
391 			 int (*valid_handler)(struct nl_msg *, void *),
392 			 void *valid_data)
393 {
394 	struct nl_cb *cb;
395 	int err = -ENOMEM, opt;
396 
397 	if (!msg)
398 		return -ENOMEM;
399 
400 	cb = nl_cb_clone(global->nl_cb);
401 	if (!cb)
402 		goto out;
403 
404 	/* try to set NETLINK_EXT_ACK to 1, ignoring errors */
405 	opt = 1;
406 	setsockopt(nl_socket_get_fd(nl_handle), SOL_NETLINK,
407 		   NETLINK_EXT_ACK, &opt, sizeof(opt));
408 
409 	err = nl_send_auto_complete(nl_handle, msg);
410 	if (err < 0)
411 		goto out;
412 
413 	err = 1;
414 
415 	nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
416 	nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
417 	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
418 
419 	if (valid_handler)
420 		nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
421 			  valid_handler, valid_data);
422 
423 	while (err > 0) {
424 		int res = nl_recvmsgs(nl_handle, cb);
425 		if (res < 0) {
426 			wpa_printf(MSG_INFO,
427 				   "nl80211: %s->nl_recvmsgs failed: %d",
428 				   __func__, res);
429 		}
430 	}
431  out:
432 	nl_cb_put(cb);
433 	if (!valid_handler && valid_data == (void *) -1)
434 		nl80211_nlmsg_clear(msg);
435 	nlmsg_free(msg);
436 	return err;
437 }
438 
439 
440 int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
441 		       struct nl_msg *msg,
442 		       int (*valid_handler)(struct nl_msg *, void *),
443 		       void *valid_data)
444 {
445 	return send_and_recv(drv->global, drv->global->nl, msg,
446 			     valid_handler, valid_data);
447 }
448 
449 
450 struct family_data {
451 	const char *group;
452 	int id;
453 };
454 
455 
456 static int family_handler(struct nl_msg *msg, void *arg)
457 {
458 	struct family_data *res = arg;
459 	struct nlattr *tb[CTRL_ATTR_MAX + 1];
460 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
461 	struct nlattr *mcgrp;
462 	int i;
463 
464 	nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
465 		  genlmsg_attrlen(gnlh, 0), NULL);
466 	if (!tb[CTRL_ATTR_MCAST_GROUPS])
467 		return NL_SKIP;
468 
469 	nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
470 		struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
471 		nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
472 			  nla_len(mcgrp), NULL);
473 		if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
474 		    !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
475 		    os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
476 			       res->group,
477 			       nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
478 			continue;
479 		res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
480 		break;
481 	};
482 
483 	return NL_SKIP;
484 }
485 
486 
487 static int nl_get_multicast_id(struct nl80211_global *global,
488 			       const char *family, const char *group)
489 {
490 	struct nl_msg *msg;
491 	int ret;
492 	struct family_data res = { group, -ENOENT };
493 
494 	msg = nlmsg_alloc();
495 	if (!msg)
496 		return -ENOMEM;
497 	if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
498 			 0, 0, CTRL_CMD_GETFAMILY, 0) ||
499 	    nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
500 		nlmsg_free(msg);
501 		return -1;
502 	}
503 
504 	ret = send_and_recv(global, global->nl, msg, family_handler, &res);
505 	if (ret == 0)
506 		ret = res.id;
507 	return ret;
508 }
509 
510 
511 void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
512 		   struct nl_msg *msg, int flags, uint8_t cmd)
513 {
514 	if (TEST_FAIL())
515 		return NULL;
516 	return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
517 			   0, flags, cmd, 0);
518 }
519 
520 
521 static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
522 {
523 	if (bss->wdev_id_set)
524 		return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
525 	return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
526 }
527 
528 
529 struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
530 {
531 	struct nl_msg *msg;
532 
533 	msg = nlmsg_alloc();
534 	if (!msg)
535 		return NULL;
536 
537 	if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
538 	    nl80211_set_iface_id(msg, bss) < 0) {
539 		nlmsg_free(msg);
540 		return NULL;
541 	}
542 
543 	return msg;
544 }
545 
546 
547 static struct nl_msg *
548 nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
549 		    int flags, uint8_t cmd)
550 {
551 	struct nl_msg *msg;
552 
553 	msg = nlmsg_alloc();
554 	if (!msg)
555 		return NULL;
556 
557 	if (!nl80211_cmd(drv, msg, flags, cmd) ||
558 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
559 		nlmsg_free(msg);
560 		return NULL;
561 	}
562 
563 	return msg;
564 }
565 
566 
567 struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
568 				uint8_t cmd)
569 {
570 	return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
571 }
572 
573 
574 struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
575 {
576 	return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
577 }
578 
579 
580 struct wiphy_idx_data {
581 	int wiphy_idx;
582 	enum nl80211_iftype nlmode;
583 	u8 *macaddr;
584 };
585 
586 
587 static int netdev_info_handler(struct nl_msg *msg, void *arg)
588 {
589 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
590 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
591 	struct wiphy_idx_data *info = arg;
592 
593 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
594 		  genlmsg_attrlen(gnlh, 0), NULL);
595 
596 	if (tb[NL80211_ATTR_WIPHY])
597 		info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
598 
599 	if (tb[NL80211_ATTR_IFTYPE])
600 		info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
601 
602 	if (tb[NL80211_ATTR_MAC] && info->macaddr)
603 		os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
604 			  ETH_ALEN);
605 
606 	return NL_SKIP;
607 }
608 
609 
610 int nl80211_get_wiphy_index(struct i802_bss *bss)
611 {
612 	struct nl_msg *msg;
613 	struct wiphy_idx_data data = {
614 		.wiphy_idx = -1,
615 		.macaddr = NULL,
616 	};
617 
618 	if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
619 		return -1;
620 
621 	if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
622 		return data.wiphy_idx;
623 	return -1;
624 }
625 
626 
627 static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
628 {
629 	struct nl_msg *msg;
630 	struct wiphy_idx_data data = {
631 		.nlmode = NL80211_IFTYPE_UNSPECIFIED,
632 		.macaddr = NULL,
633 	};
634 
635 	if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
636 		return NL80211_IFTYPE_UNSPECIFIED;
637 
638 	if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
639 		return data.nlmode;
640 	return NL80211_IFTYPE_UNSPECIFIED;
641 }
642 
643 
644 static int nl80211_get_macaddr(struct i802_bss *bss)
645 {
646 	struct nl_msg *msg;
647 	struct wiphy_idx_data data = {
648 		.macaddr = bss->addr,
649 	};
650 
651 	if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
652 		return -1;
653 
654 	return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
655 }
656 
657 
658 static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
659 				    struct nl80211_wiphy_data *w)
660 {
661 	struct nl_msg *msg;
662 	int ret;
663 
664 	msg = nlmsg_alloc();
665 	if (!msg)
666 		return -1;
667 
668 	if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
669 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
670 		nlmsg_free(msg);
671 		return -1;
672 	}
673 
674 	ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
675 	if (ret) {
676 		wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
677 			   "failed: ret=%d (%s)",
678 			   ret, strerror(-ret));
679 	}
680 	return ret;
681 }
682 
683 
684 static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
685 {
686 	struct nl80211_wiphy_data *w = eloop_ctx;
687 	int res;
688 
689 	wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
690 
691 	res = nl_recvmsgs(handle, w->nl_cb);
692 	if (res < 0) {
693 		wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
694 			   __func__, res);
695 	}
696 }
697 
698 
699 static int process_beacon_event(struct nl_msg *msg, void *arg)
700 {
701 	struct nl80211_wiphy_data *w = arg;
702 	struct wpa_driver_nl80211_data *drv;
703 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
704 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
705 	union wpa_event_data event;
706 
707 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
708 		  genlmsg_attrlen(gnlh, 0), NULL);
709 
710 	if (gnlh->cmd != NL80211_CMD_FRAME) {
711 		wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
712 			   gnlh->cmd);
713 		return NL_SKIP;
714 	}
715 
716 	if (!tb[NL80211_ATTR_FRAME])
717 		return NL_SKIP;
718 
719 	dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
720 			 wiphy_list) {
721 		os_memset(&event, 0, sizeof(event));
722 		event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
723 		event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
724 		wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
725 	}
726 
727 	return NL_SKIP;
728 }
729 
730 
731 static struct nl80211_wiphy_data *
732 nl80211_get_wiphy_data_ap(struct i802_bss *bss)
733 {
734 	static DEFINE_DL_LIST(nl80211_wiphys);
735 	struct nl80211_wiphy_data *w;
736 	int wiphy_idx, found = 0;
737 	struct i802_bss *tmp_bss;
738 	u8 channel;
739 
740 	if (bss->wiphy_data != NULL)
741 		return bss->wiphy_data;
742 
743 	wiphy_idx = nl80211_get_wiphy_index(bss);
744 
745 	dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
746 		if (w->wiphy_idx == wiphy_idx)
747 			goto add;
748 	}
749 
750 	/* alloc new one */
751 	w = os_zalloc(sizeof(*w));
752 	if (w == NULL)
753 		return NULL;
754 	w->wiphy_idx = wiphy_idx;
755 	dl_list_init(&w->bsss);
756 	dl_list_init(&w->drvs);
757 
758 	/* Beacon frames not supported in IEEE 802.11ad */
759 	if (ieee80211_freq_to_chan(bss->freq, &channel) !=
760 	    HOSTAPD_MODE_IEEE80211AD) {
761 		w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
762 		if (!w->nl_cb) {
763 			os_free(w);
764 			return NULL;
765 		}
766 		nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
767 			  no_seq_check, NULL);
768 		nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
769 			  process_beacon_event, w);
770 
771 		w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
772 						 "wiphy beacons");
773 		if (w->nl_beacons == NULL) {
774 			os_free(w);
775 			return NULL;
776 		}
777 
778 		if (nl80211_register_beacons(bss->drv, w)) {
779 			nl_destroy_handles(&w->nl_beacons);
780 			os_free(w);
781 			return NULL;
782 		}
783 
784 		nl80211_register_eloop_read(&w->nl_beacons,
785 					    nl80211_recv_beacons, w, 0);
786 	}
787 
788 	dl_list_add(&nl80211_wiphys, &w->list);
789 
790 add:
791 	/* drv entry for this bss already there? */
792 	dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
793 		if (tmp_bss->drv == bss->drv) {
794 			found = 1;
795 			break;
796 		}
797 	}
798 	/* if not add it */
799 	if (!found)
800 		dl_list_add(&w->drvs, &bss->drv->wiphy_list);
801 
802 	dl_list_add(&w->bsss, &bss->wiphy_list);
803 	bss->wiphy_data = w;
804 	return w;
805 }
806 
807 
808 static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
809 {
810 	struct nl80211_wiphy_data *w = bss->wiphy_data;
811 	struct i802_bss *tmp_bss;
812 	int found = 0;
813 
814 	if (w == NULL)
815 		return;
816 	bss->wiphy_data = NULL;
817 	dl_list_del(&bss->wiphy_list);
818 
819 	/* still any for this drv present? */
820 	dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
821 		if (tmp_bss->drv == bss->drv) {
822 			found = 1;
823 			break;
824 		}
825 	}
826 	/* if not remove it */
827 	if (!found)
828 		dl_list_del(&bss->drv->wiphy_list);
829 
830 	if (!dl_list_empty(&w->bsss))
831 		return;
832 
833 	if (w->nl_beacons)
834 		nl80211_destroy_eloop_handle(&w->nl_beacons, 0);
835 
836 	nl_cb_put(w->nl_cb);
837 	dl_list_del(&w->list);
838 	os_free(w);
839 }
840 
841 
842 static unsigned int nl80211_get_ifindex(void *priv)
843 {
844 	struct i802_bss *bss = priv;
845 	struct wpa_driver_nl80211_data *drv = bss->drv;
846 
847 	return drv->ifindex;
848 }
849 
850 
851 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
852 {
853 	struct i802_bss *bss = priv;
854 	struct wpa_driver_nl80211_data *drv = bss->drv;
855 	if (!drv->associated)
856 		return -1;
857 	os_memcpy(bssid, drv->bssid, ETH_ALEN);
858 	return 0;
859 }
860 
861 
862 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
863 {
864 	struct i802_bss *bss = priv;
865 	struct wpa_driver_nl80211_data *drv = bss->drv;
866 	if (!drv->associated)
867 		return -1;
868 	os_memcpy(ssid, drv->ssid, drv->ssid_len);
869 	return drv->ssid_len;
870 }
871 
872 
873 static void wpa_driver_nl80211_event_newlink(
874 	struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
875 	int ifindex, const char *ifname)
876 {
877 	union wpa_event_data event;
878 
879 	if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
880 		if (if_nametoindex(drv->first_bss->ifname) == 0) {
881 			wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
882 				   drv->first_bss->ifname);
883 			return;
884 		}
885 		if (!drv->if_removed)
886 			return;
887 		wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
888 			   drv->first_bss->ifname);
889 		drv->if_removed = 0;
890 	}
891 
892 	os_memset(&event, 0, sizeof(event));
893 	event.interface_status.ifindex = ifindex;
894 	os_strlcpy(event.interface_status.ifname, ifname,
895 		   sizeof(event.interface_status.ifname));
896 	event.interface_status.ievent = EVENT_INTERFACE_ADDED;
897 	if (drv)
898 		wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
899 	else
900 		wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
901 					    &event);
902 }
903 
904 
905 static void wpa_driver_nl80211_event_dellink(
906 	struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
907 	int ifindex, const char *ifname)
908 {
909 	union wpa_event_data event;
910 
911 	if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
912 		if (drv->if_removed) {
913 			wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
914 				   ifname);
915 			return;
916 		}
917 		wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
918 			   ifname);
919 		drv->if_removed = 1;
920 	} else {
921 		wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
922 			   ifname);
923 	}
924 
925 	os_memset(&event, 0, sizeof(event));
926 	event.interface_status.ifindex = ifindex;
927 	os_strlcpy(event.interface_status.ifname, ifname,
928 		   sizeof(event.interface_status.ifname));
929 	event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
930 	if (drv)
931 		wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
932 	else
933 		wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
934 					    &event);
935 }
936 
937 
938 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
939 					 u8 *buf, size_t len)
940 {
941 	int attrlen, rta_len;
942 	struct rtattr *attr;
943 
944 	attrlen = len;
945 	attr = (struct rtattr *) buf;
946 
947 	rta_len = RTA_ALIGN(sizeof(struct rtattr));
948 	while (RTA_OK(attr, attrlen)) {
949 		if (attr->rta_type == IFLA_IFNAME) {
950 			if (os_strcmp(((char *) attr) + rta_len,
951 				      drv->first_bss->ifname) == 0)
952 				return 1;
953 			else
954 				break;
955 		}
956 		attr = RTA_NEXT(attr, attrlen);
957 	}
958 
959 	return 0;
960 }
961 
962 
963 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
964 					  int ifindex, u8 *buf, size_t len)
965 {
966 	if (drv->ifindex == ifindex)
967 		return 1;
968 
969 	if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
970 		nl80211_check_global(drv->global);
971 		wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
972 			   "interface");
973 		if (wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL) < 0)
974 			return -1;
975 		return 1;
976 	}
977 
978 	return 0;
979 }
980 
981 
982 static struct wpa_driver_nl80211_data *
983 nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len,
984 		 int *init_failed)
985 {
986 	struct wpa_driver_nl80211_data *drv;
987 	int res;
988 
989 	if (init_failed)
990 		*init_failed = 0;
991 	dl_list_for_each(drv, &global->interfaces,
992 			 struct wpa_driver_nl80211_data, list) {
993 		res = wpa_driver_nl80211_own_ifindex(drv, idx, buf, len);
994 		if (res < 0) {
995 			wpa_printf(MSG_DEBUG,
996 				   "nl80211: Found matching own interface, but failed to complete reinitialization");
997 			if (init_failed)
998 				*init_failed = 1;
999 			return drv;
1000 		}
1001 		if (res > 0 || have_ifidx(drv, idx, IFIDX_ANY))
1002 			return drv;
1003 	}
1004 	return NULL;
1005 }
1006 
1007 
1008 static void nl80211_refresh_mac(struct wpa_driver_nl80211_data *drv,
1009 				int ifindex, int notify)
1010 {
1011 	struct i802_bss *bss;
1012 	u8 addr[ETH_ALEN];
1013 
1014 	bss = get_bss_ifindex(drv, ifindex);
1015 	if (bss &&
1016 	    linux_get_ifhwaddr(drv->global->ioctl_sock,
1017 			       bss->ifname, addr) < 0) {
1018 		wpa_printf(MSG_DEBUG,
1019 			   "nl80211: %s: failed to re-read MAC address",
1020 			   bss->ifname);
1021 	} else if (bss && os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1022 		wpa_printf(MSG_DEBUG,
1023 			   "nl80211: Own MAC address on ifindex %d (%s) changed from "
1024 			   MACSTR " to " MACSTR,
1025 			   ifindex, bss->ifname,
1026 			   MAC2STR(bss->addr), MAC2STR(addr));
1027 		os_memcpy(bss->addr, addr, ETH_ALEN);
1028 		if (notify)
1029 			wpa_supplicant_event(drv->ctx,
1030 					     EVENT_INTERFACE_MAC_CHANGED, NULL);
1031 	}
1032 }
1033 
1034 
1035 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1036 						 struct ifinfomsg *ifi,
1037 						 u8 *buf, size_t len)
1038 {
1039 	struct nl80211_global *global = ctx;
1040 	struct wpa_driver_nl80211_data *drv;
1041 	int attrlen;
1042 	struct rtattr *attr;
1043 	u32 brid = 0;
1044 	char namebuf[IFNAMSIZ];
1045 	char ifname[IFNAMSIZ + 1];
1046 	char extra[100], *pos, *end;
1047 	int init_failed;
1048 
1049 	extra[0] = '\0';
1050 	pos = extra;
1051 	end = pos + sizeof(extra);
1052 	ifname[0] = '\0';
1053 
1054 	attrlen = len;
1055 	attr = (struct rtattr *) buf;
1056 	while (RTA_OK(attr, attrlen)) {
1057 		switch (attr->rta_type) {
1058 		case IFLA_IFNAME:
1059 			if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1060 				break;
1061 			os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1062 			ifname[RTA_PAYLOAD(attr)] = '\0';
1063 			break;
1064 		case IFLA_MASTER:
1065 			brid = nla_get_u32((struct nlattr *) attr);
1066 			pos += os_snprintf(pos, end - pos, " master=%u", brid);
1067 			break;
1068 		case IFLA_WIRELESS:
1069 			pos += os_snprintf(pos, end - pos, " wext");
1070 			break;
1071 		case IFLA_OPERSTATE:
1072 			pos += os_snprintf(pos, end - pos, " operstate=%u",
1073 					   nla_get_u32((struct nlattr *) attr));
1074 			break;
1075 		case IFLA_LINKMODE:
1076 			pos += os_snprintf(pos, end - pos, " linkmode=%u",
1077 					   nla_get_u32((struct nlattr *) attr));
1078 			break;
1079 		}
1080 		attr = RTA_NEXT(attr, attrlen);
1081 	}
1082 	extra[sizeof(extra) - 1] = '\0';
1083 
1084 	wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1085 		   ifi->ifi_index, ifname, extra, ifi->ifi_family,
1086 		   ifi->ifi_flags,
1087 		   (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1088 		   (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1089 		   (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1090 		   (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1091 
1092 	drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, &init_failed);
1093 	if (!drv)
1094 		goto event_newlink;
1095 	if (init_failed)
1096 		return; /* do not update interface state */
1097 
1098 	if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
1099 		namebuf[0] = '\0';
1100 		if (if_indextoname(ifi->ifi_index, namebuf) &&
1101 		    linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
1102 			wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1103 				   "event since interface %s is up", namebuf);
1104 			drv->ignore_if_down_event = 0;
1105 			/* Re-read MAC address as it may have changed */
1106 			nl80211_refresh_mac(drv, ifi->ifi_index, 1);
1107 			return;
1108 		}
1109 		wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
1110 			   namebuf, ifname);
1111 		if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1112 			wpa_printf(MSG_DEBUG,
1113 				   "nl80211: Not the main interface (%s) - do not indicate interface down",
1114 				   drv->first_bss->ifname);
1115 		} else if (drv->ignore_if_down_event) {
1116 			wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1117 				   "event generated by mode change");
1118 			drv->ignore_if_down_event = 0;
1119 		} else {
1120 			drv->if_disabled = 1;
1121 			wpa_supplicant_event(drv->ctx,
1122 					     EVENT_INTERFACE_DISABLED, NULL);
1123 
1124 			/*
1125 			 * Try to get drv again, since it may be removed as
1126 			 * part of the EVENT_INTERFACE_DISABLED handling for
1127 			 * dynamic interfaces
1128 			 */
1129 			drv = nl80211_find_drv(global, ifi->ifi_index,
1130 					       buf, len, NULL);
1131 			if (!drv)
1132 				return;
1133 		}
1134 	}
1135 
1136 	if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
1137 		namebuf[0] = '\0';
1138 		if (if_indextoname(ifi->ifi_index, namebuf) &&
1139 		    linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
1140 			wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1141 				   "event since interface %s is down",
1142 				   namebuf);
1143 			return;
1144 		}
1145 		wpa_printf(MSG_DEBUG, "nl80211: Interface up (%s/%s)",
1146 			   namebuf, ifname);
1147 		if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1148 			wpa_printf(MSG_DEBUG,
1149 				   "nl80211: Not the main interface (%s) - do not indicate interface up",
1150 				   drv->first_bss->ifname);
1151 		} else if (if_nametoindex(drv->first_bss->ifname) == 0) {
1152 			wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1153 				   "event since interface %s does not exist",
1154 				   drv->first_bss->ifname);
1155 		} else if (drv->if_removed) {
1156 			wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1157 				   "event since interface %s is marked "
1158 				   "removed", drv->first_bss->ifname);
1159 		} else {
1160 			/* Re-read MAC address as it may have changed */
1161 			nl80211_refresh_mac(drv, ifi->ifi_index, 0);
1162 
1163 			drv->if_disabled = 0;
1164 			wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1165 					     NULL);
1166 		}
1167 	}
1168 
1169 	/*
1170 	 * Some drivers send the association event before the operup event--in
1171 	 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1172 	 * fails. This will hit us when wpa_supplicant does not need to do
1173 	 * IEEE 802.1X authentication
1174 	 */
1175 	if (drv->operstate == 1 &&
1176 	    (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
1177 	    !(ifi->ifi_flags & IFF_RUNNING)) {
1178 		wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
1179 		netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
1180 				       -1, IF_OPER_UP);
1181 	}
1182 
1183 event_newlink:
1184 	if (ifname[0])
1185 		wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1186 						 ifname);
1187 
1188 	if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
1189 		struct i802_bss *bss;
1190 
1191 		/* device has been added to bridge */
1192 		if (!if_indextoname(brid, namebuf)) {
1193 			wpa_printf(MSG_DEBUG,
1194 				   "nl80211: Could not find bridge ifname for ifindex %u",
1195 				   brid);
1196 			return;
1197 		}
1198 		wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1199 			   brid, namebuf);
1200 		add_ifidx(drv, brid, ifi->ifi_index);
1201 
1202 		for (bss = drv->first_bss; bss; bss = bss->next) {
1203 			if (os_strcmp(ifname, bss->ifname) == 0) {
1204 				os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1205 				break;
1206 			}
1207 		}
1208 	}
1209 }
1210 
1211 
1212 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1213 						 struct ifinfomsg *ifi,
1214 						 u8 *buf, size_t len)
1215 {
1216 	struct nl80211_global *global = ctx;
1217 	struct wpa_driver_nl80211_data *drv;
1218 	int attrlen;
1219 	struct rtattr *attr;
1220 	u32 brid = 0;
1221 	char ifname[IFNAMSIZ + 1];
1222 	char extra[100], *pos, *end;
1223 
1224 	extra[0] = '\0';
1225 	pos = extra;
1226 	end = pos + sizeof(extra);
1227 	ifname[0] = '\0';
1228 
1229 	attrlen = len;
1230 	attr = (struct rtattr *) buf;
1231 	while (RTA_OK(attr, attrlen)) {
1232 		switch (attr->rta_type) {
1233 		case IFLA_IFNAME:
1234 			if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1235 				break;
1236 			os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1237 			ifname[RTA_PAYLOAD(attr)] = '\0';
1238 			break;
1239 		case IFLA_MASTER:
1240 			brid = nla_get_u32((struct nlattr *) attr);
1241 			pos += os_snprintf(pos, end - pos, " master=%u", brid);
1242 			break;
1243 		case IFLA_OPERSTATE:
1244 			pos += os_snprintf(pos, end - pos, " operstate=%u",
1245 					   nla_get_u32((struct nlattr *) attr));
1246 			break;
1247 		case IFLA_LINKMODE:
1248 			pos += os_snprintf(pos, end - pos, " linkmode=%u",
1249 					   nla_get_u32((struct nlattr *) attr));
1250 			break;
1251 		}
1252 		attr = RTA_NEXT(attr, attrlen);
1253 	}
1254 	extra[sizeof(extra) - 1] = '\0';
1255 
1256 	wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1257 		   ifi->ifi_index, ifname, extra, ifi->ifi_family,
1258 		   ifi->ifi_flags,
1259 		   (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1260 		   (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1261 		   (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1262 		   (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1263 
1264 	drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, NULL);
1265 
1266 	if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
1267 		/* device has been removed from bridge */
1268 		char namebuf[IFNAMSIZ];
1269 
1270 		if (!if_indextoname(brid, namebuf)) {
1271 			wpa_printf(MSG_DEBUG,
1272 				   "nl80211: Could not find bridge ifname for ifindex %u",
1273 				   brid);
1274 		} else {
1275 			wpa_printf(MSG_DEBUG,
1276 				   "nl80211: Remove ifindex %u for bridge %s",
1277 				   brid, namebuf);
1278 		}
1279 		del_ifidx(drv, brid, ifi->ifi_index);
1280 	}
1281 
1282 	if (ifi->ifi_family != AF_BRIDGE || !brid)
1283 		wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1284 						 ifname);
1285 }
1286 
1287 
1288 struct nl80211_get_assoc_freq_arg {
1289 	struct wpa_driver_nl80211_data *drv;
1290 	unsigned int assoc_freq;
1291 	unsigned int ibss_freq;
1292 	u8 assoc_bssid[ETH_ALEN];
1293 	u8 assoc_ssid[SSID_MAX_LEN];
1294 	u8 assoc_ssid_len;
1295 };
1296 
1297 static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1298 {
1299 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
1300 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1301 	struct nlattr *bss[NL80211_BSS_MAX + 1];
1302 	static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1303 		[NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1304 		[NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1305 		[NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1306 		[NL80211_BSS_STATUS] = { .type = NLA_U32 },
1307 	};
1308 	struct nl80211_get_assoc_freq_arg *ctx = arg;
1309 	enum nl80211_bss_status status;
1310 
1311 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1312 		  genlmsg_attrlen(gnlh, 0), NULL);
1313 	if (!tb[NL80211_ATTR_BSS] ||
1314 	    nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1315 			     bss_policy) ||
1316 	    !bss[NL80211_BSS_STATUS])
1317 		return NL_SKIP;
1318 
1319 	status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1320 	if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1321 	    bss[NL80211_BSS_FREQUENCY]) {
1322 		ctx->assoc_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1323 		wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1324 			   ctx->assoc_freq);
1325 	}
1326 	if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1327 	    bss[NL80211_BSS_FREQUENCY]) {
1328 		ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1329 		wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1330 			   ctx->ibss_freq);
1331 	}
1332 	if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1333 	    bss[NL80211_BSS_BSSID]) {
1334 		os_memcpy(ctx->assoc_bssid,
1335 			  nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
1336 		wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1337 			   MACSTR, MAC2STR(ctx->assoc_bssid));
1338 	}
1339 
1340 	if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1341 	    bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1342 		const u8 *ie, *ssid;
1343 		size_t ie_len;
1344 
1345 		ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1346 		ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1347 		ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1348 		if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1349 			ctx->assoc_ssid_len = ssid[1];
1350 			os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1351 		}
1352 	}
1353 
1354 	return NL_SKIP;
1355 }
1356 
1357 
1358 int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
1359 {
1360 	struct nl_msg *msg;
1361 	int ret;
1362 	struct nl80211_get_assoc_freq_arg arg;
1363 
1364 	msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1365 	os_memset(&arg, 0, sizeof(arg));
1366 	arg.drv = drv;
1367 	ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1368 				 &arg);
1369 	if (ret == 0) {
1370 		os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1371 		return arg.assoc_ssid_len;
1372 	}
1373 	wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1374 		   ret, strerror(-ret));
1375 	return ret;
1376 }
1377 
1378 
1379 unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1380 {
1381 	struct nl_msg *msg;
1382 	int ret;
1383 	struct nl80211_get_assoc_freq_arg arg;
1384 
1385 	msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1386 	os_memset(&arg, 0, sizeof(arg));
1387 	arg.drv = drv;
1388 	ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1389 				 &arg);
1390 	if (ret == 0) {
1391 		unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1392 			arg.ibss_freq : arg.assoc_freq;
1393 		wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1394 			   "associated BSS from scan results: %u MHz", freq);
1395 		if (freq)
1396 			drv->assoc_freq = freq;
1397 		return drv->assoc_freq;
1398 	}
1399 	wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1400 		   "(%s)", ret, strerror(-ret));
1401 	return drv->assoc_freq;
1402 }
1403 
1404 
1405 static int get_link_signal(struct nl_msg *msg, void *arg)
1406 {
1407 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
1408 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1409 	struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1410 	static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1411 		[NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1412 		[NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
1413 		[NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
1414 	};
1415 	struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1416 	static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1417 		[NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1418 		[NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1419 		[NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1420 		[NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1421 	};
1422 	struct wpa_signal_info *sig_change = arg;
1423 
1424 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1425 		  genlmsg_attrlen(gnlh, 0), NULL);
1426 	if (!tb[NL80211_ATTR_STA_INFO] ||
1427 	    nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1428 			     tb[NL80211_ATTR_STA_INFO], policy))
1429 		return NL_SKIP;
1430 	if (!sinfo[NL80211_STA_INFO_SIGNAL])
1431 		return NL_SKIP;
1432 
1433 	sig_change->current_signal =
1434 		(s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1435 
1436 	if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1437 		sig_change->avg_signal =
1438 			(s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1439 	else
1440 		sig_change->avg_signal = 0;
1441 
1442 	if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1443 		sig_change->avg_beacon_signal =
1444 			(s8)
1445 			nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1446 	else
1447 		sig_change->avg_beacon_signal = 0;
1448 
1449 	if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1450 		if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1451 				     sinfo[NL80211_STA_INFO_TX_BITRATE],
1452 				     rate_policy)) {
1453 			sig_change->current_txrate = 0;
1454 		} else {
1455 			if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1456 				sig_change->current_txrate =
1457 					nla_get_u16(rinfo[
1458 					     NL80211_RATE_INFO_BITRATE]) * 100;
1459 			}
1460 		}
1461 	}
1462 
1463 	return NL_SKIP;
1464 }
1465 
1466 
1467 int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1468 			    struct wpa_signal_info *sig)
1469 {
1470 	struct nl_msg *msg;
1471 
1472 	sig->current_signal = -WPA_INVALID_NOISE;
1473 	sig->current_txrate = 0;
1474 
1475 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1476 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1477 		nlmsg_free(msg);
1478 		return -ENOBUFS;
1479 	}
1480 
1481 	return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1482 }
1483 
1484 
1485 static int get_link_noise(struct nl_msg *msg, void *arg)
1486 {
1487 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
1488 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1489 	struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1490 	static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1491 		[NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1492 		[NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1493 	};
1494 	struct wpa_signal_info *sig_change = arg;
1495 
1496 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1497 		  genlmsg_attrlen(gnlh, 0), NULL);
1498 
1499 	if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1500 		wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1501 		return NL_SKIP;
1502 	}
1503 
1504 	if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1505 			     tb[NL80211_ATTR_SURVEY_INFO],
1506 			     survey_policy)) {
1507 		wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1508 			   "attributes!");
1509 		return NL_SKIP;
1510 	}
1511 
1512 	if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1513 		return NL_SKIP;
1514 
1515 	if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1516 	    sig_change->frequency)
1517 		return NL_SKIP;
1518 
1519 	if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1520 		return NL_SKIP;
1521 
1522 	sig_change->current_noise =
1523 		(s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1524 
1525 	return NL_SKIP;
1526 }
1527 
1528 
1529 int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1530 			   struct wpa_signal_info *sig_change)
1531 {
1532 	struct nl_msg *msg;
1533 
1534 	sig_change->current_noise = WPA_INVALID_NOISE;
1535 	sig_change->frequency = drv->assoc_freq;
1536 
1537 	msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1538 	return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1539 }
1540 
1541 
1542 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1543 					     void *handle)
1544 {
1545 	struct nl_cb *cb = eloop_ctx;
1546 	int res;
1547 
1548 	wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
1549 
1550 	res = nl_recvmsgs(handle, cb);
1551 	if (res < 0) {
1552 		wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1553 			   __func__, res);
1554 	}
1555 }
1556 
1557 
1558 /**
1559  * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1560  * @priv: driver_nl80211 private data
1561  * @alpha2_arg: country to which to switch to
1562  * Returns: 0 on success, -1 on failure
1563  *
1564  * This asks nl80211 to set the regulatory domain for given
1565  * country ISO / IEC alpha2.
1566  */
1567 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1568 {
1569 	struct i802_bss *bss = priv;
1570 	struct wpa_driver_nl80211_data *drv = bss->drv;
1571 	char alpha2[3];
1572 	struct nl_msg *msg;
1573 
1574 	msg = nlmsg_alloc();
1575 	if (!msg)
1576 		return -ENOMEM;
1577 
1578 	alpha2[0] = alpha2_arg[0];
1579 	alpha2[1] = alpha2_arg[1];
1580 	alpha2[2] = '\0';
1581 
1582 	if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1583 	    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1584 		nlmsg_free(msg);
1585 		return -EINVAL;
1586 	}
1587 	if (send_and_recv_msgs(drv, msg, NULL, NULL))
1588 		return -EINVAL;
1589 	return 0;
1590 }
1591 
1592 
1593 static int nl80211_get_country(struct nl_msg *msg, void *arg)
1594 {
1595 	char *alpha2 = arg;
1596 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1597 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1598 
1599 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1600 		  genlmsg_attrlen(gnlh, 0), NULL);
1601 	if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1602 		wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1603 		return NL_SKIP;
1604 	}
1605 	os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1606 	return NL_SKIP;
1607 }
1608 
1609 
1610 static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1611 {
1612 	struct i802_bss *bss = priv;
1613 	struct wpa_driver_nl80211_data *drv = bss->drv;
1614 	struct nl_msg *msg;
1615 	int ret;
1616 
1617 	msg = nlmsg_alloc();
1618 	if (!msg)
1619 		return -ENOMEM;
1620 
1621 	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1622 	alpha2[0] = '\0';
1623 	ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1624 	if (!alpha2[0])
1625 		ret = -1;
1626 
1627 	return ret;
1628 }
1629 
1630 
1631 static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
1632 {
1633 	int ret;
1634 
1635 	global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1636 	if (global->nl_cb == NULL) {
1637 		wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1638 			   "callbacks");
1639 		return -1;
1640 	}
1641 
1642 	global->nl = nl_create_handle(global->nl_cb, "nl");
1643 	if (global->nl == NULL)
1644 		goto err;
1645 
1646 	global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1647 	if (global->nl80211_id < 0) {
1648 		wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1649 			   "found");
1650 		goto err;
1651 	}
1652 
1653 	global->nl_event = nl_create_handle(global->nl_cb, "event");
1654 	if (global->nl_event == NULL)
1655 		goto err;
1656 
1657 	ret = nl_get_multicast_id(global, "nl80211", "scan");
1658 	if (ret >= 0)
1659 		ret = nl_socket_add_membership(global->nl_event, ret);
1660 	if (ret < 0) {
1661 		wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1662 			   "membership for scan events: %d (%s)",
1663 			   ret, strerror(-ret));
1664 		goto err;
1665 	}
1666 
1667 	ret = nl_get_multicast_id(global, "nl80211", "mlme");
1668 	if (ret >= 0)
1669 		ret = nl_socket_add_membership(global->nl_event, ret);
1670 	if (ret < 0) {
1671 		wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1672 			   "membership for mlme events: %d (%s)",
1673 			   ret, strerror(-ret));
1674 		goto err;
1675 	}
1676 
1677 	ret = nl_get_multicast_id(global, "nl80211", "regulatory");
1678 	if (ret >= 0)
1679 		ret = nl_socket_add_membership(global->nl_event, ret);
1680 	if (ret < 0) {
1681 		wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1682 			   "membership for regulatory events: %d (%s)",
1683 			   ret, strerror(-ret));
1684 		/* Continue without regulatory events */
1685 	}
1686 
1687 	ret = nl_get_multicast_id(global, "nl80211", "vendor");
1688 	if (ret >= 0)
1689 		ret = nl_socket_add_membership(global->nl_event, ret);
1690 	if (ret < 0) {
1691 		wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1692 			   "membership for vendor events: %d (%s)",
1693 			   ret, strerror(-ret));
1694 		/* Continue without vendor events */
1695 	}
1696 
1697 	nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1698 		  no_seq_check, NULL);
1699 	nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1700 		  process_global_event, global);
1701 
1702 	nl80211_register_eloop_read(&global->nl_event,
1703 				    wpa_driver_nl80211_event_receive,
1704 				    global->nl_cb, 0);
1705 
1706 	return 0;
1707 
1708 err:
1709 	nl_destroy_handles(&global->nl_event);
1710 	nl_destroy_handles(&global->nl);
1711 	nl_cb_put(global->nl_cb);
1712 	global->nl_cb = NULL;
1713 	return -1;
1714 }
1715 
1716 
1717 static void nl80211_check_global(struct nl80211_global *global)
1718 {
1719 	struct nl_handle *handle;
1720 	const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1721 	int ret;
1722 	unsigned int i;
1723 
1724 	/*
1725 	 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1726 	 * and all registration having been cleared.
1727 	 */
1728 	handle = (void *) (((intptr_t) global->nl_event) ^
1729 			   ELOOP_SOCKET_INVALID);
1730 
1731 	for (i = 0; groups[i]; i++) {
1732 		ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1733 		if (ret >= 0)
1734 			ret = nl_socket_add_membership(handle, ret);
1735 		if (ret < 0) {
1736 			wpa_printf(MSG_INFO,
1737 				   "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1738 				   groups[i], ret, strerror(-ret));
1739 		}
1740 	}
1741 }
1742 
1743 
1744 static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1745 {
1746 	struct wpa_driver_nl80211_data *drv = ctx;
1747 
1748 	wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
1749 
1750 	/*
1751 	 * rtnetlink ifdown handler will report interfaces other than the P2P
1752 	 * Device interface as disabled.
1753 	 */
1754 	if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1755 		wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
1756 }
1757 
1758 
1759 static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1760 {
1761 	struct wpa_driver_nl80211_data *drv = ctx;
1762 	wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
1763 	if (i802_set_iface_flags(drv->first_bss, 1)) {
1764 		wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1765 			   "after rfkill unblock");
1766 		return;
1767 	}
1768 
1769 	if (is_p2p_net_interface(drv->nlmode))
1770 		nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1771 
1772 	/*
1773 	 * rtnetlink ifup handler will report interfaces other than the P2P
1774 	 * Device interface as enabled.
1775 	 */
1776 	if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1777 		wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
1778 }
1779 
1780 
1781 static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1782 						      void *eloop_ctx,
1783 						      void *handle)
1784 {
1785 	struct wpa_driver_nl80211_data *drv = eloop_ctx;
1786 	u8 data[2048];
1787 	struct msghdr msg;
1788 	struct iovec entry;
1789 	u8 control[512];
1790 	struct cmsghdr *cmsg;
1791 	int res, found_ee = 0, found_wifi = 0, acked = 0;
1792 	union wpa_event_data event;
1793 
1794 	memset(&msg, 0, sizeof(msg));
1795 	msg.msg_iov = &entry;
1796 	msg.msg_iovlen = 1;
1797 	entry.iov_base = data;
1798 	entry.iov_len = sizeof(data);
1799 	msg.msg_control = &control;
1800 	msg.msg_controllen = sizeof(control);
1801 
1802 	res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1803 	/* if error or not fitting 802.3 header, return */
1804 	if (res < 14)
1805 		return;
1806 
1807 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1808 	{
1809 		if (cmsg->cmsg_level == SOL_SOCKET &&
1810 		    cmsg->cmsg_type == SCM_WIFI_STATUS) {
1811 			int *ack;
1812 
1813 			found_wifi = 1;
1814 			ack = (void *)CMSG_DATA(cmsg);
1815 			acked = *ack;
1816 		}
1817 
1818 		if (cmsg->cmsg_level == SOL_PACKET &&
1819 		    cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1820 			struct sock_extended_err *err =
1821 				(struct sock_extended_err *)CMSG_DATA(cmsg);
1822 
1823 			if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1824 				found_ee = 1;
1825 		}
1826 	}
1827 
1828 	if (!found_ee || !found_wifi)
1829 		return;
1830 
1831 	memset(&event, 0, sizeof(event));
1832 	event.eapol_tx_status.dst = data;
1833 	event.eapol_tx_status.data = data + 14;
1834 	event.eapol_tx_status.data_len = res - 14;
1835 	event.eapol_tx_status.ack = acked;
1836 	wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1837 }
1838 
1839 
1840 static int nl80211_init_bss(struct i802_bss *bss)
1841 {
1842 	bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1843 	if (!bss->nl_cb)
1844 		return -1;
1845 
1846 	nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1847 		  no_seq_check, NULL);
1848 	nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1849 		  process_bss_event, bss);
1850 
1851 	return 0;
1852 }
1853 
1854 
1855 static void nl80211_destroy_bss(struct i802_bss *bss)
1856 {
1857 	nl_cb_put(bss->nl_cb);
1858 	bss->nl_cb = NULL;
1859 }
1860 
1861 
1862 static void
1863 wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
1864 {
1865 	struct rfkill_config *rcfg;
1866 
1867 	if (drv->rfkill)
1868 		return;
1869 
1870 	rcfg = os_zalloc(sizeof(*rcfg));
1871 	if (!rcfg)
1872 		return;
1873 
1874 	rcfg->ctx = drv;
1875 
1876 	/* rfkill uses netdev sysfs for initialization. However, P2P Device is
1877 	 * not associated with a netdev, so use the name of some other interface
1878 	 * sharing the same wiphy as the P2P Device interface.
1879 	 *
1880 	 * Note: This is valid, as a P2P Device interface is always dynamically
1881 	 * created and is created only once another wpa_s interface was added.
1882 	 */
1883 	if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
1884 		struct nl80211_global *global = drv->global;
1885 		struct wpa_driver_nl80211_data *tmp1;
1886 
1887 		dl_list_for_each(tmp1, &global->interfaces,
1888 				 struct wpa_driver_nl80211_data, list) {
1889 			if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
1890 			    !tmp1->rfkill)
1891 				continue;
1892 
1893 			wpa_printf(MSG_DEBUG,
1894 				   "nl80211: Use (%s) to initialize P2P Device rfkill",
1895 				   tmp1->first_bss->ifname);
1896 			os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
1897 				   sizeof(rcfg->ifname));
1898 			break;
1899 		}
1900 	} else {
1901 		os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
1902 			   sizeof(rcfg->ifname));
1903 	}
1904 
1905 	rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1906 	rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1907 	drv->rfkill = rfkill_init(rcfg);
1908 	if (!drv->rfkill) {
1909 		wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1910 		os_free(rcfg);
1911 	}
1912 }
1913 
1914 
1915 static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1916 					  void *global_priv, int hostapd,
1917 					  const u8 *set_addr,
1918 					  const char *driver_params)
1919 {
1920 	struct wpa_driver_nl80211_data *drv;
1921 	struct i802_bss *bss;
1922 
1923 	if (global_priv == NULL)
1924 		return NULL;
1925 	drv = os_zalloc(sizeof(*drv));
1926 	if (drv == NULL)
1927 		return NULL;
1928 	drv->global = global_priv;
1929 	drv->ctx = ctx;
1930 	drv->hostapd = !!hostapd;
1931 	drv->eapol_sock = -1;
1932 
1933 	/*
1934 	 * There is no driver capability flag for this, so assume it is
1935 	 * supported and disable this on first attempt to use if the driver
1936 	 * rejects the command due to missing support.
1937 	 */
1938 	drv->set_rekey_offload = 1;
1939 
1940 	drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1941 	drv->if_indices = drv->default_if_indices;
1942 	drv->if_indices_reason = drv->default_if_indices_reason;
1943 
1944 	drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1945 	if (!drv->first_bss) {
1946 		os_free(drv);
1947 		return NULL;
1948 	}
1949 	bss = drv->first_bss;
1950 	bss->drv = drv;
1951 	bss->ctx = ctx;
1952 
1953 	os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1954 	drv->monitor_ifidx = -1;
1955 	drv->monitor_sock = -1;
1956 	drv->eapol_tx_sock = -1;
1957 	drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
1958 
1959 	if (nl80211_init_bss(bss))
1960 		goto failed;
1961 
1962 	if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
1963 		goto failed;
1964 
1965 	drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1966 	if (drv->eapol_tx_sock < 0)
1967 		goto failed;
1968 
1969 	if (drv->data_tx_status) {
1970 		int enabled = 1;
1971 
1972 		if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1973 			       &enabled, sizeof(enabled)) < 0) {
1974 			wpa_printf(MSG_DEBUG,
1975 				"nl80211: wifi status sockopt failed\n");
1976 			drv->data_tx_status = 0;
1977 			if (!drv->use_monitor)
1978 				drv->capa.flags &=
1979 					~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1980 		} else {
1981 			eloop_register_read_sock(drv->eapol_tx_sock,
1982 				wpa_driver_nl80211_handle_eapol_tx_status,
1983 				drv, NULL);
1984 		}
1985 	}
1986 
1987 	if (drv->global) {
1988 		nl80211_check_global(drv->global);
1989 		dl_list_add(&drv->global->interfaces, &drv->list);
1990 		drv->in_interface_list = 1;
1991 	}
1992 
1993 	return bss;
1994 
1995 failed:
1996 	wpa_driver_nl80211_deinit(bss);
1997 	return NULL;
1998 }
1999 
2000 
2001 /**
2002  * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2003  * @ctx: context to be used when calling wpa_supplicant functions,
2004  * e.g., wpa_supplicant_event()
2005  * @ifname: interface name, e.g., wlan0
2006  * @global_priv: private driver global data from global_init()
2007  * Returns: Pointer to private data, %NULL on failure
2008  */
2009 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2010 				      void *global_priv)
2011 {
2012 	return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
2013 					   NULL);
2014 }
2015 
2016 
2017 static int nl80211_register_frame(struct i802_bss *bss,
2018 				  struct nl_handle *nl_handle,
2019 				  u16 type, const u8 *match, size_t match_len)
2020 {
2021 	struct wpa_driver_nl80211_data *drv = bss->drv;
2022 	struct nl_msg *msg;
2023 	int ret;
2024 	char buf[30];
2025 
2026 	buf[0] = '\0';
2027 	wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
2028 	wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
2029 		   type, fc2str(type), nl_handle, buf);
2030 
2031 	if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
2032 	    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
2033 	    nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
2034 		nlmsg_free(msg);
2035 		return -1;
2036 	}
2037 
2038 	ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
2039 	if (ret) {
2040 		wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
2041 			   "failed (type=%u): ret=%d (%s)",
2042 			   type, ret, strerror(-ret));
2043 		wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2044 			    match, match_len);
2045 	}
2046 	return ret;
2047 }
2048 
2049 
2050 static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
2051 {
2052 	if (bss->nl_mgmt) {
2053 		wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
2054 			   "already on! (nl_mgmt=%p)", bss->nl_mgmt);
2055 		return -1;
2056 	}
2057 
2058 	bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
2059 	if (bss->nl_mgmt == NULL)
2060 		return -1;
2061 
2062 	return 0;
2063 }
2064 
2065 
2066 static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
2067 {
2068 	nl80211_register_eloop_read(&bss->nl_mgmt,
2069 				    wpa_driver_nl80211_event_receive,
2070 				    bss->nl_cb, 0);
2071 }
2072 
2073 
2074 static int nl80211_register_action_frame(struct i802_bss *bss,
2075 					 const u8 *match, size_t match_len)
2076 {
2077 	u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
2078 	return nl80211_register_frame(bss, bss->nl_mgmt,
2079 				      type, match, match_len);
2080 }
2081 
2082 
2083 static int nl80211_init_connect_handle(struct i802_bss *bss)
2084 {
2085 	if (bss->nl_connect) {
2086 		wpa_printf(MSG_DEBUG,
2087 			   "nl80211: Connect handle already created (nl_connect=%p)",
2088 			   bss->nl_connect);
2089 		return -1;
2090 	}
2091 
2092 	bss->nl_connect = nl_create_handle(bss->nl_cb, "connect");
2093 	if (!bss->nl_connect)
2094 		return -1;
2095 	nl80211_register_eloop_read(&bss->nl_connect,
2096 				    wpa_driver_nl80211_event_receive,
2097 				    bss->nl_cb, 1);
2098 	return 0;
2099 }
2100 
2101 
2102 static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
2103 {
2104 	struct wpa_driver_nl80211_data *drv = bss->drv;
2105 	int ret = 0;
2106 
2107 	if (nl80211_alloc_mgmt_handle(bss))
2108 		return -1;
2109 	wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
2110 		   "handle %p", bss->nl_mgmt);
2111 
2112 	if (drv->nlmode == NL80211_IFTYPE_ADHOC ||
2113 	    ((drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
2114 	     !(drv->capa.flags & WPA_DRIVER_FLAGS_SME))) {
2115 		u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2116 
2117 		/* register for any AUTH message */
2118 		nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
2119 	}
2120 
2121 #ifdef CONFIG_INTERWORKING
2122 	/* QoS Map Configure */
2123 	if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
2124 		ret = -1;
2125 #endif /* CONFIG_INTERWORKING */
2126 #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
2127 	/* GAS Initial Request */
2128 	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
2129 		ret = -1;
2130 	/* GAS Initial Response */
2131 	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
2132 		ret = -1;
2133 	/* GAS Comeback Request */
2134 	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
2135 		ret = -1;
2136 	/* GAS Comeback Response */
2137 	if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
2138 		ret = -1;
2139 	/* Protected GAS Initial Request */
2140 	if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2141 		ret = -1;
2142 	/* Protected GAS Initial Response */
2143 	if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2144 		ret = -1;
2145 	/* Protected GAS Comeback Request */
2146 	if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2147 		ret = -1;
2148 	/* Protected GAS Comeback Response */
2149 	if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2150 		ret = -1;
2151 #endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
2152 #ifdef CONFIG_P2P
2153 	/* P2P Public Action */
2154 	if (nl80211_register_action_frame(bss,
2155 					  (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2156 					  6) < 0)
2157 		ret = -1;
2158 	/* P2P Action */
2159 	if (nl80211_register_action_frame(bss,
2160 					  (u8 *) "\x7f\x50\x6f\x9a\x09",
2161 					  5) < 0)
2162 		ret = -1;
2163 #endif /* CONFIG_P2P */
2164 #ifdef CONFIG_DPP
2165 	/* DPP Public Action */
2166 	if (nl80211_register_action_frame(bss,
2167 					  (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
2168 					  6) < 0)
2169 		ret = -1;
2170 #endif /* CONFIG_DPP */
2171 #ifdef CONFIG_IEEE80211W
2172 	/* SA Query Response */
2173 	if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
2174 		ret = -1;
2175 #endif /* CONFIG_IEEE80211W */
2176 #ifdef CONFIG_TDLS
2177 	if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2178 		/* TDLS Discovery Response */
2179 		if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2180 		    0)
2181 			ret = -1;
2182 	}
2183 #endif /* CONFIG_TDLS */
2184 #ifdef CONFIG_FST
2185 	/* FST Action frames */
2186 	if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2187 		ret = -1;
2188 #endif /* CONFIG_FST */
2189 
2190 	/* FT Action frames */
2191 	if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2192 		ret = -1;
2193 	else
2194 		drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2195 			WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2196 
2197 	/* WNM - BSS Transition Management Request */
2198 	if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
2199 		ret = -1;
2200 	/* WNM-Sleep Mode Response */
2201 	if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
2202 		ret = -1;
2203 #ifdef CONFIG_WNM
2204 	/* WNM - Collocated Interference Request */
2205 	if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x0b", 2) < 0)
2206 		ret = -1;
2207 #endif /* CONFIG_WNM */
2208 
2209 #ifdef CONFIG_HS20
2210 	/* WNM-Notification */
2211 	if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
2212 		ret = -1;
2213 #endif /* CONFIG_HS20 */
2214 
2215 	/* WMM-AC ADDTS Response */
2216 	if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2217 		ret = -1;
2218 
2219 	/* WMM-AC DELTS */
2220 	if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2221 		ret = -1;
2222 
2223 	/* Radio Measurement - Neighbor Report Response */
2224 	if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2225 		ret = -1;
2226 
2227 	/* Radio Measurement - Radio Measurement Request */
2228 	if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
2229 		ret = -1;
2230 
2231 	/* Radio Measurement - Link Measurement Request */
2232 	if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2233 	    (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2234 		ret = -1;
2235 
2236 	nl80211_mgmt_handle_register_eloop(bss);
2237 
2238 	return ret;
2239 }
2240 
2241 
2242 static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2243 {
2244 	int ret = 0;
2245 
2246 	if (nl80211_alloc_mgmt_handle(bss))
2247 		return -1;
2248 
2249 	wpa_printf(MSG_DEBUG,
2250 		   "nl80211: Subscribe to mgmt frames with mesh handle %p",
2251 		   bss->nl_mgmt);
2252 
2253 	/* Auth frames for mesh SAE */
2254 	if (nl80211_register_frame(bss, bss->nl_mgmt,
2255 				   (WLAN_FC_TYPE_MGMT << 2) |
2256 				   (WLAN_FC_STYPE_AUTH << 4),
2257 				   NULL, 0) < 0)
2258 		ret = -1;
2259 
2260 	/* Mesh peering open */
2261 	if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2262 		ret = -1;
2263 	/* Mesh peering confirm */
2264 	if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2265 		ret = -1;
2266 	/* Mesh peering close */
2267 	if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2268 		ret = -1;
2269 
2270 	nl80211_mgmt_handle_register_eloop(bss);
2271 
2272 	return ret;
2273 }
2274 
2275 
2276 static int nl80211_register_spurious_class3(struct i802_bss *bss)
2277 {
2278 	struct nl_msg *msg;
2279 	int ret;
2280 
2281 	msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2282 	ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
2283 	if (ret) {
2284 		wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2285 			   "failed: ret=%d (%s)",
2286 			   ret, strerror(-ret));
2287 	}
2288 	return ret;
2289 }
2290 
2291 
2292 static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2293 {
2294 	int ret = 0;
2295 
2296 	/* Public Action frames */
2297 	if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2298 		ret = -1;
2299 	/* RRM Measurement Report */
2300 	if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2301 		ret = -1;
2302 	/* RRM Link Measurement Report */
2303 	if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2304 		ret = -1;
2305 	/* RRM Neighbor Report Request */
2306 	if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2307 		ret = -1;
2308 	/* FT Action frames */
2309 	if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2310 		ret = -1;
2311 #ifdef CONFIG_IEEE80211W
2312 	/* SA Query */
2313 	if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2314 		ret = -1;
2315 #endif /* CONFIG_IEEE80211W */
2316 	/* Protected Dual of Public Action */
2317 	if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2318 		ret = -1;
2319 	/* WNM */
2320 	if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2321 		ret = -1;
2322 	/* WMM */
2323 	if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2324 		ret = -1;
2325 #ifdef CONFIG_FST
2326 	/* FST Action frames */
2327 	if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2328 		ret = -1;
2329 #endif /* CONFIG_FST */
2330 	/* Vendor-specific */
2331 	if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2332 		ret = -1;
2333 
2334 	return ret;
2335 }
2336 
2337 
2338 static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2339 {
2340 	static const int stypes[] = {
2341 		WLAN_FC_STYPE_AUTH,
2342 		WLAN_FC_STYPE_ASSOC_REQ,
2343 		WLAN_FC_STYPE_REASSOC_REQ,
2344 		WLAN_FC_STYPE_DISASSOC,
2345 		WLAN_FC_STYPE_DEAUTH,
2346 		WLAN_FC_STYPE_PROBE_REQ,
2347 /* Beacon doesn't work as mac80211 doesn't currently allow
2348  * it, but it wouldn't really be the right thing anyway as
2349  * it isn't per interface ... maybe just dump the scan
2350  * results periodically for OLBC?
2351  */
2352 		/* WLAN_FC_STYPE_BEACON, */
2353 	};
2354 	unsigned int i;
2355 
2356 	if (nl80211_alloc_mgmt_handle(bss))
2357 		return -1;
2358 	wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2359 		   "handle %p", bss->nl_mgmt);
2360 
2361 	for (i = 0; i < ARRAY_SIZE(stypes); i++) {
2362 		if (nl80211_register_frame(bss, bss->nl_mgmt,
2363 					   (WLAN_FC_TYPE_MGMT << 2) |
2364 					   (stypes[i] << 4),
2365 					   NULL, 0) < 0) {
2366 			goto out_err;
2367 		}
2368 	}
2369 
2370 	if (nl80211_action_subscribe_ap(bss))
2371 		goto out_err;
2372 
2373 	if (nl80211_register_spurious_class3(bss))
2374 		goto out_err;
2375 
2376 	nl80211_mgmt_handle_register_eloop(bss);
2377 	return 0;
2378 
2379 out_err:
2380 	nl_destroy_handles(&bss->nl_mgmt);
2381 	return -1;
2382 }
2383 
2384 
2385 static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2386 {
2387 	if (nl80211_alloc_mgmt_handle(bss))
2388 		return -1;
2389 	wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2390 		   "handle %p (device SME)", bss->nl_mgmt);
2391 
2392 	if (nl80211_action_subscribe_ap(bss))
2393 		goto out_err;
2394 
2395 	nl80211_mgmt_handle_register_eloop(bss);
2396 	return 0;
2397 
2398 out_err:
2399 	nl_destroy_handles(&bss->nl_mgmt);
2400 	return -1;
2401 }
2402 
2403 
2404 static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2405 {
2406 	if (bss->nl_mgmt == NULL)
2407 		return;
2408 	wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2409 		   "(%s)", bss->nl_mgmt, reason);
2410 	nl80211_destroy_eloop_handle(&bss->nl_mgmt, 0);
2411 
2412 	nl80211_put_wiphy_data_ap(bss);
2413 }
2414 
2415 
2416 static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2417 {
2418 	wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2419 }
2420 
2421 
2422 static void nl80211_del_p2pdev(struct i802_bss *bss)
2423 {
2424 	struct nl_msg *msg;
2425 	int ret;
2426 
2427 	msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2428 	ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
2429 
2430 	wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2431 		   bss->ifname, (long long unsigned int) bss->wdev_id,
2432 		   strerror(-ret));
2433 }
2434 
2435 
2436 static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2437 {
2438 	struct nl_msg *msg;
2439 	int ret;
2440 
2441 	msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2442 			      NL80211_CMD_STOP_P2P_DEVICE);
2443 	ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
2444 
2445 	wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2446 		   start ? "Start" : "Stop",
2447 		   bss->ifname, (long long unsigned int) bss->wdev_id,
2448 		   strerror(-ret));
2449 	return ret;
2450 }
2451 
2452 
2453 static int i802_set_iface_flags(struct i802_bss *bss, int up)
2454 {
2455 	enum nl80211_iftype nlmode;
2456 
2457 	nlmode = nl80211_get_ifmode(bss);
2458 	if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2459 		return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2460 					     bss->ifname, up);
2461 	}
2462 
2463 	/* P2P Device has start/stop which is equivalent */
2464 	return nl80211_set_p2pdev(bss, up);
2465 }
2466 
2467 
2468 #ifdef CONFIG_TESTING_OPTIONS
2469 static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2470 {
2471 	/* struct wpa_driver_nl80211_data *drv = arg; */
2472 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
2473 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2474 
2475 
2476 	wpa_printf(MSG_DEBUG,
2477 		   "nl80211: QCA vendor test command response received");
2478 
2479 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2480 		  genlmsg_attrlen(gnlh, 0), NULL);
2481 	if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2482 		wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2483 		return NL_SKIP;
2484 	}
2485 
2486 	wpa_hexdump(MSG_DEBUG,
2487 		    "nl80211: Received QCA vendor test command response",
2488 		    nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2489 		    nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2490 
2491 	return NL_SKIP;
2492 }
2493 #endif /* CONFIG_TESTING_OPTIONS */
2494 
2495 
2496 static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2497 {
2498 #ifdef CONFIG_TESTING_OPTIONS
2499 	struct nl_msg *msg;
2500 	struct nlattr *params;
2501 	int ret;
2502 
2503 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2504 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2505 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2506 			QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2507 	    !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2508 	    nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2509 		nlmsg_free(msg);
2510 		return;
2511 	}
2512 	nla_nest_end(msg, params);
2513 
2514 	ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2515 	wpa_printf(MSG_DEBUG,
2516 		   "nl80211: QCA vendor test command returned %d (%s)",
2517 		   ret, strerror(-ret));
2518 #endif /* CONFIG_TESTING_OPTIONS */
2519 }
2520 
2521 
2522 static int
2523 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
2524 				   const u8 *set_addr, int first,
2525 				   const char *driver_params)
2526 {
2527 	struct i802_bss *bss = drv->first_bss;
2528 	int send_rfkill_event = 0;
2529 	enum nl80211_iftype nlmode;
2530 
2531 	drv->ifindex = if_nametoindex(bss->ifname);
2532 	bss->ifindex = drv->ifindex;
2533 	bss->wdev_id = drv->global->if_add_wdevid;
2534 	bss->wdev_id_set = drv->global->if_add_wdevid_set;
2535 
2536 	bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2537 	bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
2538 	drv->global->if_add_wdevid_set = 0;
2539 
2540 	if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2541 		bss->static_ap = 1;
2542 
2543 	if (first &&
2544 	    nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2545 	    linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2546 		drv->start_iface_up = 1;
2547 
2548 	if (wpa_driver_nl80211_capa(drv))
2549 		return -1;
2550 
2551 	if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2552 		return -1;
2553 
2554 	wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2555 		   bss->ifname, drv->phyname);
2556 
2557 	if (set_addr &&
2558 	    (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2559 	     linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2560 				set_addr)))
2561 		return -1;
2562 
2563 	if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2564 		drv->start_mode_ap = 1;
2565 
2566 	if (drv->hostapd || bss->static_ap)
2567 		nlmode = NL80211_IFTYPE_AP;
2568 	else if (bss->if_dynamic ||
2569 		 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
2570 		nlmode = nl80211_get_ifmode(bss);
2571 	else
2572 		nlmode = NL80211_IFTYPE_STATION;
2573 
2574 	if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
2575 		wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
2576 		return -1;
2577 	}
2578 
2579 	if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2580 		nl80211_get_macaddr(bss);
2581 
2582 	wpa_driver_nl80211_drv_init_rfkill(drv);
2583 
2584 	if (!rfkill_is_blocked(drv->rfkill)) {
2585 		int ret = i802_set_iface_flags(bss, 1);
2586 		if (ret) {
2587 			wpa_printf(MSG_ERROR, "nl80211: Could not set "
2588 				   "interface '%s' UP", bss->ifname);
2589 			return ret;
2590 		}
2591 
2592 		if (is_p2p_net_interface(nlmode))
2593 			nl80211_disable_11b_rates(bss->drv,
2594 						  bss->drv->ifindex, 1);
2595 
2596 		if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2597 			return ret;
2598 	} else {
2599 		wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2600 			   "interface '%s' due to rfkill", bss->ifname);
2601 		if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2602 			drv->if_disabled = 1;
2603 
2604 		send_rfkill_event = 1;
2605 	}
2606 
2607 	if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
2608 		netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2609 				       1, IF_OPER_DORMANT);
2610 
2611 	if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2612 		if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2613 				       bss->addr))
2614 			return -1;
2615 		os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2616 	}
2617 
2618 	if (send_rfkill_event) {
2619 		eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2620 				       drv, drv->ctx);
2621 	}
2622 
2623 	if (drv->vendor_cmd_test_avail)
2624 		qca_vendor_test(drv);
2625 
2626 	nl80211_init_connect_handle(bss);
2627 
2628 	return 0;
2629 }
2630 
2631 
2632 static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss)
2633 {
2634 	struct nl_msg *msg;
2635 	struct wpa_driver_nl80211_data *drv = bss->drv;
2636 
2637 	wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2638 		   drv->ifindex);
2639 	nl80211_put_wiphy_data_ap(bss);
2640 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
2641 	return send_and_recv_msgs(drv, msg, NULL, NULL);
2642 }
2643 
2644 
2645 /**
2646  * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
2647  * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
2648  *
2649  * Shut down driver interface and processing of driver events. Free
2650  * private data buffer if one was allocated in wpa_driver_nl80211_init().
2651  */
2652 static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
2653 {
2654 	struct wpa_driver_nl80211_data *drv = bss->drv;
2655 	unsigned int i;
2656 
2657 	wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2658 		   bss->ifname, drv->disabled_11b_rates);
2659 
2660 	bss->in_deinit = 1;
2661 	if (drv->data_tx_status)
2662 		eloop_unregister_read_sock(drv->eapol_tx_sock);
2663 	if (drv->eapol_tx_sock >= 0)
2664 		close(drv->eapol_tx_sock);
2665 
2666 	if (bss->nl_preq)
2667 		wpa_driver_nl80211_probe_req_report(bss, 0);
2668 	if (bss->added_if_into_bridge) {
2669 		if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2670 				    bss->ifname) < 0)
2671 			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2672 				   "interface %s from bridge %s: %s",
2673 				   bss->ifname, bss->brname, strerror(errno));
2674 	}
2675 
2676 	if (drv->rtnl_sk)
2677 		nl80211_handle_destroy(drv->rtnl_sk);
2678 
2679 	if (bss->added_bridge) {
2680 		if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2681 					  0) < 0)
2682 			wpa_printf(MSG_INFO,
2683 				   "nl80211: Could not set bridge %s down",
2684 				   bss->brname);
2685 		if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
2686 			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2687 				   "bridge %s: %s",
2688 				   bss->brname, strerror(errno));
2689 	}
2690 
2691 	nl80211_remove_monitor_interface(drv);
2692 
2693 	if (is_ap_interface(drv->nlmode))
2694 		wpa_driver_nl80211_del_beacon(bss);
2695 
2696 	if (drv->eapol_sock >= 0) {
2697 		eloop_unregister_read_sock(drv->eapol_sock);
2698 		close(drv->eapol_sock);
2699 	}
2700 
2701 	if (drv->if_indices != drv->default_if_indices)
2702 		os_free(drv->if_indices);
2703 
2704 	if (drv->if_indices_reason != drv->default_if_indices_reason)
2705 		os_free(drv->if_indices_reason);
2706 
2707 	if (drv->disabled_11b_rates)
2708 		nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2709 
2710 	netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2711 			       IF_OPER_UP);
2712 	eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
2713 	rfkill_deinit(drv->rfkill);
2714 
2715 	eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2716 
2717 	if (!drv->start_iface_up)
2718 		(void) i802_set_iface_flags(bss, 0);
2719 
2720 	if (drv->addr_changed) {
2721 		if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2722 					  0) < 0) {
2723 			wpa_printf(MSG_DEBUG,
2724 				   "nl80211: Could not set interface down to restore permanent MAC address");
2725 		}
2726 		if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2727 				       drv->perm_addr) < 0) {
2728 			wpa_printf(MSG_DEBUG,
2729 				   "nl80211: Could not restore permanent MAC address");
2730 		}
2731 	}
2732 
2733 	if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2734 		if (!drv->hostapd || !drv->start_mode_ap)
2735 			wpa_driver_nl80211_set_mode(bss,
2736 						    NL80211_IFTYPE_STATION);
2737 		nl80211_mgmt_unsubscribe(bss, "deinit");
2738 	} else {
2739 		nl80211_mgmt_unsubscribe(bss, "deinit");
2740 		nl80211_del_p2pdev(bss);
2741 	}
2742 
2743 	if (bss->nl_connect)
2744 		nl80211_destroy_eloop_handle(&bss->nl_connect, 1);
2745 
2746 	nl80211_destroy_bss(drv->first_bss);
2747 
2748 	os_free(drv->filter_ssids);
2749 
2750 	os_free(drv->auth_ie);
2751 
2752 	if (drv->in_interface_list)
2753 		dl_list_del(&drv->list);
2754 
2755 	os_free(drv->extended_capa);
2756 	os_free(drv->extended_capa_mask);
2757 	for (i = 0; i < drv->num_iface_ext_capa; i++) {
2758 		os_free(drv->iface_ext_capa[i].ext_capa);
2759 		os_free(drv->iface_ext_capa[i].ext_capa_mask);
2760 	}
2761 	os_free(drv->first_bss);
2762 	os_free(drv);
2763 }
2764 
2765 
2766 static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2767 {
2768 	switch (alg) {
2769 	case WPA_ALG_WEP:
2770 		if (key_len == 5)
2771 			return RSN_CIPHER_SUITE_WEP40;
2772 		return RSN_CIPHER_SUITE_WEP104;
2773 	case WPA_ALG_TKIP:
2774 		return RSN_CIPHER_SUITE_TKIP;
2775 	case WPA_ALG_CCMP:
2776 		return RSN_CIPHER_SUITE_CCMP;
2777 	case WPA_ALG_GCMP:
2778 		return RSN_CIPHER_SUITE_GCMP;
2779 	case WPA_ALG_CCMP_256:
2780 		return RSN_CIPHER_SUITE_CCMP_256;
2781 	case WPA_ALG_GCMP_256:
2782 		return RSN_CIPHER_SUITE_GCMP_256;
2783 	case WPA_ALG_IGTK:
2784 		return RSN_CIPHER_SUITE_AES_128_CMAC;
2785 	case WPA_ALG_BIP_GMAC_128:
2786 		return RSN_CIPHER_SUITE_BIP_GMAC_128;
2787 	case WPA_ALG_BIP_GMAC_256:
2788 		return RSN_CIPHER_SUITE_BIP_GMAC_256;
2789 	case WPA_ALG_BIP_CMAC_256:
2790 		return RSN_CIPHER_SUITE_BIP_CMAC_256;
2791 	case WPA_ALG_SMS4:
2792 		return RSN_CIPHER_SUITE_SMS4;
2793 	case WPA_ALG_KRK:
2794 		return RSN_CIPHER_SUITE_KRK;
2795 	case WPA_ALG_NONE:
2796 	case WPA_ALG_PMK:
2797 		wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2798 			   alg);
2799 		return 0;
2800 	}
2801 
2802 	wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2803 		   alg);
2804 	return 0;
2805 }
2806 
2807 
2808 static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2809 {
2810 	switch (cipher) {
2811 	case WPA_CIPHER_CCMP_256:
2812 		return RSN_CIPHER_SUITE_CCMP_256;
2813 	case WPA_CIPHER_GCMP_256:
2814 		return RSN_CIPHER_SUITE_GCMP_256;
2815 	case WPA_CIPHER_CCMP:
2816 		return RSN_CIPHER_SUITE_CCMP;
2817 	case WPA_CIPHER_GCMP:
2818 		return RSN_CIPHER_SUITE_GCMP;
2819 	case WPA_CIPHER_TKIP:
2820 		return RSN_CIPHER_SUITE_TKIP;
2821 	case WPA_CIPHER_WEP104:
2822 		return RSN_CIPHER_SUITE_WEP104;
2823 	case WPA_CIPHER_WEP40:
2824 		return RSN_CIPHER_SUITE_WEP40;
2825 	case WPA_CIPHER_GTK_NOT_USED:
2826 		return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
2827 	}
2828 
2829 	return 0;
2830 }
2831 
2832 
2833 static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2834 				       int max_suites)
2835 {
2836 	int num_suites = 0;
2837 
2838 	if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2839 		suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
2840 	if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2841 		suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
2842 	if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2843 		suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
2844 	if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2845 		suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
2846 	if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2847 		suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
2848 	if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2849 		suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
2850 	if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2851 		suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
2852 
2853 	return num_suites;
2854 }
2855 
2856 
2857 #ifdef CONFIG_DRIVER_NL80211_QCA
2858 static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2859 				  const u8 *key, size_t key_len)
2860 {
2861 	struct nl_msg *msg;
2862 	int ret;
2863 
2864 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2865 		return 0;
2866 
2867 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2868 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2869 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2870 			QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2871 	    nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2872 		nl80211_nlmsg_clear(msg);
2873 		nlmsg_free(msg);
2874 		return -1;
2875 	}
2876 	ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2877 	if (ret) {
2878 		wpa_printf(MSG_DEBUG,
2879 			   "nl80211: Key management set key failed: ret=%d (%s)",
2880 			   ret, strerror(-ret));
2881 	}
2882 
2883 	return ret;
2884 }
2885 #endif /* CONFIG_DRIVER_NL80211_QCA */
2886 
2887 
2888 static int nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
2889 			   const u8 *key, size_t key_len,
2890 			   const u8 *addr)
2891 {
2892 	struct nl_msg *msg = NULL;
2893 	int ret;
2894 
2895 	/*
2896 	 * If the authenticator address is not set, assume it is
2897 	 * the current BSSID.
2898 	 */
2899 	if (!addr && drv->associated)
2900 		addr = drv->bssid;
2901 	else if (!addr)
2902 		return -1;
2903 
2904 	wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
2905 		   MAC2STR(addr));
2906 	wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
2907 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
2908 	if (!msg ||
2909 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
2910 	    nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
2911 		nl80211_nlmsg_clear(msg);
2912 		nlmsg_free(msg);
2913 		return -ENOBUFS;
2914 	}
2915 
2916 	ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2917 	if (ret) {
2918 		wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
2919 			   ret, strerror(-ret));
2920 	}
2921 
2922 	return ret;
2923 }
2924 
2925 
2926 static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
2927 				      enum wpa_alg alg, const u8 *addr,
2928 				      int key_idx, int set_tx,
2929 				      const u8 *seq, size_t seq_len,
2930 				      const u8 *key, size_t key_len)
2931 {
2932 	struct wpa_driver_nl80211_data *drv = bss->drv;
2933 	int ifindex;
2934 	struct nl_msg *msg = NULL;
2935 	int ret;
2936 	int tdls = 0;
2937 
2938 	/* Ignore for P2P Device */
2939 	if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2940 		return 0;
2941 
2942 	ifindex = if_nametoindex(ifname);
2943 	wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
2944 		   "set_tx=%d seq_len=%lu key_len=%lu",
2945 		   __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
2946 		   (unsigned long) seq_len, (unsigned long) key_len);
2947 #ifdef CONFIG_TDLS
2948 	if (key_idx == -1) {
2949 		key_idx = 0;
2950 		tdls = 1;
2951 	}
2952 #endif /* CONFIG_TDLS */
2953 
2954 #ifdef CONFIG_DRIVER_NL80211_QCA
2955 	if (alg == WPA_ALG_PMK &&
2956 	    (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2957 		wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2958 			   __func__);
2959 		ret = issue_key_mgmt_set_key(drv, key, key_len);
2960 		return ret;
2961 	}
2962 #endif /* CONFIG_DRIVER_NL80211_QCA */
2963 
2964 	if (alg == WPA_ALG_PMK &&
2965 	    (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2966 		return nl80211_set_pmk(drv, key, key_len, addr);
2967 
2968 	if (alg == WPA_ALG_NONE) {
2969 		msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2970 		if (!msg)
2971 			return -ENOBUFS;
2972 	} else {
2973 		u32 suite;
2974 
2975 		suite = wpa_alg_to_cipher_suite(alg, key_len);
2976 		if (!suite)
2977 			goto fail;
2978 		msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2979 		if (!msg ||
2980 		    nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
2981 		    nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
2982 			goto fail;
2983 		wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
2984 	}
2985 
2986 	if (seq && seq_len) {
2987 		if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2988 			goto fail;
2989 		wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2990 	}
2991 
2992 	if (addr && !is_broadcast_ether_addr(addr)) {
2993 		wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
2994 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2995 			goto fail;
2996 
2997 		if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2998 			wpa_printf(MSG_DEBUG, "   RSN IBSS RX GTK");
2999 			if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
3000 					NL80211_KEYTYPE_GROUP))
3001 				goto fail;
3002 		}
3003 	} else if (addr && is_broadcast_ether_addr(addr)) {
3004 		struct nlattr *types;
3005 
3006 		wpa_printf(MSG_DEBUG, "   broadcast key");
3007 
3008 		types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
3009 		if (!types ||
3010 		    nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
3011 			goto fail;
3012 		nla_nest_end(msg, types);
3013 	}
3014 	if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3015 		goto fail;
3016 
3017 	ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
3018 	if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
3019 		ret = 0;
3020 	if (ret)
3021 		wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
3022 			   ret, strerror(-ret));
3023 
3024 	/*
3025 	 * If we failed or don't need to set the default TX key (below),
3026 	 * we're done here.
3027 	 */
3028 	if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
3029 		return ret;
3030 	if (is_ap_interface(drv->nlmode) && addr &&
3031 	    !is_broadcast_ether_addr(addr))
3032 		return ret;
3033 
3034 	msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
3035 	if (!msg ||
3036 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
3037 	    nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
3038 			       alg == WPA_ALG_BIP_GMAC_128 ||
3039 			       alg == WPA_ALG_BIP_GMAC_256 ||
3040 			       alg == WPA_ALG_BIP_CMAC_256) ?
3041 			 NL80211_ATTR_KEY_DEFAULT_MGMT :
3042 			 NL80211_ATTR_KEY_DEFAULT))
3043 		goto fail;
3044 	if (addr && is_broadcast_ether_addr(addr)) {
3045 		struct nlattr *types;
3046 
3047 		types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
3048 		if (!types ||
3049 		    nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
3050 			goto fail;
3051 		nla_nest_end(msg, types);
3052 	} else if (addr) {
3053 		struct nlattr *types;
3054 
3055 		types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
3056 		if (!types ||
3057 		    nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
3058 			goto fail;
3059 		nla_nest_end(msg, types);
3060 	}
3061 
3062 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3063 	if (ret == -ENOENT)
3064 		ret = 0;
3065 	if (ret)
3066 		wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
3067 			   "err=%d %s)", ret, strerror(-ret));
3068 	return ret;
3069 
3070 fail:
3071 	nl80211_nlmsg_clear(msg);
3072 	nlmsg_free(msg);
3073 	return -ENOBUFS;
3074 }
3075 
3076 
3077 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3078 		      int key_idx, int defkey,
3079 		      const u8 *seq, size_t seq_len,
3080 		      const u8 *key, size_t key_len)
3081 {
3082 	struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
3083 	u32 suite;
3084 
3085 	if (!key_attr)
3086 		return -1;
3087 
3088 	suite = wpa_alg_to_cipher_suite(alg, key_len);
3089 	if (!suite)
3090 		return -1;
3091 
3092 	if (defkey && alg == WPA_ALG_IGTK) {
3093 		if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3094 			return -1;
3095 	} else if (defkey) {
3096 		if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3097 			return -1;
3098 	}
3099 
3100 	if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
3101 	    nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
3102 	    (seq && seq_len &&
3103 	     nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3104 	    nla_put(msg, NL80211_KEY_DATA, key_len, key))
3105 		return -1;
3106 
3107 	nla_nest_end(msg, key_attr);
3108 
3109 	return 0;
3110 }
3111 
3112 
3113 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3114 				 struct nl_msg *msg)
3115 {
3116 	int i, privacy = 0;
3117 	struct nlattr *nl_keys, *nl_key;
3118 
3119 	for (i = 0; i < 4; i++) {
3120 		if (!params->wep_key[i])
3121 			continue;
3122 		privacy = 1;
3123 		break;
3124 	}
3125 	if (params->wps == WPS_MODE_PRIVACY)
3126 		privacy = 1;
3127 	if (params->pairwise_suite &&
3128 	    params->pairwise_suite != WPA_CIPHER_NONE)
3129 		privacy = 1;
3130 
3131 	if (!privacy)
3132 		return 0;
3133 
3134 	if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3135 		return -ENOBUFS;
3136 
3137 	nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3138 	if (!nl_keys)
3139 		return -ENOBUFS;
3140 
3141 	for (i = 0; i < 4; i++) {
3142 		if (!params->wep_key[i])
3143 			continue;
3144 
3145 		nl_key = nla_nest_start(msg, i);
3146 		if (!nl_key ||
3147 		    nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3148 			    params->wep_key[i]) ||
3149 		    nla_put_u32(msg, NL80211_KEY_CIPHER,
3150 				params->wep_key_len[i] == 5 ?
3151 				RSN_CIPHER_SUITE_WEP40 :
3152 				RSN_CIPHER_SUITE_WEP104) ||
3153 		    nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3154 		    (i == params->wep_tx_keyidx &&
3155 		     nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3156 			return -ENOBUFS;
3157 
3158 		nla_nest_end(msg, nl_key);
3159 	}
3160 	nla_nest_end(msg, nl_keys);
3161 
3162 	return 0;
3163 }
3164 
3165 
3166 int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3167 			    const u8 *addr, int cmd, u16 reason_code,
3168 			    int local_state_change,
3169 			    struct nl_handle *nl_connect)
3170 {
3171 	int ret;
3172 	struct nl_msg *msg;
3173 
3174 	if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
3175 	    nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3176 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3177 	    (local_state_change &&
3178 	     nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3179 		nlmsg_free(msg);
3180 		return -1;
3181 	}
3182 
3183 	if (nl_connect)
3184 		ret = send_and_recv(drv->global, nl_connect, msg, NULL, NULL);
3185 	else
3186 		ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3187 	if (ret) {
3188 		wpa_dbg(drv->ctx, MSG_DEBUG,
3189 			"nl80211: MLME command failed: reason=%u ret=%d (%s)",
3190 			reason_code, ret, strerror(-ret));
3191 	}
3192 	return ret;
3193 }
3194 
3195 
3196 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
3197 					 int reason_code,
3198 					 struct nl_handle *nl_connect)
3199 {
3200 	int ret;
3201 	int drv_associated = drv->associated;
3202 
3203 	wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
3204 	nl80211_mark_disconnected(drv);
3205 	/* Disconnect command doesn't need BSSID - it uses cached value */
3206 	ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
3207 				      reason_code, 0, nl_connect);
3208 	/*
3209 	 * For locally generated disconnect, supplicant already generates a
3210 	 * DEAUTH event, so ignore the event from NL80211.
3211 	 */
3212 	drv->ignore_next_local_disconnect = drv_associated && (ret == 0);
3213 
3214 	return ret;
3215 }
3216 
3217 
3218 static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
3219 					     const u8 *addr, int reason_code)
3220 {
3221 	struct wpa_driver_nl80211_data *drv = bss->drv;
3222 	int ret;
3223 	int drv_associated = drv->associated;
3224 
3225 	if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3226 		nl80211_mark_disconnected(drv);
3227 		return nl80211_leave_ibss(drv, 1);
3228 	}
3229 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
3230 		struct nl_handle *nl_connect = NULL;
3231 
3232 		if (bss->use_nl_connect)
3233 			nl_connect = bss->nl_connect;
3234 		return wpa_driver_nl80211_disconnect(drv, reason_code,
3235 						     nl_connect);
3236 	}
3237 	wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3238 		   __func__, MAC2STR(addr), reason_code);
3239 	nl80211_mark_disconnected(drv);
3240 	ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
3241 				      reason_code, 0, NULL);
3242 	/*
3243 	 * For locally generated deauthenticate, supplicant already generates a
3244 	 * DEAUTH event, so ignore the event from NL80211.
3245 	 */
3246 	drv->ignore_next_local_deauth = drv_associated && (ret == 0);
3247 
3248 	return ret;
3249 }
3250 
3251 
3252 static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3253 				     struct wpa_driver_auth_params *params)
3254 {
3255 	int i;
3256 
3257 	drv->auth_freq = params->freq;
3258 	drv->auth_alg = params->auth_alg;
3259 	drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3260 	drv->auth_local_state_change = params->local_state_change;
3261 	drv->auth_p2p = params->p2p;
3262 
3263 	if (params->bssid)
3264 		os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3265 	else
3266 		os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3267 
3268 	if (params->ssid) {
3269 		os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3270 		drv->auth_ssid_len = params->ssid_len;
3271 	} else
3272 		drv->auth_ssid_len = 0;
3273 
3274 
3275 	os_free(drv->auth_ie);
3276 	drv->auth_ie = NULL;
3277 	drv->auth_ie_len = 0;
3278 	if (params->ie) {
3279 		drv->auth_ie = os_malloc(params->ie_len);
3280 		if (drv->auth_ie) {
3281 			os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3282 			drv->auth_ie_len = params->ie_len;
3283 		}
3284 	}
3285 
3286 	for (i = 0; i < 4; i++) {
3287 		if (params->wep_key[i] && params->wep_key_len[i] &&
3288 		    params->wep_key_len[i] <= 16) {
3289 			os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3290 				  params->wep_key_len[i]);
3291 			drv->auth_wep_key_len[i] = params->wep_key_len[i];
3292 		} else
3293 			drv->auth_wep_key_len[i] = 0;
3294 	}
3295 }
3296 
3297 
3298 static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3299 {
3300 	struct wpa_driver_nl80211_data *drv = bss->drv;
3301 
3302 	if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3303 		return;
3304 
3305 	/*
3306 	 * Looks like we failed to unmask 11b rates previously. This could
3307 	 * happen, e.g., if the interface was down at the point in time when a
3308 	 * P2P group was terminated.
3309 	 */
3310 	wpa_printf(MSG_DEBUG,
3311 		   "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3312 		   bss->ifname);
3313 	nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3314 }
3315 
3316 
3317 static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
3318 {
3319 	if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
3320 		return NL80211_AUTHTYPE_OPEN_SYSTEM;
3321 	if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
3322 		return NL80211_AUTHTYPE_SHARED_KEY;
3323 	if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
3324 		return NL80211_AUTHTYPE_NETWORK_EAP;
3325 	if (wpa_auth_alg & WPA_AUTH_ALG_FT)
3326 		return NL80211_AUTHTYPE_FT;
3327 	if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
3328 		return NL80211_AUTHTYPE_SAE;
3329 	if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
3330 		return NL80211_AUTHTYPE_FILS_SK;
3331 	if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
3332 		return NL80211_AUTHTYPE_FILS_SK_PFS;
3333 
3334 	return NL80211_AUTHTYPE_MAX;
3335 }
3336 
3337 
3338 static int wpa_driver_nl80211_authenticate(
3339 	struct i802_bss *bss, struct wpa_driver_auth_params *params)
3340 {
3341 	struct wpa_driver_nl80211_data *drv = bss->drv;
3342 	int ret = -1, i;
3343 	struct nl_msg *msg;
3344 	enum nl80211_auth_type type;
3345 	enum nl80211_iftype nlmode;
3346 	int count = 0;
3347 	int is_retry;
3348 
3349 	nl80211_unmask_11b_rates(bss);
3350 
3351 	is_retry = drv->retry_auth;
3352 	drv->retry_auth = 0;
3353 	drv->ignore_deauth_event = 0;
3354 
3355 	nl80211_mark_disconnected(drv);
3356 	os_memset(drv->auth_bssid, 0, ETH_ALEN);
3357 	if (params->bssid)
3358 		os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3359 	else
3360 		os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
3361 	/* FIX: IBSS mode */
3362 	nlmode = params->p2p ?
3363 		NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3364 	if (drv->nlmode != nlmode &&
3365 	    wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
3366 		return -1;
3367 
3368 retry:
3369 	wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3370 		   drv->ifindex);
3371 
3372 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3373 	if (!msg)
3374 		goto fail;
3375 
3376 	for (i = 0; i < 4; i++) {
3377 		if (!params->wep_key[i])
3378 			continue;
3379 		wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
3380 					   NULL, i,
3381 					   i == params->wep_tx_keyidx, NULL, 0,
3382 					   params->wep_key[i],
3383 					   params->wep_key_len[i]);
3384 		if (params->wep_tx_keyidx != i)
3385 			continue;
3386 		if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
3387 			       params->wep_key[i], params->wep_key_len[i]))
3388 			goto fail;
3389 	}
3390 
3391 	if (params->bssid) {
3392 		wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
3393 			   MAC2STR(params->bssid));
3394 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3395 			goto fail;
3396 	}
3397 	if (params->freq) {
3398 		wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
3399 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3400 			goto fail;
3401 	}
3402 	if (params->ssid) {
3403 		wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
3404 				  params->ssid, params->ssid_len);
3405 		if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3406 			    params->ssid))
3407 			goto fail;
3408 	}
3409 	wpa_hexdump(MSG_DEBUG, "  * IEs", params->ie, params->ie_len);
3410 	if (params->ie &&
3411 	    nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3412 		goto fail;
3413 	if (params->auth_data) {
3414 		wpa_hexdump(MSG_DEBUG, "  * auth_data", params->auth_data,
3415 			    params->auth_data_len);
3416 		if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
3417 			    params->auth_data))
3418 			goto fail;
3419 	}
3420 	type = get_nl_auth_type(params->auth_alg);
3421 	wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
3422 	if (type == NL80211_AUTHTYPE_MAX ||
3423 	    nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
3424 		goto fail;
3425 	if (params->local_state_change) {
3426 		wpa_printf(MSG_DEBUG, "  * Local state change only");
3427 		if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3428 			goto fail;
3429 	}
3430 
3431 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3432 	msg = NULL;
3433 	if (ret) {
3434 		wpa_dbg(drv->ctx, MSG_DEBUG,
3435 			"nl80211: MLME command failed (auth): count=%d ret=%d (%s)",
3436 			count, ret, strerror(-ret));
3437 		count++;
3438 		if ((ret == -EALREADY || ret == -EEXIST) && count == 1 &&
3439 		    params->bssid && !params->local_state_change) {
3440 			/*
3441 			 * mac80211 does not currently accept new
3442 			 * authentication if we are already authenticated. As a
3443 			 * workaround, force deauthentication and try again.
3444 			 */
3445 			wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3446 				   "after forced deauthentication");
3447 			drv->ignore_deauth_event = 1;
3448 			wpa_driver_nl80211_deauthenticate(
3449 				bss, params->bssid,
3450 				WLAN_REASON_PREV_AUTH_NOT_VALID);
3451 			nlmsg_free(msg);
3452 			goto retry;
3453 		}
3454 
3455 		if (ret == -ENOENT && params->freq && !is_retry) {
3456 			/*
3457 			 * cfg80211 has likely expired the BSS entry even
3458 			 * though it was previously available in our internal
3459 			 * BSS table. To recover quickly, start a single
3460 			 * channel scan on the specified channel.
3461 			 */
3462 			struct wpa_driver_scan_params scan;
3463 			int freqs[2];
3464 
3465 			os_memset(&scan, 0, sizeof(scan));
3466 			scan.num_ssids = 1;
3467 			if (params->ssid) {
3468 				scan.ssids[0].ssid = params->ssid;
3469 				scan.ssids[0].ssid_len = params->ssid_len;
3470 			}
3471 			freqs[0] = params->freq;
3472 			freqs[1] = 0;
3473 			scan.freqs = freqs;
3474 			wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3475 				   "channel scan to refresh cfg80211 BSS "
3476 				   "entry");
3477 			ret = wpa_driver_nl80211_scan(bss, &scan);
3478 			if (ret == 0) {
3479 				nl80211_copy_auth_params(drv, params);
3480 				drv->scan_for_auth = 1;
3481 			}
3482 		} else if (is_retry) {
3483 			/*
3484 			 * Need to indicate this with an event since the return
3485 			 * value from the retry is not delivered to core code.
3486 			 */
3487 			union wpa_event_data event;
3488 			wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3489 				   "failed");
3490 			os_memset(&event, 0, sizeof(event));
3491 			os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3492 				  ETH_ALEN);
3493 			wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3494 					     &event);
3495 		}
3496 	} else {
3497 		wpa_printf(MSG_DEBUG,
3498 			   "nl80211: Authentication request send successfully");
3499 	}
3500 
3501 fail:
3502 	nlmsg_free(msg);
3503 	return ret;
3504 }
3505 
3506 
3507 int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
3508 {
3509 	struct wpa_driver_auth_params params;
3510 	struct i802_bss *bss = drv->first_bss;
3511 	int i;
3512 
3513 	wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3514 
3515 	os_memset(&params, 0, sizeof(params));
3516 	params.freq = drv->auth_freq;
3517 	params.auth_alg = drv->auth_alg;
3518 	params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3519 	params.local_state_change = drv->auth_local_state_change;
3520 	params.p2p = drv->auth_p2p;
3521 
3522 	if (!is_zero_ether_addr(drv->auth_bssid_))
3523 		params.bssid = drv->auth_bssid_;
3524 
3525 	if (drv->auth_ssid_len) {
3526 		params.ssid = drv->auth_ssid;
3527 		params.ssid_len = drv->auth_ssid_len;
3528 	}
3529 
3530 	params.ie = drv->auth_ie;
3531 	params.ie_len = drv->auth_ie_len;
3532 
3533 	for (i = 0; i < 4; i++) {
3534 		if (drv->auth_wep_key_len[i]) {
3535 			params.wep_key[i] = drv->auth_wep_key[i];
3536 			params.wep_key_len[i] = drv->auth_wep_key_len[i];
3537 		}
3538 	}
3539 
3540 	drv->retry_auth = 1;
3541 	return wpa_driver_nl80211_authenticate(bss, &params);
3542 }
3543 
3544 
3545 static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3546 					 const void *data, size_t len,
3547 					 int encrypt, int noack,
3548 					 unsigned int freq, int no_cck,
3549 					 int offchanok, unsigned int wait_time,
3550 					 const u16 *csa_offs,
3551 					 size_t csa_offs_len)
3552 {
3553 	struct wpa_driver_nl80211_data *drv = bss->drv;
3554 	u64 cookie;
3555 	int res;
3556 
3557 	if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3558 		freq = nl80211_get_assoc_freq(drv);
3559 		wpa_printf(MSG_DEBUG,
3560 			   "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3561 			   freq);
3562 	}
3563 	if (freq == 0) {
3564 		wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3565 			   bss->freq);
3566 		freq = bss->freq;
3567 	}
3568 
3569 	if (drv->use_monitor) {
3570 		wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
3571 			   freq, bss->freq);
3572 		return nl80211_send_monitor(drv, data, len, encrypt, noack);
3573 	}
3574 
3575 	wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
3576 	res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
3577 				     &cookie, no_cck, noack, offchanok,
3578 				     csa_offs, csa_offs_len);
3579 	if (res == 0 && !noack) {
3580 		const struct ieee80211_mgmt *mgmt;
3581 		u16 fc;
3582 
3583 		mgmt = (const struct ieee80211_mgmt *) data;
3584 		fc = le_to_host16(mgmt->frame_control);
3585 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3586 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3587 			wpa_printf(MSG_MSGDUMP,
3588 				   "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3589 				   (long long unsigned int)
3590 				   drv->send_action_cookie,
3591 				   (long long unsigned int) cookie);
3592 			drv->send_action_cookie = cookie;
3593 		}
3594 	}
3595 
3596 	return res;
3597 }
3598 
3599 
3600 static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3601 					size_t data_len, int noack,
3602 					unsigned int freq, int no_cck,
3603 					int offchanok,
3604 					unsigned int wait_time,
3605 					const u16 *csa_offs,
3606 					size_t csa_offs_len)
3607 {
3608 	struct wpa_driver_nl80211_data *drv = bss->drv;
3609 	struct ieee80211_mgmt *mgmt;
3610 	int encrypt = 1;
3611 	u16 fc;
3612 
3613 	mgmt = (struct ieee80211_mgmt *) data;
3614 	fc = le_to_host16(mgmt->frame_control);
3615 	wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3616 		   " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3617 		   MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3618 		   fc, fc2str(fc), drv->nlmode);
3619 
3620 	if ((is_sta_interface(drv->nlmode) ||
3621 	     drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
3622 	    WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3623 	    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3624 		/*
3625 		 * The use of last_mgmt_freq is a bit of a hack,
3626 		 * but it works due to the single-threaded nature
3627 		 * of wpa_supplicant.
3628 		 */
3629 		if (freq == 0) {
3630 			wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3631 				   drv->last_mgmt_freq);
3632 			freq = drv->last_mgmt_freq;
3633 		}
3634 		return nl80211_send_frame_cmd(bss, freq, 0,
3635 					      data, data_len, NULL, 1, noack,
3636 					      1, csa_offs, csa_offs_len);
3637 	}
3638 
3639 	if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
3640 		if (freq == 0) {
3641 			wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3642 				   bss->freq);
3643 			freq = bss->freq;
3644 		}
3645 		return nl80211_send_frame_cmd(bss, freq,
3646 					      (int) freq == bss->freq ? 0 :
3647 					      wait_time,
3648 					      data, data_len,
3649 					      &drv->send_action_cookie,
3650 					      no_cck, noack, offchanok,
3651 					      csa_offs, csa_offs_len);
3652 	}
3653 
3654 	if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3655 	    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3656 		/*
3657 		 * Only one of the authentication frame types is encrypted.
3658 		 * In order for static WEP encryption to work properly (i.e.,
3659 		 * to not encrypt the frame), we need to tell mac80211 about
3660 		 * the frames that must not be encrypted.
3661 		 */
3662 		u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3663 		u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3664 		if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3665 			encrypt = 0;
3666 	}
3667 
3668 	wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
3669 	return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
3670 					     noack, freq, no_cck, offchanok,
3671 					     wait_time, csa_offs,
3672 					     csa_offs_len);
3673 }
3674 
3675 
3676 static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3677 {
3678 	u8 rates[NL80211_MAX_SUPP_RATES];
3679 	u8 rates_len = 0;
3680 	int i;
3681 
3682 	if (!basic_rates)
3683 		return 0;
3684 
3685 	for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3686 		rates[rates_len++] = basic_rates[i] / 5;
3687 
3688 	return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3689 }
3690 
3691 
3692 static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3693 			   int slot, int ht_opmode, int ap_isolate,
3694 			   const int *basic_rates)
3695 {
3696 	struct wpa_driver_nl80211_data *drv = bss->drv;
3697 	struct nl_msg *msg;
3698 
3699 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3700 	    (cts >= 0 &&
3701 	     nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3702 	    (preamble >= 0 &&
3703 	     nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3704 	    (slot >= 0 &&
3705 	     nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3706 	    (ht_opmode >= 0 &&
3707 	     nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3708 	    (ap_isolate >= 0 &&
3709 	     nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3710 	    nl80211_put_basic_rates(msg, basic_rates)) {
3711 		nlmsg_free(msg);
3712 		return -ENOBUFS;
3713 	}
3714 
3715 	return send_and_recv_msgs(drv, msg, NULL, NULL);
3716 }
3717 
3718 
3719 static int wpa_driver_nl80211_set_acl(void *priv,
3720 				      struct hostapd_acl_params *params)
3721 {
3722 	struct i802_bss *bss = priv;
3723 	struct wpa_driver_nl80211_data *drv = bss->drv;
3724 	struct nl_msg *msg;
3725 	struct nl_msg *acl;
3726 	unsigned int i;
3727 	int ret;
3728 
3729 	if (!(drv->capa.max_acl_mac_addrs))
3730 		return -ENOTSUP;
3731 
3732 	if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3733 		return -ENOTSUP;
3734 
3735 	wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3736 		   params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3737 
3738 	acl = nlmsg_alloc();
3739 	if (!acl)
3740 		return -ENOMEM;
3741 	for (i = 0; i < params->num_mac_acl; i++) {
3742 		if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3743 			nlmsg_free(acl);
3744 			return -ENOMEM;
3745 		}
3746 	}
3747 
3748 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3749 	    nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3750 			NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3751 			NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3752 	    nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3753 		nlmsg_free(msg);
3754 		nlmsg_free(acl);
3755 		return -ENOMEM;
3756 	}
3757 	nlmsg_free(acl);
3758 
3759 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3760 	if (ret) {
3761 		wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3762 			   ret, strerror(-ret));
3763 	}
3764 
3765 	return ret;
3766 }
3767 
3768 
3769 static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3770 {
3771 	if (beacon_int > 0) {
3772 		wpa_printf(MSG_DEBUG, "  * beacon_int=%d", beacon_int);
3773 		return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3774 				   beacon_int);
3775 	}
3776 
3777 	return 0;
3778 }
3779 
3780 
3781 static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
3782 {
3783 	if (dtim_period > 0) {
3784 		wpa_printf(MSG_DEBUG, "  * dtim_period=%d", dtim_period);
3785 		return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
3786 	}
3787 
3788 	return 0;
3789 }
3790 
3791 
3792 #ifdef CONFIG_MESH
3793 static int nl80211_set_mesh_config(void *priv,
3794 				   struct wpa_driver_mesh_bss_params *params)
3795 {
3796 	struct i802_bss *bss = priv;
3797 	struct wpa_driver_nl80211_data *drv = bss->drv;
3798 	struct nl_msg *msg;
3799 	int ret;
3800 
3801 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
3802 	if (!msg)
3803 		return -1;
3804 
3805 	ret = nl80211_put_mesh_config(msg, params);
3806 	if (ret < 0) {
3807 		nlmsg_free(msg);
3808 		return ret;
3809 	}
3810 
3811 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3812 	if (ret) {
3813 		wpa_printf(MSG_ERROR,
3814 			   "nl80211: Mesh config set failed: %d (%s)",
3815 			   ret, strerror(-ret));
3816 		return ret;
3817 	}
3818 	return 0;
3819 }
3820 #endif /* CONFIG_MESH */
3821 
3822 
3823 static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
3824 				   struct wpa_driver_ap_params *params)
3825 {
3826 	struct nlattr *bands, *band;
3827 	struct nl80211_txrate_vht vht_rate;
3828 
3829 	if (!params->freq ||
3830 	    (params->beacon_rate == 0 &&
3831 	     params->rate_type == BEACON_RATE_LEGACY))
3832 		return 0;
3833 
3834 	bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
3835 	if (!bands)
3836 		return -1;
3837 
3838 	switch (params->freq->mode) {
3839 	case HOSTAPD_MODE_IEEE80211B:
3840 	case HOSTAPD_MODE_IEEE80211G:
3841 		band = nla_nest_start(msg, NL80211_BAND_2GHZ);
3842 		break;
3843 	case HOSTAPD_MODE_IEEE80211A:
3844 		band = nla_nest_start(msg, NL80211_BAND_5GHZ);
3845 		break;
3846 	case HOSTAPD_MODE_IEEE80211AD:
3847 		band = nla_nest_start(msg, NL80211_BAND_60GHZ);
3848 		break;
3849 	default:
3850 		return 0;
3851 	}
3852 
3853 	if (!band)
3854 		return -1;
3855 
3856 	os_memset(&vht_rate, 0, sizeof(vht_rate));
3857 
3858 	switch (params->rate_type) {
3859 	case BEACON_RATE_LEGACY:
3860 		if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
3861 			wpa_printf(MSG_INFO,
3862 				   "nl80211: Driver does not support setting Beacon frame rate (legacy)");
3863 			return -1;
3864 		}
3865 
3866 		if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
3867 			       (u8) params->beacon_rate / 5) ||
3868 		    nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
3869 		    (params->freq->vht_enabled &&
3870 		     nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3871 			     &vht_rate)))
3872 			return -1;
3873 
3874 		wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
3875 			   params->beacon_rate);
3876 		break;
3877 	case BEACON_RATE_HT:
3878 		if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
3879 			wpa_printf(MSG_INFO,
3880 				   "nl80211: Driver does not support setting Beacon frame rate (HT)");
3881 			return -1;
3882 		}
3883 		if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
3884 		    nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
3885 		    (params->freq->vht_enabled &&
3886 		     nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3887 			     &vht_rate)))
3888 			return -1;
3889 		wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
3890 			   params->beacon_rate);
3891 		break;
3892 	case BEACON_RATE_VHT:
3893 		if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
3894 			wpa_printf(MSG_INFO,
3895 				   "nl80211: Driver does not support setting Beacon frame rate (VHT)");
3896 			return -1;
3897 		}
3898 		vht_rate.mcs[0] = BIT(params->beacon_rate);
3899 		if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
3900 			return -1;
3901 		if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
3902 			return -1;
3903 		if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3904 			    &vht_rate))
3905 			return -1;
3906 		wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
3907 			   params->beacon_rate);
3908 		break;
3909 	}
3910 
3911 	nla_nest_end(msg, band);
3912 	nla_nest_end(msg, bands);
3913 
3914 	return 0;
3915 }
3916 
3917 
3918 static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
3919 					    int multicast_to_unicast)
3920 {
3921 	struct wpa_driver_nl80211_data *drv = bss->drv;
3922 	struct nl_msg *msg;
3923 	int ret;
3924 
3925 	msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
3926 	if (!msg ||
3927 	    (multicast_to_unicast &&
3928 	     nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
3929 		wpa_printf(MSG_ERROR,
3930 			   "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
3931 			   bss->ifname);
3932 		nlmsg_free(msg);
3933 		return -ENOBUFS;
3934 	}
3935 
3936 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3937 
3938 	switch (ret) {
3939 	case 0:
3940 		wpa_printf(MSG_DEBUG,
3941 			   "nl80211: multicast to unicast %s on interface %s",
3942 			   multicast_to_unicast ? "enabled" : "disabled",
3943 			   bss->ifname);
3944 		break;
3945 	case -EOPNOTSUPP:
3946 		if (!multicast_to_unicast)
3947 			break;
3948 		wpa_printf(MSG_INFO,
3949 			   "nl80211: multicast to unicast not supported on interface %s",
3950 			   bss->ifname);
3951 		break;
3952 	default:
3953 		wpa_printf(MSG_ERROR,
3954 			   "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
3955 			   multicast_to_unicast ? "enabling" : "disabling",
3956 			   ret, strerror(-ret), bss->ifname);
3957 		break;
3958 	}
3959 
3960 	return ret;
3961 }
3962 
3963 
3964 static int wpa_driver_nl80211_set_ap(void *priv,
3965 				     struct wpa_driver_ap_params *params)
3966 {
3967 	struct i802_bss *bss = priv;
3968 	struct wpa_driver_nl80211_data *drv = bss->drv;
3969 	struct nl_msg *msg;
3970 	u8 cmd = NL80211_CMD_NEW_BEACON;
3971 	int ret;
3972 	int beacon_set;
3973 	int num_suites;
3974 	int smps_mode;
3975 	u32 suites[10], suite;
3976 	u32 ver;
3977 #ifdef CONFIG_MESH
3978 	struct wpa_driver_mesh_bss_params mesh_params;
3979 #endif /* CONFIG_MESH */
3980 
3981 	beacon_set = params->reenable ? 0 : bss->beacon_set;
3982 
3983 	wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3984 		   beacon_set);
3985 	if (beacon_set)
3986 		cmd = NL80211_CMD_SET_BEACON;
3987 	else if (!drv->device_ap_sme && !drv->use_monitor &&
3988 		 !nl80211_get_wiphy_data_ap(bss))
3989 		return -ENOBUFS;
3990 
3991 	wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3992 		    params->head, params->head_len);
3993 	wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3994 		    params->tail, params->tail_len);
3995 	wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
3996 	wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
3997 	wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
3998 	wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
3999 	wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
4000 	wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
4001 			  params->ssid, params->ssid_len);
4002 	if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
4003 	    nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
4004 		    params->head) ||
4005 	    nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
4006 		    params->tail) ||
4007 	    nl80211_put_beacon_int(msg, params->beacon_int) ||
4008 	    nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
4009 	    nl80211_put_dtim_period(msg, params->dtim_period) ||
4010 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4011 		goto fail;
4012 	if (params->proberesp && params->proberesp_len) {
4013 		wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
4014 			    params->proberesp, params->proberesp_len);
4015 		if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
4016 			    params->proberesp))
4017 			goto fail;
4018 	}
4019 	switch (params->hide_ssid) {
4020 	case NO_SSID_HIDING:
4021 		wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
4022 		if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4023 				NL80211_HIDDEN_SSID_NOT_IN_USE))
4024 			goto fail;
4025 		break;
4026 	case HIDDEN_SSID_ZERO_LEN:
4027 		wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
4028 		if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4029 				NL80211_HIDDEN_SSID_ZERO_LEN))
4030 			goto fail;
4031 		break;
4032 	case HIDDEN_SSID_ZERO_CONTENTS:
4033 		wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
4034 		if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4035 				NL80211_HIDDEN_SSID_ZERO_CONTENTS))
4036 			goto fail;
4037 		break;
4038 	}
4039 	wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
4040 	if (params->privacy &&
4041 	    nla_put_flag(msg, NL80211_ATTR_PRIVACY))
4042 		goto fail;
4043 	wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
4044 	if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
4045 	    (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
4046 		/* Leave out the attribute */
4047 	} else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
4048 		if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
4049 				NL80211_AUTHTYPE_SHARED_KEY))
4050 			goto fail;
4051 	} else {
4052 		if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
4053 				NL80211_AUTHTYPE_OPEN_SYSTEM))
4054 			goto fail;
4055 	}
4056 
4057 	wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
4058 	ver = 0;
4059 	if (params->wpa_version & WPA_PROTO_WPA)
4060 		ver |= NL80211_WPA_VERSION_1;
4061 	if (params->wpa_version & WPA_PROTO_RSN)
4062 		ver |= NL80211_WPA_VERSION_2;
4063 	if (ver &&
4064 	    nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4065 		goto fail;
4066 
4067 	wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
4068 		   params->key_mgmt_suites);
4069 	num_suites = 0;
4070 	if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
4071 		suites[num_suites++] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
4072 	if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
4073 		suites[num_suites++] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
4074 	if (num_suites &&
4075 	    nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
4076 		    suites))
4077 		goto fail;
4078 
4079 	if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
4080 	    (!params->pairwise_ciphers ||
4081 	     params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
4082 	    (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
4083 	     nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
4084 		goto fail;
4085 
4086 	wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
4087 		   params->pairwise_ciphers);
4088 	num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
4089 						 suites, ARRAY_SIZE(suites));
4090 	if (num_suites &&
4091 	    nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4092 		    num_suites * sizeof(u32), suites))
4093 		goto fail;
4094 
4095 	wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
4096 		   params->group_cipher);
4097 	suite = wpa_cipher_to_cipher_suite(params->group_cipher);
4098 	if (suite &&
4099 	    nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
4100 		goto fail;
4101 
4102 	if (params->ht_opmode != -1) {
4103 		switch (params->smps_mode) {
4104 		case HT_CAP_INFO_SMPS_DYNAMIC:
4105 			wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
4106 			smps_mode = NL80211_SMPS_DYNAMIC;
4107 			break;
4108 		case HT_CAP_INFO_SMPS_STATIC:
4109 			wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
4110 			smps_mode = NL80211_SMPS_STATIC;
4111 			break;
4112 		default:
4113 			/* invalid - fallback to smps off */
4114 		case HT_CAP_INFO_SMPS_DISABLED:
4115 			wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
4116 			smps_mode = NL80211_SMPS_OFF;
4117 			break;
4118 		}
4119 		if (nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
4120 			goto fail;
4121 	}
4122 
4123 	if (params->beacon_ies) {
4124 		wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
4125 				params->beacon_ies);
4126 		if (nla_put(msg, NL80211_ATTR_IE,
4127 			    wpabuf_len(params->beacon_ies),
4128 			    wpabuf_head(params->beacon_ies)))
4129 			goto fail;
4130 	}
4131 	if (params->proberesp_ies) {
4132 		wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
4133 				params->proberesp_ies);
4134 		if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
4135 			    wpabuf_len(params->proberesp_ies),
4136 			    wpabuf_head(params->proberesp_ies)))
4137 			goto fail;
4138 	}
4139 	if (params->assocresp_ies) {
4140 		wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
4141 				params->assocresp_ies);
4142 		if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
4143 			    wpabuf_len(params->assocresp_ies),
4144 			    wpabuf_head(params->assocresp_ies)))
4145 			goto fail;
4146 	}
4147 
4148 	if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)  {
4149 		wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
4150 			   params->ap_max_inactivity);
4151 		if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
4152 				params->ap_max_inactivity))
4153 			goto fail;
4154 	}
4155 
4156 #ifdef CONFIG_P2P
4157 	if (params->p2p_go_ctwindow > 0) {
4158 		if (drv->p2p_go_ctwindow_supported) {
4159 			wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
4160 				   params->p2p_go_ctwindow);
4161 			if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
4162 				       params->p2p_go_ctwindow))
4163 				goto fail;
4164 		} else {
4165 			wpa_printf(MSG_INFO,
4166 				   "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
4167 		}
4168 	}
4169 #endif /* CONFIG_P2P */
4170 
4171 	if (params->pbss) {
4172 		wpa_printf(MSG_DEBUG, "nl80211: PBSS");
4173 		if (nla_put_flag(msg, NL80211_ATTR_PBSS))
4174 			goto fail;
4175 	}
4176 
4177 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4178 	if (ret) {
4179 		wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4180 			   ret, strerror(-ret));
4181 	} else {
4182 		bss->beacon_set = 1;
4183 		nl80211_set_bss(bss, params->cts_protect, params->preamble,
4184 				params->short_slot_time, params->ht_opmode,
4185 				params->isolate, params->basic_rates);
4186 		nl80211_set_multicast_to_unicast(bss,
4187 						 params->multicast_to_unicast);
4188 		if (beacon_set && params->freq &&
4189 		    params->freq->bandwidth != bss->bandwidth) {
4190 			wpa_printf(MSG_DEBUG,
4191 				   "nl80211: Update BSS %s bandwidth: %d -> %d",
4192 				   bss->ifname, bss->bandwidth,
4193 				   params->freq->bandwidth);
4194 			ret = nl80211_set_channel(bss, params->freq, 1);
4195 			if (ret) {
4196 				wpa_printf(MSG_DEBUG,
4197 					   "nl80211: Frequency set failed: %d (%s)",
4198 					   ret, strerror(-ret));
4199 			} else {
4200 				wpa_printf(MSG_DEBUG,
4201 					   "nl80211: Frequency set succeeded for ht2040 coex");
4202 				bss->bandwidth = params->freq->bandwidth;
4203 			}
4204 		} else if (!beacon_set && params->freq) {
4205 			/*
4206 			 * cfg80211 updates the driver on frequence change in AP
4207 			 * mode only at the point when beaconing is started, so
4208 			 * set the initial value here.
4209 			 */
4210 			bss->bandwidth = params->freq->bandwidth;
4211 		}
4212 	}
4213 
4214 #ifdef CONFIG_MESH
4215 	if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4216 		os_memset(&mesh_params, 0, sizeof(mesh_params));
4217 		mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4218 		mesh_params.ht_opmode = params->ht_opmode;
4219 		ret = nl80211_set_mesh_config(priv, &mesh_params);
4220 		if (ret < 0)
4221 			return ret;
4222 	}
4223 #endif /* CONFIG_MESH */
4224 
4225 	return ret;
4226 fail:
4227 	nlmsg_free(msg);
4228 	return -ENOBUFS;
4229 }
4230 
4231 
4232 static int nl80211_put_freq_params(struct nl_msg *msg,
4233 				   const struct hostapd_freq_params *freq)
4234 {
4235 	wpa_printf(MSG_DEBUG, "  * freq=%d", freq->freq);
4236 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4237 		return -ENOBUFS;
4238 
4239 	wpa_printf(MSG_DEBUG, "  * vht_enabled=%d", freq->vht_enabled);
4240 	wpa_printf(MSG_DEBUG, "  * ht_enabled=%d", freq->ht_enabled);
4241 
4242 	if (freq->vht_enabled) {
4243 		enum nl80211_chan_width cw;
4244 
4245 		wpa_printf(MSG_DEBUG, "  * bandwidth=%d", freq->bandwidth);
4246 		switch (freq->bandwidth) {
4247 		case 20:
4248 			cw = NL80211_CHAN_WIDTH_20;
4249 			break;
4250 		case 40:
4251 			cw = NL80211_CHAN_WIDTH_40;
4252 			break;
4253 		case 80:
4254 			if (freq->center_freq2)
4255 				cw = NL80211_CHAN_WIDTH_80P80;
4256 			else
4257 				cw = NL80211_CHAN_WIDTH_80;
4258 			break;
4259 		case 160:
4260 			cw = NL80211_CHAN_WIDTH_160;
4261 			break;
4262 		default:
4263 			return -EINVAL;
4264 		}
4265 
4266 		wpa_printf(MSG_DEBUG, "  * channel_width=%d", cw);
4267 		wpa_printf(MSG_DEBUG, "  * center_freq1=%d",
4268 			   freq->center_freq1);
4269 		wpa_printf(MSG_DEBUG, "  * center_freq2=%d",
4270 			   freq->center_freq2);
4271 		if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4272 		    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4273 				freq->center_freq1) ||
4274 		    (freq->center_freq2 &&
4275 		     nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4276 				 freq->center_freq2)))
4277 			return -ENOBUFS;
4278 	} else if (freq->ht_enabled) {
4279 		enum nl80211_channel_type ct;
4280 
4281 		wpa_printf(MSG_DEBUG, "  * sec_channel_offset=%d",
4282 			   freq->sec_channel_offset);
4283 		switch (freq->sec_channel_offset) {
4284 		case -1:
4285 			ct = NL80211_CHAN_HT40MINUS;
4286 			break;
4287 		case 1:
4288 			ct = NL80211_CHAN_HT40PLUS;
4289 			break;
4290 		default:
4291 			ct = NL80211_CHAN_HT20;
4292 			break;
4293 		}
4294 
4295 		wpa_printf(MSG_DEBUG, "  * channel_type=%d", ct);
4296 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4297 			return -ENOBUFS;
4298 	} else {
4299 		wpa_printf(MSG_DEBUG, "  * channel_type=%d",
4300 			   NL80211_CHAN_NO_HT);
4301 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4302 				NL80211_CHAN_NO_HT))
4303 			return -ENOBUFS;
4304 	}
4305 	return 0;
4306 }
4307 
4308 
4309 static int nl80211_set_channel(struct i802_bss *bss,
4310 			       struct hostapd_freq_params *freq, int set_chan)
4311 {
4312 	struct wpa_driver_nl80211_data *drv = bss->drv;
4313 	struct nl_msg *msg;
4314 	int ret;
4315 
4316 	wpa_printf(MSG_DEBUG,
4317 		   "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4318 		   freq->freq, freq->ht_enabled, freq->vht_enabled,
4319 		   freq->bandwidth, freq->center_freq1, freq->center_freq2);
4320 
4321 	msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4322 			      NL80211_CMD_SET_WIPHY);
4323 	if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
4324 		nlmsg_free(msg);
4325 		return -1;
4326 	}
4327 
4328 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4329 	if (ret == 0) {
4330 		bss->freq = freq->freq;
4331 		return 0;
4332 	}
4333 	wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
4334 		   "%d (%s)", freq->freq, ret, strerror(-ret));
4335 	return -1;
4336 }
4337 
4338 
4339 static u32 sta_flags_nl80211(int flags)
4340 {
4341 	u32 f = 0;
4342 
4343 	if (flags & WPA_STA_AUTHORIZED)
4344 		f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4345 	if (flags & WPA_STA_WMM)
4346 		f |= BIT(NL80211_STA_FLAG_WME);
4347 	if (flags & WPA_STA_SHORT_PREAMBLE)
4348 		f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4349 	if (flags & WPA_STA_MFP)
4350 		f |= BIT(NL80211_STA_FLAG_MFP);
4351 	if (flags & WPA_STA_TDLS_PEER)
4352 		f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
4353 	if (flags & WPA_STA_AUTHENTICATED)
4354 		f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4355 	if (flags & WPA_STA_ASSOCIATED)
4356 		f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4357 
4358 	return f;
4359 }
4360 
4361 
4362 #ifdef CONFIG_MESH
4363 static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4364 {
4365 	switch (state) {
4366 	case PLINK_IDLE:
4367 		return NL80211_PLINK_LISTEN;
4368 	case PLINK_OPN_SNT:
4369 		return NL80211_PLINK_OPN_SNT;
4370 	case PLINK_OPN_RCVD:
4371 		return NL80211_PLINK_OPN_RCVD;
4372 	case PLINK_CNF_RCVD:
4373 		return NL80211_PLINK_CNF_RCVD;
4374 	case PLINK_ESTAB:
4375 		return NL80211_PLINK_ESTAB;
4376 	case PLINK_HOLDING:
4377 		return NL80211_PLINK_HOLDING;
4378 	case PLINK_BLOCKED:
4379 		return NL80211_PLINK_BLOCKED;
4380 	default:
4381 		wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4382 			   state);
4383 	}
4384 	return -1;
4385 }
4386 #endif /* CONFIG_MESH */
4387 
4388 
4389 static int wpa_driver_nl80211_sta_add(void *priv,
4390 				      struct hostapd_sta_add_params *params)
4391 {
4392 	struct i802_bss *bss = priv;
4393 	struct wpa_driver_nl80211_data *drv = bss->drv;
4394 	struct nl_msg *msg;
4395 	struct nl80211_sta_flag_update upd;
4396 	int ret = -ENOBUFS;
4397 
4398 	if ((params->flags & WPA_STA_TDLS_PEER) &&
4399 	    !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4400 		return -EOPNOTSUPP;
4401 
4402 	wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4403 		   params->set ? "Set" : "Add", MAC2STR(params->addr));
4404 	msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4405 			      NL80211_CMD_NEW_STATION);
4406 	if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
4407 		goto fail;
4408 
4409 	/*
4410 	 * Set the below properties only in one of the following cases:
4411 	 * 1. New station is added, already associated.
4412 	 * 2. Set WPA_STA_TDLS_PEER station.
4413 	 * 3. Set an already added unassociated station, if driver supports
4414 	 * full AP client state. (Set these properties after station became
4415 	 * associated will be rejected by the driver).
4416 	 */
4417 	if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4418 	    (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4419 	     (params->flags & WPA_STA_ASSOCIATED))) {
4420 		wpa_hexdump(MSG_DEBUG, "  * supported rates",
4421 			    params->supp_rates, params->supp_rates_len);
4422 		wpa_printf(MSG_DEBUG, "  * capability=0x%x",
4423 			   params->capability);
4424 		if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4425 			    params->supp_rates_len, params->supp_rates) ||
4426 		    nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4427 				params->capability))
4428 			goto fail;
4429 
4430 		if (params->ht_capabilities) {
4431 			wpa_hexdump(MSG_DEBUG, "  * ht_capabilities",
4432 				    (u8 *) params->ht_capabilities,
4433 				    sizeof(*params->ht_capabilities));
4434 			if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4435 				    sizeof(*params->ht_capabilities),
4436 				    params->ht_capabilities))
4437 				goto fail;
4438 		}
4439 
4440 		if (params->vht_capabilities) {
4441 			wpa_hexdump(MSG_DEBUG, "  * vht_capabilities",
4442 				    (u8 *) params->vht_capabilities,
4443 				    sizeof(*params->vht_capabilities));
4444 			if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4445 				    sizeof(*params->vht_capabilities),
4446 				    params->vht_capabilities))
4447 				goto fail;
4448 		}
4449 
4450 		if (params->ext_capab) {
4451 			wpa_hexdump(MSG_DEBUG, "  * ext_capab",
4452 				    params->ext_capab, params->ext_capab_len);
4453 			if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4454 				    params->ext_capab_len, params->ext_capab))
4455 				goto fail;
4456 		}
4457 
4458 		if (is_ap_interface(drv->nlmode) &&
4459 		    nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4460 			       params->support_p2p_ps ?
4461 			       NL80211_P2P_PS_SUPPORTED :
4462 			       NL80211_P2P_PS_UNSUPPORTED))
4463 			goto fail;
4464 	}
4465 	if (!params->set) {
4466 		if (params->aid) {
4467 			wpa_printf(MSG_DEBUG, "  * aid=%u", params->aid);
4468 			if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4469 				goto fail;
4470 		} else {
4471 			/*
4472 			 * cfg80211 validates that AID is non-zero, so we have
4473 			 * to make this a non-zero value for the TDLS case where
4474 			 * a dummy STA entry is used for now and for a station
4475 			 * that is still not associated.
4476 			 */
4477 			wpa_printf(MSG_DEBUG, "  * aid=1 (%s workaround)",
4478 				   (params->flags & WPA_STA_TDLS_PEER) ?
4479 				   "TDLS" : "UNASSOC_STA");
4480 			if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4481 				goto fail;
4482 		}
4483 		wpa_printf(MSG_DEBUG, "  * listen_interval=%u",
4484 			   params->listen_interval);
4485 		if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4486 				params->listen_interval))
4487 			goto fail;
4488 	} else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4489 		wpa_printf(MSG_DEBUG, "  * peer_aid=%u", params->aid);
4490 		if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4491 			goto fail;
4492 	} else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4493 		   (params->flags & WPA_STA_ASSOCIATED)) {
4494 		wpa_printf(MSG_DEBUG, "  * aid=%u", params->aid);
4495 		wpa_printf(MSG_DEBUG, "  * listen_interval=%u",
4496 			   params->listen_interval);
4497 		if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4498 		    nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4499 				params->listen_interval))
4500 			goto fail;
4501 	}
4502 
4503 	if (params->vht_opmode_enabled) {
4504 		wpa_printf(MSG_DEBUG, "  * opmode=%u", params->vht_opmode);
4505 		if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4506 			       params->vht_opmode))
4507 			goto fail;
4508 	}
4509 
4510 	if (params->supp_channels) {
4511 		wpa_hexdump(MSG_DEBUG, "  * supported channels",
4512 			    params->supp_channels, params->supp_channels_len);
4513 		if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4514 			    params->supp_channels_len, params->supp_channels))
4515 			goto fail;
4516 	}
4517 
4518 	if (params->supp_oper_classes) {
4519 		wpa_hexdump(MSG_DEBUG, "  * supported operating classes",
4520 			    params->supp_oper_classes,
4521 			    params->supp_oper_classes_len);
4522 		if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4523 			    params->supp_oper_classes_len,
4524 			    params->supp_oper_classes))
4525 			goto fail;
4526 	}
4527 
4528 	os_memset(&upd, 0, sizeof(upd));
4529 	upd.set = sta_flags_nl80211(params->flags);
4530 	upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
4531 
4532 	/*
4533 	 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4534 	 * flags, as nl80211 driver moves a new station, by default, into
4535 	 * associated state.
4536 	 *
4537 	 * On the other hand, if the driver supports that feature and the
4538 	 * station is added in unauthenticated state, set the
4539 	 * authenticated/associated bits in the mask to prevent moving this
4540 	 * station to associated state before it is actually associated.
4541 	 *
4542 	 * This is irrelevant for mesh mode where the station is added to the
4543 	 * driver as authenticated already, and ASSOCIATED isn't part of the
4544 	 * nl80211 API.
4545 	 */
4546 	if (!is_mesh_interface(drv->nlmode)) {
4547 		if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4548 			wpa_printf(MSG_DEBUG,
4549 				   "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4550 			upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4551 				      BIT(NL80211_STA_FLAG_AUTHENTICATED));
4552 		} else if (!params->set &&
4553 			   !(params->flags & WPA_STA_TDLS_PEER)) {
4554 			if (!(params->flags & WPA_STA_AUTHENTICATED))
4555 				upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4556 			if (!(params->flags & WPA_STA_ASSOCIATED))
4557 				upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4558 		}
4559 #ifdef CONFIG_MESH
4560 	} else {
4561 		if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4562 			ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4563 					  params->peer_aid);
4564 			if (ret)
4565 				goto fail;
4566 		}
4567 #endif /* CONFIG_MESH */
4568 	}
4569 
4570 	wpa_printf(MSG_DEBUG, "  * flags set=0x%x mask=0x%x",
4571 		   upd.set, upd.mask);
4572 	if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4573 		goto fail;
4574 
4575 #ifdef CONFIG_MESH
4576 	if (params->plink_state &&
4577 	    nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4578 		       sta_plink_state_nl80211(params->plink_state)))
4579 		goto fail;
4580 #endif /* CONFIG_MESH */
4581 
4582 	if (params->flags & WPA_STA_WMM) {
4583 		struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4584 
4585 		wpa_printf(MSG_DEBUG, "  * qosinfo=0x%x", params->qosinfo);
4586 		if (!wme ||
4587 		    nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4588 			       params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4589 		    nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4590 			       (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4591 			       WMM_QOSINFO_STA_SP_MASK))
4592 			goto fail;
4593 		nla_nest_end(msg, wme);
4594 	}
4595 
4596 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4597 	msg = NULL;
4598 	if (ret)
4599 		wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4600 			   "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4601 			   strerror(-ret));
4602 	if (ret == -EEXIST)
4603 		ret = 0;
4604 fail:
4605 	nlmsg_free(msg);
4606 	return ret;
4607 }
4608 
4609 
4610 static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4611 {
4612 #ifdef CONFIG_LIBNL3_ROUTE
4613 	struct wpa_driver_nl80211_data *drv = bss->drv;
4614 	struct rtnl_neigh *rn;
4615 	struct nl_addr *nl_addr;
4616 	int err;
4617 
4618 	rn = rtnl_neigh_alloc();
4619 	if (!rn)
4620 		return;
4621 
4622 	rtnl_neigh_set_family(rn, AF_BRIDGE);
4623 	rtnl_neigh_set_ifindex(rn, bss->ifindex);
4624 	nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4625 	if (!nl_addr) {
4626 		rtnl_neigh_put(rn);
4627 		return;
4628 	}
4629 	rtnl_neigh_set_lladdr(rn, nl_addr);
4630 
4631 	err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4632 	if (err < 0) {
4633 		wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4634 			   MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4635 			   bss->ifindex, nl_geterror(err));
4636 	} else {
4637 		wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4638 			   MACSTR, MAC2STR(addr));
4639 	}
4640 
4641 	nl_addr_put(nl_addr);
4642 	rtnl_neigh_put(rn);
4643 #endif /* CONFIG_LIBNL3_ROUTE */
4644 }
4645 
4646 
4647 static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4648 					 int deauth, u16 reason_code)
4649 {
4650 	struct wpa_driver_nl80211_data *drv = bss->drv;
4651 	struct nl_msg *msg;
4652 	int ret;
4653 
4654 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4655 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4656 	    (deauth == 0 &&
4657 	     nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4658 			WLAN_FC_STYPE_DISASSOC)) ||
4659 	    (deauth == 1 &&
4660 	     nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4661 			WLAN_FC_STYPE_DEAUTH)) ||
4662 	    (reason_code &&
4663 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4664 		nlmsg_free(msg);
4665 		return -ENOBUFS;
4666 	}
4667 
4668 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4669 	wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4670 		   " --> %d (%s)",
4671 		   bss->ifname, MAC2STR(addr), ret, strerror(-ret));
4672 
4673 	if (drv->rtnl_sk)
4674 		rtnl_neigh_delete_fdb_entry(bss, addr);
4675 
4676 	if (ret == -ENOENT)
4677 		return 0;
4678 	return ret;
4679 }
4680 
4681 
4682 void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
4683 {
4684 	struct nl_msg *msg;
4685 	struct wpa_driver_nl80211_data *drv2;
4686 
4687 	wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4688 
4689 	/* stop listening for EAPOL on this interface */
4690 	dl_list_for_each(drv2, &drv->global->interfaces,
4691 			 struct wpa_driver_nl80211_data, list)
4692 	{
4693 		del_ifidx(drv2, ifidx, IFIDX_ANY);
4694 		/* Remove all bridges learned for this iface */
4695 		del_ifidx(drv2, IFIDX_ANY, ifidx);
4696 	}
4697 
4698 	msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
4699 	if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4700 		return;
4701 	wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4702 }
4703 
4704 
4705 const char * nl80211_iftype_str(enum nl80211_iftype mode)
4706 {
4707 	switch (mode) {
4708 	case NL80211_IFTYPE_ADHOC:
4709 		return "ADHOC";
4710 	case NL80211_IFTYPE_STATION:
4711 		return "STATION";
4712 	case NL80211_IFTYPE_AP:
4713 		return "AP";
4714 	case NL80211_IFTYPE_AP_VLAN:
4715 		return "AP_VLAN";
4716 	case NL80211_IFTYPE_WDS:
4717 		return "WDS";
4718 	case NL80211_IFTYPE_MONITOR:
4719 		return "MONITOR";
4720 	case NL80211_IFTYPE_MESH_POINT:
4721 		return "MESH_POINT";
4722 	case NL80211_IFTYPE_P2P_CLIENT:
4723 		return "P2P_CLIENT";
4724 	case NL80211_IFTYPE_P2P_GO:
4725 		return "P2P_GO";
4726 	case NL80211_IFTYPE_P2P_DEVICE:
4727 		return "P2P_DEVICE";
4728 	default:
4729 		return "unknown";
4730 	}
4731 }
4732 
4733 
4734 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4735 				     const char *ifname,
4736 				     enum nl80211_iftype iftype,
4737 				     const u8 *addr, int wds,
4738 				     int (*handler)(struct nl_msg *, void *),
4739 				     void *arg)
4740 {
4741 	struct nl_msg *msg;
4742 	int ifidx;
4743 	int ret = -ENOBUFS;
4744 
4745 	wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4746 		   iftype, nl80211_iftype_str(iftype));
4747 
4748 	msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4749 	if (!msg ||
4750 	    nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4751 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4752 		goto fail;
4753 
4754 	if (iftype == NL80211_IFTYPE_MONITOR) {
4755 		struct nlattr *flags;
4756 
4757 		flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
4758 		if (!flags ||
4759 		    nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4760 			goto fail;
4761 
4762 		nla_nest_end(msg, flags);
4763 	} else if (wds) {
4764 		if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4765 			goto fail;
4766 	}
4767 
4768 	/*
4769 	 * Tell cfg80211 that the interface belongs to the socket that created
4770 	 * it, and the interface should be deleted when the socket is closed.
4771 	 */
4772 	if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4773 		goto fail;
4774 
4775 	ret = send_and_recv_msgs(drv, msg, handler, arg);
4776 	msg = NULL;
4777 	if (ret) {
4778 	fail:
4779 		nlmsg_free(msg);
4780 		wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4781 			   ifname, ret, strerror(-ret));
4782 		return ret;
4783 	}
4784 
4785 	if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4786 		return 0;
4787 
4788 	ifidx = if_nametoindex(ifname);
4789 	wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4790 		   ifname, ifidx);
4791 
4792 	if (ifidx <= 0)
4793 		return -1;
4794 
4795 	/*
4796 	 * Some virtual interfaces need to process EAPOL packets and events on
4797 	 * the parent interface. This is used mainly with hostapd.
4798 	 */
4799 	if (drv->hostapd ||
4800 	    iftype == NL80211_IFTYPE_AP_VLAN ||
4801 	    iftype == NL80211_IFTYPE_WDS ||
4802 	    iftype == NL80211_IFTYPE_MONITOR) {
4803 		/* start listening for EAPOL on this interface */
4804 		add_ifidx(drv, ifidx, IFIDX_ANY);
4805 	}
4806 
4807 	if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4808 	    linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
4809 		nl80211_remove_iface(drv, ifidx);
4810 		return -1;
4811 	}
4812 
4813 	return ifidx;
4814 }
4815 
4816 
4817 int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4818 			 const char *ifname, enum nl80211_iftype iftype,
4819 			 const u8 *addr, int wds,
4820 			 int (*handler)(struct nl_msg *, void *),
4821 			 void *arg, int use_existing)
4822 {
4823 	int ret;
4824 
4825 	ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4826 					arg);
4827 
4828 	/* if error occurred and interface exists already */
4829 	if (ret == -ENFILE && if_nametoindex(ifname)) {
4830 		if (use_existing) {
4831 			wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4832 				   ifname);
4833 			if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4834 			    linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4835 					       addr) < 0 &&
4836 			    (linux_set_iface_flags(drv->global->ioctl_sock,
4837 						   ifname, 0) < 0 ||
4838 			     linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4839 						addr) < 0 ||
4840 			     linux_set_iface_flags(drv->global->ioctl_sock,
4841 						   ifname, 1) < 0))
4842 					return -1;
4843 			return -ENFILE;
4844 		}
4845 		wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4846 
4847 		/* Try to remove the interface that was already there. */
4848 		nl80211_remove_iface(drv, if_nametoindex(ifname));
4849 
4850 		/* Try to create the interface again */
4851 		ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
4852 						wds, handler, arg);
4853 	}
4854 
4855 	if (ret >= 0 && is_p2p_net_interface(iftype)) {
4856 		wpa_printf(MSG_DEBUG,
4857 			   "nl80211: Interface %s created for P2P - disable 11b rates",
4858 			   ifname);
4859 		nl80211_disable_11b_rates(drv, ret, 1);
4860 	}
4861 
4862 	return ret;
4863 }
4864 
4865 
4866 static int nl80211_setup_ap(struct i802_bss *bss)
4867 {
4868 	struct wpa_driver_nl80211_data *drv = bss->drv;
4869 
4870 	wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4871 		   bss->ifname, drv->device_ap_sme, drv->use_monitor);
4872 
4873 	/*
4874 	 * Disable Probe Request reporting unless we need it in this way for
4875 	 * devices that include the AP SME, in the other case (unless using
4876 	 * monitor iface) we'll get it through the nl_mgmt socket instead.
4877 	 */
4878 	if (!drv->device_ap_sme)
4879 		wpa_driver_nl80211_probe_req_report(bss, 0);
4880 
4881 	if (!drv->device_ap_sme && !drv->use_monitor)
4882 		if (nl80211_mgmt_subscribe_ap(bss))
4883 			return -1;
4884 
4885 	if (drv->device_ap_sme && !drv->use_monitor)
4886 		if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
4887 			wpa_printf(MSG_DEBUG,
4888 				   "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
4889 
4890 	if (!drv->device_ap_sme && drv->use_monitor &&
4891 	    nl80211_create_monitor_interface(drv) &&
4892 	    !drv->device_ap_sme)
4893 		return -1;
4894 
4895 	if (drv->device_ap_sme &&
4896 	    wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4897 		wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4898 			   "Probe Request frame reporting in AP mode");
4899 		/* Try to survive without this */
4900 	}
4901 
4902 	return 0;
4903 }
4904 
4905 
4906 static void nl80211_teardown_ap(struct i802_bss *bss)
4907 {
4908 	struct wpa_driver_nl80211_data *drv = bss->drv;
4909 
4910 	wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4911 		   bss->ifname, drv->device_ap_sme, drv->use_monitor);
4912 	if (drv->device_ap_sme) {
4913 		wpa_driver_nl80211_probe_req_report(bss, 0);
4914 		if (!drv->use_monitor)
4915 			nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4916 	} else if (drv->use_monitor)
4917 		nl80211_remove_monitor_interface(drv);
4918 	else
4919 		nl80211_mgmt_unsubscribe(bss, "AP teardown");
4920 
4921 	nl80211_put_wiphy_data_ap(bss);
4922 	bss->beacon_set = 0;
4923 }
4924 
4925 
4926 static int nl80211_send_eapol_data(struct i802_bss *bss,
4927 				   const u8 *addr, const u8 *data,
4928 				   size_t data_len)
4929 {
4930 	struct sockaddr_ll ll;
4931 	int ret;
4932 
4933 	if (bss->drv->eapol_tx_sock < 0) {
4934 		wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4935 		return -1;
4936 	}
4937 
4938 	os_memset(&ll, 0, sizeof(ll));
4939 	ll.sll_family = AF_PACKET;
4940 	ll.sll_ifindex = bss->ifindex;
4941 	ll.sll_protocol = htons(ETH_P_PAE);
4942 	ll.sll_halen = ETH_ALEN;
4943 	os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4944 	ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4945 		     (struct sockaddr *) &ll, sizeof(ll));
4946 	if (ret < 0)
4947 		wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4948 			   strerror(errno));
4949 
4950 	return ret;
4951 }
4952 
4953 
4954 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4955 
4956 static int wpa_driver_nl80211_hapd_send_eapol(
4957 	void *priv, const u8 *addr, const u8 *data,
4958 	size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4959 {
4960 	struct i802_bss *bss = priv;
4961 	struct wpa_driver_nl80211_data *drv = bss->drv;
4962 	struct ieee80211_hdr *hdr;
4963 	size_t len;
4964 	u8 *pos;
4965 	int res;
4966 	int qos = flags & WPA_STA_WMM;
4967 
4968 	if (drv->device_ap_sme || !drv->use_monitor)
4969 		return nl80211_send_eapol_data(bss, addr, data, data_len);
4970 
4971 	len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4972 		data_len;
4973 	hdr = os_zalloc(len);
4974 	if (hdr == NULL) {
4975 		wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4976 			   (unsigned long) len);
4977 		return -1;
4978 	}
4979 
4980 	hdr->frame_control =
4981 		IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4982 	hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4983 	if (encrypt)
4984 		hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4985 	if (qos) {
4986 		hdr->frame_control |=
4987 			host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4988 	}
4989 
4990 	memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4991 	memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4992 	memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4993 	pos = (u8 *) (hdr + 1);
4994 
4995 	if (qos) {
4996 		/* Set highest priority in QoS header */
4997 		pos[0] = 7;
4998 		pos[1] = 0;
4999 		pos += 2;
5000 	}
5001 
5002 	memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
5003 	pos += sizeof(rfc1042_header);
5004 	WPA_PUT_BE16(pos, ETH_P_PAE);
5005 	pos += 2;
5006 	memcpy(pos, data, data_len);
5007 
5008 	res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
5009 					    0, 0, 0, 0, NULL, 0);
5010 	if (res < 0) {
5011 		wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
5012 			   "failed: %d (%s)",
5013 			   (unsigned long) len, errno, strerror(errno));
5014 	}
5015 	os_free(hdr);
5016 
5017 	return res;
5018 }
5019 
5020 
5021 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
5022 					    unsigned int total_flags,
5023 					    unsigned int flags_or,
5024 					    unsigned int flags_and)
5025 {
5026 	struct i802_bss *bss = priv;
5027 	struct nl_msg *msg;
5028 	struct nlattr *flags;
5029 	struct nl80211_sta_flag_update upd;
5030 
5031 	wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
5032 		   " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
5033 		   bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
5034 		   !!(total_flags & WPA_STA_AUTHORIZED));
5035 
5036 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5037 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
5038 		goto fail;
5039 
5040 	/*
5041 	 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
5042 	 * can be removed eventually.
5043 	 */
5044 	flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
5045 	if (!flags ||
5046 	    ((total_flags & WPA_STA_AUTHORIZED) &&
5047 	     nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
5048 	    ((total_flags & WPA_STA_WMM) &&
5049 	     nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
5050 	    ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
5051 	     nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
5052 	    ((total_flags & WPA_STA_MFP) &&
5053 	     nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
5054 	    ((total_flags & WPA_STA_TDLS_PEER) &&
5055 	     nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
5056 		goto fail;
5057 
5058 	nla_nest_end(msg, flags);
5059 
5060 	os_memset(&upd, 0, sizeof(upd));
5061 	upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
5062 	upd.set = sta_flags_nl80211(flags_or);
5063 	if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
5064 		goto fail;
5065 
5066 	return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
5067 fail:
5068 	nlmsg_free(msg);
5069 	return -ENOBUFS;
5070 }
5071 
5072 
5073 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
5074 				 struct wpa_driver_associate_params *params)
5075 {
5076 	enum nl80211_iftype nlmode, old_mode;
5077 
5078 	if (params->p2p) {
5079 		wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
5080 			   "group (GO)");
5081 		nlmode = NL80211_IFTYPE_P2P_GO;
5082 	} else
5083 		nlmode = NL80211_IFTYPE_AP;
5084 
5085 	old_mode = drv->nlmode;
5086 	if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
5087 		nl80211_remove_monitor_interface(drv);
5088 		return -1;
5089 	}
5090 
5091 	if (params->freq.freq &&
5092 	    nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
5093 		if (old_mode != nlmode)
5094 			wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
5095 		nl80211_remove_monitor_interface(drv);
5096 		return -1;
5097 	}
5098 
5099 	return 0;
5100 }
5101 
5102 
5103 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
5104 			      int reset_mode)
5105 {
5106 	struct nl_msg *msg;
5107 	int ret;
5108 
5109 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
5110 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5111 	if (ret) {
5112 		wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5113 			   "(%s)", ret, strerror(-ret));
5114 	} else {
5115 		wpa_printf(MSG_DEBUG,
5116 			   "nl80211: Leave IBSS request sent successfully");
5117 	}
5118 
5119 	if (reset_mode &&
5120 	    wpa_driver_nl80211_set_mode(drv->first_bss,
5121 					NL80211_IFTYPE_STATION)) {
5122 		wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5123 			   "station mode");
5124 	}
5125 
5126 	return ret;
5127 }
5128 
5129 
5130 static int nl80211_ht_vht_overrides(struct nl_msg *msg,
5131 				    struct wpa_driver_associate_params *params)
5132 {
5133 	if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
5134 		return -1;
5135 
5136 	if (params->htcaps && params->htcaps_mask) {
5137 		int sz = sizeof(struct ieee80211_ht_capabilities);
5138 		wpa_hexdump(MSG_DEBUG, "  * htcaps", params->htcaps, sz);
5139 		wpa_hexdump(MSG_DEBUG, "  * htcaps_mask",
5140 			    params->htcaps_mask, sz);
5141 		if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
5142 			    params->htcaps) ||
5143 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
5144 			    params->htcaps_mask))
5145 			return -1;
5146 	}
5147 
5148 #ifdef CONFIG_VHT_OVERRIDES
5149 	if (params->disable_vht) {
5150 		wpa_printf(MSG_DEBUG, "  * VHT disabled");
5151 		if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
5152 			return -1;
5153 	}
5154 
5155 	if (params->vhtcaps && params->vhtcaps_mask) {
5156 		int sz = sizeof(struct ieee80211_vht_capabilities);
5157 		wpa_hexdump(MSG_DEBUG, "  * vhtcaps", params->vhtcaps, sz);
5158 		wpa_hexdump(MSG_DEBUG, "  * vhtcaps_mask",
5159 			    params->vhtcaps_mask, sz);
5160 		if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
5161 			    params->vhtcaps) ||
5162 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
5163 			    params->vhtcaps_mask))
5164 			return -1;
5165 	}
5166 #endif /* CONFIG_VHT_OVERRIDES */
5167 
5168 	return 0;
5169 }
5170 
5171 
5172 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5173 				   struct wpa_driver_associate_params *params)
5174 {
5175 	struct nl_msg *msg;
5176 	int ret = -1;
5177 	int count = 0;
5178 
5179 	wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5180 
5181 	if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
5182 		wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5183 			   "IBSS mode");
5184 		return -1;
5185 	}
5186 
5187 retry:
5188 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
5189 	    params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5190 		goto fail;
5191 
5192 	wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
5193 			  params->ssid, params->ssid_len);
5194 	if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5195 		goto fail;
5196 	os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5197 	drv->ssid_len = params->ssid_len;
5198 
5199 	if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5200 	    nl80211_put_beacon_int(msg, params->beacon_int))
5201 		goto fail;
5202 
5203 	ret = nl80211_set_conn_keys(params, msg);
5204 	if (ret)
5205 		goto fail;
5206 
5207 	if (params->bssid && params->fixed_bssid) {
5208 		wpa_printf(MSG_DEBUG, "  * BSSID=" MACSTR,
5209 			   MAC2STR(params->bssid));
5210 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5211 			goto fail;
5212 	}
5213 
5214 	if (params->fixed_freq) {
5215 		wpa_printf(MSG_DEBUG, "  * fixed_freq");
5216 		if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5217 			goto fail;
5218 	}
5219 
5220 	if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5221 	    params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5222 	    params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5223 	    params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
5224 		wpa_printf(MSG_DEBUG, "  * control port");
5225 		if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5226 			goto fail;
5227 	}
5228 
5229 	if (params->wpa_ie) {
5230 		wpa_hexdump(MSG_DEBUG,
5231 			    "  * Extra IEs for Beacon/Probe Response frames",
5232 			    params->wpa_ie, params->wpa_ie_len);
5233 		if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5234 			    params->wpa_ie))
5235 			goto fail;
5236 	}
5237 
5238 	ret = nl80211_ht_vht_overrides(msg, params);
5239 	if (ret < 0)
5240 		goto fail;
5241 
5242 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5243 	msg = NULL;
5244 	if (ret) {
5245 		wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5246 			   ret, strerror(-ret));
5247 		count++;
5248 		if (ret == -EALREADY && count == 1) {
5249 			wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5250 				   "forced leave");
5251 			nl80211_leave_ibss(drv, 0);
5252 			nlmsg_free(msg);
5253 			goto retry;
5254 		}
5255 	} else {
5256 		wpa_printf(MSG_DEBUG,
5257 			   "nl80211: Join IBSS request sent successfully");
5258 	}
5259 
5260 fail:
5261 	nlmsg_free(msg);
5262 	return ret;
5263 }
5264 
5265 
5266 static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
5267 					   struct wpa_driver_associate_params *params,
5268 					   struct nl_msg *msg)
5269 {
5270 	if (params->fils_erp_username_len) {
5271 		wpa_hexdump_ascii(MSG_DEBUG, "  * FILS ERP EMSKname/username",
5272 				  params->fils_erp_username,
5273 				  params->fils_erp_username_len);
5274 		if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
5275 			    params->fils_erp_username_len,
5276 			    params->fils_erp_username))
5277 			return -1;
5278 	}
5279 
5280 	if (params->fils_erp_realm_len) {
5281 		wpa_hexdump_ascii(MSG_DEBUG, "  * FILS ERP Realm",
5282 				  params->fils_erp_realm,
5283 				  params->fils_erp_realm_len);
5284 		if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
5285 			    params->fils_erp_realm_len, params->fils_erp_realm))
5286 			return -1;
5287 	}
5288 
5289 	wpa_printf(MSG_DEBUG, "  * FILS ERP next seq %u",
5290 		   params->fils_erp_next_seq_num);
5291 	if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
5292 			params->fils_erp_next_seq_num))
5293 		return -1;
5294 
5295 	if (params->fils_erp_rrk_len) {
5296 		wpa_printf(MSG_DEBUG, "  * FILS ERP rRK (len=%lu)",
5297 			   (unsigned long) params->fils_erp_rrk_len);
5298 		if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
5299 			    params->fils_erp_rrk_len, params->fils_erp_rrk))
5300 			return -1;
5301 	}
5302 
5303 	return 0;
5304 }
5305 
5306 
5307 static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5308 				  struct wpa_driver_associate_params *params,
5309 				  struct nl_msg *msg)
5310 {
5311 	if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5312 		return -1;
5313 
5314 	if (params->bssid) {
5315 		wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
5316 			   MAC2STR(params->bssid));
5317 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5318 			return -1;
5319 	}
5320 
5321 	if (params->bssid_hint) {
5322 		wpa_printf(MSG_DEBUG, "  * bssid_hint=" MACSTR,
5323 			   MAC2STR(params->bssid_hint));
5324 		if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5325 			    params->bssid_hint))
5326 			return -1;
5327 	}
5328 
5329 	if (params->freq.freq) {
5330 		wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq.freq);
5331 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5332 				params->freq.freq))
5333 			return -1;
5334 		drv->assoc_freq = params->freq.freq;
5335 	} else
5336 		drv->assoc_freq = 0;
5337 
5338 	if (params->freq_hint) {
5339 		wpa_printf(MSG_DEBUG, "  * freq_hint=%d", params->freq_hint);
5340 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5341 				params->freq_hint))
5342 			return -1;
5343 	}
5344 
5345 	if (params->bg_scan_period >= 0) {
5346 		wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
5347 			   params->bg_scan_period);
5348 		if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5349 				params->bg_scan_period))
5350 			return -1;
5351 	}
5352 
5353 	if (params->ssid) {
5354 		wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
5355 				  params->ssid, params->ssid_len);
5356 		if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5357 			    params->ssid))
5358 			return -1;
5359 		if (params->ssid_len > sizeof(drv->ssid))
5360 			return -1;
5361 		os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5362 		drv->ssid_len = params->ssid_len;
5363 	}
5364 
5365 	wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
5366 	if (params->wpa_ie &&
5367 	    nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5368 		return -1;
5369 
5370 	if (params->wpa_proto) {
5371 		enum nl80211_wpa_versions ver = 0;
5372 
5373 		if (params->wpa_proto & WPA_PROTO_WPA)
5374 			ver |= NL80211_WPA_VERSION_1;
5375 		if (params->wpa_proto & WPA_PROTO_RSN)
5376 			ver |= NL80211_WPA_VERSION_2;
5377 
5378 		wpa_printf(MSG_DEBUG, "  * WPA Versions 0x%x", ver);
5379 		if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5380 			return -1;
5381 	}
5382 
5383 	if (params->pairwise_suite != WPA_CIPHER_NONE) {
5384 		u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5385 		wpa_printf(MSG_DEBUG, "  * pairwise=0x%x", cipher);
5386 		if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5387 				cipher))
5388 			return -1;
5389 	}
5390 
5391 	if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5392 	    !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5393 		/*
5394 		 * This is likely to work even though many drivers do not
5395 		 * advertise support for operations without GTK.
5396 		 */
5397 		wpa_printf(MSG_DEBUG, "  * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5398 	} else if (params->group_suite != WPA_CIPHER_NONE) {
5399 		u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5400 		wpa_printf(MSG_DEBUG, "  * group=0x%x", cipher);
5401 		if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5402 			return -1;
5403 	}
5404 
5405 	if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5406 	    params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5407 	    params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5408 	    params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
5409 	    params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
5410 	    params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5411 	    params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5412 	    params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5413 	    params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
5414 	    params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
5415 	    params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
5416 	    params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
5417 	    params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
5418 	    params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
5419 	    params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
5420 	    params->key_mgmt_suite == WPA_KEY_MGMT_DPP) {
5421 		int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
5422 
5423 		switch (params->key_mgmt_suite) {
5424 		case WPA_KEY_MGMT_CCKM:
5425 			mgmt = RSN_AUTH_KEY_MGMT_CCKM;
5426 			break;
5427 		case WPA_KEY_MGMT_IEEE8021X:
5428 			mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
5429 			break;
5430 		case WPA_KEY_MGMT_FT_IEEE8021X:
5431 			mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
5432 			break;
5433 		case WPA_KEY_MGMT_FT_PSK:
5434 			mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
5435 			break;
5436 		case WPA_KEY_MGMT_IEEE8021X_SHA256:
5437 			mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
5438 			break;
5439 		case WPA_KEY_MGMT_PSK_SHA256:
5440 			mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
5441 			break;
5442 		case WPA_KEY_MGMT_OSEN:
5443 			mgmt = RSN_AUTH_KEY_MGMT_OSEN;
5444 			break;
5445 		case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
5446 			mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
5447 			break;
5448 		case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
5449 			mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
5450 			break;
5451 		case WPA_KEY_MGMT_FILS_SHA256:
5452 			mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA256;
5453 			break;
5454 		case WPA_KEY_MGMT_FILS_SHA384:
5455 			mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA384;
5456 			break;
5457 		case WPA_KEY_MGMT_FT_FILS_SHA256:
5458 			mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
5459 			break;
5460 		case WPA_KEY_MGMT_FT_FILS_SHA384:
5461 			mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
5462 			break;
5463 		case WPA_KEY_MGMT_OWE:
5464 			mgmt = RSN_AUTH_KEY_MGMT_OWE;
5465 			break;
5466 		case WPA_KEY_MGMT_DPP:
5467 			mgmt = RSN_AUTH_KEY_MGMT_DPP;
5468 			break;
5469 		case WPA_KEY_MGMT_PSK:
5470 		default:
5471 			mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
5472 			break;
5473 		}
5474 		wpa_printf(MSG_DEBUG, "  * akm=0x%x", mgmt);
5475 		if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5476 			return -1;
5477 	}
5478 
5479 	/* Add PSK in case of 4-way handshake offload */
5480 	if (params->psk &&
5481 	    (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)) {
5482 		wpa_hexdump_key(MSG_DEBUG, "  * PSK", params->psk, 32);
5483 		if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
5484 			return -1;
5485 	}
5486 
5487 	if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5488 		return -1;
5489 
5490 	if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5491 	    (params->pairwise_suite == WPA_CIPHER_NONE ||
5492 	     params->pairwise_suite == WPA_CIPHER_WEP104 ||
5493 	     params->pairwise_suite == WPA_CIPHER_WEP40) &&
5494 	    (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5495 	     nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5496 		return -1;
5497 
5498 	if (params->rrm_used) {
5499 		u32 drv_rrm_flags = drv->capa.rrm_flags;
5500 		if ((!((drv_rrm_flags &
5501 			WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5502 		       (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5503 		     !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
5504 		    nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5505 			return -1;
5506 	}
5507 
5508 	if (nl80211_ht_vht_overrides(msg, params) < 0)
5509 		return -1;
5510 
5511 	if (params->p2p)
5512 		wpa_printf(MSG_DEBUG, "  * P2P group");
5513 
5514 	if (params->pbss) {
5515 		wpa_printf(MSG_DEBUG, "  * PBSS");
5516 		if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5517 			return -1;
5518 	}
5519 
5520 	drv->connect_reassoc = 0;
5521 	if (params->prev_bssid) {
5522 		wpa_printf(MSG_DEBUG, "  * prev_bssid=" MACSTR,
5523 			   MAC2STR(params->prev_bssid));
5524 		if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5525 			    params->prev_bssid))
5526 			return -1;
5527 		drv->connect_reassoc = 1;
5528 	}
5529 
5530 	if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
5531 	    nl80211_put_fils_connect_params(drv, params, msg) != 0)
5532 		return -1;
5533 
5534 	if ((params->auth_alg & WPA_AUTH_ALG_SAE) &&
5535 	    (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
5536 	    nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5537 		return -1;
5538 
5539 	return 0;
5540 }
5541 
5542 
5543 static int wpa_driver_nl80211_try_connect(
5544 	struct wpa_driver_nl80211_data *drv,
5545 	struct wpa_driver_associate_params *params,
5546 	struct nl_handle *nl_connect)
5547 {
5548 	struct nl_msg *msg;
5549 	enum nl80211_auth_type type;
5550 	int ret;
5551 	int algs;
5552 
5553 #ifdef CONFIG_DRIVER_NL80211_QCA
5554 	if (params->req_key_mgmt_offload && params->psk &&
5555 	    (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5556 	     params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5557 	     params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5558 		wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5559 		ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5560 		if (ret)
5561 			return ret;
5562 	}
5563 #endif /* CONFIG_DRIVER_NL80211_QCA */
5564 
5565 	wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5566 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
5567 	if (!msg)
5568 		return -1;
5569 
5570 	ret = nl80211_connect_common(drv, params, msg);
5571 	if (ret)
5572 		goto fail;
5573 
5574 	if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5575 	    nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5576 		goto fail;
5577 
5578 	if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
5579 	    (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
5580 	    nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
5581 		goto fail;
5582 
5583 	algs = 0;
5584 	if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5585 		algs++;
5586 	if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5587 		algs++;
5588 	if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5589 		algs++;
5590 	if (params->auth_alg & WPA_AUTH_ALG_FILS)
5591 		algs++;
5592 	if (params->auth_alg & WPA_AUTH_ALG_FT)
5593 		algs++;
5594 	if (algs > 1) {
5595 		wpa_printf(MSG_DEBUG, "  * Leave out Auth Type for automatic "
5596 			   "selection");
5597 		goto skip_auth_type;
5598 	}
5599 
5600 	type = get_nl_auth_type(params->auth_alg);
5601 	wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
5602 	if (type == NL80211_AUTHTYPE_MAX ||
5603 	    nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
5604 		goto fail;
5605 
5606 skip_auth_type:
5607 	ret = nl80211_set_conn_keys(params, msg);
5608 	if (ret)
5609 		goto fail;
5610 
5611 	if (nl_connect)
5612 		ret = send_and_recv(drv->global, nl_connect, msg, NULL, NULL);
5613 	else
5614 		ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5615 
5616 	msg = NULL;
5617 	if (ret) {
5618 		wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5619 			   "(%s)", ret, strerror(-ret));
5620 	} else {
5621 		wpa_printf(MSG_DEBUG,
5622 			   "nl80211: Connect request send successfully");
5623 	}
5624 
5625 fail:
5626 	nlmsg_free(msg);
5627 	return ret;
5628 
5629 }
5630 
5631 
5632 static int wpa_driver_nl80211_connect(
5633 	struct wpa_driver_nl80211_data *drv,
5634 	struct wpa_driver_associate_params *params,
5635 	struct nl_handle *nl_connect)
5636 {
5637 	int ret;
5638 
5639 	/* Store the connection attempted bssid for future use */
5640 	if (params->bssid)
5641 		os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5642 	else
5643 		os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5644 
5645 	ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
5646 	if (ret == -EALREADY) {
5647 		/*
5648 		 * cfg80211 does not currently accept new connections if
5649 		 * we are already connected. As a workaround, force
5650 		 * disconnection and try again.
5651 		 */
5652 		wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5653 			   "disconnecting before reassociation "
5654 			   "attempt");
5655 		if (wpa_driver_nl80211_disconnect(
5656 			    drv, WLAN_REASON_PREV_AUTH_NOT_VALID, nl_connect))
5657 			return -1;
5658 		ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
5659 	}
5660 	return ret;
5661 }
5662 
5663 
5664 static int wpa_driver_nl80211_associate(
5665 	void *priv, struct wpa_driver_associate_params *params)
5666 {
5667 	struct i802_bss *bss = priv;
5668 	struct wpa_driver_nl80211_data *drv = bss->drv;
5669 	int ret = -1;
5670 	struct nl_msg *msg;
5671 
5672 	nl80211_unmask_11b_rates(bss);
5673 
5674 	if (params->mode == IEEE80211_MODE_AP)
5675 		return wpa_driver_nl80211_ap(drv, params);
5676 
5677 	if (params->mode == IEEE80211_MODE_IBSS)
5678 		return wpa_driver_nl80211_ibss(drv, params);
5679 
5680 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
5681 		enum nl80211_iftype nlmode = params->p2p ?
5682 			NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5683 		struct nl_handle *nl_connect = NULL;
5684 
5685 		if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
5686 			return -1;
5687 		if (params->auth_alg & WPA_AUTH_ALG_SAE) {
5688 			nl_connect = bss->nl_connect;
5689 			bss->use_nl_connect = 1;
5690 		} else {
5691 			bss->use_nl_connect = 0;
5692 		}
5693 
5694 		return wpa_driver_nl80211_connect(drv, params, nl_connect);
5695 	}
5696 
5697 	nl80211_mark_disconnected(drv);
5698 
5699 	wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5700 		   drv->ifindex);
5701 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
5702 	if (!msg)
5703 		return -1;
5704 
5705 	ret = nl80211_connect_common(drv, params, msg);
5706 	if (ret)
5707 		goto fail;
5708 
5709 	if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5710 	    nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5711 		goto fail;
5712 
5713 	if (params->fils_kek) {
5714 		wpa_printf(MSG_DEBUG, "  * FILS KEK (len=%u)",
5715 			   (unsigned int) params->fils_kek_len);
5716 		if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
5717 			    params->fils_kek))
5718 			goto fail;
5719 	}
5720 	if (params->fils_nonces) {
5721 		wpa_hexdump(MSG_DEBUG, "  * FILS nonces (for AAD)",
5722 			    params->fils_nonces,
5723 			    params->fils_nonces_len);
5724 		if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
5725 			    params->fils_nonces_len, params->fils_nonces))
5726 			goto fail;
5727 	}
5728 
5729 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5730 	msg = NULL;
5731 	if (ret) {
5732 		wpa_dbg(drv->ctx, MSG_DEBUG,
5733 			"nl80211: MLME command failed (assoc): ret=%d (%s)",
5734 			ret, strerror(-ret));
5735 		nl80211_dump_scan(drv);
5736 	} else {
5737 		wpa_printf(MSG_DEBUG,
5738 			   "nl80211: Association request send successfully");
5739 	}
5740 
5741 fail:
5742 	nlmsg_free(msg);
5743 	return ret;
5744 }
5745 
5746 
5747 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
5748 			    int ifindex, enum nl80211_iftype mode)
5749 {
5750 	struct nl_msg *msg;
5751 	int ret = -ENOBUFS;
5752 
5753 	wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5754 		   ifindex, mode, nl80211_iftype_str(mode));
5755 
5756 	msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5757 	if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
5758 		goto fail;
5759 
5760 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5761 	msg = NULL;
5762 	if (!ret)
5763 		return 0;
5764 fail:
5765 	nlmsg_free(msg);
5766 	wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5767 		   " %d (%s)", ifindex, mode, ret, strerror(-ret));
5768 	return ret;
5769 }
5770 
5771 
5772 static int wpa_driver_nl80211_set_mode_impl(
5773 		struct i802_bss *bss,
5774 		enum nl80211_iftype nlmode,
5775 		struct hostapd_freq_params *desired_freq_params)
5776 {
5777 	struct wpa_driver_nl80211_data *drv = bss->drv;
5778 	int ret = -1;
5779 	int i;
5780 	int was_ap = is_ap_interface(drv->nlmode);
5781 	int res;
5782 	int mode_switch_res;
5783 
5784 	if (TEST_FAIL())
5785 		return -1;
5786 
5787 	mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5788 	if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5789 		mode_switch_res = 0;
5790 
5791 	if (mode_switch_res == 0) {
5792 		drv->nlmode = nlmode;
5793 		ret = 0;
5794 		goto done;
5795 	}
5796 
5797 	if (mode_switch_res == -ENODEV)
5798 		return -1;
5799 
5800 	if (nlmode == drv->nlmode) {
5801 		wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5802 			   "requested mode - ignore error");
5803 		ret = 0;
5804 		goto done; /* Already in the requested mode */
5805 	}
5806 
5807 	/* mac80211 doesn't allow mode changes while the device is up, so
5808 	 * take the device down, try to set the mode again, and bring the
5809 	 * device back up.
5810 	 */
5811 	wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5812 		   "interface down");
5813 	for (i = 0; i < 10; i++) {
5814 		res = i802_set_iface_flags(bss, 0);
5815 		if (res == -EACCES || res == -ENODEV)
5816 			break;
5817 		if (res != 0) {
5818 			wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5819 				   "interface down");
5820 			os_sleep(0, 100000);
5821 			continue;
5822 		}
5823 
5824 		/*
5825 		 * Setting the mode will fail for some drivers if the phy is
5826 		 * on a frequency that the mode is disallowed in.
5827 		 */
5828 		if (desired_freq_params) {
5829 			res = nl80211_set_channel(bss, desired_freq_params, 0);
5830 			if (res) {
5831 				wpa_printf(MSG_DEBUG,
5832 					   "nl80211: Failed to set frequency on interface");
5833 			}
5834 		}
5835 
5836 		/* Try to set the mode again while the interface is down */
5837 		mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5838 		if (mode_switch_res == -EBUSY) {
5839 			wpa_printf(MSG_DEBUG,
5840 				   "nl80211: Delaying mode set while interface going down");
5841 			os_sleep(0, 100000);
5842 			continue;
5843 		}
5844 		ret = mode_switch_res;
5845 		break;
5846 	}
5847 
5848 	if (!ret) {
5849 		wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5850 			   "interface is down");
5851 		drv->nlmode = nlmode;
5852 		drv->ignore_if_down_event = 1;
5853 	}
5854 
5855 	/* Bring the interface back up */
5856 	res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5857 	if (res != 0) {
5858 		wpa_printf(MSG_DEBUG,
5859 			   "nl80211: Failed to set interface up after switching mode");
5860 		ret = -1;
5861 	}
5862 
5863 done:
5864 	if (ret) {
5865 		wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5866 			   "from %d failed", nlmode, drv->nlmode);
5867 		return ret;
5868 	}
5869 
5870 	if (is_p2p_net_interface(nlmode)) {
5871 		wpa_printf(MSG_DEBUG,
5872 			   "nl80211: Interface %s mode change to P2P - disable 11b rates",
5873 			   bss->ifname);
5874 		nl80211_disable_11b_rates(drv, drv->ifindex, 1);
5875 	} else if (drv->disabled_11b_rates) {
5876 		wpa_printf(MSG_DEBUG,
5877 			   "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5878 			   bss->ifname);
5879 		nl80211_disable_11b_rates(drv, drv->ifindex, 0);
5880 	}
5881 
5882 	if (is_ap_interface(nlmode)) {
5883 		nl80211_mgmt_unsubscribe(bss, "start AP");
5884 		/* Setup additional AP mode functionality if needed */
5885 		if (nl80211_setup_ap(bss))
5886 			return -1;
5887 	} else if (was_ap) {
5888 		/* Remove additional AP mode functionality */
5889 		nl80211_teardown_ap(bss);
5890 	} else {
5891 		nl80211_mgmt_unsubscribe(bss, "mode change");
5892 	}
5893 
5894 	if (is_mesh_interface(nlmode) &&
5895 	    nl80211_mgmt_subscribe_mesh(bss))
5896 		return -1;
5897 
5898 	if (!bss->in_deinit && !is_ap_interface(nlmode) &&
5899 	    !is_mesh_interface(nlmode) &&
5900 	    nl80211_mgmt_subscribe_non_ap(bss) < 0)
5901 		wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5902 			   "frame processing - ignore for now");
5903 
5904 	return 0;
5905 }
5906 
5907 
5908 int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5909 				enum nl80211_iftype nlmode)
5910 {
5911 	return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5912 }
5913 
5914 
5915 static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5916 					    struct hostapd_freq_params *freq)
5917 {
5918 	return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
5919 						freq);
5920 }
5921 
5922 
5923 static int wpa_driver_nl80211_get_capa(void *priv,
5924 				       struct wpa_driver_capa *capa)
5925 {
5926 	struct i802_bss *bss = priv;
5927 	struct wpa_driver_nl80211_data *drv = bss->drv;
5928 
5929 	if (!drv->has_capability)
5930 		return -1;
5931 	os_memcpy(capa, &drv->capa, sizeof(*capa));
5932 	if (drv->extended_capa && drv->extended_capa_mask) {
5933 		capa->extended_capa = drv->extended_capa;
5934 		capa->extended_capa_mask = drv->extended_capa_mask;
5935 		capa->extended_capa_len = drv->extended_capa_len;
5936 	}
5937 
5938 	return 0;
5939 }
5940 
5941 
5942 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5943 {
5944 	struct i802_bss *bss = priv;
5945 	struct wpa_driver_nl80211_data *drv = bss->drv;
5946 
5947 	wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5948 		   bss->ifname, drv->operstate, state,
5949 		   state ? "UP" : "DORMANT");
5950 	drv->operstate = state;
5951 	return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
5952 				      state ? IF_OPER_UP : IF_OPER_DORMANT);
5953 }
5954 
5955 
5956 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5957 {
5958 	struct i802_bss *bss = priv;
5959 	struct wpa_driver_nl80211_data *drv = bss->drv;
5960 	struct nl_msg *msg;
5961 	struct nl80211_sta_flag_update upd;
5962 	int ret;
5963 
5964 	if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5965 		wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5966 		return 0;
5967 	}
5968 
5969 	wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5970 		   MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5971 
5972 	os_memset(&upd, 0, sizeof(upd));
5973 	upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5974 	if (authorized)
5975 		upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
5976 
5977 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5978 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5979 	    nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5980 		nlmsg_free(msg);
5981 		return -ENOBUFS;
5982 	}
5983 
5984 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5985 	if (!ret)
5986 		return 0;
5987 	wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5988 		   ret, strerror(-ret));
5989 	return ret;
5990 }
5991 
5992 
5993 /* Set kernel driver on given frequency (MHz) */
5994 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
5995 {
5996 	struct i802_bss *bss = priv;
5997 	return nl80211_set_channel(bss, freq, 0);
5998 }
5999 
6000 
6001 static inline int min_int(int a, int b)
6002 {
6003 	if (a < b)
6004 		return a;
6005 	return b;
6006 }
6007 
6008 
6009 static int get_key_handler(struct nl_msg *msg, void *arg)
6010 {
6011 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
6012 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6013 
6014 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6015 		  genlmsg_attrlen(gnlh, 0), NULL);
6016 
6017 	/*
6018 	 * TODO: validate the key index and mac address!
6019 	 * Otherwise, there's a race condition as soon as
6020 	 * the kernel starts sending key notifications.
6021 	 */
6022 
6023 	if (tb[NL80211_ATTR_KEY_SEQ])
6024 		memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
6025 		       min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
6026 	return NL_SKIP;
6027 }
6028 
6029 
6030 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
6031 			   int idx, u8 *seq)
6032 {
6033 	struct i802_bss *bss = priv;
6034 	struct wpa_driver_nl80211_data *drv = bss->drv;
6035 	struct nl_msg *msg;
6036 
6037 	msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
6038 				  NL80211_CMD_GET_KEY);
6039 	if (!msg ||
6040 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
6041 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
6042 		nlmsg_free(msg);
6043 		return -ENOBUFS;
6044 	}
6045 
6046 	memset(seq, 0, 6);
6047 
6048 	return send_and_recv_msgs(drv, msg, get_key_handler, seq);
6049 }
6050 
6051 
6052 static int i802_set_rts(void *priv, int rts)
6053 {
6054 	struct i802_bss *bss = priv;
6055 	struct wpa_driver_nl80211_data *drv = bss->drv;
6056 	struct nl_msg *msg;
6057 	int ret;
6058 	u32 val;
6059 
6060 	if (rts >= 2347)
6061 		val = (u32) -1;
6062 	else
6063 		val = rts;
6064 
6065 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6066 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
6067 		nlmsg_free(msg);
6068 		return -ENOBUFS;
6069 	}
6070 
6071 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6072 	if (!ret)
6073 		return 0;
6074 	wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
6075 		   "%d (%s)", rts, ret, strerror(-ret));
6076 	return ret;
6077 }
6078 
6079 
6080 static int i802_set_frag(void *priv, int frag)
6081 {
6082 	struct i802_bss *bss = priv;
6083 	struct wpa_driver_nl80211_data *drv = bss->drv;
6084 	struct nl_msg *msg;
6085 	int ret;
6086 	u32 val;
6087 
6088 	if (frag >= 2346)
6089 		val = (u32) -1;
6090 	else
6091 		val = frag;
6092 
6093 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6094 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
6095 		nlmsg_free(msg);
6096 		return -ENOBUFS;
6097 	}
6098 
6099 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6100 	if (!ret)
6101 		return 0;
6102 	wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
6103 		   "%d: %d (%s)", frag, ret, strerror(-ret));
6104 	return ret;
6105 }
6106 
6107 
6108 static int i802_flush(void *priv)
6109 {
6110 	struct i802_bss *bss = priv;
6111 	struct nl_msg *msg;
6112 	int res;
6113 
6114 	wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
6115 		   bss->ifname);
6116 
6117 	/*
6118 	 * XXX: FIX! this needs to flush all VLANs too
6119 	 */
6120 	msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
6121 	res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
6122 	if (res) {
6123 		wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
6124 			   "(%s)", res, strerror(-res));
6125 	}
6126 	return res;
6127 }
6128 
6129 
6130 static int get_sta_handler(struct nl_msg *msg, void *arg)
6131 {
6132 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
6133 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6134 	struct hostap_sta_driver_data *data = arg;
6135 	struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
6136 	static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
6137 		[NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
6138 		[NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
6139 		[NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
6140 		[NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
6141 		[NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
6142 		[NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
6143 		[NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
6144 		[NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
6145 		[NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
6146 		[NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
6147 	};
6148 	struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
6149 	static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
6150 		[NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
6151 		[NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
6152 		[NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
6153 		[NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
6154 		[NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
6155 		[NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
6156 	};
6157 
6158 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6159 		  genlmsg_attrlen(gnlh, 0), NULL);
6160 
6161 	/*
6162 	 * TODO: validate the interface and mac address!
6163 	 * Otherwise, there's a race condition as soon as
6164 	 * the kernel starts sending station notifications.
6165 	 */
6166 
6167 	if (!tb[NL80211_ATTR_STA_INFO]) {
6168 		wpa_printf(MSG_DEBUG, "sta stats missing!");
6169 		return NL_SKIP;
6170 	}
6171 	if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6172 			     tb[NL80211_ATTR_STA_INFO],
6173 			     stats_policy)) {
6174 		wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6175 		return NL_SKIP;
6176 	}
6177 
6178 	if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6179 		data->inactive_msec =
6180 			nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
6181 	/* For backwards compatibility, fetch the 32-bit counters first. */
6182 	if (stats[NL80211_STA_INFO_RX_BYTES])
6183 		data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6184 	if (stats[NL80211_STA_INFO_TX_BYTES])
6185 		data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
6186 	if (stats[NL80211_STA_INFO_RX_BYTES64] &&
6187 	    stats[NL80211_STA_INFO_TX_BYTES64]) {
6188 		/*
6189 		 * The driver supports 64-bit counters, so use them to override
6190 		 * the 32-bit values.
6191 		 */
6192 		data->rx_bytes =
6193 			nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
6194 		data->tx_bytes =
6195 			nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
6196 		data->bytes_64bit = 1;
6197 	}
6198 	if (stats[NL80211_STA_INFO_RX_PACKETS])
6199 		data->rx_packets =
6200 			nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6201 	if (stats[NL80211_STA_INFO_TX_PACKETS])
6202 		data->tx_packets =
6203 			nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
6204 	if (stats[NL80211_STA_INFO_TX_FAILED])
6205 		data->tx_retry_failed =
6206 			nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
6207 	if (stats[NL80211_STA_INFO_SIGNAL])
6208 		data->signal = nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
6209 	if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
6210 		data->last_ack_rssi =
6211 			nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
6212 		data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
6213 	}
6214 
6215 	if (stats[NL80211_STA_INFO_TX_BITRATE] &&
6216 	    nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6217 			     stats[NL80211_STA_INFO_TX_BITRATE],
6218 			     rate_policy) == 0) {
6219 		if (rate[NL80211_RATE_INFO_BITRATE32])
6220 			data->current_tx_rate =
6221 				nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6222 		else if (rate[NL80211_RATE_INFO_BITRATE])
6223 			data->current_tx_rate =
6224 				nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6225 
6226 		if (rate[NL80211_RATE_INFO_MCS]) {
6227 			data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6228 			data->flags |= STA_DRV_DATA_TX_MCS;
6229 		}
6230 		if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6231 			data->tx_vhtmcs =
6232 				nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6233 			data->flags |= STA_DRV_DATA_TX_VHT_MCS;
6234 		}
6235 		if (rate[NL80211_RATE_INFO_SHORT_GI])
6236 			data->flags |= STA_DRV_DATA_TX_SHORT_GI;
6237 		if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6238 			data->tx_vht_nss =
6239 				nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6240 			data->flags |= STA_DRV_DATA_TX_VHT_NSS;
6241 		}
6242 	}
6243 
6244 	if (stats[NL80211_STA_INFO_RX_BITRATE] &&
6245 	    nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6246 			     stats[NL80211_STA_INFO_RX_BITRATE],
6247 			     rate_policy) == 0) {
6248 		if (rate[NL80211_RATE_INFO_BITRATE32])
6249 			data->current_rx_rate =
6250 				nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6251 		else if (rate[NL80211_RATE_INFO_BITRATE])
6252 			data->current_rx_rate =
6253 				nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6254 
6255 		if (rate[NL80211_RATE_INFO_MCS]) {
6256 			data->rx_mcs =
6257 				nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6258 			data->flags |= STA_DRV_DATA_RX_MCS;
6259 		}
6260 		if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6261 			data->rx_vhtmcs =
6262 				nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6263 			data->flags |= STA_DRV_DATA_RX_VHT_MCS;
6264 		}
6265 		if (rate[NL80211_RATE_INFO_SHORT_GI])
6266 			data->flags |= STA_DRV_DATA_RX_SHORT_GI;
6267 		if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6268 			data->rx_vht_nss =
6269 				nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6270 			data->flags |= STA_DRV_DATA_RX_VHT_NSS;
6271 		}
6272 	}
6273 
6274 	return NL_SKIP;
6275 }
6276 
6277 static int i802_read_sta_data(struct i802_bss *bss,
6278 			      struct hostap_sta_driver_data *data,
6279 			      const u8 *addr)
6280 {
6281 	struct nl_msg *msg;
6282 
6283 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
6284 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6285 		nlmsg_free(msg);
6286 		return -ENOBUFS;
6287 	}
6288 
6289 	return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
6290 }
6291 
6292 
6293 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6294 				    int cw_min, int cw_max, int burst_time)
6295 {
6296 	struct i802_bss *bss = priv;
6297 	struct wpa_driver_nl80211_data *drv = bss->drv;
6298 	struct nl_msg *msg;
6299 	struct nlattr *txq, *params;
6300 	int res;
6301 
6302 	msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
6303 	if (!msg)
6304 		return -1;
6305 
6306 	txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6307 	if (!txq)
6308 		goto fail;
6309 
6310 	/* We are only sending parameters for a single TXQ at a time */
6311 	params = nla_nest_start(msg, 1);
6312 	if (!params)
6313 		goto fail;
6314 
6315 	switch (queue) {
6316 	case 0:
6317 		if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
6318 			goto fail;
6319 		break;
6320 	case 1:
6321 		if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
6322 			goto fail;
6323 		break;
6324 	case 2:
6325 		if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
6326 			goto fail;
6327 		break;
6328 	case 3:
6329 		if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
6330 			goto fail;
6331 		break;
6332 	}
6333 	/* Burst time is configured in units of 0.1 msec and TXOP parameter in
6334 	 * 32 usec, so need to convert the value here. */
6335 	if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
6336 			(burst_time * 100 + 16) / 32) ||
6337 	    nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
6338 	    nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
6339 	    nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
6340 		goto fail;
6341 
6342 	nla_nest_end(msg, params);
6343 
6344 	nla_nest_end(msg, txq);
6345 
6346 	res = send_and_recv_msgs(drv, msg, NULL, NULL);
6347 	wpa_printf(MSG_DEBUG,
6348 		   "nl80211: TX queue param set: queue=%d aifs=%d cw_min=%d cw_max=%d burst_time=%d --> res=%d",
6349 		   queue, aifs, cw_min, cw_max, burst_time, res);
6350 	if (res == 0)
6351 		return 0;
6352 	msg = NULL;
6353 fail:
6354 	nlmsg_free(msg);
6355 	return -1;
6356 }
6357 
6358 
6359 static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
6360 			     const char *ifname, int vlan_id)
6361 {
6362 	struct wpa_driver_nl80211_data *drv = bss->drv;
6363 	struct nl_msg *msg;
6364 	int ret;
6365 
6366 	wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
6367 		   ", ifname=%s[%d], vlan_id=%d)",
6368 		   bss->ifname, if_nametoindex(bss->ifname),
6369 		   MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
6370 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6371 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6372 	    nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
6373 		nlmsg_free(msg);
6374 		return -ENOBUFS;
6375 	}
6376 
6377 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6378 	if (ret < 0) {
6379 		wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6380 			   MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6381 			   MAC2STR(addr), ifname, vlan_id, ret,
6382 			   strerror(-ret));
6383 	}
6384 	return ret;
6385 }
6386 
6387 
6388 static int i802_get_inact_sec(void *priv, const u8 *addr)
6389 {
6390 	struct hostap_sta_driver_data data;
6391 	int ret;
6392 
6393 	os_memset(&data, 0, sizeof(data));
6394 	data.inactive_msec = (unsigned long) -1;
6395 	ret = i802_read_sta_data(priv, &data, addr);
6396 	if (ret == -ENOENT)
6397 		return -ENOENT;
6398 	if (ret || data.inactive_msec == (unsigned long) -1)
6399 		return -1;
6400 	return data.inactive_msec / 1000;
6401 }
6402 
6403 
6404 static int i802_sta_clear_stats(void *priv, const u8 *addr)
6405 {
6406 #if 0
6407 	/* TODO */
6408 #endif
6409 	return 0;
6410 }
6411 
6412 
6413 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6414 			   int reason)
6415 {
6416 	struct i802_bss *bss = priv;
6417 	struct wpa_driver_nl80211_data *drv = bss->drv;
6418 	struct ieee80211_mgmt mgmt;
6419 	u8 channel;
6420 
6421 	if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6422 	    HOSTAPD_MODE_IEEE80211AD) {
6423 		/* Deauthentication is not used in DMG/IEEE 802.11ad;
6424 		 * disassociate the STA instead. */
6425 		return i802_sta_disassoc(priv, own_addr, addr, reason);
6426 	}
6427 
6428 	if (is_mesh_interface(drv->nlmode))
6429 		return -1;
6430 
6431 	if (drv->device_ap_sme)
6432 		return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
6433 
6434 	memset(&mgmt, 0, sizeof(mgmt));
6435 	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6436 					  WLAN_FC_STYPE_DEAUTH);
6437 	memcpy(mgmt.da, addr, ETH_ALEN);
6438 	memcpy(mgmt.sa, own_addr, ETH_ALEN);
6439 	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6440 	mgmt.u.deauth.reason_code = host_to_le16(reason);
6441 	return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6442 					    IEEE80211_HDRLEN +
6443 					    sizeof(mgmt.u.deauth), 0, 0, 0, 0,
6444 					    0, NULL, 0);
6445 }
6446 
6447 
6448 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6449 			     int reason)
6450 {
6451 	struct i802_bss *bss = priv;
6452 	struct wpa_driver_nl80211_data *drv = bss->drv;
6453 	struct ieee80211_mgmt mgmt;
6454 
6455 	if (is_mesh_interface(drv->nlmode))
6456 		return -1;
6457 
6458 	if (drv->device_ap_sme)
6459 		return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
6460 
6461 	memset(&mgmt, 0, sizeof(mgmt));
6462 	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6463 					  WLAN_FC_STYPE_DISASSOC);
6464 	memcpy(mgmt.da, addr, ETH_ALEN);
6465 	memcpy(mgmt.sa, own_addr, ETH_ALEN);
6466 	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6467 	mgmt.u.disassoc.reason_code = host_to_le16(reason);
6468 	return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6469 					    IEEE80211_HDRLEN +
6470 					    sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
6471 					    0, NULL, 0);
6472 }
6473 
6474 
6475 static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6476 {
6477 	char buf[200], *pos, *end;
6478 	int i, res;
6479 
6480 	pos = buf;
6481 	end = pos + sizeof(buf);
6482 
6483 	for (i = 0; i < drv->num_if_indices; i++) {
6484 		if (!drv->if_indices[i])
6485 			continue;
6486 		res = os_snprintf(pos, end - pos, " %d(%d)",
6487 				  drv->if_indices[i],
6488 				  drv->if_indices_reason[i]);
6489 		if (os_snprintf_error(end - pos, res))
6490 			break;
6491 		pos += res;
6492 	}
6493 	*pos = '\0';
6494 
6495 	wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
6496 		   drv->num_if_indices, buf);
6497 }
6498 
6499 
6500 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6501 		      int ifidx_reason)
6502 {
6503 	int i;
6504 	int *old, *old_reason;
6505 
6506 	wpa_printf(MSG_DEBUG,
6507 		   "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
6508 		   ifidx, ifidx_reason);
6509 	if (have_ifidx(drv, ifidx, ifidx_reason)) {
6510 		wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
6511 			   ifidx);
6512 		return;
6513 	}
6514 	for (i = 0; i < drv->num_if_indices; i++) {
6515 		if (drv->if_indices[i] == 0) {
6516 			drv->if_indices[i] = ifidx;
6517 			drv->if_indices_reason[i] = ifidx_reason;
6518 			dump_ifidx(drv);
6519 			return;
6520 		}
6521 	}
6522 
6523 	if (drv->if_indices != drv->default_if_indices)
6524 		old = drv->if_indices;
6525 	else
6526 		old = NULL;
6527 
6528 	if (drv->if_indices_reason != drv->default_if_indices_reason)
6529 		old_reason = drv->if_indices_reason;
6530 	else
6531 		old_reason = NULL;
6532 
6533 	drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
6534 					   sizeof(int));
6535 	drv->if_indices_reason = os_realloc_array(old_reason,
6536 						  drv->num_if_indices + 1,
6537 						  sizeof(int));
6538 	if (!drv->if_indices) {
6539 		if (!old)
6540 			drv->if_indices = drv->default_if_indices;
6541 		else
6542 			drv->if_indices = old;
6543 	}
6544 	if (!drv->if_indices_reason) {
6545 		if (!old_reason)
6546 			drv->if_indices_reason = drv->default_if_indices_reason;
6547 		else
6548 			drv->if_indices_reason = old_reason;
6549 	}
6550 	if (!drv->if_indices || !drv->if_indices_reason) {
6551 		wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6552 			   "interfaces");
6553 		wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6554 		return;
6555 	}
6556 	if (!old)
6557 		os_memcpy(drv->if_indices, drv->default_if_indices,
6558 			  sizeof(drv->default_if_indices));
6559 	if (!old_reason)
6560 		os_memcpy(drv->if_indices_reason,
6561 			  drv->default_if_indices_reason,
6562 			  sizeof(drv->default_if_indices_reason));
6563 	drv->if_indices[drv->num_if_indices] = ifidx;
6564 	drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
6565 	drv->num_if_indices++;
6566 	dump_ifidx(drv);
6567 }
6568 
6569 
6570 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6571 		      int ifidx_reason)
6572 {
6573 	int i;
6574 
6575 	for (i = 0; i < drv->num_if_indices; i++) {
6576 		if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
6577 		    (drv->if_indices_reason[i] == ifidx_reason ||
6578 		     ifidx_reason == IFIDX_ANY)) {
6579 			drv->if_indices[i] = 0;
6580 			break;
6581 		}
6582 	}
6583 	dump_ifidx(drv);
6584 }
6585 
6586 
6587 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6588 		      int ifidx_reason)
6589 {
6590 	int i;
6591 
6592 	for (i = 0; i < drv->num_if_indices; i++)
6593 		if (drv->if_indices[i] == ifidx &&
6594 		    (drv->if_indices_reason[i] == ifidx_reason ||
6595 		     ifidx_reason == IFIDX_ANY))
6596 			return 1;
6597 
6598 	return 0;
6599 }
6600 
6601 
6602 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
6603 			    const char *bridge_ifname, char *ifname_wds)
6604 {
6605 	struct i802_bss *bss = priv;
6606 	struct wpa_driver_nl80211_data *drv = bss->drv;
6607 	char name[IFNAMSIZ + 1];
6608 	union wpa_event_data event;
6609 	int ret;
6610 
6611 	ret = os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
6612 	if (ret >= (int) sizeof(name))
6613 		wpa_printf(MSG_WARNING,
6614 			   "nl80211: WDS interface name was truncated");
6615 	else if (ret < 0)
6616 		return ret;
6617 
6618 	if (ifname_wds)
6619 		os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
6620 
6621 	wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6622 		   " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6623 	if (val) {
6624 		if (!if_nametoindex(name)) {
6625 			if (nl80211_create_iface(drv, name,
6626 						 NL80211_IFTYPE_AP_VLAN,
6627 						 bss->addr, 1, NULL, NULL, 0) <
6628 			    0)
6629 				return -1;
6630 			if (bridge_ifname &&
6631 			    linux_br_add_if(drv->global->ioctl_sock,
6632 					    bridge_ifname, name) < 0)
6633 				return -1;
6634 
6635 			os_memset(&event, 0, sizeof(event));
6636 			event.wds_sta_interface.sta_addr = addr;
6637 			event.wds_sta_interface.ifname = name;
6638 			event.wds_sta_interface.istatus = INTERFACE_ADDED;
6639 			wpa_supplicant_event(bss->ctx,
6640 					     EVENT_WDS_STA_INTERFACE_STATUS,
6641 					     &event);
6642 		}
6643 		if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
6644 			wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
6645 				   "interface %s up", name);
6646 		}
6647 		return i802_set_sta_vlan(priv, addr, name, 0);
6648 	} else {
6649 		if (bridge_ifname)
6650 			linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
6651 					name);
6652 
6653 		i802_set_sta_vlan(priv, addr, bss->ifname, 0);
6654 		nl80211_remove_iface(drv, if_nametoindex(name));
6655 		os_memset(&event, 0, sizeof(event));
6656 		event.wds_sta_interface.sta_addr = addr;
6657 		event.wds_sta_interface.ifname = name;
6658 		event.wds_sta_interface.istatus = INTERFACE_REMOVED;
6659 		wpa_supplicant_event(bss->ctx, EVENT_WDS_STA_INTERFACE_STATUS,
6660 				     &event);
6661 		return 0;
6662 	}
6663 }
6664 
6665 
6666 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6667 {
6668 	struct wpa_driver_nl80211_data *drv = eloop_ctx;
6669 	struct sockaddr_ll lladdr;
6670 	unsigned char buf[3000];
6671 	int len;
6672 	socklen_t fromlen = sizeof(lladdr);
6673 
6674 	len = recvfrom(sock, buf, sizeof(buf), 0,
6675 		       (struct sockaddr *)&lladdr, &fromlen);
6676 	if (len < 0) {
6677 		wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
6678 			   strerror(errno));
6679 		return;
6680 	}
6681 
6682 	if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
6683 		drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6684 }
6685 
6686 
6687 static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
6688 			     struct i802_bss *bss,
6689 			     const char *brname, const char *ifname)
6690 {
6691 	int br_ifindex;
6692 	char in_br[IFNAMSIZ];
6693 
6694 	os_strlcpy(bss->brname, brname, IFNAMSIZ);
6695 	br_ifindex = if_nametoindex(brname);
6696 	if (br_ifindex == 0) {
6697 		/*
6698 		 * Bridge was configured, but the bridge device does
6699 		 * not exist. Try to add it now.
6700 		 */
6701 		if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
6702 			wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6703 				   "bridge interface %s: %s",
6704 				   brname, strerror(errno));
6705 			return -1;
6706 		}
6707 		bss->added_bridge = 1;
6708 		br_ifindex = if_nametoindex(brname);
6709 		add_ifidx(drv, br_ifindex, drv->ifindex);
6710 	}
6711 	bss->br_ifindex = br_ifindex;
6712 
6713 	if (linux_br_get(in_br, ifname) == 0) {
6714 		if (os_strcmp(in_br, brname) == 0) {
6715 			bss->already_in_bridge = 1;
6716 			return 0; /* already in the bridge */
6717 		}
6718 
6719 		wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6720 			   "bridge %s", ifname, in_br);
6721 		if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6722 		    0) {
6723 			wpa_printf(MSG_ERROR, "nl80211: Failed to "
6724 				   "remove interface %s from bridge "
6725 				   "%s: %s",
6726 				   ifname, in_br, strerror(errno));
6727 			return -1;
6728 		}
6729 	}
6730 
6731 	wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6732 		   ifname, brname);
6733 	if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
6734 		wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6735 			   "into bridge %s: %s",
6736 			   ifname, brname, strerror(errno));
6737 		return -1;
6738 	}
6739 	bss->added_if_into_bridge = 1;
6740 
6741 	return 0;
6742 }
6743 
6744 
6745 static void *i802_init(struct hostapd_data *hapd,
6746 		       struct wpa_init_params *params)
6747 {
6748 	struct wpa_driver_nl80211_data *drv;
6749 	struct i802_bss *bss;
6750 	size_t i;
6751 	char master_ifname[IFNAMSIZ];
6752 	int ifindex, br_ifindex = 0;
6753 	int br_added = 0;
6754 
6755 	bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6756 					  params->global_priv, 1,
6757 					  params->bssid, params->driver_params);
6758 	if (bss == NULL)
6759 		return NULL;
6760 
6761 	drv = bss->drv;
6762 
6763 	if (linux_br_get(master_ifname, params->ifname) == 0) {
6764 		wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
6765 			   params->ifname, master_ifname);
6766 		br_ifindex = if_nametoindex(master_ifname);
6767 		os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6768 	} else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6769 		   linux_master_get(master_ifname, params->ifname) == 0) {
6770 		wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6771 			params->ifname, master_ifname);
6772 		/* start listening for EAPOL on the master interface */
6773 		add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
6774 
6775 		/* check if master itself is under bridge */
6776 		if (linux_br_get(master_ifname, master_ifname) == 0) {
6777 			wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6778 				   master_ifname);
6779 			br_ifindex = if_nametoindex(master_ifname);
6780 			os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6781 		}
6782 	} else {
6783 		master_ifname[0] = '\0';
6784 	}
6785 
6786 	bss->br_ifindex = br_ifindex;
6787 
6788 	for (i = 0; i < params->num_bridge; i++) {
6789 		if (params->bridge[i]) {
6790 			ifindex = if_nametoindex(params->bridge[i]);
6791 			if (ifindex)
6792 				add_ifidx(drv, ifindex, drv->ifindex);
6793 			if (ifindex == br_ifindex)
6794 				br_added = 1;
6795 		}
6796 	}
6797 
6798 	/* start listening for EAPOL on the default AP interface */
6799 	add_ifidx(drv, drv->ifindex, IFIDX_ANY);
6800 
6801 	if (params->num_bridge && params->bridge[0]) {
6802 		if (i802_check_bridge(drv, bss, params->bridge[0],
6803 				      params->ifname) < 0)
6804 			goto failed;
6805 		if (os_strcmp(params->bridge[0], master_ifname) != 0)
6806 			br_added = 1;
6807 	}
6808 
6809 	if (!br_added && br_ifindex &&
6810 	    (params->num_bridge == 0 || !params->bridge[0]))
6811 		add_ifidx(drv, br_ifindex, drv->ifindex);
6812 
6813 #ifdef CONFIG_LIBNL3_ROUTE
6814 	if (bss->added_if_into_bridge || bss->already_in_bridge) {
6815 		drv->rtnl_sk = nl_socket_alloc();
6816 		if (drv->rtnl_sk == NULL) {
6817 			wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6818 			goto failed;
6819 		}
6820 
6821 		if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6822 			wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6823 				   strerror(errno));
6824 			goto failed;
6825 		}
6826 	}
6827 #endif /* CONFIG_LIBNL3_ROUTE */
6828 
6829 	drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6830 	if (drv->eapol_sock < 0) {
6831 		wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6832 			   strerror(errno));
6833 		goto failed;
6834 	}
6835 
6836 	if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6837 	{
6838 		wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
6839 		goto failed;
6840 	}
6841 
6842 	if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6843 			       params->own_addr))
6844 		goto failed;
6845 	os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
6846 
6847 	memcpy(bss->addr, params->own_addr, ETH_ALEN);
6848 
6849 	return bss;
6850 
6851 failed:
6852 	wpa_driver_nl80211_deinit(bss);
6853 	return NULL;
6854 }
6855 
6856 
6857 static void i802_deinit(void *priv)
6858 {
6859 	struct i802_bss *bss = priv;
6860 	wpa_driver_nl80211_deinit(bss);
6861 }
6862 
6863 
6864 static enum nl80211_iftype wpa_driver_nl80211_if_type(
6865 	enum wpa_driver_if_type type)
6866 {
6867 	switch (type) {
6868 	case WPA_IF_STATION:
6869 		return NL80211_IFTYPE_STATION;
6870 	case WPA_IF_P2P_CLIENT:
6871 	case WPA_IF_P2P_GROUP:
6872 		return NL80211_IFTYPE_P2P_CLIENT;
6873 	case WPA_IF_AP_VLAN:
6874 		return NL80211_IFTYPE_AP_VLAN;
6875 	case WPA_IF_AP_BSS:
6876 		return NL80211_IFTYPE_AP;
6877 	case WPA_IF_P2P_GO:
6878 		return NL80211_IFTYPE_P2P_GO;
6879 	case WPA_IF_P2P_DEVICE:
6880 		return NL80211_IFTYPE_P2P_DEVICE;
6881 	case WPA_IF_MESH:
6882 		return NL80211_IFTYPE_MESH_POINT;
6883 	default:
6884 		return -1;
6885 	}
6886 }
6887 
6888 
6889 static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6890 {
6891 	struct wpa_driver_nl80211_data *drv;
6892 	dl_list_for_each(drv, &global->interfaces,
6893 			 struct wpa_driver_nl80211_data, list) {
6894 		if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
6895 			return 1;
6896 	}
6897 	return 0;
6898 }
6899 
6900 
6901 static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
6902 {
6903 	unsigned int idx;
6904 
6905 	if (!drv->global)
6906 		return -1;
6907 
6908 	os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
6909 	for (idx = 0; idx < 64; idx++) {
6910 		new_addr[0] = drv->first_bss->addr[0] | 0x02;
6911 		new_addr[0] ^= idx << 2;
6912 		if (!nl80211_addr_in_use(drv->global, new_addr))
6913 			break;
6914 	}
6915 	if (idx == 64)
6916 		return -1;
6917 
6918 	wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
6919 		   MACSTR, MAC2STR(new_addr));
6920 
6921 	return 0;
6922 }
6923 
6924 
6925 struct wdev_info {
6926 	u64 wdev_id;
6927 	int wdev_id_set;
6928 	u8 macaddr[ETH_ALEN];
6929 };
6930 
6931 static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
6932 {
6933 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6934 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
6935 	struct wdev_info *wi = arg;
6936 
6937 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6938 		  genlmsg_attrlen(gnlh, 0), NULL);
6939 	if (tb[NL80211_ATTR_WDEV]) {
6940 		wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
6941 		wi->wdev_id_set = 1;
6942 	}
6943 
6944 	if (tb[NL80211_ATTR_MAC])
6945 		os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
6946 			  ETH_ALEN);
6947 
6948 	return NL_SKIP;
6949 }
6950 
6951 
6952 static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6953 				     const char *ifname, const u8 *addr,
6954 				     void *bss_ctx, void **drv_priv,
6955 				     char *force_ifname, u8 *if_addr,
6956 				     const char *bridge, int use_existing,
6957 				     int setup_ap)
6958 {
6959 	enum nl80211_iftype nlmode;
6960 	struct i802_bss *bss = priv;
6961 	struct wpa_driver_nl80211_data *drv = bss->drv;
6962 	int ifidx;
6963 	int added = 1;
6964 
6965 	if (addr)
6966 		os_memcpy(if_addr, addr, ETH_ALEN);
6967 	nlmode = wpa_driver_nl80211_if_type(type);
6968 	if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
6969 		struct wdev_info p2pdev_info;
6970 
6971 		os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
6972 		ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6973 					     0, nl80211_wdev_handler,
6974 					     &p2pdev_info, use_existing);
6975 		if (!p2pdev_info.wdev_id_set || ifidx != 0) {
6976 			wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
6977 				   ifname);
6978 			return -1;
6979 		}
6980 
6981 		drv->global->if_add_wdevid = p2pdev_info.wdev_id;
6982 		drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
6983 		if (!is_zero_ether_addr(p2pdev_info.macaddr))
6984 			os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
6985 		wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
6986 			   ifname,
6987 			   (long long unsigned int) p2pdev_info.wdev_id);
6988 	} else {
6989 		ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6990 					     0, NULL, NULL, use_existing);
6991 		if (use_existing && ifidx == -ENFILE) {
6992 			added = 0;
6993 			ifidx = if_nametoindex(ifname);
6994 		} else if (ifidx < 0) {
6995 			return -1;
6996 		}
6997 	}
6998 
6999 	if (!addr) {
7000 		if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
7001 			os_memcpy(if_addr, bss->addr, ETH_ALEN);
7002 		else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
7003 					    ifname, if_addr) < 0) {
7004 			if (added)
7005 				nl80211_remove_iface(drv, ifidx);
7006 			return -1;
7007 		}
7008 	}
7009 
7010 	if (!addr &&
7011 	    (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
7012 	     type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
7013 	     type == WPA_IF_STATION)) {
7014 		/* Enforce unique address */
7015 		u8 new_addr[ETH_ALEN];
7016 
7017 		if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
7018 				       new_addr) < 0) {
7019 			if (added)
7020 				nl80211_remove_iface(drv, ifidx);
7021 			return -1;
7022 		}
7023 		if (nl80211_addr_in_use(drv->global, new_addr)) {
7024 			wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
7025 				   "for interface %s type %d", ifname, type);
7026 			if (nl80211_vif_addr(drv, new_addr) < 0) {
7027 				if (added)
7028 					nl80211_remove_iface(drv, ifidx);
7029 				return -1;
7030 			}
7031 			if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7032 					       new_addr) < 0) {
7033 				if (added)
7034 					nl80211_remove_iface(drv, ifidx);
7035 				return -1;
7036 			}
7037 		}
7038 		os_memcpy(if_addr, new_addr, ETH_ALEN);
7039 	}
7040 
7041 	if (type == WPA_IF_AP_BSS && setup_ap) {
7042 		struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
7043 		if (new_bss == NULL) {
7044 			if (added)
7045 				nl80211_remove_iface(drv, ifidx);
7046 			return -1;
7047 		}
7048 
7049 		if (bridge &&
7050 		    i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
7051 			wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
7052 				   "interface %s to a bridge %s",
7053 				   ifname, bridge);
7054 			if (added)
7055 				nl80211_remove_iface(drv, ifidx);
7056 			os_free(new_bss);
7057 			return -1;
7058 		}
7059 
7060 		if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
7061 		{
7062 			if (added)
7063 				nl80211_remove_iface(drv, ifidx);
7064 			os_free(new_bss);
7065 			return -1;
7066 		}
7067 		os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
7068 		os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
7069 		new_bss->ifindex = ifidx;
7070 		new_bss->drv = drv;
7071 		new_bss->next = drv->first_bss->next;
7072 		new_bss->freq = drv->first_bss->freq;
7073 		new_bss->ctx = bss_ctx;
7074 		new_bss->added_if = added;
7075 		drv->first_bss->next = new_bss;
7076 		if (drv_priv)
7077 			*drv_priv = new_bss;
7078 		nl80211_init_bss(new_bss);
7079 
7080 		/* Subscribe management frames for this WPA_IF_AP_BSS */
7081 		if (nl80211_setup_ap(new_bss))
7082 			return -1;
7083 	}
7084 
7085 	if (drv->global)
7086 		drv->global->if_add_ifindex = ifidx;
7087 
7088 	/*
7089 	 * Some virtual interfaces need to process EAPOL packets and events on
7090 	 * the parent interface. This is used mainly with hostapd.
7091 	 */
7092 	if (ifidx > 0 &&
7093 	    (drv->hostapd ||
7094 	     nlmode == NL80211_IFTYPE_AP_VLAN ||
7095 	     nlmode == NL80211_IFTYPE_WDS ||
7096 	     nlmode == NL80211_IFTYPE_MONITOR))
7097 		add_ifidx(drv, ifidx, IFIDX_ANY);
7098 
7099 	return 0;
7100 }
7101 
7102 
7103 static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
7104 					enum wpa_driver_if_type type,
7105 					const char *ifname)
7106 {
7107 	struct wpa_driver_nl80211_data *drv = bss->drv;
7108 	int ifindex = if_nametoindex(ifname);
7109 
7110 	wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
7111 		   __func__, type, ifname, ifindex, bss->added_if);
7112 	if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
7113 		nl80211_remove_iface(drv, ifindex);
7114 	else if (ifindex > 0 && !bss->added_if) {
7115 		struct wpa_driver_nl80211_data *drv2;
7116 		dl_list_for_each(drv2, &drv->global->interfaces,
7117 				 struct wpa_driver_nl80211_data, list) {
7118 			del_ifidx(drv2, ifindex, IFIDX_ANY);
7119 			del_ifidx(drv2, IFIDX_ANY, ifindex);
7120 		}
7121 	}
7122 
7123 	if (type != WPA_IF_AP_BSS)
7124 		return 0;
7125 
7126 	if (bss->added_if_into_bridge) {
7127 		if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
7128 				    bss->ifname) < 0)
7129 			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7130 				   "interface %s from bridge %s: %s",
7131 				   bss->ifname, bss->brname, strerror(errno));
7132 	}
7133 	if (bss->added_bridge) {
7134 		if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
7135 			wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7136 				   "bridge %s: %s",
7137 				   bss->brname, strerror(errno));
7138 	}
7139 
7140 	if (bss != drv->first_bss) {
7141 		struct i802_bss *tbss;
7142 
7143 		wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
7144 		for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
7145 			if (tbss->next == bss) {
7146 				tbss->next = bss->next;
7147 				/* Unsubscribe management frames */
7148 				nl80211_teardown_ap(bss);
7149 				nl80211_destroy_bss(bss);
7150 				if (!bss->added_if)
7151 					i802_set_iface_flags(bss, 0);
7152 				os_free(bss);
7153 				bss = NULL;
7154 				break;
7155 			}
7156 		}
7157 		if (bss)
7158 			wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7159 				   "BSS %p in the list", __func__, bss);
7160 	} else {
7161 		wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
7162 		nl80211_teardown_ap(bss);
7163 		if (!bss->added_if && !drv->first_bss->next)
7164 			wpa_driver_nl80211_del_beacon(bss);
7165 		nl80211_destroy_bss(bss);
7166 		if (!bss->added_if)
7167 			i802_set_iface_flags(bss, 0);
7168 		if (drv->first_bss->next) {
7169 			drv->first_bss = drv->first_bss->next;
7170 			drv->ctx = drv->first_bss->ctx;
7171 			os_free(bss);
7172 		} else {
7173 			wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
7174 		}
7175 	}
7176 
7177 	return 0;
7178 }
7179 
7180 
7181 static int cookie_handler(struct nl_msg *msg, void *arg)
7182 {
7183 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
7184 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7185 	u64 *cookie = arg;
7186 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7187 		  genlmsg_attrlen(gnlh, 0), NULL);
7188 	if (tb[NL80211_ATTR_COOKIE])
7189 		*cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7190 	return NL_SKIP;
7191 }
7192 
7193 
7194 static int nl80211_send_frame_cmd(struct i802_bss *bss,
7195 				  unsigned int freq, unsigned int wait,
7196 				  const u8 *buf, size_t buf_len,
7197 				  u64 *cookie_out, int no_cck, int no_ack,
7198 				  int offchanok, const u16 *csa_offs,
7199 				  size_t csa_offs_len)
7200 {
7201 	struct wpa_driver_nl80211_data *drv = bss->drv;
7202 	struct nl_msg *msg;
7203 	u64 cookie;
7204 	int ret = -1;
7205 
7206 	wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
7207 		   "no_ack=%d offchanok=%d",
7208 		   freq, wait, no_cck, no_ack, offchanok);
7209 	wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
7210 
7211 	if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
7212 	    (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
7213 	    (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
7214 	    (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7215 			   drv->test_use_roc_tx) &&
7216 	     nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
7217 	    (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
7218 	    (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
7219 	    (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
7220 				 csa_offs_len * sizeof(u16), csa_offs)) ||
7221 	    nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
7222 		goto fail;
7223 
7224 	cookie = 0;
7225 	ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7226 	msg = NULL;
7227 	if (ret) {
7228 		wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
7229 			   "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7230 			   freq, wait);
7231 	} else {
7232 		wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
7233 			   "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7234 			   (long long unsigned int) cookie);
7235 
7236 		if (cookie_out)
7237 			*cookie_out = no_ack ? (u64) -1 : cookie;
7238 
7239 		if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
7240 			wpa_printf(MSG_DEBUG,
7241 				   "nl80211: Drop oldest pending send action cookie 0x%llx",
7242 				   (long long unsigned int)
7243 				   drv->send_action_cookies[0]);
7244 			os_memmove(&drv->send_action_cookies[0],
7245 				   &drv->send_action_cookies[1],
7246 				   (MAX_SEND_ACTION_COOKIES - 1) *
7247 				   sizeof(u64));
7248 			drv->num_send_action_cookies--;
7249 		}
7250 		drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
7251 		drv->num_send_action_cookies++;
7252 	}
7253 
7254 fail:
7255 	nlmsg_free(msg);
7256 	return ret;
7257 }
7258 
7259 
7260 static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
7261 					  unsigned int freq,
7262 					  unsigned int wait_time,
7263 					  const u8 *dst, const u8 *src,
7264 					  const u8 *bssid,
7265 					  const u8 *data, size_t data_len,
7266 					  int no_cck)
7267 {
7268 	struct wpa_driver_nl80211_data *drv = bss->drv;
7269 	int ret = -1;
7270 	u8 *buf;
7271 	struct ieee80211_hdr *hdr;
7272 
7273 	wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
7274 		   "freq=%u MHz wait=%d ms no_cck=%d)",
7275 		   drv->ifindex, freq, wait_time, no_cck);
7276 
7277 	buf = os_zalloc(24 + data_len);
7278 	if (buf == NULL)
7279 		return ret;
7280 	os_memcpy(buf + 24, data, data_len);
7281 	hdr = (struct ieee80211_hdr *) buf;
7282 	hdr->frame_control =
7283 		IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
7284 	os_memcpy(hdr->addr1, dst, ETH_ALEN);
7285 	os_memcpy(hdr->addr2, src, ETH_ALEN);
7286 	os_memcpy(hdr->addr3, bssid, ETH_ALEN);
7287 
7288 	if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
7289 		wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
7290 			   MAC2STR(src));
7291 		os_memcpy(bss->rand_addr, src, ETH_ALEN);
7292 	} else {
7293 		os_memset(bss->rand_addr, 0, ETH_ALEN);
7294 	}
7295 
7296 	if (is_ap_interface(drv->nlmode) &&
7297 	    (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7298 	     (int) freq == bss->freq || drv->device_ap_sme ||
7299 	     !drv->use_monitor))
7300 		ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
7301 						   0, freq, no_cck, 1,
7302 						   wait_time, NULL, 0);
7303 	else
7304 		ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
7305 					     24 + data_len,
7306 					     &drv->send_action_cookie,
7307 					     no_cck, 0, 1, NULL, 0);
7308 
7309 	os_free(buf);
7310 	return ret;
7311 }
7312 
7313 
7314 static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
7315 {
7316 	struct wpa_driver_nl80211_data *drv = bss->drv;
7317 	struct nl_msg *msg;
7318 	int ret;
7319 
7320 	wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
7321 		   (long long unsigned int) cookie);
7322 	if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
7323 	    nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
7324 		nlmsg_free(msg);
7325 		return;
7326 	}
7327 
7328 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7329 	if (ret)
7330 		wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
7331 			   "(%s)", ret, strerror(-ret));
7332 }
7333 
7334 
7335 static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
7336 {
7337 	struct i802_bss *bss = priv;
7338 	struct wpa_driver_nl80211_data *drv = bss->drv;
7339 	unsigned int i;
7340 	u64 cookie;
7341 
7342 	/* Cancel the last pending TX cookie */
7343 	nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
7344 
7345 	/*
7346 	 * Cancel the other pending TX cookies, if any. This is needed since
7347 	 * the driver may keep a list of all pending offchannel TX operations
7348 	 * and free up the radio only once they have expired or cancelled.
7349 	 */
7350 	for (i = drv->num_send_action_cookies; i > 0; i--) {
7351 		cookie = drv->send_action_cookies[i - 1];
7352 		if (cookie != drv->send_action_cookie)
7353 			nl80211_frame_wait_cancel(bss, cookie);
7354 	}
7355 	drv->num_send_action_cookies = 0;
7356 }
7357 
7358 
7359 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
7360 						unsigned int duration)
7361 {
7362 	struct i802_bss *bss = priv;
7363 	struct wpa_driver_nl80211_data *drv = bss->drv;
7364 	struct nl_msg *msg;
7365 	int ret;
7366 	u64 cookie;
7367 
7368 	if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
7369 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7370 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
7371 		nlmsg_free(msg);
7372 		return -1;
7373 	}
7374 
7375 	cookie = 0;
7376 	ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7377 	if (ret == 0) {
7378 		wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
7379 			   "0x%llx for freq=%u MHz duration=%u",
7380 			   (long long unsigned int) cookie, freq, duration);
7381 		drv->remain_on_chan_cookie = cookie;
7382 		drv->pending_remain_on_chan = 1;
7383 		return 0;
7384 	}
7385 	wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
7386 		   "(freq=%d duration=%u): %d (%s)",
7387 		   freq, duration, ret, strerror(-ret));
7388 	return -1;
7389 }
7390 
7391 
7392 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
7393 {
7394 	struct i802_bss *bss = priv;
7395 	struct wpa_driver_nl80211_data *drv = bss->drv;
7396 	struct nl_msg *msg;
7397 	int ret;
7398 
7399 	if (!drv->pending_remain_on_chan) {
7400 		wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
7401 			   "to cancel");
7402 		return -1;
7403 	}
7404 
7405 	wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
7406 		   "0x%llx",
7407 		   (long long unsigned int) drv->remain_on_chan_cookie);
7408 
7409 	msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7410 	if (!msg ||
7411 	    nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
7412 		nlmsg_free(msg);
7413 		return -1;
7414 	}
7415 
7416 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7417 	if (ret == 0)
7418 		return 0;
7419 	wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7420 		   "%d (%s)", ret, strerror(-ret));
7421 	return -1;
7422 }
7423 
7424 
7425 static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
7426 {
7427 	struct wpa_driver_nl80211_data *drv = bss->drv;
7428 
7429 	if (!report) {
7430 		if (bss->nl_preq && drv->device_ap_sme &&
7431 		    is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7432 		    !bss->static_ap) {
7433 			/*
7434 			 * Do not disable Probe Request reporting that was
7435 			 * enabled in nl80211_setup_ap().
7436 			 */
7437 			wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7438 				   "Probe Request reporting nl_preq=%p while "
7439 				   "in AP mode", bss->nl_preq);
7440 		} else if (bss->nl_preq) {
7441 			wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7442 				   "reporting nl_preq=%p", bss->nl_preq);
7443 			nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
7444 		}
7445 		return 0;
7446 	}
7447 
7448 	if (bss->nl_preq) {
7449 		wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
7450 			   "already on! nl_preq=%p", bss->nl_preq);
7451 		return 0;
7452 	}
7453 
7454 	bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7455 	if (bss->nl_preq == NULL)
7456 		return -1;
7457 	wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7458 		   "reporting nl_preq=%p", bss->nl_preq);
7459 
7460 	if (nl80211_register_frame(bss, bss->nl_preq,
7461 				   (WLAN_FC_TYPE_MGMT << 2) |
7462 				   (WLAN_FC_STYPE_PROBE_REQ << 4),
7463 				   NULL, 0) < 0)
7464 		goto out_err;
7465 
7466 	nl80211_register_eloop_read(&bss->nl_preq,
7467 				    wpa_driver_nl80211_event_receive,
7468 				    bss->nl_cb, 0);
7469 
7470 	return 0;
7471 
7472  out_err:
7473 	nl_destroy_handles(&bss->nl_preq);
7474 	return -1;
7475 }
7476 
7477 
7478 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7479 				     int ifindex, int disabled)
7480 {
7481 	struct nl_msg *msg;
7482 	struct nlattr *bands, *band;
7483 	int ret;
7484 
7485 	wpa_printf(MSG_DEBUG,
7486 		   "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
7487 		   ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
7488 		   "no NL80211_TXRATE_LEGACY constraint");
7489 
7490 	msg = nl80211_ifindex_msg(drv, ifindex, 0,
7491 				  NL80211_CMD_SET_TX_BITRATE_MASK);
7492 	if (!msg)
7493 		return -1;
7494 
7495 	bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7496 	if (!bands)
7497 		goto fail;
7498 
7499 	/*
7500 	 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7501 	 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7502 	 * rates. All 5 GHz rates are left enabled.
7503 	 */
7504 	band = nla_nest_start(msg, NL80211_BAND_2GHZ);
7505 	if (!band ||
7506 	    (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
7507 				 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
7508 		goto fail;
7509 	nla_nest_end(msg, band);
7510 
7511 	nla_nest_end(msg, bands);
7512 
7513 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7514 	if (ret) {
7515 		wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7516 			   "(%s)", ret, strerror(-ret));
7517 	} else
7518 		drv->disabled_11b_rates = disabled;
7519 
7520 	return ret;
7521 
7522 fail:
7523 	nlmsg_free(msg);
7524 	return -1;
7525 }
7526 
7527 
7528 static int wpa_driver_nl80211_deinit_ap(void *priv)
7529 {
7530 	struct i802_bss *bss = priv;
7531 	struct wpa_driver_nl80211_data *drv = bss->drv;
7532 	if (!is_ap_interface(drv->nlmode))
7533 		return -1;
7534 	wpa_driver_nl80211_del_beacon(bss);
7535 	bss->beacon_set = 0;
7536 
7537 	/*
7538 	 * If the P2P GO interface was dynamically added, then it is
7539 	 * possible that the interface change to station is not possible.
7540 	 */
7541 	if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
7542 		return 0;
7543 
7544 	return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7545 }
7546 
7547 
7548 static int wpa_driver_nl80211_stop_ap(void *priv)
7549 {
7550 	struct i802_bss *bss = priv;
7551 	struct wpa_driver_nl80211_data *drv = bss->drv;
7552 	if (!is_ap_interface(drv->nlmode))
7553 		return -1;
7554 	wpa_driver_nl80211_del_beacon(bss);
7555 	bss->beacon_set = 0;
7556 	return 0;
7557 }
7558 
7559 
7560 static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
7561 {
7562 	struct i802_bss *bss = priv;
7563 	struct wpa_driver_nl80211_data *drv = bss->drv;
7564 	if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
7565 		return -1;
7566 
7567 	/*
7568 	 * If the P2P Client interface was dynamically added, then it is
7569 	 * possible that the interface change to station is not possible.
7570 	 */
7571 	if (bss->if_dynamic)
7572 		return 0;
7573 
7574 	return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7575 }
7576 
7577 
7578 static void wpa_driver_nl80211_resume(void *priv)
7579 {
7580 	struct i802_bss *bss = priv;
7581 	enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
7582 
7583 	if (i802_set_iface_flags(bss, 1))
7584 		wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
7585 
7586 	if (is_p2p_net_interface(nlmode))
7587 		nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
7588 }
7589 
7590 
7591 static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7592 {
7593 	struct i802_bss *bss = priv;
7594 	struct wpa_driver_nl80211_data *drv = bss->drv;
7595 	struct nl_msg *msg;
7596 	struct nlattr *cqm;
7597 
7598 	wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7599 		   "hysteresis=%d", threshold, hysteresis);
7600 
7601 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
7602 	    !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
7603 	    nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
7604 	    nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
7605 		nlmsg_free(msg);
7606 		return -1;
7607 	}
7608 	nla_nest_end(msg, cqm);
7609 
7610 	return send_and_recv_msgs(drv, msg, NULL, NULL);
7611 }
7612 
7613 
7614 static int get_channel_width(struct nl_msg *msg, void *arg)
7615 {
7616 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
7617 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7618 	struct wpa_signal_info *sig_change = arg;
7619 
7620 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7621 		  genlmsg_attrlen(gnlh, 0), NULL);
7622 
7623 	sig_change->center_frq1 = -1;
7624 	sig_change->center_frq2 = -1;
7625 	sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
7626 
7627 	if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
7628 		sig_change->chanwidth = convert2width(
7629 			nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
7630 		if (tb[NL80211_ATTR_CENTER_FREQ1])
7631 			sig_change->center_frq1 =
7632 				nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
7633 		if (tb[NL80211_ATTR_CENTER_FREQ2])
7634 			sig_change->center_frq2 =
7635 				nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
7636 	}
7637 
7638 	return NL_SKIP;
7639 }
7640 
7641 
7642 static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
7643 				     struct wpa_signal_info *sig)
7644 {
7645 	struct nl_msg *msg;
7646 
7647 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
7648 	return send_and_recv_msgs(drv, msg, get_channel_width, sig);
7649 }
7650 
7651 
7652 static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7653 {
7654 	struct i802_bss *bss = priv;
7655 	struct wpa_driver_nl80211_data *drv = bss->drv;
7656 	int res;
7657 
7658 	os_memset(si, 0, sizeof(*si));
7659 	res = nl80211_get_link_signal(drv, si);
7660 	if (res) {
7661 		if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
7662 		    drv->nlmode != NL80211_IFTYPE_MESH_POINT)
7663 			return res;
7664 		si->current_signal = 0;
7665 	}
7666 
7667 	res = nl80211_get_channel_width(drv, si);
7668 	if (res != 0)
7669 		return res;
7670 
7671 	return nl80211_get_link_noise(drv, si);
7672 }
7673 
7674 
7675 static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7676 			      int encrypt)
7677 {
7678 	struct i802_bss *bss = priv;
7679 	return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
7680 					     0, 0, 0, 0, NULL, 0);
7681 }
7682 
7683 
7684 static int nl80211_set_param(void *priv, const char *param)
7685 {
7686 	struct i802_bss *bss = priv;
7687 	struct wpa_driver_nl80211_data *drv = bss->drv;
7688 
7689 	if (param == NULL)
7690 		return 0;
7691 	wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
7692 
7693 #ifdef CONFIG_P2P
7694 	if (os_strstr(param, "use_p2p_group_interface=1")) {
7695 		wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7696 			   "interface");
7697 		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7698 		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7699 	}
7700 #endif /* CONFIG_P2P */
7701 
7702 	if (os_strstr(param, "use_monitor=1"))
7703 		drv->use_monitor = 1;
7704 
7705 	if (os_strstr(param, "force_connect_cmd=1")) {
7706 		drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
7707 		drv->force_connect_cmd = 1;
7708 	}
7709 
7710 	if (os_strstr(param, "force_bss_selection=1"))
7711 		drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
7712 
7713 	if (os_strstr(param, "no_offchannel_tx=1")) {
7714 		drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
7715 		drv->test_use_roc_tx = 1;
7716 	}
7717 
7718 	return 0;
7719 }
7720 
7721 
7722 static void * nl80211_global_init(void *ctx)
7723 {
7724 	struct nl80211_global *global;
7725 	struct netlink_config *cfg;
7726 
7727 	global = os_zalloc(sizeof(*global));
7728 	if (global == NULL)
7729 		return NULL;
7730 	global->ctx = ctx;
7731 	global->ioctl_sock = -1;
7732 	dl_list_init(&global->interfaces);
7733 	global->if_add_ifindex = -1;
7734 
7735 	cfg = os_zalloc(sizeof(*cfg));
7736 	if (cfg == NULL)
7737 		goto err;
7738 
7739 	cfg->ctx = global;
7740 	cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7741 	cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7742 	global->netlink = netlink_init(cfg);
7743 	if (global->netlink == NULL) {
7744 		os_free(cfg);
7745 		goto err;
7746 	}
7747 
7748 	if (wpa_driver_nl80211_init_nl_global(global) < 0)
7749 		goto err;
7750 
7751 	global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7752 	if (global->ioctl_sock < 0) {
7753 		wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7754 			   strerror(errno));
7755 		goto err;
7756 	}
7757 
7758 	return global;
7759 
7760 err:
7761 	nl80211_global_deinit(global);
7762 	return NULL;
7763 }
7764 
7765 
7766 static void nl80211_global_deinit(void *priv)
7767 {
7768 	struct nl80211_global *global = priv;
7769 	if (global == NULL)
7770 		return;
7771 	if (!dl_list_empty(&global->interfaces)) {
7772 		wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7773 			   "nl80211_global_deinit",
7774 			   dl_list_len(&global->interfaces));
7775 	}
7776 
7777 	if (global->netlink)
7778 		netlink_deinit(global->netlink);
7779 
7780 	nl_destroy_handles(&global->nl);
7781 
7782 	if (global->nl_event)
7783 		nl80211_destroy_eloop_handle(&global->nl_event, 0);
7784 
7785 	nl_cb_put(global->nl_cb);
7786 
7787 	if (global->ioctl_sock >= 0)
7788 		close(global->ioctl_sock);
7789 
7790 	os_free(global);
7791 }
7792 
7793 
7794 static const char * nl80211_get_radio_name(void *priv)
7795 {
7796 	struct i802_bss *bss = priv;
7797 	struct wpa_driver_nl80211_data *drv = bss->drv;
7798 	return drv->phyname;
7799 }
7800 
7801 
7802 static int nl80211_pmkid(struct i802_bss *bss, int cmd,
7803 			 struct wpa_pmkid_params *params)
7804 {
7805 	struct nl_msg *msg;
7806 	const size_t PMK_MAX_LEN = 48; /* current cfg80211 limit */
7807 
7808 	if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
7809 	    (params->pmkid &&
7810 	     nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
7811 	    (params->bssid &&
7812 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
7813 	    (params->ssid_len &&
7814 	     nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
7815 	    (params->fils_cache_id &&
7816 	     nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
7817 		     params->fils_cache_id)) ||
7818 	    (params->pmk_len && params->pmk_len <= PMK_MAX_LEN &&
7819 	     nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
7820 		nlmsg_free(msg);
7821 		return -ENOBUFS;
7822 	}
7823 
7824 	return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
7825 }
7826 
7827 
7828 static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
7829 {
7830 	struct i802_bss *bss = priv;
7831 	int ret;
7832 
7833 	if (params->bssid)
7834 		wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
7835 			   MAC2STR(params->bssid));
7836 	else if (params->fils_cache_id && params->ssid_len) {
7837 		wpa_printf(MSG_DEBUG,
7838 			   "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
7839 			   params->fils_cache_id[0], params->fils_cache_id[1],
7840 			   wpa_ssid_txt(params->ssid, params->ssid_len));
7841 	}
7842 
7843 	ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params);
7844 	if (ret < 0) {
7845 		wpa_printf(MSG_DEBUG,
7846 			   "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
7847 			   ret, strerror(-ret));
7848 	}
7849 
7850 	return ret;
7851 }
7852 
7853 
7854 static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
7855 {
7856 	struct i802_bss *bss = priv;
7857 	int ret;
7858 
7859 	if (params->bssid)
7860 		wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7861 			   MAC2STR(params->bssid));
7862 	else if (params->fils_cache_id && params->ssid_len) {
7863 		wpa_printf(MSG_DEBUG,
7864 			   "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
7865 			   params->fils_cache_id[0], params->fils_cache_id[1],
7866 			   wpa_ssid_txt(params->ssid, params->ssid_len));
7867 	}
7868 
7869 	ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params);
7870 	if (ret < 0) {
7871 		wpa_printf(MSG_DEBUG,
7872 			   "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
7873 			   ret, strerror(-ret));
7874 	}
7875 
7876 	return ret;
7877 }
7878 
7879 
7880 static int nl80211_flush_pmkid(void *priv)
7881 {
7882 	struct i802_bss *bss = priv;
7883 	struct nl_msg *msg;
7884 
7885 	wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
7886 	msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
7887 	if (!msg)
7888 		return -ENOBUFS;
7889 	return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
7890 }
7891 
7892 
7893 static void clean_survey_results(struct survey_results *survey_results)
7894 {
7895 	struct freq_survey *survey, *tmp;
7896 
7897 	if (dl_list_empty(&survey_results->survey_list))
7898 		return;
7899 
7900 	dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
7901 			      struct freq_survey, list) {
7902 		dl_list_del(&survey->list);
7903 		os_free(survey);
7904 	}
7905 }
7906 
7907 
7908 static void add_survey(struct nlattr **sinfo, u32 ifidx,
7909 		       struct dl_list *survey_list)
7910 {
7911 	struct freq_survey *survey;
7912 
7913 	survey = os_zalloc(sizeof(struct freq_survey));
7914 	if  (!survey)
7915 		return;
7916 
7917 	survey->ifidx = ifidx;
7918 	survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7919 	survey->filled = 0;
7920 
7921 	if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
7922 		survey->nf = (int8_t)
7923 			nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
7924 		survey->filled |= SURVEY_HAS_NF;
7925 	}
7926 
7927 	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
7928 		survey->channel_time =
7929 			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
7930 		survey->filled |= SURVEY_HAS_CHAN_TIME;
7931 	}
7932 
7933 	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
7934 		survey->channel_time_busy =
7935 			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
7936 		survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
7937 	}
7938 
7939 	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
7940 		survey->channel_time_rx =
7941 			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
7942 		survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
7943 	}
7944 
7945 	if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
7946 		survey->channel_time_tx =
7947 			nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
7948 		survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
7949 	}
7950 
7951 	wpa_printf(MSG_DEBUG, "nl80211: Freq survey dump event (freq=%d MHz noise=%d channel_time=%ld busy_time=%ld tx_time=%ld rx_time=%ld filled=%04x)",
7952 		   survey->freq,
7953 		   survey->nf,
7954 		   (unsigned long int) survey->channel_time,
7955 		   (unsigned long int) survey->channel_time_busy,
7956 		   (unsigned long int) survey->channel_time_tx,
7957 		   (unsigned long int) survey->channel_time_rx,
7958 		   survey->filled);
7959 
7960 	dl_list_add_tail(survey_list, &survey->list);
7961 }
7962 
7963 
7964 static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
7965 			   unsigned int freq_filter)
7966 {
7967 	if (!freq_filter)
7968 		return 1;
7969 
7970 	return freq_filter == surveyed_freq;
7971 }
7972 
7973 
7974 static int survey_handler(struct nl_msg *msg, void *arg)
7975 {
7976 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
7977 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7978 	struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
7979 	struct survey_results *survey_results;
7980 	u32 surveyed_freq = 0;
7981 	u32 ifidx;
7982 
7983 	static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
7984 		[NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
7985 		[NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
7986 	};
7987 
7988 	survey_results = (struct survey_results *) arg;
7989 
7990 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7991 		  genlmsg_attrlen(gnlh, 0), NULL);
7992 
7993 	if (!tb[NL80211_ATTR_IFINDEX])
7994 		return NL_SKIP;
7995 
7996 	ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
7997 
7998 	if (!tb[NL80211_ATTR_SURVEY_INFO])
7999 		return NL_SKIP;
8000 
8001 	if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
8002 			     tb[NL80211_ATTR_SURVEY_INFO],
8003 			     survey_policy))
8004 		return NL_SKIP;
8005 
8006 	if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
8007 		wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
8008 		return NL_SKIP;
8009 	}
8010 
8011 	surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
8012 
8013 	if (!check_survey_ok(sinfo, surveyed_freq,
8014 			     survey_results->freq_filter))
8015 		return NL_SKIP;
8016 
8017 	if (survey_results->freq_filter &&
8018 	    survey_results->freq_filter != surveyed_freq) {
8019 		wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
8020 			   surveyed_freq);
8021 		return NL_SKIP;
8022 	}
8023 
8024 	add_survey(sinfo, ifidx, &survey_results->survey_list);
8025 
8026 	return NL_SKIP;
8027 }
8028 
8029 
8030 static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
8031 {
8032 	struct i802_bss *bss = priv;
8033 	struct wpa_driver_nl80211_data *drv = bss->drv;
8034 	struct nl_msg *msg;
8035 	int err;
8036 	union wpa_event_data data;
8037 	struct survey_results *survey_results;
8038 
8039 	os_memset(&data, 0, sizeof(data));
8040 	survey_results = &data.survey_results;
8041 
8042 	dl_list_init(&survey_results->survey_list);
8043 
8044 	msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
8045 	if (!msg)
8046 		return -ENOBUFS;
8047 
8048 	if (freq)
8049 		data.survey_results.freq_filter = freq;
8050 
8051 	do {
8052 		wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
8053 		err = send_and_recv_msgs(drv, msg, survey_handler,
8054 					 survey_results);
8055 	} while (err > 0);
8056 
8057 	if (err)
8058 		wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
8059 	else
8060 		wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
8061 
8062 	clean_survey_results(survey_results);
8063 	return err;
8064 }
8065 
8066 
8067 static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
8068 				   const u8 *kck, size_t kck_len,
8069 				   const u8 *replay_ctr)
8070 {
8071 	struct i802_bss *bss = priv;
8072 	struct wpa_driver_nl80211_data *drv = bss->drv;
8073 	struct nlattr *replay_nested;
8074 	struct nl_msg *msg;
8075 	int ret;
8076 
8077 	if (!drv->set_rekey_offload)
8078 		return;
8079 
8080 	wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
8081 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
8082 	    !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
8083 	    nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
8084 	    (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
8085 	    nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
8086 		    replay_ctr)) {
8087 		nl80211_nlmsg_clear(msg);
8088 		nlmsg_free(msg);
8089 		return;
8090 	}
8091 
8092 	nla_nest_end(msg, replay_nested);
8093 
8094 	ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
8095 	if (ret == -EOPNOTSUPP) {
8096 		wpa_printf(MSG_DEBUG,
8097 			   "nl80211: Driver does not support rekey offload");
8098 		drv->set_rekey_offload = 0;
8099 	}
8100 }
8101 
8102 
8103 static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
8104 				    const u8 *addr, int qos)
8105 {
8106 	/* send data frame to poll STA and check whether
8107 	 * this frame is ACKed */
8108 	struct {
8109 		struct ieee80211_hdr hdr;
8110 		u16 qos_ctl;
8111 	} STRUCT_PACKED nulldata;
8112 	size_t size;
8113 
8114 	/* Send data frame to poll STA and check whether this frame is ACKed */
8115 
8116 	os_memset(&nulldata, 0, sizeof(nulldata));
8117 
8118 	if (qos) {
8119 		nulldata.hdr.frame_control =
8120 			IEEE80211_FC(WLAN_FC_TYPE_DATA,
8121 				     WLAN_FC_STYPE_QOS_NULL);
8122 		size = sizeof(nulldata);
8123 	} else {
8124 		nulldata.hdr.frame_control =
8125 			IEEE80211_FC(WLAN_FC_TYPE_DATA,
8126 				     WLAN_FC_STYPE_NULLFUNC);
8127 		size = sizeof(struct ieee80211_hdr);
8128 	}
8129 
8130 	nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8131 	os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8132 	os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8133 	os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8134 
8135 	if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
8136 					 0, 0, NULL, 0) < 0)
8137 		wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8138 			   "send poll frame");
8139 }
8140 
8141 static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8142 				int qos)
8143 {
8144 	struct i802_bss *bss = priv;
8145 	struct wpa_driver_nl80211_data *drv = bss->drv;
8146 	struct nl_msg *msg;
8147 	int ret;
8148 
8149 	if (!drv->poll_command_supported) {
8150 		nl80211_send_null_frame(bss, own_addr, addr, qos);
8151 		return;
8152 	}
8153 
8154 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
8155 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8156 		nlmsg_free(msg);
8157 		return;
8158 	}
8159 
8160 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8161 	if (ret < 0) {
8162 		wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
8163 			   MACSTR " failed: ret=%d (%s)",
8164 			   MAC2STR(addr), ret, strerror(-ret));
8165 	}
8166 }
8167 
8168 
8169 static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8170 {
8171 	struct nl_msg *msg;
8172 	int ret;
8173 
8174 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
8175 	    nla_put_u32(msg, NL80211_ATTR_PS_STATE,
8176 			enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
8177 		nlmsg_free(msg);
8178 		return -ENOBUFS;
8179 	}
8180 
8181 	ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8182 	if (ret < 0) {
8183 		wpa_printf(MSG_DEBUG,
8184 			   "nl80211: Setting PS state %s failed: %d (%s)",
8185 			   enabled ? "enabled" : "disabled",
8186 			   ret, strerror(-ret));
8187 	}
8188 	return ret;
8189 }
8190 
8191 
8192 static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8193 				     int ctwindow)
8194 {
8195 	struct i802_bss *bss = priv;
8196 
8197 	wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8198 		   "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8199 
8200 	if (opp_ps != -1 || ctwindow != -1) {
8201 #ifdef ANDROID_P2P
8202 		wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
8203 #else /* ANDROID_P2P */
8204 		return -1; /* Not yet supported */
8205 #endif /* ANDROID_P2P */
8206 	}
8207 
8208 	if (legacy_ps == -1)
8209 		return 0;
8210 	if (legacy_ps != 0 && legacy_ps != 1)
8211 		return -1; /* Not yet supported */
8212 
8213 	return nl80211_set_power_save(bss, legacy_ps);
8214 }
8215 
8216 
8217 static int nl80211_start_radar_detection(void *priv,
8218 					 struct hostapd_freq_params *freq)
8219 {
8220 	struct i802_bss *bss = priv;
8221 	struct wpa_driver_nl80211_data *drv = bss->drv;
8222 	struct nl_msg *msg;
8223 	int ret;
8224 
8225 	wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
8226 		   freq->freq, freq->ht_enabled, freq->vht_enabled,
8227 		   freq->bandwidth, freq->center_freq1, freq->center_freq2);
8228 
8229 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
8230 		wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
8231 			   "detection");
8232 		return -1;
8233 	}
8234 
8235 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
8236 	    nl80211_put_freq_params(msg, freq) < 0) {
8237 		nlmsg_free(msg);
8238 		return -1;
8239 	}
8240 
8241 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8242 	if (ret == 0)
8243 		return 0;
8244 	wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
8245 		   "%d (%s)", ret, strerror(-ret));
8246 	return -1;
8247 }
8248 
8249 #ifdef CONFIG_TDLS
8250 
8251 static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8252 				  u8 dialog_token, u16 status_code,
8253 				  u32 peer_capab, int initiator, const u8 *buf,
8254 				  size_t len)
8255 {
8256 	struct i802_bss *bss = priv;
8257 	struct wpa_driver_nl80211_data *drv = bss->drv;
8258 	struct nl_msg *msg;
8259 
8260 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8261 		return -EOPNOTSUPP;
8262 
8263 	if (!dst)
8264 		return -EINVAL;
8265 
8266 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
8267 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
8268 	    nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
8269 	    nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
8270 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
8271 		goto fail;
8272 	if (peer_capab) {
8273 		/*
8274 		 * The internal enum tdls_peer_capability definition is
8275 		 * currently identical with the nl80211 enum
8276 		 * nl80211_tdls_peer_capability, so no conversion is needed
8277 		 * here.
8278 		 */
8279 		if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
8280 				peer_capab))
8281 			goto fail;
8282 	}
8283 	if ((initiator &&
8284 	     nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
8285 	    nla_put(msg, NL80211_ATTR_IE, len, buf))
8286 		goto fail;
8287 
8288 	return send_and_recv_msgs(drv, msg, NULL, NULL);
8289 
8290 fail:
8291 	nlmsg_free(msg);
8292 	return -ENOBUFS;
8293 }
8294 
8295 
8296 static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8297 {
8298 	struct i802_bss *bss = priv;
8299 	struct wpa_driver_nl80211_data *drv = bss->drv;
8300 	struct nl_msg *msg;
8301 	enum nl80211_tdls_operation nl80211_oper;
8302 	int res;
8303 
8304 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8305 		return -EOPNOTSUPP;
8306 
8307 	switch (oper) {
8308 	case TDLS_DISCOVERY_REQ:
8309 		nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8310 		break;
8311 	case TDLS_SETUP:
8312 		nl80211_oper = NL80211_TDLS_SETUP;
8313 		break;
8314 	case TDLS_TEARDOWN:
8315 		nl80211_oper = NL80211_TDLS_TEARDOWN;
8316 		break;
8317 	case TDLS_ENABLE_LINK:
8318 		nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8319 		break;
8320 	case TDLS_DISABLE_LINK:
8321 		nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8322 		break;
8323 	case TDLS_ENABLE:
8324 		return 0;
8325 	case TDLS_DISABLE:
8326 		return 0;
8327 	default:
8328 		return -EINVAL;
8329 	}
8330 
8331 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
8332 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
8333 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
8334 		nlmsg_free(msg);
8335 		return -ENOBUFS;
8336 	}
8337 
8338 	res = send_and_recv_msgs(drv, msg, NULL, NULL);
8339 	wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
8340 		   " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
8341 		   strerror(-res));
8342 	return res;
8343 }
8344 
8345 
8346 static int
8347 nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
8348 				   const struct hostapd_freq_params *params)
8349 {
8350 	struct i802_bss *bss = priv;
8351 	struct wpa_driver_nl80211_data *drv = bss->drv;
8352 	struct nl_msg *msg;
8353 	int ret = -ENOBUFS;
8354 
8355 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8356 	    !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8357 		return -EOPNOTSUPP;
8358 
8359 	wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
8360 		   " oper_class=%u freq=%u",
8361 		   MAC2STR(addr), oper_class, params->freq);
8362 	msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
8363 	if (!msg ||
8364 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8365 	    nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
8366 	    (ret = nl80211_put_freq_params(msg, params))) {
8367 		nlmsg_free(msg);
8368 		wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
8369 		return ret;
8370 	}
8371 
8372 	return send_and_recv_msgs(drv, msg, NULL, NULL);
8373 }
8374 
8375 
8376 static int
8377 nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
8378 {
8379 	struct i802_bss *bss = priv;
8380 	struct wpa_driver_nl80211_data *drv = bss->drv;
8381 	struct nl_msg *msg;
8382 
8383 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8384 	    !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8385 		return -EOPNOTSUPP;
8386 
8387 	wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
8388 		   MAC2STR(addr));
8389 	msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
8390 	if (!msg ||
8391 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8392 		nlmsg_free(msg);
8393 		wpa_printf(MSG_DEBUG,
8394 			   "nl80211: Could not build TDLS cancel chan switch");
8395 		return -ENOBUFS;
8396 	}
8397 
8398 	return send_and_recv_msgs(drv, msg, NULL, NULL);
8399 }
8400 
8401 #endif /* CONFIG TDLS */
8402 
8403 
8404 static int driver_nl80211_set_key(const char *ifname, void *priv,
8405 				  enum wpa_alg alg, const u8 *addr,
8406 				  int key_idx, int set_tx,
8407 				  const u8 *seq, size_t seq_len,
8408 				  const u8 *key, size_t key_len)
8409 {
8410 	struct i802_bss *bss = priv;
8411 	return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
8412 					  set_tx, seq, seq_len, key, key_len);
8413 }
8414 
8415 
8416 static int driver_nl80211_scan2(void *priv,
8417 				struct wpa_driver_scan_params *params)
8418 {
8419 	struct i802_bss *bss = priv;
8420 #ifdef CONFIG_DRIVER_NL80211_QCA
8421 	struct wpa_driver_nl80211_data *drv = bss->drv;
8422 
8423 	/*
8424 	 * Do a vendor specific scan if possible. If only_new_results is
8425 	 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
8426 	 * cannot be achieved through a vendor scan. The below condition may
8427 	 * need to be modified if new scan flags are added in the future whose
8428 	 * functionality can only be achieved through a normal scan.
8429 	 */
8430 	if (drv->scan_vendor_cmd_avail && !params->only_new_results)
8431 		return wpa_driver_nl80211_vendor_scan(bss, params);
8432 #endif /* CONFIG_DRIVER_NL80211_QCA */
8433 	return wpa_driver_nl80211_scan(bss, params);
8434 }
8435 
8436 
8437 static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
8438 					 int reason_code)
8439 {
8440 	struct i802_bss *bss = priv;
8441 	return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
8442 }
8443 
8444 
8445 static int driver_nl80211_authenticate(void *priv,
8446 				       struct wpa_driver_auth_params *params)
8447 {
8448 	struct i802_bss *bss = priv;
8449 	return wpa_driver_nl80211_authenticate(bss, params);
8450 }
8451 
8452 
8453 static void driver_nl80211_deinit(void *priv)
8454 {
8455 	struct i802_bss *bss = priv;
8456 	wpa_driver_nl80211_deinit(bss);
8457 }
8458 
8459 
8460 static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
8461 				    const char *ifname)
8462 {
8463 	struct i802_bss *bss = priv;
8464 	return wpa_driver_nl80211_if_remove(bss, type, ifname);
8465 }
8466 
8467 
8468 static int driver_nl80211_send_mlme(void *priv, const u8 *data,
8469 				    size_t data_len, int noack,
8470 				    unsigned int freq,
8471 				    const u16 *csa_offs, size_t csa_offs_len)
8472 {
8473 	struct i802_bss *bss = priv;
8474 	return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
8475 					    freq, 0, 0, 0, csa_offs,
8476 					    csa_offs_len);
8477 }
8478 
8479 
8480 static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
8481 {
8482 	struct i802_bss *bss = priv;
8483 	return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
8484 }
8485 
8486 
8487 static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
8488 				       const char *ifname, int vlan_id)
8489 {
8490 	struct i802_bss *bss = priv;
8491 	return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
8492 }
8493 
8494 
8495 static int driver_nl80211_read_sta_data(void *priv,
8496 					struct hostap_sta_driver_data *data,
8497 					const u8 *addr)
8498 {
8499 	struct i802_bss *bss = priv;
8500 
8501 	os_memset(data, 0, sizeof(*data));
8502 	return i802_read_sta_data(bss, data, addr);
8503 }
8504 
8505 
8506 static int driver_nl80211_send_action(void *priv, unsigned int freq,
8507 				      unsigned int wait_time,
8508 				      const u8 *dst, const u8 *src,
8509 				      const u8 *bssid,
8510 				      const u8 *data, size_t data_len,
8511 				      int no_cck)
8512 {
8513 	struct i802_bss *bss = priv;
8514 	return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
8515 					      bssid, data, data_len, no_cck);
8516 }
8517 
8518 
8519 static int driver_nl80211_probe_req_report(void *priv, int report)
8520 {
8521 	struct i802_bss *bss = priv;
8522 	return wpa_driver_nl80211_probe_req_report(bss, report);
8523 }
8524 
8525 
8526 static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
8527 					    const u8 *ies, size_t ies_len)
8528 {
8529 	int ret;
8530 	struct nl_msg *msg;
8531 	struct i802_bss *bss = priv;
8532 	struct wpa_driver_nl80211_data *drv = bss->drv;
8533 	u16 mdid = WPA_GET_LE16(md);
8534 
8535 	wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
8536 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
8537 	    nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
8538 	    nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
8539 		nlmsg_free(msg);
8540 		return -ENOBUFS;
8541 	}
8542 
8543 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8544 	if (ret) {
8545 		wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
8546 			   "err=%d (%s)", ret, strerror(-ret));
8547 	}
8548 
8549 	return ret;
8550 }
8551 
8552 
8553 static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
8554 {
8555 	struct i802_bss *bss = priv;
8556 	struct wpa_driver_nl80211_data *drv = bss->drv;
8557 
8558 	if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
8559 		return NULL;
8560 
8561 	return bss->addr;
8562 }
8563 
8564 
8565 static const char * scan_state_str(enum scan_states scan_state)
8566 {
8567 	switch (scan_state) {
8568 	case NO_SCAN:
8569 		return "NO_SCAN";
8570 	case SCAN_REQUESTED:
8571 		return "SCAN_REQUESTED";
8572 	case SCAN_STARTED:
8573 		return "SCAN_STARTED";
8574 	case SCAN_COMPLETED:
8575 		return "SCAN_COMPLETED";
8576 	case SCAN_ABORTED:
8577 		return "SCAN_ABORTED";
8578 	case SCHED_SCAN_STARTED:
8579 		return "SCHED_SCAN_STARTED";
8580 	case SCHED_SCAN_STOPPED:
8581 		return "SCHED_SCAN_STOPPED";
8582 	case SCHED_SCAN_RESULTS:
8583 		return "SCHED_SCAN_RESULTS";
8584 	}
8585 
8586 	return "??";
8587 }
8588 
8589 
8590 static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
8591 {
8592 	struct i802_bss *bss = priv;
8593 	struct wpa_driver_nl80211_data *drv = bss->drv;
8594 	int res;
8595 	char *pos, *end;
8596 
8597 	pos = buf;
8598 	end = buf + buflen;
8599 
8600 	res = os_snprintf(pos, end - pos,
8601 			  "ifindex=%d\n"
8602 			  "ifname=%s\n"
8603 			  "brname=%s\n"
8604 			  "addr=" MACSTR "\n"
8605 			  "freq=%d\n"
8606 			  "%s%s%s%s%s%s",
8607 			  bss->ifindex,
8608 			  bss->ifname,
8609 			  bss->brname,
8610 			  MAC2STR(bss->addr),
8611 			  bss->freq,
8612 			  bss->beacon_set ? "beacon_set=1\n" : "",
8613 			  bss->added_if_into_bridge ?
8614 			  "added_if_into_bridge=1\n" : "",
8615 			  bss->already_in_bridge ? "already_in_bridge=1\n" : "",
8616 			  bss->added_bridge ? "added_bridge=1\n" : "",
8617 			  bss->in_deinit ? "in_deinit=1\n" : "",
8618 			  bss->if_dynamic ? "if_dynamic=1\n" : "");
8619 	if (os_snprintf_error(end - pos, res))
8620 		return pos - buf;
8621 	pos += res;
8622 
8623 	if (bss->wdev_id_set) {
8624 		res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
8625 				  (unsigned long long) bss->wdev_id);
8626 		if (os_snprintf_error(end - pos, res))
8627 			return pos - buf;
8628 		pos += res;
8629 	}
8630 
8631 	res = os_snprintf(pos, end - pos,
8632 			  "phyname=%s\n"
8633 			  "perm_addr=" MACSTR "\n"
8634 			  "drv_ifindex=%d\n"
8635 			  "operstate=%d\n"
8636 			  "scan_state=%s\n"
8637 			  "auth_bssid=" MACSTR "\n"
8638 			  "auth_attempt_bssid=" MACSTR "\n"
8639 			  "bssid=" MACSTR "\n"
8640 			  "prev_bssid=" MACSTR "\n"
8641 			  "associated=%d\n"
8642 			  "assoc_freq=%u\n"
8643 			  "monitor_sock=%d\n"
8644 			  "monitor_ifidx=%d\n"
8645 			  "monitor_refcount=%d\n"
8646 			  "last_mgmt_freq=%u\n"
8647 			  "eapol_tx_sock=%d\n"
8648 			  "%s%s%s%s%s%s%s%s%s%s%s%s%s",
8649 			  drv->phyname,
8650 			  MAC2STR(drv->perm_addr),
8651 			  drv->ifindex,
8652 			  drv->operstate,
8653 			  scan_state_str(drv->scan_state),
8654 			  MAC2STR(drv->auth_bssid),
8655 			  MAC2STR(drv->auth_attempt_bssid),
8656 			  MAC2STR(drv->bssid),
8657 			  MAC2STR(drv->prev_bssid),
8658 			  drv->associated,
8659 			  drv->assoc_freq,
8660 			  drv->monitor_sock,
8661 			  drv->monitor_ifidx,
8662 			  drv->monitor_refcount,
8663 			  drv->last_mgmt_freq,
8664 			  drv->eapol_tx_sock,
8665 			  drv->ignore_if_down_event ?
8666 			  "ignore_if_down_event=1\n" : "",
8667 			  drv->scan_complete_events ?
8668 			  "scan_complete_events=1\n" : "",
8669 			  drv->disabled_11b_rates ?
8670 			  "disabled_11b_rates=1\n" : "",
8671 			  drv->pending_remain_on_chan ?
8672 			  "pending_remain_on_chan=1\n" : "",
8673 			  drv->in_interface_list ? "in_interface_list=1\n" : "",
8674 			  drv->device_ap_sme ? "device_ap_sme=1\n" : "",
8675 			  drv->poll_command_supported ?
8676 			  "poll_command_supported=1\n" : "",
8677 			  drv->data_tx_status ? "data_tx_status=1\n" : "",
8678 			  drv->scan_for_auth ? "scan_for_auth=1\n" : "",
8679 			  drv->retry_auth ? "retry_auth=1\n" : "",
8680 			  drv->use_monitor ? "use_monitor=1\n" : "",
8681 			  drv->ignore_next_local_disconnect ?
8682 			  "ignore_next_local_disconnect=1\n" : "",
8683 			  drv->ignore_next_local_deauth ?
8684 			  "ignore_next_local_deauth=1\n" : "");
8685 	if (os_snprintf_error(end - pos, res))
8686 		return pos - buf;
8687 	pos += res;
8688 
8689 	if (drv->has_capability) {
8690 		res = os_snprintf(pos, end - pos,
8691 				  "capa.key_mgmt=0x%x\n"
8692 				  "capa.enc=0x%x\n"
8693 				  "capa.auth=0x%x\n"
8694 				  "capa.flags=0x%llx\n"
8695 				  "capa.rrm_flags=0x%x\n"
8696 				  "capa.max_scan_ssids=%d\n"
8697 				  "capa.max_sched_scan_ssids=%d\n"
8698 				  "capa.sched_scan_supported=%d\n"
8699 				  "capa.max_match_sets=%d\n"
8700 				  "capa.max_remain_on_chan=%u\n"
8701 				  "capa.max_stations=%u\n"
8702 				  "capa.probe_resp_offloads=0x%x\n"
8703 				  "capa.max_acl_mac_addrs=%u\n"
8704 				  "capa.num_multichan_concurrent=%u\n"
8705 				  "capa.mac_addr_rand_sched_scan_supported=%d\n"
8706 				  "capa.mac_addr_rand_scan_supported=%d\n"
8707 				  "capa.conc_capab=%u\n"
8708 				  "capa.max_conc_chan_2_4=%u\n"
8709 				  "capa.max_conc_chan_5_0=%u\n"
8710 				  "capa.max_sched_scan_plans=%u\n"
8711 				  "capa.max_sched_scan_plan_interval=%u\n"
8712 				  "capa.max_sched_scan_plan_iterations=%u\n",
8713 				  drv->capa.key_mgmt,
8714 				  drv->capa.enc,
8715 				  drv->capa.auth,
8716 				  (unsigned long long) drv->capa.flags,
8717 				  drv->capa.rrm_flags,
8718 				  drv->capa.max_scan_ssids,
8719 				  drv->capa.max_sched_scan_ssids,
8720 				  drv->capa.sched_scan_supported,
8721 				  drv->capa.max_match_sets,
8722 				  drv->capa.max_remain_on_chan,
8723 				  drv->capa.max_stations,
8724 				  drv->capa.probe_resp_offloads,
8725 				  drv->capa.max_acl_mac_addrs,
8726 				  drv->capa.num_multichan_concurrent,
8727 				  drv->capa.mac_addr_rand_sched_scan_supported,
8728 				  drv->capa.mac_addr_rand_scan_supported,
8729 				  drv->capa.conc_capab,
8730 				  drv->capa.max_conc_chan_2_4,
8731 				  drv->capa.max_conc_chan_5_0,
8732 				  drv->capa.max_sched_scan_plans,
8733 				  drv->capa.max_sched_scan_plan_interval,
8734 				  drv->capa.max_sched_scan_plan_iterations);
8735 		if (os_snprintf_error(end - pos, res))
8736 			return pos - buf;
8737 		pos += res;
8738 	}
8739 
8740 	return pos - buf;
8741 }
8742 
8743 
8744 static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
8745 {
8746 	if ((settings->head &&
8747 	     nla_put(msg, NL80211_ATTR_BEACON_HEAD,
8748 		     settings->head_len, settings->head)) ||
8749 	    (settings->tail &&
8750 	     nla_put(msg, NL80211_ATTR_BEACON_TAIL,
8751 		     settings->tail_len, settings->tail)) ||
8752 	    (settings->beacon_ies &&
8753 	     nla_put(msg, NL80211_ATTR_IE,
8754 		     settings->beacon_ies_len, settings->beacon_ies)) ||
8755 	    (settings->proberesp_ies &&
8756 	     nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
8757 		     settings->proberesp_ies_len, settings->proberesp_ies)) ||
8758 	    (settings->assocresp_ies &&
8759 	     nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
8760 		     settings->assocresp_ies_len, settings->assocresp_ies)) ||
8761 	    (settings->probe_resp &&
8762 	     nla_put(msg, NL80211_ATTR_PROBE_RESP,
8763 		     settings->probe_resp_len, settings->probe_resp)))
8764 		return -ENOBUFS;
8765 
8766 	return 0;
8767 }
8768 
8769 
8770 static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
8771 {
8772 	struct nl_msg *msg;
8773 	struct i802_bss *bss = priv;
8774 	struct wpa_driver_nl80211_data *drv = bss->drv;
8775 	struct nlattr *beacon_csa;
8776 	int ret = -ENOBUFS;
8777 	int csa_off_len = 0;
8778 	int i;
8779 
8780 	wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
8781 		   settings->cs_count, settings->block_tx,
8782 		   settings->freq_params.freq, settings->freq_params.bandwidth,
8783 		   settings->freq_params.center_freq1,
8784 		   settings->freq_params.center_freq2);
8785 
8786 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8787 		wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
8788 		return -EOPNOTSUPP;
8789 	}
8790 
8791 	if (drv->nlmode != NL80211_IFTYPE_AP &&
8792 	    drv->nlmode != NL80211_IFTYPE_P2P_GO &&
8793 	    drv->nlmode != NL80211_IFTYPE_MESH_POINT)
8794 		return -EOPNOTSUPP;
8795 
8796 	/*
8797 	 * Remove empty counters, assuming Probe Response and Beacon frame
8798 	 * counters match. This implementation assumes that there are only two
8799 	 * counters.
8800 	 */
8801 	if (settings->counter_offset_beacon[0] &&
8802 	    !settings->counter_offset_beacon[1]) {
8803 		csa_off_len = 1;
8804 	} else if (settings->counter_offset_beacon[1] &&
8805 		   !settings->counter_offset_beacon[0]) {
8806 		csa_off_len = 1;
8807 		settings->counter_offset_beacon[0] =
8808 			settings->counter_offset_beacon[1];
8809 		settings->counter_offset_presp[0] =
8810 			settings->counter_offset_presp[1];
8811 	} else if (settings->counter_offset_beacon[1] &&
8812 		   settings->counter_offset_beacon[0]) {
8813 		csa_off_len = 2;
8814 	} else {
8815 		wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
8816 		return -EINVAL;
8817 	}
8818 
8819 	/* Check CSA counters validity */
8820 	if (drv->capa.max_csa_counters &&
8821 	    csa_off_len > drv->capa.max_csa_counters) {
8822 		wpa_printf(MSG_ERROR,
8823 			   "nl80211: Too many CSA counters provided");
8824 		return -EINVAL;
8825 	}
8826 
8827 	if (!settings->beacon_csa.tail)
8828 		return -EINVAL;
8829 
8830 	for (i = 0; i < csa_off_len; i++) {
8831 		u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8832 		u16 csa_c_off_presp = settings->counter_offset_presp[i];
8833 
8834 		if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8835 		    (settings->beacon_csa.tail[csa_c_off_bcn] !=
8836 		     settings->cs_count))
8837 			return -EINVAL;
8838 
8839 		if (settings->beacon_csa.probe_resp &&
8840 		    ((settings->beacon_csa.probe_resp_len <=
8841 		      csa_c_off_presp) ||
8842 		     (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8843 		      settings->cs_count)))
8844 			return -EINVAL;
8845 	}
8846 
8847 	if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
8848 	    nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8849 			settings->cs_count) ||
8850 	    (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8851 	    (settings->block_tx &&
8852 	     nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
8853 		goto error;
8854 
8855 	/* beacon_after params */
8856 	ret = set_beacon_data(msg, &settings->beacon_after);
8857 	if (ret)
8858 		goto error;
8859 
8860 	/* beacon_csa params */
8861 	beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
8862 	if (!beacon_csa)
8863 		goto fail;
8864 
8865 	ret = set_beacon_data(msg, &settings->beacon_csa);
8866 	if (ret)
8867 		goto error;
8868 
8869 	if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
8870 		    csa_off_len * sizeof(u16),
8871 		    settings->counter_offset_beacon) ||
8872 	    (settings->beacon_csa.probe_resp &&
8873 	     nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
8874 		     csa_off_len * sizeof(u16),
8875 		     settings->counter_offset_presp)))
8876 		goto fail;
8877 
8878 	nla_nest_end(msg, beacon_csa);
8879 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8880 	if (ret) {
8881 		wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
8882 			   ret, strerror(-ret));
8883 	}
8884 	return ret;
8885 
8886 fail:
8887 	ret = -ENOBUFS;
8888 error:
8889 	nlmsg_free(msg);
8890 	wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
8891 	return ret;
8892 }
8893 
8894 
8895 static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
8896 			  u8 user_priority, u16 admitted_time)
8897 {
8898 	struct i802_bss *bss = priv;
8899 	struct wpa_driver_nl80211_data *drv = bss->drv;
8900 	struct nl_msg *msg;
8901 	int ret;
8902 
8903 	wpa_printf(MSG_DEBUG,
8904 		   "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
8905 		   tsid, admitted_time, user_priority);
8906 
8907 	if (!is_sta_interface(drv->nlmode))
8908 		return -ENOTSUP;
8909 
8910 	msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
8911 	if (!msg ||
8912 	    nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8913 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8914 	    nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
8915 	    nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
8916 		nlmsg_free(msg);
8917 		return -ENOBUFS;
8918 	}
8919 
8920 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8921 	if (ret)
8922 		wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
8923 			   ret, strerror(-ret));
8924 	return ret;
8925 }
8926 
8927 
8928 static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
8929 {
8930 	struct i802_bss *bss = priv;
8931 	struct wpa_driver_nl80211_data *drv = bss->drv;
8932 	struct nl_msg *msg;
8933 	int ret;
8934 
8935 	wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
8936 
8937 	if (!is_sta_interface(drv->nlmode))
8938 		return -ENOTSUP;
8939 
8940 	if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
8941 	    nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8942 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8943 		nlmsg_free(msg);
8944 		return -ENOBUFS;
8945 	}
8946 
8947 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8948 	if (ret)
8949 		wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
8950 			   ret, strerror(-ret));
8951 	return ret;
8952 }
8953 
8954 
8955 #ifdef CONFIG_TESTING_OPTIONS
8956 static int cmd_reply_handler(struct nl_msg *msg, void *arg)
8957 {
8958 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8959 	struct wpabuf *buf = arg;
8960 
8961 	if (!buf)
8962 		return NL_SKIP;
8963 
8964 	if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
8965 		wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
8966 		return NL_SKIP;
8967 	}
8968 
8969 	wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
8970 			genlmsg_attrlen(gnlh, 0));
8971 
8972 	return NL_SKIP;
8973 }
8974 #endif /* CONFIG_TESTING_OPTIONS */
8975 
8976 
8977 static int vendor_reply_handler(struct nl_msg *msg, void *arg)
8978 {
8979 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
8980 	struct nlattr *nl_vendor_reply, *nl;
8981 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8982 	struct wpabuf *buf = arg;
8983 	int rem;
8984 
8985 	if (!buf)
8986 		return NL_SKIP;
8987 
8988 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8989 		  genlmsg_attrlen(gnlh, 0), NULL);
8990 	nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
8991 
8992 	if (!nl_vendor_reply)
8993 		return NL_SKIP;
8994 
8995 	if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
8996 		wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
8997 		return NL_SKIP;
8998 	}
8999 
9000 	nla_for_each_nested(nl, nl_vendor_reply, rem) {
9001 		wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
9002 	}
9003 
9004 	return NL_SKIP;
9005 }
9006 
9007 
9008 static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
9009 			      unsigned int subcmd, const u8 *data,
9010 			      size_t data_len, struct wpabuf *buf)
9011 {
9012 	struct i802_bss *bss = priv;
9013 	struct wpa_driver_nl80211_data *drv = bss->drv;
9014 	struct nl_msg *msg;
9015 	int ret;
9016 
9017 #ifdef CONFIG_TESTING_OPTIONS
9018 	if (vendor_id == 0xffffffff) {
9019 		msg = nlmsg_alloc();
9020 		if (!msg)
9021 			return -ENOMEM;
9022 
9023 		nl80211_cmd(drv, msg, 0, subcmd);
9024 		if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
9025 		    0)
9026 			goto fail;
9027 		ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
9028 		if (ret)
9029 			wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
9030 				   ret);
9031 		return ret;
9032 	}
9033 #endif /* CONFIG_TESTING_OPTIONS */
9034 
9035 	if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
9036 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
9037 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
9038 	    (data &&
9039 	     nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
9040 		goto fail;
9041 
9042 	ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
9043 	if (ret)
9044 		wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
9045 			   ret);
9046 	return ret;
9047 
9048 fail:
9049 	nlmsg_free(msg);
9050 	return -ENOBUFS;
9051 }
9052 
9053 
9054 static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
9055 			       u8 qos_map_set_len)
9056 {
9057 	struct i802_bss *bss = priv;
9058 	struct wpa_driver_nl80211_data *drv = bss->drv;
9059 	struct nl_msg *msg;
9060 	int ret;
9061 
9062 	wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
9063 		    qos_map_set, qos_map_set_len);
9064 
9065 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
9066 	    nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
9067 		nlmsg_free(msg);
9068 		return -ENOBUFS;
9069 	}
9070 
9071 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9072 	if (ret)
9073 		wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
9074 
9075 	return ret;
9076 }
9077 
9078 
9079 static int nl80211_set_wowlan(void *priv,
9080 			      const struct wowlan_triggers *triggers)
9081 {
9082 	struct i802_bss *bss = priv;
9083 	struct wpa_driver_nl80211_data *drv = bss->drv;
9084 	struct nl_msg *msg;
9085 	struct nlattr *wowlan_triggers;
9086 	int ret;
9087 
9088 	wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
9089 
9090 	if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
9091 	    !(wowlan_triggers = nla_nest_start(msg,
9092 					       NL80211_ATTR_WOWLAN_TRIGGERS)) ||
9093 	    (triggers->any &&
9094 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
9095 	    (triggers->disconnect &&
9096 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
9097 	    (triggers->magic_pkt &&
9098 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
9099 	    (triggers->gtk_rekey_failure &&
9100 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
9101 	    (triggers->eap_identity_req &&
9102 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
9103 	    (triggers->four_way_handshake &&
9104 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
9105 	    (triggers->rfkill_release &&
9106 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
9107 		nlmsg_free(msg);
9108 		return -ENOBUFS;
9109 	}
9110 
9111 	nla_nest_end(msg, wowlan_triggers);
9112 
9113 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9114 	if (ret)
9115 		wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
9116 
9117 	return ret;
9118 }
9119 
9120 
9121 #ifdef CONFIG_DRIVER_NL80211_QCA
9122 static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
9123 {
9124 	struct i802_bss *bss = priv;
9125 	struct wpa_driver_nl80211_data *drv = bss->drv;
9126 	struct nl_msg *msg;
9127 	struct nlattr *params;
9128 
9129 	wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
9130 
9131 	if (!drv->roaming_vendor_cmd_avail) {
9132 		wpa_printf(MSG_DEBUG,
9133 			   "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
9134 		return -1;
9135 	}
9136 
9137 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9138 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9139 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9140 			QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
9141 	    !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9142 	    nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
9143 			allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
9144 			QCA_ROAMING_NOT_ALLOWED) ||
9145 	    (bssid &&
9146 	     nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
9147 		nlmsg_free(msg);
9148 		return -1;
9149 	}
9150 	nla_nest_end(msg, params);
9151 
9152 	return send_and_recv_msgs(drv, msg, NULL, NULL);
9153 }
9154 
9155 
9156 static int nl80211_disable_fils(void *priv, int disable)
9157 {
9158 	struct i802_bss *bss = priv;
9159 	struct wpa_driver_nl80211_data *drv = bss->drv;
9160 	struct nl_msg *msg;
9161 	struct nlattr *params;
9162 
9163 	wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
9164 
9165 	if (!drv->set_wifi_conf_vendor_cmd_avail)
9166 		return -1;
9167 
9168 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9169 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9170 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9171 			QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
9172 	    !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9173 	    nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
9174 		       disable)) {
9175 		nlmsg_free(msg);
9176 		return -1;
9177 	}
9178 	nla_nest_end(msg, params);
9179 
9180 	return send_and_recv_msgs(drv, msg, NULL, NULL);
9181 }
9182 
9183 
9184 /* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
9185 #define WPA_SUPPLICANT_CLIENT_ID 1
9186 
9187 static int nl80211_set_bssid_blacklist(void *priv, unsigned int num_bssid,
9188 				       const u8 *bssid)
9189 {
9190 	struct i802_bss *bss = priv;
9191 	struct wpa_driver_nl80211_data *drv = bss->drv;
9192 	struct nl_msg *msg;
9193 	struct nlattr *params, *nlbssids, *attr;
9194 	unsigned int i;
9195 
9196 	wpa_printf(MSG_DEBUG, "nl80211: Set blacklist BSSID (num=%u)",
9197 		   num_bssid);
9198 
9199 	if (!drv->roam_vendor_cmd_avail)
9200 		return -1;
9201 
9202 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9203 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9204 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9205 			QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
9206 	    !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9207 	    nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
9208 			QCA_WLAN_VENDOR_ATTR_ROAM_SUBCMD_SET_BLACKLIST_BSSID) ||
9209 	    nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
9210 			WPA_SUPPLICANT_CLIENT_ID) ||
9211 	    nla_put_u32(msg,
9212 			QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
9213 			num_bssid))
9214 		goto fail;
9215 
9216 	nlbssids = nla_nest_start(
9217 		msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
9218 	if (!nlbssids)
9219 		goto fail;
9220 
9221 	for (i = 0; i < num_bssid; i++) {
9222 		attr = nla_nest_start(msg, i);
9223 		if (!attr)
9224 			goto fail;
9225 		if (nla_put(msg,
9226 			    QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
9227 			    ETH_ALEN, &bssid[i * ETH_ALEN]))
9228 			goto fail;
9229 		wpa_printf(MSG_DEBUG, "nl80211:   BSSID[%u]: " MACSTR, i,
9230 			   MAC2STR(&bssid[i * ETH_ALEN]));
9231 		nla_nest_end(msg, attr);
9232 	}
9233 	nla_nest_end(msg, nlbssids);
9234 	nla_nest_end(msg, params);
9235 
9236 	return send_and_recv_msgs(drv, msg, NULL, NULL);
9237 
9238 fail:
9239 	nlmsg_free(msg);
9240 	return -1;
9241 }
9242 
9243 #endif /* CONFIG_DRIVER_NL80211_QCA */
9244 
9245 
9246 static int nl80211_set_mac_addr(void *priv, const u8 *addr)
9247 {
9248 	struct i802_bss *bss = priv;
9249 	struct wpa_driver_nl80211_data *drv = bss->drv;
9250 	int new_addr = addr != NULL;
9251 
9252 	if (TEST_FAIL())
9253 		return -1;
9254 
9255 	if (!addr)
9256 		addr = drv->perm_addr;
9257 
9258 	if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
9259 		return -1;
9260 
9261 	if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
9262 	{
9263 		wpa_printf(MSG_DEBUG,
9264 			   "nl80211: failed to set_mac_addr for %s to " MACSTR,
9265 			   bss->ifname, MAC2STR(addr));
9266 		if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
9267 					  1) < 0) {
9268 			wpa_printf(MSG_DEBUG,
9269 				   "nl80211: Could not restore interface UP after failed set_mac_addr");
9270 		}
9271 		return -1;
9272 	}
9273 
9274 	wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
9275 		   bss->ifname, MAC2STR(addr));
9276 	drv->addr_changed = new_addr;
9277 	os_memcpy(bss->addr, addr, ETH_ALEN);
9278 
9279 	if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
9280 	{
9281 		wpa_printf(MSG_DEBUG,
9282 			   "nl80211: Could not restore interface UP after set_mac_addr");
9283 	}
9284 
9285 	return 0;
9286 }
9287 
9288 
9289 #ifdef CONFIG_MESH
9290 
9291 static int wpa_driver_nl80211_init_mesh(void *priv)
9292 {
9293 	if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
9294 		wpa_printf(MSG_INFO,
9295 			   "nl80211: Failed to set interface into mesh mode");
9296 		return -1;
9297 	}
9298 	return 0;
9299 }
9300 
9301 
9302 static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
9303 			       size_t mesh_id_len)
9304 {
9305 	if (mesh_id) {
9306 		wpa_hexdump_ascii(MSG_DEBUG, "  * Mesh ID (SSID)",
9307 				  mesh_id, mesh_id_len);
9308 		return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
9309 	}
9310 
9311 	return 0;
9312 }
9313 
9314 
9315 static int nl80211_put_mesh_config(struct nl_msg *msg,
9316 				   struct wpa_driver_mesh_bss_params *params)
9317 {
9318 	struct nlattr *container;
9319 
9320 	container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
9321 	if (!container)
9322 		return -1;
9323 
9324 	if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
9325 	     nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
9326 			params->auto_plinks)) ||
9327 	    ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
9328 	     nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
9329 			 params->max_peer_links)) ||
9330 	    ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
9331 	     nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
9332 			 params->rssi_threshold)))
9333 		return -1;
9334 
9335 	/*
9336 	 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
9337 	 * the timer could disconnect stations even in that case.
9338 	 */
9339 	if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
9340 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
9341 			params->peer_link_timeout)) {
9342 		wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
9343 		return -1;
9344 	}
9345 
9346 	if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
9347 	    nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
9348 		wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
9349 		return -1;
9350 	}
9351 
9352 	nla_nest_end(msg, container);
9353 
9354 	return 0;
9355 }
9356 
9357 
9358 static int nl80211_join_mesh(struct i802_bss *bss,
9359 			     struct wpa_driver_mesh_join_params *params)
9360 {
9361 	struct wpa_driver_nl80211_data *drv = bss->drv;
9362 	struct nl_msg *msg;
9363 	struct nlattr *container;
9364 	int ret = -1;
9365 
9366 	wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
9367 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
9368 	if (!msg ||
9369 	    nl80211_put_freq_params(msg, &params->freq) ||
9370 	    nl80211_put_basic_rates(msg, params->basic_rates) ||
9371 	    nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
9372 	    nl80211_put_beacon_int(msg, params->beacon_int) ||
9373 	    nl80211_put_dtim_period(msg, params->dtim_period))
9374 		goto fail;
9375 
9376 	wpa_printf(MSG_DEBUG, "  * flags=%08X", params->flags);
9377 
9378 	container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
9379 	if (!container)
9380 		goto fail;
9381 
9382 	if (params->ies) {
9383 		wpa_hexdump(MSG_DEBUG, "  * IEs", params->ies, params->ie_len);
9384 		if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
9385 			    params->ies))
9386 			goto fail;
9387 	}
9388 	/* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
9389 	if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
9390 		if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
9391 		    nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
9392 			goto fail;
9393 	}
9394 	if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
9395 	    nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
9396 		goto fail;
9397 	if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
9398 	    nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
9399 		goto fail;
9400 	nla_nest_end(msg, container);
9401 
9402 	params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
9403 	params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
9404 	params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
9405 	if (nl80211_put_mesh_config(msg, &params->conf) < 0)
9406 		goto fail;
9407 
9408 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9409 	msg = NULL;
9410 	if (ret) {
9411 		wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
9412 			   ret, strerror(-ret));
9413 		goto fail;
9414 	}
9415 	ret = 0;
9416 	drv->assoc_freq = bss->freq = params->freq.freq;
9417 	wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
9418 
9419 fail:
9420 	nlmsg_free(msg);
9421 	return ret;
9422 }
9423 
9424 
9425 static int
9426 wpa_driver_nl80211_join_mesh(void *priv,
9427 			     struct wpa_driver_mesh_join_params *params)
9428 {
9429 	struct i802_bss *bss = priv;
9430 	int ret, timeout;
9431 
9432 	timeout = params->conf.peer_link_timeout;
9433 
9434 	/* Disable kernel inactivity timer */
9435 	if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
9436 		params->conf.peer_link_timeout = 0;
9437 
9438 	ret = nl80211_join_mesh(bss, params);
9439 	if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
9440 		wpa_printf(MSG_DEBUG,
9441 			   "nl80211: Mesh join retry for peer_link_timeout");
9442 		/*
9443 		 * Old kernel does not support setting
9444 		 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
9445 		 * into future from peer_link_timeout.
9446 		 */
9447 		params->conf.peer_link_timeout = timeout + 60;
9448 		ret = nl80211_join_mesh(priv, params);
9449 	}
9450 
9451 	params->conf.peer_link_timeout = timeout;
9452 	return ret;
9453 }
9454 
9455 
9456 static int wpa_driver_nl80211_leave_mesh(void *priv)
9457 {
9458 	struct i802_bss *bss = priv;
9459 	struct wpa_driver_nl80211_data *drv = bss->drv;
9460 	struct nl_msg *msg;
9461 	int ret;
9462 
9463 	wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
9464 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
9465 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9466 	if (ret) {
9467 		wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
9468 			   ret, strerror(-ret));
9469 	} else {
9470 		wpa_printf(MSG_DEBUG,
9471 			   "nl80211: mesh leave request send successfully");
9472 	}
9473 
9474 	if (wpa_driver_nl80211_set_mode(drv->first_bss,
9475 					NL80211_IFTYPE_STATION)) {
9476 		wpa_printf(MSG_INFO,
9477 			   "nl80211: Failed to set interface into station mode");
9478 	}
9479 	return ret;
9480 }
9481 
9482 #endif /* CONFIG_MESH */
9483 
9484 
9485 static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
9486 				      const u8 *ipaddr, int prefixlen,
9487 				      const u8 *addr)
9488 {
9489 #ifdef CONFIG_LIBNL3_ROUTE
9490 	struct i802_bss *bss = priv;
9491 	struct wpa_driver_nl80211_data *drv = bss->drv;
9492 	struct rtnl_neigh *rn;
9493 	struct nl_addr *nl_ipaddr = NULL;
9494 	struct nl_addr *nl_lladdr = NULL;
9495 	int family, addrsize;
9496 	int res;
9497 
9498 	if (!ipaddr || prefixlen == 0 || !addr)
9499 		return -EINVAL;
9500 
9501 	if (bss->br_ifindex == 0) {
9502 		wpa_printf(MSG_DEBUG,
9503 			   "nl80211: bridge must be set before adding an ip neigh to it");
9504 		return -1;
9505 	}
9506 
9507 	if (!drv->rtnl_sk) {
9508 		wpa_printf(MSG_DEBUG,
9509 			   "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9510 		return -1;
9511 	}
9512 
9513 	if (version == 4) {
9514 		family = AF_INET;
9515 		addrsize = 4;
9516 	} else if (version == 6) {
9517 		family = AF_INET6;
9518 		addrsize = 16;
9519 	} else {
9520 		return -EINVAL;
9521 	}
9522 
9523 	rn = rtnl_neigh_alloc();
9524 	if (rn == NULL)
9525 		return -ENOMEM;
9526 
9527 	/* set the destination ip address for neigh */
9528 	nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9529 	if (nl_ipaddr == NULL) {
9530 		wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9531 		res = -ENOMEM;
9532 		goto errout;
9533 	}
9534 	nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
9535 	res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9536 	if (res) {
9537 		wpa_printf(MSG_DEBUG,
9538 			   "nl80211: neigh set destination addr failed");
9539 		goto errout;
9540 	}
9541 
9542 	/* set the corresponding lladdr for neigh */
9543 	nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
9544 	if (nl_lladdr == NULL) {
9545 		wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
9546 		res = -ENOMEM;
9547 		goto errout;
9548 	}
9549 	rtnl_neigh_set_lladdr(rn, nl_lladdr);
9550 
9551 	rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9552 	rtnl_neigh_set_state(rn, NUD_PERMANENT);
9553 
9554 	res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
9555 	if (res) {
9556 		wpa_printf(MSG_DEBUG,
9557 			   "nl80211: Adding bridge ip neigh failed: %s",
9558 			   strerror(errno));
9559 	}
9560 errout:
9561 	if (nl_lladdr)
9562 		nl_addr_put(nl_lladdr);
9563 	if (nl_ipaddr)
9564 		nl_addr_put(nl_ipaddr);
9565 	if (rn)
9566 		rtnl_neigh_put(rn);
9567 	return res;
9568 #else /* CONFIG_LIBNL3_ROUTE */
9569 	return -1;
9570 #endif /* CONFIG_LIBNL3_ROUTE */
9571 }
9572 
9573 
9574 static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
9575 					 const u8 *ipaddr)
9576 {
9577 #ifdef CONFIG_LIBNL3_ROUTE
9578 	struct i802_bss *bss = priv;
9579 	struct wpa_driver_nl80211_data *drv = bss->drv;
9580 	struct rtnl_neigh *rn;
9581 	struct nl_addr *nl_ipaddr;
9582 	int family, addrsize;
9583 	int res;
9584 
9585 	if (!ipaddr)
9586 		return -EINVAL;
9587 
9588 	if (version == 4) {
9589 		family = AF_INET;
9590 		addrsize = 4;
9591 	} else if (version == 6) {
9592 		family = AF_INET6;
9593 		addrsize = 16;
9594 	} else {
9595 		return -EINVAL;
9596 	}
9597 
9598 	if (bss->br_ifindex == 0) {
9599 		wpa_printf(MSG_DEBUG,
9600 			   "nl80211: bridge must be set to delete an ip neigh");
9601 		return -1;
9602 	}
9603 
9604 	if (!drv->rtnl_sk) {
9605 		wpa_printf(MSG_DEBUG,
9606 			   "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9607 		return -1;
9608 	}
9609 
9610 	rn = rtnl_neigh_alloc();
9611 	if (rn == NULL)
9612 		return -ENOMEM;
9613 
9614 	/* set the destination ip address for neigh */
9615 	nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9616 	if (nl_ipaddr == NULL) {
9617 		wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9618 		res = -ENOMEM;
9619 		goto errout;
9620 	}
9621 	res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9622 	if (res) {
9623 		wpa_printf(MSG_DEBUG,
9624 			   "nl80211: neigh set destination addr failed");
9625 		goto errout;
9626 	}
9627 
9628 	rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9629 
9630 	res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
9631 	if (res) {
9632 		wpa_printf(MSG_DEBUG,
9633 			   "nl80211: Deleting bridge ip neigh failed: %s",
9634 			   strerror(errno));
9635 	}
9636 errout:
9637 	if (nl_ipaddr)
9638 		nl_addr_put(nl_ipaddr);
9639 	if (rn)
9640 		rtnl_neigh_put(rn);
9641 	return res;
9642 #else /* CONFIG_LIBNL3_ROUTE */
9643 	return -1;
9644 #endif /* CONFIG_LIBNL3_ROUTE */
9645 }
9646 
9647 
9648 static int linux_write_system_file(const char *path, unsigned int val)
9649 {
9650 	char buf[50];
9651 	int fd, len;
9652 
9653 	len = os_snprintf(buf, sizeof(buf), "%u\n", val);
9654 	if (os_snprintf_error(sizeof(buf), len))
9655 		return -1;
9656 
9657 	fd = open(path, O_WRONLY);
9658 	if (fd < 0)
9659 		return -1;
9660 
9661 	if (write(fd, buf, len) < 0) {
9662 		wpa_printf(MSG_DEBUG,
9663 			   "nl80211: Failed to write Linux system file: %s with the value of %d",
9664 			   path, val);
9665 		close(fd);
9666 		return -1;
9667 	}
9668 	close(fd);
9669 
9670 	return 0;
9671 }
9672 
9673 
9674 static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
9675 {
9676 	switch (attr) {
9677 	case DRV_BR_PORT_ATTR_PROXYARP:
9678 		return "proxyarp_wifi";
9679 	case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
9680 		return "hairpin_mode";
9681 	}
9682 
9683 	return NULL;
9684 }
9685 
9686 
9687 static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
9688 				       unsigned int val)
9689 {
9690 	struct i802_bss *bss = priv;
9691 	char path[128];
9692 	const char *attr_txt;
9693 
9694 	attr_txt = drv_br_port_attr_str(attr);
9695 	if (attr_txt == NULL)
9696 		return -EINVAL;
9697 
9698 	os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
9699 		    bss->ifname, attr_txt);
9700 
9701 	if (linux_write_system_file(path, val))
9702 		return -1;
9703 
9704 	return 0;
9705 }
9706 
9707 
9708 static const char * drv_br_net_param_str(enum drv_br_net_param param)
9709 {
9710 	switch (param) {
9711 	case DRV_BR_NET_PARAM_GARP_ACCEPT:
9712 		return "arp_accept";
9713 	default:
9714 		return NULL;
9715 	}
9716 }
9717 
9718 
9719 static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
9720 				       unsigned int val)
9721 {
9722 	struct i802_bss *bss = priv;
9723 	char path[128];
9724 	const char *param_txt;
9725 	int ip_version = 4;
9726 
9727 	if (param == DRV_BR_MULTICAST_SNOOPING) {
9728 		os_snprintf(path, sizeof(path),
9729 			    "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
9730 			    bss->brname);
9731 		goto set_val;
9732 	}
9733 
9734 	param_txt = drv_br_net_param_str(param);
9735 	if (param_txt == NULL)
9736 		return -EINVAL;
9737 
9738 	switch (param) {
9739 		case DRV_BR_NET_PARAM_GARP_ACCEPT:
9740 			ip_version = 4;
9741 			break;
9742 		default:
9743 			return -EINVAL;
9744 	}
9745 
9746 	os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
9747 		    ip_version, bss->brname, param_txt);
9748 
9749 set_val:
9750 	if (linux_write_system_file(path, val))
9751 		return -1;
9752 
9753 	return 0;
9754 }
9755 
9756 
9757 #ifdef CONFIG_DRIVER_NL80211_QCA
9758 
9759 static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
9760 {
9761 	switch (hw_mode) {
9762 	case HOSTAPD_MODE_IEEE80211B:
9763 		return QCA_ACS_MODE_IEEE80211B;
9764 	case HOSTAPD_MODE_IEEE80211G:
9765 		return QCA_ACS_MODE_IEEE80211G;
9766 	case HOSTAPD_MODE_IEEE80211A:
9767 		return QCA_ACS_MODE_IEEE80211A;
9768 	case HOSTAPD_MODE_IEEE80211AD:
9769 		return QCA_ACS_MODE_IEEE80211AD;
9770 	case HOSTAPD_MODE_IEEE80211ANY:
9771 		return QCA_ACS_MODE_IEEE80211ANY;
9772 	default:
9773 		return -1;
9774 	}
9775 }
9776 
9777 
9778 static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
9779 {
9780 	int i, len, ret;
9781 	u32 *freqs;
9782 
9783 	if (!freq_list)
9784 		return 0;
9785 	len = int_array_len(freq_list);
9786 	freqs = os_malloc(sizeof(u32) * len);
9787 	if (!freqs)
9788 		return -1;
9789 	for (i = 0; i < len; i++)
9790 		freqs[i] = freq_list[i];
9791 	ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
9792 		      sizeof(u32) * len, freqs);
9793 	os_free(freqs);
9794 	return ret;
9795 }
9796 
9797 
9798 static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
9799 {
9800 	struct i802_bss *bss = priv;
9801 	struct wpa_driver_nl80211_data *drv = bss->drv;
9802 	struct nl_msg *msg;
9803 	struct nlattr *data;
9804 	int ret;
9805 	int mode;
9806 
9807 	mode = hw_mode_to_qca_acs(params->hw_mode);
9808 	if (mode < 0)
9809 		return -1;
9810 
9811 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9812 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9813 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9814 			QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
9815 	    !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9816 	    nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
9817 	    (params->ht_enabled &&
9818 	     nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
9819 	    (params->ht40_enabled &&
9820 	     nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
9821 	    (params->vht_enabled &&
9822 	     nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
9823 	    nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
9824 			params->ch_width) ||
9825 	    (params->ch_list_len &&
9826 	     nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
9827 		     params->ch_list)) ||
9828 	    add_acs_freq_list(msg, params->freq_list)) {
9829 		nlmsg_free(msg);
9830 		return -ENOBUFS;
9831 	}
9832 	nla_nest_end(msg, data);
9833 
9834 	wpa_printf(MSG_DEBUG,
9835 		   "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
9836 		   params->hw_mode, params->ht_enabled, params->ht40_enabled,
9837 		   params->vht_enabled, params->ch_width, params->ch_list_len);
9838 
9839 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9840 	if (ret) {
9841 		wpa_printf(MSG_DEBUG,
9842 			   "nl80211: Failed to invoke driver ACS function: %s",
9843 			   strerror(errno));
9844 	}
9845 	return ret;
9846 }
9847 
9848 
9849 static int nl80211_set_band(void *priv, enum set_band band)
9850 {
9851 	struct i802_bss *bss = priv;
9852 	struct wpa_driver_nl80211_data *drv = bss->drv;
9853 	struct nl_msg *msg;
9854 	struct nlattr *data;
9855 	int ret;
9856 	enum qca_set_band qca_band;
9857 
9858 	if (!drv->setband_vendor_cmd_avail)
9859 		return -1;
9860 
9861 	switch (band) {
9862 	case WPA_SETBAND_AUTO:
9863 		qca_band = QCA_SETBAND_AUTO;
9864 		break;
9865 	case WPA_SETBAND_5G:
9866 		qca_band = QCA_SETBAND_5G;
9867 		break;
9868 	case WPA_SETBAND_2G:
9869 		qca_band = QCA_SETBAND_2G;
9870 		break;
9871 	default:
9872 		return -1;
9873 	}
9874 
9875 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9876 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9877 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9878 			QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
9879 	    !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9880 	    nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
9881 		nlmsg_free(msg);
9882 		return -ENOBUFS;
9883 	}
9884 	nla_nest_end(msg, data);
9885 
9886 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9887 	if (ret) {
9888 		wpa_printf(MSG_DEBUG,
9889 			   "nl80211: Driver setband function failed: %s",
9890 			   strerror(errno));
9891 	}
9892 	return ret;
9893 }
9894 
9895 
9896 struct nl80211_pcl {
9897 	unsigned int num;
9898 	unsigned int *freq_list;
9899 };
9900 
9901 static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
9902 {
9903 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
9904 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9905 	struct nl80211_pcl *param = arg;
9906 	struct nlattr *nl_vend, *attr;
9907 	enum qca_iface_type iface_type;
9908 	struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9909 	unsigned int num, max_num;
9910 	u32 *freqs;
9911 
9912 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9913 		  genlmsg_attrlen(gnlh, 0), NULL);
9914 
9915 	nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9916 	if (!nl_vend)
9917 		return NL_SKIP;
9918 
9919 	nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9920 		  nla_data(nl_vend), nla_len(nl_vend), NULL);
9921 
9922 	attr = tb_vendor[
9923 		QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
9924 	if (!attr) {
9925 		wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
9926 		param->num = 0;
9927 		return NL_SKIP;
9928 	}
9929 
9930 	iface_type = (enum qca_iface_type) nla_get_u32(attr);
9931 	wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
9932 		   iface_type);
9933 
9934 	attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
9935 	if (!attr) {
9936 		wpa_printf(MSG_ERROR,
9937 			   "nl80211: preferred_freq_list couldn't be found");
9938 		param->num = 0;
9939 		return NL_SKIP;
9940 	}
9941 
9942 	/*
9943 	 * param->num has the maximum number of entries for which there
9944 	 * is room in the freq_list provided by the caller.
9945 	 */
9946 	freqs = nla_data(attr);
9947 	max_num = nla_len(attr) / sizeof(u32);
9948 	if (max_num > param->num)
9949 		max_num = param->num;
9950 	for (num = 0; num < max_num; num++)
9951 		param->freq_list[num] = freqs[num];
9952 	param->num = num;
9953 
9954 	return NL_SKIP;
9955 }
9956 
9957 
9958 static int nl80211_get_pref_freq_list(void *priv,
9959 				      enum wpa_driver_if_type if_type,
9960 				      unsigned int *num,
9961 				      unsigned int *freq_list)
9962 {
9963 	struct i802_bss *bss = priv;
9964 	struct wpa_driver_nl80211_data *drv = bss->drv;
9965 	struct nl_msg *msg;
9966 	int ret;
9967 	unsigned int i;
9968 	struct nlattr *params;
9969 	struct nl80211_pcl param;
9970 	enum qca_iface_type iface_type;
9971 
9972 	if (!drv->get_pref_freq_list)
9973 		return -1;
9974 
9975 	switch (if_type) {
9976 	case WPA_IF_STATION:
9977 		iface_type = QCA_IFACE_TYPE_STA;
9978 		break;
9979 	case WPA_IF_AP_BSS:
9980 		iface_type = QCA_IFACE_TYPE_AP;
9981 		break;
9982 	case WPA_IF_P2P_GO:
9983 		iface_type = QCA_IFACE_TYPE_P2P_GO;
9984 		break;
9985 	case WPA_IF_P2P_CLIENT:
9986 		iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
9987 		break;
9988 	case WPA_IF_IBSS:
9989 		iface_type = QCA_IFACE_TYPE_IBSS;
9990 		break;
9991 	case WPA_IF_TDLS:
9992 		iface_type = QCA_IFACE_TYPE_TDLS;
9993 		break;
9994 	default:
9995 		return -1;
9996 	}
9997 
9998 	param.num = *num;
9999 	param.freq_list = freq_list;
10000 
10001 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10002 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
10003 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10004 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10005 			QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
10006 	    !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10007 	    nla_put_u32(msg,
10008 			QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
10009 			iface_type)) {
10010 		wpa_printf(MSG_ERROR,
10011 			   "%s: err in adding vendor_cmd and vendor_data",
10012 			   __func__);
10013 		nlmsg_free(msg);
10014 		return -1;
10015 	}
10016 	nla_nest_end(msg, params);
10017 
10018 	os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
10019 	ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
10020 	if (ret) {
10021 		wpa_printf(MSG_ERROR,
10022 			   "%s: err in send_and_recv_msgs", __func__);
10023 		return ret;
10024 	}
10025 
10026 	*num = param.num;
10027 
10028 	for (i = 0; i < *num; i++) {
10029 		wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
10030 			   i, freq_list[i]);
10031 	}
10032 
10033 	return 0;
10034 }
10035 
10036 
10037 static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
10038 {
10039 	struct i802_bss *bss = priv;
10040 	struct wpa_driver_nl80211_data *drv = bss->drv;
10041 	struct nl_msg *msg;
10042 	int ret;
10043 	struct nlattr *params;
10044 
10045 	if (!drv->set_prob_oper_freq)
10046 		return -1;
10047 
10048 	wpa_printf(MSG_DEBUG,
10049 		   "nl80211: Set P2P probable operating freq %u for ifindex %d",
10050 		   freq, bss->ifindex);
10051 
10052 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10053 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10054 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10055 			QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
10056 	    !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10057 	    nla_put_u32(msg,
10058 			QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
10059 			QCA_IFACE_TYPE_P2P_CLIENT) ||
10060 	    nla_put_u32(msg,
10061 			QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
10062 			freq)) {
10063 		wpa_printf(MSG_ERROR,
10064 			   "%s: err in adding vendor_cmd and vendor_data",
10065 			   __func__);
10066 		nlmsg_free(msg);
10067 		return -1;
10068 	}
10069 	nla_nest_end(msg, params);
10070 
10071 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10072 	msg = NULL;
10073 	if (ret) {
10074 		wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
10075 			   __func__);
10076 		return ret;
10077 	}
10078 	nlmsg_free(msg);
10079 	return 0;
10080 }
10081 
10082 
10083 static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
10084 				unsigned int period, unsigned int interval,
10085 				unsigned int count, const u8 *device_types,
10086 				size_t dev_types_len,
10087 				const u8 *ies, size_t ies_len)
10088 {
10089 	struct i802_bss *bss = priv;
10090 	struct wpa_driver_nl80211_data *drv = bss->drv;
10091 	struct nl_msg *msg;
10092 	struct nlattr *container;
10093 	int ret;
10094 
10095 	wpa_printf(MSG_DEBUG,
10096 		   "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
10097 		   freq, period, interval, count);
10098 
10099 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10100 		return -1;
10101 
10102 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10103 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10104 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10105 			QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
10106 		goto fail;
10107 
10108 	container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10109 	if (!container)
10110 		goto fail;
10111 
10112 	if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
10113 			freq) ||
10114 	    nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
10115 			period) ||
10116 	    nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
10117 			interval) ||
10118 	    nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
10119 			count) ||
10120 	    nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
10121 		    dev_types_len, device_types) ||
10122 	    nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
10123 		    ies_len, ies))
10124 		goto fail;
10125 
10126 	nla_nest_end(msg, container);
10127 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10128 	msg = NULL;
10129 	if (ret) {
10130 		wpa_printf(MSG_DEBUG,
10131 			   "nl80211: Failed to send P2P Listen offload vendor command");
10132 		goto fail;
10133 	}
10134 
10135 	return 0;
10136 
10137 fail:
10138 	nlmsg_free(msg);
10139 	return -1;
10140 }
10141 
10142 
10143 static int nl80211_p2p_lo_stop(void *priv)
10144 {
10145 	struct i802_bss *bss = priv;
10146 	struct wpa_driver_nl80211_data *drv = bss->drv;
10147 	struct nl_msg *msg;
10148 
10149 	wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
10150 
10151 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10152 		return -1;
10153 
10154 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10155 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10156 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10157 			QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
10158 		nlmsg_free(msg);
10159 		return -1;
10160 	}
10161 
10162 	return send_and_recv_msgs(drv, msg, NULL, NULL);
10163 }
10164 
10165 
10166 static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
10167 {
10168 	struct i802_bss *bss = priv;
10169 	struct wpa_driver_nl80211_data *drv = bss->drv;
10170 	struct nl_msg *msg;
10171 	struct nlattr *params;
10172 	int ret;
10173 	u32 tdls_mode;
10174 
10175 	wpa_printf(MSG_DEBUG,
10176 		   "nl80211: Set TDKS mode: tdls_external_control=%d",
10177 		   tdls_external_control);
10178 
10179 	if (tdls_external_control == 1)
10180 		tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
10181 			QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
10182 	else
10183 		tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
10184 
10185 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10186 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10187 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10188 			QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
10189 		goto fail;
10190 
10191 	params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10192 	if (!params)
10193 		goto fail;
10194 
10195 	if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
10196 			tdls_mode))
10197 		goto fail;
10198 
10199 	nla_nest_end(msg, params);
10200 
10201 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10202 	msg = NULL;
10203 	if (ret) {
10204 		wpa_printf(MSG_ERROR,
10205 			   "nl80211: Set TDLS mode failed: ret=%d (%s)",
10206 			   ret, strerror(-ret));
10207 		goto fail;
10208 	}
10209 	return 0;
10210 fail:
10211 	nlmsg_free(msg);
10212 	return -1;
10213 }
10214 
10215 
10216 #ifdef CONFIG_MBO
10217 
10218 static enum mbo_transition_reject_reason
10219 nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
10220 {
10221 	switch (status) {
10222 	case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
10223 		return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
10224 	case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
10225 		return MBO_TRANSITION_REJECT_REASON_DELAY;
10226 	case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
10227 		return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
10228 	case QCA_STATUS_REJECT_LOW_RSSI:
10229 		return MBO_TRANSITION_REJECT_REASON_RSSI;
10230 	case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
10231 		return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
10232 	case QCA_STATUS_REJECT_UNKNOWN:
10233 	default:
10234 		return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
10235 	}
10236 }
10237 
10238 
10239 static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
10240 					     struct nlattr *tb[], int num)
10241 {
10242 	enum qca_wlan_btm_candidate_status status;
10243 	char buf[50];
10244 
10245 	os_memcpy(candidate->bssid,
10246 		  nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
10247 		  ETH_ALEN);
10248 
10249 	status = nla_get_u32(
10250 		tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
10251 	candidate->is_accept = status == QCA_STATUS_ACCEPT;
10252 	candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
10253 
10254 	if (candidate->is_accept)
10255 		os_snprintf(buf, sizeof(buf), "Accepted");
10256 	else
10257 		os_snprintf(buf, sizeof(buf),
10258 			    "Rejected, Reject_reason: %d",
10259 			    candidate->reject_reason);
10260 	wpa_printf(MSG_DEBUG, "nl80211:   BSSID[%d]: " MACSTR " %s",
10261 		   num, MAC2STR(candidate->bssid), buf);
10262 }
10263 
10264 
10265 static int
10266 nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
10267 {
10268 	struct wpa_bss_candidate_info *info = arg;
10269 	struct candidate_list *candidate = info->candidates;
10270 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
10271 	struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10272 	struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
10273 	static struct nla_policy policy[
10274 		QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
10275 		[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
10276 			.minlen = ETH_ALEN
10277 		},
10278 		[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
10279 			.type = NLA_U32,
10280 		},
10281 	};
10282 	struct nlattr *attr;
10283 	int rem;
10284 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10285 	u8 num;
10286 
10287 	num = info->num; /* number of candidates sent to driver */
10288 	info->num = 0;
10289 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10290 		  genlmsg_attrlen(gnlh, 0), NULL);
10291 
10292 	if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
10293 	    nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10294 			     tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
10295 	    !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
10296 		return NL_SKIP;
10297 
10298 	wpa_printf(MSG_DEBUG,
10299 		   "nl80211: WNM Candidate list received from driver");
10300 	nla_for_each_nested(attr,
10301 			    tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
10302 			    rem) {
10303 		if (info->num >= num ||
10304 		    nla_parse_nested(
10305 			    tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
10306 			    attr, policy) ||
10307 		    !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
10308 		    !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
10309 			break;
10310 
10311 		nl80211_parse_btm_candidate_info(candidate, tb, info->num);
10312 
10313 		candidate++;
10314 		info->num++;
10315 	}
10316 
10317 	return NL_SKIP;
10318 }
10319 
10320 
10321 static struct wpa_bss_candidate_info *
10322 nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
10323 {
10324 	struct i802_bss *bss = priv;
10325 	struct wpa_driver_nl80211_data *drv = bss->drv;
10326 	struct nl_msg *msg;
10327 	struct nlattr *attr, *attr1, *attr2;
10328 	struct wpa_bss_candidate_info *info;
10329 	u8 i;
10330 	int ret;
10331 	u8 *pos;
10332 
10333 	if (!drv->fetch_bss_trans_status)
10334 		return NULL;
10335 
10336 	info = os_zalloc(sizeof(*info));
10337 	if (!info)
10338 		return NULL;
10339 	/* Allocate memory for number of candidates sent to driver */
10340 	info->candidates = os_calloc(params->n_candidates,
10341 				     sizeof(*info->candidates));
10342 	if (!info->candidates) {
10343 		os_free(info);
10344 		return NULL;
10345 	}
10346 
10347 	/* Copy the number of candidates being sent to driver. This is used in
10348 	 * nl80211_get_bss_transition_status_handler() to limit the number of
10349 	 * candidates that can be populated in info->candidates and will be
10350 	 * later overwritten with the actual number of candidates received from
10351 	 * the driver.
10352 	 */
10353 	info->num = params->n_candidates;
10354 
10355 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10356 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10357 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10358 			QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
10359 		goto fail;
10360 
10361 	attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10362 	if (!attr)
10363 		goto fail;
10364 
10365 	if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
10366 		       params->mbo_transition_reason))
10367 		goto fail;
10368 
10369 	attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
10370 	if (!attr1)
10371 		goto fail;
10372 
10373 	wpa_printf(MSG_DEBUG,
10374 		   "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
10375 		   params->mbo_transition_reason, params->n_candidates);
10376 	pos = params->bssid;
10377 	for (i = 0; i < params->n_candidates; i++) {
10378 		wpa_printf(MSG_DEBUG, "nl80211:   BSSID[%d]: " MACSTR, i,
10379 			   MAC2STR(pos));
10380 		attr2 = nla_nest_start(msg, i);
10381 		if (!attr2 ||
10382 		    nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
10383 			    ETH_ALEN, pos))
10384 			goto fail;
10385 		pos += ETH_ALEN;
10386 		nla_nest_end(msg, attr2);
10387 	}
10388 
10389 	nla_nest_end(msg, attr1);
10390 	nla_nest_end(msg, attr);
10391 
10392 	ret = send_and_recv_msgs(drv, msg,
10393 				 nl80211_get_bss_transition_status_handler,
10394 				 info);
10395 	msg = NULL;
10396 	if (ret) {
10397 		wpa_printf(MSG_ERROR,
10398 			   "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
10399 			   ret, strerror(-ret));
10400 		goto fail;
10401 	}
10402 	return info;
10403 
10404 fail:
10405 	nlmsg_free(msg);
10406 	os_free(info->candidates);
10407 	os_free(info);
10408 	return NULL;
10409 }
10410 
10411 
10412 /**
10413  * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
10414  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
10415  * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
10416  * Returns: 0 on success, -1 on failure
10417  */
10418 static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
10419 {
10420 	struct i802_bss *bss = priv;
10421 	struct wpa_driver_nl80211_data *drv = bss->drv;
10422 	struct nl_msg *msg;
10423 	struct nlattr *attr;
10424 	int ret = -1;
10425 
10426 	if (!drv->set_wifi_conf_vendor_cmd_avail)
10427 		return -1;
10428 
10429 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10430 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10431 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10432 			QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
10433 		goto fail;
10434 
10435 	attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10436 	if (!attr)
10437 		goto fail;
10438 
10439 	wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
10440 		   ignore_disallow);
10441 	if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
10442 		       ignore_disallow))
10443 		goto fail;
10444 
10445 	nla_nest_end(msg, attr);
10446 
10447 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10448 	msg = NULL;
10449 	if (ret) {
10450 		wpa_printf(MSG_ERROR,
10451 			   "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
10452 			   ret, strerror(-ret));
10453 		goto fail;
10454 	}
10455 
10456 fail:
10457 	nlmsg_free(msg);
10458 	return ret;
10459 }
10460 
10461 #endif /* CONFIG_MBO */
10462 
10463 #endif /* CONFIG_DRIVER_NL80211_QCA */
10464 
10465 
10466 static int nl80211_write_to_file(const char *name, unsigned int val)
10467 {
10468 	int fd, len;
10469 	char tmp[128];
10470 
10471 	fd = open(name, O_RDWR);
10472 	if (fd < 0) {
10473 		wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
10474 			   name, strerror(errno));
10475 		return fd;
10476 	}
10477 
10478 	len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
10479 	len = write(fd, tmp, len);
10480 	if (len < 0)
10481 		wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
10482 			   name, strerror(errno));
10483 	close(fd);
10484 
10485 	return 0;
10486 }
10487 
10488 
10489 static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
10490 {
10491 	struct i802_bss *bss = priv;
10492 	char path[128];
10493 	int ret;
10494 
10495 	wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
10496 		   filter_flags);
10497 
10498 	/* Configure filtering of unicast frame encrypted using GTK */
10499 	ret = os_snprintf(path, sizeof(path),
10500 			  "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
10501 			  bss->ifname);
10502 	if (os_snprintf_error(sizeof(path), ret))
10503 		return -1;
10504 
10505 	ret = nl80211_write_to_file(path,
10506 				    !!(filter_flags &
10507 				       WPA_DATA_FRAME_FILTER_FLAG_GTK));
10508 	if (ret) {
10509 		wpa_printf(MSG_ERROR,
10510 			   "nl80211: Failed to set IPv4 unicast in multicast filter");
10511 		return ret;
10512 	}
10513 
10514 	os_snprintf(path, sizeof(path),
10515 		    "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
10516 		    bss->ifname);
10517 	ret = nl80211_write_to_file(path,
10518 				    !!(filter_flags &
10519 				       WPA_DATA_FRAME_FILTER_FLAG_GTK));
10520 
10521 	if (ret) {
10522 		wpa_printf(MSG_ERROR,
10523 			   "nl80211: Failed to set IPv6 unicast in multicast filter");
10524 		return ret;
10525 	}
10526 
10527 	/* Configure filtering of unicast frame encrypted using GTK */
10528 	os_snprintf(path, sizeof(path),
10529 		    "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
10530 		    bss->ifname);
10531 	ret = nl80211_write_to_file(path,
10532 				    !!(filter_flags &
10533 				       WPA_DATA_FRAME_FILTER_FLAG_ARP));
10534 	if (ret) {
10535 		wpa_printf(MSG_ERROR,
10536 			   "nl80211: Failed set gratuitous ARP filter");
10537 		return ret;
10538 	}
10539 
10540 	/* Configure filtering of IPv6 NA frames */
10541 	os_snprintf(path, sizeof(path),
10542 		    "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
10543 		    bss->ifname);
10544 	ret = nl80211_write_to_file(path,
10545 				    !!(filter_flags &
10546 				       WPA_DATA_FRAME_FILTER_FLAG_NA));
10547 	if (ret) {
10548 		wpa_printf(MSG_ERROR,
10549 			   "nl80211: Failed to set unsolicited NA filter");
10550 		return ret;
10551 	}
10552 
10553 	return 0;
10554 }
10555 
10556 
10557 static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
10558 				 const u8 **ext_capa, const u8 **ext_capa_mask,
10559 				 unsigned int *ext_capa_len)
10560 {
10561 	struct i802_bss *bss = priv;
10562 	struct wpa_driver_nl80211_data *drv = bss->drv;
10563 	enum nl80211_iftype nlmode;
10564 	unsigned int i;
10565 
10566 	if (!ext_capa || !ext_capa_mask || !ext_capa_len)
10567 		return -1;
10568 
10569 	nlmode = wpa_driver_nl80211_if_type(type);
10570 
10571 	/* By default, use the per-radio values */
10572 	*ext_capa = drv->extended_capa;
10573 	*ext_capa_mask = drv->extended_capa_mask;
10574 	*ext_capa_len = drv->extended_capa_len;
10575 
10576 	/* Replace the default value if a per-interface type value exists */
10577 	for (i = 0; i < drv->num_iface_ext_capa; i++) {
10578 		if (nlmode == drv->iface_ext_capa[i].iftype) {
10579 			*ext_capa = drv->iface_ext_capa[i].ext_capa;
10580 			*ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
10581 			*ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
10582 			break;
10583 		}
10584 	}
10585 
10586 	return 0;
10587 }
10588 
10589 
10590 static int nl80211_update_connection_params(
10591 	void *priv, struct wpa_driver_associate_params *params,
10592 	enum wpa_drv_update_connect_params_mask mask)
10593 {
10594 	struct i802_bss *bss = priv;
10595 	struct wpa_driver_nl80211_data *drv = bss->drv;
10596 	struct nl_msg *msg;
10597 	int ret = -1;
10598 	enum nl80211_auth_type type;
10599 
10600 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
10601 	if (!msg)
10602 		goto fail;
10603 
10604 	wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
10605 		   drv->ifindex);
10606 
10607 	if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
10608 		if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
10609 			    params->wpa_ie))
10610 			goto fail;
10611 		wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie,
10612 			    params->wpa_ie_len);
10613 	}
10614 
10615 	if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
10616 		type = get_nl_auth_type(params->auth_alg);
10617 		if (type == NL80211_AUTHTYPE_MAX ||
10618 		    nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
10619 			goto fail;
10620 		wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
10621 	}
10622 
10623 	if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
10624 	    nl80211_put_fils_connect_params(drv, params, msg))
10625 		goto fail;
10626 
10627 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10628 	msg = NULL;
10629 	if (ret)
10630 		wpa_dbg(drv->ctx, MSG_DEBUG,
10631 			"nl80211: Update connect params command failed: ret=%d (%s)",
10632 			ret, strerror(-ret));
10633 
10634 fail:
10635 	nlmsg_free(msg);
10636 	return ret;
10637 }
10638 
10639 
10640 static int nl80211_send_external_auth_status(void *priv,
10641 					     struct external_auth *params)
10642 {
10643 	struct i802_bss *bss = priv;
10644 	struct wpa_driver_nl80211_data *drv = bss->drv;
10645 	struct nl_msg *msg = NULL;
10646 	int ret = -1;
10647 
10648 	wpa_dbg(drv->ctx, MSG_DEBUG,
10649 		"nl80211: External auth status: %u", params->status);
10650 
10651 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
10652 	if (!msg ||
10653 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
10654 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
10655 		    params->ssid) ||
10656 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid))
10657 		goto fail;
10658 	ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10659 	msg = NULL;
10660 	if (ret) {
10661 		wpa_printf(MSG_DEBUG,
10662 			   "nl80211: External Auth status update failed: ret=%d (%s)",
10663 			   ret, strerror(-ret));
10664 		goto fail;
10665 	}
10666 fail:
10667 	nlmsg_free(msg);
10668 	return ret;
10669 }
10670 
10671 
10672 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
10673 	.name = "nl80211",
10674 	.desc = "Linux nl80211/cfg80211",
10675 	.get_bssid = wpa_driver_nl80211_get_bssid,
10676 	.get_ssid = wpa_driver_nl80211_get_ssid,
10677 	.set_key = driver_nl80211_set_key,
10678 	.scan2 = driver_nl80211_scan2,
10679 	.sched_scan = wpa_driver_nl80211_sched_scan,
10680 	.stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
10681 	.get_scan_results2 = wpa_driver_nl80211_get_scan_results,
10682 	.abort_scan = wpa_driver_nl80211_abort_scan,
10683 	.deauthenticate = driver_nl80211_deauthenticate,
10684 	.authenticate = driver_nl80211_authenticate,
10685 	.associate = wpa_driver_nl80211_associate,
10686 	.global_init = nl80211_global_init,
10687 	.global_deinit = nl80211_global_deinit,
10688 	.init2 = wpa_driver_nl80211_init,
10689 	.deinit = driver_nl80211_deinit,
10690 	.get_capa = wpa_driver_nl80211_get_capa,
10691 	.set_operstate = wpa_driver_nl80211_set_operstate,
10692 	.set_supp_port = wpa_driver_nl80211_set_supp_port,
10693 	.set_country = wpa_driver_nl80211_set_country,
10694 	.get_country = wpa_driver_nl80211_get_country,
10695 	.set_ap = wpa_driver_nl80211_set_ap,
10696 	.set_acl = wpa_driver_nl80211_set_acl,
10697 	.if_add = wpa_driver_nl80211_if_add,
10698 	.if_remove = driver_nl80211_if_remove,
10699 	.send_mlme = driver_nl80211_send_mlme,
10700 	.get_hw_feature_data = nl80211_get_hw_feature_data,
10701 	.sta_add = wpa_driver_nl80211_sta_add,
10702 	.sta_remove = driver_nl80211_sta_remove,
10703 	.hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
10704 	.sta_set_flags = wpa_driver_nl80211_sta_set_flags,
10705 	.hapd_init = i802_init,
10706 	.hapd_deinit = i802_deinit,
10707 	.set_wds_sta = i802_set_wds_sta,
10708 	.get_seqnum = i802_get_seqnum,
10709 	.flush = i802_flush,
10710 	.get_inact_sec = i802_get_inact_sec,
10711 	.sta_clear_stats = i802_sta_clear_stats,
10712 	.set_rts = i802_set_rts,
10713 	.set_frag = i802_set_frag,
10714 	.set_tx_queue_params = i802_set_tx_queue_params,
10715 	.set_sta_vlan = driver_nl80211_set_sta_vlan,
10716 	.sta_deauth = i802_sta_deauth,
10717 	.sta_disassoc = i802_sta_disassoc,
10718 	.read_sta_data = driver_nl80211_read_sta_data,
10719 	.set_freq = i802_set_freq,
10720 	.send_action = driver_nl80211_send_action,
10721 	.send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
10722 	.remain_on_channel = wpa_driver_nl80211_remain_on_channel,
10723 	.cancel_remain_on_channel =
10724 	wpa_driver_nl80211_cancel_remain_on_channel,
10725 	.probe_req_report = driver_nl80211_probe_req_report,
10726 	.deinit_ap = wpa_driver_nl80211_deinit_ap,
10727 	.deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
10728 	.resume = wpa_driver_nl80211_resume,
10729 	.signal_monitor = nl80211_signal_monitor,
10730 	.signal_poll = nl80211_signal_poll,
10731 	.send_frame = nl80211_send_frame,
10732 	.set_param = nl80211_set_param,
10733 	.get_radio_name = nl80211_get_radio_name,
10734 	.add_pmkid = nl80211_add_pmkid,
10735 	.remove_pmkid = nl80211_remove_pmkid,
10736 	.flush_pmkid = nl80211_flush_pmkid,
10737 	.set_rekey_info = nl80211_set_rekey_info,
10738 	.poll_client = nl80211_poll_client,
10739 	.set_p2p_powersave = nl80211_set_p2p_powersave,
10740 	.start_dfs_cac = nl80211_start_radar_detection,
10741 	.stop_ap = wpa_driver_nl80211_stop_ap,
10742 #ifdef CONFIG_TDLS
10743 	.send_tdls_mgmt = nl80211_send_tdls_mgmt,
10744 	.tdls_oper = nl80211_tdls_oper,
10745 	.tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
10746 	.tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
10747 #endif /* CONFIG_TDLS */
10748 	.update_ft_ies = wpa_driver_nl80211_update_ft_ies,
10749 	.get_mac_addr = wpa_driver_nl80211_get_macaddr,
10750 	.get_survey = wpa_driver_nl80211_get_survey,
10751 	.status = wpa_driver_nl80211_status,
10752 	.switch_channel = nl80211_switch_channel,
10753 #ifdef ANDROID_P2P
10754 	.set_noa = wpa_driver_set_p2p_noa,
10755 	.get_noa = wpa_driver_get_p2p_noa,
10756 	.set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
10757 #endif /* ANDROID_P2P */
10758 #ifdef ANDROID
10759 #ifndef ANDROID_LIB_STUB
10760 	.driver_cmd = wpa_driver_nl80211_driver_cmd,
10761 #endif /* !ANDROID_LIB_STUB */
10762 #endif /* ANDROID */
10763 	.vendor_cmd = nl80211_vendor_cmd,
10764 	.set_qos_map = nl80211_set_qos_map,
10765 	.set_wowlan = nl80211_set_wowlan,
10766 	.set_mac_addr = nl80211_set_mac_addr,
10767 #ifdef CONFIG_MESH
10768 	.init_mesh = wpa_driver_nl80211_init_mesh,
10769 	.join_mesh = wpa_driver_nl80211_join_mesh,
10770 	.leave_mesh = wpa_driver_nl80211_leave_mesh,
10771 #endif /* CONFIG_MESH */
10772 	.br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
10773 	.br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
10774 	.br_port_set_attr = wpa_driver_br_port_set_attr,
10775 	.br_set_net_param = wpa_driver_br_set_net_param,
10776 	.add_tx_ts = nl80211_add_ts,
10777 	.del_tx_ts = nl80211_del_ts,
10778 	.get_ifindex = nl80211_get_ifindex,
10779 #ifdef CONFIG_DRIVER_NL80211_QCA
10780 	.roaming = nl80211_roaming,
10781 	.disable_fils = nl80211_disable_fils,
10782 	.do_acs = wpa_driver_do_acs,
10783 	.set_band = nl80211_set_band,
10784 	.get_pref_freq_list = nl80211_get_pref_freq_list,
10785 	.set_prob_oper_freq = nl80211_set_prob_oper_freq,
10786 	.p2p_lo_start = nl80211_p2p_lo_start,
10787 	.p2p_lo_stop = nl80211_p2p_lo_stop,
10788 	.set_default_scan_ies = nl80211_set_default_scan_ies,
10789 	.set_tdls_mode = nl80211_set_tdls_mode,
10790 #ifdef CONFIG_MBO
10791 	.get_bss_transition_status = nl80211_get_bss_transition_status,
10792 	.ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
10793 #endif /* CONFIG_MBO */
10794 	.set_bssid_blacklist = nl80211_set_bssid_blacklist,
10795 #endif /* CONFIG_DRIVER_NL80211_QCA */
10796 	.configure_data_frame_filters = nl80211_configure_data_frame_filters,
10797 	.get_ext_capab = nl80211_get_ext_capab,
10798 	.update_connect_params = nl80211_update_connection_params,
10799 	.send_external_auth_status = nl80211_send_external_auth_status,
10800 };
10801