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