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