blob: 95e678fcf30dcaefd6b12f9e060b722e1351f814 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * 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 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include <sys/types.h>
Sunil Ravib0ac25f2024-07-12 01:42:03 +000015#include <sys/utsname.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include <fcntl.h>
17#include <net/if.h>
18#include <netlink/genl/genl.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include <netlink/genl/ctrl.h>
Sunil Ravib0ac25f2024-07-12 01:42:03 +000020#include <netlink/genl/family.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include <linux/rtnetlink.h>
22#include <netpacket/packet.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080023#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024
25#include "common.h"
26#include "eloop.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080027#include "common/qca-vendor.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070028#include "common/qca-vendor-attr.h"
Hai Shalomc1a21442022-02-04 13:43:00 -080029#include "common/brcm_vendor.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "common/ieee802_11_common.h"
Paul Stewart092955c2017-02-06 09:13:09 -080032#include "common/wpa_common.h"
Sunil Ravic0f5d412024-09-11 22:12:49 +000033#include "common/nan.h"
34#include "common/nan_de.h"
Sunil Ravi89eba102022-09-13 21:04:37 -070035#include "crypto/sha256.h"
36#include "crypto/sha384.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "netlink.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080038#include "linux_defines.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "linux_ioctl.h"
40#include "radiotap.h"
41#include "radiotap_iter.h"
42#include "rfkill.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080043#include "driver_nl80211.h"
Andy Kuoaba17c12022-04-14 16:05:31 +080044#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Ajay Davanagerib921bb82020-09-16 12:49:08 +053045#include "common/brcm_vendor.h"
Andy Kuoaba17c12022-04-14 16:05:31 +080046#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080047
Hai Shalom74f70d42019-02-11 14:42:39 -080048#ifndef NETLINK_CAP_ACK
49#define NETLINK_CAP_ACK 10
50#endif /* NETLINK_CAP_ACK */
Hai Shalom39ba6fc2019-01-22 12:40:38 -080051/* support for extack if compilation headers are too old */
52#ifndef NETLINK_EXT_ACK
53#define NETLINK_EXT_ACK 11
54enum nlmsgerr_attrs {
55 NLMSGERR_ATTR_UNUSED,
56 NLMSGERR_ATTR_MSG,
57 NLMSGERR_ATTR_OFFS,
58 NLMSGERR_ATTR_COOKIE,
59
60 __NLMSGERR_ATTR_MAX,
61 NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1
62};
63#endif
64#ifndef NLM_F_CAPPED
65#define NLM_F_CAPPED 0x100
66#endif
67#ifndef NLM_F_ACK_TLVS
68#define NLM_F_ACK_TLVS 0x200
69#endif
70#ifndef SOL_NETLINK
71#define SOL_NETLINK 270
72#endif
73
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074
Dmitry Shmidt54605472013-11-08 11:10:19 -080075#ifdef ANDROID
76/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
Dmitry Shmidt54605472013-11-08 11:10:19 -080077#undef nl_socket_set_nonblocking
78#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080079
Dmitry Shmidt54605472013-11-08 11:10:19 -080080#endif /* ANDROID */
81
82
Hai Shalomfdcde762020-04-02 11:19:20 -070083static struct nl_sock * nl_create_handle(struct nl_cb *cb, const char *dbg)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080084{
Hai Shalomfdcde762020-04-02 11:19:20 -070085 struct nl_sock *handle;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080086
Hai Shalomfdcde762020-04-02 11:19:20 -070087 handle = nl_socket_alloc_cb(cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080088 if (handle == NULL) {
89 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
90 "callbacks (%s)", dbg);
91 return NULL;
92 }
93
94 if (genl_connect(handle)) {
95 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
96 "netlink (%s)", dbg);
Hai Shalomfdcde762020-04-02 11:19:20 -070097 nl_socket_free(handle);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080098 return NULL;
99 }
100
101 return handle;
102}
103
104
Hai Shalomfdcde762020-04-02 11:19:20 -0700105static void nl_destroy_handles(struct nl_sock **handle)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800106{
107 if (*handle == NULL)
108 return;
Hai Shalomfdcde762020-04-02 11:19:20 -0700109 nl_socket_free(*handle);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800110 *handle = NULL;
111}
112
113
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700114#if __WORDSIZE == 64
115#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
116#else
117#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
118#endif
119
Hai Shalomfdcde762020-04-02 11:19:20 -0700120static void nl80211_register_eloop_read(struct nl_sock **handle,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700121 eloop_sock_handler handler,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700122 void *eloop_data, int persist)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700123{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800124 /*
125 * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
126 * by default. It is possible to hit that limit in some cases where
127 * operations are blocked, e.g., with a burst of Deauthentication frames
128 * to hostapd and STA entry deletion. Try to increase the buffer to make
129 * this less likely to occur.
130 */
Hai Shalomfdcde762020-04-02 11:19:20 -0700131 int err;
132
133 err = nl_socket_set_buffer_size(*handle, 262144, 0);
134 if (err < 0) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800135 wpa_printf(MSG_DEBUG,
136 "nl80211: Could not set nl_socket RX buffer size: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -0700137 nl_geterror(err));
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800138 /* continue anyway with the default (smaller) buffer */
139 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800140
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700141 nl_socket_set_nonblocking(*handle);
142 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
143 eloop_data, *handle);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700144 if (!persist)
145 *handle = (void *) (((intptr_t) *handle) ^
146 ELOOP_SOCKET_INVALID);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700147}
148
149
Hai Shalomfdcde762020-04-02 11:19:20 -0700150static void nl80211_destroy_eloop_handle(struct nl_sock **handle, int persist)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700151{
Roshan Pius3a1667e2018-07-03 15:17:14 -0700152 if (!persist)
153 *handle = (void *) (((intptr_t) *handle) ^
154 ELOOP_SOCKET_INVALID);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700155 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
156 nl_destroy_handles(handle);
157}
158
159
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800160static void nl80211_global_deinit(void *priv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800161static void nl80211_check_global(struct nl80211_global *global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800162
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800163static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700164static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
165 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800168wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800169 const u8 *set_addr, int first,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000170 const char *driver_params,
171 enum wpa_p2p_mode p2p_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800172static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 unsigned int freq, unsigned int wait,
Hai Shalomfdcde762020-04-02 11:19:20 -0700174 const u8 *buf, size_t buf_len,
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000175 int save_cookie, int no_cck, int no_ack,
176 int offchanok, const u16 *csa_offs,
177 size_t csa_offs_len, int link_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800178static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
179 int report);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700180
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800181#define IFIDX_ANY -1
182
183static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
184 int ifidx_reason);
185static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
186 int ifidx_reason);
187static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
188 int ifidx_reason);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700189
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700190static int nl80211_set_channel(struct i802_bss *bss,
191 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
193 int ifindex, int disabled);
194
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800195static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
196 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800197
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700198static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800199static int nl80211_set_param(void *priv, const char *param);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000200static void nl80211_remove_links(struct i802_bss *bss);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -0700201#ifdef CONFIG_MESH
202static int nl80211_put_mesh_config(struct nl_msg *msg,
203 struct wpa_driver_mesh_bss_params *params);
204#endif /* CONFIG_MESH */
Dmitry Shmidt29333592017-01-09 12:27:11 -0800205static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -0700206 u16 reason);
Winnie Chen4138eec2022-11-10 16:32:53 +0800207#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawade62409f2022-01-20 12:32:07 +0530208static int nl80211_set_td_policy(void *priv, u32 td_policy);
Winnie Chen4138eec2022-11-10 16:32:53 +0800209#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700210
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800211/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800212enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800213{
214 switch (width) {
215 case NL80211_CHAN_WIDTH_20_NOHT:
216 return CHAN_WIDTH_20_NOHT;
217 case NL80211_CHAN_WIDTH_20:
218 return CHAN_WIDTH_20;
219 case NL80211_CHAN_WIDTH_40:
220 return CHAN_WIDTH_40;
221 case NL80211_CHAN_WIDTH_80:
222 return CHAN_WIDTH_80;
223 case NL80211_CHAN_WIDTH_80P80:
224 return CHAN_WIDTH_80P80;
225 case NL80211_CHAN_WIDTH_160:
226 return CHAN_WIDTH_160;
Sunil8cd6f4d2022-06-28 18:40:46 +0000227 case NL80211_CHAN_WIDTH_320:
228 return CHAN_WIDTH_320;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000229 default:
230 return CHAN_WIDTH_UNKNOWN;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800231 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800232}
233
234
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800235int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800236{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700237 return nlmode == NL80211_IFTYPE_AP ||
238 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800239}
240
241
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800242int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800243{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700244 return nlmode == NL80211_IFTYPE_STATION ||
245 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800246}
247
248
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700249static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800250{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700251 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
252 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800253}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254
255
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800256struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
257 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700258{
259 struct i802_bss *bss;
260
261 for (bss = drv->first_bss; bss; bss = bss->next) {
262 if (bss->ifindex == ifindex)
263 return bss;
264 }
265
266 return NULL;
267}
268
269
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800270static int is_mesh_interface(enum nl80211_iftype nlmode)
271{
272 return nlmode == NL80211_IFTYPE_MESH_POINT;
273}
274
275
276void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700277{
278 if (drv->associated)
279 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
280 drv->associated = 0;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000281 os_memset(&drv->sta_mlo_info, 0, sizeof(drv->sta_mlo_info));
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700282 os_memset(drv->bssid, 0, ETH_ALEN);
Sunil Ravi036cec52023-03-29 11:35:17 -0700283 drv->first_bss->flink->freq = 0;
Sunil Ravi89eba102022-09-13 21:04:37 -0700284#ifdef CONFIG_DRIVER_NL80211_QCA
285 os_free(drv->pending_roam_data);
286 drv->pending_roam_data = NULL;
Sunil Ravi640215c2023-06-28 23:08:09 +0000287 os_free(drv->pending_t2lm_data);
288 drv->pending_t2lm_data = NULL;
289 os_free(drv->pending_link_reconfig_data);
290 drv->pending_link_reconfig_data = NULL;
Sunil Ravi89eba102022-09-13 21:04:37 -0700291#endif /* CONFIG_DRIVER_NL80211_QCA */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000292
293 drv->auth_mld = false;
294 drv->auth_mld_link_id = -1;
295 os_memset(drv->auth_ap_mld_addr, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700296}
297
298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299/* nl80211 code */
300static int ack_handler(struct nl_msg *msg, void *arg)
301{
302 int *err = arg;
303 *err = 0;
304 return NL_STOP;
305}
306
Hai Shalom899fcc72020-10-19 14:38:18 -0700307
308struct nl80211_ack_ext_arg {
309 int *err;
310 void *ext_data;
311};
312
313
314static int ack_handler_cookie(struct nl_msg *msg, void *arg)
315{
316 struct nl80211_ack_ext_arg *ext_arg = arg;
317 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
318 u64 *cookie = ext_arg->ext_data;
319 struct nlattr *attrs;
320 size_t ack_len, attr_len;
321
322 *ext_arg->err = 0;
323 ack_len = sizeof(struct nlmsghdr) + sizeof(int) +
324 sizeof(struct nlmsghdr);
325 attrs = (struct nlattr *)
326 ((u8 *) nlmsg_data(nlmsg_hdr(msg)) + sizeof(struct nlmsghdr) +
327 sizeof(int));
328 if (nlmsg_hdr(msg)->nlmsg_len <= ack_len)
329 return NL_STOP;
330
331 attr_len = nlmsg_hdr(msg)->nlmsg_len - ack_len;
332
333 if(!(nlmsg_hdr(msg)->nlmsg_flags & NLM_F_ACK_TLVS))
334 return NL_STOP;
335
336 nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, attr_len, NULL);
337 if (tb[NLMSGERR_ATTR_COOKIE])
338 *cookie = nla_get_u64(tb[NLMSGERR_ATTR_COOKIE]);
339
340 return NL_STOP;
341}
342
343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344static int finish_handler(struct nl_msg *msg, void *arg)
345{
346 int *ret = arg;
347 *ret = 0;
348 return NL_SKIP;
349}
350
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000351struct nl80211_ack_err_args {
352 int err;
353 struct nl_msg *orig_msg;
354 struct nl80211_err_info *err_info;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000355 struct wpa_driver_nl80211_data *drv;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000356};
357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
359 void *arg)
360{
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000361 struct nl80211_ack_err_args *err_args = arg;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800362 struct nlmsghdr *nlh = (struct nlmsghdr *) err - 1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000363 struct nlmsghdr *orig_nlh = nlmsg_hdr(err_args->orig_msg);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800364 int len = nlh->nlmsg_len;
365 struct nlattr *attrs;
366 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800367 int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000368 struct nlattr *mlo_links, *link_attr;
369 u32 offset;
370 int rem;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800371
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000372 err_args->err = err->error;
373 if (err_args->err_info)
374 err_args->err_info->link_id = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800375
376 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
377 return NL_SKIP;
378
379 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
380 ack_len += err->msg.nlmsg_len - sizeof(*nlh);
381
382 if (len <= ack_len)
383 return NL_STOP;
384
385 attrs = (void *) ((unsigned char *) nlh + ack_len);
386 len -= ack_len;
387
388 nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, len, NULL);
389 if (tb[NLMSGERR_ATTR_MSG]) {
390 len = strnlen((char *) nla_data(tb[NLMSGERR_ATTR_MSG]),
391 nla_len(tb[NLMSGERR_ATTR_MSG]));
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000392 if (err_args->drv)
393 wpa_msg(err_args->drv->ctx, MSG_ERROR, "nl80211: kernel reports: %*s",
394 len, (char *) nla_data(tb[NLMSGERR_ATTR_MSG]));
395 else
396 wpa_printf(MSG_ERROR, "nl80211: kernel reports: %*s",
397 len, (char *) nla_data(tb[NLMSGERR_ATTR_MSG]));
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800398 }
399
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000400 if (!err_args->err_info)
401 return NL_SKIP;
402
403 /* Check if it was a per-link error report */
404
405 if (!tb[NLMSGERR_ATTR_OFFS] ||
406 os_memcmp(orig_nlh, &err->msg, sizeof(err->msg)) != 0)
407 return NL_SKIP;
408
409 offset = nla_get_u32(tb[NLMSGERR_ATTR_OFFS]);
410
411 mlo_links = nlmsg_find_attr(orig_nlh, GENL_HDRLEN,
412 NL80211_ATTR_MLO_LINKS);
413 if (!mlo_links)
414 return NL_SKIP;
415
416 nla_for_each_nested(link_attr, mlo_links, rem) {
417 struct nlattr *link_id;
418 size_t link_offset = (u8 *) link_attr - (u8 *) orig_nlh;
419
420 if (offset < link_offset ||
421 offset >= link_offset + link_attr->nla_len)
422 continue;
423
424 link_id = nla_find(nla_data(link_attr), nla_len(link_attr),
425 NL80211_ATTR_MLO_LINK_ID);
426 if (link_id) {
427 err_args->err_info->link_id = nla_get_u8(link_id);
428 wpa_printf(MSG_DEBUG,
429 "nl80211: kernel reports error for link: %d",
430 err_args->err_info->link_id);
431 break;
432 }
433 }
434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 return NL_SKIP;
436}
437
438
439static int no_seq_check(struct nl_msg *msg, void *arg)
440{
441 return NL_OK;
442}
443
444
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800445static void nl80211_nlmsg_clear(struct nl_msg *msg)
446{
447 /*
448 * Clear nlmsg data, e.g., to make sure key material is not left in
449 * heap memory for unnecessarily long time.
450 */
451 if (msg) {
452 struct nlmsghdr *hdr = nlmsg_hdr(msg);
453 void *data = nlmsg_data(hdr);
454 /*
455 * This would use nlmsg_datalen() or the older nlmsg_len() if
456 * only libnl were to maintain a stable API.. Neither will work
457 * with all released versions, so just calculate the length
458 * here.
459 */
460 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
461
462 os_memset(data, 0, len);
463 }
464}
465
466
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000467static int send_event_marker(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468{
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000469 struct nl_sock *handle;
470 struct nl_msg *msg;
471 struct nlmsghdr *hdr;
472 int res = 0;
473 int err = -NLE_NOMEM;
474
475 msg = nlmsg_alloc();
476 if (!msg)
477 goto out;
478
479 /* We only care about the returned sequence number for matching. */
480 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES))
481 goto out;
482
483 handle = (void *) (((intptr_t) drv->global->nl_event) ^
484 ELOOP_SOCKET_INVALID);
485
486 err = nl_send_auto_complete(handle, msg);
487 if (err < 0)
488 goto out;
489
490 hdr = nlmsg_hdr(msg);
491 res = hdr->nlmsg_seq;
492
493out:
494 nlmsg_free(msg);
495 if (err)
496 wpa_printf(MSG_INFO, "nl80211: %s failed: %s",
497 __func__, nl_geterror(err));
498 return res;
499}
500
501
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000502int send_and_recv_glb(struct nl80211_global *global,
503 struct wpa_driver_nl80211_data *drv, /* may be NULL */
504 struct nl_sock *nl_handle, struct nl_msg *msg,
505 int (*valid_handler)(struct nl_msg *, void *),
506 void *valid_data,
507 int (*ack_handler_custom)(struct nl_msg *, void *),
508 void *ack_data,
509 struct nl80211_err_info *err_info)
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000510{
511 struct nl_cb *cb, *s_nl_cb;
512 struct nl80211_ack_err_args err;
513 int opt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800515 if (!msg)
516 return -ENOMEM;
517
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000518 err.err = -ENOMEM;
519
520 s_nl_cb = nl_socket_get_cb(nl_handle);
521 cb = nl_cb_clone(s_nl_cb);
522 nl_cb_put(s_nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 if (!cb)
524 goto out;
525
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800526 /* try to set NETLINK_EXT_ACK to 1, ignoring errors */
527 opt = 1;
528 setsockopt(nl_socket_get_fd(nl_handle), SOL_NETLINK,
529 NETLINK_EXT_ACK, &opt, sizeof(opt));
530
Hai Shalom74f70d42019-02-11 14:42:39 -0800531 /* try to set NETLINK_CAP_ACK to 1, ignoring errors */
532 opt = 1;
533 setsockopt(nl_socket_get_fd(nl_handle), SOL_NETLINK,
534 NETLINK_CAP_ACK, &opt, sizeof(opt));
535
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000536 err.err = nl_send_auto_complete(nl_handle, msg);
537 if (err.err < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700538 wpa_printf(MSG_INFO,
539 "nl80211: nl_send_auto_complete() failed: %s",
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000540 nl_geterror(err.err));
Hai Shalomfdcde762020-04-02 11:19:20 -0700541 /* Need to convert libnl error code to an errno value. For now,
542 * just hardcode this to EBADF; the real error reason is shown
543 * in that error print above. */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000544 err.err = -EBADF;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 goto out;
Hai Shalomfdcde762020-04-02 11:19:20 -0700546 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000548 err.err = 1;
549 err.orig_msg = msg;
550 err.err_info = err_info;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000551 err.drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552
553 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000554 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err.err);
Hai Shalom899fcc72020-10-19 14:38:18 -0700555 if (ack_handler_custom) {
556 struct nl80211_ack_ext_arg *ext_arg = ack_data;
557
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000558 ext_arg->err = &err.err;
Hai Shalom899fcc72020-10-19 14:38:18 -0700559 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM,
560 ack_handler_custom, ack_data);
561 } else {
562 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
563 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564
565 if (valid_handler)
566 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
567 valid_handler, valid_data);
568
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000569 while (err.err > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700570 int res = nl_recvmsgs(nl_handle, cb);
Hai Shalomfdcde762020-04-02 11:19:20 -0700571
572 if (res == -NLE_DUMP_INTR) {
573 /* Most likely one of the nl80211 dump routines hit a
574 * case where internal results changed while the dump
575 * was being sent. The most common known case for this
576 * is scan results fetching while associated were every
577 * received Beacon frame from the AP may end up
578 * incrementing bss_generation. This
579 * NL80211_CMD_GET_SCAN case tries again in the caller;
580 * other cases (of which there are no known common ones)
581 * will stop and return an error. */
582 wpa_printf(MSG_DEBUG, "nl80211: %s; convert to -EAGAIN",
583 nl_geterror(res));
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000584 err.err = -EAGAIN;
Hai Shalomfdcde762020-04-02 11:19:20 -0700585 } else if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700586 wpa_printf(MSG_INFO,
Hai Shalomfdcde762020-04-02 11:19:20 -0700587 "nl80211: %s->nl_recvmsgs failed: %d (%s)",
588 __func__, res, nl_geterror(res));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700589 }
590 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700591 out:
592 nl_cb_put(cb);
Hai Shalom60840252021-02-19 19:02:11 -0800593 /* Always clear the message as it can potentially contain keys */
594 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 nlmsg_free(msg);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000596 return err.err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597}
598
599
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000600static int nl80211_put_control_port(struct wpa_driver_nl80211_data *drv,
601 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602{
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000603 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT) ||
604 nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
605 ((drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX) &&
606 (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_OVER_NL80211) ||
607 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_PREAUTH))))
Hai Shalomb755a2a2020-04-23 21:49:02 -0700608 return -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000609 return 0;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700610}
611
612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613struct family_data {
614 const char *group;
615 int id;
616};
617
618
619static int family_handler(struct nl_msg *msg, void *arg)
620{
621 struct family_data *res = arg;
622 struct nlattr *tb[CTRL_ATTR_MAX + 1];
623 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
624 struct nlattr *mcgrp;
625 int i;
626
627 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
628 genlmsg_attrlen(gnlh, 0), NULL);
629 if (!tb[CTRL_ATTR_MCAST_GROUPS])
630 return NL_SKIP;
631
632 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
633 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
634 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
635 nla_len(mcgrp), NULL);
636 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
637 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
638 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
639 res->group,
640 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
641 continue;
642 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
643 break;
644 };
645
646 return NL_SKIP;
647}
648
649
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800650static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651 const char *family, const char *group)
652{
653 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800654 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655 struct family_data res = { group, -ENOENT };
656
657 msg = nlmsg_alloc();
658 if (!msg)
659 return -ENOMEM;
Hai Shalomc1a21442022-02-04 13:43:00 -0800660 if (!genlmsg_put(msg, 0, 0, global->nlctrl_id,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800661 0, 0, CTRL_CMD_GETFAMILY, 0) ||
662 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
663 nlmsg_free(msg);
664 return -1;
665 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000667 ret = send_and_recv_glb(global, NULL, global->nl, msg, family_handler,
668 &res, NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 if (ret == 0)
670 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 return ret;
672}
673
674
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800675void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
676 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800677{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700678 if (TEST_FAIL())
679 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800680 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
681 0, flags, cmd, 0);
682}
683
684
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800685static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
686{
687 if (bss->wdev_id_set)
688 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
689 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
690}
691
692
693struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
694{
695 struct nl_msg *msg;
696
697 msg = nlmsg_alloc();
698 if (!msg)
699 return NULL;
700
701 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
702 nl80211_set_iface_id(msg, bss) < 0) {
703 nlmsg_free(msg);
704 return NULL;
705 }
706
707 return msg;
708}
709
710
711static struct nl_msg *
Hai Shalomc1a21442022-02-04 13:43:00 -0800712nl80211_ifindex_msg_build(struct wpa_driver_nl80211_data *drv,
713 struct nl_msg *msg, int ifindex, int flags,
714 uint8_t cmd)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800715{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800716 if (!msg)
717 return NULL;
718
719 if (!nl80211_cmd(drv, msg, flags, cmd) ||
720 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
721 nlmsg_free(msg);
722 return NULL;
723 }
724
725 return msg;
726}
727
728
Hai Shalomc1a21442022-02-04 13:43:00 -0800729static struct nl_msg *
730nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
731 int flags, uint8_t cmd)
732{
733 return nl80211_ifindex_msg_build(drv, nlmsg_alloc(), ifindex, flags,
734 cmd);
735}
736
737
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800738struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
739 uint8_t cmd)
740{
741 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
742}
743
744
745struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
746{
747 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
748}
749
750
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800751struct wiphy_idx_data {
752 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700753 enum nl80211_iftype nlmode;
754 u8 *macaddr;
Hai Shalom60840252021-02-19 19:02:11 -0800755 u8 use_4addr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800756};
757
758
759static int netdev_info_handler(struct nl_msg *msg, void *arg)
760{
761 struct nlattr *tb[NL80211_ATTR_MAX + 1];
762 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
763 struct wiphy_idx_data *info = arg;
764
765 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
766 genlmsg_attrlen(gnlh, 0), NULL);
767
768 if (tb[NL80211_ATTR_WIPHY])
769 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
770
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700771 if (tb[NL80211_ATTR_IFTYPE])
772 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
773
774 if (tb[NL80211_ATTR_MAC] && info->macaddr)
775 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
776 ETH_ALEN);
777
Hai Shalom60840252021-02-19 19:02:11 -0800778 if (tb[NL80211_ATTR_4ADDR])
779 info->use_4addr = nla_get_u8(tb[NL80211_ATTR_4ADDR]);
780
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800781 return NL_SKIP;
782}
783
784
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800785int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800786{
787 struct nl_msg *msg;
788 struct wiphy_idx_data data = {
789 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700790 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800791 };
792
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800793 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
794 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800795
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000796 if (send_and_recv_resp(bss->drv, msg, netdev_info_handler, &data) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800797 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798 return -1;
799}
800
801
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700802static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
803{
804 struct nl_msg *msg;
805 struct wiphy_idx_data data = {
806 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
807 .macaddr = NULL,
808 };
809
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800810 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
811 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700812
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000813 if (send_and_recv_resp(bss->drv, msg, netdev_info_handler, &data) == 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700814 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700815 return NL80211_IFTYPE_UNSPECIFIED;
816}
817
818
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700819static int nl80211_get_macaddr(struct i802_bss *bss)
820{
821 struct nl_msg *msg;
822 struct wiphy_idx_data data = {
823 .macaddr = bss->addr,
824 };
825
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800826 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
827 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700828
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000829 return send_and_recv_resp(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700830}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700831
832
Hai Shalom60840252021-02-19 19:02:11 -0800833static int nl80211_get_4addr(struct i802_bss *bss)
834{
835 struct nl_msg *msg;
836 struct wiphy_idx_data data = {
837 .use_4addr = 0,
838 };
839
840 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000841 send_and_recv_resp(bss->drv, msg, netdev_info_handler, &data))
Hai Shalom60840252021-02-19 19:02:11 -0800842 return -1;
843 return data.use_4addr;
844}
845
846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800847static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
848 struct nl80211_wiphy_data *w)
849{
850 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800851 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800852
853 msg = nlmsg_alloc();
854 if (!msg)
855 return -1;
856
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800857 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
858 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
859 nlmsg_free(msg);
860 return -1;
861 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000863 ret = send_and_recv(drv, w->nl_beacons, msg, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000864 NULL, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800865 if (ret) {
866 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
867 "failed: ret=%d (%s)",
868 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800869 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800870 return ret;
871}
872
873
874static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
875{
876 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700877 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800878
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800879 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800880
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700881 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700882 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700883 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
884 __func__, res);
885 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800886}
887
888
889static int process_beacon_event(struct nl_msg *msg, void *arg)
890{
891 struct nl80211_wiphy_data *w = arg;
892 struct wpa_driver_nl80211_data *drv;
893 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
894 struct nlattr *tb[NL80211_ATTR_MAX + 1];
895 union wpa_event_data event;
896
897 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
898 genlmsg_attrlen(gnlh, 0), NULL);
899
900 if (gnlh->cmd != NL80211_CMD_FRAME) {
901 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
902 gnlh->cmd);
903 return NL_SKIP;
904 }
905
906 if (!tb[NL80211_ATTR_FRAME])
907 return NL_SKIP;
908
909 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
910 wiphy_list) {
911 os_memset(&event, 0, sizeof(event));
912 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
913 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
914 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
915 }
916
917 return NL_SKIP;
918}
919
920
921static struct nl80211_wiphy_data *
922nl80211_get_wiphy_data_ap(struct i802_bss *bss)
923{
924 static DEFINE_DL_LIST(nl80211_wiphys);
925 struct nl80211_wiphy_data *w;
926 int wiphy_idx, found = 0;
927 struct i802_bss *tmp_bss;
Paul Stewart092955c2017-02-06 09:13:09 -0800928 u8 channel;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800929
930 if (bss->wiphy_data != NULL)
931 return bss->wiphy_data;
932
933 wiphy_idx = nl80211_get_wiphy_index(bss);
934
935 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
936 if (w->wiphy_idx == wiphy_idx)
937 goto add;
938 }
939
940 /* alloc new one */
941 w = os_zalloc(sizeof(*w));
942 if (w == NULL)
943 return NULL;
944 w->wiphy_idx = wiphy_idx;
945 dl_list_init(&w->bsss);
946 dl_list_init(&w->drvs);
947
Paul Stewart092955c2017-02-06 09:13:09 -0800948 /* Beacon frames not supported in IEEE 802.11ad */
Sunil Ravi036cec52023-03-29 11:35:17 -0700949 if (ieee80211_freq_to_chan(bss->flink->freq, &channel) !=
Paul Stewart092955c2017-02-06 09:13:09 -0800950 HOSTAPD_MODE_IEEE80211AD) {
951 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
952 if (!w->nl_cb) {
953 os_free(w);
954 return NULL;
955 }
956 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
957 no_seq_check, NULL);
958 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
959 process_beacon_event, w);
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000960
Paul Stewart092955c2017-02-06 09:13:09 -0800961 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
962 "wiphy beacons");
963 if (w->nl_beacons == NULL) {
964 os_free(w);
965 return NULL;
966 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000967
Paul Stewart092955c2017-02-06 09:13:09 -0800968 if (nl80211_register_beacons(bss->drv, w)) {
969 nl_destroy_handles(&w->nl_beacons);
970 os_free(w);
971 return NULL;
972 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000973
Paul Stewart092955c2017-02-06 09:13:09 -0800974 nl80211_register_eloop_read(&w->nl_beacons,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700975 nl80211_recv_beacons, w, 0);
Paul Stewart092955c2017-02-06 09:13:09 -0800976 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977
978 dl_list_add(&nl80211_wiphys, &w->list);
979
980add:
981 /* drv entry for this bss already there? */
982 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
983 if (tmp_bss->drv == bss->drv) {
984 found = 1;
985 break;
986 }
987 }
988 /* if not add it */
989 if (!found)
990 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
991
992 dl_list_add(&w->bsss, &bss->wiphy_list);
993 bss->wiphy_data = w;
994 return w;
995}
996
997
998static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
999{
1000 struct nl80211_wiphy_data *w = bss->wiphy_data;
1001 struct i802_bss *tmp_bss;
1002 int found = 0;
1003
1004 if (w == NULL)
1005 return;
1006 bss->wiphy_data = NULL;
1007 dl_list_del(&bss->wiphy_list);
1008
1009 /* still any for this drv present? */
1010 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1011 if (tmp_bss->drv == bss->drv) {
1012 found = 1;
1013 break;
1014 }
1015 }
1016 /* if not remove it */
1017 if (!found)
1018 dl_list_del(&bss->drv->wiphy_list);
1019
1020 if (!dl_list_empty(&w->bsss))
1021 return;
1022
Paul Stewart092955c2017-02-06 09:13:09 -08001023 if (w->nl_beacons)
Roshan Pius3a1667e2018-07-03 15:17:14 -07001024 nl80211_destroy_eloop_handle(&w->nl_beacons, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001025
1026 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001027 dl_list_del(&w->list);
1028 os_free(w);
1029}
1030
1031
Dmitry Shmidte4663042016-04-04 10:07:49 -07001032static unsigned int nl80211_get_ifindex(void *priv)
1033{
1034 struct i802_bss *bss = priv;
1035 struct wpa_driver_nl80211_data *drv = bss->drv;
1036
1037 return drv->ifindex;
1038}
1039
1040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1042{
1043 struct i802_bss *bss = priv;
1044 struct wpa_driver_nl80211_data *drv = bss->drv;
1045 if (!drv->associated)
1046 return -1;
1047 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1048 return 0;
1049}
1050
1051
1052static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1053{
1054 struct i802_bss *bss = priv;
1055 struct wpa_driver_nl80211_data *drv = bss->drv;
1056 if (!drv->associated)
1057 return -1;
1058 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1059 return drv->ssid_len;
1060}
1061
1062
Sunil Ravi77d572f2023-01-17 23:58:31 +00001063static int get_mlo_info(struct nl_msg *msg, void *arg)
1064{
1065 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1066 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1067 struct nlattr *link_attr, *link_data[NL80211_ATTR_MAX + 1];
1068 static struct nla_policy link_policy[NL80211_ATTR_MAX + 1] = {
1069 [NL80211_ATTR_MLO_LINK_ID] = { .type = NLA_U8 },
1070 [NL80211_ATTR_MAC] = { .minlen = ETH_ALEN, .maxlen = ETH_ALEN },
1071 };
1072 struct driver_sta_mlo_info *info = arg;
1073 int rem;
1074
1075 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1076 genlmsg_attrlen(gnlh, 0), NULL);
1077
1078 if (!tb[NL80211_ATTR_MLO_LINKS])
1079 return NL_SKIP;
1080
1081 info->valid_links = 0;
1082 nla_for_each_nested(link_attr, tb[NL80211_ATTR_MLO_LINKS], rem) {
1083 u8 link_id;
1084
1085 if (nla_parse_nested(link_data, NL80211_ATTR_MAX,
1086 link_attr, link_policy) != 0)
1087 continue;
1088
1089 if (!link_data[NL80211_ATTR_MLO_LINK_ID] ||
1090 !link_data[NL80211_ATTR_MAC])
1091 continue;
1092
1093 link_id = nla_get_u8(link_data[NL80211_ATTR_MLO_LINK_ID]);
1094 if (link_id >= MAX_NUM_MLD_LINKS)
1095 continue;
1096 info->valid_links |= BIT(link_id);
1097 os_memcpy(info->links[link_id].addr,
1098 nla_data(link_data[NL80211_ATTR_MAC]), ETH_ALEN);
1099 if (link_data[NL80211_ATTR_WIPHY_FREQ])
1100 info->links[link_id].freq =
1101 nla_get_u32(link_data[NL80211_ATTR_WIPHY_FREQ]);
1102 }
1103
1104 return NL_SKIP;
1105}
1106
1107
Sunil Ravi89eba102022-09-13 21:04:37 -07001108static int nl80211_get_sta_mlo_info(void *priv,
1109 struct driver_sta_mlo_info *mlo_info)
1110{
1111 struct i802_bss *bss = priv;
1112 struct wpa_driver_nl80211_data *drv = bss->drv;
1113
1114 if (!drv->associated)
1115 return -1;
1116
Sunil Ravi77d572f2023-01-17 23:58:31 +00001117 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1118 struct nl_msg *msg;
1119
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001120 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001121 if (send_and_recv_resp(drv, msg, get_mlo_info,
1122 &drv->sta_mlo_info))
Sunil Ravi77d572f2023-01-17 23:58:31 +00001123 return -1;
1124 }
1125
Sunil Ravi89eba102022-09-13 21:04:37 -07001126 os_memcpy(mlo_info, &drv->sta_mlo_info, sizeof(*mlo_info));
1127 return 0;
1128}
1129
1130
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001131static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidte4663042016-04-04 10:07:49 -07001132 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
1133 int ifindex, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134{
1135 union wpa_event_data event;
1136
Dmitry Shmidte4663042016-04-04 10:07:49 -07001137 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001138 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1139 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1140 drv->first_bss->ifname);
1141 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001142 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001143 if (!drv->if_removed)
1144 return;
1145 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1146 drv->first_bss->ifname);
1147 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 }
1149
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001150 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -07001151 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001152 os_strlcpy(event.interface_status.ifname, ifname,
1153 sizeof(event.interface_status.ifname));
1154 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001155 if (drv)
1156 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1157 else
1158 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
1159 &event);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001160}
1161
1162
1163static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidte4663042016-04-04 10:07:49 -07001164 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
1165 int ifindex, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001166{
1167 union wpa_event_data event;
1168
Dmitry Shmidte4663042016-04-04 10:07:49 -07001169 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001170 if (drv->if_removed) {
1171 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1172 ifname);
1173 return;
1174 }
1175 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1176 ifname);
1177 drv->if_removed = 1;
1178 } else {
1179 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1180 ifname);
1181 }
1182
1183 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -07001184 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001185 os_strlcpy(event.interface_status.ifname, ifname,
1186 sizeof(event.interface_status.ifname));
1187 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001188 if (drv)
1189 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1190 else
1191 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
1192 &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193}
1194
1195
1196static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1197 u8 *buf, size_t len)
1198{
1199 int attrlen, rta_len;
1200 struct rtattr *attr;
1201
1202 attrlen = len;
1203 attr = (struct rtattr *) buf;
1204
1205 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1206 while (RTA_OK(attr, attrlen)) {
1207 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001208 if (os_strcmp(((char *) attr) + rta_len,
1209 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 return 1;
1211 else
1212 break;
1213 }
1214 attr = RTA_NEXT(attr, attrlen);
1215 }
1216
1217 return 0;
1218}
1219
1220
1221static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1222 int ifindex, u8 *buf, size_t len)
1223{
1224 if (drv->ifindex == ifindex)
1225 return 1;
1226
1227 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001228 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1230 "interface");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001231 if (wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL,
1232 WPA_P2P_MODE_WFD_R1) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001233 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 return 1;
1235 }
1236
1237 return 0;
1238}
1239
1240
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001241static struct wpa_driver_nl80211_data *
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001242nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len,
1243 int *init_failed)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001244{
1245 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001246 int res;
1247
1248 if (init_failed)
1249 *init_failed = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001250 dl_list_for_each(drv, &global->interfaces,
1251 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001252 res = wpa_driver_nl80211_own_ifindex(drv, idx, buf, len);
1253 if (res < 0) {
1254 wpa_printf(MSG_DEBUG,
1255 "nl80211: Found matching own interface, but failed to complete reinitialization");
1256 if (init_failed)
1257 *init_failed = 1;
1258 return drv;
1259 }
1260 if (res > 0 || have_ifidx(drv, idx, IFIDX_ANY))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001261 return drv;
1262 }
1263 return NULL;
1264}
1265
1266
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001267static void nl80211_refresh_mac(struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001268 int ifindex, int notify)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001269{
1270 struct i802_bss *bss;
1271 u8 addr[ETH_ALEN];
1272
1273 bss = get_bss_ifindex(drv, ifindex);
1274 if (bss &&
1275 linux_get_ifhwaddr(drv->global->ioctl_sock,
1276 bss->ifname, addr) < 0) {
1277 wpa_printf(MSG_DEBUG,
1278 "nl80211: %s: failed to re-read MAC address",
1279 bss->ifname);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001280 } else if (bss && !ether_addr_equal(addr, bss->addr)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001281 wpa_printf(MSG_DEBUG,
1282 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1283 MACSTR " to " MACSTR,
1284 ifindex, bss->ifname,
1285 MAC2STR(bss->addr), MAC2STR(addr));
Sunil Ravi77d572f2023-01-17 23:58:31 +00001286 os_memcpy(bss->prev_addr, bss->addr, ETH_ALEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001287 os_memcpy(bss->addr, addr, ETH_ALEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001288 if (notify)
1289 wpa_supplicant_event(drv->ctx,
1290 EVENT_INTERFACE_MAC_CHANGED, NULL);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001291 }
1292}
1293
1294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1296 struct ifinfomsg *ifi,
1297 u8 *buf, size_t len)
1298{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001299 struct nl80211_global *global = ctx;
1300 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001301 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 struct rtattr *attr;
1303 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001304 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001305 char ifname[IFNAMSIZ + 1];
1306 char extra[100], *pos, *end;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001307 int init_failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001309 extra[0] = '\0';
1310 pos = extra;
1311 end = pos + sizeof(extra);
1312 ifname[0] = '\0';
1313
1314 attrlen = len;
1315 attr = (struct rtattr *) buf;
1316 while (RTA_OK(attr, attrlen)) {
1317 switch (attr->rta_type) {
1318 case IFLA_IFNAME:
Hai Shalomfdcde762020-04-02 11:19:20 -07001319 if (RTA_PAYLOAD(attr) > IFNAMSIZ)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001320 break;
1321 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1322 ifname[RTA_PAYLOAD(attr)] = '\0';
1323 break;
1324 case IFLA_MASTER:
1325 brid = nla_get_u32((struct nlattr *) attr);
1326 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1327 break;
1328 case IFLA_WIRELESS:
1329 pos += os_snprintf(pos, end - pos, " wext");
1330 break;
1331 case IFLA_OPERSTATE:
1332 pos += os_snprintf(pos, end - pos, " operstate=%u",
1333 nla_get_u32((struct nlattr *) attr));
1334 break;
1335 case IFLA_LINKMODE:
1336 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1337 nla_get_u32((struct nlattr *) attr));
1338 break;
1339 }
1340 attr = RTA_NEXT(attr, attrlen);
1341 }
1342 extra[sizeof(extra) - 1] = '\0';
1343
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001344 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1345 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1346 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1348 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1349 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1350 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1351
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001352 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, &init_failed);
Dmitry Shmidte4663042016-04-04 10:07:49 -07001353 if (!drv)
1354 goto event_newlink;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001355 if (init_failed)
1356 return; /* do not update interface state */
Dmitry Shmidte4663042016-04-04 10:07:49 -07001357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001359 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001360 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001361 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001362 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1363 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001364 drv->ignore_if_down_event = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001365 /* Re-read MAC address as it may have changed */
1366 nl80211_refresh_mac(drv, ifi->ifi_index, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001367 return;
1368 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001369 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
1370 namebuf, ifname);
1371 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1372 wpa_printf(MSG_DEBUG,
1373 "nl80211: Not the main interface (%s) - do not indicate interface down",
1374 drv->first_bss->ifname);
1375 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001376 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1377 "event generated by mode change");
1378 drv->ignore_if_down_event = 0;
1379 } else {
1380 drv->if_disabled = 1;
1381 wpa_supplicant_event(drv->ctx,
1382 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001383
1384 /*
1385 * Try to get drv again, since it may be removed as
1386 * part of the EVENT_INTERFACE_DISABLED handling for
1387 * dynamic interfaces
1388 */
1389 drv = nl80211_find_drv(global, ifi->ifi_index,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001390 buf, len, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001391 if (!drv)
1392 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001393 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 }
1395
1396 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07001397 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001398 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001399 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001400 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1401 "event since interface %s is down",
1402 namebuf);
Hai Shalomc9e41a12018-07-31 14:41:42 -07001403 return;
1404 }
1405 wpa_printf(MSG_DEBUG, "nl80211: Interface up (%s/%s)",
1406 namebuf, ifname);
1407 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1408 wpa_printf(MSG_DEBUG,
1409 "nl80211: Not the main interface (%s) - do not indicate interface up",
1410 drv->first_bss->ifname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001411 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001412 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1413 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001414 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001415 } else if (drv->if_removed) {
1416 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1417 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001418 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001419 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001420 /* Re-read MAC address as it may have changed */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001421 nl80211_refresh_mac(drv, ifi->ifi_index, 0);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001422
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001423 drv->if_disabled = 0;
1424 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1425 NULL);
1426 }
Sunil Ravi90775442020-09-24 11:53:19 -07001427 } else if (ifi->ifi_flags & IFF_UP) {
1428 /* Re-read MAC address as it may have changed */
1429 nl80211_refresh_mac(drv, ifi->ifi_index, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 }
1431
1432 /*
1433 * Some drivers send the association event before the operup event--in
1434 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1435 * fails. This will hit us when wpa_supplicant does not need to do
1436 * IEEE 802.1X authentication
1437 */
1438 if (drv->operstate == 1 &&
1439 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001440 !(ifi->ifi_flags & IFF_RUNNING)) {
1441 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001442 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444 }
1445
Dmitry Shmidte4663042016-04-04 10:07:49 -07001446event_newlink:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001447 if (ifname[0])
Dmitry Shmidte4663042016-04-04 10:07:49 -07001448 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1449 ifname);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001450
Dmitry Shmidte4663042016-04-04 10:07:49 -07001451 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001452 struct i802_bss *bss;
1453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001455 if (!if_indextoname(brid, namebuf)) {
1456 wpa_printf(MSG_DEBUG,
1457 "nl80211: Could not find bridge ifname for ifindex %u",
1458 brid);
1459 return;
1460 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1462 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001463 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001464
1465 for (bss = drv->first_bss; bss; bss = bss->next) {
1466 if (os_strcmp(ifname, bss->ifname) == 0) {
1467 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1468 break;
1469 }
1470 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472}
1473
1474
1475static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1476 struct ifinfomsg *ifi,
1477 u8 *buf, size_t len)
1478{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001479 struct nl80211_global *global = ctx;
1480 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001481 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482 struct rtattr *attr;
1483 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001484 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001485 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001487 extra[0] = '\0';
1488 pos = extra;
1489 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001490 ifname[0] = '\0';
1491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492 attrlen = len;
1493 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001495 switch (attr->rta_type) {
1496 case IFLA_IFNAME:
Hai Shalomfdcde762020-04-02 11:19:20 -07001497 if (RTA_PAYLOAD(attr) > IFNAMSIZ)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001498 break;
1499 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1500 ifname[RTA_PAYLOAD(attr)] = '\0';
1501 break;
1502 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001504 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1505 break;
1506 case IFLA_OPERSTATE:
1507 pos += os_snprintf(pos, end - pos, " operstate=%u",
1508 nla_get_u32((struct nlattr *) attr));
1509 break;
1510 case IFLA_LINKMODE:
1511 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1512 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001513 break;
1514 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515 attr = RTA_NEXT(attr, attrlen);
1516 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001517 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001519 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1520 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1521 ifi->ifi_flags,
1522 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1523 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1524 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1525 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1526
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001527 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001528
Dmitry Shmidte4663042016-04-04 10:07:49 -07001529 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530 /* device has been removed from bridge */
1531 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001532
1533 if (!if_indextoname(brid, namebuf)) {
1534 wpa_printf(MSG_DEBUG,
1535 "nl80211: Could not find bridge ifname for ifindex %u",
1536 brid);
1537 } else {
1538 wpa_printf(MSG_DEBUG,
1539 "nl80211: Remove ifindex %u for bridge %s",
1540 brid, namebuf);
1541 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001542 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001543 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001544
1545 if (ifi->ifi_family != AF_BRIDGE || !brid)
1546 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1547 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548}
1549
1550
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001551struct nl80211_get_assoc_freq_arg {
1552 struct wpa_driver_nl80211_data *drv;
1553 unsigned int assoc_freq;
1554 unsigned int ibss_freq;
1555 u8 assoc_bssid[ETH_ALEN];
1556 u8 assoc_ssid[SSID_MAX_LEN];
1557 u8 assoc_ssid_len;
Sunil Ravi89eba102022-09-13 21:04:37 -07001558 u8 bssid[MAX_NUM_MLD_LINKS][ETH_ALEN];
1559 unsigned int freq[MAX_NUM_MLD_LINKS];
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001560};
1561
1562static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1563{
1564 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1565 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1566 struct nlattr *bss[NL80211_BSS_MAX + 1];
1567 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1568 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1569 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1570 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1571 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
Sunil Ravi89eba102022-09-13 21:04:37 -07001572 [NL80211_BSS_MLO_LINK_ID] = { .type = NLA_U8 },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001573 };
1574 struct nl80211_get_assoc_freq_arg *ctx = arg;
1575 enum nl80211_bss_status status;
Sunil Ravi89eba102022-09-13 21:04:37 -07001576 struct wpa_driver_nl80211_data *drv = ctx->drv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001577
1578 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1579 genlmsg_attrlen(gnlh, 0), NULL);
1580 if (!tb[NL80211_ATTR_BSS] ||
1581 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1582 bss_policy) ||
1583 !bss[NL80211_BSS_STATUS])
1584 return NL_SKIP;
1585
1586 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1587 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1588 bss[NL80211_BSS_FREQUENCY]) {
Sunil Ravi89eba102022-09-13 21:04:37 -07001589 int link_id = -1;
1590 u32 freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1591
1592 if (bss[NL80211_BSS_MLO_LINK_ID])
1593 link_id = nla_get_u8(bss[NL80211_BSS_MLO_LINK_ID]);
1594
1595 if (link_id >= 0 && link_id < MAX_NUM_MLD_LINKS) {
1596 ctx->freq[link_id] = freq;
1597 wpa_printf(MSG_DEBUG,
1598 "nl80211: MLO link %d associated on %u MHz",
1599 link_id, ctx->freq[link_id]);
1600 }
1601
1602 if (!drv->sta_mlo_info.valid_links ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00001603 drv->sta_mlo_info.assoc_link_id == link_id) {
Sunil Ravi89eba102022-09-13 21:04:37 -07001604 ctx->assoc_freq = freq;
1605 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1606 ctx->assoc_freq);
1607 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001608 }
1609 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1610 bss[NL80211_BSS_FREQUENCY]) {
1611 ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1612 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1613 ctx->ibss_freq);
1614 }
1615 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1616 bss[NL80211_BSS_BSSID]) {
Sunil Ravi89eba102022-09-13 21:04:37 -07001617 int link_id = -1;
1618 const u8 *bssid = nla_data(bss[NL80211_BSS_BSSID]);
1619
1620 if (bss[NL80211_BSS_MLO_LINK_ID])
1621 link_id = nla_get_u8(bss[NL80211_BSS_MLO_LINK_ID]);
1622
1623 if (link_id >= 0 && link_id < MAX_NUM_MLD_LINKS) {
1624 os_memcpy(ctx->bssid[link_id], bssid, ETH_ALEN);
1625 wpa_printf(MSG_DEBUG,
1626 "nl80211: MLO link %d associated with "
1627 MACSTR, link_id, MAC2STR(bssid));
1628 }
1629
1630 if (!drv->sta_mlo_info.valid_links ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00001631 drv->sta_mlo_info.assoc_link_id == link_id) {
Sunil Ravi89eba102022-09-13 21:04:37 -07001632 os_memcpy(ctx->assoc_bssid, bssid, ETH_ALEN);
1633 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1634 MACSTR, MAC2STR(bssid));
1635 }
1636
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001637 }
1638
1639 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1640 bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1641 const u8 *ie, *ssid;
1642 size_t ie_len;
1643
1644 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1645 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1646 ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1647 if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1648 ctx->assoc_ssid_len = ssid[1];
1649 os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1650 }
1651 }
1652
1653 return NL_SKIP;
1654}
1655
1656
1657int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001658{
1659 struct nl_msg *msg;
1660 int ret;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001661 struct nl80211_get_assoc_freq_arg arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07001662 int count = 0;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001663
Hai Shalomfdcde762020-04-02 11:19:20 -07001664try_again:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001665 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001666 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001667 arg.drv = drv;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001668 ret = send_and_recv_resp(drv, msg, nl80211_get_assoc_freq_handler,
1669 &arg);
Hai Shalomfdcde762020-04-02 11:19:20 -07001670 if (ret == -EAGAIN) {
1671 count++;
1672 if (count >= 10) {
1673 wpa_printf(MSG_INFO,
1674 "nl80211: Failed to receive consistent scan result dump for get_assoc_ssid");
1675 } else {
1676 wpa_printf(MSG_DEBUG,
1677 "nl80211: Failed to receive consistent scan result dump for get_assoc_ssid - try again");
1678 goto try_again;
1679 }
1680 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001681 if (ret == 0) {
1682 os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1683 return arg.assoc_ssid_len;
1684 }
1685 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1686 ret, strerror(-ret));
1687 return ret;
1688}
1689
1690
1691unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1692{
1693 struct nl_msg *msg;
1694 int ret;
1695 struct nl80211_get_assoc_freq_arg arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07001696 int count = 0;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001697
Hai Shalomfdcde762020-04-02 11:19:20 -07001698try_again:
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001699 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1700 os_memset(&arg, 0, sizeof(arg));
1701 arg.drv = drv;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001702 ret = send_and_recv_resp(drv, msg, nl80211_get_assoc_freq_handler,
1703 &arg);
Hai Shalomfdcde762020-04-02 11:19:20 -07001704 if (ret == -EAGAIN) {
1705 count++;
1706 if (count >= 10) {
1707 wpa_printf(MSG_INFO,
1708 "nl80211: Failed to receive consistent scan result dump for get_assoc_freq");
1709 } else {
1710 wpa_printf(MSG_DEBUG,
1711 "nl80211: Failed to receive consistent scan result dump for get_assoc_freq - try again");
1712 goto try_again;
1713 }
1714 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03001715 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001716 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1717 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001718 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001719 "associated BSS from scan results: %u MHz", freq);
1720 if (freq)
1721 drv->assoc_freq = freq;
Sunil Ravi89eba102022-09-13 21:04:37 -07001722
1723 if (drv->sta_mlo_info.valid_links) {
1724 int i;
1725
1726 for (i = 0; i < MAX_NUM_MLD_LINKS; i++)
1727 drv->sta_mlo_info.links[i].freq = arg.freq[i];
1728 }
1729
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001730 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001731 }
1732 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1733 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001734 return drv->assoc_freq;
1735}
1736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737static int get_link_noise(struct nl_msg *msg, void *arg)
1738{
1739 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1740 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1741 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1742 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1743 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1744 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1745 };
1746 struct wpa_signal_info *sig_change = arg;
1747
1748 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1749 genlmsg_attrlen(gnlh, 0), NULL);
1750
1751 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1752 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1753 return NL_SKIP;
1754 }
1755
1756 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1757 tb[NL80211_ATTR_SURVEY_INFO],
1758 survey_policy)) {
1759 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1760 "attributes!");
1761 return NL_SKIP;
1762 }
1763
1764 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1765 return NL_SKIP;
1766
1767 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1768 sig_change->frequency)
1769 return NL_SKIP;
1770
1771 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1772 return NL_SKIP;
1773
1774 sig_change->current_noise =
1775 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1776
1777 return NL_SKIP;
1778}
1779
1780
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001781int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1782 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783{
1784 struct nl_msg *msg;
1785
Hai Shalom74f70d42019-02-11 14:42:39 -08001786 sig_change->current_noise = WPA_INVALID_NOISE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787 sig_change->frequency = drv->assoc_freq;
1788
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001789 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001790 return send_and_recv_resp(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791}
1792
1793
Hai Shalom74f70d42019-02-11 14:42:39 -08001794static int get_channel_info(struct nl_msg *msg, void *arg)
1795{
1796 struct nlattr *tb[NL80211_ATTR_MAX + 1] = { 0 };
1797 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1798 struct wpa_channel_info *chan_info = arg;
1799
1800 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1801 genlmsg_attrlen(gnlh, 0), NULL);
1802
1803 os_memset(chan_info, 0, sizeof(struct wpa_channel_info));
1804 chan_info->chanwidth = CHAN_WIDTH_UNKNOWN;
1805
1806 if (tb[NL80211_ATTR_WIPHY_FREQ])
1807 chan_info->frequency =
1808 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1809 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
1810 chan_info->chanwidth = convert2width(
1811 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
1812 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1813 enum nl80211_channel_type ct =
1814 nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1815
1816 switch (ct) {
1817 case NL80211_CHAN_HT40MINUS:
1818 chan_info->sec_channel = -1;
1819 break;
1820 case NL80211_CHAN_HT40PLUS:
1821 chan_info->sec_channel = 1;
1822 break;
1823 default:
1824 chan_info->sec_channel = 0;
1825 break;
1826 }
1827 }
1828 if (tb[NL80211_ATTR_CENTER_FREQ1])
1829 chan_info->center_frq1 =
1830 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
1831 if (tb[NL80211_ATTR_CENTER_FREQ2])
1832 chan_info->center_frq2 =
1833 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
1834
1835 if (chan_info->center_frq2) {
1836 u8 seg1_idx = 0;
1837
1838 if (ieee80211_freq_to_chan(chan_info->center_frq2, &seg1_idx) !=
1839 NUM_HOSTAPD_MODES)
1840 chan_info->seg1_idx = seg1_idx;
1841 }
1842
1843 return NL_SKIP;
1844}
1845
1846
1847static int nl80211_channel_info(void *priv, struct wpa_channel_info *ci)
1848{
1849 struct i802_bss *bss = priv;
1850 struct wpa_driver_nl80211_data *drv = bss->drv;
1851 struct nl_msg *msg;
1852
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001853 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001854 return send_and_recv_resp(drv, msg, get_channel_info, ci);
Hai Shalom74f70d42019-02-11 14:42:39 -08001855}
1856
1857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001858static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1859 void *handle)
1860{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001861 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001862 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001863
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001864 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001866 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001867 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001868 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1869 __func__, res);
1870 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871}
1872
1873
1874/**
1875 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1876 * @priv: driver_nl80211 private data
1877 * @alpha2_arg: country to which to switch to
1878 * Returns: 0 on success, -1 on failure
1879 *
1880 * This asks nl80211 to set the regulatory domain for given
1881 * country ISO / IEC alpha2.
1882 */
1883static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1884{
1885 struct i802_bss *bss = priv;
1886 struct wpa_driver_nl80211_data *drv = bss->drv;
1887 char alpha2[3];
1888 struct nl_msg *msg;
1889
1890 msg = nlmsg_alloc();
1891 if (!msg)
1892 return -ENOMEM;
1893
1894 alpha2[0] = alpha2_arg[0];
1895 alpha2[1] = alpha2_arg[1];
1896 alpha2[2] = '\0';
1897
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001898 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1899 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1900 nlmsg_free(msg);
1901 return -EINVAL;
1902 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001903 if (send_and_recv_cmd(drv, msg))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 return -EINVAL;
1905 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906}
1907
1908
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001909static int nl80211_get_country(struct nl_msg *msg, void *arg)
1910{
1911 char *alpha2 = arg;
1912 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1913 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1914
1915 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1916 genlmsg_attrlen(gnlh, 0), NULL);
1917 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1918 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1919 return NL_SKIP;
1920 }
1921 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1922 return NL_SKIP;
1923}
1924
1925
1926static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1927{
1928 struct i802_bss *bss = priv;
1929 struct wpa_driver_nl80211_data *drv = bss->drv;
1930 struct nl_msg *msg;
1931 int ret;
1932
1933 msg = nlmsg_alloc();
1934 if (!msg)
1935 return -ENOMEM;
1936
1937 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Sunil Ravi036cec52023-03-29 11:35:17 -07001938
1939 if (drv->capa.flags & WPA_DRIVER_FLAGS_SELF_MANAGED_REGULATORY) {
1940 /* put wiphy idx to get the interface specific country code
1941 * instead of the global one. */
1942 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, drv->wiphy_idx)) {
1943 nlmsg_free(msg);
1944 return -1;
1945 }
1946 }
1947
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001948 alpha2[0] = '\0';
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001949 ret = send_and_recv_resp(drv, msg, nl80211_get_country, alpha2);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001950 if (!alpha2[0])
1951 ret = -1;
1952
1953 return ret;
1954}
1955
1956
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001957static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001958{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001959 struct nl_cache *cache = NULL;
1960 struct genl_family *family = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 int ret;
1962
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001963 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1964 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1966 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001967 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001968 }
1969
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001970 global->nl = nl_create_handle(global->nl_cb, "nl");
1971 if (global->nl == NULL)
1972 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001974 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1975 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1977 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001978 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 }
1980
Hai Shalomc1a21442022-02-04 13:43:00 -08001981 global->nlctrl_id = genl_ctrl_resolve(global->nl, "nlctrl");
1982 if (global->nlctrl_id < 0) {
1983 wpa_printf(MSG_ERROR,
1984 "nl80211: 'nlctrl' generic netlink not found");
1985 goto err;
1986 }
1987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001988 global->nl_event = nl_create_handle(global->nl_cb, "event");
1989 if (global->nl_event == NULL)
1990 goto err;
1991
1992 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001994 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995 if (ret < 0) {
1996 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1997 "membership for scan events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001998 ret, nl_geterror(ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001999 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002000 }
2001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002002 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002004 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 if (ret < 0) {
2006 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2007 "membership for mlme events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07002008 ret, nl_geterror(ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002009 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002010 }
2011
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002012 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002014 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 if (ret < 0) {
2016 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2017 "membership for regulatory events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07002018 ret, nl_geterror(ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019 /* Continue without regulatory events */
2020 }
2021
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002022 ret = nl_get_multicast_id(global, "nl80211", "vendor");
2023 if (ret >= 0)
2024 ret = nl_socket_add_membership(global->nl_event, ret);
2025 if (ret < 0) {
2026 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2027 "membership for vendor events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07002028 ret, nl_geterror(ret));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002029 /* Continue without vendor events */
2030 }
2031
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002032 /* Resolve maxattr for kernel support checks */
2033 ret = genl_ctrl_alloc_cache(global->nl, &cache);
2034 if (ret < 0) {
2035 wpa_printf(MSG_DEBUG,
2036 "nl80211: Could not allocate genl cache: %d (%s)",
2037 ret, nl_geterror(ret));
2038 goto err;
2039 }
2040
2041 family = genl_ctrl_search(cache, global->nl80211_id);
2042 if (!family) {
2043 wpa_printf(MSG_DEBUG,
2044 "nl80211: Could not get nl80211 family from cache: %d (%s)",
2045 ret, nl_geterror(ret));
2046 goto err;
2047 }
2048
2049 global->nl80211_maxattr = genl_family_get_maxattr(family);
2050 wpa_printf(MSG_DEBUG, "nl80211: Maximum supported attribute ID: %u",
2051 global->nl80211_maxattr);
2052 genl_family_put(family);
2053 nl_cache_free(cache);
2054
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002055 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2056 no_seq_check, NULL);
2057 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2058 process_global_event, global);
2059
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002060 nl80211_register_eloop_read(&global->nl_event,
2061 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002062 global->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063
2064 return 0;
2065
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002066err:
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002067 genl_family_put(family);
2068 nl_cache_free(cache);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002069 nl_destroy_handles(&global->nl_event);
2070 nl_destroy_handles(&global->nl);
2071 nl_cb_put(global->nl_cb);
2072 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 return -1;
2074}
2075
2076
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002077static void nl80211_check_global(struct nl80211_global *global)
2078{
Hai Shalomfdcde762020-04-02 11:19:20 -07002079 struct nl_sock *handle;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002080 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
2081 int ret;
2082 unsigned int i;
2083
2084 /*
2085 * Try to re-add memberships to handle case of cfg80211 getting reloaded
2086 * and all registration having been cleared.
2087 */
2088 handle = (void *) (((intptr_t) global->nl_event) ^
2089 ELOOP_SOCKET_INVALID);
2090
2091 for (i = 0; groups[i]; i++) {
2092 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
2093 if (ret >= 0)
2094 ret = nl_socket_add_membership(handle, ret);
2095 if (ret < 0) {
2096 wpa_printf(MSG_INFO,
2097 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07002098 groups[i], ret, nl_geterror(ret));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002099 }
2100 }
2101}
2102
2103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
2105{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002106 struct wpa_driver_nl80211_data *drv = ctx;
2107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002111 * rtnetlink ifdown handler will report interfaces other than the P2P
2112 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002114 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2115 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116}
2117
2118
2119static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
2120{
2121 struct wpa_driver_nl80211_data *drv = ctx;
2122 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002123 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
2125 "after rfkill unblock");
2126 return;
2127 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002128
2129 if (is_p2p_net_interface(drv->nlmode))
2130 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
2131
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002132 /*
2133 * rtnetlink ifup handler will report interfaces other than the P2P
2134 * Device interface as enabled.
2135 */
2136 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2137 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138}
2139
2140
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002141static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
2142 void *eloop_ctx,
2143 void *handle)
2144{
2145 struct wpa_driver_nl80211_data *drv = eloop_ctx;
2146 u8 data[2048];
2147 struct msghdr msg;
2148 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002149 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002150 struct cmsghdr *cmsg;
2151 int res, found_ee = 0, found_wifi = 0, acked = 0;
2152 union wpa_event_data event;
2153
2154 memset(&msg, 0, sizeof(msg));
2155 msg.msg_iov = &entry;
2156 msg.msg_iovlen = 1;
2157 entry.iov_base = data;
2158 entry.iov_len = sizeof(data);
2159 msg.msg_control = &control;
2160 msg.msg_controllen = sizeof(control);
2161
2162 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
2163 /* if error or not fitting 802.3 header, return */
2164 if (res < 14)
2165 return;
2166
2167 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
2168 {
2169 if (cmsg->cmsg_level == SOL_SOCKET &&
2170 cmsg->cmsg_type == SCM_WIFI_STATUS) {
2171 int *ack;
2172
2173 found_wifi = 1;
2174 ack = (void *)CMSG_DATA(cmsg);
2175 acked = *ack;
2176 }
2177
2178 if (cmsg->cmsg_level == SOL_PACKET &&
2179 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
2180 struct sock_extended_err *err =
2181 (struct sock_extended_err *)CMSG_DATA(cmsg);
2182
2183 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
2184 found_ee = 1;
2185 }
2186 }
2187
2188 if (!found_ee || !found_wifi)
2189 return;
2190
2191 memset(&event, 0, sizeof(event));
2192 event.eapol_tx_status.dst = data;
2193 event.eapol_tx_status.data = data + 14;
2194 event.eapol_tx_status.data_len = res - 14;
2195 event.eapol_tx_status.ack = acked;
2196 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
2197}
2198
2199
Hai Shalomb755a2a2020-04-23 21:49:02 -07002200static int nl80211_init_connect_handle(struct i802_bss *bss)
2201{
2202 if (bss->nl_connect) {
2203 wpa_printf(MSG_DEBUG,
2204 "nl80211: Connect handle already created (nl_connect=%p)",
2205 bss->nl_connect);
2206 return -1;
2207 }
2208
2209 bss->nl_connect = nl_create_handle(bss->nl_cb, "connect");
2210 if (!bss->nl_connect)
2211 return -1;
2212 nl80211_register_eloop_read(&bss->nl_connect,
2213 wpa_driver_nl80211_event_receive,
2214 bss->nl_cb, 1);
2215 return 0;
2216}
2217
2218
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002219static int nl80211_init_bss(struct i802_bss *bss)
2220{
2221 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2222 if (!bss->nl_cb)
2223 return -1;
2224
2225 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2226 no_seq_check, NULL);
2227 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2228 process_bss_event, bss);
2229
Hai Shalomb755a2a2020-04-23 21:49:02 -07002230 nl80211_init_connect_handle(bss);
2231
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002232 return 0;
2233}
2234
2235
2236static void nl80211_destroy_bss(struct i802_bss *bss)
2237{
2238 nl_cb_put(bss->nl_cb);
2239 bss->nl_cb = NULL;
Hai Shalomb755a2a2020-04-23 21:49:02 -07002240
2241 if (bss->nl_connect)
2242 nl80211_destroy_eloop_handle(&bss->nl_connect, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002243}
2244
2245
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002246static void
2247wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
2248{
2249 struct rfkill_config *rcfg;
2250
2251 if (drv->rfkill)
2252 return;
2253
2254 rcfg = os_zalloc(sizeof(*rcfg));
2255 if (!rcfg)
2256 return;
2257
2258 rcfg->ctx = drv;
2259
2260 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
2261 * not associated with a netdev, so use the name of some other interface
2262 * sharing the same wiphy as the P2P Device interface.
2263 *
2264 * Note: This is valid, as a P2P Device interface is always dynamically
2265 * created and is created only once another wpa_s interface was added.
2266 */
2267 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
2268 struct nl80211_global *global = drv->global;
2269 struct wpa_driver_nl80211_data *tmp1;
2270
2271 dl_list_for_each(tmp1, &global->interfaces,
2272 struct wpa_driver_nl80211_data, list) {
2273 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
2274 !tmp1->rfkill)
2275 continue;
2276
2277 wpa_printf(MSG_DEBUG,
2278 "nl80211: Use (%s) to initialize P2P Device rfkill",
2279 tmp1->first_bss->ifname);
2280 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
2281 sizeof(rcfg->ifname));
2282 break;
2283 }
2284 } else {
2285 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
2286 sizeof(rcfg->ifname));
2287 }
2288
2289 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
2290 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
2291 drv->rfkill = rfkill_init(rcfg);
2292 if (!drv->rfkill) {
2293 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
2294 os_free(rcfg);
2295 }
2296}
2297
2298
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002299static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
2300 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002301 const u8 *set_addr,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002302 const char *driver_params,
2303 enum wpa_p2p_mode p2p_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002304{
2305 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306 struct i802_bss *bss;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002307 char path[128], buf[200], *pos;
2308 ssize_t len;
2309 int ret;
2310
2311 ret = os_snprintf(path, sizeof(path), "/sys/class/net/%s/device/driver",
2312 ifname);
2313 if (!os_snprintf_error(sizeof(path), ret)) {
2314 len = readlink(path, buf, sizeof(buf));
2315 if (len > 0 && (size_t) len < sizeof(buf)) {
2316 buf[len] = '\0';
2317 pos = strrchr(buf, '/');
2318 if (pos)
2319 pos++;
2320 else
2321 pos = buf;
2322 wpa_printf(MSG_DEBUG,
2323 "nl80211: Initialize interface %s (driver: %s)",
2324 ifname, pos);
2325 }
2326 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002328 if (global_priv == NULL)
2329 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330 drv = os_zalloc(sizeof(*drv));
2331 if (drv == NULL)
2332 return NULL;
2333 drv->global = global_priv;
2334 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002335 drv->hostapd = !!hostapd;
2336 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002337
2338 /*
2339 * There is no driver capability flag for this, so assume it is
2340 * supported and disable this on first attempt to use if the driver
2341 * rejects the command due to missing support.
2342 */
2343 drv->set_rekey_offload = 1;
2344
Hai Shalom81f62d82019-07-22 12:10:00 -07002345 drv->num_if_indices = ARRAY_SIZE(drv->default_if_indices);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002346 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002347
2348 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
2349 if (!drv->first_bss) {
2350 os_free(drv);
2351 return NULL;
2352 }
2353 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002354 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002355 bss->ctx = ctx;
2356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
2358 drv->monitor_ifidx = -1;
2359 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002360 drv->eapol_tx_sock = -1;
2361 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002363 if (nl80211_init_bss(bss))
2364 goto failed;
2365
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002366 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params,
2367 p2p_mode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368 goto failed;
2369
Hai Shalom899fcc72020-10-19 14:38:18 -07002370 if (drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_TX_STATUS) {
2371 drv->control_port_ap = 1;
2372 goto skip_wifi_status;
2373 }
2374
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002375 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
2376 if (drv->eapol_tx_sock < 0)
2377 goto failed;
2378
2379 if (drv->data_tx_status) {
2380 int enabled = 1;
2381
2382 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
2383 &enabled, sizeof(enabled)) < 0) {
2384 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07002385 "nl80211: wifi status sockopt failed: %s",
2386 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002387 drv->data_tx_status = 0;
2388 if (!drv->use_monitor)
2389 drv->capa.flags &=
2390 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2391 } else {
Hai Shalom899fcc72020-10-19 14:38:18 -07002392 eloop_register_read_sock(
2393 drv->eapol_tx_sock,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002394 wpa_driver_nl80211_handle_eapol_tx_status,
2395 drv, NULL);
2396 }
2397 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002398skip_wifi_status:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002399
2400 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002401 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002403 drv->in_interface_list = 1;
2404 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405
Sunil Ravi036cec52023-03-29 11:35:17 -07002406 /*
Sunil Ravi99c035e2024-07-12 01:42:03 +00002407 * Use link ID 0 for the single "link" of a non-MLD.
Sunil Ravi036cec52023-03-29 11:35:17 -07002408 */
Sunil Ravi99c035e2024-07-12 01:42:03 +00002409 bss->valid_links = 0;
Sunil Ravi036cec52023-03-29 11:35:17 -07002410 bss->flink = &bss->links[0];
Sunil Ravi036cec52023-03-29 11:35:17 -07002411 os_memcpy(bss->flink->addr, bss->addr, ETH_ALEN);
2412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 return bss;
2414
2415failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002416 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 return NULL;
2418}
2419
2420
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002421/**
2422 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2423 * @ctx: context to be used when calling wpa_supplicant functions,
2424 * e.g., wpa_supplicant_event()
2425 * @ifname: interface name, e.g., wlan0
2426 * @global_priv: private driver global data from global_init()
2427 * Returns: Pointer to private data, %NULL on failure
2428 */
2429static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002430 void *global_priv,
2431 enum wpa_p2p_mode p2p_mode)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002432{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002433 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002434 NULL, p2p_mode);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002435}
2436
2437
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002438static int nl80211_register_frame(struct i802_bss *bss,
Hai Shalomfdcde762020-04-02 11:19:20 -07002439 struct nl_sock *nl_handle,
Hai Shalome21d4e82020-04-29 16:34:06 -07002440 u16 type, const u8 *match, size_t match_len,
2441 bool multicast)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002443 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002445 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002446 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002448 buf[0] = '\0';
2449 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Hai Shalome21d4e82020-04-29 16:34:06 -07002450 wpa_printf(MSG_DEBUG,
2451 "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s multicast=%d",
2452 type, fc2str(type), nl_handle, buf, multicast);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453
Hai Shalomfdcde762020-04-02 11:19:20 -07002454 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_FRAME)) ||
Hai Shalome21d4e82020-04-29 16:34:06 -07002455 (multicast && nla_put_flag(msg, NL80211_ATTR_RECEIVE_MULTICAST)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002456 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
2457 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
2458 nlmsg_free(msg);
2459 return -1;
2460 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002461
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002462 ret = send_and_recv(drv, nl_handle, msg, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002463 NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 if (ret) {
2465 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
2466 "failed (type=%u): ret=%d (%s)",
2467 type, ret, strerror(-ret));
2468 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2469 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471 return ret;
2472}
2473
2474
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002475static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
2476{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002477 if (bss->nl_mgmt) {
2478 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
2479 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
2480 return -1;
2481 }
2482
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002483 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002484 if (bss->nl_mgmt == NULL)
2485 return -1;
2486
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002487 return 0;
2488}
2489
2490
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002491static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
2492{
2493 nl80211_register_eloop_read(&bss->nl_mgmt,
2494 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002495 bss->nl_cb, 0);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002496}
2497
2498
Sunil Ravi7f769292024-07-23 22:21:32 +00002499static int nl80211_register_action_frame2(struct i802_bss *bss,
2500 const u8 *match, size_t match_len,
2501 bool multicast)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502{
2503 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002504 return nl80211_register_frame(bss, bss->nl_mgmt,
Sunil Ravi7f769292024-07-23 22:21:32 +00002505 type, match, match_len, multicast);
2506}
2507
2508
2509static int nl80211_register_action_frame(struct i802_bss *bss,
2510 const u8 *match, size_t match_len)
2511{
2512 return nl80211_register_action_frame2(bss, match, match_len, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513}
2514
2515
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002516static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002518 struct wpa_driver_nl80211_data *drv = bss->drv;
Hai Shalomfdcde762020-04-02 11:19:20 -07002519 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002520 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002521
2522 if (nl80211_alloc_mgmt_handle(bss))
2523 return -1;
2524 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
2525 "handle %p", bss->nl_mgmt);
2526
Hai Shalomfdcde762020-04-02 11:19:20 -07002527 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002528 /* register for any AUTH message */
Hai Shalome21d4e82020-04-29 16:34:06 -07002529 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0, false);
Hai Shalomfdcde762020-04-02 11:19:20 -07002530 } else if ((drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
2531 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
2532 /* register for SAE Authentication frames */
2533 nl80211_register_frame(bss, bss->nl_mgmt, type,
Hai Shalome21d4e82020-04-29 16:34:06 -07002534 (u8 *) "\x03\x00", 2, false);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002535 }
2536
Hai Shalom60840252021-02-19 19:02:11 -08002537#ifdef CONFIG_PASN
2538 /* register for PASN Authentication frames */
Sunil Ravi89eba102022-09-13 21:04:37 -07002539 if (nl80211_register_frame(bss, bss->nl_mgmt, type,
Hai Shalom60840252021-02-19 19:02:11 -08002540 (u8 *) "\x07\x00", 2, false))
2541 ret = -1;
2542#endif /* CONFIG_PASN */
2543
Dmitry Shmidt051af732013-10-22 13:52:46 -07002544#ifdef CONFIG_INTERWORKING
2545 /* QoS Map Configure */
2546 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002547 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002548#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002549#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002550 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002551 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002552 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002554 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002555 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002557 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002558 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002560 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002561 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002562 /* Protected GAS Initial Request */
2563 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2564 ret = -1;
2565 /* Protected GAS Initial Response */
2566 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2567 ret = -1;
2568 /* Protected GAS Comeback Request */
2569 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2570 ret = -1;
2571 /* Protected GAS Comeback Response */
2572 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2573 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002574#endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002575#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002577 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2579 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002580 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002582 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 (u8 *) "\x7f\x50\x6f\x9a\x09",
2584 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002585 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002586#endif /* CONFIG_P2P */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002587#ifdef CONFIG_NAN_USD
Sunil Ravi7f769292024-07-23 22:21:32 +00002588#define NAN_PUB_ACTION ((u8 *) "\x04\x09\x50\x6f\x9a\x13")
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002589 /* NAN SDF Public Action */
Sunil Ravi7f769292024-07-23 22:21:32 +00002590 if (nl80211_register_action_frame2(bss, NAN_PUB_ACTION, 6, true) < 0) {
2591 /* fallback to non-multicast */
2592 if (nl80211_register_action_frame2(bss, NAN_PUB_ACTION, 6,
2593 false) < 0)
2594 ret = -1;
2595 }
2596#undef NAN_PUB_ACTION
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002597#endif /* CONFIG_NAN_USD */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002598#ifdef CONFIG_DPP
2599 /* DPP Public Action */
2600 if (nl80211_register_action_frame(bss,
2601 (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
2602 6) < 0)
2603 ret = -1;
2604#endif /* CONFIG_DPP */
Hai Shalom74f70d42019-02-11 14:42:39 -08002605#ifdef CONFIG_OCV
2606 /* SA Query Request */
2607 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x00", 2) < 0)
2608 ret = -1;
2609#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002611 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002612 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002613#ifdef CONFIG_TDLS
2614 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2615 /* TDLS Discovery Response */
2616 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2617 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002618 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002619 }
2620#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002621#ifdef CONFIG_FST
2622 /* FST Action frames */
2623 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2624 ret = -1;
2625#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626
2627 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002628 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002629 ret = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002630 else if (!drv->has_driver_key_mgmt) {
2631 int i;
2632
2633 /* Update supported AKMs only if the driver doesn't advertize
2634 * any AKM capabilities. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2636 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2637
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002638 /* Update per interface supported AKMs */
2639 for (i = 0; i < WPA_IF_MAX; i++)
2640 drv->capa.key_mgmt_iftype[i] = drv->capa.key_mgmt;
2641 }
2642
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002643 /* WNM - BSS Transition Management Request */
2644 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002645 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002646 /* WNM-Sleep Mode Response */
2647 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002648 ret = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002649#ifdef CONFIG_WNM
2650 /* WNM - Collocated Interference Request */
2651 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x0b", 2) < 0)
2652 ret = -1;
2653#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002654
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002655#ifdef CONFIG_HS20
2656 /* WNM-Notification */
2657 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002658 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002659#endif /* CONFIG_HS20 */
2660
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002661 /* WMM-AC ADDTS Response */
2662 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2663 ret = -1;
2664
2665 /* WMM-AC DELTS */
2666 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2667 ret = -1;
2668
2669 /* Radio Measurement - Neighbor Report Response */
2670 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2671 ret = -1;
2672
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002673 /* Radio Measurement - Radio Measurement Request */
Hai Shalom899fcc72020-10-19 14:38:18 -07002674 if (!drv->no_rrm &&
2675 nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002676 ret = -1;
2677
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002678 /* Radio Measurement - Link Measurement Request */
2679 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2680 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2681 ret = -1;
2682
Hai Shalomc1a21442022-02-04 13:43:00 -08002683 /* Robust AV SCS Response */
2684 if (nl80211_register_action_frame(bss, (u8 *) "\x13\x01", 2) < 0)
2685 ret = -1;
2686
Hai Shalom899fcc72020-10-19 14:38:18 -07002687 /* Robust AV MSCS Response */
2688 if (nl80211_register_action_frame(bss, (u8 *) "\x13\x05", 2) < 0)
2689 ret = -1;
2690
Hai Shalomc1a21442022-02-04 13:43:00 -08002691 /* Protected QoS Management Action frame */
2692 if (nl80211_register_action_frame(bss, (u8 *) "\x7e\x50\x6f\x9a\x1a",
2693 5) < 0)
2694 ret = -1;
2695
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002696 nl80211_mgmt_handle_register_eloop(bss);
2697
2698 return ret;
2699}
2700
2701
2702static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2703{
2704 int ret = 0;
2705
2706 if (nl80211_alloc_mgmt_handle(bss))
2707 return -1;
2708
2709 wpa_printf(MSG_DEBUG,
2710 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2711 bss->nl_mgmt);
2712
2713 /* Auth frames for mesh SAE */
2714 if (nl80211_register_frame(bss, bss->nl_mgmt,
2715 (WLAN_FC_TYPE_MGMT << 2) |
2716 (WLAN_FC_STYPE_AUTH << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07002717 NULL, 0, false) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002718 ret = -1;
2719
2720 /* Mesh peering open */
2721 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2722 ret = -1;
2723 /* Mesh peering confirm */
2724 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2725 ret = -1;
2726 /* Mesh peering close */
2727 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2728 ret = -1;
2729
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002730 nl80211_mgmt_handle_register_eloop(bss);
2731
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002732 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733}
2734
2735
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002736static int nl80211_register_spurious_class3(struct i802_bss *bss)
2737{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002738 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002739 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002740
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002741 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002742 ret = send_and_recv(bss->drv, bss->nl_mgmt, msg, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002743 NULL, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002744 if (ret) {
2745 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2746 "failed: ret=%d (%s)",
2747 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002748 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002749 return ret;
2750}
2751
2752
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002753static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2754{
2755 int ret = 0;
2756
2757 /* Public Action frames */
2758 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2759 ret = -1;
2760 /* RRM Measurement Report */
2761 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2762 ret = -1;
Paul Stewart092955c2017-02-06 09:13:09 -08002763 /* RRM Link Measurement Report */
2764 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2765 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002766 /* RRM Neighbor Report Request */
2767 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2768 ret = -1;
2769 /* FT Action frames */
2770 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2771 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002772 /* SA Query */
2773 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2774 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002775 /* Protected Dual of Public Action */
2776 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2777 ret = -1;
2778 /* WNM */
2779 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2780 ret = -1;
2781 /* WMM */
2782 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2783 ret = -1;
2784#ifdef CONFIG_FST
2785 /* FST Action frames */
2786 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2787 ret = -1;
2788#endif /* CONFIG_FST */
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002789 /* Vendor-specific Protected */
2790 if (nl80211_register_action_frame(bss, (u8 *) "\x7e", 1) < 0)
2791 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002792 /* Vendor-specific */
2793 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2794 ret = -1;
2795
2796 return ret;
2797}
2798
2799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002800static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2801{
2802 static const int stypes[] = {
2803 WLAN_FC_STYPE_AUTH,
2804 WLAN_FC_STYPE_ASSOC_REQ,
2805 WLAN_FC_STYPE_REASSOC_REQ,
2806 WLAN_FC_STYPE_DISASSOC,
2807 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002808 WLAN_FC_STYPE_PROBE_REQ,
2809/* Beacon doesn't work as mac80211 doesn't currently allow
2810 * it, but it wouldn't really be the right thing anyway as
2811 * it isn't per interface ... maybe just dump the scan
2812 * results periodically for OLBC?
2813 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002814 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002815 };
2816 unsigned int i;
2817
2818 if (nl80211_alloc_mgmt_handle(bss))
2819 return -1;
2820 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2821 "handle %p", bss->nl_mgmt);
2822
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002823 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002824 if (nl80211_register_frame(bss, bss->nl_mgmt,
2825 (WLAN_FC_TYPE_MGMT << 2) |
2826 (stypes[i] << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07002827 NULL, 0, false) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002828 goto out_err;
2829 }
2830 }
2831
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002832 if (nl80211_action_subscribe_ap(bss))
2833 goto out_err;
2834
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002835 if (nl80211_register_spurious_class3(bss))
2836 goto out_err;
2837
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002838 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002839 return 0;
2840
2841out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002842 nl_destroy_handles(&bss->nl_mgmt);
2843 return -1;
2844}
2845
2846
2847static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2848{
2849 if (nl80211_alloc_mgmt_handle(bss))
2850 return -1;
2851 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2852 "handle %p (device SME)", bss->nl_mgmt);
2853
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002854 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002855 goto out_err;
2856
Hai Shalom5f92bc92019-04-18 11:54:11 -07002857 if (bss->drv->device_ap_sme) {
2858 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2859
2860 /* Register for all Authentication frames */
Hai Shalome21d4e82020-04-29 16:34:06 -07002861 if (nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0,
2862 false) < 0)
Hai Shalom5f92bc92019-04-18 11:54:11 -07002863 wpa_printf(MSG_DEBUG,
2864 "nl80211: Failed to subscribe to handle Authentication frames - SAE offload may not work");
2865 }
2866
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002867 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002868 return 0;
2869
2870out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002871 nl_destroy_handles(&bss->nl_mgmt);
2872 return -1;
2873}
2874
2875
2876static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2877{
2878 if (bss->nl_mgmt == NULL)
2879 return;
2880 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2881 "(%s)", bss->nl_mgmt, reason);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002882 nl80211_destroy_eloop_handle(&bss->nl_mgmt, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883
2884 nl80211_put_wiphy_data_ap(bss);
2885}
2886
2887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2889{
2890 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2891}
2892
2893
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002894static void nl80211_del_p2pdev(struct i802_bss *bss)
2895{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002896 struct nl_msg *msg;
2897 int ret;
2898
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002899 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002900 ret = send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002901
2902 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2903 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002904 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002905}
2906
2907
2908static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2909{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002910 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002911 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002912
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002913 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2914 NL80211_CMD_STOP_P2P_DEVICE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002915 ret = send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002916
2917 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2918 start ? "Start" : "Stop",
2919 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002920 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002921 return ret;
2922}
2923
2924
2925static int i802_set_iface_flags(struct i802_bss *bss, int up)
2926{
2927 enum nl80211_iftype nlmode;
2928
2929 nlmode = nl80211_get_ifmode(bss);
2930 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2931 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2932 bss->ifname, up);
2933 }
2934
2935 /* P2P Device has start/stop which is equivalent */
2936 return nl80211_set_p2pdev(bss, up);
2937}
2938
2939
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002940#ifdef CONFIG_TESTING_OPTIONS
2941static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2942{
2943 /* struct wpa_driver_nl80211_data *drv = arg; */
2944 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2945 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2946
2947
2948 wpa_printf(MSG_DEBUG,
2949 "nl80211: QCA vendor test command response received");
2950
2951 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2952 genlmsg_attrlen(gnlh, 0), NULL);
2953 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2954 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2955 return NL_SKIP;
2956 }
2957
2958 wpa_hexdump(MSG_DEBUG,
2959 "nl80211: Received QCA vendor test command response",
2960 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2961 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2962
2963 return NL_SKIP;
2964}
2965#endif /* CONFIG_TESTING_OPTIONS */
2966
2967
2968static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2969{
2970#ifdef CONFIG_TESTING_OPTIONS
2971 struct nl_msg *msg;
2972 struct nlattr *params;
2973 int ret;
2974
2975 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2976 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2977 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2978 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2979 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2980 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2981 nlmsg_free(msg);
2982 return;
2983 }
2984 nla_nest_end(msg, params);
2985
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002986 ret = send_and_recv_resp(drv, msg, qca_vendor_test_cmd_handler, drv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002987 wpa_printf(MSG_DEBUG,
2988 "nl80211: QCA vendor test command returned %d (%s)",
2989 ret, strerror(-ret));
2990#endif /* CONFIG_TESTING_OPTIONS */
2991}
2992
2993
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002994#ifdef CONFIG_DRIVER_NL80211_QCA
2995static int nl80211_set_p2p_mode(void *priv, enum wpa_p2p_mode mode)
2996{
2997 struct i802_bss *bss = priv;
2998 struct wpa_driver_nl80211_data *drv = bss->drv;
2999 struct nl_msg *msg;
3000 struct nlattr *container;
3001 int ret;
3002 enum qca_wlan_vendor_p2p_mode drv_mode;
3003
3004 wpa_printf(MSG_DEBUG, "nl80211: Set P2P mode: %d", mode);
3005
3006 switch (mode) {
3007 case WPA_P2P_MODE_WFD_R1:
3008 drv_mode = QCA_P2P_MODE_WFD_R1;
3009 break;
3010 case WPA_P2P_MODE_WFD_R2:
3011 drv_mode = QCA_P2P_MODE_WFD_R2;
3012 break;
3013 case WPA_P2P_MODE_WFD_PCC:
3014 drv_mode = QCA_P2P_MODE_WFD_PCC;
3015 break;
3016 default:
3017 return -1;
3018 }
3019
3020 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR);
3021 if (!msg ||
3022 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
3023 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
3024 QCA_NL80211_VENDOR_SUBCMD_SET_P2P_MODE))
3025 goto fail;
3026
3027 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
3028 if (!container)
3029 goto fail;
3030
3031 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_SET_P2P_MODE_CONFIG, drv_mode))
3032 goto fail;
3033
3034 nla_nest_end(msg, container);
3035
3036 ret = send_and_recv_cmd(drv, msg);
3037 if (ret)
3038 wpa_printf(MSG_ERROR,
3039 "nl80211: Failed to set P2P Mode: ret=%d (%s)",
3040 ret, strerror(-ret));
3041 return ret;
3042
3043fail:
3044 nlmsg_free(msg);
3045 return -1;
3046}
3047#endif /* CONFIG_DRIVER_NL80211_QCA */
3048
3049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003051wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003052 const u8 *set_addr, int first,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003053 const char *driver_params,
3054 enum wpa_p2p_mode p2p_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003056 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003057 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003058 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059
3060 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003061 bss->ifindex = drv->ifindex;
3062 bss->wdev_id = drv->global->if_add_wdevid;
3063 bss->wdev_id_set = drv->global->if_add_wdevid_set;
3064
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003065 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
3066 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003067 drv->global->if_add_wdevid_set = 0;
3068
Dmitry Shmidt03658832014-08-13 11:03:49 -07003069 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
3070 bss->static_ap = 1;
3071
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08003072 if (first &&
3073 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
3074 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
3075 drv->start_iface_up = 1;
3076
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003077 if (wpa_driver_nl80211_capa(drv))
3078 return -1;
3079
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003080 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
3081 return -1;
3082
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003083 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
3084 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003085
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003086 if (set_addr &&
3087 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
3088 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3089 set_addr)))
3090 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003091
Hai Shalomc1a21442022-02-04 13:43:00 -08003092 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_STATION)
3093 drv->start_mode_sta = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003094
Dmitry Shmidt03658832014-08-13 11:03:49 -07003095 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003096 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07003097 else if (bss->if_dynamic ||
3098 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003099 nlmode = nl80211_get_ifmode(bss);
3100 else
3101 nlmode = NL80211_IFTYPE_STATION;
3102
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003103 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003104 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003105 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003107
Dmitry Shmidt98660862014-03-11 17:26:21 -07003108 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003109 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003111 wpa_driver_nl80211_drv_init_rfkill(drv);
3112
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003113#ifdef CONFIG_DRIVER_NL80211_QCA
3114 if (nlmode == NL80211_IFTYPE_P2P_GO)
3115 nl80211_set_p2p_mode(bss, p2p_mode);
3116#endif /* CONFIG_DRIVER_NL80211_QCA */
3117
Dmitry Shmidt98660862014-03-11 17:26:21 -07003118 if (!rfkill_is_blocked(drv->rfkill)) {
3119 int ret = i802_set_iface_flags(bss, 1);
3120 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003121 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3122 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003123 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003124 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003125
3126 if (is_p2p_net_interface(nlmode))
3127 nl80211_disable_11b_rates(bss->drv,
3128 bss->drv->ifindex, 1);
3129
Dmitry Shmidt98660862014-03-11 17:26:21 -07003130 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
3131 return ret;
3132 } else {
3133 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3134 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003135 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
3136 drv->if_disabled = 1;
3137
Dmitry Shmidt98660862014-03-11 17:26:21 -07003138 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139 }
3140
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003141 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003142 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
3143 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003145 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
3146 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3147 bss->addr))
3148 return -1;
3149 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
3150 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152 if (send_rfkill_event) {
3153 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3154 drv, drv->ctx);
3155 }
3156
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003157 if (drv->vendor_cmd_test_avail)
3158 qca_vendor_test(drv);
3159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003160 return 0;
3161}
3162
3163
Sunil Ravi036cec52023-03-29 11:35:17 -07003164static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss,
Sunil Ravi99c035e2024-07-12 01:42:03 +00003165 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166{
3167 struct nl_msg *msg;
Paul Stewart092955c2017-02-06 09:13:09 -08003168 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003169 struct i802_link *link = nl80211_get_link(bss, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003170
Sunil Ravic0f5d412024-09-11 22:12:49 +00003171 if (!link || !link->beacon_set)
Sunil Ravi036cec52023-03-29 11:35:17 -07003172 return 0;
3173
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003174 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
Sunil Ravi99c035e2024-07-12 01:42:03 +00003175 bss->ifindex);
Sunil Ravi036cec52023-03-29 11:35:17 -07003176 link->beacon_set = 0;
3177 link->freq = 0;
3178
Paul Stewart092955c2017-02-06 09:13:09 -08003179 nl80211_put_wiphy_data_ap(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003180 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_BEACON);
Sunil Ravi036cec52023-03-29 11:35:17 -07003181 if (!msg)
3182 return -ENOBUFS;
3183
Sunil Ravi99c035e2024-07-12 01:42:03 +00003184 if (link_id != NL80211_DRV_LINK_ID_NA) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003185 wpa_printf(MSG_DEBUG,
3186 "nl80211: MLD: stop beaconing on link=%u",
Sunil Ravi99c035e2024-07-12 01:42:03 +00003187 link_id);
Sunil Ravi036cec52023-03-29 11:35:17 -07003188
Sunil Ravi99c035e2024-07-12 01:42:03 +00003189 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003190 nlmsg_free(msg);
3191 return -ENOBUFS;
3192 }
3193 }
3194
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003195 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003196}
3197
3198
Sunil Ravi036cec52023-03-29 11:35:17 -07003199static void wpa_driver_nl80211_del_beacon_all(struct i802_bss *bss)
3200{
Sunil Ravi99c035e2024-07-12 01:42:03 +00003201 int link_id;
Sunil Ravi036cec52023-03-29 11:35:17 -07003202
Sunil Ravi99c035e2024-07-12 01:42:03 +00003203 for_each_link_default(bss->valid_links, link_id, NL80211_DRV_LINK_ID_NA)
3204 wpa_driver_nl80211_del_beacon(bss, link_id);
Sunil Ravi036cec52023-03-29 11:35:17 -07003205}
3206
3207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003208/**
3209 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003210 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211 *
3212 * Shut down driver interface and processing of driver events. Free
3213 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3214 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003215static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003218 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003220 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
3221 bss->ifname, drv->disabled_11b_rates);
3222
Dmitry Shmidt04949592012-07-19 12:16:46 -07003223 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003224 if (drv->data_tx_status)
3225 eloop_unregister_read_sock(drv->eapol_tx_sock);
3226 if (drv->eapol_tx_sock >= 0)
3227 close(drv->eapol_tx_sock);
3228
3229 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003230 wpa_driver_nl80211_probe_req_report(bss, 0);
3231 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003232 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3233 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3235 "interface %s from bridge %s: %s",
3236 bss->ifname, bss->brname, strerror(errno));
3237 }
Hai Shalomc9e41a12018-07-31 14:41:42 -07003238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003240 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
3241 0) < 0)
3242 wpa_printf(MSG_INFO,
3243 "nl80211: Could not set bridge %s down",
3244 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003245 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003246 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3247 "bridge %s: %s",
3248 bss->brname, strerror(errno));
3249 }
3250
3251 nl80211_remove_monitor_interface(drv);
3252
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003253 if (is_ap_interface(drv->nlmode)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003254 wpa_driver_nl80211_del_beacon_all(bss);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003255 nl80211_remove_links(bss);
3256 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257
Sunil Ravic0f5d412024-09-11 22:12:49 +00003258 if (drv->rtnl_sk)
3259 nl_socket_free(drv->rtnl_sk);
3260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003261 if (drv->eapol_sock >= 0) {
3262 eloop_unregister_read_sock(drv->eapol_sock);
3263 close(drv->eapol_sock);
3264 }
3265
3266 if (drv->if_indices != drv->default_if_indices)
3267 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003269 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3271
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003272 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3273 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003274 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 rfkill_deinit(drv->rfkill);
3276
Sunil Ravic0f5d412024-09-11 22:12:49 +00003277 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, bss->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003279 if (!drv->start_iface_up)
3280 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003281
3282 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003283 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
3284 0) < 0) {
3285 wpa_printf(MSG_DEBUG,
3286 "nl80211: Could not set interface down to restore permanent MAC address");
3287 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003288 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3289 drv->perm_addr) < 0) {
3290 wpa_printf(MSG_DEBUG,
3291 "nl80211: Could not restore permanent MAC address");
3292 }
3293 }
3294
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003295 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003296 if (drv->start_mode_sta)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003297 wpa_driver_nl80211_set_mode(bss,
3298 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003299 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003300 } else {
3301 nl80211_mgmt_unsubscribe(bss, "deinit");
3302 nl80211_del_p2pdev(bss);
3303 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003305 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 os_free(drv->filter_ssids);
3308
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003309 os_free(drv->auth_ie);
Hai Shalom60840252021-02-19 19:02:11 -08003310 os_free(drv->auth_data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003311
3312 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 dl_list_del(&drv->list);
3314
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003315 os_free(drv->extended_capa);
3316 os_free(drv->extended_capa_mask);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003317 for (i = 0; i < drv->num_iface_capa; i++) {
3318 os_free(drv->iface_capa[i].ext_capa);
3319 os_free(drv->iface_capa[i].ext_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003320 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003321 os_free(drv->first_bss);
Hai Shalom60840252021-02-19 19:02:11 -08003322#ifdef CONFIG_DRIVER_NL80211_QCA
3323 os_free(drv->pending_roam_data);
3324#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 os_free(drv);
3326}
3327
3328
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003329static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
3330{
3331 switch (alg) {
3332 case WPA_ALG_WEP:
3333 if (key_len == 5)
Paul Stewart092955c2017-02-06 09:13:09 -08003334 return RSN_CIPHER_SUITE_WEP40;
3335 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003336 case WPA_ALG_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08003337 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003338 case WPA_ALG_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003339 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003340 case WPA_ALG_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003341 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003342 case WPA_ALG_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003343 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003344 case WPA_ALG_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003345 return RSN_CIPHER_SUITE_GCMP_256;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003346 case WPA_ALG_BIP_CMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08003347 return RSN_CIPHER_SUITE_AES_128_CMAC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003348 case WPA_ALG_BIP_GMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08003349 return RSN_CIPHER_SUITE_BIP_GMAC_128;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003350 case WPA_ALG_BIP_GMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003351 return RSN_CIPHER_SUITE_BIP_GMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003352 case WPA_ALG_BIP_CMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003353 return RSN_CIPHER_SUITE_BIP_CMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003354 case WPA_ALG_SMS4:
Paul Stewart092955c2017-02-06 09:13:09 -08003355 return RSN_CIPHER_SUITE_SMS4;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003356 case WPA_ALG_KRK:
Paul Stewart092955c2017-02-06 09:13:09 -08003357 return RSN_CIPHER_SUITE_KRK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003358 case WPA_ALG_NONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003359 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
3360 alg);
3361 return 0;
3362 }
3363
3364 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
3365 alg);
3366 return 0;
3367}
3368
3369
3370static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
3371{
3372 switch (cipher) {
3373 case WPA_CIPHER_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003374 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003375 case WPA_CIPHER_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003376 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003377 case WPA_CIPHER_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003378 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003379 case WPA_CIPHER_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003380 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003381 case WPA_CIPHER_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08003382 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003383 case WPA_CIPHER_WEP104:
Paul Stewart092955c2017-02-06 09:13:09 -08003384 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003385 case WPA_CIPHER_WEP40:
Paul Stewart092955c2017-02-06 09:13:09 -08003386 return RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003387 case WPA_CIPHER_GTK_NOT_USED:
Paul Stewart092955c2017-02-06 09:13:09 -08003388 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003389 default:
3390 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003391 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003392}
3393
3394
3395static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
3396 int max_suites)
3397{
3398 int num_suites = 0;
3399
3400 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08003401 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003402 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08003403 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003404 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08003405 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003406 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08003407 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003408 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
Paul Stewart092955c2017-02-06 09:13:09 -08003409 suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003410 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
Paul Stewart092955c2017-02-06 09:13:09 -08003411 suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003412 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
Paul Stewart092955c2017-02-06 09:13:09 -08003413 suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003414
3415 return num_suites;
3416}
3417
3418
Hai Shalomfdcde762020-04-02 11:19:20 -07003419static int wpa_key_mgmt_to_suites(unsigned int key_mgmt_suites, u32 suites[],
3420 int max_suites)
3421{
3422 int num_suites = 0;
3423
3424#define __AKM(a, b) \
3425 if (num_suites < max_suites && \
3426 (key_mgmt_suites & (WPA_KEY_MGMT_ ## a))) \
3427 suites[num_suites++] = (RSN_AUTH_KEY_MGMT_ ## b)
3428 __AKM(IEEE8021X, UNSPEC_802_1X);
3429 __AKM(PSK, PSK_OVER_802_1X);
3430 __AKM(FT_IEEE8021X, FT_802_1X);
3431 __AKM(FT_PSK, FT_PSK);
3432 __AKM(IEEE8021X_SHA256, 802_1X_SHA256);
3433 __AKM(PSK_SHA256, PSK_SHA256);
3434 __AKM(SAE, SAE);
Sunil Ravi89eba102022-09-13 21:04:37 -07003435 __AKM(SAE_EXT_KEY, SAE_EXT_KEY);
Hai Shalomfdcde762020-04-02 11:19:20 -07003436 __AKM(FT_SAE, FT_SAE);
Sunil Ravi89eba102022-09-13 21:04:37 -07003437 __AKM(FT_SAE_EXT_KEY, FT_SAE_EXT_KEY);
Hai Shalomfdcde762020-04-02 11:19:20 -07003438 __AKM(CCKM, CCKM);
3439 __AKM(OSEN, OSEN);
3440 __AKM(IEEE8021X_SUITE_B, 802_1X_SUITE_B);
3441 __AKM(IEEE8021X_SUITE_B_192, 802_1X_SUITE_B_192);
3442 __AKM(FILS_SHA256, FILS_SHA256);
3443 __AKM(FILS_SHA384, FILS_SHA384);
3444 __AKM(FT_FILS_SHA256, FT_FILS_SHA256);
3445 __AKM(FT_FILS_SHA384, FT_FILS_SHA384);
3446 __AKM(OWE, OWE);
3447 __AKM(DPP, DPP);
3448 __AKM(FT_IEEE8021X_SHA384, FT_802_1X_SHA384);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003449 __AKM(IEEE8021X_SHA384, 802_1X_SHA384);
Hai Shalomfdcde762020-04-02 11:19:20 -07003450#undef __AKM
3451
3452 return num_suites;
3453}
3454
3455
Isaac Chiou6ce580d2024-04-24 17:07:24 +08003456#if (defined(CONFIG_DRIVER_NL80211_BRCM) && !defined(WIFI_BRCM_OPEN_SOURCE_MULTI_AKM)) || \
3457 defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawad14709082022-03-17 14:25:11 +05303458static int wpa_cross_akm_key_mgmt_to_suites(unsigned int key_mgmt_suites, u32 suites[],
3459 int max_suites)
3460{
3461 int num_suites = 0;
3462
3463#define __AKM_TO_SUITES_ARRAY(a, b) \
3464 if (num_suites < max_suites && \
3465 (key_mgmt_suites & (WPA_KEY_MGMT_ ## a))) \
3466 suites[num_suites++] = (RSN_AUTH_KEY_MGMT_ ## b)
3467 __AKM_TO_SUITES_ARRAY(PSK, PSK_OVER_802_1X);
3468 __AKM_TO_SUITES_ARRAY(SAE, SAE);
3469#undef __AKM_TO_SUITES_ARRAY
3470
3471 return num_suites;
3472}
Isaac Chiou6ce580d2024-04-24 17:07:24 +08003473#endif /* (CONFIG_DRIVER_NL80211_BRCM && !WIFI_BRCM_OPEN_SOURCE_MULTI_AKM) ||
3474 * CONFIG_DRIVER_NL80211_SYNA */
Vinayak Yadawad14709082022-03-17 14:25:11 +05303475
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003476
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003477#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003478static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
3479 const u8 *key, size_t key_len)
3480{
3481 struct nl_msg *msg;
3482 int ret;
3483
3484 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
3485 return 0;
3486
3487 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
3488 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
3489 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
3490 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
3491 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
3492 nl80211_nlmsg_clear(msg);
3493 nlmsg_free(msg);
3494 return -1;
3495 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003496 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003497 if (ret) {
3498 wpa_printf(MSG_DEBUG,
3499 "nl80211: Key management set key failed: ret=%d (%s)",
3500 ret, strerror(-ret));
3501 }
3502
3503 return ret;
3504}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003505#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003506
3507
Andy Kuoaba17c12022-04-14 16:05:31 +08003508#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05303509static int key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
3510 const u8 *key, size_t key_len)
3511{
3512 struct nl_msg *msg;
3513 int ret;
3514 struct nlattr *params;
3515
3516 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
3517 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
3518 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
Hai Shalomc1a21442022-02-04 13:43:00 -08003519 BRCM_VENDOR_SCMD_SET_PMK) ||
Mir Ali677e7482020-11-12 19:49:02 +05303520 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
3521 nla_put(msg, BRCM_ATTR_DRIVER_KEY_PMK, key_len, key)) {
3522 nl80211_nlmsg_clear(msg);
3523 nlmsg_free(msg);
3524 return -ENOBUFS;
3525 }
3526 nla_nest_end(msg, params);
3527
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003528 ret = send_and_recv_cmd(drv, msg);
Mir Ali677e7482020-11-12 19:49:02 +05303529 if (ret) {
3530 wpa_printf(MSG_DEBUG, "nl80211: Key mgmt set key failed: ret=%d (%s)",
3531 ret, strerror(-ret));
3532 }
3533
3534 return ret;
3535}
Andy Kuoaba17c12022-04-14 16:05:31 +08003536#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3537
Mir Ali677e7482020-11-12 19:49:02 +05303538
Roshan Pius3a1667e2018-07-03 15:17:14 -07003539static int nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
3540 const u8 *key, size_t key_len,
3541 const u8 *addr)
3542{
3543 struct nl_msg *msg = NULL;
3544 int ret;
3545
3546 /*
3547 * If the authenticator address is not set, assume it is
3548 * the current BSSID.
3549 */
3550 if (!addr && drv->associated)
3551 addr = drv->bssid;
3552 else if (!addr)
3553 return -1;
3554
3555 wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
3556 MAC2STR(addr));
3557 wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
3558 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
3559 if (!msg ||
3560 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3561 nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
3562 nl80211_nlmsg_clear(msg);
3563 nlmsg_free(msg);
3564 return -ENOBUFS;
3565 }
3566
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003567 ret = send_and_recv_cmd(drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003568 if (ret) {
3569 wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
3570 ret, strerror(-ret));
3571 }
3572
3573 return ret;
3574}
3575
3576
Hai Shalomfdcde762020-04-02 11:19:20 -07003577static int wpa_driver_nl80211_set_key(struct i802_bss *bss,
3578 struct wpa_driver_set_key_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003581 int ifindex;
Hai Shalomc3565922019-10-28 11:58:20 -07003582 struct nl_msg *msg;
3583 struct nl_msg *key_msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07003585 int skip_set_key = 1;
3586 const char *ifname = params->ifname;
3587 enum wpa_alg alg = params->alg;
3588 const u8 *addr = params->addr;
3589 int key_idx = params->key_idx;
3590 int set_tx = params->set_tx;
3591 const u8 *seq = params->seq;
3592 size_t seq_len = params->seq_len;
3593 const u8 *key = params->key;
3594 size_t key_len = params->key_len;
3595 int vlan_id = params->vlan_id;
3596 enum key_flag key_flag = params->key_flag;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003597 int link_id = params->link_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003599 /* Ignore for P2P Device */
3600 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
3601 return 0;
3602
3603 ifindex = if_nametoindex(ifname);
3604 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Sunil Ravi77d572f2023-01-17 23:58:31 +00003605 "set_tx=%d seq_len=%lu key_len=%lu key_flag=0x%x link_id=%d",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003606 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003607 (unsigned long) seq_len, (unsigned long) key_len, key_flag,
3608 link_id);
Hai Shalomfdcde762020-04-02 11:19:20 -07003609
3610 if (check_key_flag(key_flag)) {
3611 wpa_printf(MSG_DEBUG, "%s: invalid key_flag", __func__);
3612 return -EINVAL;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07003613 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003615#ifdef CONFIG_DRIVER_NL80211_QCA
Hai Shalomfdcde762020-04-02 11:19:20 -07003616 if ((key_flag & KEY_FLAG_PMK) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003617 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
3618 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
3619 __func__);
3620 ret = issue_key_mgmt_set_key(drv, key, key_len);
3621 return ret;
3622 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003623#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624
Hai Shalomfdcde762020-04-02 11:19:20 -07003625 if (key_flag & KEY_FLAG_PMK) {
3626 if (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)
3627 return nl80211_set_pmk(drv, key, key_len, addr);
Andy Kuoaba17c12022-04-14 16:05:31 +08003628#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05303629 if (drv->vendor_set_pmk) {
Jay Patel731adae2021-02-17 14:55:58 -08003630 wpa_printf(MSG_INFO, "nl80211: key_mgmt_set_key with key_len %lu", (unsigned long) key_len);
Mir Ali677e7482020-11-12 19:49:02 +05303631 return key_mgmt_set_key(drv, key, key_len);
3632 }
Andy Kuoaba17c12022-04-14 16:05:31 +08003633#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3634
Hai Shalomfdcde762020-04-02 11:19:20 -07003635 /* The driver does not have any offload mechanism for PMK, so
3636 * there is no need to configure this key. */
3637 return 0;
3638 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003639
Hai Shalomfdcde762020-04-02 11:19:20 -07003640 ret = -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07003641 key_msg = nlmsg_alloc();
3642 if (!key_msg)
Hai Shalomfdcde762020-04-02 11:19:20 -07003643 return ret;
Hai Shalomc3565922019-10-28 11:58:20 -07003644
Hai Shalomfdcde762020-04-02 11:19:20 -07003645 if ((key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3646 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) {
3647 wpa_printf(MSG_DEBUG,
3648 "nl80211: SET_KEY (pairwise RX/TX modify)");
3649 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
3650 if (!msg)
3651 goto fail2;
3652 } else if (alg == WPA_ALG_NONE && (key_flag & KEY_FLAG_RX_TX)) {
3653 wpa_printf(MSG_DEBUG, "%s: invalid key_flag to delete key",
3654 __func__);
3655 ret = -EINVAL;
3656 goto fail2;
3657 } else if (alg == WPA_ALG_NONE) {
3658 wpa_printf(MSG_DEBUG, "nl80211: DEL_KEY");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003659 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
3660 if (!msg)
Hai Shalomc3565922019-10-28 11:58:20 -07003661 goto fail2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003663 u32 suite;
3664
3665 suite = wpa_alg_to_cipher_suite(alg, key_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07003666 if (!suite) {
3667 ret = -EINVAL;
Hai Shalomc3565922019-10-28 11:58:20 -07003668 goto fail2;
Hai Shalomfdcde762020-04-02 11:19:20 -07003669 }
3670 wpa_printf(MSG_DEBUG, "nl80211: NEW_KEY");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003671 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
Hai Shalomc3565922019-10-28 11:58:20 -07003672 if (!msg)
3673 goto fail2;
3674 if (nla_put(key_msg, NL80211_KEY_DATA, key_len, key) ||
3675 nla_put_u32(key_msg, NL80211_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003676 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003677 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678
Hai Shalomfdcde762020-04-02 11:19:20 -07003679 if (seq && seq_len) {
3680 if (nla_put(key_msg, NL80211_KEY_SEQ, seq_len, seq))
3681 goto fail;
3682 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ",
3683 seq, seq_len);
3684 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003685 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686
3687 if (addr && !is_broadcast_ether_addr(addr)) {
3688 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003689 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
3690 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691
Hai Shalomfdcde762020-04-02 11:19:20 -07003692 if ((key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3693 KEY_FLAG_PAIRWISE_RX ||
3694 (key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3695 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) {
3696 if (nla_put_u8(key_msg, NL80211_KEY_MODE,
3697 key_flag == KEY_FLAG_PAIRWISE_RX ?
3698 NL80211_KEY_NO_TX : NL80211_KEY_SET_TX))
3699 goto fail;
3700 } else if ((key_flag & KEY_FLAG_GROUP_MASK) ==
3701 KEY_FLAG_GROUP_RX) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Hai Shalomc3565922019-10-28 11:58:20 -07003703 if (nla_put_u32(key_msg, NL80211_KEY_TYPE,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003704 NL80211_KEYTYPE_GROUP))
3705 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07003706 } else if (!(key_flag & KEY_FLAG_PAIRWISE)) {
3707 wpa_printf(MSG_DEBUG,
3708 " key_flag missing PAIRWISE when setting a pairwise key");
3709 ret = -EINVAL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003710 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07003711 } else if (alg == WPA_ALG_WEP &&
3712 (key_flag & KEY_FLAG_RX_TX) == KEY_FLAG_RX_TX) {
3713 wpa_printf(MSG_DEBUG, " unicast WEP key");
3714 skip_set_key = 0;
3715 } else {
3716 wpa_printf(MSG_DEBUG, " pairwise key");
3717 }
3718 } else if ((key_flag & KEY_FLAG_PAIRWISE) ||
3719 !(key_flag & KEY_FLAG_GROUP)) {
3720 wpa_printf(MSG_DEBUG,
3721 " invalid key_flag for a broadcast key");
3722 ret = -EINVAL;
3723 goto fail;
3724 } else {
3725 wpa_printf(MSG_DEBUG, " broadcast key");
3726 if (key_flag & KEY_FLAG_DEFAULT)
3727 skip_set_key = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003728 }
Hai Shalomc3565922019-10-28 11:58:20 -07003729 if (nla_put_u8(key_msg, NL80211_KEY_IDX, key_idx) ||
3730 nla_put_nested(msg, NL80211_ATTR_KEY, key_msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003731 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003732 nl80211_nlmsg_clear(key_msg);
3733 nlmsg_free(key_msg);
3734 key_msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003735
Hai Shalomfdcde762020-04-02 11:19:20 -07003736 if (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
3737 wpa_printf(MSG_DEBUG, "nl80211: VLAN ID %d", vlan_id);
3738 if (nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id))
3739 goto fail;
3740 }
3741
Sunil Ravi77d572f2023-01-17 23:58:31 +00003742 if (link_id != -1) {
3743 wpa_printf(MSG_DEBUG, "nl80211: Link ID %d", link_id);
3744 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3745 goto fail;
3746 }
3747
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003748 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003749 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
3750 ret = 0;
3751 if (ret)
Hai Shalomfdcde762020-04-02 11:19:20 -07003752 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003753 ret, strerror(-ret));
3754
3755 /*
Hai Shalomfdcde762020-04-02 11:19:20 -07003756 * If we failed or don't need to set the key as default (below),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003757 * we're done here.
3758 */
Hai Shalomfdcde762020-04-02 11:19:20 -07003759 if (ret || skip_set_key)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003760 return ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07003761 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_SET_KEY - default key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762
Hai Shalomfdcde762020-04-02 11:19:20 -07003763 ret = -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07003764 key_msg = nlmsg_alloc();
3765 if (!key_msg)
Hai Shalomfdcde762020-04-02 11:19:20 -07003766 return ret;
Hai Shalomc3565922019-10-28 11:58:20 -07003767
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003768 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
Hai Shalomc3565922019-10-28 11:58:20 -07003769 if (!msg)
3770 goto fail2;
3771 if (!key_msg ||
3772 nla_put_u8(key_msg, NL80211_KEY_IDX, key_idx) ||
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003773 nla_put_flag(key_msg, wpa_alg_bip(alg) ?
Hai Shalomfdcde762020-04-02 11:19:20 -07003774 (key_idx == 6 || key_idx == 7 ?
3775 NL80211_KEY_DEFAULT_BEACON :
3776 NL80211_KEY_DEFAULT_MGMT) :
3777 NL80211_KEY_DEFAULT))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003778 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003780 struct nlattr *types;
3781
Hai Shalomc3565922019-10-28 11:58:20 -07003782 types = nla_nest_start(key_msg, NL80211_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003783 if (!types ||
Hai Shalomc3565922019-10-28 11:58:20 -07003784 nla_put_flag(key_msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003785 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003786 nla_nest_end(key_msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003787 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003788 struct nlattr *types;
3789
Hai Shalomc3565922019-10-28 11:58:20 -07003790 types = nla_nest_start(key_msg, NL80211_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003791 if (!types ||
Hai Shalomc3565922019-10-28 11:58:20 -07003792 nla_put_flag(key_msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003793 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003794 nla_nest_end(key_msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795 }
3796
Hai Shalomc3565922019-10-28 11:58:20 -07003797 if (nla_put_nested(msg, NL80211_ATTR_KEY, key_msg))
3798 goto fail;
3799 nl80211_nlmsg_clear(key_msg);
3800 nlmsg_free(key_msg);
3801 key_msg = NULL;
3802
Hai Shalomfdcde762020-04-02 11:19:20 -07003803 if (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
3804 wpa_printf(MSG_DEBUG, "nl80211: set_key default - VLAN ID %d",
3805 vlan_id);
3806 if (nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id))
3807 goto fail;
3808 }
3809
Sunil Ravi77d572f2023-01-17 23:58:31 +00003810 if (link_id != -1) {
3811 wpa_printf(MSG_DEBUG, "nl80211: set_key default - Link ID %d",
3812 link_id);
3813 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3814 goto fail;
3815 }
3816
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003817 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003818 if (ret)
Hai Shalomfdcde762020-04-02 11:19:20 -07003819 wpa_printf(MSG_DEBUG,
3820 "nl80211: set_key default failed; err=%d %s",
3821 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003822 return ret;
3823
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003824fail:
3825 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003826 nlmsg_free(msg);
Hai Shalomc3565922019-10-28 11:58:20 -07003827fail2:
3828 nl80211_nlmsg_clear(key_msg);
3829 nlmsg_free(key_msg);
Hai Shalomfdcde762020-04-02 11:19:20 -07003830 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831}
3832
3833
3834static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3835 int key_idx, int defkey,
3836 const u8 *seq, size_t seq_len,
3837 const u8 *key, size_t key_len)
3838{
3839 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003840 u32 suite;
3841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003842 if (!key_attr)
3843 return -1;
3844
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003845 suite = wpa_alg_to_cipher_suite(alg, key_len);
3846 if (!suite)
3847 return -1;
3848
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003849 if (defkey && wpa_alg_bip(alg)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003850 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3851 return -1;
3852 } else if (defkey) {
3853 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3854 return -1;
3855 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003857 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003858 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003859 (seq && seq_len &&
3860 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3861 nla_put(msg, NL80211_KEY_DATA, key_len, key))
3862 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863
3864 nla_nest_end(msg, key_attr);
3865
3866 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867}
3868
3869
3870static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3871 struct nl_msg *msg)
3872{
3873 int i, privacy = 0;
3874 struct nlattr *nl_keys, *nl_key;
3875
3876 for (i = 0; i < 4; i++) {
3877 if (!params->wep_key[i])
3878 continue;
3879 privacy = 1;
3880 break;
3881 }
3882 if (params->wps == WPS_MODE_PRIVACY)
3883 privacy = 1;
3884 if (params->pairwise_suite &&
3885 params->pairwise_suite != WPA_CIPHER_NONE)
3886 privacy = 1;
3887
3888 if (!privacy)
3889 return 0;
3890
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003891 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3892 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003893
3894 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3895 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003896 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897
3898 for (i = 0; i < 4; i++) {
3899 if (!params->wep_key[i])
3900 continue;
3901
3902 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003903 if (!nl_key ||
3904 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3905 params->wep_key[i]) ||
3906 nla_put_u32(msg, NL80211_KEY_CIPHER,
3907 params->wep_key_len[i] == 5 ?
Paul Stewart092955c2017-02-06 09:13:09 -08003908 RSN_CIPHER_SUITE_WEP40 :
3909 RSN_CIPHER_SUITE_WEP104) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003910 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3911 (i == params->wep_tx_keyidx &&
3912 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3913 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914
3915 nla_nest_end(msg, nl_key);
3916 }
3917 nla_nest_end(msg, nl_keys);
3918
3919 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003920}
3921
3922
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003923int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3924 const u8 *addr, int cmd, u16 reason_code,
Hai Shalom74f70d42019-02-11 14:42:39 -08003925 int local_state_change,
Hai Shalomc1a21442022-02-04 13:43:00 -08003926 struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003927{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003928 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003929 struct nl_msg *msg;
3930
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003931 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
3932 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3933 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3934 (local_state_change &&
3935 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3936 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003938 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003940 ret = send_and_recv(drv, bss->nl_connect, msg,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003941 NULL, NULL, NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003942 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003943 wpa_dbg(drv->ctx, MSG_DEBUG,
3944 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3945 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 return ret;
3948}
3949
3950
3951static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Hai Shalom81f62d82019-07-22 12:10:00 -07003952 u16 reason_code,
Hai Shalomc1a21442022-02-04 13:43:00 -08003953 struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003955 int ret;
3956
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003957 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003958 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003959 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003960 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
Hai Shalomc1a21442022-02-04 13:43:00 -08003961 reason_code, 0, bss);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003962 /*
3963 * For locally generated disconnect, supplicant already generates a
3964 * DEAUTH event, so ignore the event from NL80211.
3965 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003966 if (ret == 0)
3967 drv->ignore_next_local_disconnect = send_event_marker(drv);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003968
3969 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003970}
3971
3972
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003973static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
Hai Shalom81f62d82019-07-22 12:10:00 -07003974 const u8 *addr, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003975{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003977 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003978
3979 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3980 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003981 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003982 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003983 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003984 return wpa_driver_nl80211_disconnect(drv, reason_code, bss);
Hai Shalom74f70d42019-02-11 14:42:39 -08003985 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3987 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003988 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003989 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
Hai Shalomc1a21442022-02-04 13:43:00 -08003990 reason_code, 0, bss);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003991 /*
3992 * For locally generated deauthenticate, supplicant already generates a
3993 * DEAUTH event, so ignore the event from NL80211.
3994 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003995 if (ret == 0)
3996 drv->ignore_next_local_deauth = send_event_marker(drv);
Hai Shalomce48b4a2018-09-05 11:41:35 -07003997
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003998 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999}
4000
4001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004002static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
4003 struct wpa_driver_auth_params *params)
4004{
4005 int i;
4006
4007 drv->auth_freq = params->freq;
4008 drv->auth_alg = params->auth_alg;
4009 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
4010 drv->auth_local_state_change = params->local_state_change;
4011 drv->auth_p2p = params->p2p;
4012
4013 if (params->bssid)
4014 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
4015 else
4016 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
4017
4018 if (params->ssid) {
4019 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
4020 drv->auth_ssid_len = params->ssid_len;
4021 } else
4022 drv->auth_ssid_len = 0;
4023
4024
4025 os_free(drv->auth_ie);
4026 drv->auth_ie = NULL;
4027 drv->auth_ie_len = 0;
4028 if (params->ie) {
4029 drv->auth_ie = os_malloc(params->ie_len);
4030 if (drv->auth_ie) {
4031 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
4032 drv->auth_ie_len = params->ie_len;
4033 }
4034 }
4035
Sunil Ravi77d572f2023-01-17 23:58:31 +00004036 if (params->mld && params->ap_mld_addr) {
4037 drv->auth_mld = params->mld;
4038 drv->auth_mld_link_id = params->mld_link_id;
4039 os_memcpy(drv->auth_ap_mld_addr, params->ap_mld_addr, ETH_ALEN);
4040 } else {
4041 drv->auth_mld = false;
4042 drv->auth_mld_link_id = -1;
4043 }
4044
Hai Shalom60840252021-02-19 19:02:11 -08004045 os_free(drv->auth_data);
4046 drv->auth_data = NULL;
4047 drv->auth_data_len = 0;
4048 if (params->auth_data) {
4049 drv->auth_data = os_memdup(params->auth_data,
4050 params->auth_data_len);
4051 if (drv->auth_data)
4052 drv->auth_data_len = params->auth_data_len;
4053 }
4054
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004055 for (i = 0; i < 4; i++) {
4056 if (params->wep_key[i] && params->wep_key_len[i] &&
4057 params->wep_key_len[i] <= 16) {
4058 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
4059 params->wep_key_len[i]);
4060 drv->auth_wep_key_len[i] = params->wep_key_len[i];
4061 } else
4062 drv->auth_wep_key_len[i] = 0;
4063 }
4064}
4065
4066
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004067static void nl80211_unmask_11b_rates(struct i802_bss *bss)
4068{
4069 struct wpa_driver_nl80211_data *drv = bss->drv;
4070
4071 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
4072 return;
4073
4074 /*
4075 * Looks like we failed to unmask 11b rates previously. This could
4076 * happen, e.g., if the interface was down at the point in time when a
4077 * P2P group was terminated.
4078 */
4079 wpa_printf(MSG_DEBUG,
4080 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
4081 bss->ifname);
4082 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4083}
4084
4085
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004086static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
4087{
4088 if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
4089 return NL80211_AUTHTYPE_OPEN_SYSTEM;
4090 if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
4091 return NL80211_AUTHTYPE_SHARED_KEY;
4092 if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
4093 return NL80211_AUTHTYPE_NETWORK_EAP;
4094 if (wpa_auth_alg & WPA_AUTH_ALG_FT)
4095 return NL80211_AUTHTYPE_FT;
4096 if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
4097 return NL80211_AUTHTYPE_SAE;
4098 if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
4099 return NL80211_AUTHTYPE_FILS_SK;
4100 if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
4101 return NL80211_AUTHTYPE_FILS_SK_PFS;
4102
4103 return NL80211_AUTHTYPE_MAX;
4104}
4105
4106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004108 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004109{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110 struct wpa_driver_nl80211_data *drv = bss->drv;
4111 int ret = -1, i;
4112 struct nl_msg *msg;
4113 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004114 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004116 int is_retry;
Hai Shalomfdcde762020-04-02 11:19:20 -07004117 struct wpa_driver_set_key_params p;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004118
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004119 nl80211_unmask_11b_rates(bss);
4120
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004121 is_retry = drv->retry_auth;
4122 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07004123 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004125 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004127 if (params->bssid)
4128 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4129 else
4130 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004132 nlmode = params->p2p ?
4133 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4134 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004135 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136 return -1;
4137
4138retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4140 drv->ifindex);
4141
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004142 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004143 if (!msg)
4144 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145
Hai Shalomfdcde762020-04-02 11:19:20 -07004146 os_memset(&p, 0, sizeof(p));
4147 p.ifname = bss->ifname;
4148 p.alg = WPA_ALG_WEP;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004149 p.link_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004150 for (i = 0; i < 4; i++) {
4151 if (!params->wep_key[i])
4152 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07004153 p.key_idx = i;
4154 p.set_tx = i == params->wep_tx_keyidx;
4155 p.key = params->wep_key[i];
4156 p.key_len = params->wep_key_len[i];
4157 p.key_flag = i == params->wep_tx_keyidx ?
4158 KEY_FLAG_GROUP_RX_TX_DEFAULT :
4159 KEY_FLAG_GROUP_RX_TX;
4160 wpa_driver_nl80211_set_key(bss, &p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161 if (params->wep_tx_keyidx != i)
4162 continue;
4163 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004164 params->wep_key[i], params->wep_key_len[i]))
4165 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166 }
4167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004168 if (params->bssid) {
4169 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4170 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004171 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4172 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173 }
4174 if (params->freq) {
4175 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004176 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
4177 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178 }
4179 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004180 wpa_printf(MSG_DEBUG, " * SSID=%s",
4181 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004182 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4183 params->ssid))
4184 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004185 }
4186 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004187 if (params->ie &&
4188 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
4189 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004190 if (params->auth_data) {
4191 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
4192 params->auth_data_len);
4193 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
4194 params->auth_data))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004195 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004196 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004197 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004199 if (type == NL80211_AUTHTYPE_MAX ||
4200 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004201 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202 if (params->local_state_change) {
4203 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004204 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
4205 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004206 }
4207
Sunil Ravi77d572f2023-01-17 23:58:31 +00004208 if (params->mld && params->ap_mld_addr) {
4209 wpa_printf(MSG_DEBUG, " * MLD: link_id=%u, MLD addr=" MACSTR,
4210 params->mld_link_id, MAC2STR(params->ap_mld_addr));
4211
4212 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
4213 params->mld_link_id) ||
4214 nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
4215 params->ap_mld_addr))
4216 goto fail;
4217 }
4218
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004219 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220 msg = NULL;
4221 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004222 wpa_dbg(drv->ctx, MSG_DEBUG,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004223 "nl80211: MLME command failed (auth): count=%d ret=%d (%s)",
4224 count, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 count++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004226 if ((ret == -EALREADY || ret == -EEXIST) && count == 1 &&
4227 params->bssid && !params->local_state_change) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228 /*
4229 * mac80211 does not currently accept new
4230 * authentication if we are already authenticated. As a
4231 * workaround, force deauthentication and try again.
4232 */
4233 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4234 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07004235 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236 wpa_driver_nl80211_deauthenticate(
4237 bss, params->bssid,
4238 WLAN_REASON_PREV_AUTH_NOT_VALID);
4239 nlmsg_free(msg);
4240 goto retry;
4241 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004242
4243 if (ret == -ENOENT && params->freq && !is_retry) {
4244 /*
4245 * cfg80211 has likely expired the BSS entry even
4246 * though it was previously available in our internal
4247 * BSS table. To recover quickly, start a single
4248 * channel scan on the specified channel.
4249 */
4250 struct wpa_driver_scan_params scan;
4251 int freqs[2];
4252
4253 os_memset(&scan, 0, sizeof(scan));
4254 scan.num_ssids = 1;
4255 if (params->ssid) {
4256 scan.ssids[0].ssid = params->ssid;
4257 scan.ssids[0].ssid_len = params->ssid_len;
4258 }
4259 freqs[0] = params->freq;
4260 freqs[1] = 0;
4261 scan.freqs = freqs;
4262 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
4263 "channel scan to refresh cfg80211 BSS "
4264 "entry");
4265 ret = wpa_driver_nl80211_scan(bss, &scan);
4266 if (ret == 0) {
4267 nl80211_copy_auth_params(drv, params);
4268 drv->scan_for_auth = 1;
4269 }
4270 } else if (is_retry) {
4271 /*
4272 * Need to indicate this with an event since the return
4273 * value from the retry is not delivered to core code.
4274 */
4275 union wpa_event_data event;
4276 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
4277 "failed");
4278 os_memset(&event, 0, sizeof(event));
4279 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
4280 ETH_ALEN);
4281 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
4282 &event);
4283 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004284 } else {
4285 wpa_printf(MSG_DEBUG,
4286 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004287 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004289fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004290 nlmsg_free(msg);
4291 return ret;
4292}
4293
4294
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004295int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004296{
4297 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004298 struct i802_bss *bss = drv->first_bss;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004299 u8 ap_mld_addr[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004300 int i;
4301
4302 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
4303
4304 os_memset(&params, 0, sizeof(params));
4305 params.freq = drv->auth_freq;
4306 params.auth_alg = drv->auth_alg;
4307 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
4308 params.local_state_change = drv->auth_local_state_change;
4309 params.p2p = drv->auth_p2p;
4310
4311 if (!is_zero_ether_addr(drv->auth_bssid_))
4312 params.bssid = drv->auth_bssid_;
4313
4314 if (drv->auth_ssid_len) {
4315 params.ssid = drv->auth_ssid;
4316 params.ssid_len = drv->auth_ssid_len;
4317 }
4318
4319 params.ie = drv->auth_ie;
4320 params.ie_len = drv->auth_ie_len;
Hai Shalom60840252021-02-19 19:02:11 -08004321 params.auth_data = drv->auth_data;
4322 params.auth_data_len = drv->auth_data_len;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004323 params.mld = drv->auth_mld;
4324 params.mld_link_id = drv->auth_mld_link_id;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004325 if (drv->auth_mld) {
4326 os_memcpy(ap_mld_addr, drv->auth_ap_mld_addr, ETH_ALEN);
4327 params.ap_mld_addr = ap_mld_addr;
4328 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004329
4330 for (i = 0; i < 4; i++) {
4331 if (drv->auth_wep_key_len[i]) {
4332 params.wep_key[i] = drv->auth_wep_key[i];
4333 params.wep_key_len[i] = drv->auth_wep_key_len[i];
4334 }
4335 }
4336
4337 drv->retry_auth = 1;
4338 return wpa_driver_nl80211_authenticate(bss, &params);
4339}
4340
4341
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004342struct i802_link * nl80211_get_link(struct i802_bss *bss, s8 link_id)
4343{
Sunil Ravi99c035e2024-07-12 01:42:03 +00004344 if (link_id < 0 || link_id >= MAX_NUM_MLD_LINKS)
4345 return bss->flink;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004346
Sunil Ravi99c035e2024-07-12 01:42:03 +00004347 if (BIT(link_id) & bss->valid_links)
4348 return &bss->links[link_id];
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004349
4350 return bss->flink;
4351}
4352
4353
Sunil Ravic0f5d412024-09-11 22:12:49 +00004354u8 nl80211_get_link_id_from_link(struct i802_bss *bss, struct i802_link *link)
4355{
4356 u8 link_id;
4357
4358 if (link == bss->flink)
4359 return 0;
4360
4361 for_each_link(bss->valid_links, link_id) {
4362 if (&bss->links[link_id] == link)
4363 return link_id;
4364 }
4365
4366 return 0;
4367}
4368
4369
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004370static void nl80211_link_set_freq(struct i802_bss *bss, s8 link_id, int freq)
4371{
4372 struct i802_link *link = nl80211_get_link(bss, link_id);
4373
4374 link->freq = freq;
4375}
4376
4377
4378static int nl80211_get_link_freq(struct i802_bss *bss, const u8 *addr,
4379 bool bss_freq_debug)
4380{
Sunil Ravi99c035e2024-07-12 01:42:03 +00004381 u8 i;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004382
Sunil Ravi99c035e2024-07-12 01:42:03 +00004383 for_each_link(bss->valid_links, i) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004384 if (ether_addr_equal(bss->links[i].addr, addr)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004385 wpa_printf(MSG_DEBUG,
4386 "nl80211: Use link freq=%d for address "
4387 MACSTR,
4388 bss->links[i].freq, MAC2STR(addr));
4389 return bss->links[i].freq;
4390 }
4391 }
4392
4393 if (bss_freq_debug)
4394 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
4395 bss->flink->freq);
4396
4397 return bss->flink->freq;
4398}
4399
4400
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004401static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
4402 size_t data_len, int noack,
4403 unsigned int freq, int no_cck,
4404 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004405 unsigned int wait_time,
4406 const u16 *csa_offs,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004407 size_t csa_offs_len, int no_encrypt,
4408 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004410 struct wpa_driver_nl80211_data *drv = bss->drv;
4411 struct ieee80211_mgmt *mgmt;
Hai Shalomfdcde762020-04-02 11:19:20 -07004412 int encrypt = !no_encrypt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004413 u16 fc;
Hai Shalomfdcde762020-04-02 11:19:20 -07004414 int use_cookie = 1;
4415 int res;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004416 struct i802_link *link = nl80211_get_link(bss, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417
4418 mgmt = (struct ieee80211_mgmt *) data;
4419 fc = le_to_host16(mgmt->frame_control);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004420 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da=" MACSTR " sa=" MACSTR
4421 " bssid=" MACSTR
Hai Shalomfdcde762020-04-02 11:19:20 -07004422 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u no_encrypt=%d fc=0x%x (%s) nlmode=%d",
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004423 MAC2STR(mgmt->da), MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid),
4424 noack, freq, no_cck, offchanok, wait_time,
Hai Shalomfdcde762020-04-02 11:19:20 -07004425 no_encrypt, fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004426
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004427 if ((is_sta_interface(drv->nlmode) ||
4428 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004429 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4430 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
4431 /*
4432 * The use of last_mgmt_freq is a bit of a hack,
4433 * but it works due to the single-threaded nature
4434 * of wpa_supplicant.
4435 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07004436 if (freq == 0) {
4437 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
4438 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004439 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07004440 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004441 wait_time = 0;
4442 use_cookie = 0;
4443 no_cck = 1;
4444 offchanok = 1;
4445 goto send_frame_cmd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004446 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004447
4448 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004449 unsigned int link_freq = nl80211_get_link_freq(bss, mgmt->sa,
4450 !freq);
4451
4452 if (!freq)
4453 freq = link_freq;
4454
4455 if (freq == link_freq)
Hai Shalomfdcde762020-04-02 11:19:20 -07004456 wait_time = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004457
Hai Shalomfdcde762020-04-02 11:19:20 -07004458 goto send_frame_cmd;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07004459 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07004460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4462 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
4463 /*
4464 * Only one of the authentication frame types is encrypted.
4465 * In order for static WEP encryption to work properly (i.e.,
4466 * to not encrypt the frame), we need to tell mac80211 about
4467 * the frames that must not be encrypted.
4468 */
4469 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
4470 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
4471 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
4472 encrypt = 0;
4473 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004474
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004475 if ((is_sta_interface(drv->nlmode) ||
4476 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Hai Shalomfdcde762020-04-02 11:19:20 -07004477 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4478 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
Hai Shalom60840252021-02-19 19:02:11 -08004479 if (freq == 0 &&
4480 (drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
4481 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
4482 freq = nl80211_get_assoc_freq(drv);
4483 wpa_printf(MSG_DEBUG,
4484 "nl80211: send_mlme - Use assoc_freq=%u for external auth",
4485 freq);
4486 }
4487
4488 /* Allow off channel for PASN authentication */
4489 if (data_len >= IEEE80211_HDRLEN + 2 &&
4490 WPA_GET_LE16(data + IEEE80211_HDRLEN) == WLAN_AUTH_PASN &&
4491 !offchanok) {
4492 wpa_printf(MSG_DEBUG,
4493 "nl80211: send_mlme: allow off channel for PASN");
4494 offchanok = 1;
4495 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004496 }
4497
Hai Shalomc1a21442022-02-04 13:43:00 -08004498#ifdef CONFIG_PASN
4499 if (is_sta_interface(drv->nlmode) &&
4500 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4501 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_DEAUTH) {
4502 wpa_printf(MSG_DEBUG,
4503 "nl80211: send_mlme: allow Deauthentication frame for PASN");
4504
4505 use_cookie = 0;
4506 offchanok = 1;
4507 goto send_frame_cmd;
4508 }
4509#endif /* CONFIG_PASN */
4510
Hai Shalomfdcde762020-04-02 11:19:20 -07004511 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
4512 freq = nl80211_get_assoc_freq(drv);
4513 wpa_printf(MSG_DEBUG,
4514 "nl80211: send_mlme - Use assoc_freq=%u for IBSS",
4515 freq);
4516 }
4517 if (freq == 0) {
4518 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - Use bss->freq=%u",
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004519 link->freq);
4520 freq = link->freq;
Hai Shalomfdcde762020-04-02 11:19:20 -07004521 }
4522
Hai Shalomc1a21442022-02-04 13:43:00 -08004523 if (drv->use_monitor && is_ap_interface(drv->nlmode)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004524 wpa_printf(MSG_DEBUG,
4525 "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004526 freq, link->freq);
Hai Shalomfdcde762020-04-02 11:19:20 -07004527 return nl80211_send_monitor(drv, data, data_len, encrypt,
4528 noack);
4529 }
4530
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004531 if ((noack || WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
4532 WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION) &&
4533 link_id == NL80211_DRV_LINK_ID_NA)
Hai Shalomfdcde762020-04-02 11:19:20 -07004534 use_cookie = 0;
4535send_frame_cmd:
4536#ifdef CONFIG_TESTING_OPTIONS
4537 if (no_encrypt && !encrypt && !drv->use_monitor) {
4538 wpa_printf(MSG_DEBUG,
4539 "nl80211: Request to send an unencrypted frame - use a monitor interface for this");
4540 if (nl80211_create_monitor_interface(drv) < 0)
4541 return -1;
4542 res = nl80211_send_monitor(drv, data, data_len, encrypt,
4543 noack);
4544 nl80211_remove_monitor_interface(drv);
4545 return res;
4546 }
4547#endif /* CONFIG_TESTING_OPTIONS */
4548
4549 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame_cmd");
4550 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, data_len,
4551 use_cookie, no_cck, noack, offchanok,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004552 csa_offs, csa_offs_len, link_id);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004553 if (!res)
4554 drv->send_frame_link_id = link_id;
Hai Shalomfdcde762020-04-02 11:19:20 -07004555
4556 return res;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004557}
4558
4559
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004560static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
4561{
4562 u8 rates[NL80211_MAX_SUPP_RATES];
4563 u8 rates_len = 0;
4564 int i;
4565
4566 if (!basic_rates)
4567 return 0;
4568
4569 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
4570 rates[rates_len++] = basic_rates[i] / 5;
4571
4572 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
4573}
4574
4575
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004576static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
4577 int slot, int ht_opmode, int ap_isolate,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004578 const int *basic_rates, int link_id)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004579{
4580 struct wpa_driver_nl80211_data *drv = bss->drv;
4581 struct nl_msg *msg;
4582
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004583 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
4584 (cts >= 0 &&
4585 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
4586 (preamble >= 0 &&
4587 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
4588 (slot >= 0 &&
4589 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
4590 (ht_opmode >= 0 &&
4591 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
4592 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004593 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004594 nl80211_put_basic_rates(msg, basic_rates) ||
4595 (link_id != NL80211_DRV_LINK_ID_NA &&
4596 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004597 nlmsg_free(msg);
4598 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004599 }
4600
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004601 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004602}
4603
4604
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004605static int wpa_driver_nl80211_set_acl(void *priv,
4606 struct hostapd_acl_params *params)
4607{
4608 struct i802_bss *bss = priv;
4609 struct wpa_driver_nl80211_data *drv = bss->drv;
4610 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004611 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004612 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004613 int ret;
Hai Shalomc1a21442022-02-04 13:43:00 -08004614 size_t acl_nla_sz, acl_nlmsg_sz, nla_sz, nlmsg_sz;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004615
4616 if (!(drv->capa.max_acl_mac_addrs))
4617 return -ENOTSUP;
4618
4619 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
4620 return -ENOTSUP;
4621
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004622 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
4623 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
4624
Hai Shalomc1a21442022-02-04 13:43:00 -08004625 acl_nla_sz = nla_total_size(ETH_ALEN) * params->num_mac_acl;
4626 acl_nlmsg_sz = nlmsg_total_size(acl_nla_sz);
4627 acl = nlmsg_alloc_size(acl_nlmsg_sz);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004628 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004629 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004630 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004631 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
4632 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004633 return -ENOMEM;
4634 }
4635 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004636
Hai Shalomc1a21442022-02-04 13:43:00 -08004637 /*
4638 * genetlink message header (Length of user header is 0) +
4639 * u32 attr: NL80211_ATTR_IFINDEX +
4640 * u32 attr: NL80211_ATTR_ACL_POLICY +
4641 * nested acl attr
4642 */
4643 nla_sz = GENL_HDRLEN +
4644 nla_total_size(4) * 2 +
4645 nla_total_size(acl_nla_sz);
4646 nlmsg_sz = nlmsg_total_size(nla_sz);
4647 if (!(msg = nl80211_ifindex_msg_build(drv, nlmsg_alloc_size(nlmsg_sz),
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004648 bss->ifindex, 0,
Hai Shalomc1a21442022-02-04 13:43:00 -08004649 NL80211_CMD_SET_MAC_ACL)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004650 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
4651 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
4652 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
4653 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
4654 nlmsg_free(msg);
4655 nlmsg_free(acl);
4656 return -ENOMEM;
4657 }
4658 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004659
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004660 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004661 if (ret) {
4662 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
4663 ret, strerror(-ret));
4664 }
4665
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004666 return ret;
4667}
4668
4669
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004670static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
4671{
4672 if (beacon_int > 0) {
4673 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
4674 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
4675 beacon_int);
4676 }
4677
4678 return 0;
4679}
4680
4681
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004682static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
4683{
4684 if (dtim_period > 0) {
4685 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
4686 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
4687 }
4688
4689 return 0;
4690}
4691
4692
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004693#ifdef CONFIG_MESH
4694static int nl80211_set_mesh_config(void *priv,
4695 struct wpa_driver_mesh_bss_params *params)
4696{
4697 struct i802_bss *bss = priv;
4698 struct wpa_driver_nl80211_data *drv = bss->drv;
4699 struct nl_msg *msg;
4700 int ret;
4701
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004702 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MESH_CONFIG);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004703 if (!msg)
4704 return -1;
4705
4706 ret = nl80211_put_mesh_config(msg, params);
4707 if (ret < 0) {
4708 nlmsg_free(msg);
4709 return ret;
4710 }
4711
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004712 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004713 if (ret) {
4714 wpa_printf(MSG_ERROR,
4715 "nl80211: Mesh config set failed: %d (%s)",
4716 ret, strerror(-ret));
4717 return ret;
4718 }
4719 return 0;
4720}
4721#endif /* CONFIG_MESH */
4722
4723
Hai Shalom60840252021-02-19 19:02:11 -08004724static int nl80211_put_beacon_rate(struct nl_msg *msg, u64 flags, u64 flags2,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004725 struct wpa_driver_ap_params *params)
4726{
4727 struct nlattr *bands, *band;
4728 struct nl80211_txrate_vht vht_rate;
Hai Shalom60840252021-02-19 19:02:11 -08004729 struct nl80211_txrate_he he_rate;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004730
4731 if (!params->freq ||
4732 (params->beacon_rate == 0 &&
4733 params->rate_type == BEACON_RATE_LEGACY))
4734 return 0;
4735
4736 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
4737 if (!bands)
4738 return -1;
4739
4740 switch (params->freq->mode) {
4741 case HOSTAPD_MODE_IEEE80211B:
4742 case HOSTAPD_MODE_IEEE80211G:
4743 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
4744 break;
4745 case HOSTAPD_MODE_IEEE80211A:
Hai Shalom60840252021-02-19 19:02:11 -08004746 if (is_6ghz_freq(params->freq->freq))
4747 band = nla_nest_start(msg, NL80211_BAND_6GHZ);
4748 else
4749 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004750 break;
4751 case HOSTAPD_MODE_IEEE80211AD:
4752 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
4753 break;
4754 default:
4755 return 0;
4756 }
4757
4758 if (!band)
4759 return -1;
4760
4761 os_memset(&vht_rate, 0, sizeof(vht_rate));
Hai Shalom60840252021-02-19 19:02:11 -08004762 os_memset(&he_rate, 0, sizeof(he_rate));
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004763
4764 switch (params->rate_type) {
4765 case BEACON_RATE_LEGACY:
4766 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
4767 wpa_printf(MSG_INFO,
4768 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
4769 return -1;
4770 }
4771
4772 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004773 (u8) (params->beacon_rate / 5)) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004774 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
4775 (params->freq->vht_enabled &&
4776 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4777 &vht_rate)))
4778 return -1;
4779
4780 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
4781 params->beacon_rate);
4782 break;
4783 case BEACON_RATE_HT:
4784 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
4785 wpa_printf(MSG_INFO,
4786 "nl80211: Driver does not support setting Beacon frame rate (HT)");
4787 return -1;
4788 }
4789 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
4790 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
4791 (params->freq->vht_enabled &&
4792 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4793 &vht_rate)))
4794 return -1;
4795 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
4796 params->beacon_rate);
4797 break;
4798 case BEACON_RATE_VHT:
4799 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
4800 wpa_printf(MSG_INFO,
4801 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
4802 return -1;
4803 }
4804 vht_rate.mcs[0] = BIT(params->beacon_rate);
4805 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
4806 return -1;
4807 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
4808 return -1;
4809 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4810 &vht_rate))
4811 return -1;
4812 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
4813 params->beacon_rate);
4814 break;
Hai Shalom60840252021-02-19 19:02:11 -08004815 case BEACON_RATE_HE:
4816 if (!(flags2 & WPA_DRIVER_FLAGS2_BEACON_RATE_HE)) {
4817 wpa_printf(MSG_INFO,
4818 "nl80211: Driver does not support setting Beacon frame rate (HE)");
4819 return -1;
4820 }
4821 he_rate.mcs[0] = BIT(params->beacon_rate);
4822 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
4823 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
4824 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4825 &vht_rate) ||
4826 nla_put(msg, NL80211_TXRATE_HE, sizeof(he_rate), &he_rate))
4827 return -1;
4828 wpa_printf(MSG_DEBUG, " * beacon_rate = HE-MCS %u",
4829 params->beacon_rate);
4830 break;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004831 }
4832
4833 nla_nest_end(msg, band);
4834 nla_nest_end(msg, bands);
4835
4836 return 0;
4837}
4838
4839
4840static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
4841 int multicast_to_unicast)
4842{
4843 struct wpa_driver_nl80211_data *drv = bss->drv;
4844 struct nl_msg *msg;
4845 int ret;
4846
4847 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
4848 if (!msg ||
4849 (multicast_to_unicast &&
4850 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
4851 wpa_printf(MSG_ERROR,
4852 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
4853 bss->ifname);
4854 nlmsg_free(msg);
4855 return -ENOBUFS;
4856 }
4857
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004858 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004859
4860 switch (ret) {
4861 case 0:
4862 wpa_printf(MSG_DEBUG,
4863 "nl80211: multicast to unicast %s on interface %s",
4864 multicast_to_unicast ? "enabled" : "disabled",
4865 bss->ifname);
4866 break;
4867 case -EOPNOTSUPP:
4868 if (!multicast_to_unicast)
4869 break;
4870 wpa_printf(MSG_INFO,
4871 "nl80211: multicast to unicast not supported on interface %s",
4872 bss->ifname);
4873 break;
4874 default:
4875 wpa_printf(MSG_ERROR,
4876 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
4877 multicast_to_unicast ? "enabling" : "disabling",
4878 ret, strerror(-ret), bss->ifname);
4879 break;
4880 }
4881
4882 return ret;
4883}
4884
4885
Hai Shalom60840252021-02-19 19:02:11 -08004886#ifdef CONFIG_SAE
Sunil Ravi77d572f2023-01-17 23:58:31 +00004887static int nl80211_put_sae_pwe(struct nl_msg *msg, enum sae_pwe pwe)
Hai Shalom60840252021-02-19 19:02:11 -08004888{
4889 u8 sae_pwe;
4890
4891 wpa_printf(MSG_DEBUG, "nl802111: sae_pwe=%d", pwe);
Sunil Ravi77d572f2023-01-17 23:58:31 +00004892 if (pwe == SAE_PWE_HUNT_AND_PECK)
Hai Shalom60840252021-02-19 19:02:11 -08004893 sae_pwe = NL80211_SAE_PWE_HUNT_AND_PECK;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004894 else if (pwe == SAE_PWE_HASH_TO_ELEMENT)
Hai Shalom60840252021-02-19 19:02:11 -08004895 sae_pwe = NL80211_SAE_PWE_HASH_TO_ELEMENT;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004896 else if (pwe == SAE_PWE_BOTH)
Hai Shalom60840252021-02-19 19:02:11 -08004897 sae_pwe = NL80211_SAE_PWE_BOTH;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004898 else if (pwe == SAE_PWE_FORCE_HUNT_AND_PECK)
Hai Shalom60840252021-02-19 19:02:11 -08004899 return 0; /* special test mode */
4900 else
4901 return -1;
4902 if (nla_put_u8(msg, NL80211_ATTR_SAE_PWE, sae_pwe))
4903 return -1;
4904
4905 return 0;
4906}
4907#endif /* CONFIG_SAE */
4908
4909
4910#ifdef CONFIG_FILS
4911static int nl80211_fils_discovery(struct i802_bss *bss, struct nl_msg *msg,
4912 struct wpa_driver_ap_params *params)
4913{
4914 struct nlattr *attr;
4915
4916 if (!bss->drv->fils_discovery) {
4917 wpa_printf(MSG_ERROR,
4918 "nl80211: Driver does not support FILS Discovery frame transmission for %s",
4919 bss->ifname);
4920 return -1;
4921 }
4922
4923 attr = nla_nest_start(msg, NL80211_ATTR_FILS_DISCOVERY);
4924 if (!attr ||
4925 nla_put_u32(msg, NL80211_FILS_DISCOVERY_ATTR_INT_MIN,
4926 params->fd_min_int) ||
4927 nla_put_u32(msg, NL80211_FILS_DISCOVERY_ATTR_INT_MAX,
4928 params->fd_max_int) ||
4929 (params->fd_frame_tmpl &&
4930 nla_put(msg, NL80211_FILS_DISCOVERY_ATTR_TMPL,
4931 params->fd_frame_tmpl_len, params->fd_frame_tmpl)))
4932 return -1;
4933
4934 nla_nest_end(msg, attr);
4935 return 0;
4936}
4937#endif /* CONFIG_FILS */
4938
4939
4940#ifdef CONFIG_IEEE80211AX
Sunil Ravi77d572f2023-01-17 23:58:31 +00004941
Hai Shalom60840252021-02-19 19:02:11 -08004942static int nl80211_unsol_bcast_probe_resp(struct i802_bss *bss,
4943 struct nl_msg *msg,
Sunil Ravi7f769292024-07-23 22:21:32 +00004944 struct unsol_bcast_probe_resp *ubpr)
Hai Shalom60840252021-02-19 19:02:11 -08004945{
4946 struct nlattr *attr;
4947
4948 if (!bss->drv->unsol_bcast_probe_resp) {
4949 wpa_printf(MSG_ERROR,
4950 "nl80211: Driver does not support unsolicited broadcast Probe Response frame transmission for %s",
4951 bss->ifname);
4952 return -1;
4953 }
4954
4955 wpa_printf(MSG_DEBUG,
4956 "nl80211: Unsolicited broadcast Probe Response frame interval: %u",
Sunil Ravi7f769292024-07-23 22:21:32 +00004957 ubpr->unsol_bcast_probe_resp_interval);
Hai Shalom60840252021-02-19 19:02:11 -08004958 attr = nla_nest_start(msg, NL80211_ATTR_UNSOL_BCAST_PROBE_RESP);
4959 if (!attr ||
4960 nla_put_u32(msg, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT,
Sunil Ravi7f769292024-07-23 22:21:32 +00004961 ubpr->unsol_bcast_probe_resp_interval) ||
4962 (ubpr->unsol_bcast_probe_resp_tmpl &&
Hai Shalom60840252021-02-19 19:02:11 -08004963 nla_put(msg, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL,
Sunil Ravi7f769292024-07-23 22:21:32 +00004964 ubpr->unsol_bcast_probe_resp_tmpl_len,
4965 ubpr->unsol_bcast_probe_resp_tmpl)))
Hai Shalom60840252021-02-19 19:02:11 -08004966 return -1;
4967
4968 nla_nest_end(msg, attr);
4969 return 0;
4970}
Sunil Ravi77d572f2023-01-17 23:58:31 +00004971
4972
4973static int nl80211_mbssid(struct nl_msg *msg,
4974 struct wpa_driver_ap_params *params)
4975{
4976 struct nlattr *config, *elems;
4977 int ifidx;
4978
4979 if (!params->mbssid_tx_iface)
4980 return 0;
4981
4982 config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
4983 if (!config ||
4984 nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_INDEX,
4985 params->mbssid_index))
4986 return -1;
4987
4988 if (params->mbssid_tx_iface) {
4989 ifidx = if_nametoindex(params->mbssid_tx_iface);
4990 if (ifidx <= 0 ||
4991 nla_put_u32(msg, NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX,
4992 ifidx))
4993 return -1;
4994 }
4995
4996 if (params->ema && nla_put_flag(msg, NL80211_MBSSID_CONFIG_ATTR_EMA))
4997 return -1;
4998
4999 nla_nest_end(msg, config);
5000
5001 if (params->mbssid_elem_count && params->mbssid_elem_len &&
5002 params->mbssid_elem_offset && *params->mbssid_elem_offset) {
5003 u8 i, **offs = params->mbssid_elem_offset;
5004
5005 elems = nla_nest_start(msg, NL80211_ATTR_MBSSID_ELEMS);
5006 if (!elems)
5007 return -1;
5008
5009 for (i = 0; i < params->mbssid_elem_count - 1; i++) {
5010 if (nla_put(msg, i + 1, offs[i + 1] - offs[i], offs[i]))
5011 return -1;
5012 }
5013
5014 if (nla_put(msg, i + 1,
5015 *offs + params->mbssid_elem_len - offs[i],
5016 offs[i]))
5017 return -1;
5018
5019 nla_nest_end(msg, elems);
5020 }
5021
Sunil Ravi640215c2023-06-28 23:08:09 +00005022 if (!params->ema)
5023 return 0;
5024
5025 if (params->rnr_elem_count && params->rnr_elem_len &&
5026 params->rnr_elem_offset && *params->rnr_elem_offset) {
5027 u8 i, **offs = params->rnr_elem_offset;
5028
5029 elems = nla_nest_start(msg, NL80211_ATTR_EMA_RNR_ELEMS);
5030 if (!elems)
5031 return -1;
5032
5033 for (i = 0; i < params->rnr_elem_count - 1; i++) {
5034 if (nla_put(msg, i + 1, offs[i + 1] - offs[i], offs[i]))
5035 return -1;
5036 }
5037
5038 if (nla_put(msg, i + 1, *offs + params->rnr_elem_len - offs[i],
5039 offs[i]))
5040 return -1;
5041 nla_nest_end(msg, elems);
5042 }
5043
Sunil Ravi77d572f2023-01-17 23:58:31 +00005044 return 0;
5045}
5046
Hai Shalom60840252021-02-19 19:02:11 -08005047#endif /* CONFIG_IEEE80211AX */
5048
5049
Sunil Ravi640215c2023-06-28 23:08:09 +00005050#ifdef CONFIG_DRIVER_NL80211_QCA
5051static void qca_set_allowed_ap_freqs(struct wpa_driver_nl80211_data *drv,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005052 const int *freqs, int num_freqs,
5053 int link_id)
Sunil Ravi640215c2023-06-28 23:08:09 +00005054{
5055 struct nl_msg *msg;
5056 struct nlattr *params, *freqs_list;
5057 int i, ret;
5058
5059 if (!drv->set_wifi_conf_vendor_cmd_avail || !drv->qca_ap_allowed_freqs)
5060 return;
5061
5062 wpa_printf(MSG_DEBUG, "nl80211: Set AP allowed frequency list");
5063
5064 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
5065 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
5066 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
5067 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
5068 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)))
5069 goto err;
5070
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005071 if (link_id != NL80211_DRV_LINK_ID_NA &&
5072 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_MLO_LINK_ID, link_id))
5073 goto err;
5074
Sunil Ravi640215c2023-06-28 23:08:09 +00005075 freqs_list = nla_nest_start(
5076 msg, QCA_WLAN_VENDOR_ATTR_CONFIG_AP_ALLOWED_FREQ_LIST);
5077 if (!freqs_list)
5078 goto err;
5079
5080 for (i = 0; i < num_freqs; i++) {
5081 if (nla_put_u32(msg, i, freqs[i]))
5082 goto err;
5083 }
5084
5085 nla_nest_end(msg, freqs_list);
5086 nla_nest_end(msg, params);
5087
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005088 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi640215c2023-06-28 23:08:09 +00005089 if (ret)
5090 wpa_printf(MSG_ERROR,
5091 "nl80211: Failed set AP alllowed frequency list: %d (%s)",
5092 ret, strerror(-ret));
5093
5094 return;
5095err:
5096 nlmsg_free(msg);
5097}
5098#endif /* CONFIG_DRIVER_NL80211_QCA */
5099
5100
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005101static int nl80211_put_freq_params(struct nl_msg *msg,
5102 const struct hostapd_freq_params *freq)
5103{
5104 enum hostapd_hw_mode hw_mode;
5105 int is_24ghz;
5106 u8 channel;
5107
5108 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
5109 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
5110 return -ENOBUFS;
5111
5112 wpa_printf(MSG_DEBUG, " * eht_enabled=%d", freq->eht_enabled);
5113 wpa_printf(MSG_DEBUG, " * he_enabled=%d", freq->he_enabled);
5114 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
5115 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
5116 wpa_printf(MSG_DEBUG, " * radar_background=%d",
5117 freq->radar_background);
5118
5119 hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
5120 is_24ghz = hw_mode == HOSTAPD_MODE_IEEE80211G ||
5121 hw_mode == HOSTAPD_MODE_IEEE80211B;
5122
5123 if (freq->vht_enabled ||
5124 ((freq->he_enabled || freq->eht_enabled) && !is_24ghz)) {
5125 enum nl80211_chan_width cw;
5126
5127 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
5128 switch (freq->bandwidth) {
5129 case 20:
5130 cw = NL80211_CHAN_WIDTH_20;
5131 break;
5132 case 40:
5133 cw = NL80211_CHAN_WIDTH_40;
5134 break;
5135 case 80:
5136 if (freq->center_freq2)
5137 cw = NL80211_CHAN_WIDTH_80P80;
5138 else
5139 cw = NL80211_CHAN_WIDTH_80;
5140 break;
5141 case 160:
5142 cw = NL80211_CHAN_WIDTH_160;
5143 break;
5144 case 320:
5145 cw = NL80211_CHAN_WIDTH_320;
5146 break;
5147 default:
5148 return -EINVAL;
5149 }
5150
5151 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
5152 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
5153 freq->center_freq1);
5154 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
5155 freq->center_freq2);
5156 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
5157 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
5158 freq->center_freq1) ||
5159 (freq->center_freq2 &&
5160 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
5161 freq->center_freq2)))
5162 return -ENOBUFS;
5163 } else if (freq->ht_enabled) {
5164 enum nl80211_channel_type ct;
5165
5166 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
5167 freq->sec_channel_offset);
5168 switch (freq->sec_channel_offset) {
5169 case -1:
5170 ct = NL80211_CHAN_HT40MINUS;
5171 break;
5172 case 1:
5173 ct = NL80211_CHAN_HT40PLUS;
5174 break;
5175 default:
5176 ct = NL80211_CHAN_HT20;
5177 break;
5178 }
5179
5180 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
5181 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
5182 return -ENOBUFS;
5183 } else if (freq->edmg.channels && freq->edmg.bw_config) {
5184 wpa_printf(MSG_DEBUG,
5185 " * EDMG configuration: channels=0x%x bw_config=%d",
5186 freq->edmg.channels, freq->edmg.bw_config);
5187 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_CHANNELS,
5188 freq->edmg.channels) ||
5189 nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_BW_CONFIG,
5190 freq->edmg.bw_config))
5191 return -1;
5192 } else {
5193 wpa_printf(MSG_DEBUG, " * channel_type=%d",
5194 NL80211_CHAN_NO_HT);
5195 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5196 NL80211_CHAN_NO_HT))
5197 return -ENOBUFS;
5198 }
5199 if (freq->radar_background &&
5200 nla_put_flag(msg, NL80211_ATTR_RADAR_BACKGROUND))
5201 return -ENOBUFS;
5202
5203 return 0;
5204}
5205
5206
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005207static int wpa_driver_nl80211_set_ap(void *priv,
5208 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209{
5210 struct i802_bss *bss = priv;
5211 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005212 struct i802_link *link = bss->flink;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213 struct nl_msg *msg;
5214 u8 cmd = NL80211_CMD_NEW_BEACON;
Hai Shalom74f70d42019-02-11 14:42:39 -08005215 int ret = -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005216 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005217 int num_suites;
Hai Shalomfdcde762020-04-02 11:19:20 -07005218 u32 suites[20], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005219 u32 ver;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005220#ifdef CONFIG_MESH
5221 struct wpa_driver_mesh_bss_params mesh_params;
5222#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005223
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005224 if (params->mld_ap) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00005225 if (!nl80211_link_valid(bss->valid_links,
5226 params->mld_link_id)) {
5227 wpa_printf(MSG_DEBUG,
5228 "nl80211: Link ID=%u invalid (valid: 0x%04x)",
5229 params->mld_link_id, bss->valid_links);
Sunil Ravi88611412024-06-28 17:34:56 +00005230 return -EINVAL;
5231 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00005232
5233 link = nl80211_get_link(bss, params->mld_link_id);
5234 } else if (bss->valid_links) {
5235 wpa_printf(MSG_DEBUG, "nl80211: MLD configuration expected");
5236 return -EINVAL;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005237 }
5238
5239 beacon_set = params->reenable ? 0 : link->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
5242 beacon_set);
5243 if (beacon_set)
5244 cmd = NL80211_CMD_SET_BEACON;
Paul Stewart092955c2017-02-06 09:13:09 -08005245 else if (!drv->device_ap_sme && !drv->use_monitor &&
5246 !nl80211_get_wiphy_data_ap(bss))
5247 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005249 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
5250 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005251 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
5252 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005253 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005254 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08005255 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
5256 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005257 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Hai Shalom74f70d42019-02-11 14:42:39 -08005258 wpa_printf(MSG_DEBUG, "nl80211: ssid=%s",
5259 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005260 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
5261 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
5262 params->head) ||
5263 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
5264 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005265 nl80211_put_beacon_int(msg, params->beacon_int) ||
Hai Shalom60840252021-02-19 19:02:11 -08005266 nl80211_put_beacon_rate(msg, drv->capa.flags, drv->capa.flags2,
5267 params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005268 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005269 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5270 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005271
5272 if (params->mld_ap) {
5273 wpa_printf(MSG_DEBUG, "nl80211: link_id=%u",
5274 params->mld_link_id);
5275
5276 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
Sunil Ravi99c035e2024-07-12 01:42:03 +00005277 params->mld_link_id))
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005278 goto fail;
5279
Sunil Ravi99c035e2024-07-12 01:42:03 +00005280 if (params->freq)
5281 nl80211_link_set_freq(bss, params->mld_link_id,
5282 params->freq->freq);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005283 }
5284
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005285 if (params->proberesp && params->proberesp_len) {
5286 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
5287 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005288 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5289 params->proberesp))
5290 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005291 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005292 switch (params->hide_ssid) {
5293 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005294 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005295 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
5296 NL80211_HIDDEN_SSID_NOT_IN_USE))
5297 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005298 break;
5299 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005300 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005301 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
5302 NL80211_HIDDEN_SSID_ZERO_LEN))
5303 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005304 break;
5305 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005306 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005307 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
5308 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
5309 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005310 break;
5311 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005312 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005313 if (params->privacy &&
5314 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
5315 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005316 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005317 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5318 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5319 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005320 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
5321 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
5322 NL80211_AUTHTYPE_SHARED_KEY))
5323 goto fail;
5324 } else {
5325 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
5326 NL80211_AUTHTYPE_OPEN_SYSTEM))
5327 goto fail;
5328 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005329
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005330 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005331 ver = 0;
5332 if (params->wpa_version & WPA_PROTO_WPA)
5333 ver |= NL80211_WPA_VERSION_1;
5334 if (params->wpa_version & WPA_PROTO_RSN)
5335 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005336 if (ver &&
5337 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5338 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005339
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005340 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
5341 params->key_mgmt_suites);
Hai Shalomfdcde762020-04-02 11:19:20 -07005342 num_suites = wpa_key_mgmt_to_suites(params->key_mgmt_suites,
5343 suites, ARRAY_SIZE(suites));
Sunil Ravi7f769292024-07-23 22:21:32 +00005344 if ((unsigned int) num_suites > drv->capa.max_num_akms)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07005345 wpa_printf(MSG_DEBUG,
Sunil Ravi7f769292024-07-23 22:21:32 +00005346 "nl80211: Not enough room for all AKM suites (num_suites=%d > %d)",
5347 num_suites, drv->capa.max_num_akms);
Hai Shalomfdcde762020-04-02 11:19:20 -07005348 else if (num_suites &&
5349 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
5350 suites))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005351 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005352
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005353 if (wpa_key_mgmt_wpa_psk_no_sae(params->key_mgmt_suites) &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005354 (drv->capa.flags2 & WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005355 params->psk_len &&
5356 nla_put(msg, NL80211_ATTR_PMK, params->psk_len, params->psk))
5357 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005358
5359 if (wpa_key_mgmt_sae(params->key_mgmt_suites) &&
5360 (drv->capa.flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_AP) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005361 params->sae_password &&
5362 nla_put(msg, NL80211_ATTR_SAE_PASSWORD,
5363 os_strlen(params->sae_password), params->sae_password))
5364 goto fail;
5365
5366 if (nl80211_put_control_port(drv, msg) < 0)
5367 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005368
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005369 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005370 (!params->pairwise_ciphers ||
5371 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005372 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005373 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005374
Sunil Ravia04bd252022-05-02 22:54:18 -07005375 if (drv->device_ap_sme) {
5376 u32 flags = 0;
5377
Sunil Ravi89eba102022-09-13 21:04:37 -07005378 if (params->key_mgmt_suites & (WPA_KEY_MGMT_SAE |
5379 WPA_KEY_MGMT_SAE_EXT_KEY)) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005380 /* Add the previously used flag attribute to support
5381 * older kernel versions and the newer flag bit for
5382 * newer kernels. */
5383 if (nla_put_flag(msg,
5384 NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5385 goto fail;
5386 flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
5387 }
5388
5389 flags |= NL80211_AP_SETTINGS_SA_QUERY_OFFLOAD_SUPPORT;
5390
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005391 if (nl80211_attr_supported(drv,
5392 NL80211_ATTR_AP_SETTINGS_FLAGS) &&
5393 nla_put_u32(msg, NL80211_ATTR_AP_SETTINGS_FLAGS, flags))
Sunil Ravia04bd252022-05-02 22:54:18 -07005394 goto fail;
5395 }
Hai Shalom5f92bc92019-04-18 11:54:11 -07005396
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005397 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
5398 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005399 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
5400 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005401 if (num_suites &&
5402 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5403 num_suites * sizeof(u32), suites))
5404 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005405
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005406 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
5407 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005408 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005409 if (suite &&
5410 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
5411 goto fail;
5412
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005413 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005414 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
5415 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005416 if (nla_put(msg, NL80211_ATTR_IE,
5417 wpabuf_len(params->beacon_ies),
5418 wpabuf_head(params->beacon_ies)))
5419 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005420 }
5421 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005422 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
5423 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005424 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
5425 wpabuf_len(params->proberesp_ies),
5426 wpabuf_head(params->proberesp_ies)))
5427 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005428 }
5429 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005430 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
5431 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005432 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
5433 wpabuf_len(params->assocresp_ies),
5434 wpabuf_head(params->assocresp_ies)))
5435 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005436 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005437
Dmitry Shmidt04949592012-07-19 12:16:46 -07005438 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005439 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
5440 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005441 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
5442 params->ap_max_inactivity))
5443 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005444 }
5445
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005446#ifdef CONFIG_P2P
5447 if (params->p2p_go_ctwindow > 0) {
5448 if (drv->p2p_go_ctwindow_supported) {
5449 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
5450 params->p2p_go_ctwindow);
5451 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
5452 params->p2p_go_ctwindow))
5453 goto fail;
5454 } else {
5455 wpa_printf(MSG_INFO,
5456 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
5457 }
5458 }
5459#endif /* CONFIG_P2P */
5460
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005461 if (params->pbss) {
5462 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
5463 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5464 goto fail;
5465 }
5466
Hai Shalom74f70d42019-02-11 14:42:39 -08005467 if (params->ftm_responder) {
5468 struct nlattr *ftm;
5469
5470 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_FTM_RESPONDER)) {
5471 ret = -ENOTSUP;
5472 goto fail;
5473 }
5474
5475 ftm = nla_nest_start(msg, NL80211_ATTR_FTM_RESPONDER);
5476 if (!ftm ||
5477 nla_put_flag(msg, NL80211_FTM_RESP_ATTR_ENABLED) ||
5478 (params->lci &&
5479 nla_put(msg, NL80211_FTM_RESP_ATTR_LCI,
5480 wpabuf_len(params->lci),
5481 wpabuf_head(params->lci))) ||
5482 (params->civic &&
5483 nla_put(msg, NL80211_FTM_RESP_ATTR_CIVICLOC,
5484 wpabuf_len(params->civic),
5485 wpabuf_head(params->civic))))
5486 goto fail;
5487 nla_nest_end(msg, ftm);
5488 }
5489
Sunil Ravi99c035e2024-07-12 01:42:03 +00005490 if (params->freq && nl80211_put_freq_params(msg, params->freq) < 0)
5491 goto fail;
5492
Hai Shalomc3565922019-10-28 11:58:20 -07005493#ifdef CONFIG_IEEE80211AX
Hai Shalom60840252021-02-19 19:02:11 -08005494 if (params->he_spr_ctrl) {
Hai Shalomc3565922019-10-28 11:58:20 -07005495 struct nlattr *spr;
5496
5497 spr = nla_nest_start(msg, NL80211_ATTR_HE_OBSS_PD);
Hai Shalom60840252021-02-19 19:02:11 -08005498 wpa_printf(MSG_DEBUG, "nl80211: he_spr_ctrl=0x%x",
5499 params->he_spr_ctrl);
Hai Shalomc3565922019-10-28 11:58:20 -07005500
Hai Shalomfdcde762020-04-02 11:19:20 -07005501 if (!spr ||
Hai Shalom60840252021-02-19 19:02:11 -08005502 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_SR_CTRL,
5503 params->he_spr_ctrl) ||
5504 ((params->he_spr_ctrl &
5505 SPATIAL_REUSE_NON_SRG_OFFSET_PRESENT) &&
5506 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET,
5507 params->he_spr_non_srg_obss_pd_max_offset)))
5508 goto fail;
5509
5510 if ((params->he_spr_ctrl &
5511 SPATIAL_REUSE_SRG_INFORMATION_PRESENT) &&
5512 (nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET,
5513 params->he_spr_srg_obss_pd_min_offset) ||
5514 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET,
5515 params->he_spr_srg_obss_pd_max_offset) ||
5516 nla_put(msg, NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP,
5517 sizeof(params->he_spr_bss_color_bitmap),
5518 params->he_spr_bss_color_bitmap) ||
5519 nla_put(msg, NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP,
5520 sizeof(params->he_spr_partial_bssid_bitmap),
5521 params->he_spr_partial_bssid_bitmap)))
Hai Shalomc3565922019-10-28 11:58:20 -07005522 goto fail;
5523
5524 nla_nest_end(msg, spr);
5525 }
Hai Shalomfdcde762020-04-02 11:19:20 -07005526
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005527 if (params->freq && params->freq->he_enabled &&
5528 nl80211_attr_supported(drv, NL80211_ATTR_HE_BSS_COLOR)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005529 struct nlattr *bss_color;
5530
5531 bss_color = nla_nest_start(msg, NL80211_ATTR_HE_BSS_COLOR);
5532 if (!bss_color ||
5533 (params->he_bss_color_disabled &&
5534 nla_put_flag(msg, NL80211_HE_BSS_COLOR_ATTR_DISABLED)) ||
5535 (params->he_bss_color_partial &&
5536 nla_put_flag(msg, NL80211_HE_BSS_COLOR_ATTR_PARTIAL)) ||
5537 nla_put_u8(msg, NL80211_HE_BSS_COLOR_ATTR_COLOR,
5538 params->he_bss_color))
5539 goto fail;
5540 nla_nest_end(msg, bss_color);
5541 }
5542
5543 if (params->twt_responder) {
5544 wpa_printf(MSG_DEBUG, "nl80211: twt_responder=%d",
5545 params->twt_responder);
5546 if (nla_put_flag(msg, NL80211_ATTR_TWT_RESPONDER))
5547 goto fail;
5548 }
Hai Shalom60840252021-02-19 19:02:11 -08005549
Sunil Ravi7f769292024-07-23 22:21:32 +00005550 if (params->ubpr.unsol_bcast_probe_resp_interval &&
5551 nl80211_unsol_bcast_probe_resp(bss, msg, &params->ubpr) < 0)
Hai Shalom60840252021-02-19 19:02:11 -08005552 goto fail;
Sunil Ravi77d572f2023-01-17 23:58:31 +00005553
5554 if (nl80211_mbssid(msg, params) < 0)
5555 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07005556#endif /* CONFIG_IEEE80211AX */
5557
Hai Shalom60840252021-02-19 19:02:11 -08005558#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -07005559 if (wpa_key_mgmt_sae(params->key_mgmt_suites) &&
Hai Shalom60840252021-02-19 19:02:11 -08005560 nl80211_put_sae_pwe(msg, params->sae_pwe) < 0)
5561 goto fail;
5562#endif /* CONFIG_SAE */
5563
5564#ifdef CONFIG_FILS
5565 if (params->fd_max_int && nl80211_fils_discovery(bss, msg, params) < 0)
5566 goto fail;
5567#endif /* CONFIG_FILS */
5568
Sunil Ravi036cec52023-03-29 11:35:17 -07005569 if (params->punct_bitmap) {
5570 wpa_printf(MSG_DEBUG, "nl80211: Puncturing bitmap=0x%04x",
5571 params->punct_bitmap);
5572 if (nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP,
5573 params->punct_bitmap))
5574 goto fail;
5575 }
5576
Sunil Ravi640215c2023-06-28 23:08:09 +00005577#ifdef CONFIG_DRIVER_NL80211_QCA
5578 if (cmd == NL80211_CMD_NEW_BEACON && params->allowed_freqs)
5579 qca_set_allowed_ap_freqs(drv, params->allowed_freqs,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005580 int_array_len(params->allowed_freqs),
5581 params->mld_ap ? params->mld_link_id :
5582 NL80211_DRV_LINK_ID_NA);
Sunil Ravi640215c2023-06-28 23:08:09 +00005583#endif /* CONFIG_DRIVER_NL80211_QCA */
5584
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005585 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
5586 goto fail;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005587 ret = send_and_recv(drv, bss->nl_connect, msg, NULL, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005588 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589 if (ret) {
5590 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5591 ret, strerror(-ret));
5592 } else {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005593 link->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005594 nl80211_set_bss(bss, params->cts_protect, params->preamble,
5595 params->short_slot_time, params->ht_opmode,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005596 params->isolate, params->basic_rates,
5597 params->mld_ap ? params->mld_link_id :
5598 NL80211_DRV_LINK_ID_NA);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08005599 nl80211_set_multicast_to_unicast(bss,
5600 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005601 if (beacon_set && params->freq &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005602 params->freq->bandwidth != link->bandwidth) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005603 wpa_printf(MSG_DEBUG,
5604 "nl80211: Update BSS %s bandwidth: %d -> %d",
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005605 bss->ifname, link->bandwidth,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005606 params->freq->bandwidth);
5607 ret = nl80211_set_channel(bss, params->freq, 1);
5608 if (ret) {
5609 wpa_printf(MSG_DEBUG,
5610 "nl80211: Frequency set failed: %d (%s)",
5611 ret, strerror(-ret));
5612 } else {
5613 wpa_printf(MSG_DEBUG,
5614 "nl80211: Frequency set succeeded for ht2040 coex");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005615 link->bandwidth = params->freq->bandwidth;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005616 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005617 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005618 /*
5619 * cfg80211 updates the driver on frequence change in AP
5620 * mode only at the point when beaconing is started, so
5621 * set the initial value here.
5622 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005623 link->bandwidth = params->freq->bandwidth;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005624 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005626
5627#ifdef CONFIG_MESH
5628 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
5629 os_memset(&mesh_params, 0, sizeof(mesh_params));
5630 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
5631 mesh_params.ht_opmode = params->ht_opmode;
5632 ret = nl80211_set_mesh_config(priv, &mesh_params);
5633 if (ret < 0)
5634 return ret;
5635 }
5636#endif /* CONFIG_MESH */
5637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005639fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005640 nlmsg_free(msg);
Hai Shalom74f70d42019-02-11 14:42:39 -08005641 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005642}
5643
5644
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005645static int nl80211_set_channel(struct i802_bss *bss,
5646 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005647{
5648 struct wpa_driver_nl80211_data *drv = bss->drv;
5649 struct nl_msg *msg;
5650 int ret;
5651
5652 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07005653 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, he_enabled=%d, eht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
5654 freq->freq, freq->ht_enabled, freq->vht_enabled,
5655 freq->he_enabled, freq->eht_enabled, freq->bandwidth,
5656 freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005657
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005658 msg = nl80211_bss_msg(bss, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005659 NL80211_CMD_SET_WIPHY);
5660 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
5661 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005662 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005663 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005664
Sunil Ravi99c035e2024-07-12 01:42:03 +00005665 if (nl80211_link_valid(bss->valid_links, freq->link_id)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005666 wpa_printf(MSG_DEBUG, "nl80211: Set link_id=%u for freq",
5667 freq->link_id);
5668
5669 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, freq->link_id)) {
5670 nlmsg_free(msg);
5671 return -ENOBUFS;
5672 }
5673 }
5674
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005675 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005676 if (ret == 0) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005677 nl80211_link_set_freq(bss, freq->link_id, freq->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005678 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005679 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005681 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005682 return -1;
5683}
5684
5685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005686static u32 sta_flags_nl80211(int flags)
5687{
5688 u32 f = 0;
5689
5690 if (flags & WPA_STA_AUTHORIZED)
5691 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
5692 if (flags & WPA_STA_WMM)
5693 f |= BIT(NL80211_STA_FLAG_WME);
5694 if (flags & WPA_STA_SHORT_PREAMBLE)
5695 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
5696 if (flags & WPA_STA_MFP)
5697 f |= BIT(NL80211_STA_FLAG_MFP);
5698 if (flags & WPA_STA_TDLS_PEER)
5699 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005700 if (flags & WPA_STA_AUTHENTICATED)
5701 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005702 if (flags & WPA_STA_ASSOCIATED)
5703 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005704
5705 return f;
5706}
5707
5708
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005709#ifdef CONFIG_MESH
5710static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
5711{
5712 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005713 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005714 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005715 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005716 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005717 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005718 return NL80211_PLINK_OPN_RCVD;
5719 case PLINK_CNF_RCVD:
5720 return NL80211_PLINK_CNF_RCVD;
5721 case PLINK_ESTAB:
5722 return NL80211_PLINK_ESTAB;
5723 case PLINK_HOLDING:
5724 return NL80211_PLINK_HOLDING;
5725 case PLINK_BLOCKED:
5726 return NL80211_PLINK_BLOCKED;
5727 default:
5728 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
5729 state);
5730 }
5731 return -1;
5732}
5733#endif /* CONFIG_MESH */
5734
5735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005736static int wpa_driver_nl80211_sta_add(void *priv,
5737 struct hostapd_sta_add_params *params)
5738{
5739 struct i802_bss *bss = priv;
5740 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005741 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005742 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743 int ret = -ENOBUFS;
Sunil Ravi036cec52023-03-29 11:35:17 -07005744 u8 cmd;
5745 const char *cmd_string;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005747 if ((params->flags & WPA_STA_TDLS_PEER) &&
5748 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
5749 return -EOPNOTSUPP;
5750
Sunil Ravi036cec52023-03-29 11:35:17 -07005751 if (params->mld_link_sta) {
5752 cmd = params->set ? NL80211_CMD_MODIFY_LINK_STA :
5753 NL80211_CMD_ADD_LINK_STA;
5754 cmd_string = params->set ? "NL80211_CMD_MODIFY_LINK_STA" :
5755 "NL80211_CMD_ADD_LINK_STA";
5756 } else {
5757 cmd = params->set ? NL80211_CMD_SET_STATION :
5758 NL80211_CMD_NEW_STATION;
5759 cmd_string = params->set ? "NL80211_CMD_SET_STATION" :
5760 "NL80211_CMD_NEW_STATION";
5761 }
5762
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005763 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
Sunil Ravi036cec52023-03-29 11:35:17 -07005764 cmd_string, MAC2STR(params->addr));
5765 msg = nl80211_bss_msg(bss, 0, cmd);
5766 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005767 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005768
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005769 /*
5770 * Set the below properties only in one of the following cases:
5771 * 1. New station is added, already associated.
5772 * 2. Set WPA_STA_TDLS_PEER station.
5773 * 3. Set an already added unassociated station, if driver supports
5774 * full AP client state. (Set these properties after station became
5775 * associated will be rejected by the driver).
5776 */
5777 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
5778 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
5779 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005780 wpa_hexdump(MSG_DEBUG, " * supported rates",
5781 params->supp_rates, params->supp_rates_len);
5782 wpa_printf(MSG_DEBUG, " * capability=0x%x",
5783 params->capability);
5784 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
5785 params->supp_rates_len, params->supp_rates) ||
5786 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
5787 params->capability))
5788 goto fail;
5789
5790 if (params->ht_capabilities) {
5791 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
5792 (u8 *) params->ht_capabilities,
5793 sizeof(*params->ht_capabilities));
5794 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
5795 sizeof(*params->ht_capabilities),
5796 params->ht_capabilities))
5797 goto fail;
5798 }
5799
5800 if (params->vht_capabilities) {
5801 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
5802 (u8 *) params->vht_capabilities,
5803 sizeof(*params->vht_capabilities));
5804 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
5805 sizeof(*params->vht_capabilities),
5806 params->vht_capabilities))
5807 goto fail;
5808 }
5809
Hai Shalom81f62d82019-07-22 12:10:00 -07005810 if (params->he_capab) {
5811 wpa_hexdump(MSG_DEBUG, " * he_capab",
5812 params->he_capab, params->he_capab_len);
5813 if (nla_put(msg, NL80211_ATTR_HE_CAPABILITY,
5814 params->he_capab_len, params->he_capab))
5815 goto fail;
5816 }
5817
Hai Shalom60840252021-02-19 19:02:11 -08005818 if (params->he_6ghz_capab) {
5819 wpa_hexdump(MSG_DEBUG, " * he_6ghz_capab",
5820 params->he_6ghz_capab,
5821 sizeof(*params->he_6ghz_capab));
5822 if (nla_put(msg, NL80211_ATTR_HE_6GHZ_CAPABILITY,
5823 sizeof(*params->he_6ghz_capab),
5824 params->he_6ghz_capab))
5825 goto fail;
5826 }
5827
Sunil Ravia04bd252022-05-02 22:54:18 -07005828 if (params->eht_capab) {
5829 wpa_hexdump(MSG_DEBUG, " * eht_capab",
5830 params->eht_capab, params->eht_capab_len);
5831 if (nla_put(msg, NL80211_ATTR_EHT_CAPABILITY,
5832 params->eht_capab_len, params->eht_capab))
5833 goto fail;
5834 }
5835
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005836 if (params->ext_capab) {
5837 wpa_hexdump(MSG_DEBUG, " * ext_capab",
5838 params->ext_capab, params->ext_capab_len);
5839 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
5840 params->ext_capab_len, params->ext_capab))
5841 goto fail;
5842 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005843
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005844 /* Set EML capabilities of ML STA */
5845 if (params->mld_link_addr && params->eml_cap) {
5846 wpa_printf(MSG_DEBUG, " * eml_cap =%u",
5847 params->eml_cap);
5848 if (nla_put_u16(msg, NL80211_ATTR_EML_CAPABILITY,
5849 params->eml_cap))
5850 goto fail;
5851 }
5852
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005853 if (is_ap_interface(drv->nlmode) &&
5854 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
5855 params->support_p2p_ps ?
5856 NL80211_P2P_PS_SUPPORTED :
5857 NL80211_P2P_PS_UNSUPPORTED))
5858 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005859 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005860 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005861 if (params->aid) {
5862 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005863 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
5864 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005865 } else {
5866 /*
5867 * cfg80211 validates that AID is non-zero, so we have
5868 * to make this a non-zero value for the TDLS case where
Hai Shalomc1a21442022-02-04 13:43:00 -08005869 * a stub STA entry is used for now and for a station
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005870 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005871 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005872 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
5873 (params->flags & WPA_STA_TDLS_PEER) ?
5874 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005875 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
5876 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005877 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005878 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
5879 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005880 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5881 params->listen_interval))
5882 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005883 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
5884 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005885 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
5886 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005887 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
5888 (params->flags & WPA_STA_ASSOCIATED)) {
5889 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
5890 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
5891 params->listen_interval);
5892 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
5893 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5894 params->listen_interval))
5895 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005896 }
5897
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005898 if (params->vht_opmode_enabled) {
5899 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005900 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
5901 params->vht_opmode))
5902 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005903 }
5904
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005905 if (params->supp_channels) {
5906 wpa_hexdump(MSG_DEBUG, " * supported channels",
5907 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005908 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
5909 params->supp_channels_len, params->supp_channels))
5910 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005911 }
5912
5913 if (params->supp_oper_classes) {
5914 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
5915 params->supp_oper_classes,
5916 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005917 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
5918 params->supp_oper_classes_len,
5919 params->supp_oper_classes))
5920 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005921 }
5922
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005923 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005924 upd.set = sta_flags_nl80211(params->flags);
5925 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005926
5927 /*
5928 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
5929 * flags, as nl80211 driver moves a new station, by default, into
5930 * associated state.
5931 *
5932 * On the other hand, if the driver supports that feature and the
5933 * station is added in unauthenticated state, set the
5934 * authenticated/associated bits in the mask to prevent moving this
5935 * station to associated state before it is actually associated.
5936 *
5937 * This is irrelevant for mesh mode where the station is added to the
5938 * driver as authenticated already, and ASSOCIATED isn't part of the
5939 * nl80211 API.
5940 */
5941 if (!is_mesh_interface(drv->nlmode)) {
5942 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
5943 wpa_printf(MSG_DEBUG,
5944 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
5945 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
5946 BIT(NL80211_STA_FLAG_AUTHENTICATED));
5947 } else if (!params->set &&
5948 !(params->flags & WPA_STA_TDLS_PEER)) {
5949 if (!(params->flags & WPA_STA_AUTHENTICATED))
5950 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
5951 if (!(params->flags & WPA_STA_ASSOCIATED))
5952 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
5953 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005954#ifdef CONFIG_MESH
5955 } else {
5956 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
5957 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
5958 params->peer_aid);
5959 if (ret)
5960 goto fail;
5961 }
5962#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005963 }
5964
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005965 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
5966 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005967 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
5968 goto fail;
5969
5970#ifdef CONFIG_MESH
5971 if (params->plink_state &&
5972 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
5973 sta_plink_state_nl80211(params->plink_state)))
5974 goto fail;
5975#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005976
Hai Shalomc3565922019-10-28 11:58:20 -07005977 if ((!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
5978 FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) &&
5979 (params->flags & WPA_STA_WMM)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005980 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
5981
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005982 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005983 if (!wme ||
5984 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
5985 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
5986 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
5987 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
5988 WMM_QOSINFO_STA_SP_MASK))
5989 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005990 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005991 }
5992
Sunil Ravi036cec52023-03-29 11:35:17 -07005993 /* In case we are an AP MLD need to always specify the link ID */
5994 if (params->mld_link_id >= 0) {
5995 wpa_printf(MSG_DEBUG, " * mld_link_id=%d",
5996 params->mld_link_id);
5997 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
5998 params->mld_link_id))
5999 goto fail;
6000
6001 /*
6002 * If the link address is specified the station is a non-AP MLD
6003 * and thus need to provide the MLD address as the station
6004 * address, and the non-AP MLD link address as the link address.
6005 */
6006 if (params->mld_link_addr) {
6007 wpa_printf(MSG_DEBUG, " * mld_link_addr=" MACSTR,
6008 MAC2STR(params->mld_link_addr));
6009
6010 if (nla_put(msg, NL80211_ATTR_MLD_ADDR,
6011 ETH_ALEN, params->addr) ||
6012 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
6013 params->mld_link_addr))
6014 goto fail;
6015 } else {
6016 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
6017 params->addr))
6018 goto fail;
6019 }
6020 } else {
6021 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
6022 goto fail;
6023 }
6024
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006025 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006026 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006027 if (ret)
Sunil Ravi036cec52023-03-29 11:35:17 -07006028 wpa_printf(MSG_DEBUG, "nl80211: %s result: %d (%s)",
6029 cmd_string, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006030 if (ret == -EEXIST)
6031 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006032fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006033 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006034 return ret;
6035}
6036
6037
Sunil Ravic0f5d412024-09-11 22:12:49 +00006038static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr,
6039 bool is_bridge)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006040{
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006041 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +00006042 struct ndmsg nhdr = {
6043 .ndm_state = NUD_PERMANENT,
Sunil Ravic0f5d412024-09-11 22:12:49 +00006044 .ndm_ifindex = is_bridge ? bss->br_ifindex : bss->ifindex,
Sunil Ravi99c035e2024-07-12 01:42:03 +00006045 .ndm_family = AF_BRIDGE,
Sunil Ravic0f5d412024-09-11 22:12:49 +00006046 .ndm_type = is_bridge ? NTF_SELF : 0,
Sunil Ravi99c035e2024-07-12 01:42:03 +00006047 };
6048 struct nl_msg *msg;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006049 int err;
6050
Sunil Ravi99c035e2024-07-12 01:42:03 +00006051 msg = nlmsg_alloc_simple(RTM_DELNEIGH, NLM_F_CREATE);
6052 if (!msg)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006053 return;
6054
Sunil Ravi99c035e2024-07-12 01:42:03 +00006055 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0 ||
6056 nla_put(msg, NDA_LLADDR, ETH_ALEN, (void *) addr) ||
6057 nl_send_auto_complete(drv->rtnl_sk, msg) < 0)
6058 goto errout;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006059
Sunil Ravi99c035e2024-07-12 01:42:03 +00006060 err = nl_wait_for_ack(drv->rtnl_sk);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006061 if (err < 0) {
6062 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
Sunil Ravic0f5d412024-09-11 22:12:49 +00006063 MACSTR " ifindex=%d ifname %s failed: %s",
6064 MAC2STR(addr),
6065 is_bridge ? bss->br_ifindex : bss->ifindex,
6066 is_bridge ? bss->brname : bss->ifname,
6067 nl_geterror(err));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006068 } else {
Sunil Ravic0f5d412024-09-11 22:12:49 +00006069 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry "
6070 MACSTR " from %s",
6071 MAC2STR(addr),
6072 is_bridge ? bss->brname : bss->ifname);
6073 }
6074
6075errout:
6076 nlmsg_free(msg);
6077}
6078
6079
6080static void rtnl_neigh_add_fdb_entry(struct i802_bss *bss, const u8 *addr,
6081 bool is_bridge)
6082{
6083 struct wpa_driver_nl80211_data *drv = bss->drv;
6084 struct ndmsg nhdr = {
6085 .ndm_state = NUD_PERMANENT,
6086 .ndm_ifindex = is_bridge ? bss->br_ifindex : bss->ifindex,
6087 .ndm_family = AF_BRIDGE,
6088 /* TODO: remove this check if this flag needs to be used,
6089 * for other interfaces type.
6090 */
6091 .ndm_flags = is_bridge ? NTF_SELF : 0,
6092 };
6093 struct nl_msg *msg;
6094 int err;
6095
6096 msg = nlmsg_alloc_simple(RTM_NEWNEIGH, NLM_F_CREATE);
6097 if (!msg)
6098 return;
6099
6100 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0 ||
6101 nla_put(msg, NDA_LLADDR, ETH_ALEN, (void *) addr) ||
6102 nl_send_auto_complete(drv->rtnl_sk, msg) < 0)
6103 goto errout;
6104
6105 err = nl_wait_for_ack(drv->rtnl_sk);
6106 if (err < 0) {
6107 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry addition for "
6108 MACSTR " ifindex=%d ifname %s failed: %s",
6109 MAC2STR(addr),
6110 is_bridge ? bss->br_ifindex : bss->ifindex,
6111 is_bridge ? bss->brname : bss->ifname,
6112 nl_geterror(err));
6113 } else {
6114 wpa_printf(MSG_DEBUG, "nl80211: added bridge FDB entry " MACSTR
6115 " to %s",
6116 MAC2STR(addr),
6117 is_bridge ? bss->brname : bss->ifname);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006118 }
6119
Sunil Ravi99c035e2024-07-12 01:42:03 +00006120errout:
6121 nlmsg_free(msg);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006122}
6123
6124
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006125static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
6126 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006127{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006128 struct wpa_driver_nl80211_data *drv = bss->drv;
6129 struct nl_msg *msg;
6130 int ret;
6131
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006132 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
6133 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6134 (deauth == 0 &&
6135 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
6136 WLAN_FC_STYPE_DISASSOC)) ||
6137 (deauth == 1 &&
6138 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
6139 WLAN_FC_STYPE_DEAUTH)) ||
6140 (reason_code &&
6141 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
6142 nlmsg_free(msg);
6143 return -ENOBUFS;
6144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006145
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006146 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006147 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
6148 " --> %d (%s)",
6149 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006150
6151 if (drv->rtnl_sk)
Sunil Ravic0f5d412024-09-11 22:12:49 +00006152 rtnl_neigh_delete_fdb_entry(bss, addr, false);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006154 if (ret == -ENOENT)
6155 return 0;
6156 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006157}
6158
6159
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006160void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006161{
6162 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006163 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006164
6165 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
6166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006167 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006168 dl_list_for_each(drv2, &drv->global->interfaces,
6169 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006170 {
6171 del_ifidx(drv2, ifidx, IFIDX_ANY);
6172 /* Remove all bridges learned for this iface */
6173 del_ifidx(drv2, IFIDX_ANY, ifidx);
6174 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006175
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006176 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006177 if (send_and_recv_cmd(drv, msg) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006178 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006179 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
6180}
6181
6182
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07006183const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006184{
6185 switch (mode) {
6186 case NL80211_IFTYPE_ADHOC:
6187 return "ADHOC";
6188 case NL80211_IFTYPE_STATION:
6189 return "STATION";
6190 case NL80211_IFTYPE_AP:
6191 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07006192 case NL80211_IFTYPE_AP_VLAN:
6193 return "AP_VLAN";
6194 case NL80211_IFTYPE_WDS:
6195 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006196 case NL80211_IFTYPE_MONITOR:
6197 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07006198 case NL80211_IFTYPE_MESH_POINT:
6199 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006200 case NL80211_IFTYPE_P2P_CLIENT:
6201 return "P2P_CLIENT";
6202 case NL80211_IFTYPE_P2P_GO:
6203 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006204 case NL80211_IFTYPE_P2P_DEVICE:
6205 return "P2P_DEVICE";
Hai Shalom4fbc08f2020-05-18 12:37:00 -07006206 case NL80211_IFTYPE_OCB:
6207 return "OCB";
6208 case NL80211_IFTYPE_NAN:
6209 return "NAN";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006210 default:
6211 return "unknown";
6212 }
6213}
6214
6215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006216static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6217 const char *ifname,
6218 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006219 const u8 *addr, int wds,
6220 int (*handler)(struct nl_msg *, void *),
6221 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006222{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006223 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006224 int ifidx;
6225 int ret = -ENOBUFS;
6226
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006227 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6228 iftype, nl80211_iftype_str(iftype));
6229
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006230 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
6231 if (!msg ||
6232 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
6233 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
6234 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006235
6236 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006237 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006239 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006240 if (!flags ||
6241 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
6242 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006243
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006244 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006245 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006246 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
6247 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006248 }
6249
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006250 /*
6251 * Tell cfg80211 that the interface belongs to the socket that created
6252 * it, and the interface should be deleted when the socket is closed.
6253 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006254 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
6255 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006256
Hai Shalom60840252021-02-19 19:02:11 -08006257 if ((addr && iftype == NL80211_IFTYPE_P2P_DEVICE) &&
6258 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
6259 goto fail;
6260
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006261 ret = send_and_recv_resp(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006262 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006263 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006264 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006265 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006266 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6267 ifname, ret, strerror(-ret));
6268 return ret;
6269 }
6270
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006271 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
6272 return 0;
6273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006274 ifidx = if_nametoindex(ifname);
6275 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6276 ifname, ifidx);
6277
6278 if (ifidx <= 0)
6279 return -1;
6280
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006281 /*
6282 * Some virtual interfaces need to process EAPOL packets and events on
6283 * the parent interface. This is used mainly with hostapd.
6284 */
6285 if (drv->hostapd ||
6286 iftype == NL80211_IFTYPE_AP_VLAN ||
6287 iftype == NL80211_IFTYPE_WDS ||
6288 iftype == NL80211_IFTYPE_MONITOR) {
6289 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006290 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006291 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006292
6293 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006294 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006295 nl80211_remove_iface(drv, ifidx);
6296 return -1;
6297 }
6298
6299 return ifidx;
6300}
6301
6302
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006303int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6304 const char *ifname, enum nl80211_iftype iftype,
6305 const u8 *addr, int wds,
6306 int (*handler)(struct nl_msg *, void *),
6307 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006308{
6309 int ret;
6310
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006311 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
6312 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006314 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006315 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006316 if (use_existing) {
6317 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
6318 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006319 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
6320 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
6321 addr) < 0 &&
6322 (linux_set_iface_flags(drv->global->ioctl_sock,
6323 ifname, 0) < 0 ||
6324 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
6325 addr) < 0 ||
6326 linux_set_iface_flags(drv->global->ioctl_sock,
6327 ifname, 1) < 0))
6328 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006329 return -ENFILE;
6330 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006331 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6332
6333 /* Try to remove the interface that was already there. */
6334 nl80211_remove_iface(drv, if_nametoindex(ifname));
6335
6336 /* Try to create the interface again */
6337 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006338 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006339 }
6340
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006341 if (ret >= 0 && is_p2p_net_interface(iftype)) {
6342 wpa_printf(MSG_DEBUG,
6343 "nl80211: Interface %s created for P2P - disable 11b rates",
6344 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006345 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006346 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006347
6348 return ret;
6349}
6350
6351
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006352static int nl80211_setup_ap(struct i802_bss *bss)
6353{
6354 struct wpa_driver_nl80211_data *drv = bss->drv;
6355
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006356 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
6357 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006358
6359 /*
6360 * Disable Probe Request reporting unless we need it in this way for
6361 * devices that include the AP SME, in the other case (unless using
6362 * monitor iface) we'll get it through the nl_mgmt socket instead.
6363 */
6364 if (!drv->device_ap_sme)
6365 wpa_driver_nl80211_probe_req_report(bss, 0);
6366
6367 if (!drv->device_ap_sme && !drv->use_monitor)
6368 if (nl80211_mgmt_subscribe_ap(bss))
6369 return -1;
6370
6371 if (drv->device_ap_sme && !drv->use_monitor)
6372 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006373 wpa_printf(MSG_DEBUG,
6374 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006375
6376 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07006377 nl80211_create_monitor_interface(drv) &&
6378 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006379 return -1;
6380
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006381 if (drv->device_ap_sme &&
6382 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6383 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6384 "Probe Request frame reporting in AP mode");
6385 /* Try to survive without this */
6386 }
6387
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006388 return 0;
6389}
6390
6391
6392static void nl80211_teardown_ap(struct i802_bss *bss)
6393{
6394 struct wpa_driver_nl80211_data *drv = bss->drv;
6395
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006396 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
6397 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006398 if (drv->device_ap_sme) {
6399 wpa_driver_nl80211_probe_req_report(bss, 0);
6400 if (!drv->use_monitor)
6401 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6402 } else if (drv->use_monitor)
6403 nl80211_remove_monitor_interface(drv);
6404 else
6405 nl80211_mgmt_unsubscribe(bss, "AP teardown");
6406
Paul Stewart092955c2017-02-06 09:13:09 -08006407 nl80211_put_wiphy_data_ap(bss);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006408 if (bss->flink)
6409 bss->flink->beacon_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006410}
6411
6412
Hai Shalomfdcde762020-04-02 11:19:20 -07006413static int nl80211_tx_control_port(void *priv, const u8 *dest,
6414 u16 proto, const u8 *buf, size_t len,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006415 int no_encrypt, int link_id)
Hai Shalomfdcde762020-04-02 11:19:20 -07006416{
Hai Shalom899fcc72020-10-19 14:38:18 -07006417 struct nl80211_ack_ext_arg ext_arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07006418 struct i802_bss *bss = priv;
6419 struct nl_msg *msg;
Hai Shalom899fcc72020-10-19 14:38:18 -07006420 u64 cookie = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07006421 int ret;
6422
6423 wpa_printf(MSG_DEBUG,
6424 "nl80211: Send over control port dest=" MACSTR
6425 " proto=0x%04x len=%u no_encrypt=%d",
6426 MAC2STR(dest), proto, (unsigned int) len, no_encrypt);
6427
6428 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CONTROL_PORT_FRAME);
6429 if (!msg ||
6430 nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
6431 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dest) ||
6432 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
6433 (no_encrypt &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006434 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)) ||
6435 (link_id != NL80211_DRV_LINK_ID_NA &&
6436 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))) {
Hai Shalomfdcde762020-04-02 11:19:20 -07006437 nlmsg_free(msg);
6438 return -ENOBUFS;
6439 }
6440
Hai Shalom899fcc72020-10-19 14:38:18 -07006441 os_memset(&ext_arg, 0, sizeof(struct nl80211_ack_ext_arg));
6442 ext_arg.ext_data = &cookie;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006443 ret = send_and_recv(bss->drv, bss->drv->global->nl, msg,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006444 NULL, NULL, ack_handler_cookie, &ext_arg, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07006445 if (ret) {
Hai Shalomfdcde762020-04-02 11:19:20 -07006446 wpa_printf(MSG_DEBUG,
6447 "nl80211: tx_control_port failed: ret=%d (%s)",
6448 ret, strerror(-ret));
Hai Shalom899fcc72020-10-19 14:38:18 -07006449 } else {
6450 struct wpa_driver_nl80211_data *drv = bss->drv;
6451
6452 wpa_printf(MSG_DEBUG,
6453 "nl80211: tx_control_port cookie=0x%llx",
6454 (long long unsigned int) cookie);
6455 drv->eapol_tx_cookie = cookie;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006456 drv->eapol_tx_link_id = link_id;
Hai Shalom899fcc72020-10-19 14:38:18 -07006457 }
Hai Shalomfdcde762020-04-02 11:19:20 -07006458
6459 return ret;
6460}
6461
6462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006463static int nl80211_send_eapol_data(struct i802_bss *bss,
6464 const u8 *addr, const u8 *data,
6465 size_t data_len)
6466{
6467 struct sockaddr_ll ll;
6468 int ret;
6469
6470 if (bss->drv->eapol_tx_sock < 0) {
6471 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
6472 return -1;
6473 }
6474
6475 os_memset(&ll, 0, sizeof(ll));
6476 ll.sll_family = AF_PACKET;
6477 ll.sll_ifindex = bss->ifindex;
6478 ll.sll_protocol = htons(ETH_P_PAE);
6479 ll.sll_halen = ETH_ALEN;
6480 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6481 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6482 (struct sockaddr *) &ll, sizeof(ll));
6483 if (ret < 0)
6484 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6485 strerror(errno));
6486
6487 return ret;
6488}
6489
6490
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006491static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6492
6493static int wpa_driver_nl80211_hapd_send_eapol(
6494 void *priv, const u8 *addr, const u8 *data,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006495 size_t data_len, int encrypt, const u8 *own_addr, u32 flags,
6496 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006497{
6498 struct i802_bss *bss = priv;
6499 struct wpa_driver_nl80211_data *drv = bss->drv;
6500 struct ieee80211_hdr *hdr;
6501 size_t len;
6502 u8 *pos;
6503 int res;
6504 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08006505
Hai Shalomb755a2a2020-04-23 21:49:02 -07006506 /* For now, disable EAPOL TX over control port in AP mode by default
6507 * since it does not provide TX status notifications. */
6508 if (drv->control_port_ap &&
6509 (drv->capa.flags & WPA_DRIVER_FLAGS_CONTROL_PORT))
Hai Shalomfdcde762020-04-02 11:19:20 -07006510 return nl80211_tx_control_port(bss, addr, ETH_P_EAPOL,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006511 data, data_len, !encrypt,
6512 link_id);
Hai Shalomfdcde762020-04-02 11:19:20 -07006513
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006514 if (drv->device_ap_sme || !drv->use_monitor)
6515 return nl80211_send_eapol_data(bss, addr, data, data_len);
6516
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006517 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6518 data_len;
6519 hdr = os_zalloc(len);
6520 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006521 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
6522 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006523 return -1;
6524 }
6525
6526 hdr->frame_control =
6527 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6528 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6529 if (encrypt)
6530 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
6531 if (qos) {
6532 hdr->frame_control |=
6533 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6534 }
6535
6536 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6537 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6538 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6539 pos = (u8 *) (hdr + 1);
6540
6541 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006542 /* Set highest priority in QoS header */
6543 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006544 pos[1] = 0;
6545 pos += 2;
6546 }
6547
6548 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6549 pos += sizeof(rfc1042_header);
6550 WPA_PUT_BE16(pos, ETH_P_PAE);
6551 pos += 2;
6552 memcpy(pos, data, data_len);
6553
Hai Shalomfdcde762020-04-02 11:19:20 -07006554 res = nl80211_send_monitor(drv, hdr, len, encrypt, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006555 if (res < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -07006556 wpa_printf(MSG_ERROR,
6557 "hapd_send_eapol - packet len: %lu - failed",
6558 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006559 }
6560 os_free(hdr);
6561
6562 return res;
6563}
6564
6565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006566static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006567 unsigned int total_flags,
6568 unsigned int flags_or,
6569 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006570{
6571 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006572 struct nl_msg *msg;
6573 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006574 struct nl80211_sta_flag_update upd;
6575
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006576 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
6577 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
6578 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
6579 !!(total_flags & WPA_STA_AUTHORIZED));
6580
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006581 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6582 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
6583 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006584
6585 /*
6586 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6587 * can be removed eventually.
6588 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006589 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006590 if (!flags ||
6591 ((total_flags & WPA_STA_AUTHORIZED) &&
6592 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
6593 ((total_flags & WPA_STA_WMM) &&
6594 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
6595 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
6596 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
6597 ((total_flags & WPA_STA_MFP) &&
6598 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
6599 ((total_flags & WPA_STA_TDLS_PEER) &&
6600 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
6601 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006602
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006603 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006604
6605 os_memset(&upd, 0, sizeof(upd));
6606 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6607 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006608 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
6609 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006610
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006611 return send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006612fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006613 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006614 return -ENOBUFS;
6615}
6616
6617
Hai Shalom81f62d82019-07-22 12:10:00 -07006618static int driver_nl80211_sta_set_airtime_weight(void *priv, const u8 *addr,
6619 unsigned int weight)
6620{
6621 struct i802_bss *bss = priv;
6622 struct nl_msg *msg;
Hai Shalomc1a21442022-02-04 13:43:00 -08006623 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07006624
6625 wpa_printf(MSG_DEBUG,
6626 "nl80211: Set STA airtime weight - ifname=%s addr=" MACSTR
6627 " weight=%u", bss->ifname, MAC2STR(addr), weight);
6628
6629 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6630 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6631 nla_put_u16(msg, NL80211_ATTR_AIRTIME_WEIGHT, weight))
6632 goto fail;
6633
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006634 ret = send_and_recv_cmd(bss->drv, msg);
Hai Shalomc1a21442022-02-04 13:43:00 -08006635 if (ret) {
6636 wpa_printf(MSG_DEBUG,
6637 "nl80211: SET_STATION[AIRTIME_WEIGHT] failed: ret=%d (%s)",
6638 ret, strerror(-ret));
6639 }
6640 return ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07006641fail:
6642 nlmsg_free(msg);
6643 return -ENOBUFS;
6644}
6645
6646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006647static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6648 struct wpa_driver_associate_params *params)
6649{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006650 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006651
6652 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006653 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6654 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006655 nlmode = NL80211_IFTYPE_P2P_GO;
6656 } else
6657 nlmode = NL80211_IFTYPE_AP;
6658
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006659 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006660 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006661 nl80211_remove_monitor_interface(drv);
6662 return -1;
6663 }
6664
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006665 if (params->freq.freq &&
6666 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006667 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006668 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006669 nl80211_remove_monitor_interface(drv);
6670 return -1;
6671 }
6672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006673 return 0;
6674}
6675
6676
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006677static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
6678 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006679{
6680 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006681 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006682
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006683 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006684 ret = send_and_recv(drv, drv->first_bss->nl_connect, msg, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006685 NULL, NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006686 if (ret) {
6687 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
6688 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006689 } else {
6690 wpa_printf(MSG_DEBUG,
6691 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006692 }
6693
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006694 if (reset_mode &&
6695 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07006696 NL80211_IFTYPE_STATION)) {
6697 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6698 "station mode");
6699 }
6700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006701 return ret;
6702}
6703
6704
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006705static int nl80211_ht_vht_overrides(struct nl_msg *msg,
6706 struct wpa_driver_associate_params *params)
6707{
6708 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
6709 return -1;
6710
6711 if (params->htcaps && params->htcaps_mask) {
6712 int sz = sizeof(struct ieee80211_ht_capabilities);
6713 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
6714 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
6715 params->htcaps_mask, sz);
6716 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
6717 params->htcaps) ||
6718 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6719 params->htcaps_mask))
6720 return -1;
6721 }
6722
6723#ifdef CONFIG_VHT_OVERRIDES
6724 if (params->disable_vht) {
6725 wpa_printf(MSG_DEBUG, " * VHT disabled");
6726 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
6727 return -1;
6728 }
6729
6730 if (params->vhtcaps && params->vhtcaps_mask) {
6731 int sz = sizeof(struct ieee80211_vht_capabilities);
6732 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
6733 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
6734 params->vhtcaps_mask, sz);
6735 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
6736 params->vhtcaps) ||
6737 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
6738 params->vhtcaps_mask))
6739 return -1;
6740 }
6741#endif /* CONFIG_VHT_OVERRIDES */
6742
Hai Shalomc1a21442022-02-04 13:43:00 -08006743#ifdef CONFIG_HE_OVERRIDES
6744 if (params->disable_he) {
6745 wpa_printf(MSG_DEBUG, " * HE disabled");
6746 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_HE))
6747 return -1;
6748 }
6749#endif /* CONFIG_HE_OVERRIDES */
6750
Sunil Ravi77d572f2023-01-17 23:58:31 +00006751 if (params->disable_eht) {
6752 wpa_printf(MSG_DEBUG, " * EHT disabled");
6753 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_EHT))
6754 return -1;
6755 }
6756
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006757 return 0;
6758}
6759
6760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006761static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
6762 struct wpa_driver_associate_params *params)
6763{
6764 struct nl_msg *msg;
6765 int ret = -1;
6766 int count = 0;
6767
6768 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
6769
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006770 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006771 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6772 "IBSS mode");
6773 return -1;
6774 }
6775
6776retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006777 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
6778 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
6779 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006780
Hai Shalom74f70d42019-02-11 14:42:39 -08006781 wpa_printf(MSG_DEBUG, " * SSID=%s",
6782 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006783 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
6784 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006785 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6786 drv->ssid_len = params->ssid_len;
6787
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006788 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
6789 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006790 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006791
6792 ret = nl80211_set_conn_keys(params, msg);
6793 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006794 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006795
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006796 if (params->bssid && params->fixed_bssid) {
6797 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
6798 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006799 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
6800 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006801 }
6802
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006803 if (params->fixed_freq) {
6804 wpa_printf(MSG_DEBUG, " * fixed_freq");
6805 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
6806 goto fail;
6807 }
6808
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006809 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
6810 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
6811 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006812 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
6813 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA384) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006814 wpa_printf(MSG_DEBUG, " * control port");
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006815 if (nl80211_put_control_port(drv, msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006816 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006817 }
6818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006819 if (params->wpa_ie) {
6820 wpa_hexdump(MSG_DEBUG,
6821 " * Extra IEs for Beacon/Probe Response frames",
6822 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006823 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6824 params->wpa_ie))
6825 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006826 }
6827
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08006828 ret = nl80211_ht_vht_overrides(msg, params);
6829 if (ret < 0)
6830 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006831
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006832 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
6833 goto fail;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006834 ret = send_and_recv(drv, drv->first_bss->nl_connect, msg, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006835 NULL, NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006836 msg = NULL;
6837 if (ret) {
6838 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
6839 ret, strerror(-ret));
6840 count++;
6841 if (ret == -EALREADY && count == 1) {
6842 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
6843 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006844 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006845 nlmsg_free(msg);
6846 goto retry;
6847 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006848 } else {
6849 wpa_printf(MSG_DEBUG,
6850 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006851 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006852
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006853fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006854 nlmsg_free(msg);
6855 return ret;
6856}
6857
6858
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006859static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
6860 struct wpa_driver_associate_params *params,
6861 struct nl_msg *msg)
6862{
6863 if (params->fils_erp_username_len) {
6864 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
6865 params->fils_erp_username,
6866 params->fils_erp_username_len);
6867 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
6868 params->fils_erp_username_len,
6869 params->fils_erp_username))
6870 return -1;
6871 }
6872
6873 if (params->fils_erp_realm_len) {
6874 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
6875 params->fils_erp_realm,
6876 params->fils_erp_realm_len);
6877 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
6878 params->fils_erp_realm_len, params->fils_erp_realm))
6879 return -1;
6880 }
6881
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006882 if (params->fils_erp_rrk_len) {
Vinita S. Maloo3a5b4412020-05-19 17:43:22 +05306883 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
6884 params->fils_erp_next_seq_num);
6885 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
6886 params->fils_erp_next_seq_num))
6887 return -1;
6888
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006889 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
6890 (unsigned long) params->fils_erp_rrk_len);
6891 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
6892 params->fils_erp_rrk_len, params->fils_erp_rrk))
6893 return -1;
6894 }
6895
6896 return 0;
6897}
6898
6899
Sunil Ravi89eba102022-09-13 21:04:37 -07006900static unsigned int num_bits_set(u32 val)
6901{
6902 unsigned int c;
6903
6904 for (c = 0; val; c++)
6905 val &= val - 1;
6906
6907 return c;
6908}
6909
6910
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006911static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
6912 struct wpa_driver_associate_params *params,
6913 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006914{
Sunil Ravi77d572f2023-01-17 23:58:31 +00006915 if (params->mld_params.mld_addr && params->mld_params.valid_links > 0) {
6916 struct wpa_driver_mld_params *mld_params = &params->mld_params;
6917 struct nlattr *links, *attr;
Sunil Ravi77d572f2023-01-17 23:58:31 +00006918 u8 link_id;
6919
6920 wpa_printf(MSG_DEBUG, " * MLD: MLD addr=" MACSTR,
6921 MAC2STR(mld_params->mld_addr));
6922
6923 if (nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
6924 mld_params->mld_addr) ||
6925 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6926 mld_params->assoc_link_id))
6927 return -1;
6928
6929 links = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
6930 if (!links)
6931 return -1;
6932
Sunil Ravi99c035e2024-07-12 01:42:03 +00006933 for_each_link(mld_params->valid_links, link_id) {
6934 attr = nla_nest_start(msg, 0);
Sunil Ravi77d572f2023-01-17 23:58:31 +00006935 if (!attr)
6936 return -1;
6937
6938 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6939 link_id) ||
6940 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
6941 mld_params->mld_links[link_id].bssid) ||
6942 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
6943 mld_params->mld_links[link_id].freq) ||
Sunil Ravi99c035e2024-07-12 01:42:03 +00006944 (mld_params->mld_links[link_id].disabled &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006945 nla_put_flag(msg,
6946 NL80211_ATTR_MLO_LINK_DISABLED)) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00006947 (mld_params->mld_links[link_id].ies &&
Sunil Ravi99c035e2024-07-12 01:42:03 +00006948 mld_params->mld_links[link_id].ies_len &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00006949 nla_put(msg, NL80211_ATTR_IE,
6950 mld_params->mld_links[link_id].ies_len,
6951 mld_params->mld_links[link_id].ies)))
6952 return -1;
6953
6954 os_memcpy(drv->sta_mlo_info.links[link_id].bssid,
6955 mld_params->mld_links[link_id].bssid,
6956 ETH_ALEN);
6957 nla_nest_end(msg, attr);
Sunil Ravi77d572f2023-01-17 23:58:31 +00006958 }
6959
6960 nla_nest_end(msg, links);
6961
6962 os_memcpy(drv->sta_mlo_info.ap_mld_addr,
6963 params->mld_params.mld_addr, ETH_ALEN);
6964 drv->sta_mlo_info.assoc_link_id = mld_params->assoc_link_id;
6965 drv->sta_mlo_info.req_links = mld_params->valid_links;
6966 }
6967
Paul Stewart092955c2017-02-06 09:13:09 -08006968 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
6969 return -1;
6970
Sunil Ravi77d572f2023-01-17 23:58:31 +00006971 if (params->bssid && !params->mld_params.mld_addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6973 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006974 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
6975 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006977
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006978 if (params->bssid_hint) {
6979 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
6980 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006981 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
6982 params->bssid_hint))
6983 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006984 }
6985
Sunil Ravi77d572f2023-01-17 23:58:31 +00006986 if (params->freq.freq && !params->mld_params.mld_addr) {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006987 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006988 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
6989 params->freq.freq))
6990 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006991 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006992 } else
6993 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006994
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006995 if (params->freq_hint) {
6996 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006997 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
6998 params->freq_hint))
6999 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007000 }
7001
Hai Shalomc3565922019-10-28 11:58:20 -07007002 if (params->freq.edmg.channels && params->freq.edmg.bw_config) {
7003 wpa_printf(MSG_DEBUG,
7004 " * EDMG configuration: channels=0x%x bw_config=%d",
7005 params->freq.edmg.channels,
7006 params->freq.edmg.bw_config);
7007 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_CHANNELS,
7008 params->freq.edmg.channels) ||
7009 nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_BW_CONFIG,
7010 params->freq.edmg.bw_config))
7011 return -1;
7012 }
7013
Dmitry Shmidt04949592012-07-19 12:16:46 -07007014 if (params->bg_scan_period >= 0) {
7015 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7016 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007017 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7018 params->bg_scan_period))
7019 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007020 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007022 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08007023 wpa_printf(MSG_DEBUG, " * SSID=%s",
7024 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007025 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
7026 params->ssid))
7027 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007028 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007029 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007030 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7031 drv->ssid_len = params->ssid_len;
7032 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007034 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007035 if (params->wpa_ie &&
7036 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
7037 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007039 if (params->wpa_proto) {
7040 enum nl80211_wpa_versions ver = 0;
7041
7042 if (params->wpa_proto & WPA_PROTO_WPA)
7043 ver |= NL80211_WPA_VERSION_1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007044 if (params->wpa_proto & WPA_PROTO_RSN) {
7045#if !defined(CONFIG_DRIVER_NL80211_BRCM) && !defined(CONFIG_DRIVER_NL80211_SYNA)
7046 /*
7047 * NL80211_ATTR_SAE_PASSWORD is related and was added
7048 * at the same time as NL80211_WPA_VERSION_3.
7049 */
7050 if (nl80211_attr_supported(drv,
7051 NL80211_ATTR_SAE_PASSWORD) &&
7052 wpa_key_mgmt_sae(params->key_mgmt_suite))
7053 ver |= NL80211_WPA_VERSION_3;
7054 else
7055#endif
7056 ver |= NL80211_WPA_VERSION_2;
7057 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007058
7059 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007060 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
7061 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007062 }
7063
7064 if (params->pairwise_suite != WPA_CIPHER_NONE) {
7065 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
7066 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007067 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7068 cipher))
7069 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007070 }
7071
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007072 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
7073 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
7074 /*
7075 * This is likely to work even though many drivers do not
7076 * advertise support for operations without GTK.
7077 */
7078 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
7079 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007080 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
7081 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007082 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
7083 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007084 }
7085
7086 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
7087 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
7088 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
7089 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07007090 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07007091 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
7092 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007093 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07007094 params->key_mgmt_suite == WPA_KEY_MGMT_SAE ||
Sunil Ravi89eba102022-09-13 21:04:37 -07007095 params->key_mgmt_suite == WPA_KEY_MGMT_SAE_EXT_KEY ||
Hai Shalom021b0b52019-04-10 11:17:58 -07007096 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE ||
Sunil Ravi89eba102022-09-13 21:04:37 -07007097 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE_EXT_KEY ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007098 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007099 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07007100 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X_SHA384 ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007101 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
7102 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
7103 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07007104 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
7105 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007106 params->key_mgmt_suite == WPA_KEY_MGMT_DPP ||
7107 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA384) {
Sunil Ravi89eba102022-09-13 21:04:37 -07007108 u32 *mgmt;
7109 unsigned int akm_count = 1, i;
7110
7111 /*
7112 * Make sure the driver has capability to handle default AKM in
7113 * key_mgmt_suite plus allowed AKMs in allowed_key_mgmts.
7114 */
7115 if (drv->capa.max_num_akms <=
7116 num_bits_set(params->allowed_key_mgmts)) {
7117 wpa_printf(MSG_INFO,
7118 "nl80211: Not enough support for the allowed AKMs (max_num_akms=%u <= num_bits_set=%u)",
7119 drv->capa.max_num_akms,
7120 num_bits_set(params->allowed_key_mgmts));
7121 return -1;
7122 }
7123
7124 mgmt = os_malloc(sizeof(u32) * drv->capa.max_num_akms);
7125 if (!mgmt)
7126 return -1;
7127
7128 mgmt[0] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007129
7130 switch (params->key_mgmt_suite) {
7131 case WPA_KEY_MGMT_CCKM:
Sunil Ravi89eba102022-09-13 21:04:37 -07007132 mgmt[0] = RSN_AUTH_KEY_MGMT_CCKM;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007133 break;
7134 case WPA_KEY_MGMT_IEEE8021X:
Sunil Ravi89eba102022-09-13 21:04:37 -07007135 mgmt[0] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007136 break;
7137 case WPA_KEY_MGMT_FT_IEEE8021X:
Sunil Ravi89eba102022-09-13 21:04:37 -07007138 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007139 break;
7140 case WPA_KEY_MGMT_FT_PSK:
Sunil Ravi89eba102022-09-13 21:04:37 -07007141 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_PSK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007142 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07007143 case WPA_KEY_MGMT_IEEE8021X_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07007144 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07007145 break;
7146 case WPA_KEY_MGMT_PSK_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07007147 mgmt[0] = RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07007148 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007149 case WPA_KEY_MGMT_OSEN:
Sunil Ravi89eba102022-09-13 21:04:37 -07007150 mgmt[0] = RSN_AUTH_KEY_MGMT_OSEN;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007151 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07007152 case WPA_KEY_MGMT_SAE:
Sunil Ravi89eba102022-09-13 21:04:37 -07007153 mgmt[0] = RSN_AUTH_KEY_MGMT_SAE;
7154 break;
7155 case WPA_KEY_MGMT_SAE_EXT_KEY:
7156 mgmt[0] = RSN_AUTH_KEY_MGMT_SAE_EXT_KEY;
Hai Shalom021b0b52019-04-10 11:17:58 -07007157 break;
7158 case WPA_KEY_MGMT_FT_SAE:
Sunil Ravi89eba102022-09-13 21:04:37 -07007159 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_SAE;
7160 break;
7161 case WPA_KEY_MGMT_FT_SAE_EXT_KEY:
7162 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY;
Hai Shalom021b0b52019-04-10 11:17:58 -07007163 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007164 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
Sunil Ravi89eba102022-09-13 21:04:37 -07007165 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007166 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007167 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
Sunil Ravi89eba102022-09-13 21:04:37 -07007168 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007169 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07007170 case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
Sunil Ravi89eba102022-09-13 21:04:37 -07007171 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384;
Hai Shalom021b0b52019-04-10 11:17:58 -07007172 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007173 case WPA_KEY_MGMT_FILS_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07007174 mgmt[0] = RSN_AUTH_KEY_MGMT_FILS_SHA256;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007175 break;
7176 case WPA_KEY_MGMT_FILS_SHA384:
Sunil Ravi89eba102022-09-13 21:04:37 -07007177 mgmt[0] = RSN_AUTH_KEY_MGMT_FILS_SHA384;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007178 break;
7179 case WPA_KEY_MGMT_FT_FILS_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07007180 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007181 break;
7182 case WPA_KEY_MGMT_FT_FILS_SHA384:
Sunil Ravi89eba102022-09-13 21:04:37 -07007183 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007184 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007185 case WPA_KEY_MGMT_OWE:
Sunil Ravi89eba102022-09-13 21:04:37 -07007186 mgmt[0] = RSN_AUTH_KEY_MGMT_OWE;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007187 break;
7188 case WPA_KEY_MGMT_DPP:
Sunil Ravi89eba102022-09-13 21:04:37 -07007189 mgmt[0] = RSN_AUTH_KEY_MGMT_DPP;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007190 break;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007191 case WPA_KEY_MGMT_IEEE8021X_SHA384:
7192 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SHA384;
7193 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007194 case WPA_KEY_MGMT_PSK:
7195 default:
Sunil Ravi89eba102022-09-13 21:04:37 -07007196 mgmt[0] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007197 break;
7198 }
Sunil Ravi89eba102022-09-13 21:04:37 -07007199
7200 if (drv->capa.max_num_akms > 1) {
7201 akm_count += wpa_key_mgmt_to_suites(
7202 params->allowed_key_mgmts, &mgmt[1],
7203 drv->capa.max_num_akms - 1);
7204 }
7205
7206 for (i = 0; i < akm_count; i++)
7207 wpa_printf(MSG_DEBUG, " * akm[%d]=0x%x", i, mgmt[i]);
7208
7209 if (nla_put(msg, NL80211_ATTR_AKM_SUITES,
7210 akm_count * sizeof(u32), mgmt)) {
7211 os_free(mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007212 return -1;
Sunil Ravi89eba102022-09-13 21:04:37 -07007213 }
7214
7215 os_free(mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007216 }
7217
Isaac Chiou6ce580d2024-04-24 17:07:24 +08007218#if (defined(CONFIG_DRIVER_NL80211_BRCM) && !defined(WIFI_BRCM_OPEN_SOURCE_MULTI_AKM)) || \
7219 defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawad14709082022-03-17 14:25:11 +05307220 if (IS_CROSS_AKM_ROAM_KEY_MGMT(params->key_mgmt_suite)) {
7221 int num_suites;
7222 u32 suites[NL80211_MAX_NR_AKM_SUITES];
7223
7224 wpa_printf(MSG_INFO, "nl80211: key_mgmt_suites=0x%x",
7225 params->key_mgmt_suite);
7226 num_suites = wpa_cross_akm_key_mgmt_to_suites(params->key_mgmt_suite,
7227 suites, ARRAY_SIZE(suites));
7228 if (num_suites &&
7229 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32), suites)) {
7230 wpa_printf(MSG_ERROR, "Updating multi akm_suite failed");
7231 return -1;
7232 }
7233 }
Isaac Chiou6ce580d2024-04-24 17:07:24 +08007234#endif /* (CONFIG_DRIVER_NL80211_BRCM && !WIFI_BRCM_OPEN_SOURCE_MULTI_AKM) ||
7235 * CONFIG_DRIVER_NL80211_SYNA */
Hai Shalomc3565922019-10-28 11:58:20 -07007236 if (params->req_handshake_offload &&
Hai Shalom74f70d42019-02-11 14:42:39 -08007237 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)) {
7238 wpa_printf(MSG_DEBUG, " * WANT_1X_4WAY_HS");
7239 if (nla_put_flag(msg, NL80211_ATTR_WANT_1X_4WAY_HS))
7240 return -1;
7241 }
7242
Roshan Pius3a1667e2018-07-03 15:17:14 -07007243 /* Add PSK in case of 4-way handshake offload */
7244 if (params->psk &&
Hai Shalom74f70d42019-02-11 14:42:39 -08007245 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07007246 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
7247 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
7248 return -1;
7249 }
7250
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007251 if (nl80211_put_control_port(drv, msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007252 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007253
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07007254 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
7255 (params->pairwise_suite == WPA_CIPHER_NONE ||
7256 params->pairwise_suite == WPA_CIPHER_WEP104 ||
7257 params->pairwise_suite == WPA_CIPHER_WEP40) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007258 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07007259 return -1;
7260
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007261 if (params->rrm_used) {
7262 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007263 if ((!((drv_rrm_flags &
7264 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
7265 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
7266 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007267 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
7268 return -1;
7269 }
7270
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007271 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007272 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007273
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007274 if (params->p2p)
7275 wpa_printf(MSG_DEBUG, " * P2P group");
7276
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007277 if (params->pbss) {
7278 wpa_printf(MSG_DEBUG, " * PBSS");
7279 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
7280 return -1;
7281 }
7282
Dmitry Shmidte4663042016-04-04 10:07:49 -07007283 drv->connect_reassoc = 0;
7284 if (params->prev_bssid) {
7285 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
7286 MAC2STR(params->prev_bssid));
7287 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
7288 params->prev_bssid))
7289 return -1;
7290 drv->connect_reassoc = 1;
7291 }
7292
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007293 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
7294 nl80211_put_fils_connect_params(drv, params, msg) != 0)
7295 return -1;
7296
Sunil Ravi89eba102022-09-13 21:04:37 -07007297 if ((wpa_key_mgmt_sae(params->key_mgmt_suite) ||
7298 wpa_key_mgmt_sae(params->allowed_key_mgmts)) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07007299 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
7300 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
7301 return -1;
7302
Sunil Ravi036cec52023-03-29 11:35:17 -07007303 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
7304 nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT))
7305 return -1;
7306
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007307 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007308}
7309
7310
Sunil Ravic0f5d412024-09-11 22:12:49 +00007311#ifdef CONFIG_DRIVER_NL80211_QCA
7312static void connect_ext_feature_set(u8 *features,
7313 enum qca_wlan_connect_ext_features idx)
7314{
7315 u8 *idx_byte = &features[idx / 8];
7316
7317 *idx_byte |= BIT(idx % 8);
7318}
7319#endif /* CONFIG_DRIVER_NL80211_QCA */
7320
7321
7322static int nl80211_connect_ext(struct wpa_driver_nl80211_data *drv,
7323 struct wpa_driver_associate_params *params)
7324{
7325#ifdef CONFIG_DRIVER_NL80211_QCA
7326 struct nl_msg *msg;
7327 struct nlattr *attr;
7328 u8 features[(NUM_QCA_CONNECT_EXT_FEATURES + 7) / 8] = {};
7329
7330 if (!drv->connect_ext_vendor_cmd_avail)
7331 return -1;
7332
7333 wpa_printf(MSG_DEBUG, "nl80211: Connect_ext (ifindex=%d)",
7334 drv->ifindex);
7335
7336 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
7337 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
7338 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
7339 QCA_NL80211_VENDOR_SUBCMD_CONNECT_EXT))
7340 goto fail;
7341
7342 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
7343 if (!attr)
7344 goto fail;
7345
7346 if (params->rsn_overriding) {
7347 wpa_printf(MSG_DEBUG, "- RSN overriding");
7348 connect_ext_feature_set(features, QCA_CONNECT_EXT_FEATURE_RSNO);
7349 }
7350
7351 if (nla_put(msg, QCA_WLAN_VENDOR_ATTR_CONNECT_EXT_FEATURES,
7352 sizeof(features), features))
7353 goto fail;
7354
7355 nla_nest_end(msg, attr);
7356
7357 return send_and_recv_cmd(drv, msg);
7358fail:
7359 nlmsg_free(msg);
7360#endif /* CONFIG_DRIVER_NL80211_QCA */
7361 return -1;
7362}
7363
7364
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007365static int wpa_driver_nl80211_try_connect(
7366 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007367 struct wpa_driver_associate_params *params,
Hai Shalomc1a21442022-02-04 13:43:00 -08007368 struct i802_bss *bss)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007369{
7370 struct nl_msg *msg;
7371 enum nl80211_auth_type type;
7372 int ret;
7373 int algs;
7374
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007375#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007376 if (params->req_key_mgmt_offload && params->psk &&
Sunil Ravi89eba102022-09-13 21:04:37 -07007377 (wpa_key_mgmt_wpa_psk_no_sae(params->key_mgmt_suite) ||
7378 wpa_key_mgmt_wpa_psk_no_sae(params->allowed_key_mgmts))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007379 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
7380 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
7381 if (ret)
7382 return ret;
7383 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007384#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007385
Sunil Ravic0f5d412024-09-11 22:12:49 +00007386 nl80211_connect_ext(drv, params);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007387 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007388 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007389 if (!msg)
7390 return -1;
7391
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007392 ret = nl80211_connect_common(drv, params, msg);
7393 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007394 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007395
Roshan Pius3a1667e2018-07-03 15:17:14 -07007396 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
7397 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
7398 goto fail;
7399
7400 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
7401 (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
7402 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
7403 goto fail;
7404
Hai Shalom60840252021-02-19 19:02:11 -08007405#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -07007406 if ((wpa_key_mgmt_sae(params->key_mgmt_suite) ||
7407 wpa_key_mgmt_sae(params->allowed_key_mgmts)) &&
Hai Shalom60840252021-02-19 19:02:11 -08007408 nl80211_put_sae_pwe(msg, params->sae_pwe) < 0)
7409 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007410
7411 /* Add SAE password in case of SAE authentication offload */
7412 if ((params->sae_password || params->passphrase) &&
7413 (drv->capa.flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_STA)) {
7414 const char *password;
7415 size_t pwd_len;
7416
7417 if (params->sae_password && params->sae_password_id) {
7418 wpa_printf(MSG_INFO,
7419 "nl80211: Use of SAE password identifiers not supported with driver-based SAE");
7420 goto fail;
7421 }
7422
7423 password = params->sae_password;
7424 if (!password)
7425 password = params->passphrase;
7426 pwd_len = os_strlen(password);
7427 wpa_printf(MSG_DEBUG, " * SAE password");
7428 if (nla_put(msg, NL80211_ATTR_SAE_PASSWORD, pwd_len, password))
7429 goto fail;
7430 }
Hai Shalom60840252021-02-19 19:02:11 -08007431#endif /* CONFIG_SAE */
7432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433 algs = 0;
7434 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7435 algs++;
7436 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7437 algs++;
7438 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7439 algs++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007440 if (params->auth_alg & WPA_AUTH_ALG_FILS)
7441 algs++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007442 if (params->auth_alg & WPA_AUTH_ALG_FT)
7443 algs++;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007444 if (params->auth_alg & WPA_AUTH_ALG_SAE)
7445 algs++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007446 if (algs > 1) {
7447 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
7448 "selection");
7449 goto skip_auth_type;
7450 }
7451
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007452 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007453 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007454 if (type == NL80211_AUTHTYPE_MAX ||
7455 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007456 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007457
7458skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007459 ret = nl80211_set_conn_keys(params, msg);
7460 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007461 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007462
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007463 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
7464 goto fail;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007465 ret = send_and_recv(drv, bss->nl_connect, msg, NULL, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007466 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007467 msg = NULL;
7468 if (ret) {
7469 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7470 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007471 } else {
Hai Shalom60840252021-02-19 19:02:11 -08007472#ifdef CONFIG_DRIVER_NL80211_QCA
7473 drv->roam_indication_done = false;
7474#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007475 wpa_printf(MSG_DEBUG,
7476 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007477 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007478
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007479fail:
Hai Shalom74f70d42019-02-11 14:42:39 -08007480 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007481 nlmsg_free(msg);
7482 return ret;
7483
7484}
7485
7486
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007487static int wpa_driver_nl80211_connect(
7488 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007489 struct wpa_driver_associate_params *params,
Hai Shalomc1a21442022-02-04 13:43:00 -08007490 struct i802_bss *bss)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007491{
Jithu Jancea7c60b42014-12-03 18:54:40 +05307492 int ret;
7493
7494 /* Store the connection attempted bssid for future use */
7495 if (params->bssid)
7496 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
7497 else
7498 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
7499
Hai Shalomc1a21442022-02-04 13:43:00 -08007500 ret = wpa_driver_nl80211_try_connect(drv, params, bss);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007501 if (ret == -EALREADY) {
7502 /*
7503 * cfg80211 does not currently accept new connections if
7504 * we are already connected. As a workaround, force
7505 * disconnection and try again.
7506 */
7507 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7508 "disconnecting before reassociation "
7509 "attempt");
7510 if (wpa_driver_nl80211_disconnect(
Hai Shalomc1a21442022-02-04 13:43:00 -08007511 drv, WLAN_REASON_PREV_AUTH_NOT_VALID, bss))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007512 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007513 ret = wpa_driver_nl80211_try_connect(drv, params, bss);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007514 }
7515 return ret;
7516}
7517
7518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007519static int wpa_driver_nl80211_associate(
7520 void *priv, struct wpa_driver_associate_params *params)
7521{
7522 struct i802_bss *bss = priv;
7523 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007524 struct nl80211_err_info err_info;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007525 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007526 struct nl_msg *msg;
7527
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007528 nl80211_unmask_11b_rates(bss);
7529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007530 if (params->mode == IEEE80211_MODE_AP)
7531 return wpa_driver_nl80211_ap(drv, params);
7532
7533 if (params->mode == IEEE80211_MODE_IBSS)
7534 return wpa_driver_nl80211_ibss(drv, params);
7535
7536 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007537 enum nl80211_iftype nlmode = params->p2p ?
7538 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
7539
7540 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007541 return -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08007542
Hai Shalomc1a21442022-02-04 13:43:00 -08007543 return wpa_driver_nl80211_connect(drv, params, bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007544 }
7545
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007546 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007547
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007548 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
7549 drv->ifindex);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007550 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007551 if (!msg)
7552 return -1;
7553
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007554 ret = nl80211_connect_common(drv, params, msg);
7555 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007556 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007557
Roshan Pius3a1667e2018-07-03 15:17:14 -07007558 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
7559 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
7560 goto fail;
7561
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08007562 if (params->fils_kek) {
7563 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
7564 (unsigned int) params->fils_kek_len);
7565 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
7566 params->fils_kek))
7567 goto fail;
7568 }
7569 if (params->fils_nonces) {
7570 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
7571 params->fils_nonces,
7572 params->fils_nonces_len);
7573 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
7574 params->fils_nonces_len, params->fils_nonces))
7575 goto fail;
7576 }
7577
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007578 if (!TEST_FAIL_TAG("assoc")) {
7579 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
7580 goto fail;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007581 ret = send_and_recv(drv, drv->first_bss->nl_connect,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007582 msg, NULL, NULL, NULL, NULL, &err_info);
7583 msg = NULL;
7584 } else {
7585 int i;
7586
7587 /* Error and force TEST_FAIL checking for each link */
7588 ret = -EINVAL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00007589 for_each_link(params->mld_params.valid_links, i) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007590 if (TEST_FAIL_TAG("link"))
7591 err_info.link_id = i;
7592 }
7593 }
7594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007595 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007596 wpa_dbg(drv->ctx, MSG_DEBUG,
7597 "nl80211: MLME command failed (assoc): ret=%d (%s)",
7598 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007599 nl80211_dump_scan(drv);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007600
7601 /* Mark failed link within params */
7602 if (err_info.link_id >= 0) {
7603 if (err_info.link_id >= MAX_NUM_MLD_LINKS ||
7604 !(params->mld_params.valid_links &
7605 BIT(err_info.link_id))) {
7606 wpa_printf(MSG_DEBUG,
7607 "nl80211: Invalid errorred link_id %d",
7608 err_info.link_id);
7609 goto fail;
7610 }
7611 params->mld_params.mld_links[err_info.link_id].error =
7612 ret;
7613 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007614 } else {
7615 wpa_printf(MSG_DEBUG,
7616 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007618
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007619fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007620 nlmsg_free(msg);
7621 return ret;
7622}
7623
7624
7625static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007626 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007627{
7628 struct nl_msg *msg;
7629 int ret = -ENOBUFS;
7630
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007631 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
7632 ifindex, mode, nl80211_iftype_str(mode));
7633
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007634 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
7635 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
7636 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007637
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007638 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007639 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007640 if (!ret)
7641 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007642fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007643 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007644 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7645 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7646 return ret;
7647}
7648
7649
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007650static int wpa_driver_nl80211_set_mode_impl(
7651 struct i802_bss *bss,
7652 enum nl80211_iftype nlmode,
7653 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007654{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007655 struct wpa_driver_nl80211_data *drv = bss->drv;
7656 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007657 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007658 int was_ap = is_ap_interface(drv->nlmode);
7659 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007660 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007661
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007662 if (TEST_FAIL())
7663 return -1;
7664
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007665 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7666 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
7667 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007668
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007669 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007670 drv->nlmode = nlmode;
7671 ret = 0;
7672 goto done;
7673 }
7674
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007675 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007676 return -1;
7677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007678 if (nlmode == drv->nlmode) {
7679 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7680 "requested mode - ignore error");
7681 ret = 0;
7682 goto done; /* Already in the requested mode */
7683 }
7684
7685 /* mac80211 doesn't allow mode changes while the device is up, so
7686 * take the device down, try to set the mode again, and bring the
7687 * device back up.
7688 */
7689 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7690 "interface down");
7691 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007692 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007693 if (res == -EACCES || res == -ENODEV)
7694 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007695 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007696 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7697 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007698 os_sleep(0, 100000);
7699 continue;
7700 }
7701
7702 /*
7703 * Setting the mode will fail for some drivers if the phy is
7704 * on a frequency that the mode is disallowed in.
7705 */
7706 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007707 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007708 if (res) {
7709 wpa_printf(MSG_DEBUG,
7710 "nl80211: Failed to set frequency on interface");
7711 }
7712 }
7713
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007714 if (i == 0 && was_ap && !is_ap_interface(nlmode) &&
7715 bss->brname[0] &&
7716 (bss->added_if_into_bridge || bss->already_in_bridge)) {
7717 wpa_printf(MSG_DEBUG,
7718 "nl80211: Remove AP interface %s temporarily from the bridge %s to allow its mode to be set to STATION",
7719 bss->ifname, bss->brname);
7720 if (linux_br_del_if(drv->global->ioctl_sock,
7721 bss->brname, bss->ifname) < 0)
7722 wpa_printf(MSG_INFO,
7723 "nl80211: Failed to remove interface %s from bridge %s: %s",
7724 bss->ifname, bss->brname,
7725 strerror(errno));
7726 }
7727
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007728 /* Try to set the mode again while the interface is down */
7729 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7730 if (mode_switch_res == -EBUSY) {
7731 wpa_printf(MSG_DEBUG,
7732 "nl80211: Delaying mode set while interface going down");
7733 os_sleep(0, 100000);
7734 continue;
7735 }
7736 ret = mode_switch_res;
7737 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007738 }
7739
7740 if (!ret) {
7741 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7742 "interface is down");
7743 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007744 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007745 }
7746
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007747 /* Bring the interface back up */
7748 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
7749 if (res != 0) {
7750 wpa_printf(MSG_DEBUG,
7751 "nl80211: Failed to set interface up after switching mode");
7752 ret = -1;
7753 }
7754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007755done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007756 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007757 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7758 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007759 return ret;
7760 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007761
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007762 if (is_p2p_net_interface(nlmode)) {
7763 wpa_printf(MSG_DEBUG,
7764 "nl80211: Interface %s mode change to P2P - disable 11b rates",
7765 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007766 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007767 } else if (drv->disabled_11b_rates) {
7768 wpa_printf(MSG_DEBUG,
7769 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
7770 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007771 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007772 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007773
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007774 if (is_ap_interface(nlmode)) {
7775 nl80211_mgmt_unsubscribe(bss, "start AP");
7776 /* Setup additional AP mode functionality if needed */
7777 if (nl80211_setup_ap(bss))
7778 return -1;
7779 } else if (was_ap) {
7780 /* Remove additional AP mode functionality */
7781 nl80211_teardown_ap(bss);
7782 } else {
7783 nl80211_mgmt_unsubscribe(bss, "mode change");
7784 }
7785
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007786 if (is_mesh_interface(nlmode) &&
7787 nl80211_mgmt_subscribe_mesh(bss))
7788 return -1;
7789
Dmitry Shmidt04949592012-07-19 12:16:46 -07007790 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007791 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007792 nl80211_mgmt_subscribe_non_ap(bss) < 0)
7793 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7794 "frame processing - ignore for now");
7795
7796 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007797}
7798
7799
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007800void nl80211_restore_ap_mode(struct i802_bss *bss)
7801{
7802 struct wpa_driver_nl80211_data *drv = bss->drv;
7803 int was_ap = is_ap_interface(drv->nlmode);
leslc3979c32021-03-29 22:34:02 +08007804 int br_ifindex;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007805
7806 wpa_driver_nl80211_set_mode(bss, drv->ap_scan_as_station);
7807 if (!was_ap && is_ap_interface(drv->ap_scan_as_station) &&
7808 bss->brname[0] &&
7809 (bss->added_if_into_bridge || bss->already_in_bridge)) {
7810 wpa_printf(MSG_DEBUG,
7811 "nl80211: Add AP interface %s back into the bridge %s",
7812 bss->ifname, bss->brname);
7813 if (linux_br_add_if(drv->global->ioctl_sock, bss->brname,
7814 bss->ifname) < 0) {
7815 wpa_printf(MSG_WARNING,
7816 "nl80211: Failed to add interface %s into bridge %s: %s",
7817 bss->ifname, bss->brname, strerror(errno));
7818 }
leslc3979c32021-03-29 22:34:02 +08007819 br_ifindex = if_nametoindex(bss->brname);
7820 add_ifidx(drv, br_ifindex, drv->ifindex);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007821 }
7822 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
7823}
7824
7825
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007826int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7827 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007828{
7829 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
7830}
7831
7832
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007833static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
7834 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007835{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007836 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007837 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007838}
7839
7840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007841static int wpa_driver_nl80211_get_capa(void *priv,
7842 struct wpa_driver_capa *capa)
7843{
7844 struct i802_bss *bss = priv;
7845 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07007846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007847 if (!drv->has_capability)
7848 return -1;
7849 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007850 if (drv->extended_capa && drv->extended_capa_mask) {
7851 capa->extended_capa = drv->extended_capa;
7852 capa->extended_capa_mask = drv->extended_capa_mask;
7853 capa->extended_capa_len = drv->extended_capa_len;
7854 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007855
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007856 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007857}
7858
7859
7860static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7861{
7862 struct i802_bss *bss = priv;
7863 struct wpa_driver_nl80211_data *drv = bss->drv;
7864
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007865 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
7866 bss->ifname, drv->operstate, state,
7867 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007868 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007869 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007870 state ? IF_OPER_UP : IF_OPER_DORMANT);
7871}
7872
7873
7874static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7875{
7876 struct i802_bss *bss = priv;
7877 struct wpa_driver_nl80211_data *drv = bss->drv;
7878 struct nl_msg *msg;
7879 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007880 int ret;
Sunil Ravi89eba102022-09-13 21:04:37 -07007881 const u8 *connected_addr = drv->sta_mlo_info.valid_links ?
7882 drv->sta_mlo_info.ap_mld_addr : drv->bssid;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007883
Sunil Ravi89eba102022-09-13 21:04:37 -07007884 if (!drv->associated && is_zero_ether_addr(connected_addr) &&
7885 !authorized) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007886 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
7887 return 0;
7888 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007889
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007890 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
Sunil Ravi89eba102022-09-13 21:04:37 -07007891 MACSTR, authorized ? "" : "un", MAC2STR(connected_addr));
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007893 os_memset(&upd, 0, sizeof(upd));
7894 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7895 if (authorized)
7896 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007897
7898 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
Sunil Ravi89eba102022-09-13 21:04:37 -07007899 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007900 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
7901 nlmsg_free(msg);
7902 return -ENOBUFS;
7903 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007904
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007905 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007906 if (!ret)
7907 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007908 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
7909 ret, strerror(-ret));
7910 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007911}
7912
7913
Jouni Malinen75ecf522011-06-27 15:19:46 -07007914/* Set kernel driver on given frequency (MHz) */
7915static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007916{
Jouni Malinen75ecf522011-06-27 15:19:46 -07007917 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007918 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007919}
7920
7921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007922static inline int min_int(int a, int b)
7923{
7924 if (a < b)
7925 return a;
7926 return b;
7927}
7928
7929
7930static int get_key_handler(struct nl_msg *msg, void *arg)
7931{
7932 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7933 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7934
7935 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7936 genlmsg_attrlen(gnlh, 0), NULL);
7937
7938 /*
7939 * TODO: validate the key index and mac address!
7940 * Otherwise, there's a race condition as soon as
7941 * the kernel starts sending key notifications.
7942 */
7943
7944 if (tb[NL80211_ATTR_KEY_SEQ])
7945 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7946 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
Hai Shalom021b0b52019-04-10 11:17:58 -07007947 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007948 return NL_SKIP;
7949}
7950
7951
7952static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007953 int idx, int link_id, u8 *seq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007954{
7955 struct i802_bss *bss = priv;
7956 struct wpa_driver_nl80211_data *drv = bss->drv;
7957 struct nl_msg *msg;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007958 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007959
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007960 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
7961 NL80211_CMD_GET_KEY);
7962 if (!msg ||
7963 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007964 (link_id != NL80211_DRV_LINK_ID_NA &&
7965 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007966 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
7967 nlmsg_free(msg);
7968 return -ENOBUFS;
7969 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007970
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007971 os_memset(seq, 0, 6);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007972 res = send_and_recv_resp(drv, msg, get_key_handler, seq);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007973 if (res) {
7974 wpa_printf(MSG_DEBUG,
7975 "nl80211: Failed to get current TX sequence for a key (link_id=%d idx=%d): %d (%s)",
7976 link_id, idx, res, strerror(-res));
7977 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007978
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007979 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007980}
7981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007982
7983static int i802_set_rts(void *priv, int rts)
7984{
7985 struct i802_bss *bss = priv;
7986 struct wpa_driver_nl80211_data *drv = bss->drv;
7987 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007988 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007989 u32 val;
7990
Hai Shalom021b0b52019-04-10 11:17:58 -07007991 if (rts >= 2347 || rts == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007992 val = (u32) -1;
7993 else
7994 val = rts;
7995
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007996 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007997 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
7998 nlmsg_free(msg);
7999 return -ENOBUFS;
8000 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008001
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008002 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008003 if (!ret)
8004 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008005 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
8006 "%d (%s)", rts, ret, strerror(-ret));
8007 return ret;
8008}
8009
8010
8011static int i802_set_frag(void *priv, int frag)
8012{
8013 struct i802_bss *bss = priv;
8014 struct wpa_driver_nl80211_data *drv = bss->drv;
8015 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008016 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008017 u32 val;
8018
Hai Shalom021b0b52019-04-10 11:17:58 -07008019 if (frag >= 2346 || frag == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008020 val = (u32) -1;
8021 else
8022 val = frag;
8023
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008024 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008025 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
8026 nlmsg_free(msg);
8027 return -ENOBUFS;
8028 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008029
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008030 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008031 if (!ret)
8032 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008033 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
8034 "%d: %d (%s)", frag, ret, strerror(-ret));
8035 return ret;
8036}
8037
8038
Sunil Ravi7f769292024-07-23 22:21:32 +00008039static int i802_flush(void *priv, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008040{
8041 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008042 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008043 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008044
Sunil Ravi7f769292024-07-23 22:21:32 +00008045 if (link_id == NL80211_DRV_LINK_ID_NA)
8046 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
8047 bss->ifname);
8048 else
8049 wpa_printf(MSG_DEBUG,
8050 "nl80211: flush -> DEL_STATION %s (with link %d)",
8051 bss->ifname, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008052
8053 /*
8054 * XXX: FIX! this needs to flush all VLANs too
8055 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008056 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008057 if (!msg)
8058 goto fail;
Sunil Ravi7f769292024-07-23 22:21:32 +00008059 if (link_id >= 0 && (bss->valid_links & BIT(link_id)) &&
8060 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
8061 goto fail;
8062
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008063 res = send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008064 if (res) {
8065 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
8066 "(%s)", res, strerror(-res));
8067 }
8068 return res;
Sunil Ravi7f769292024-07-23 22:21:32 +00008069fail:
8070 nlmsg_free(msg);
8071 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008072}
8073
8074
Hai Shalom81f62d82019-07-22 12:10:00 -07008075static void get_sta_tid_stats(struct hostap_sta_driver_data *data,
8076 struct nlattr *attr)
8077{
8078 struct nlattr *tid_stats[NL80211_TID_STATS_MAX + 1], *tidattr;
8079 struct nlattr *txq_stats[NL80211_TXQ_STATS_MAX + 1];
8080 static struct nla_policy txq_stats_policy[NL80211_TXQ_STATS_MAX + 1] = {
8081 [NL80211_TXQ_STATS_BACKLOG_BYTES] = { .type = NLA_U32 },
8082 [NL80211_TXQ_STATS_BACKLOG_PACKETS] = { .type = NLA_U32 },
8083 };
8084 int rem;
8085
8086 nla_for_each_nested(tidattr, attr, rem) {
8087 if (nla_parse_nested(tid_stats, NL80211_TID_STATS_MAX,
8088 tidattr, NULL) != 0 ||
8089 !tid_stats[NL80211_TID_STATS_TXQ_STATS] ||
8090 nla_parse_nested(txq_stats, NL80211_TXQ_STATS_MAX,
8091 tid_stats[NL80211_TID_STATS_TXQ_STATS],
8092 txq_stats_policy) != 0)
8093 continue;
8094 /* sum the backlogs over all TIDs for station */
8095 if (txq_stats[NL80211_TXQ_STATS_BACKLOG_BYTES])
8096 data->backlog_bytes += nla_get_u32(
8097 txq_stats[NL80211_TXQ_STATS_BACKLOG_BYTES]);
8098 if (txq_stats[NL80211_TXQ_STATS_BACKLOG_PACKETS])
8099 data->backlog_bytes += nla_get_u32(
8100 txq_stats[NL80211_TXQ_STATS_BACKLOG_PACKETS]);
8101 }
8102}
8103
8104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008105static int get_sta_handler(struct nl_msg *msg, void *arg)
8106{
8107 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8108 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8109 struct hostap_sta_driver_data *data = arg;
8110 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
8111 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
8112 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
8113 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
8114 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00008115 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008116 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
8117 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00008118 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03008119 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00008120 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
8121 [NL80211_STA_INFO_CONNECTED_TIME] = { .type = NLA_U32 },
8122 [NL80211_STA_INFO_BEACON_LOSS] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08008123 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
8124 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00008125 [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 },
8126 [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64 },
8127 [NL80211_STA_INFO_BEACON_RX] = { .type = NLA_U64 },
8128 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8},
Hai Shalom81f62d82019-07-22 12:10:00 -07008129 [NL80211_STA_INFO_RX_DURATION] = { .type = NLA_U64 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00008130 [NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
8131 [NL80211_STA_INFO_ACK_SIGNAL_AVG] = { .type = NLA_S8 },
8132 [NL80211_STA_INFO_RX_MPDUS] = { .type = NLA_U32 },
8133 [NL80211_STA_INFO_FCS_ERROR_COUNT] = { .type = NLA_U32 },
Hai Shalom81f62d82019-07-22 12:10:00 -07008134 [NL80211_STA_INFO_TX_DURATION] = { .type = NLA_U64 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008135 };
8136 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
8137 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
8138 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
8139 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
8140 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
8141 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
8142 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
8143 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00008144 [NL80211_RATE_INFO_HE_MCS] = { .type = NLA_U8 },
8145 [NL80211_RATE_INFO_HE_NSS] = { .type = NLA_U8 },
Sunil Ravi036cec52023-03-29 11:35:17 -07008146 [NL80211_RATE_INFO_HE_GI] = { .type = NLA_U8 },
8147 [NL80211_RATE_INFO_HE_DCM] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008148 };
8149
8150 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8151 genlmsg_attrlen(gnlh, 0), NULL);
8152
8153 /*
8154 * TODO: validate the interface and mac address!
8155 * Otherwise, there's a race condition as soon as
8156 * the kernel starts sending station notifications.
8157 */
8158
8159 if (!tb[NL80211_ATTR_STA_INFO]) {
8160 wpa_printf(MSG_DEBUG, "sta stats missing!");
8161 return NL_SKIP;
8162 }
8163 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
8164 tb[NL80211_ATTR_STA_INFO],
8165 stats_policy)) {
8166 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
8167 return NL_SKIP;
8168 }
8169
8170 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
8171 data->inactive_msec =
8172 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08008173 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008174 if (stats[NL80211_STA_INFO_RX_BYTES])
8175 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
8176 if (stats[NL80211_STA_INFO_TX_BYTES])
8177 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08008178 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
8179 stats[NL80211_STA_INFO_TX_BYTES64]) {
8180 /*
8181 * The driver supports 64-bit counters, so use them to override
8182 * the 32-bit values.
8183 */
8184 data->rx_bytes =
8185 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
8186 data->tx_bytes =
8187 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
8188 data->bytes_64bit = 1;
8189 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00008190 if (stats[NL80211_STA_INFO_SIGNAL])
8191 data->signal = (s8) nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008192 if (stats[NL80211_STA_INFO_RX_PACKETS])
8193 data->rx_packets =
8194 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
8195 if (stats[NL80211_STA_INFO_TX_PACKETS])
8196 data->tx_packets =
8197 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008198 if (stats[NL80211_STA_INFO_TX_RETRIES])
8199 data->tx_retry_count =
8200 nla_get_u32(stats[NL80211_STA_INFO_TX_RETRIES]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03008201 if (stats[NL80211_STA_INFO_TX_FAILED])
8202 data->tx_retry_failed =
8203 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008204 if (stats[NL80211_STA_INFO_SIGNAL_AVG])
8205 data->avg_signal =
8206 (s8) nla_get_u8(stats[NL80211_STA_INFO_SIGNAL_AVG]);
Hai Shalomc1a21442022-02-04 13:43:00 -08008207 if (stats[NL80211_STA_INFO_CONNECTED_TIME]) {
8208 data->connected_sec =
8209 nla_get_u32(stats[NL80211_STA_INFO_CONNECTED_TIME]);
8210 data->flags |= STA_DRV_DATA_CONN_TIME;
8211 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00008212 if (stats[NL80211_STA_INFO_BEACON_LOSS])
8213 data->beacon_loss_count =
8214 nla_get_u32(stats[NL80211_STA_INFO_BEACON_LOSS]);
8215 if (stats[NL80211_STA_INFO_EXPECTED_THROUGHPUT])
8216 data->expected_throughput =
8217 nla_get_u32(stats[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
8218 if (stats[NL80211_STA_INFO_RX_DROP_MISC])
8219 data->rx_drop_misc =
8220 nla_get_u64(stats[NL80211_STA_INFO_RX_DROP_MISC]);
8221 if (stats[NL80211_STA_INFO_BEACON_RX])
8222 data->beacons_count =
8223 nla_get_u64(stats[NL80211_STA_INFO_BEACON_RX]);
8224 if (stats[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
8225 data->avg_beacon_signal =
8226 (s8) nla_get_u8(stats[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
8227 if (stats[NL80211_STA_INFO_RX_DURATION])
8228 data->rx_airtime =
8229 nla_get_u64(stats[NL80211_STA_INFO_RX_DURATION]);
8230 if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
8231 data->last_ack_rssi =
8232 nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
8233 data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
8234 }
8235 if (stats[NL80211_STA_INFO_ACK_SIGNAL_AVG])
8236 data->avg_ack_signal =
8237 nla_get_s8(stats[NL80211_STA_INFO_ACK_SIGNAL_AVG]);
8238 if (stats[NL80211_STA_INFO_RX_MPDUS])
8239 data->rx_mpdus = nla_get_u32(stats[NL80211_STA_INFO_RX_MPDUS]);
8240 if (stats[NL80211_STA_INFO_FCS_ERROR_COUNT])
8241 data->fcs_error_count =
8242 nla_get_u32(stats[NL80211_STA_INFO_FCS_ERROR_COUNT]);
8243 if (stats[NL80211_STA_INFO_TX_DURATION])
8244 data->tx_airtime =
8245 nla_get_u64(stats[NL80211_STA_INFO_TX_DURATION]);
Hai Shalomc1a21442022-02-04 13:43:00 -08008246
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008247 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
8248 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
8249 stats[NL80211_STA_INFO_TX_BITRATE],
8250 rate_policy) == 0) {
8251 if (rate[NL80211_RATE_INFO_BITRATE32])
8252 data->current_tx_rate =
8253 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
8254 else if (rate[NL80211_RATE_INFO_BITRATE])
8255 data->current_tx_rate =
8256 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
8257
Sunil Ravi77d572f2023-01-17 23:58:31 +00008258 /* Convert from 100 kbps to kbps; it's a more convenient unit.
8259 * It's also safe up until ~1Tbps. */
8260 data->current_tx_rate = data->current_tx_rate * 100;
8261
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008262 if (rate[NL80211_RATE_INFO_MCS]) {
8263 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
8264 data->flags |= STA_DRV_DATA_TX_MCS;
8265 }
8266 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
8267 data->tx_vhtmcs =
8268 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
8269 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
8270 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008271 if (rate[NL80211_RATE_INFO_SHORT_GI]) {
8272 data->tx_guard_interval = GUARD_INTERVAL_0_4;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008273 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
Sunil Ravi036cec52023-03-29 11:35:17 -07008274 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008275 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
8276 data->tx_vht_nss =
8277 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
8278 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
8279 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00008280 if (rate[NL80211_RATE_INFO_HE_MCS]) {
8281 data->tx_hemcs =
8282 nla_get_u8(rate[NL80211_RATE_INFO_HE_MCS]);
8283 data->flags |= STA_DRV_DATA_TX_HE_MCS;
8284 }
8285 if (rate[NL80211_RATE_INFO_HE_NSS]) {
8286 data->tx_he_nss =
8287 nla_get_u8(rate[NL80211_RATE_INFO_HE_NSS]);
8288 data->flags |= STA_DRV_DATA_TX_HE_NSS;
8289 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008290 if (rate[NL80211_RATE_INFO_HE_GI]) {
8291 switch (nla_get_u8(rate[NL80211_RATE_INFO_HE_GI])) {
8292 case NL80211_RATE_INFO_HE_GI_0_8:
8293 data->tx_guard_interval = GUARD_INTERVAL_0_8;
8294 break;
8295 case NL80211_RATE_INFO_HE_GI_1_6:
8296 data->tx_guard_interval = GUARD_INTERVAL_1_6;
8297 break;
8298 case NL80211_RATE_INFO_HE_GI_3_2:
8299 data->tx_guard_interval = GUARD_INTERVAL_3_2;
8300 break;
8301 }
8302 data->flags |= STA_DRV_DATA_TX_HE_GI;
8303 }
8304 if (rate[NL80211_RATE_INFO_HE_DCM]) {
8305 data->tx_dcm =
8306 nla_get_u8(rate[NL80211_RATE_INFO_HE_DCM]);
8307 data->flags |= STA_DRV_DATA_TX_HE_DCM;
8308 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008309 }
8310
8311 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
8312 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
8313 stats[NL80211_STA_INFO_RX_BITRATE],
8314 rate_policy) == 0) {
8315 if (rate[NL80211_RATE_INFO_BITRATE32])
8316 data->current_rx_rate =
8317 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
8318 else if (rate[NL80211_RATE_INFO_BITRATE])
8319 data->current_rx_rate =
8320 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
8321
Sunil Ravi77d572f2023-01-17 23:58:31 +00008322 /* Convert from 100 kbps to kbps; it's a more convenient unit.
8323 * It's also safe up until ~1Tbps. */
8324 data->current_rx_rate = data->current_rx_rate * 100;
8325
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008326 if (rate[NL80211_RATE_INFO_MCS]) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00008327 data->rx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008328 data->flags |= STA_DRV_DATA_RX_MCS;
8329 }
8330 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
8331 data->rx_vhtmcs =
8332 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
8333 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
8334 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008335 if (rate[NL80211_RATE_INFO_SHORT_GI]) {
8336 data->rx_guard_interval = GUARD_INTERVAL_0_4;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008337 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
Sunil Ravi036cec52023-03-29 11:35:17 -07008338 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008339 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
8340 data->rx_vht_nss =
8341 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
8342 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
8343 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00008344 if (rate[NL80211_RATE_INFO_HE_MCS]) {
8345 data->rx_hemcs =
8346 nla_get_u8(rate[NL80211_RATE_INFO_HE_MCS]);
8347 data->flags |= STA_DRV_DATA_RX_HE_MCS;
8348 }
8349 if (rate[NL80211_RATE_INFO_HE_NSS]) {
8350 data->rx_he_nss =
8351 nla_get_u8(rate[NL80211_RATE_INFO_HE_NSS]);
8352 data->flags |= STA_DRV_DATA_RX_HE_NSS;
8353 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008354 if (rate[NL80211_RATE_INFO_HE_GI]) {
8355 switch (nla_get_u8(rate[NL80211_RATE_INFO_HE_GI])) {
8356 case NL80211_RATE_INFO_HE_GI_0_8:
8357 data->rx_guard_interval = GUARD_INTERVAL_0_8;
8358 break;
8359 case NL80211_RATE_INFO_HE_GI_1_6:
8360 data->rx_guard_interval = GUARD_INTERVAL_1_6;
8361 break;
8362 case NL80211_RATE_INFO_HE_GI_3_2:
8363 data->rx_guard_interval = GUARD_INTERVAL_3_2;
8364 break;
8365 }
8366 data->flags |= STA_DRV_DATA_RX_HE_GI;
8367 }
8368 if (rate[NL80211_RATE_INFO_HE_DCM]) {
8369 data->rx_dcm =
8370 nla_get_u8(rate[NL80211_RATE_INFO_HE_DCM]);
8371 data->flags |= STA_DRV_DATA_RX_HE_DCM;
8372 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008373 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008374
Hai Shalom81f62d82019-07-22 12:10:00 -07008375 if (stats[NL80211_STA_INFO_TID_STATS])
8376 get_sta_tid_stats(data, stats[NL80211_STA_INFO_TID_STATS]);
8377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008378 return NL_SKIP;
8379}
8380
Sunil Ravi77d572f2023-01-17 23:58:31 +00008381
8382int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
8383 const u8 *bssid,
8384 struct hostap_sta_driver_data *data)
8385{
8386 struct nl_msg *msg;
8387
8388 data->signal = -WPA_INVALID_NOISE;
8389 data->current_tx_rate = 0;
8390
8391 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
8392 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) {
8393 nlmsg_free(msg);
8394 return -ENOBUFS;
8395 }
8396
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008397 return send_and_recv_resp(drv, msg, get_sta_handler, data);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008398}
8399
8400
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008401static int i802_read_sta_data(struct i802_bss *bss,
8402 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008403 const u8 *addr)
8404{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008405 struct nl_msg *msg;
8406
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008407 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
8408 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8409 nlmsg_free(msg);
8410 return -ENOBUFS;
8411 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008412
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008413 return send_and_recv_resp(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008414}
8415
8416
8417static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008418 int cw_min, int cw_max, int burst_time,
8419 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008420{
8421 struct i802_bss *bss = priv;
8422 struct wpa_driver_nl80211_data *drv = bss->drv;
8423 struct nl_msg *msg;
8424 struct nlattr *txq, *params;
Hai Shalom74f70d42019-02-11 14:42:39 -08008425 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008426
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008427 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008428 if (!msg)
8429 return -1;
8430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008431 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
8432 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008433 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008434
8435 /* We are only sending parameters for a single TXQ at a time */
8436 params = nla_nest_start(msg, 1);
8437 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008438 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008439
8440 switch (queue) {
8441 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008442 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
8443 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008444 break;
8445 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008446 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
8447 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008448 break;
8449 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008450 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
8451 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008452 break;
8453 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008454 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
8455 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008456 break;
8457 }
8458 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
8459 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008460 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
8461 (burst_time * 100 + 16) / 32) ||
8462 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
8463 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
8464 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
8465 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008466
8467 nla_nest_end(msg, params);
8468
8469 nla_nest_end(msg, txq);
8470
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008471 if (link_id != NL80211_DRV_LINK_ID_NA &&
8472 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
8473 goto fail;
8474
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008475 res = send_and_recv_cmd(drv, msg);
Hai Shalom74f70d42019-02-11 14:42:39 -08008476 wpa_printf(MSG_DEBUG,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008477 "nl80211: link=%d: TX queue param set: queue=%d aifs=%d cw_min=%d cw_max=%d burst_time=%d --> res=%d",
8478 link_id, queue, aifs, cw_min, cw_max, burst_time, res);
Hai Shalom74f70d42019-02-11 14:42:39 -08008479 if (res == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008480 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008481 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008482fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008483 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008484 return -1;
8485}
8486
8487
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008488static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008489 const char *ifname, int vlan_id, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008490{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008491 struct wpa_driver_nl80211_data *drv = bss->drv;
8492 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008493 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008494
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008495 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
8496 ", ifname=%s[%d], vlan_id=%d)",
8497 bss->ifname, if_nametoindex(bss->ifname),
8498 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008499 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
8500 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07008501 (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD) &&
Hai Shalomfdcde762020-04-02 11:19:20 -07008502 nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id)) ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008503 (link_id != NL80211_DRV_LINK_ID_NA &&
8504 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008505 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
8506 nlmsg_free(msg);
8507 return -ENOBUFS;
8508 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008509
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008510 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008511 if (ret < 0) {
8512 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8513 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8514 MAC2STR(addr), ifname, vlan_id, ret,
8515 strerror(-ret));
8516 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008517 return ret;
8518}
8519
8520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008521static int i802_get_inact_sec(void *priv, const u8 *addr)
8522{
8523 struct hostap_sta_driver_data data;
8524 int ret;
8525
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08008526 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008527 data.inactive_msec = (unsigned long) -1;
8528 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008529 if (ret == -ENOENT)
8530 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008531 if (ret || data.inactive_msec == (unsigned long) -1)
8532 return -1;
8533 return data.inactive_msec / 1000;
8534}
8535
8536
8537static int i802_sta_clear_stats(void *priv, const u8 *addr)
8538{
8539#if 0
8540 /* TODO */
8541#endif
8542 return 0;
8543}
8544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008545
8546static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008547 u16 reason, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008548{
8549 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008550 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008551 struct ieee80211_mgmt mgmt;
Dmitry Shmidt29333592017-01-09 12:27:11 -08008552 u8 channel;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008553 struct i802_link *link = nl80211_get_link(bss, link_id);
Dmitry Shmidt29333592017-01-09 12:27:11 -08008554
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008555 if (ieee80211_freq_to_chan(link->freq, &channel) ==
Dmitry Shmidt29333592017-01-09 12:27:11 -08008556 HOSTAPD_MODE_IEEE80211AD) {
8557 /* Deauthentication is not used in DMG/IEEE 802.11ad;
8558 * disassociate the STA instead. */
8559 return i802_sta_disassoc(priv, own_addr, addr, reason);
8560 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008561
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008562 if (is_mesh_interface(drv->nlmode))
8563 return -1;
8564
Dmitry Shmidt04949592012-07-19 12:16:46 -07008565 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008566 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008568 memset(&mgmt, 0, sizeof(mgmt));
8569 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8570 WLAN_FC_STYPE_DEAUTH);
8571 memcpy(mgmt.da, addr, ETH_ALEN);
8572 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8573 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8574 mgmt.u.deauth.reason_code = host_to_le16(reason);
8575 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8576 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008577 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Sunil Ravi7f769292024-07-23 22:21:32 +00008578 0, NULL, 0, 0, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008579}
8580
8581
8582static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -07008583 u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008584{
8585 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008586 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008587 struct ieee80211_mgmt mgmt;
8588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008589 if (is_mesh_interface(drv->nlmode))
8590 return -1;
8591
Dmitry Shmidt04949592012-07-19 12:16:46 -07008592 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008593 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008595 memset(&mgmt, 0, sizeof(mgmt));
8596 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8597 WLAN_FC_STYPE_DISASSOC);
8598 memcpy(mgmt.da, addr, ETH_ALEN);
8599 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8600 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8601 mgmt.u.disassoc.reason_code = host_to_le16(reason);
8602 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8603 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008604 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008605 0, NULL, 0, 0, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008606}
8607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008608
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008609static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
8610{
8611 char buf[200], *pos, *end;
8612 int i, res;
8613
8614 pos = buf;
8615 end = pos + sizeof(buf);
8616
8617 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008618 if (!drv->if_indices[i].ifindex)
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008619 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008620 res = os_snprintf(pos, end - pos, " %d(%d)",
Hai Shalom81f62d82019-07-22 12:10:00 -07008621 drv->if_indices[i].ifindex,
8622 drv->if_indices[i].reason);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008623 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008624 break;
8625 pos += res;
8626 }
8627 *pos = '\0';
8628
8629 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
8630 drv->num_if_indices, buf);
8631}
8632
8633
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008634static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
8635 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008636{
8637 int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07008638 struct drv_nl80211_if_info *old;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008639
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008640 wpa_printf(MSG_DEBUG,
8641 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
8642 ifidx, ifidx_reason);
8643 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008644 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
8645 ifidx);
8646 return;
8647 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07008648 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008649 if (drv->if_indices[i].ifindex == 0) {
8650 drv->if_indices[i].ifindex = ifidx;
8651 drv->if_indices[i].reason = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008652 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008653 return;
8654 }
8655 }
8656
8657 if (drv->if_indices != drv->default_if_indices)
8658 old = drv->if_indices;
8659 else
8660 old = NULL;
8661
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008662 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
Hai Shalom81f62d82019-07-22 12:10:00 -07008663 sizeof(*old));
Jouni Malinen75ecf522011-06-27 15:19:46 -07008664 if (!drv->if_indices) {
8665 if (!old)
8666 drv->if_indices = drv->default_if_indices;
8667 else
8668 drv->if_indices = old;
8669 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8670 "interfaces");
8671 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
8672 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008673 }
8674 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008675 os_memcpy(drv->if_indices, drv->default_if_indices,
8676 sizeof(drv->default_if_indices));
Hai Shalom81f62d82019-07-22 12:10:00 -07008677 drv->if_indices[drv->num_if_indices].ifindex = ifidx;
8678 drv->if_indices[drv->num_if_indices].reason = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008679 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008680 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008681}
8682
8683
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008684static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
8685 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008686{
8687 int i;
8688
8689 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008690 if ((drv->if_indices[i].ifindex == ifidx ||
8691 ifidx == IFIDX_ANY) &&
8692 (drv->if_indices[i].reason == ifidx_reason ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008693 ifidx_reason == IFIDX_ANY)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008694 drv->if_indices[i].ifindex = 0;
8695 drv->if_indices[i].reason = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008696 break;
8697 }
8698 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008699 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008700}
8701
8702
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008703static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
8704 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008705{
8706 int i;
8707
8708 for (i = 0; i < drv->num_if_indices; i++)
Hai Shalom81f62d82019-07-22 12:10:00 -07008709 if (drv->if_indices[i].ifindex == ifidx &&
8710 (drv->if_indices[i].reason == ifidx_reason ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008711 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07008712 return 1;
8713
8714 return 0;
8715}
8716
8717
8718static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008719 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008720{
8721 struct i802_bss *bss = priv;
8722 struct wpa_driver_nl80211_data *drv = bss->drv;
8723 char name[IFNAMSIZ + 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07008724 union wpa_event_data event;
Sunil Ravic0f5d412024-09-11 22:12:49 +00008725 bool add_br = false;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08008726 int ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008727
Hai Shalom39ba6fc2019-01-22 12:40:38 -08008728 ret = os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
8729 if (ret >= (int) sizeof(name))
8730 wpa_printf(MSG_WARNING,
8731 "nl80211: WDS interface name was truncated");
8732 else if (ret < 0)
8733 return ret;
8734
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008735 if (ifname_wds)
8736 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
8737
Jouni Malinen75ecf522011-06-27 15:19:46 -07008738 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
8739 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
8740 if (val) {
8741 if (!if_nametoindex(name)) {
8742 if (nl80211_create_iface(drv, name,
8743 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008744 bss->addr, 1, NULL, NULL, 0) <
8745 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008746 return -1;
Sunil Ravic0f5d412024-09-11 22:12:49 +00008747
8748 if (bridge_ifname)
8749 add_br = true;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008750
8751 os_memset(&event, 0, sizeof(event));
8752 event.wds_sta_interface.sta_addr = addr;
8753 event.wds_sta_interface.ifname = name;
8754 event.wds_sta_interface.istatus = INTERFACE_ADDED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07008755 wpa_supplicant_event(bss->ctx,
Roshan Pius3a1667e2018-07-03 15:17:14 -07008756 EVENT_WDS_STA_INTERFACE_STATUS,
8757 &event);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008758 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008759 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
8760 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
8761 "interface %s up", name);
8762 }
Sunil Ravic0f5d412024-09-11 22:12:49 +00008763
8764 if (add_br &&
8765 linux_br_add_if(drv->global->ioctl_sock,
8766 bridge_ifname, name) < 0)
8767 return -1;
8768
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008769 return i802_set_sta_vlan(priv, addr, name, 0,
8770 NL80211_DRV_LINK_ID_NA);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008771 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08008772 if (bridge_ifname &&
8773 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
8774 name) < 0)
8775 wpa_printf(MSG_INFO,
8776 "nl80211: Failed to remove interface %s from bridge %s: %s",
8777 name, bridge_ifname, strerror(errno));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008778
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008779 i802_set_sta_vlan(priv, addr, bss->ifname, 0,
8780 NL80211_DRV_LINK_ID_NA);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008781 nl80211_remove_iface(drv, if_nametoindex(name));
Roshan Pius3a1667e2018-07-03 15:17:14 -07008782 os_memset(&event, 0, sizeof(event));
8783 event.wds_sta_interface.sta_addr = addr;
8784 event.wds_sta_interface.ifname = name;
8785 event.wds_sta_interface.istatus = INTERFACE_REMOVED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07008786 wpa_supplicant_event(bss->ctx, EVENT_WDS_STA_INTERFACE_STATUS,
Roshan Pius3a1667e2018-07-03 15:17:14 -07008787 &event);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008788 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008789 }
8790}
8791
8792
8793static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
8794{
8795 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8796 struct sockaddr_ll lladdr;
8797 unsigned char buf[3000];
8798 int len;
8799 socklen_t fromlen = sizeof(lladdr);
8800
8801 len = recvfrom(sock, buf, sizeof(buf), 0,
8802 (struct sockaddr *)&lladdr, &fromlen);
8803 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008804 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
8805 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07008806 return;
8807 }
8808
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008809 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07008810 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
8811}
8812
8813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008814static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
8815 struct i802_bss *bss,
8816 const char *brname, const char *ifname)
8817{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008818 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008819 char in_br[IFNAMSIZ];
8820
8821 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008822 br_ifindex = if_nametoindex(brname);
8823 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008824 /*
8825 * Bridge was configured, but the bridge device does
8826 * not exist. Try to add it now.
8827 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008828 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008829 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8830 "bridge interface %s: %s",
8831 brname, strerror(errno));
8832 return -1;
8833 }
8834 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008835 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008836 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008837 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008838 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008839
8840 if (linux_br_get(in_br, ifname) == 0) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07008841 if (os_strcmp(in_br, brname) == 0) {
8842 bss->already_in_bridge = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008843 return 0; /* already in the bridge */
Hai Shalomc9e41a12018-07-31 14:41:42 -07008844 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008845
8846 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8847 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008848 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8849 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008850 wpa_printf(MSG_ERROR, "nl80211: Failed to "
8851 "remove interface %s from bridge "
8852 "%s: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008853 ifname, in_br, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008854 return -1;
8855 }
8856 }
8857
8858 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8859 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008860 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008861 wpa_printf(MSG_WARNING,
8862 "nl80211: Failed to add interface %s into bridge %s: %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008863 ifname, brname, strerror(errno));
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008864 /* Try to continue without the interface being in a bridge. This
8865 * may be needed for some cases, e.g., with Open vSwitch, where
8866 * an external component will need to handle bridge
8867 * configuration. */
8868 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008869 }
8870 bss->added_if_into_bridge = 1;
8871
8872 return 0;
8873}
8874
8875
8876static void *i802_init(struct hostapd_data *hapd,
8877 struct wpa_init_params *params)
8878{
8879 struct wpa_driver_nl80211_data *drv;
8880 struct i802_bss *bss;
8881 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008882 char master_ifname[IFNAMSIZ];
8883 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008884 int br_added = 0;
8885
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008886 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
8887 params->global_priv, 1,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008888 params->bssid, params->driver_params,
8889 WPA_P2P_MODE_WFD_R1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008890 if (bss == NULL)
8891 return NULL;
8892
8893 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008894
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008895 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008896 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008897 params->ifname, master_ifname);
8898 br_ifindex = if_nametoindex(master_ifname);
8899 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
8900 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
8901 linux_master_get(master_ifname, params->ifname) == 0) {
8902 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
8903 params->ifname, master_ifname);
8904 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008905 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08008906
8907 /* check if master itself is under bridge */
8908 if (linux_br_get(master_ifname, master_ifname) == 0) {
8909 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
8910 master_ifname);
8911 br_ifindex = if_nametoindex(master_ifname);
8912 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
8913 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008914 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008915 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008916 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008917
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008918 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008920 for (i = 0; i < params->num_bridge; i++) {
8921 if (params->bridge[i]) {
8922 ifindex = if_nametoindex(params->bridge[i]);
8923 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008924 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008925 if (ifindex == br_ifindex)
8926 br_added = 1;
8927 }
8928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008929
8930 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008931 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008932
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008933 if (params->num_bridge && params->bridge[0]) {
8934 if (i802_check_bridge(drv, bss, params->bridge[0],
8935 params->ifname) < 0)
8936 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008937 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008938 br_added = 1;
8939 }
8940
8941 if (!br_added && br_ifindex &&
8942 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008943 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008944
Hai Shalomc9e41a12018-07-31 14:41:42 -07008945 if (bss->added_if_into_bridge || bss->already_in_bridge) {
Hai Shalomfdcde762020-04-02 11:19:20 -07008946 int err;
8947
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008948 drv->rtnl_sk = nl_socket_alloc();
8949 if (drv->rtnl_sk == NULL) {
8950 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
8951 goto failed;
8952 }
8953
Hai Shalomfdcde762020-04-02 11:19:20 -07008954 err = nl_connect(drv->rtnl_sk, NETLINK_ROUTE);
8955 if (err) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008956 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -07008957 nl_geterror(err));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008958 goto failed;
8959 }
8960 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008961
Hai Shalomb755a2a2020-04-23 21:49:02 -07008962 if (drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX) {
8963 wpa_printf(MSG_DEBUG,
8964 "nl80211: Do not open EAPOL RX socket - using control port for RX");
8965 goto skip_eapol_sock;
8966 }
8967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008968 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
8969 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008970 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
8971 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008972 goto failed;
8973 }
8974
8975 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
8976 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008977 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008978 goto failed;
8979 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07008980skip_eapol_sock:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008981
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008982 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8983 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008984 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008985 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008986
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008987 memcpy(bss->addr, params->own_addr, ETH_ALEN);
8988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008989 return bss;
8990
8991failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008992 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008993 return NULL;
8994}
8995
8996
8997static void i802_deinit(void *priv)
8998{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008999 struct i802_bss *bss = priv;
9000 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009001}
9002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009003
9004static enum nl80211_iftype wpa_driver_nl80211_if_type(
9005 enum wpa_driver_if_type type)
9006{
9007 switch (type) {
9008 case WPA_IF_STATION:
9009 return NL80211_IFTYPE_STATION;
9010 case WPA_IF_P2P_CLIENT:
9011 case WPA_IF_P2P_GROUP:
9012 return NL80211_IFTYPE_P2P_CLIENT;
9013 case WPA_IF_AP_VLAN:
9014 return NL80211_IFTYPE_AP_VLAN;
9015 case WPA_IF_AP_BSS:
9016 return NL80211_IFTYPE_AP;
9017 case WPA_IF_P2P_GO:
9018 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009019 case WPA_IF_P2P_DEVICE:
9020 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009021 case WPA_IF_MESH:
9022 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009023 default:
9024 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009025 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009026}
9027
9028
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009029static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9030{
9031 struct wpa_driver_nl80211_data *drv;
9032 dl_list_for_each(drv, &global->interfaces,
9033 struct wpa_driver_nl80211_data, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009034 if (ether_addr_equal(addr, drv->first_bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009035 return 1;
9036 }
9037 return 0;
9038}
9039
9040
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009041static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009042{
9043 unsigned int idx;
9044
9045 if (!drv->global)
9046 return -1;
9047
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009048 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009049 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009050 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009051 new_addr[0] ^= idx << 2;
9052 if (!nl80211_addr_in_use(drv->global, new_addr))
9053 break;
9054 }
9055 if (idx == 64)
9056 return -1;
9057
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009058 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009059 MACSTR, MAC2STR(new_addr));
9060
9061 return 0;
9062}
9063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009064
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009065struct wdev_info {
9066 u64 wdev_id;
9067 int wdev_id_set;
9068 u8 macaddr[ETH_ALEN];
9069};
9070
9071static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
9072{
9073 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9074 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9075 struct wdev_info *wi = arg;
9076
9077 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9078 genlmsg_attrlen(gnlh, 0), NULL);
9079 if (tb[NL80211_ATTR_WDEV]) {
9080 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9081 wi->wdev_id_set = 1;
9082 }
9083
9084 if (tb[NL80211_ATTR_MAC])
9085 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
9086 ETH_ALEN);
9087
9088 return NL_SKIP;
9089}
9090
9091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009092static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
9093 const char *ifname, const u8 *addr,
9094 void *bss_ctx, void **drv_priv,
9095 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009096 const char *bridge, int use_existing,
9097 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009098{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009099 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009100 struct i802_bss *bss = priv;
9101 struct wpa_driver_nl80211_data *drv = bss->drv;
9102 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009103 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009104
9105 if (addr)
9106 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009107 nlmode = wpa_driver_nl80211_if_type(type);
9108 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
9109 struct wdev_info p2pdev_info;
9110
9111 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
9112 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
9113 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009114 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009115 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
9116 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
9117 ifname);
9118 return -1;
9119 }
9120
9121 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
9122 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
Mir Ali8a8f1002020-10-06 22:41:40 +05309123 if (!is_zero_ether_addr(p2pdev_info.macaddr)) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009124 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
Mir Ali8a8f1002020-10-06 22:41:40 +05309125 os_memcpy(drv->global->p2p_perm_addr, p2pdev_info.macaddr, ETH_ALEN);
9126 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009127 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
9128 ifname,
9129 (long long unsigned int) p2pdev_info.wdev_id);
9130 } else {
9131 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009132 0, NULL, NULL, use_existing);
9133 if (use_existing && ifidx == -ENFILE) {
9134 added = 0;
9135 ifidx = if_nametoindex(ifname);
9136 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009137 return -1;
9138 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009139 }
9140
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009141 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07009142 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009143 os_memcpy(if_addr, bss->addr, ETH_ALEN);
9144 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07009145 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009146 if (added)
9147 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009148 return -1;
9149 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009150 }
9151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009152 if (!addr &&
9153 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07009154 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00009155 type == WPA_IF_STATION || type == WPA_IF_AP_BSS)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07009156 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009157 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009158
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009159 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009160 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07009161 if (added)
9162 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009163 return -1;
9164 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009165 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009166 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07009167 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009168 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07009169 if (added)
9170 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009171 return -1;
9172 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009173 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009174 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07009175 if (added)
9176 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009177 return -1;
9178 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009179 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009180 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009181 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009182
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009183 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009184 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
Sunil Ravi036cec52023-03-29 11:35:17 -07009185
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009186 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009187 if (added)
9188 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009189 return -1;
9190 }
9191
9192 if (bridge &&
9193 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
9194 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
9195 "interface %s to a bridge %s",
9196 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009197 if (added)
9198 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009199 os_free(new_bss);
9200 return -1;
9201 }
9202
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009203 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
9204 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07009205 if (added)
9206 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009207 os_free(new_bss);
9208 return -1;
9209 }
9210 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009211 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009212 new_bss->ifindex = ifidx;
9213 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009214 new_bss->next = drv->first_bss->next;
Sunil Ravi036cec52023-03-29 11:35:17 -07009215 new_bss->flink = &new_bss->links[0];
Sunil Ravi99c035e2024-07-12 01:42:03 +00009216 new_bss->valid_links = 0;
Sunil Ravi036cec52023-03-29 11:35:17 -07009217 os_memcpy(new_bss->flink->addr, new_bss->addr, ETH_ALEN);
9218
9219 new_bss->flink->freq = drv->first_bss->flink->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009220 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009221 new_bss->added_if = added;
9222 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009223 if (drv_priv)
9224 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009225 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009226
9227 /* Subscribe management frames for this WPA_IF_AP_BSS */
9228 if (nl80211_setup_ap(new_bss))
9229 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009230 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009231
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009232 if (drv->global)
9233 drv->global->if_add_ifindex = ifidx;
9234
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009235 /*
9236 * Some virtual interfaces need to process EAPOL packets and events on
9237 * the parent interface. This is used mainly with hostapd.
9238 */
9239 if (ifidx > 0 &&
9240 (drv->hostapd ||
9241 nlmode == NL80211_IFTYPE_AP_VLAN ||
9242 nlmode == NL80211_IFTYPE_WDS ||
9243 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009244 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009246 return 0;
9247}
9248
9249
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009250static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009251 enum wpa_driver_if_type type,
9252 const char *ifname)
9253{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009254 struct wpa_driver_nl80211_data *drv = bss->drv;
9255 int ifindex = if_nametoindex(ifname);
9256
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009257 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
9258 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08009259 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07009260 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07009261 else if (ifindex > 0 && !bss->added_if) {
9262 struct wpa_driver_nl80211_data *drv2;
9263 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009264 struct wpa_driver_nl80211_data, list) {
9265 del_ifidx(drv2, ifindex, IFIDX_ANY);
9266 del_ifidx(drv2, IFIDX_ANY, ifindex);
9267 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07009268 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009269
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009270 if (type != WPA_IF_AP_BSS)
9271 return 0;
9272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009273 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009274 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9275 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009276 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9277 "interface %s from bridge %s: %s",
9278 bss->ifname, bss->brname, strerror(errno));
9279 }
9280 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009281 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009282 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9283 "bridge %s: %s",
9284 bss->brname, strerror(errno));
9285 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009286
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009287 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009288 struct i802_bss *tbss;
9289
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009290 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
9291 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009292 if (tbss->next == bss) {
9293 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009294 /* Unsubscribe management frames */
9295 nl80211_teardown_ap(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00009296 nl80211_remove_links(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009297 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009298 if (!bss->added_if)
9299 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009300 os_free(bss);
9301 bss = NULL;
9302 break;
9303 }
9304 }
9305 if (bss)
9306 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9307 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009308 } else {
9309 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
9310 nl80211_teardown_ap(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00009311 nl80211_remove_links(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009312 if (!bss->added_if && !drv->first_bss->next)
Sunil Ravi036cec52023-03-29 11:35:17 -07009313 wpa_driver_nl80211_del_beacon_all(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009314 nl80211_destroy_bss(bss);
9315 if (!bss->added_if)
9316 i802_set_iface_flags(bss, 0);
9317 if (drv->first_bss->next) {
9318 drv->first_bss = drv->first_bss->next;
9319 drv->ctx = drv->first_bss->ctx;
Sunil Ravi99c035e2024-07-12 01:42:03 +00009320 drv->ifindex = drv->first_bss->ifindex;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009321 os_free(bss);
9322 } else {
9323 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
9324 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009325 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009326
9327 return 0;
9328}
9329
9330
9331static int cookie_handler(struct nl_msg *msg, void *arg)
9332{
9333 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9334 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9335 u64 *cookie = arg;
9336 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9337 genlmsg_attrlen(gnlh, 0), NULL);
9338 if (tb[NL80211_ATTR_COOKIE])
9339 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9340 return NL_SKIP;
9341}
9342
9343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009344static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009345 unsigned int freq, unsigned int wait,
9346 const u8 *buf, size_t buf_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07009347 int save_cookie, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009348 int offchanok, const u16 *csa_offs,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009349 size_t csa_offs_len, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009350{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009351 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009352 struct nl_msg *msg;
9353 u64 cookie;
9354 int ret = -1;
9355
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009356 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07009357 "no_ack=%d offchanok=%d",
9358 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009359 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009360
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009361 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009362 ((link_id != NL80211_DRV_LINK_ID_NA) &&
9363 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009364 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9365 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
9366 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9367 drv->test_use_roc_tx) &&
9368 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
9369 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
9370 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009371 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
9372 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009373 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
9374 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009375
9376 cookie = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009377 ret = send_and_recv_resp(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009378 msg = NULL;
9379 if (ret) {
9380 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009381 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9382 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009383 } else {
9384 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
9385 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9386 (long long unsigned int) cookie);
9387
Hai Shalomfdcde762020-04-02 11:19:20 -07009388 if (save_cookie)
9389 drv->send_frame_cookie = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009390
Sunil Ravia04bd252022-05-02 22:54:18 -07009391 if (!wait) {
9392 /* There is no need to store this cookie since there
9393 * is no wait that could be canceled later. */
9394 goto fail;
9395 }
Hai Shalomfdcde762020-04-02 11:19:20 -07009396 if (drv->num_send_frame_cookies == MAX_SEND_FRAME_COOKIES) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009397 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -07009398 "nl80211: Drop oldest pending send frame cookie 0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009399 (long long unsigned int)
Hai Shalomfdcde762020-04-02 11:19:20 -07009400 drv->send_frame_cookies[0]);
9401 os_memmove(&drv->send_frame_cookies[0],
9402 &drv->send_frame_cookies[1],
9403 (MAX_SEND_FRAME_COOKIES - 1) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009404 sizeof(u64));
Hai Shalomfdcde762020-04-02 11:19:20 -07009405 drv->num_send_frame_cookies--;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009406 }
Hai Shalomfdcde762020-04-02 11:19:20 -07009407 drv->send_frame_cookies[drv->num_send_frame_cookies] = cookie;
9408 drv->num_send_frame_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009410
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009411fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009412 nlmsg_free(msg);
9413 return ret;
9414}
9415
9416
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009417static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
9418 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009419 unsigned int wait_time,
9420 const u8 *dst, const u8 *src,
9421 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009422 const u8 *data, size_t data_len,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00009423 int no_cck, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009424{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009425 struct wpa_driver_nl80211_data *drv = bss->drv;
9426 int ret = -1;
9427 u8 *buf;
9428 struct ieee80211_hdr *hdr;
Hai Shalomfdcde762020-04-02 11:19:20 -07009429 int offchanok = 1;
9430
Sunil Ravi036cec52023-03-29 11:35:17 -07009431 if (is_ap_interface(drv->nlmode) && (int) freq == bss->flink->freq &&
9432 bss->flink->beacon_set)
Hai Shalomfdcde762020-04-02 11:19:20 -07009433 offchanok = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009434
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009435 if (!freq && is_sta_interface(drv->nlmode))
9436 offchanok = 0;
9437
9438 wpa_printf(MSG_DEBUG,
9439 "nl80211: Send Action frame (ifindex=%d, freq=%u MHz wait=%d ms no_cck=%d offchanok=%d dst="
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00009440 MACSTR " src=" MACSTR " bssid=" MACSTR ", link_id=%d)",
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009441 drv->ifindex, freq, wait_time, no_cck, offchanok,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00009442 MAC2STR(dst), MAC2STR(src), MAC2STR(bssid), link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009443
9444 buf = os_zalloc(24 + data_len);
9445 if (buf == NULL)
9446 return ret;
9447 os_memcpy(buf + 24, data, data_len);
9448 hdr = (struct ieee80211_hdr *) buf;
9449 hdr->frame_control =
9450 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
9451 os_memcpy(hdr->addr1, dst, ETH_ALEN);
9452 os_memcpy(hdr->addr2, src, ETH_ALEN);
9453 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
9454
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009455 if (!ether_addr_equal(bss->addr, src)) {
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08009456 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
9457 MAC2STR(src));
9458 os_memcpy(bss->rand_addr, src, ETH_ALEN);
9459 } else {
9460 os_memset(bss->rand_addr, 0, ETH_ALEN);
9461 }
9462
Hai Shalom60840252021-02-19 19:02:11 -08009463#ifdef CONFIG_MESH
9464 if (is_mesh_interface(drv->nlmode)) {
9465 struct hostapd_hw_modes *modes;
9466 u16 num_modes, flags;
9467 u8 dfs_domain;
9468 int i;
9469
9470 modes = nl80211_get_hw_feature_data(bss, &num_modes,
9471 &flags, &dfs_domain);
9472 if (dfs_domain != HOSTAPD_DFS_REGION_ETSI &&
Sunil Ravi036cec52023-03-29 11:35:17 -07009473 ieee80211_is_dfs(bss->flink->freq, modes, num_modes))
Hai Shalom60840252021-02-19 19:02:11 -08009474 offchanok = 0;
9475 if (modes) {
9476 for (i = 0; i < num_modes; i++) {
9477 os_free(modes[i].channels);
9478 os_free(modes[i].rates);
9479 }
9480 os_free(modes);
9481 }
9482 }
9483#endif /* CONFIG_MESH */
9484
Dmitry Shmidt56052862013-10-04 10:23:25 -07009485 if (is_ap_interface(drv->nlmode) &&
9486 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
Sunil Ravi036cec52023-03-29 11:35:17 -07009487 (int) freq == bss->flink->freq || drv->device_ap_sme ||
Dmitry Shmidt56052862013-10-04 10:23:25 -07009488 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009489 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07009490 0, freq, no_cck, offchanok,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00009491 wait_time, NULL, 0, 0,
9492 link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009493 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009494 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009495 24 + data_len, 1, no_cck, 0,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00009496 offchanok, NULL, 0, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009497
9498 os_free(buf);
9499 return ret;
9500}
9501
9502
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009503static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009504{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009505 struct wpa_driver_nl80211_data *drv = bss->drv;
9506 struct nl_msg *msg;
9507 int ret;
9508
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08009509 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009510 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009511 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009512 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009513 nlmsg_free(msg);
9514 return;
9515 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009516
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009517 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009518 if (ret)
9519 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
9520 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009521}
9522
9523
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009524static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
9525{
9526 struct i802_bss *bss = priv;
9527 struct wpa_driver_nl80211_data *drv = bss->drv;
9528 unsigned int i;
9529 u64 cookie;
9530
9531 /* Cancel the last pending TX cookie */
Sunil Ravia04bd252022-05-02 22:54:18 -07009532 if (drv->send_frame_cookie != (u64) -1)
9533 nl80211_frame_wait_cancel(bss, drv->send_frame_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009534
9535 /*
9536 * Cancel the other pending TX cookies, if any. This is needed since
9537 * the driver may keep a list of all pending offchannel TX operations
9538 * and free up the radio only once they have expired or cancelled.
9539 */
Hai Shalomfdcde762020-04-02 11:19:20 -07009540 for (i = drv->num_send_frame_cookies; i > 0; i--) {
9541 cookie = drv->send_frame_cookies[i - 1];
9542 if (cookie != drv->send_frame_cookie)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009543 nl80211_frame_wait_cancel(bss, cookie);
9544 }
Hai Shalomfdcde762020-04-02 11:19:20 -07009545 drv->num_send_frame_cookies = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009546}
9547
9548
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009549static int nl80211_put_any_link_id(struct nl_msg *msg,
9550 struct driver_sta_mlo_info *mlo,
9551 int freq)
9552{
9553 int i;
9554 int link_id = -1;
9555 int any_valid_link_id = -1;
9556
9557 if (!mlo->valid_links)
9558 return 0;
9559
9560 /* First try to pick a link that uses the same band */
Sunil Ravi99c035e2024-07-12 01:42:03 +00009561 for_each_link(mlo->valid_links, i) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009562 if (any_valid_link_id == -1)
9563 any_valid_link_id = i;
9564
9565 if (is_same_band(freq, mlo->links[i].freq)) {
9566 link_id = i;
9567 break;
9568 }
9569 }
9570
9571 /* Use any valid link ID if no band match was found */
9572 if (link_id == -1)
9573 link_id = any_valid_link_id;
9574
9575 if (link_id == -1) {
9576 wpa_printf(MSG_INFO,
9577 "nl80211: No valid Link ID found for freq %u", freq);
9578 return 0;
9579 }
9580
9581 wpa_printf(MSG_DEBUG, "nl80211: Add Link ID %d", link_id);
9582 return nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id);
9583}
9584
9585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009586static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
9587 unsigned int duration)
9588{
9589 struct i802_bss *bss = priv;
9590 struct wpa_driver_nl80211_data *drv = bss->drv;
9591 struct nl_msg *msg;
9592 int ret;
9593 u64 cookie;
9594
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009595 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
9596 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009597 nla_put_u32(msg, NL80211_ATTR_DURATION, duration) ||
9598 nl80211_put_any_link_id(msg, &drv->sta_mlo_info, freq)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009599 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009600 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009601 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009602
9603 cookie = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009604 ret = send_and_recv_resp(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009605 if (ret == 0) {
9606 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
9607 "0x%llx for freq=%u MHz duration=%u",
9608 (long long unsigned int) cookie, freq, duration);
9609 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009610 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009611 return 0;
9612 }
9613 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
9614 "(freq=%d duration=%u): %d (%s)",
9615 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009616 return -1;
9617}
9618
9619
9620static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
9621{
9622 struct i802_bss *bss = priv;
9623 struct wpa_driver_nl80211_data *drv = bss->drv;
9624 struct nl_msg *msg;
9625 int ret;
9626
9627 if (!drv->pending_remain_on_chan) {
9628 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
9629 "to cancel");
9630 return -1;
9631 }
9632
9633 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
9634 "0x%llx",
9635 (long long unsigned int) drv->remain_on_chan_cookie);
9636
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009637 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
9638 if (!msg ||
9639 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
9640 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009641 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009642 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009643
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009644 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009645 if (ret == 0)
9646 return 0;
9647 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
9648 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009649 return -1;
9650}
9651
9652
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009653static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009654{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009655 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009657 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009658 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07009659 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
9660 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009661 /*
9662 * Do not disable Probe Request reporting that was
9663 * enabled in nl80211_setup_ap().
9664 */
9665 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
9666 "Probe Request reporting nl_preq=%p while "
9667 "in AP mode", bss->nl_preq);
9668 } else if (bss->nl_preq) {
9669 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
9670 "reporting nl_preq=%p", bss->nl_preq);
Roshan Pius3a1667e2018-07-03 15:17:14 -07009671 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009672 }
9673 return 0;
9674 }
9675
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009676 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009677 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009678 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009679 return 0;
9680 }
9681
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009682 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
9683 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009684 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009685 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
9686 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009687
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009688 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009689 (WLAN_FC_TYPE_MGMT << 2) |
9690 (WLAN_FC_STYPE_PROBE_REQ << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07009691 NULL, 0, false) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009692 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07009693
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009694 nl80211_register_eloop_read(&bss->nl_preq,
9695 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07009696 bss->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009697
9698 return 0;
9699
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009700 out_err:
9701 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009702 return -1;
9703}
9704
9705
9706static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
9707 int ifindex, int disabled)
9708{
9709 struct nl_msg *msg;
9710 struct nlattr *bands, *band;
9711 int ret;
9712
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009713 wpa_printf(MSG_DEBUG,
9714 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
9715 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
9716 "no NL80211_TXRATE_LEGACY constraint");
9717
9718 msg = nl80211_ifindex_msg(drv, ifindex, 0,
9719 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009720 if (!msg)
9721 return -1;
9722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009723 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
9724 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009725 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009726
9727 /*
9728 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
9729 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
9730 * rates. All 5 GHz rates are left enabled.
9731 */
9732 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009733 if (!band ||
9734 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
9735 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
9736 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009737 nla_nest_end(msg, band);
9738
9739 nla_nest_end(msg, bands);
9740
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009741 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009742 if (ret) {
9743 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
9744 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009745 } else
9746 drv->disabled_11b_rates = disabled;
9747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009748 return ret;
9749
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009750fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009751 nlmsg_free(msg);
9752 return -1;
9753}
9754
9755
Sunil Ravic0f5d412024-09-11 22:12:49 +00009756int nl80211_remove_link(struct i802_bss *bss, int link_id)
Sunil Raviaf399a82024-05-05 20:56:55 +00009757{
Sunil Ravi88611412024-06-28 17:34:56 +00009758 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +00009759 struct i802_link *link;
Sunil Ravi88611412024-06-28 17:34:56 +00009760 struct nl_msg *msg;
Sunil Ravi99c035e2024-07-12 01:42:03 +00009761 size_t i;
9762 int ret;
Sunil Ravic0f5d412024-09-11 22:12:49 +00009763 u8 link_addr[ETH_ALEN];
Sunil Ravi99c035e2024-07-12 01:42:03 +00009764
9765 wpa_printf(MSG_DEBUG, "nl80211: Remove link (ifindex=%d link_id=%u)",
9766 bss->ifindex, link_id);
9767
9768 if (!(bss->valid_links & BIT(link_id))) {
9769 wpa_printf(MSG_DEBUG,
9770 "nl80211: MLD: remove link: Link not found");
9771 return -1;
9772 }
9773
9774 link = &bss->links[link_id];
9775
9776 wpa_driver_nl80211_del_beacon(bss, link_id);
9777
Sunil Ravic0f5d412024-09-11 22:12:49 +00009778 os_memcpy(link_addr, link->addr, ETH_ALEN);
Sunil Ravi99c035e2024-07-12 01:42:03 +00009779 /* First remove the link locally */
9780 bss->valid_links &= ~BIT(link_id);
9781 os_memset(link->addr, 0, ETH_ALEN);
9782
9783 /* Choose new deflink if we are removing that link */
9784 if (bss->flink == link) {
9785 for_each_link_default(bss->valid_links, i, 0) {
9786 bss->flink = &bss->links[i];
9787 break;
9788 }
9789 }
9790
9791 /* If this was the last link, reset default link */
9792 if (!bss->valid_links) {
9793 /* TODO: Does keeping freq/bandwidth make sense? */
9794 if (bss->flink != link)
9795 os_memcpy(bss->flink, link, sizeof(*link));
9796
9797 os_memcpy(bss->flink->addr, bss->addr, ETH_ALEN);
9798 }
9799
9800 /* Remove the link from the kernel */
9801 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_REMOVE_LINK);
9802 if (!msg ||
9803 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) {
9804 nlmsg_free(msg);
9805 wpa_printf(MSG_ERROR,
9806 "nl80211: remove link (%d) failed", link_id);
9807 return -1;
9808 }
9809
9810 ret = send_and_recv_cmd(drv, msg);
9811 if (ret)
9812 wpa_printf(MSG_ERROR,
9813 "nl80211: remove link (%d) failed. ret=%d (%s)",
9814 link_id, ret, strerror(-ret));
9815
Sunil Ravic0f5d412024-09-11 22:12:49 +00009816 if (drv->rtnl_sk)
9817 rtnl_neigh_delete_fdb_entry(bss, link_addr, true);
9818
Sunil Ravi99c035e2024-07-12 01:42:03 +00009819 return ret;
9820}
9821
9822
9823static void nl80211_remove_links(struct i802_bss *bss)
9824{
Sunil Ravi036cec52023-03-29 11:35:17 -07009825 int ret;
9826 u8 link_id;
9827
Sunil Ravi99c035e2024-07-12 01:42:03 +00009828 for_each_link(bss->valid_links, link_id) {
9829 ret = nl80211_remove_link(bss, link_id);
9830 if (ret)
9831 break;
Sunil Ravi88611412024-06-28 17:34:56 +00009832 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00009833
9834 if (bss->flink)
9835 os_memcpy(bss->flink->addr, bss->addr, ETH_ALEN);
Sunil Ravi036cec52023-03-29 11:35:17 -07009836}
9837
9838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009839static int wpa_driver_nl80211_deinit_ap(void *priv)
9840{
9841 struct i802_bss *bss = priv;
9842 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi036cec52023-03-29 11:35:17 -07009843
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009844 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009845 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07009846
9847 /* Stop beaconing */
Sunil Ravi99c035e2024-07-12 01:42:03 +00009848 wpa_driver_nl80211_del_beacon(bss, NL80211_DRV_LINK_ID_NA);
Sunil Ravi036cec52023-03-29 11:35:17 -07009849
9850 nl80211_remove_links(bss);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009851
9852 /*
9853 * If the P2P GO interface was dynamically added, then it is
9854 * possible that the interface change to station is not possible.
9855 */
9856 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
9857 return 0;
9858
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009859 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009860}
9861
9862
Sunil Ravi99c035e2024-07-12 01:42:03 +00009863static int wpa_driver_nl80211_stop_ap(void *priv, int link_id)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009864{
9865 struct i802_bss *bss = priv;
9866 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi036cec52023-03-29 11:35:17 -07009867
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009868 if (!is_ap_interface(drv->nlmode))
9869 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07009870
Sunil Ravi99c035e2024-07-12 01:42:03 +00009871 if (link_id == -1) {
9872 wpa_driver_nl80211_del_beacon_all(bss);
9873 return 0;
9874 }
Sunil Ravi036cec52023-03-29 11:35:17 -07009875
Sunil Ravi99c035e2024-07-12 01:42:03 +00009876 if (nl80211_link_valid(bss->valid_links, link_id)) {
9877 wpa_driver_nl80211_del_beacon(bss, link_id);
9878 return 0;
9879 }
9880
9881 return -1;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009882}
9883
9884
Dmitry Shmidt04949592012-07-19 12:16:46 -07009885static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
9886{
9887 struct i802_bss *bss = priv;
9888 struct wpa_driver_nl80211_data *drv = bss->drv;
9889 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
9890 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009891
9892 /*
9893 * If the P2P Client interface was dynamically added, then it is
9894 * possible that the interface change to station is not possible.
9895 */
9896 if (bss->if_dynamic)
9897 return 0;
9898
Dmitry Shmidt04949592012-07-19 12:16:46 -07009899 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9900}
9901
9902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009903static void wpa_driver_nl80211_resume(void *priv)
9904{
9905 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009906 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009907
9908 if (i802_set_iface_flags(bss, 1))
9909 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009910
9911 if (is_p2p_net_interface(nlmode))
9912 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009913}
9914
9915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009916static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9917{
9918 struct i802_bss *bss = priv;
9919 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009920 struct nl_msg *msg;
9921 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009922
9923 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9924 "hysteresis=%d", threshold, hysteresis);
9925
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009926 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
9927 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
9928 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
9929 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
9930 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009931 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009932 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009933 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009934
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009935 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009936}
9937
9938
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009939static int get_channel_width(struct nl_msg *msg, void *arg)
9940{
9941 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9942 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9943 struct wpa_signal_info *sig_change = arg;
9944
9945 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9946 genlmsg_attrlen(gnlh, 0), NULL);
9947
9948 sig_change->center_frq1 = -1;
9949 sig_change->center_frq2 = -1;
9950 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
9951
9952 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
9953 sig_change->chanwidth = convert2width(
9954 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
9955 if (tb[NL80211_ATTR_CENTER_FREQ1])
9956 sig_change->center_frq1 =
9957 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
9958 if (tb[NL80211_ATTR_CENTER_FREQ2])
9959 sig_change->center_frq2 =
9960 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
9961 }
9962
9963 return NL_SKIP;
9964}
9965
9966
9967static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
9968 struct wpa_signal_info *sig)
9969{
9970 struct nl_msg *msg;
9971
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009972 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009973 return send_and_recv_resp(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009974}
9975
9976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009977static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
9978{
9979 struct i802_bss *bss = priv;
9980 struct wpa_driver_nl80211_data *drv = bss->drv;
9981 int res;
9982
9983 os_memset(si, 0, sizeof(*si));
Sunil Ravi77d572f2023-01-17 23:58:31 +00009984 res = nl80211_get_link_signal(drv, drv->bssid, &si->data);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009985 if (res) {
9986 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
9987 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
9988 return res;
Sunil Ravi77d572f2023-01-17 23:58:31 +00009989 si->data.signal = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009990 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009991
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009992 res = nl80211_get_channel_width(drv, si);
9993 if (res != 0)
9994 return res;
9995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009996 return nl80211_get_link_noise(drv, si);
9997}
9998
9999
Sunil Ravi89eba102022-09-13 21:04:37 -070010000static int get_links_noise(struct nl_msg *msg, void *arg)
10001{
10002 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10003 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10004 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10005 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10006 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10007 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10008 };
10009 struct wpa_mlo_signal_info *mlo_sig = arg;
10010 int i;
10011
10012 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10013 genlmsg_attrlen(gnlh, 0), NULL);
10014
10015 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
10016 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
10017 return NL_SKIP;
10018 }
10019
10020 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10021 tb[NL80211_ATTR_SURVEY_INFO],
10022 survey_policy)) {
10023 wpa_printf(MSG_DEBUG,
10024 "nl80211: Failed to parse nested attributes");
10025 return NL_SKIP;
10026 }
10027
10028 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
10029 return NL_SKIP;
10030
10031 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
10032 return NL_SKIP;
10033
Sunil Ravi99c035e2024-07-12 01:42:03 +000010034 for_each_link(mlo_sig->valid_links, i) {
Sunil Ravi89eba102022-09-13 21:04:37 -070010035 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
10036 mlo_sig->links[i].frequency)
10037 continue;
10038
10039 mlo_sig->links[i].current_noise =
10040 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10041 break;
10042 }
10043
10044 return NL_SKIP;
10045}
10046
10047
10048static int nl80211_get_links_noise(struct wpa_driver_nl80211_data *drv,
10049 struct wpa_mlo_signal_info *mlo_sig)
10050{
10051 struct nl_msg *msg;
10052
10053 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010054 return send_and_recv_resp(drv, msg, get_links_noise, mlo_sig);
Sunil Ravi89eba102022-09-13 21:04:37 -070010055}
10056
10057
10058static int get_links_channel_width(struct nl_msg *msg, void *arg)
10059{
10060 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10061 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10062 struct wpa_mlo_signal_info *mlo_sig = arg;
10063 struct nlattr *link;
10064 int rem_links;
10065
10066 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10067 genlmsg_attrlen(gnlh, 0), NULL);
10068
10069 if (!tb[NL80211_ATTR_MLO_LINKS])
10070 return NL_SKIP;
10071
10072 nla_for_each_nested(link, tb[NL80211_ATTR_MLO_LINKS], rem_links) {
10073 struct nlattr *tb2[NL80211_ATTR_MAX + 1];
10074 int link_id;
10075
10076 nla_parse(tb2, NL80211_ATTR_MAX, nla_data(link), nla_len(link),
10077 NULL);
10078
10079 if (!tb2[NL80211_ATTR_MLO_LINK_ID])
10080 continue;
10081
10082 link_id = nla_get_u8(tb2[NL80211_ATTR_MLO_LINK_ID]);
10083 if (link_id >= MAX_NUM_MLD_LINKS)
10084 continue;
10085
10086 if (!tb2[NL80211_ATTR_CHANNEL_WIDTH])
10087 continue;
10088 mlo_sig->links[link_id].chanwidth = convert2width(
10089 nla_get_u32(tb2[NL80211_ATTR_CHANNEL_WIDTH]));
10090 if (tb2[NL80211_ATTR_CENTER_FREQ1])
10091 mlo_sig->links[link_id].center_frq1 =
10092 nla_get_u32(tb2[NL80211_ATTR_CENTER_FREQ1]);
10093 if (tb2[NL80211_ATTR_CENTER_FREQ2])
10094 mlo_sig->links[link_id].center_frq2 =
10095 nla_get_u32(tb2[NL80211_ATTR_CENTER_FREQ2]);
10096 }
10097
10098 return NL_SKIP;
10099}
10100
10101
10102static int nl80211_get_links_channel_width(struct wpa_driver_nl80211_data *drv,
10103 struct wpa_mlo_signal_info *mlo_sig)
10104{
10105 struct nl_msg *msg;
10106
10107 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010108 return send_and_recv_resp(drv, msg, get_links_channel_width, mlo_sig);
Sunil Ravi89eba102022-09-13 21:04:37 -070010109}
10110
10111
10112static int nl80211_mlo_signal_poll(void *priv,
10113 struct wpa_mlo_signal_info *mlo_si)
10114{
10115 struct i802_bss *bss = priv;
10116 struct wpa_driver_nl80211_data *drv = bss->drv;
10117 int res;
10118 int i;
10119
10120 if (drv->nlmode != NL80211_IFTYPE_STATION ||
10121 !drv->sta_mlo_info.valid_links)
10122 return -1;
10123
10124 os_memset(mlo_si, 0, sizeof(*mlo_si));
10125 mlo_si->valid_links = drv->sta_mlo_info.valid_links;
10126
Sunil Ravi99c035e2024-07-12 01:42:03 +000010127 for_each_link(mlo_si->valid_links, i) {
Sunil Ravi89eba102022-09-13 21:04:37 -070010128 res = nl80211_get_link_signal(drv,
10129 drv->sta_mlo_info.links[i].bssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +000010130 &mlo_si->links[i].data);
Sunil Ravi89eba102022-09-13 21:04:37 -070010131 if (res != 0)
10132 return res;
10133
10134 mlo_si->links[i].center_frq1 = -1;
10135 mlo_si->links[i].center_frq2 = -1;
10136 mlo_si->links[i].chanwidth = CHAN_WIDTH_UNKNOWN;
10137 mlo_si->links[i].current_noise = WPA_INVALID_NOISE;
10138 mlo_si->links[i].frequency = drv->sta_mlo_info.links[i].freq;
10139 }
10140
10141 res = nl80211_get_links_channel_width(drv, mlo_si);
10142 if (res != 0)
10143 return res;
10144
10145 return nl80211_get_links_noise(drv, mlo_si);
10146}
10147
10148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010149static int nl80211_set_param(void *priv, const char *param)
10150{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -070010151 struct i802_bss *bss = priv;
10152 struct wpa_driver_nl80211_data *drv = bss->drv;
10153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010154 if (param == NULL)
10155 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -080010156 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010157
10158#ifdef CONFIG_P2P
10159 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010160 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10161 "interface");
10162 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10163 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10164 }
10165#endif /* CONFIG_P2P */
10166
Dmitry Shmidtaca489e2016-09-28 15:44:14 -070010167 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010168 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010169
10170 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010171 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070010172 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010173 }
10174
Dmitry Shmidtaca489e2016-09-28 15:44:14 -070010175 if (os_strstr(param, "force_bss_selection=1"))
10176 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
10177
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010178 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010179 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
10180 drv->test_use_roc_tx = 1;
10181 }
10182
Hai Shalomb755a2a2020-04-23 21:49:02 -070010183 if (os_strstr(param, "control_port=0")) {
Hai Shalomfdcde762020-04-02 11:19:20 -070010184 drv->capa.flags &= ~WPA_DRIVER_FLAGS_CONTROL_PORT;
Hai Shalom899fcc72020-10-19 14:38:18 -070010185 drv->capa.flags2 &= ~(WPA_DRIVER_FLAGS2_CONTROL_PORT_RX |
10186 WPA_DRIVER_FLAGS2_CONTROL_PORT_TX_STATUS);
10187 drv->control_port_ap = 0;
Hai Shalomb755a2a2020-04-23 21:49:02 -070010188 }
10189
10190 if (os_strstr(param, "control_port_ap=1"))
10191 drv->control_port_ap = 1;
Hai Shalomfdcde762020-04-02 11:19:20 -070010192
Hai Shalom899fcc72020-10-19 14:38:18 -070010193 if (os_strstr(param, "control_port_ap=0")) {
10194 drv->capa.flags2 &= ~WPA_DRIVER_FLAGS2_CONTROL_PORT_TX_STATUS;
10195 drv->control_port_ap = 0;
10196 }
10197
Hai Shalomfdcde762020-04-02 11:19:20 -070010198 if (os_strstr(param, "full_ap_client_state=0"))
10199 drv->capa.flags &= ~WPA_DRIVER_FLAGS_FULL_AP_CLIENT_STATE;
10200
Hai Shalom899fcc72020-10-19 14:38:18 -070010201 if (os_strstr(param, "no_rrm=1")) {
10202 drv->no_rrm = 1;
10203
10204 if (!bss->in_deinit && !is_ap_interface(drv->nlmode) &&
10205 !is_mesh_interface(drv->nlmode)) {
10206 nl80211_mgmt_unsubscribe(bss, "no_rrm=1");
10207 if (nl80211_mgmt_subscribe_non_ap(bss) < 0)
10208 wpa_printf(MSG_DEBUG,
10209 "nl80211: Failed to re-register Action frame processing - ignore for now");
10210 }
10211 }
10212
Sunil Ravi640215c2023-06-28 23:08:09 +000010213 if (os_strstr(param, "secure_ltf=1")) {
10214 drv->capa.flags2 |= WPA_DRIVER_FLAGS2_SEC_LTF_STA |
10215 WPA_DRIVER_FLAGS2_SEC_LTF_AP;
10216 }
10217
Sunil Ravi7f769292024-07-23 22:21:32 +000010218 if (os_strstr(param, "rsn_override_in_driver=1"))
10219 drv->capa.flags2 |= WPA_DRIVER_FLAGS2_RSN_OVERRIDE_STA;
10220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010221 return 0;
10222}
10223
10224
Dmitry Shmidte4663042016-04-04 10:07:49 -070010225static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010226{
10227 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010228 struct netlink_config *cfg;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010229 struct utsname name;
10230
10231 if (uname(&name) == 0) {
10232 wpa_printf(MSG_DEBUG, "nl80211: Kernel version: %s %s (%s; %s)",
10233 name.sysname, name.release,
10234 name.version, name.machine);
10235 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010237 global = os_zalloc(sizeof(*global));
10238 if (global == NULL)
10239 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -070010240 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010241 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010242 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010243 global->if_add_ifindex = -1;
10244
10245 cfg = os_zalloc(sizeof(*cfg));
10246 if (cfg == NULL)
10247 goto err;
10248
10249 cfg->ctx = global;
10250 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10251 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10252 global->netlink = netlink_init(cfg);
10253 if (global->netlink == NULL) {
10254 os_free(cfg);
10255 goto err;
10256 }
10257
10258 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10259 goto err;
10260
10261 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10262 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010263 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10264 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010265 goto err;
10266 }
10267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010268 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010269
10270err:
10271 nl80211_global_deinit(global);
10272 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010273}
10274
10275
10276static void nl80211_global_deinit(void *priv)
10277{
10278 struct nl80211_global *global = priv;
10279 if (global == NULL)
10280 return;
10281 if (!dl_list_empty(&global->interfaces)) {
10282 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10283 "nl80211_global_deinit",
10284 dl_list_len(&global->interfaces));
10285 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010286
10287 if (global->netlink)
10288 netlink_deinit(global->netlink);
10289
10290 nl_destroy_handles(&global->nl);
10291
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010292 if (global->nl_event)
Roshan Pius3a1667e2018-07-03 15:17:14 -070010293 nl80211_destroy_eloop_handle(&global->nl_event, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010294
10295 nl_cb_put(global->nl_cb);
10296
10297 if (global->ioctl_sock >= 0)
10298 close(global->ioctl_sock);
10299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010300 os_free(global);
10301}
10302
10303
10304static const char * nl80211_get_radio_name(void *priv)
10305{
10306 struct i802_bss *bss = priv;
10307 struct wpa_driver_nl80211_data *drv = bss->drv;
10308 return drv->phyname;
10309}
10310
10311
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010312static int nl80211_pmkid(struct i802_bss *bss, int cmd,
Sunil Ravi77d572f2023-01-17 23:58:31 +000010313 struct wpa_pmkid_params *params, bool skip_pmk)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010314{
10315 struct nl_msg *msg;
Sunil Ravi77d572f2023-01-17 23:58:31 +000010316
10317 if (cmd == NL80211_CMD_SET_PMKSA)
10318 wpa_printf(MSG_DEBUG,
10319 "nl80211: NL80211_CMD_SET_PMKSA with skip_pmk=%s pmk_len=%zu",
10320 skip_pmk ? "true" : "false", params->pmk_len);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010321
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010322 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010323 (params->pmkid &&
10324 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
10325 (params->bssid &&
10326 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
10327 (params->ssid_len &&
10328 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
10329 (params->fils_cache_id &&
10330 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
10331 params->fils_cache_id)) ||
Hai Shalomfdcde762020-04-02 11:19:20 -070010332 (params->pmk_lifetime &&
10333 nla_put_u32(msg, NL80211_ATTR_PMK_LIFETIME,
10334 params->pmk_lifetime)) ||
10335 (params->pmk_reauth_threshold &&
10336 nla_put_u8(msg, NL80211_ATTR_PMK_REAUTH_THRESHOLD,
10337 params->pmk_reauth_threshold)) ||
Hai Shalom021b0b52019-04-10 11:17:58 -070010338 (cmd != NL80211_CMD_DEL_PMKSA &&
Sunil Ravi77d572f2023-01-17 23:58:31 +000010339 params->pmk_len && !skip_pmk &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010340 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
Hai Shalom74f70d42019-02-11 14:42:39 -080010341 nl80211_nlmsg_clear(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010342 nlmsg_free(msg);
10343 return -ENOBUFS;
10344 }
Jouni Malinen75ecf522011-06-27 15:19:46 -070010345
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010346 return send_and_recv_cmd(bss->drv, msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010347}
10348
10349
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010350static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010351{
10352 struct i802_bss *bss = priv;
Sunil Ravi77d572f2023-01-17 23:58:31 +000010353 const size_t PMK_MAX_LEN = 64; /* current cfg80211 limit */
10354 const size_t LEGACY_PMK_MAX_LEN = 48; /* old cfg80211 limit */
10355 bool skip_pmk = params->pmk_len > PMK_MAX_LEN;
Roshan Pius3a1667e2018-07-03 15:17:14 -070010356 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010357
10358 if (params->bssid)
10359 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
10360 MAC2STR(params->bssid));
10361 else if (params->fils_cache_id && params->ssid_len) {
10362 wpa_printf(MSG_DEBUG,
10363 "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
10364 params->fils_cache_id[0], params->fils_cache_id[1],
10365 wpa_ssid_txt(params->ssid, params->ssid_len));
10366 }
10367
Sunil Ravi77d572f2023-01-17 23:58:31 +000010368 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params, skip_pmk);
10369 /*
10370 * Try again by skipping PMK if the first attempt failed with ERANGE
10371 * error, PMK was not skipped, and PMK length is greater than the
10372 * legacy kernel maximum allowed limit.
10373 */
10374 if (ret == -ERANGE && !skip_pmk &&
10375 params->pmk_len > LEGACY_PMK_MAX_LEN)
10376 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params, true);
Roshan Pius3a1667e2018-07-03 15:17:14 -070010377 if (ret < 0) {
10378 wpa_printf(MSG_DEBUG,
10379 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
10380 ret, strerror(-ret));
10381 }
10382
10383 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -070010384}
10385
10386
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010387static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010388{
10389 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -070010390 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010391
10392 if (params->bssid)
10393 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10394 MAC2STR(params->bssid));
10395 else if (params->fils_cache_id && params->ssid_len) {
10396 wpa_printf(MSG_DEBUG,
10397 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
10398 params->fils_cache_id[0], params->fils_cache_id[1],
10399 wpa_ssid_txt(params->ssid, params->ssid_len));
10400 }
10401
Sunil Ravi77d572f2023-01-17 23:58:31 +000010402 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params, true);
Roshan Pius3a1667e2018-07-03 15:17:14 -070010403 if (ret < 0) {
10404 wpa_printf(MSG_DEBUG,
10405 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
10406 ret, strerror(-ret));
10407 }
10408
10409 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -070010410}
10411
10412
10413static int nl80211_flush_pmkid(void *priv)
10414{
10415 struct i802_bss *bss = priv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010416 struct nl_msg *msg;
10417
Jouni Malinen75ecf522011-06-27 15:19:46 -070010418 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010419 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
10420 if (!msg)
10421 return -ENOBUFS;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010422 return send_and_recv_cmd(bss->drv, msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010423}
10424
10425
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010426static void clean_survey_results(struct survey_results *survey_results)
10427{
10428 struct freq_survey *survey, *tmp;
10429
10430 if (dl_list_empty(&survey_results->survey_list))
10431 return;
10432
10433 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10434 struct freq_survey, list) {
10435 dl_list_del(&survey->list);
10436 os_free(survey);
10437 }
10438}
10439
10440
10441static void add_survey(struct nlattr **sinfo, u32 ifidx,
10442 struct dl_list *survey_list)
10443{
10444 struct freq_survey *survey;
10445
10446 survey = os_zalloc(sizeof(struct freq_survey));
10447 if (!survey)
10448 return;
10449
10450 survey->ifidx = ifidx;
10451 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10452 survey->filled = 0;
10453
10454 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10455 survey->nf = (int8_t)
10456 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10457 survey->filled |= SURVEY_HAS_NF;
10458 }
10459
10460 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10461 survey->channel_time =
10462 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10463 survey->filled |= SURVEY_HAS_CHAN_TIME;
10464 }
10465
10466 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10467 survey->channel_time_busy =
10468 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10469 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10470 }
10471
10472 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10473 survey->channel_time_rx =
10474 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10475 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10476 }
10477
10478 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10479 survey->channel_time_tx =
10480 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10481 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10482 }
10483
10484 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)",
10485 survey->freq,
10486 survey->nf,
10487 (unsigned long int) survey->channel_time,
10488 (unsigned long int) survey->channel_time_busy,
10489 (unsigned long int) survey->channel_time_tx,
10490 (unsigned long int) survey->channel_time_rx,
10491 survey->filled);
10492
10493 dl_list_add_tail(survey_list, &survey->list);
10494}
10495
10496
10497static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10498 unsigned int freq_filter)
10499{
10500 if (!freq_filter)
10501 return 1;
10502
10503 return freq_filter == surveyed_freq;
10504}
10505
10506
10507static int survey_handler(struct nl_msg *msg, void *arg)
10508{
10509 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10510 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10511 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10512 struct survey_results *survey_results;
10513 u32 surveyed_freq = 0;
10514 u32 ifidx;
10515
10516 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10517 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10518 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10519 };
10520
10521 survey_results = (struct survey_results *) arg;
10522
10523 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10524 genlmsg_attrlen(gnlh, 0), NULL);
10525
Dmitry Shmidt97672262014-02-03 13:02:54 -080010526 if (!tb[NL80211_ATTR_IFINDEX])
10527 return NL_SKIP;
10528
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010529 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10530
10531 if (!tb[NL80211_ATTR_SURVEY_INFO])
10532 return NL_SKIP;
10533
10534 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10535 tb[NL80211_ATTR_SURVEY_INFO],
10536 survey_policy))
10537 return NL_SKIP;
10538
10539 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10540 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10541 return NL_SKIP;
10542 }
10543
10544 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10545
10546 if (!check_survey_ok(sinfo, surveyed_freq,
10547 survey_results->freq_filter))
10548 return NL_SKIP;
10549
10550 if (survey_results->freq_filter &&
10551 survey_results->freq_filter != surveyed_freq) {
10552 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10553 surveyed_freq);
10554 return NL_SKIP;
10555 }
10556
10557 add_survey(sinfo, ifidx, &survey_results->survey_list);
10558
10559 return NL_SKIP;
10560}
10561
10562
10563static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10564{
10565 struct i802_bss *bss = priv;
10566 struct wpa_driver_nl80211_data *drv = bss->drv;
10567 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010568 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010569 union wpa_event_data data;
10570 struct survey_results *survey_results;
Sunil Ravic0f5d412024-09-11 22:12:49 +000010571 void *ctx = (bss->scan_link && bss->scan_link->ctx) ?
10572 bss->scan_link->ctx : bss->ctx;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010573
10574 os_memset(&data, 0, sizeof(data));
10575 survey_results = &data.survey_results;
10576
10577 dl_list_init(&survey_results->survey_list);
10578
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010579 msg = nl80211_bss_msg(bss, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010580 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010581 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010582
10583 if (freq)
10584 data.survey_results.freq_filter = freq;
10585
10586 do {
10587 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010588 err = send_and_recv_resp(drv, msg, survey_handler,
10589 survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010590 } while (err > 0);
10591
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010592 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010593 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010594 else
Sunil Ravic0f5d412024-09-11 22:12:49 +000010595 wpa_supplicant_event(ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010596
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010597 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010598 return err;
10599}
10600
10601
Dmitry Shmidt807291d2015-01-27 13:40:23 -080010602static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
10603 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010604 const u8 *replay_ctr)
10605{
10606 struct i802_bss *bss = priv;
10607 struct wpa_driver_nl80211_data *drv = bss->drv;
10608 struct nlattr *replay_nested;
10609 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010610 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010611
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010612 if (!drv->set_rekey_offload)
10613 return;
10614
10615 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010616 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
10617 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -080010618 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010619 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010620 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
10621 replay_ctr)) {
10622 nl80211_nlmsg_clear(msg);
10623 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010624 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010625 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010626
10627 nla_nest_end(msg, replay_nested);
10628
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010629 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010630 if (ret == -EOPNOTSUPP) {
10631 wpa_printf(MSG_DEBUG,
10632 "nl80211: Driver does not support rekey offload");
10633 drv->set_rekey_offload = 0;
10634 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010635}
10636
10637
10638static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
10639 const u8 *addr, int qos)
10640{
10641 /* send data frame to poll STA and check whether
10642 * this frame is ACKed */
10643 struct {
10644 struct ieee80211_hdr hdr;
10645 u16 qos_ctl;
10646 } STRUCT_PACKED nulldata;
10647 size_t size;
10648
10649 /* Send data frame to poll STA and check whether this frame is ACKed */
10650
10651 os_memset(&nulldata, 0, sizeof(nulldata));
10652
10653 if (qos) {
10654 nulldata.hdr.frame_control =
10655 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10656 WLAN_FC_STYPE_QOS_NULL);
10657 size = sizeof(nulldata);
10658 } else {
10659 nulldata.hdr.frame_control =
10660 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10661 WLAN_FC_STYPE_NULLFUNC);
10662 size = sizeof(struct ieee80211_hdr);
10663 }
10664
10665 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
10666 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
10667 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
10668 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
10669
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010670 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010671 0, 0, NULL, 0, 0, -1) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010672 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
10673 "send poll frame");
10674}
10675
10676static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
10677 int qos)
10678{
10679 struct i802_bss *bss = priv;
10680 struct wpa_driver_nl80211_data *drv = bss->drv;
10681 struct nl_msg *msg;
Hai Shalom5f92bc92019-04-18 11:54:11 -070010682 u64 cookie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010683 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010684
10685 if (!drv->poll_command_supported) {
10686 nl80211_send_null_frame(bss, own_addr, addr, qos);
10687 return;
10688 }
10689
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010690 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
10691 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
10692 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010693 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010694 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010695
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010696 ret = send_and_recv_resp(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010697 if (ret < 0) {
10698 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
10699 MACSTR " failed: ret=%d (%s)",
10700 MAC2STR(addr), ret, strerror(-ret));
Hai Shalom5f92bc92019-04-18 11:54:11 -070010701 } else {
10702 wpa_printf(MSG_DEBUG,
10703 "nl80211: Client probe request addr=" MACSTR
10704 " cookie=%llu", MAC2STR(addr),
10705 (long long unsigned int) cookie);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010706 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010707}
10708
10709
10710static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
10711{
10712 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -070010713 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010714
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010715 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
10716 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
10717 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
10718 nlmsg_free(msg);
10719 return -ENOBUFS;
10720 }
Roshan Pius3a1667e2018-07-03 15:17:14 -070010721
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010722 ret = send_and_recv_cmd(bss->drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -070010723 if (ret < 0) {
10724 wpa_printf(MSG_DEBUG,
10725 "nl80211: Setting PS state %s failed: %d (%s)",
10726 enabled ? "enabled" : "disabled",
10727 ret, strerror(-ret));
10728 }
10729 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010730}
10731
10732
10733static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
10734 int ctwindow)
10735{
10736 struct i802_bss *bss = priv;
10737
10738 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
10739 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
10740
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010741 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010742#ifdef ANDROID_P2P
10743 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010744#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010745 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010746#endif /* ANDROID_P2P */
10747 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010748
10749 if (legacy_ps == -1)
10750 return 0;
10751 if (legacy_ps != 0 && legacy_ps != 1)
10752 return -1; /* Not yet supported */
10753
10754 return nl80211_set_power_save(bss, legacy_ps);
10755}
10756
10757
Dmitry Shmidt051af732013-10-22 13:52:46 -070010758static int nl80211_start_radar_detection(void *priv,
10759 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010760{
10761 struct i802_bss *bss = priv;
10762 struct wpa_driver_nl80211_data *drv = bss->drv;
10763 struct nl_msg *msg;
10764 int ret;
10765
Hai Shalom81f62d82019-07-22 12:10:00 -070010766 wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, he_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
10767 freq->freq, freq->ht_enabled, freq->vht_enabled, freq->he_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -070010768 freq->bandwidth, freq->center_freq1, freq->center_freq2);
10769
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010770 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
10771 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
10772 "detection");
10773 return -1;
10774 }
10775
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010776 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_RADAR_DETECT)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010777 nl80211_put_freq_params(msg, freq) < 0) {
10778 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010779 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010780 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010781
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010782 if (nl80211_link_valid(bss->valid_links, freq->link_id)) {
10783 wpa_printf(MSG_DEBUG,
10784 "nl80211: Radar detection (CAC) on link_id=%d",
10785 freq->link_id);
10786
10787 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, freq->link_id)) {
10788 nlmsg_free(msg);
10789 return -1;
10790 }
10791 }
10792
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010793 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010794 if (ret == 0) {
10795 nl80211_link_set_freq(bss, freq->link_id, freq->freq);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010796 return 0;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010797 }
10798
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010799 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
10800 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010801 return -1;
10802}
10803
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010804#ifdef CONFIG_TDLS
10805
Hai Shalomc1a21442022-02-04 13:43:00 -080010806static int nl80211_add_peer_capab(struct nl_msg *msg,
10807 enum tdls_peer_capability capa)
10808{
10809 u32 peer_capab = 0;
10810
10811 if (!capa)
10812 return 0;
10813
10814 if (capa & TDLS_PEER_HT)
10815 peer_capab |= NL80211_TDLS_PEER_HT;
10816 if (capa & TDLS_PEER_VHT)
10817 peer_capab |= NL80211_TDLS_PEER_VHT;
10818 if (capa & TDLS_PEER_WMM)
10819 peer_capab |= NL80211_TDLS_PEER_WMM;
10820 if (capa & TDLS_PEER_HE)
10821 peer_capab |= NL80211_TDLS_PEER_HE;
10822
10823 return nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
10824 peer_capab);
10825}
10826
10827
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010828static int
10829nl80211_tdls_set_discovery_resp_link(struct wpa_driver_nl80211_data *drv,
10830 int link_id)
10831{
10832#ifdef CONFIG_DRIVER_NL80211_QCA
10833 struct nl_msg *msg;
10834 struct nlattr *params;
10835
10836 wpa_printf(MSG_DEBUG, "nl80211: TDLS Discovery Response Tx link ID %u",
10837 link_id);
10838
10839 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10840 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
10841 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10842 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10843 QCA_NL80211_VENDOR_SUBCMD_TDLS_DISC_RSP_EXT) ||
10844 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10845 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_TDLS_DISC_RSP_EXT_TX_LINK,
10846 link_id)) {
10847 wpa_printf(MSG_ERROR,
10848 "%s: err in adding vendor_cmd and vendor_data",
10849 __func__);
10850 nlmsg_free(msg);
10851 return -1;
10852 }
10853 nla_nest_end(msg, params);
10854
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010855 return send_and_recv_cmd(drv, msg);
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010856#else /* CONFIG_DRIVER_NL80211_QCA */
10857 wpa_printf(MSG_ERROR,
10858 "nl80211: Setting TX link for TDLS Discovery Response not supported");
10859 return -1;
10860#endif /* CONFIG_DRIVER_NL80211_QCA */
10861}
10862
10863
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010864static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
10865 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070010866 u32 peer_capab, int initiator, const u8 *buf,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010867 size_t len, int link_id)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010868{
10869 struct i802_bss *bss = priv;
10870 struct wpa_driver_nl80211_data *drv = bss->drv;
10871 struct nl_msg *msg;
10872
10873 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10874 return -EOPNOTSUPP;
10875
10876 if (!dst)
10877 return -EINVAL;
10878
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010879 if (link_id >= 0 &&
10880 nl80211_tdls_set_discovery_resp_link(drv, link_id) < 0)
10881 return -EOPNOTSUPP;
10882
Sunil Ravi7f769292024-07-23 22:21:32 +000010883 if (link_id < 0 && drv->sta_mlo_info.valid_links)
10884 link_id = drv->sta_mlo_info.assoc_link_id;
10885
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010886 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_TDLS_MGMT)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010887 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
10888 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
10889 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
Hai Shalomc1a21442022-02-04 13:43:00 -080010890 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code) ||
Sunil Ravi7f769292024-07-23 22:21:32 +000010891 (link_id >= 0 &&
10892 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Hai Shalomc1a21442022-02-04 13:43:00 -080010893 nl80211_add_peer_capab(msg, peer_capab) ||
10894 (initiator && nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010895 nla_put(msg, NL80211_ATTR_IE, len, buf))
10896 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010897
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010898 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010899
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010900fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010901 nlmsg_free(msg);
10902 return -ENOBUFS;
10903}
10904
10905
10906static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
10907{
10908 struct i802_bss *bss = priv;
10909 struct wpa_driver_nl80211_data *drv = bss->drv;
10910 struct nl_msg *msg;
10911 enum nl80211_tdls_operation nl80211_oper;
Paul Stewart092955c2017-02-06 09:13:09 -080010912 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010913
10914 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10915 return -EOPNOTSUPP;
10916
10917 switch (oper) {
10918 case TDLS_DISCOVERY_REQ:
10919 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
10920 break;
10921 case TDLS_SETUP:
10922 nl80211_oper = NL80211_TDLS_SETUP;
10923 break;
10924 case TDLS_TEARDOWN:
10925 nl80211_oper = NL80211_TDLS_TEARDOWN;
10926 break;
10927 case TDLS_ENABLE_LINK:
10928 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
10929 break;
10930 case TDLS_DISABLE_LINK:
10931 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
10932 break;
10933 case TDLS_ENABLE:
10934 return 0;
10935 case TDLS_DISABLE:
10936 return 0;
10937 default:
10938 return -EINVAL;
10939 }
10940
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010941 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_TDLS_OPER)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010942 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
10943 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
10944 nlmsg_free(msg);
10945 return -ENOBUFS;
10946 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010947
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010948 res = send_and_recv_cmd(drv, msg);
Paul Stewart092955c2017-02-06 09:13:09 -080010949 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
10950 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
10951 strerror(-res));
10952 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010953}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010954
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010955
10956static int
10957nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
10958 const struct hostapd_freq_params *params)
10959{
10960 struct i802_bss *bss = priv;
10961 struct wpa_driver_nl80211_data *drv = bss->drv;
10962 struct nl_msg *msg;
10963 int ret = -ENOBUFS;
10964
10965 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
10966 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
10967 return -EOPNOTSUPP;
10968
10969 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
10970 " oper_class=%u freq=%u",
10971 MAC2STR(addr), oper_class, params->freq);
10972 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
10973 if (!msg ||
10974 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10975 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
10976 (ret = nl80211_put_freq_params(msg, params))) {
10977 nlmsg_free(msg);
10978 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
10979 return ret;
10980 }
10981
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010982 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010983}
10984
10985
10986static int
10987nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
10988{
10989 struct i802_bss *bss = priv;
10990 struct wpa_driver_nl80211_data *drv = bss->drv;
10991 struct nl_msg *msg;
10992
10993 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
10994 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
10995 return -EOPNOTSUPP;
10996
10997 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
10998 MAC2STR(addr));
10999 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
11000 if (!msg ||
11001 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
11002 nlmsg_free(msg);
11003 wpa_printf(MSG_DEBUG,
11004 "nl80211: Could not build TDLS cancel chan switch");
11005 return -ENOBUFS;
11006 }
11007
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011008 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011009}
11010
11011#endif /* CONFIG TDLS */
11012
11013
Hai Shalomfdcde762020-04-02 11:19:20 -070011014static int driver_nl80211_set_key(void *priv,
11015 struct wpa_driver_set_key_params *params)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011016{
11017 struct i802_bss *bss = priv;
Hai Shalomfdcde762020-04-02 11:19:20 -070011018
11019 return wpa_driver_nl80211_set_key(bss, params);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011020}
11021
11022
11023static int driver_nl80211_scan2(void *priv,
11024 struct wpa_driver_scan_params *params)
11025{
11026 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011027#ifdef CONFIG_DRIVER_NL80211_QCA
11028 struct wpa_driver_nl80211_data *drv = bss->drv;
11029
11030 /*
11031 * Do a vendor specific scan if possible. If only_new_results is
11032 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
11033 * cannot be achieved through a vendor scan. The below condition may
11034 * need to be modified if new scan flags are added in the future whose
11035 * functionality can only be achieved through a normal scan.
11036 */
11037 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
11038 return wpa_driver_nl80211_vendor_scan(bss, params);
11039#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011040 return wpa_driver_nl80211_scan(bss, params);
11041}
11042
11043
11044static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -070011045 u16 reason_code)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011046{
11047 struct i802_bss *bss = priv;
11048 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11049}
11050
11051
11052static int driver_nl80211_authenticate(void *priv,
11053 struct wpa_driver_auth_params *params)
11054{
11055 struct i802_bss *bss = priv;
11056 return wpa_driver_nl80211_authenticate(bss, params);
11057}
11058
11059
11060static void driver_nl80211_deinit(void *priv)
11061{
11062 struct i802_bss *bss = priv;
11063 wpa_driver_nl80211_deinit(bss);
11064}
11065
11066
11067static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11068 const char *ifname)
11069{
11070 struct i802_bss *bss = priv;
11071 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11072}
11073
11074
Sunil Ravi99c035e2024-07-12 01:42:03 +000011075#ifdef CONFIG_IEEE80211BE
11076
11077static int driver_nl80211_link_remove(void *priv, enum wpa_driver_if_type type,
11078 const char *ifname, u8 link_id)
11079{
11080 struct i802_bss *bss = priv;
11081 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravic0f5d412024-09-11 22:12:49 +000011082 int ret;
Sunil Ravi99c035e2024-07-12 01:42:03 +000011083
11084 if (type != WPA_IF_AP_BSS ||
11085 !nl80211_link_valid(bss->valid_links, link_id))
11086 return -1;
11087
11088 wpa_printf(MSG_DEBUG,
11089 "nl80211: Teardown AP(%s) link %d (type=%d ifname=%s links=0x%x)",
11090 bss->ifname, link_id, type, ifname, bss->valid_links);
11091
11092 nl80211_remove_link(bss, link_id);
11093
11094 bss->ctx = bss->flink->ctx;
11095
Sunil Ravi7f769292024-07-23 22:21:32 +000011096 if (drv->first_bss == bss && bss->valid_links)
Sunil Ravi99c035e2024-07-12 01:42:03 +000011097 drv->ctx = bss->ctx;
11098
11099 if (!bss->valid_links) {
11100 wpa_printf(MSG_DEBUG,
11101 "nl80211: No more links remaining, so remove interface");
Sunil Ravic0f5d412024-09-11 22:12:49 +000011102 ret = wpa_driver_nl80211_if_remove(bss, type, ifname);
11103 if (ret)
11104 return ret;
11105
11106 /* Notify that the MLD interface is removed */
11107 wpa_supplicant_event(bss->ctx, EVENT_MLD_INTERFACE_FREED, NULL);
Sunil Ravi99c035e2024-07-12 01:42:03 +000011108 }
11109
11110 return 0;
11111}
11112
11113
Sunil Ravic0f5d412024-09-11 22:12:49 +000011114static bool nl80211_is_drv_shared(void *priv, int link_id)
Sunil Ravi99c035e2024-07-12 01:42:03 +000011115{
11116 struct i802_bss *bss = priv;
11117 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravic0f5d412024-09-11 22:12:49 +000011118 unsigned int num_bss = 0, num_links = 0;
11119 bool self = false;
11120 u8 i;
Sunil Ravi99c035e2024-07-12 01:42:03 +000011121
11122 /* If any other BSS exist, someone else is using this since at this
11123 * time, we would have removed all BSSs created by this driver and only
11124 * this BSS should be remaining if the driver is not shared by anyone.
11125 */
11126 for (bss = drv->first_bss; bss; bss = bss->next) {
11127 num_bss++;
11128 if (num_bss > 1)
11129 return true;
11130 }
11131
11132 /* This is the only BSS present */
11133 bss = priv;
11134
Sunil Ravic0f5d412024-09-11 22:12:49 +000011135 for_each_link(bss->valid_links, i) {
11136 num_links++;
11137 if (i == link_id)
11138 self = true;
11139 }
11140
11141 /* More than one links means some one is still sharing */
11142 if (num_links > 1)
11143 return true;
11144
11145 /* Even if only one link is there, it should match the given
11146 * link ID to assert that no one else is sharing. */
11147 if (num_links == 1 && self)
Sunil Ravi99c035e2024-07-12 01:42:03 +000011148 return false;
11149
Sunil Ravic0f5d412024-09-11 22:12:49 +000011150 /* No links are active means no one is sharing */
11151 if (num_links == 0)
Sunil Ravi99c035e2024-07-12 01:42:03 +000011152 return false;
11153
11154 return true;
11155}
11156
11157#endif /* CONFIG_IEEE80211BE */
11158
11159
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011160static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -070011161 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011162 unsigned int freq,
Hai Shalomfdcde762020-04-02 11:19:20 -070011163 const u16 *csa_offs, size_t csa_offs_len,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011164 int no_encrypt, unsigned int wait,
11165 int link_id)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011166{
11167 struct i802_bss *bss = priv;
11168 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Hai Shalomfdcde762020-04-02 11:19:20 -070011169 freq, 0, 0, wait, csa_offs,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011170 csa_offs_len, no_encrypt, link_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011171}
11172
11173
11174static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11175{
11176 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011177 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011178}
11179
11180
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011181static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011182 const char *ifname, int vlan_id,
11183 int link_id)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011184{
11185 struct i802_bss *bss = priv;
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011186 return i802_set_sta_vlan(bss, addr, ifname, vlan_id, link_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011187}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011188
11189
11190static int driver_nl80211_read_sta_data(void *priv,
11191 struct hostap_sta_driver_data *data,
11192 const u8 *addr)
11193{
11194 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -080011195
11196 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011197 return i802_read_sta_data(bss, data, addr);
11198}
11199
11200
11201static int driver_nl80211_send_action(void *priv, unsigned int freq,
11202 unsigned int wait_time,
11203 const u8 *dst, const u8 *src,
11204 const u8 *bssid,
11205 const u8 *data, size_t data_len,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000011206 int no_cck, int link_id)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011207{
11208 struct i802_bss *bss = priv;
11209 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000011210 bssid, data, data_len, no_cck,
11211 link_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011212}
11213
11214
11215static int driver_nl80211_probe_req_report(void *priv, int report)
11216{
11217 struct i802_bss *bss = priv;
11218 return wpa_driver_nl80211_probe_req_report(bss, report);
11219}
11220
11221
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011222static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11223 const u8 *ies, size_t ies_len)
11224{
11225 int ret;
11226 struct nl_msg *msg;
11227 struct i802_bss *bss = priv;
11228 struct wpa_driver_nl80211_data *drv = bss->drv;
11229 u16 mdid = WPA_GET_LE16(md);
11230
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011231 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000011232 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UPDATE_FT_IES)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011233 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
11234 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
11235 nlmsg_free(msg);
11236 return -ENOBUFS;
11237 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011238
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011239 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011240 if (ret) {
11241 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11242 "err=%d (%s)", ret, strerror(-ret));
11243 }
11244
11245 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011246}
11247
11248
Hai Shalom81f62d82019-07-22 12:10:00 -070011249static int nl80211_update_dh_ie(void *priv, const u8 *peer_mac,
11250 u16 reason_code, const u8 *ie, size_t ie_len)
11251{
11252 int ret;
11253 struct nl_msg *msg;
11254 struct i802_bss *bss = priv;
11255 struct wpa_driver_nl80211_data *drv = bss->drv;
11256
11257 wpa_printf(MSG_DEBUG, "nl80211: Updating DH IE peer: " MACSTR
11258 " reason %u", MAC2STR(peer_mac), reason_code);
11259 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UPDATE_OWE_INFO)) ||
11260 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer_mac) ||
11261 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, reason_code) ||
11262 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie))) {
11263 nlmsg_free(msg);
11264 return -ENOBUFS;
11265 }
11266
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011267 ret = send_and_recv_cmd(drv, msg);
Hai Shalom81f62d82019-07-22 12:10:00 -070011268 if (ret) {
11269 wpa_printf(MSG_DEBUG,
11270 "nl80211: update_dh_ie failed err=%d (%s)",
11271 ret, strerror(-ret));
11272 }
11273
11274 return ret;
11275}
11276
11277
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070011278static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011279{
11280 struct i802_bss *bss = priv;
11281 struct wpa_driver_nl80211_data *drv = bss->drv;
11282
11283 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11284 return NULL;
11285
11286 return bss->addr;
11287}
11288
11289
Dmitry Shmidt56052862013-10-04 10:23:25 -070011290static const char * scan_state_str(enum scan_states scan_state)
11291{
11292 switch (scan_state) {
11293 case NO_SCAN:
11294 return "NO_SCAN";
11295 case SCAN_REQUESTED:
11296 return "SCAN_REQUESTED";
11297 case SCAN_STARTED:
11298 return "SCAN_STARTED";
11299 case SCAN_COMPLETED:
11300 return "SCAN_COMPLETED";
11301 case SCAN_ABORTED:
11302 return "SCAN_ABORTED";
11303 case SCHED_SCAN_STARTED:
11304 return "SCHED_SCAN_STARTED";
11305 case SCHED_SCAN_STOPPED:
11306 return "SCHED_SCAN_STOPPED";
11307 case SCHED_SCAN_RESULTS:
11308 return "SCHED_SCAN_RESULTS";
11309 }
11310
11311 return "??";
11312}
11313
11314
11315static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11316{
11317 struct i802_bss *bss = priv;
11318 struct wpa_driver_nl80211_data *drv = bss->drv;
11319 int res;
11320 char *pos, *end;
Hai Shalom74f70d42019-02-11 14:42:39 -080011321 struct nl_msg *msg;
11322 char alpha2[3] = { 0, 0, 0 };
Dmitry Shmidt56052862013-10-04 10:23:25 -070011323
11324 pos = buf;
11325 end = buf + buflen;
11326
11327 res = os_snprintf(pos, end - pos,
11328 "ifindex=%d\n"
11329 "ifname=%s\n"
11330 "brname=%s\n"
11331 "addr=" MACSTR "\n"
11332 "freq=%d\n"
Hai Shalomc9e41a12018-07-31 14:41:42 -070011333 "%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011334 bss->ifindex,
11335 bss->ifname,
11336 bss->brname,
11337 MAC2STR(bss->addr),
Sunil Ravi036cec52023-03-29 11:35:17 -070011338 bss->flink->freq,
11339 bss->flink->beacon_set ? "beacon_set=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011340 bss->added_if_into_bridge ?
11341 "added_if_into_bridge=1\n" : "",
Hai Shalomc9e41a12018-07-31 14:41:42 -070011342 bss->already_in_bridge ? "already_in_bridge=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011343 bss->added_bridge ? "added_bridge=1\n" : "",
11344 bss->in_deinit ? "in_deinit=1\n" : "",
11345 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011346 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011347 return pos - buf;
11348 pos += res;
11349
11350 if (bss->wdev_id_set) {
11351 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11352 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011353 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011354 return pos - buf;
11355 pos += res;
11356 }
11357
11358 res = os_snprintf(pos, end - pos,
11359 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011360 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -070011361 "drv_ifindex=%d\n"
11362 "operstate=%d\n"
11363 "scan_state=%s\n"
11364 "auth_bssid=" MACSTR "\n"
11365 "auth_attempt_bssid=" MACSTR "\n"
11366 "bssid=" MACSTR "\n"
11367 "prev_bssid=" MACSTR "\n"
11368 "associated=%d\n"
11369 "assoc_freq=%u\n"
11370 "monitor_sock=%d\n"
11371 "monitor_ifidx=%d\n"
11372 "monitor_refcount=%d\n"
11373 "last_mgmt_freq=%u\n"
11374 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011375 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011376 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011377 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -070011378 drv->ifindex,
11379 drv->operstate,
11380 scan_state_str(drv->scan_state),
11381 MAC2STR(drv->auth_bssid),
11382 MAC2STR(drv->auth_attempt_bssid),
11383 MAC2STR(drv->bssid),
11384 MAC2STR(drv->prev_bssid),
11385 drv->associated,
11386 drv->assoc_freq,
11387 drv->monitor_sock,
11388 drv->monitor_ifidx,
11389 drv->monitor_refcount,
11390 drv->last_mgmt_freq,
11391 drv->eapol_tx_sock,
11392 drv->ignore_if_down_event ?
11393 "ignore_if_down_event=1\n" : "",
11394 drv->scan_complete_events ?
11395 "scan_complete_events=1\n" : "",
11396 drv->disabled_11b_rates ?
11397 "disabled_11b_rates=1\n" : "",
11398 drv->pending_remain_on_chan ?
11399 "pending_remain_on_chan=1\n" : "",
11400 drv->in_interface_list ? "in_interface_list=1\n" : "",
11401 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11402 drv->poll_command_supported ?
11403 "poll_command_supported=1\n" : "",
11404 drv->data_tx_status ? "data_tx_status=1\n" : "",
11405 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11406 drv->retry_auth ? "retry_auth=1\n" : "",
11407 drv->use_monitor ? "use_monitor=1\n" : "",
11408 drv->ignore_next_local_disconnect ?
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011409 "ignore_next_local_disconnect\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011410 drv->ignore_next_local_deauth ?
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011411 "ignore_next_local_deauth\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011412 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011413 return pos - buf;
11414 pos += res;
11415
Sunil Ravi89eba102022-09-13 21:04:37 -070011416 if (drv->sta_mlo_info.valid_links) {
11417 int i;
11418 struct driver_sta_mlo_info *mlo = &drv->sta_mlo_info;
11419
11420 res = os_snprintf(pos, end - pos,
Sunil Ravi640215c2023-06-28 23:08:09 +000011421 "ap_mld_addr=" MACSTR "\n"
11422 "default_map=%d\n",
11423 MAC2STR(mlo->ap_mld_addr),
11424 mlo->default_map);
Sunil Ravi89eba102022-09-13 21:04:37 -070011425 if (os_snprintf_error(end - pos, res))
11426 return pos - buf;
11427 pos += res;
11428
Sunil Ravi99c035e2024-07-12 01:42:03 +000011429 for_each_link(mlo->valid_links, i) {
Sunil Ravi89eba102022-09-13 21:04:37 -070011430 res = os_snprintf(pos, end - pos,
11431 "link_addr[%u]=" MACSTR "\n"
11432 "link_bssid[%u]=" MACSTR "\n"
11433 "link_freq[%u]=%u\n",
11434 i, MAC2STR(mlo->links[i].addr),
11435 i, MAC2STR(mlo->links[i].bssid),
11436 i, mlo->links[i].freq);
11437 if (os_snprintf_error(end - pos, res))
11438 return pos - buf;
11439 pos += res;
Sunil Ravi640215c2023-06-28 23:08:09 +000011440
11441 if (!mlo->default_map) {
11442 res = os_snprintf(
11443 pos, end - pos,
11444 "uplink_map[%u]=%x\n"
11445 "downlink_map[%u]=%x\n",
11446 i, mlo->links[i].t2lmap.uplink,
11447 i, mlo->links[i].t2lmap.downlink);
11448 if (os_snprintf_error(end - pos, res))
11449 return pos - buf;
11450 pos += res;
11451 }
Sunil Ravi89eba102022-09-13 21:04:37 -070011452 }
11453 }
11454
Dmitry Shmidt56052862013-10-04 10:23:25 -070011455 if (drv->has_capability) {
11456 res = os_snprintf(pos, end - pos,
11457 "capa.key_mgmt=0x%x\n"
11458 "capa.enc=0x%x\n"
11459 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011460 "capa.flags=0x%llx\n"
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011461 "capa.flags2=0x%llx\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011462 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -070011463 "capa.max_scan_ssids=%d\n"
11464 "capa.max_sched_scan_ssids=%d\n"
11465 "capa.sched_scan_supported=%d\n"
11466 "capa.max_match_sets=%d\n"
11467 "capa.max_remain_on_chan=%u\n"
11468 "capa.max_stations=%u\n"
11469 "capa.probe_resp_offloads=0x%x\n"
11470 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011471 "capa.num_multichan_concurrent=%u\n"
11472 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011473 "capa.mac_addr_rand_scan_supported=%d\n"
11474 "capa.conc_capab=%u\n"
11475 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080011476 "capa.max_conc_chan_5_0=%u\n"
11477 "capa.max_sched_scan_plans=%u\n"
11478 "capa.max_sched_scan_plan_interval=%u\n"
Sunil Ravi77d572f2023-01-17 23:58:31 +000011479 "capa.max_sched_scan_plan_iterations=%u\n"
11480 "capa.mbssid_max_interfaces=%u\n"
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000011481 "capa.ema_max_periodicity=%u\n"
11482 "capa.max_probe_req_ie_len=%zu\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011483 drv->capa.key_mgmt,
11484 drv->capa.enc,
11485 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011486 (unsigned long long) drv->capa.flags,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011487 (unsigned long long) drv->capa.flags2,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011488 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -070011489 drv->capa.max_scan_ssids,
11490 drv->capa.max_sched_scan_ssids,
11491 drv->capa.sched_scan_supported,
11492 drv->capa.max_match_sets,
11493 drv->capa.max_remain_on_chan,
11494 drv->capa.max_stations,
11495 drv->capa.probe_resp_offloads,
11496 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011497 drv->capa.num_multichan_concurrent,
11498 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011499 drv->capa.mac_addr_rand_scan_supported,
11500 drv->capa.conc_capab,
11501 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080011502 drv->capa.max_conc_chan_5_0,
11503 drv->capa.max_sched_scan_plans,
11504 drv->capa.max_sched_scan_plan_interval,
Sunil Ravi77d572f2023-01-17 23:58:31 +000011505 drv->capa.max_sched_scan_plan_iterations,
11506 drv->capa.mbssid_max_interfaces,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000011507 drv->capa.ema_max_periodicity,
11508 drv->capa.max_probe_req_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011509 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011510 return pos - buf;
11511 pos += res;
11512 }
11513
Hai Shalom74f70d42019-02-11 14:42:39 -080011514 msg = nlmsg_alloc();
11515 if (msg &&
11516 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG) &&
11517 nla_put_u32(msg, NL80211_ATTR_WIPHY, drv->wiphy_idx) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011518 if (send_and_recv_resp(drv, msg, nl80211_get_country,
11519 alpha2) == 0 &&
Hai Shalom74f70d42019-02-11 14:42:39 -080011520 alpha2[0]) {
11521 res = os_snprintf(pos, end - pos, "country=%s\n",
11522 alpha2);
11523 if (os_snprintf_error(end - pos, res))
11524 return pos - buf;
11525 pos += res;
11526 }
11527 } else {
11528 nlmsg_free(msg);
11529 }
11530
Dmitry Shmidt56052862013-10-04 10:23:25 -070011531 return pos - buf;
11532}
11533
11534
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011535static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11536{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011537 if ((settings->head &&
11538 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
11539 settings->head_len, settings->head)) ||
11540 (settings->tail &&
11541 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
11542 settings->tail_len, settings->tail)) ||
11543 (settings->beacon_ies &&
11544 nla_put(msg, NL80211_ATTR_IE,
11545 settings->beacon_ies_len, settings->beacon_ies)) ||
11546 (settings->proberesp_ies &&
11547 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
11548 settings->proberesp_ies_len, settings->proberesp_ies)) ||
11549 (settings->assocresp_ies &&
11550 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
11551 settings->assocresp_ies_len, settings->assocresp_ies)) ||
11552 (settings->probe_resp &&
11553 nla_put(msg, NL80211_ATTR_PROBE_RESP,
11554 settings->probe_resp_len, settings->probe_resp)))
11555 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011556
11557 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011558}
11559
11560
11561static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11562{
11563 struct nl_msg *msg;
11564 struct i802_bss *bss = priv;
11565 struct wpa_driver_nl80211_data *drv = bss->drv;
11566 struct nlattr *beacon_csa;
11567 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011568 int csa_off_len = 0;
11569 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011570
Hai Shalomc1a21442022-02-04 13:43:00 -080011571 wpa_printf(MSG_DEBUG,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011572 "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d channel=%d sec_channel_offset=%d width=%d cf1=%d cf2=%d puncturing_bitmap=0x%04x link_id=%d%s%s%s)",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011573 settings->cs_count, settings->block_tx,
Hai Shalomc1a21442022-02-04 13:43:00 -080011574 settings->freq_params.freq,
11575 settings->freq_params.channel,
11576 settings->freq_params.sec_channel_offset,
11577 settings->freq_params.bandwidth,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011578 settings->freq_params.center_freq1,
Hai Shalomc1a21442022-02-04 13:43:00 -080011579 settings->freq_params.center_freq2,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000011580 settings->freq_params.punct_bitmap,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011581 settings->link_id,
Hai Shalomc1a21442022-02-04 13:43:00 -080011582 settings->freq_params.ht_enabled ? " ht" : "",
11583 settings->freq_params.vht_enabled ? " vht" : "",
11584 settings->freq_params.he_enabled ? " he" : "");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011585
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011586 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011587 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11588 return -EOPNOTSUPP;
11589 }
11590
Roshan Pius3a1667e2018-07-03 15:17:14 -070011591 if (drv->nlmode != NL80211_IFTYPE_AP &&
11592 drv->nlmode != NL80211_IFTYPE_P2P_GO &&
11593 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011594 return -EOPNOTSUPP;
11595
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011596 /*
11597 * Remove empty counters, assuming Probe Response and Beacon frame
11598 * counters match. This implementation assumes that there are only two
11599 * counters.
11600 */
11601 if (settings->counter_offset_beacon[0] &&
11602 !settings->counter_offset_beacon[1]) {
11603 csa_off_len = 1;
11604 } else if (settings->counter_offset_beacon[1] &&
11605 !settings->counter_offset_beacon[0]) {
11606 csa_off_len = 1;
11607 settings->counter_offset_beacon[0] =
11608 settings->counter_offset_beacon[1];
11609 settings->counter_offset_presp[0] =
11610 settings->counter_offset_presp[1];
11611 } else if (settings->counter_offset_beacon[1] &&
11612 settings->counter_offset_beacon[0]) {
11613 csa_off_len = 2;
11614 } else {
11615 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
11616 return -EINVAL;
11617 }
11618
11619 /* Check CSA counters validity */
11620 if (drv->capa.max_csa_counters &&
11621 csa_off_len > drv->capa.max_csa_counters) {
11622 wpa_printf(MSG_ERROR,
11623 "nl80211: Too many CSA counters provided");
11624 return -EINVAL;
11625 }
11626
11627 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011628 return -EINVAL;
11629
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011630 for (i = 0; i < csa_off_len; i++) {
11631 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
11632 u16 csa_c_off_presp = settings->counter_offset_presp[i];
11633
11634 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
11635 (settings->beacon_csa.tail[csa_c_off_bcn] !=
11636 settings->cs_count))
11637 return -EINVAL;
11638
11639 if (settings->beacon_csa.probe_resp &&
11640 ((settings->beacon_csa.probe_resp_len <=
11641 csa_c_off_presp) ||
11642 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
11643 settings->cs_count)))
11644 return -EINVAL;
11645 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011646
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011647 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
11648 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
11649 settings->cs_count) ||
11650 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
11651 (settings->block_tx &&
Sunil Ravi036cec52023-03-29 11:35:17 -070011652 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)) ||
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000011653 (settings->freq_params.punct_bitmap &&
Sunil Ravi036cec52023-03-29 11:35:17 -070011654 nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000011655 settings->freq_params.punct_bitmap)) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011656 (settings->link_id != NL80211_DRV_LINK_ID_NA &&
11657 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, settings->link_id)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011658 goto error;
11659
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011660 /* beacon_after params */
11661 ret = set_beacon_data(msg, &settings->beacon_after);
11662 if (ret)
11663 goto error;
11664
11665 /* beacon_csa params */
11666 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
11667 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011668 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011669
11670 ret = set_beacon_data(msg, &settings->beacon_csa);
11671 if (ret)
11672 goto error;
11673
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011674 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
11675 csa_off_len * sizeof(u16),
11676 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011677 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011678 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
11679 csa_off_len * sizeof(u16),
11680 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011681 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011682
11683 nla_nest_end(msg, beacon_csa);
Sunil Ravi7f769292024-07-23 22:21:32 +000011684
11685#ifdef CONFIG_IEEE80211AX
11686 if (settings->ubpr.unsol_bcast_probe_resp_interval &&
11687 nl80211_unsol_bcast_probe_resp(bss, msg, &settings->ubpr) < 0)
11688 goto fail;
11689#endif /* CONFIG_IEEE80211AX */
11690
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011691 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011692 if (ret) {
11693 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
11694 ret, strerror(-ret));
11695 }
11696 return ret;
11697
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011698fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011699 ret = -ENOBUFS;
11700error:
11701 nlmsg_free(msg);
11702 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
11703 return ret;
11704}
11705
11706
Sunil Ravia04bd252022-05-02 22:54:18 -070011707#ifdef CONFIG_IEEE80211AX
11708static int nl80211_switch_color(void *priv, struct cca_settings *settings)
11709{
11710 struct i802_bss *bss = priv;
11711 struct wpa_driver_nl80211_data *drv = bss->drv;
11712 struct nlattr *beacon_cca;
11713 struct nl_msg *msg;
11714 int ret = -ENOBUFS;
11715
11716 wpa_printf(MSG_DEBUG,
11717 "nl80211: Color change request (cca_count=%u color=%d)",
11718 settings->cca_count, settings->cca_color);
11719
11720 if (drv->nlmode != NL80211_IFTYPE_AP)
11721 return -EOPNOTSUPP;
11722
11723 if (!settings->beacon_cca.tail)
11724 return -EINVAL;
11725
11726 if (settings->beacon_cca.tail_len <= settings->counter_offset_beacon ||
11727 settings->beacon_cca.tail[settings->counter_offset_beacon] !=
11728 settings->cca_count)
11729 return -EINVAL;
11730
11731 if (settings->beacon_cca.probe_resp &&
11732 (settings->beacon_cca.probe_resp_len <=
11733 settings->counter_offset_presp ||
11734 settings->beacon_cca.probe_resp[settings->counter_offset_presp] !=
11735 settings->cca_count))
11736 return -EINVAL;
11737
11738 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_COLOR_CHANGE_REQUEST);
11739 if (!msg ||
11740 nla_put_u8(msg, NL80211_ATTR_COLOR_CHANGE_COUNT,
11741 settings->cca_count) ||
11742 nla_put_u8(msg, NL80211_ATTR_COLOR_CHANGE_COLOR,
11743 settings->cca_color))
11744 goto error;
11745
11746 /* beacon_after params */
11747 ret = set_beacon_data(msg, &settings->beacon_after);
11748 if (ret)
11749 goto error;
11750
11751 /* beacon_csa params */
11752 beacon_cca = nla_nest_start(msg, NL80211_ATTR_COLOR_CHANGE_ELEMS);
11753 if (!beacon_cca) {
11754 ret = -ENOBUFS;
11755 goto error;
11756 }
11757
11758 ret = set_beacon_data(msg, &settings->beacon_cca);
11759 if (ret)
11760 goto error;
11761
11762 if (nla_put_u16(msg, NL80211_ATTR_CNTDWN_OFFS_BEACON,
11763 settings->counter_offset_beacon) ||
11764 (settings->beacon_cca.probe_resp &&
11765 nla_put_u16(msg, NL80211_ATTR_CNTDWN_OFFS_PRESP,
11766 settings->counter_offset_presp))) {
11767 ret = -ENOBUFS;
11768 goto error;
11769 }
11770
11771 nla_nest_end(msg, beacon_cca);
Sunil Ravi7f769292024-07-23 22:21:32 +000011772
11773 if (settings->ubpr.unsol_bcast_probe_resp_interval &&
11774 nl80211_unsol_bcast_probe_resp(bss, msg, &settings->ubpr) < 0) {
11775 ret = -ENOBUFS;
11776 goto error;
11777 }
11778
11779#ifdef CONFIG_IEEE80211BE
11780 if (nl80211_link_valid(bss->valid_links, settings->link_id)) {
11781 wpa_printf(MSG_DEBUG,
11782 "nl80211: Color change request on link_id=%d",
11783 settings->link_id);
11784
11785 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
11786 settings->link_id)) {
11787 nlmsg_free(msg);
11788 return -1;
11789 }
11790 }
11791#endif /* CONFIG_IEEE80211BE */
11792
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011793 ret = send_and_recv_cmd(drv, msg);
Sunil Ravia04bd252022-05-02 22:54:18 -070011794 if (ret) {
11795 wpa_printf(MSG_DEBUG,
11796 "nl80211: switch_color failed err=%d (%s)",
11797 ret, strerror(-ret));
11798 }
11799 return ret;
11800
11801error:
11802 nlmsg_free(msg);
11803 wpa_printf(MSG_DEBUG, "nl80211: Could not build color switch request");
11804 return ret;
11805}
11806#endif /* CONFIG_IEEE80211AX */
11807
11808
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011809static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
11810 u8 user_priority, u16 admitted_time)
11811{
11812 struct i802_bss *bss = priv;
11813 struct wpa_driver_nl80211_data *drv = bss->drv;
11814 struct nl_msg *msg;
11815 int ret;
11816
11817 wpa_printf(MSG_DEBUG,
11818 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
11819 tsid, admitted_time, user_priority);
11820
11821 if (!is_sta_interface(drv->nlmode))
11822 return -ENOTSUP;
11823
11824 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
11825 if (!msg ||
11826 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
11827 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11828 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
11829 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
11830 nlmsg_free(msg);
11831 return -ENOBUFS;
11832 }
11833
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011834 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011835 if (ret)
11836 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
11837 ret, strerror(-ret));
11838 return ret;
11839}
11840
11841
11842static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
11843{
11844 struct i802_bss *bss = priv;
11845 struct wpa_driver_nl80211_data *drv = bss->drv;
11846 struct nl_msg *msg;
11847 int ret;
11848
11849 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
11850
11851 if (!is_sta_interface(drv->nlmode))
11852 return -ENOTSUP;
11853
11854 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
11855 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
11856 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
11857 nlmsg_free(msg);
11858 return -ENOBUFS;
11859 }
11860
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011861 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011862 if (ret)
11863 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
11864 ret, strerror(-ret));
11865 return ret;
11866}
11867
11868
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011869#ifdef CONFIG_TESTING_OPTIONS
11870static int cmd_reply_handler(struct nl_msg *msg, void *arg)
11871{
11872 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11873 struct wpabuf *buf = arg;
11874
11875 if (!buf)
11876 return NL_SKIP;
11877
11878 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
11879 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
11880 return NL_SKIP;
11881 }
11882
11883 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
11884 genlmsg_attrlen(gnlh, 0));
11885
11886 return NL_SKIP;
11887}
11888#endif /* CONFIG_TESTING_OPTIONS */
11889
11890
11891static int vendor_reply_handler(struct nl_msg *msg, void *arg)
11892{
11893 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11894 struct nlattr *nl_vendor_reply, *nl;
11895 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11896 struct wpabuf *buf = arg;
11897 int rem;
11898
11899 if (!buf)
11900 return NL_SKIP;
11901
11902 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11903 genlmsg_attrlen(gnlh, 0), NULL);
11904 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
11905
11906 if (!nl_vendor_reply)
11907 return NL_SKIP;
11908
11909 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
11910 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
11911 return NL_SKIP;
11912 }
11913
11914 nla_for_each_nested(nl, nl_vendor_reply, rem) {
11915 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
11916 }
11917
11918 return NL_SKIP;
11919}
11920
11921
Hai Shalom60840252021-02-19 19:02:11 -080011922static bool is_cmd_with_nested_attrs(unsigned int vendor_id,
11923 unsigned int subcmd)
11924{
11925 if (vendor_id != OUI_QCA)
11926 return true;
11927
11928 switch (subcmd) {
11929 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
11930 case QCA_NL80211_VENDOR_SUBCMD_STATS_EXT:
11931 case QCA_NL80211_VENDOR_SUBCMD_SCANNING_MAC_OUI:
11932 case QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY:
11933 case QCA_NL80211_VENDOR_SUBCMD_SPECTRAL_SCAN_GET_STATUS:
11934 case QCA_NL80211_VENDOR_SUBCMD_NAN:
11935 return false;
11936 default:
11937 return true;
11938 }
11939}
11940
11941
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011942static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
11943 unsigned int subcmd, const u8 *data,
Hai Shalom60840252021-02-19 19:02:11 -080011944 size_t data_len, enum nested_attr nested_attr,
11945 struct wpabuf *buf)
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011946{
11947 struct i802_bss *bss = priv;
11948 struct wpa_driver_nl80211_data *drv = bss->drv;
11949 struct nl_msg *msg;
Hai Shalom60840252021-02-19 19:02:11 -080011950 int ret, nla_flag;
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011951
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011952#ifdef CONFIG_TESTING_OPTIONS
11953 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011954 msg = nlmsg_alloc();
11955 if (!msg)
11956 return -ENOMEM;
11957
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011958 nl80211_cmd(drv, msg, 0, subcmd);
11959 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
11960 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011961 goto fail;
Hai Shalomb755a2a2020-04-23 21:49:02 -070011962 /* This test vendor_cmd can be used with nl80211 commands that
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011963 * need the connect nl_sock, so use the variant that takes in
11964 * bss->nl_connect as the handle. */
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000011965 ret = send_and_recv(drv, bss->nl_connect, msg,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011966 cmd_reply_handler, buf, NULL, NULL, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011967 if (ret)
11968 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
11969 ret);
11970 return ret;
11971 }
11972#endif /* CONFIG_TESTING_OPTIONS */
11973
Hai Shalom60840252021-02-19 19:02:11 -080011974 if (nested_attr == NESTED_ATTR_USED)
11975 nla_flag = NLA_F_NESTED;
11976 else if (nested_attr == NESTED_ATTR_UNSPECIFIED &&
11977 is_cmd_with_nested_attrs(vendor_id, subcmd))
11978 nla_flag = NLA_F_NESTED;
11979 else
11980 nla_flag = 0;
11981
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011982 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
11983 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
11984 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
11985 (data &&
Hai Shalom60840252021-02-19 19:02:11 -080011986 nla_put(msg, nla_flag | NL80211_ATTR_VENDOR_DATA,
11987 data_len, data)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011988 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011989
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011990 ret = send_and_recv_resp(drv, msg, vendor_reply_handler, buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011991 if (ret)
11992 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
11993 ret);
11994 return ret;
11995
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011996fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011997 nlmsg_free(msg);
11998 return -ENOBUFS;
11999}
12000
12001
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012002static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
12003 u8 qos_map_set_len)
12004{
12005 struct i802_bss *bss = priv;
12006 struct wpa_driver_nl80211_data *drv = bss->drv;
12007 struct nl_msg *msg;
12008 int ret;
12009
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012010 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
12011 qos_map_set, qos_map_set_len);
12012
Sunil Ravi2a14cf12023-11-21 00:54:38 +000012013 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_QOS_MAP)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012014 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
12015 nlmsg_free(msg);
12016 return -ENOBUFS;
12017 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012018
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012019 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012020 if (ret)
12021 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
12022
12023 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012024}
12025
12026
Hai Shalomfdcde762020-04-02 11:19:20 -070012027static int get_wowlan_handler(struct nl_msg *msg, void *arg)
12028{
12029 struct nlattr *tb[NL80211_ATTR_MAX + 1];
12030 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12031 int *wowlan_enabled = arg;
12032
12033 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
12034 genlmsg_attrlen(gnlh, 0), NULL);
12035
12036 *wowlan_enabled = !!tb[NL80211_ATTR_WOWLAN_TRIGGERS];
12037
12038 return NL_SKIP;
12039}
12040
12041
12042static int nl80211_get_wowlan(void *priv)
12043{
12044 struct i802_bss *bss = priv;
12045 struct wpa_driver_nl80211_data *drv = bss->drv;
12046 struct nl_msg *msg;
12047 int wowlan_enabled;
12048 int ret;
12049
12050 wpa_printf(MSG_DEBUG, "nl80211: Getting wowlan status");
12051
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000012052 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_WOWLAN);
Hai Shalomfdcde762020-04-02 11:19:20 -070012053
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012054 ret = send_and_recv_resp(drv, msg, get_wowlan_handler, &wowlan_enabled);
Hai Shalomfdcde762020-04-02 11:19:20 -070012055 if (ret) {
12056 wpa_printf(MSG_DEBUG, "nl80211: Getting wowlan status failed");
12057 return 0;
12058 }
12059
12060 wpa_printf(MSG_DEBUG, "nl80211: wowlan is %s",
12061 wowlan_enabled ? "enabled" : "disabled");
12062
12063 return wowlan_enabled;
12064}
12065
12066
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012067static int nl80211_set_wowlan(void *priv,
12068 const struct wowlan_triggers *triggers)
12069{
12070 struct i802_bss *bss = priv;
12071 struct wpa_driver_nl80211_data *drv = bss->drv;
12072 struct nl_msg *msg;
12073 struct nlattr *wowlan_triggers;
12074 int ret;
12075
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012076 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
12077
Dmitry Shmidta3dc3092015-06-23 11:21:28 -070012078 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012079 !(wowlan_triggers = nla_nest_start(msg,
12080 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
12081 (triggers->any &&
12082 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
12083 (triggers->disconnect &&
12084 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
12085 (triggers->magic_pkt &&
12086 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
12087 (triggers->gtk_rekey_failure &&
12088 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
12089 (triggers->eap_identity_req &&
12090 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
12091 (triggers->four_way_handshake &&
12092 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
12093 (triggers->rfkill_release &&
12094 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
12095 nlmsg_free(msg);
12096 return -ENOBUFS;
12097 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012098
12099 nla_nest_end(msg, wowlan_triggers);
12100
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012101 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012102 if (ret)
12103 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
12104
12105 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012106}
12107
12108
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012109#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012110static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
12111{
12112 struct i802_bss *bss = priv;
12113 struct wpa_driver_nl80211_data *drv = bss->drv;
12114 struct nl_msg *msg;
12115 struct nlattr *params;
12116
12117 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
12118
12119 if (!drv->roaming_vendor_cmd_avail) {
12120 wpa_printf(MSG_DEBUG,
12121 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
12122 return -1;
12123 }
12124
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000012125 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012126 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12127 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12128 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
12129 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12130 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
12131 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
12132 QCA_ROAMING_NOT_ALLOWED) ||
12133 (bssid &&
12134 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
12135 nlmsg_free(msg);
12136 return -1;
12137 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012138 nla_nest_end(msg, params);
12139
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012140 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012141}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070012142
12143
Roshan Pius3a1667e2018-07-03 15:17:14 -070012144static int nl80211_disable_fils(void *priv, int disable)
12145{
12146 struct i802_bss *bss = priv;
12147 struct wpa_driver_nl80211_data *drv = bss->drv;
12148 struct nl_msg *msg;
12149 struct nlattr *params;
12150
12151 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
12152
12153 if (!drv->set_wifi_conf_vendor_cmd_avail)
12154 return -1;
12155
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000012156 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Roshan Pius3a1667e2018-07-03 15:17:14 -070012157 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12158 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12159 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
12160 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12161 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
12162 disable)) {
12163 nlmsg_free(msg);
12164 return -1;
12165 }
12166 nla_nest_end(msg, params);
12167
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012168 return send_and_recv_cmd(drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -070012169}
12170
12171
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070012172/* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
12173#define WPA_SUPPLICANT_CLIENT_ID 1
12174
Hai Shalom899fcc72020-10-19 14:38:18 -070012175static int nl80211_set_bssid_tmp_disallow(void *priv, unsigned int num_bssid,
12176 const u8 *bssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070012177{
12178 struct i802_bss *bss = priv;
12179 struct wpa_driver_nl80211_data *drv = bss->drv;
12180 struct nl_msg *msg;
12181 struct nlattr *params, *nlbssids, *attr;
12182 unsigned int i;
12183
Hai Shalom899fcc72020-10-19 14:38:18 -070012184 wpa_printf(MSG_DEBUG,
12185 "nl80211: Set temporarily disallowed BSSIDs (num=%u)",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070012186 num_bssid);
12187
12188 if (!drv->roam_vendor_cmd_avail)
12189 return -1;
12190
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000012191 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070012192 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12193 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12194 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
12195 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12196 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
Hai Shalomc3565922019-10-28 11:58:20 -070012197 QCA_WLAN_VENDOR_ROAMING_SUBCMD_SET_BLACKLIST_BSSID) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070012198 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
12199 WPA_SUPPLICANT_CLIENT_ID) ||
12200 nla_put_u32(msg,
12201 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
12202 num_bssid))
12203 goto fail;
12204
12205 nlbssids = nla_nest_start(
12206 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
12207 if (!nlbssids)
12208 goto fail;
12209
12210 for (i = 0; i < num_bssid; i++) {
12211 attr = nla_nest_start(msg, i);
12212 if (!attr)
12213 goto fail;
12214 if (nla_put(msg,
12215 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
12216 ETH_ALEN, &bssid[i * ETH_ALEN]))
12217 goto fail;
12218 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
12219 MAC2STR(&bssid[i * ETH_ALEN]));
12220 nla_nest_end(msg, attr);
12221 }
12222 nla_nest_end(msg, nlbssids);
12223 nla_nest_end(msg, params);
12224
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012225 return send_and_recv_cmd(drv, msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070012226
12227fail:
12228 nlmsg_free(msg);
12229 return -1;
12230}
12231
Hai Shalomc3565922019-10-28 11:58:20 -070012232
12233static int nl80211_add_sta_node(void *priv, const u8 *addr, u16 auth_alg)
12234{
12235 struct i802_bss *bss = priv;
12236 struct wpa_driver_nl80211_data *drv = bss->drv;
12237 struct nl_msg *msg;
12238 struct nlattr *params;
12239
12240 if (!drv->add_sta_node_vendor_cmd_avail)
12241 return -EOPNOTSUPP;
12242
12243 wpa_printf(MSG_DEBUG, "nl80211: Add STA node");
12244
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000012245 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Hai Shalomc3565922019-10-28 11:58:20 -070012246 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12247 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12248 QCA_NL80211_VENDOR_SUBCMD_ADD_STA_NODE) ||
12249 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12250 (addr &&
12251 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ADD_STA_NODE_MAC_ADDR, ETH_ALEN,
12252 addr)) ||
12253 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ADD_STA_NODE_AUTH_ALGO,
12254 auth_alg)) {
12255 nlmsg_free(msg);
12256 wpa_printf(MSG_ERROR,
12257 "%s: err in adding vendor_cmd and vendor_data",
12258 __func__);
12259 return -1;
12260 }
12261 nla_nest_end(msg, params);
12262
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012263 return send_and_recv_cmd(drv, msg);
Hai Shalomc3565922019-10-28 11:58:20 -070012264}
12265
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012266#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012267
12268
12269static int nl80211_set_mac_addr(void *priv, const u8 *addr)
12270{
12271 struct i802_bss *bss = priv;
12272 struct wpa_driver_nl80211_data *drv = bss->drv;
12273 int new_addr = addr != NULL;
Andy Kuoaba17c12022-04-14 16:05:31 +080012274#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali8a8f1002020-10-06 22:41:40 +053012275 struct nl_msg *msg;
12276 struct nlattr *params;
12277 int ret;
Andy Kuoaba17c12022-04-14 16:05:31 +080012278#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Mir Ali8a8f1002020-10-06 22:41:40 +053012279 wpa_printf(MSG_DEBUG, "Enter: %s", __FUNCTION__);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012280
Dmitry Shmidt849734c2016-05-27 09:59:01 -070012281 if (TEST_FAIL())
12282 return -1;
Mir Ali8a8f1002020-10-06 22:41:40 +053012283 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
Andy Kuoaba17c12022-04-14 16:05:31 +080012284#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali8a8f1002020-10-06 22:41:40 +053012285 if (!addr ) {
12286 addr = drv->global->p2p_perm_addr;
12287 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -070012288
Mir Ali8a8f1002020-10-06 22:41:40 +053012289 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
12290 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
12291 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
Hai Shalomc1a21442022-02-04 13:43:00 -080012292 BRCM_VENDOR_SCMD_SET_MAC) ||
Mir Ali8a8f1002020-10-06 22:41:40 +053012293 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12294 nla_put(msg, BRCM_ATTR_DRIVER_MAC_ADDR, ETH_ALEN, addr)) {
12295 wpa_printf(MSG_ERROR, "failed to put p2p randmac");
12296 nl80211_nlmsg_clear(msg);
12297 nlmsg_free(msg);
12298 return -ENOBUFS;
12299 }
12300 nla_nest_end(msg, params);
12301
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012302 ret = send_and_recv_cmd(drv, msg);
Mir Ali8a8f1002020-10-06 22:41:40 +053012303 if (ret) {
12304 wpa_printf(MSG_ERROR, "nl80211: p2p set macaddr failed: ret=%d (%s)",
12305 ret, strerror(-ret));
12306 }
12307 memcpy(bss->addr, addr, ETH_ALEN);
12308 return ret;
12309#else
12310 return -ENOTSUP;
Andy Kuoaba17c12022-04-14 16:05:31 +080012311#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Mir Ali8a8f1002020-10-06 22:41:40 +053012312 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012313 if (!addr)
12314 addr = drv->perm_addr;
12315
12316 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
12317 return -1;
12318
12319 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
12320 {
12321 wpa_printf(MSG_DEBUG,
Mir Ali8a8f1002020-10-06 22:41:40 +053012322 "nl80211: failed to set_mac_addr for %s to " MACSTR,
12323 bss->ifname, MAC2STR(addr));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012324 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
Mir Ali8a8f1002020-10-06 22:41:40 +053012325 1) < 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012326 wpa_printf(MSG_DEBUG,
Mir Ali8a8f1002020-10-06 22:41:40 +053012327 "nl80211: Could not restore interface UP after failed set_mac_addr");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012328 }
12329 return -1;
12330 }
12331
12332 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000012333 bss->ifname, MAC2STR(addr));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012334 drv->addr_changed = new_addr;
Sunil Ravi77d572f2023-01-17 23:58:31 +000012335 os_memcpy(bss->prev_addr, bss->addr, ETH_ALEN);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012336 os_memcpy(bss->addr, addr, ETH_ALEN);
12337
Hsiu-Chang Chen289286d2024-01-24 18:00:43 +080012338 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
12339 {
12340 wpa_printf(MSG_DEBUG,
12341 "nl80211: Could not restore interface UP after set_mac_addr");
12342 }
12343
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012344 return 0;
12345}
12346
12347
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012348#ifdef CONFIG_MESH
12349
12350static int wpa_driver_nl80211_init_mesh(void *priv)
12351{
12352 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
12353 wpa_printf(MSG_INFO,
12354 "nl80211: Failed to set interface into mesh mode");
12355 return -1;
12356 }
12357 return 0;
12358}
12359
12360
Dmitry Shmidtff787d52015-01-12 13:01:47 -080012361static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
12362 size_t mesh_id_len)
12363{
12364 if (mesh_id) {
Hai Shalom74f70d42019-02-11 14:42:39 -080012365 wpa_printf(MSG_DEBUG, " * Mesh ID (SSID)=%s",
12366 wpa_ssid_txt(mesh_id, mesh_id_len));
Dmitry Shmidtff787d52015-01-12 13:01:47 -080012367 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
12368 }
12369
12370 return 0;
12371}
12372
12373
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012374static int nl80211_put_mesh_config(struct nl_msg *msg,
12375 struct wpa_driver_mesh_bss_params *params)
12376{
12377 struct nlattr *container;
12378
12379 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
12380 if (!container)
12381 return -1;
12382
12383 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070012384 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
12385 params->auto_plinks)) ||
Hai Shalomc1a21442022-02-04 13:43:00 -080012386 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_FORWARDING) &&
12387 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
12388 params->forwarding)) ||
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012389 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
12390 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070012391 params->max_peer_links)) ||
12392 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
12393 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
12394 params->rssi_threshold)))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012395 return -1;
12396
12397 /*
12398 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
12399 * the timer could disconnect stations even in that case.
12400 */
12401 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
12402 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
12403 params->peer_link_timeout)) {
12404 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
12405 return -1;
12406 }
12407
12408 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
12409 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
12410 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
12411 return -1;
12412 }
12413
12414 nla_nest_end(msg, container);
12415
12416 return 0;
12417}
12418
12419
Dmitry Shmidt7f656022015-02-25 14:36:37 -080012420static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012421 struct wpa_driver_mesh_join_params *params)
12422{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012423 struct wpa_driver_nl80211_data *drv = bss->drv;
12424 struct nl_msg *msg;
12425 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080012426 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012427
12428 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000012429 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -080012430 if (!msg ||
12431 nl80211_put_freq_params(msg, &params->freq) ||
12432 nl80211_put_basic_rates(msg, params->basic_rates) ||
12433 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070012434 nl80211_put_beacon_int(msg, params->beacon_int) ||
12435 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012436 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012437
12438 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
12439
Hai Shalom60840252021-02-19 19:02:11 -080012440 if (params->handle_dfs && nla_put_flag(msg, NL80211_ATTR_HANDLE_DFS))
12441 goto fail;
12442
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012443 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
12444 if (!container)
12445 goto fail;
12446
12447 if (params->ies) {
12448 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
12449 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
12450 params->ies))
12451 goto fail;
12452 }
12453 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
12454 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
12455 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
12456 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
12457 goto fail;
12458 }
12459 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
12460 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
12461 goto fail;
12462 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
12463 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
12464 goto fail;
12465 nla_nest_end(msg, container);
12466
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012467 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
12468 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
12469 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
12470 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012471 goto fail;
12472
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012473 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
12474 return -1;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000012475 ret = send_and_recv(drv, bss->nl_connect, msg, NULL, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012476 NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012477 msg = NULL;
12478 if (ret) {
12479 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
12480 ret, strerror(-ret));
12481 goto fail;
12482 }
12483 ret = 0;
Sunil Ravi036cec52023-03-29 11:35:17 -070012484 drv->assoc_freq = bss->flink->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012485 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
12486
12487fail:
12488 nlmsg_free(msg);
12489 return ret;
12490}
12491
12492
Dmitry Shmidt7f656022015-02-25 14:36:37 -080012493static int
12494wpa_driver_nl80211_join_mesh(void *priv,
12495 struct wpa_driver_mesh_join_params *params)
12496{
12497 struct i802_bss *bss = priv;
12498 int ret, timeout;
12499
12500 timeout = params->conf.peer_link_timeout;
12501
12502 /* Disable kernel inactivity timer */
12503 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
12504 params->conf.peer_link_timeout = 0;
12505
12506 ret = nl80211_join_mesh(bss, params);
12507 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
12508 wpa_printf(MSG_DEBUG,
12509 "nl80211: Mesh join retry for peer_link_timeout");
12510 /*
12511 * Old kernel does not support setting
12512 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
12513 * into future from peer_link_timeout.
12514 */
12515 params->conf.peer_link_timeout = timeout + 60;
12516 ret = nl80211_join_mesh(priv, params);
12517 }
12518
12519 params->conf.peer_link_timeout = timeout;
12520 return ret;
12521}
12522
12523
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012524static int wpa_driver_nl80211_leave_mesh(void *priv)
12525{
12526 struct i802_bss *bss = priv;
12527 struct wpa_driver_nl80211_data *drv = bss->drv;
12528 struct nl_msg *msg;
12529 int ret;
12530
12531 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000012532 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_LEAVE_MESH);
12533 ret = send_and_recv(drv, bss->nl_connect, msg, NULL, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012534 NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012535 if (ret) {
12536 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
12537 ret, strerror(-ret));
12538 } else {
12539 wpa_printf(MSG_DEBUG,
12540 "nl80211: mesh leave request send successfully");
Sunil Ravi036cec52023-03-29 11:35:17 -070012541 drv->first_bss->flink->freq = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012542 }
12543
Hai Shalomc1a21442022-02-04 13:43:00 -080012544 if (drv->start_mode_sta &&
12545 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012546 NL80211_IFTYPE_STATION)) {
12547 wpa_printf(MSG_INFO,
12548 "nl80211: Failed to set interface into station mode");
12549 }
12550 return ret;
12551}
12552
Hai Shalom81f62d82019-07-22 12:10:00 -070012553
12554static int nl80211_probe_mesh_link(void *priv, const u8 *addr, const u8 *eth,
12555 size_t len)
12556{
12557 struct i802_bss *bss = priv;
12558 struct wpa_driver_nl80211_data *drv = bss->drv;
12559 struct nl_msg *msg;
12560 int ret;
12561
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000012562 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_MESH_LINK);
Hai Shalom81f62d82019-07-22 12:10:00 -070012563 if (!msg ||
12564 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
12565 nla_put(msg, NL80211_ATTR_FRAME, len, eth)) {
12566 nlmsg_free(msg);
12567 return -ENOBUFS;
12568 }
12569
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012570 ret = send_and_recv_cmd(drv, msg);
Hai Shalom81f62d82019-07-22 12:10:00 -070012571 if (ret) {
12572 wpa_printf(MSG_DEBUG, "nl80211: mesh link probe to " MACSTR
12573 " failed: ret=%d (%s)",
12574 MAC2STR(addr), ret, strerror(-ret));
12575 } else {
12576 wpa_printf(MSG_DEBUG, "nl80211: Mesh link to " MACSTR
12577 " probed successfully", MAC2STR(addr));
12578 }
12579
12580 return ret;
12581}
12582
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012583#endif /* CONFIG_MESH */
12584
12585
12586static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
12587 const u8 *ipaddr, int prefixlen,
12588 const u8 *addr)
12589{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012590 struct i802_bss *bss = priv;
12591 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012592 struct ndmsg nhdr = {
12593 .ndm_state = NUD_PERMANENT,
12594 .ndm_ifindex = bss->br_ifindex,
12595 };
12596 struct nl_msg *msg;
12597 int addrsize;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012598 int res;
12599
12600 if (!ipaddr || prefixlen == 0 || !addr)
12601 return -EINVAL;
12602
12603 if (bss->br_ifindex == 0) {
12604 wpa_printf(MSG_DEBUG,
12605 "nl80211: bridge must be set before adding an ip neigh to it");
12606 return -1;
12607 }
12608
12609 if (!drv->rtnl_sk) {
12610 wpa_printf(MSG_DEBUG,
12611 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
12612 return -1;
12613 }
12614
12615 if (version == 4) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012616 nhdr.ndm_family = AF_INET;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012617 addrsize = 4;
12618 } else if (version == 6) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012619 nhdr.ndm_family = AF_INET6;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012620 addrsize = 16;
12621 } else {
12622 return -EINVAL;
12623 }
12624
Sunil Ravi99c035e2024-07-12 01:42:03 +000012625 msg = nlmsg_alloc_simple(RTM_NEWNEIGH, NLM_F_CREATE);
12626 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012627 return -ENOMEM;
12628
Sunil Ravi99c035e2024-07-12 01:42:03 +000012629 res = -ENOMEM;
12630 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0 ||
12631 nla_put(msg, NDA_DST, addrsize, (void *) ipaddr) ||
12632 nla_put(msg, NDA_LLADDR, ETH_ALEN, (void *) addr))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012633 goto errout;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012634
12635 res = nl_send_auto_complete(drv->rtnl_sk, msg);
12636 if (res < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012637 goto errout;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012638
Sunil Ravi99c035e2024-07-12 01:42:03 +000012639 res = nl_wait_for_ack(drv->rtnl_sk);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012640 if (res) {
12641 wpa_printf(MSG_DEBUG,
12642 "nl80211: Adding bridge ip neigh failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012643 nl_geterror(res));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012644 }
12645errout:
Sunil Ravi99c035e2024-07-12 01:42:03 +000012646 nlmsg_free(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012647 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012648}
12649
12650
12651static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
12652 const u8 *ipaddr)
12653{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012654 struct i802_bss *bss = priv;
12655 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012656 struct ndmsg nhdr = {
12657 .ndm_state = NUD_PERMANENT,
12658 .ndm_ifindex = bss->br_ifindex,
12659 };
12660 struct nl_msg *msg;
12661 int addrsize;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012662 int res;
12663
12664 if (!ipaddr)
12665 return -EINVAL;
12666
12667 if (version == 4) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012668 nhdr.ndm_family = AF_INET;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012669 addrsize = 4;
12670 } else if (version == 6) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012671 nhdr.ndm_family = AF_INET6;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012672 addrsize = 16;
12673 } else {
12674 return -EINVAL;
12675 }
12676
12677 if (bss->br_ifindex == 0) {
12678 wpa_printf(MSG_DEBUG,
12679 "nl80211: bridge must be set to delete an ip neigh");
12680 return -1;
12681 }
12682
12683 if (!drv->rtnl_sk) {
12684 wpa_printf(MSG_DEBUG,
12685 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
12686 return -1;
12687 }
12688
Sunil Ravi99c035e2024-07-12 01:42:03 +000012689 msg = nlmsg_alloc_simple(RTM_DELNEIGH, NLM_F_CREATE);
12690 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012691 return -ENOMEM;
12692
Sunil Ravi99c035e2024-07-12 01:42:03 +000012693 res = -ENOMEM;
12694 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0 ||
12695 nla_put(msg, NDA_DST, addrsize, (void *) ipaddr))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012696 goto errout;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012697
12698 res = nl_send_auto_complete(drv->rtnl_sk, msg);
12699 if (res < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012700 goto errout;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012701
Sunil Ravi99c035e2024-07-12 01:42:03 +000012702 res = nl_wait_for_ack(drv->rtnl_sk);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012703 if (res) {
12704 wpa_printf(MSG_DEBUG,
12705 "nl80211: Deleting bridge ip neigh failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012706 nl_geterror(res));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012707 }
12708errout:
Sunil Ravi99c035e2024-07-12 01:42:03 +000012709 nlmsg_free(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012710 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012711}
12712
12713
12714static int linux_write_system_file(const char *path, unsigned int val)
12715{
12716 char buf[50];
12717 int fd, len;
12718
12719 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
12720 if (os_snprintf_error(sizeof(buf), len))
12721 return -1;
12722
12723 fd = open(path, O_WRONLY);
12724 if (fd < 0)
12725 return -1;
12726
12727 if (write(fd, buf, len) < 0) {
12728 wpa_printf(MSG_DEBUG,
12729 "nl80211: Failed to write Linux system file: %s with the value of %d",
12730 path, val);
12731 close(fd);
12732 return -1;
12733 }
12734 close(fd);
12735
12736 return 0;
12737}
12738
12739
12740static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
12741{
12742 switch (attr) {
12743 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -070012744 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012745 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
12746 return "hairpin_mode";
Sunil Ravi036cec52023-03-29 11:35:17 -070012747 case DRV_BR_PORT_ATTR_MCAST2UCAST:
12748 return "multicast_to_unicast";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012749 }
12750
12751 return NULL;
12752}
12753
12754
12755static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
12756 unsigned int val)
12757{
12758 struct i802_bss *bss = priv;
12759 char path[128];
12760 const char *attr_txt;
12761
12762 attr_txt = drv_br_port_attr_str(attr);
12763 if (attr_txt == NULL)
12764 return -EINVAL;
12765
12766 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
12767 bss->ifname, attr_txt);
12768
12769 if (linux_write_system_file(path, val))
12770 return -1;
12771
12772 return 0;
12773}
12774
12775
12776static const char * drv_br_net_param_str(enum drv_br_net_param param)
12777{
12778 switch (param) {
12779 case DRV_BR_NET_PARAM_GARP_ACCEPT:
12780 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -070012781 default:
12782 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012783 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012784}
12785
12786
12787static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
12788 unsigned int val)
12789{
12790 struct i802_bss *bss = priv;
12791 char path[128];
12792 const char *param_txt;
12793 int ip_version = 4;
12794
Dmitry Shmidt83474442015-04-15 13:47:09 -070012795 if (param == DRV_BR_MULTICAST_SNOOPING) {
12796 os_snprintf(path, sizeof(path),
12797 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
12798 bss->brname);
12799 goto set_val;
12800 }
12801
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012802 param_txt = drv_br_net_param_str(param);
12803 if (param_txt == NULL)
12804 return -EINVAL;
12805
12806 switch (param) {
12807 case DRV_BR_NET_PARAM_GARP_ACCEPT:
12808 ip_version = 4;
12809 break;
12810 default:
12811 return -EINVAL;
12812 }
12813
12814 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
12815 ip_version, bss->brname, param_txt);
12816
Dmitry Shmidt83474442015-04-15 13:47:09 -070012817set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012818 if (linux_write_system_file(path, val))
12819 return -1;
12820
12821 return 0;
12822}
12823
12824
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012825#ifdef CONFIG_DRIVER_NL80211_QCA
12826
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012827static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
12828{
12829 switch (hw_mode) {
12830 case HOSTAPD_MODE_IEEE80211B:
12831 return QCA_ACS_MODE_IEEE80211B;
12832 case HOSTAPD_MODE_IEEE80211G:
12833 return QCA_ACS_MODE_IEEE80211G;
12834 case HOSTAPD_MODE_IEEE80211A:
12835 return QCA_ACS_MODE_IEEE80211A;
12836 case HOSTAPD_MODE_IEEE80211AD:
12837 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -070012838 case HOSTAPD_MODE_IEEE80211ANY:
12839 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012840 default:
12841 return -1;
12842 }
12843}
12844
12845
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080012846static int add_acs_ch_list(struct nl_msg *msg, const int *freq_list)
12847{
12848 int num_channels = 0, num_freqs;
12849 u8 *ch_list;
12850 enum hostapd_hw_mode hw_mode;
12851 int ret = 0;
12852 int i;
12853
12854 if (!freq_list)
12855 return 0;
12856
12857 num_freqs = int_array_len(freq_list);
12858 ch_list = os_malloc(sizeof(u8) * num_freqs);
12859 if (!ch_list)
12860 return -1;
12861
12862 for (i = 0; i < num_freqs; i++) {
12863 const int freq = freq_list[i];
12864
12865 if (freq == 0)
12866 break;
12867 /* Send 2.4 GHz and 5 GHz channels with
12868 * QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST to maintain backwards
12869 * compatibility.
12870 */
12871 if (!(freq >= 2412 && freq <= 2484) &&
Hai Shalomc1a21442022-02-04 13:43:00 -080012872 !(freq >= 5180 && freq <= 5900) &&
12873 !(freq >= 5945 && freq <= 7115))
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080012874 continue;
12875 hw_mode = ieee80211_freq_to_chan(freq, &ch_list[num_channels]);
12876 if (hw_mode != NUM_HOSTAPD_MODES)
12877 num_channels++;
12878 }
12879
12880 if (num_channels)
12881 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST,
12882 num_channels, ch_list);
12883
12884 os_free(ch_list);
12885 return ret;
12886}
12887
12888
Sunil Ravic0f5d412024-09-11 22:12:49 +000012889static int add_freq_list(struct nl_msg *msg, int attr, const int *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012890{
12891 int i, len, ret;
12892 u32 *freqs;
12893
12894 if (!freq_list)
12895 return 0;
12896 len = int_array_len(freq_list);
12897 freqs = os_malloc(sizeof(u32) * len);
12898 if (!freqs)
12899 return -1;
12900 for (i = 0; i < len; i++)
12901 freqs[i] = freq_list[i];
Sunil Ravic0f5d412024-09-11 22:12:49 +000012902 ret = nla_put(msg, attr, sizeof(u32) * len, freqs);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012903 os_free(freqs);
12904 return ret;
12905}
12906
12907
Hai Shalomc1a21442022-02-04 13:43:00 -080012908static int nl80211_qca_do_acs(struct wpa_driver_nl80211_data *drv,
12909 struct drv_acs_params *params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012910{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012911 struct nl_msg *msg;
12912 struct nlattr *data;
12913 int ret;
12914 int mode;
12915
12916 mode = hw_mode_to_qca_acs(params->hw_mode);
12917 if (mode < 0)
12918 return -1;
12919
12920 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
12921 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12922 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12923 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
12924 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12925 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
12926 (params->ht_enabled &&
12927 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
12928 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012929 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
12930 (params->vht_enabled &&
12931 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
Sunil Ravia04bd252022-05-02 22:54:18 -070012932 (params->eht_enabled &&
12933 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_EHT_ENABLED)) ||
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012934 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
12935 params->ch_width) ||
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080012936 add_acs_ch_list(msg, params->freq_list) ||
Sunil Ravic0f5d412024-09-11 22:12:49 +000012937 add_freq_list(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
12938 params->freq_list) ||
Hai Shalomfdcde762020-04-02 11:19:20 -070012939 (params->edmg_enabled &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012940 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_EDMG_ENABLED)) ||
12941 (params->link_id != NL80211_DRV_LINK_ID_NA &&
12942 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_LINK_ID,
12943 params->link_id))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012944 nlmsg_free(msg);
12945 return -ENOBUFS;
12946 }
12947 nla_nest_end(msg, data);
12948
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012949 wpa_printf(MSG_DEBUG,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012950 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d EHT: %d BW: %d EDMG: %d, link_id: %d",
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012951 params->hw_mode, params->ht_enabled, params->ht40_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -070012952 params->vht_enabled, params->eht_enabled, params->ch_width,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012953 params->edmg_enabled, params->link_id);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012954
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012955 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012956 if (ret) {
12957 wpa_printf(MSG_DEBUG,
12958 "nl80211: Failed to invoke driver ACS function: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012959 strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012960 }
12961 return ret;
12962}
12963
12964
Hai Shalom60840252021-02-19 19:02:11 -080012965static int nl80211_set_band(void *priv, u32 band_mask)
Ravi Joshie6ccb162015-07-16 17:45:41 -070012966{
12967 struct i802_bss *bss = priv;
12968 struct wpa_driver_nl80211_data *drv = bss->drv;
12969 struct nl_msg *msg;
12970 struct nlattr *data;
12971 int ret;
Hai Shalom60840252021-02-19 19:02:11 -080012972 enum qca_set_band qca_band_value;
12973 u32 qca_band_mask = QCA_SETBAND_AUTO;
Ravi Joshie6ccb162015-07-16 17:45:41 -070012974
Hai Shalom60840252021-02-19 19:02:11 -080012975 if (!drv->setband_vendor_cmd_avail ||
12976 (band_mask > (WPA_SETBAND_2G | WPA_SETBAND_5G | WPA_SETBAND_6G)))
Ravi Joshie6ccb162015-07-16 17:45:41 -070012977 return -1;
12978
Hai Shalom60840252021-02-19 19:02:11 -080012979 if (band_mask & WPA_SETBAND_5G)
12980 qca_band_mask |= QCA_SETBAND_5G;
12981 if (band_mask & WPA_SETBAND_2G)
12982 qca_band_mask |= QCA_SETBAND_2G;
12983 if (band_mask & WPA_SETBAND_6G)
12984 qca_band_mask |= QCA_SETBAND_6G;
12985
12986 /*
12987 * QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE is a legacy interface hence make
12988 * it suite to its values (AUTO/5G/2G) for backwards compatibility.
12989 */
12990 qca_band_value = ((qca_band_mask & QCA_SETBAND_5G) &&
12991 (qca_band_mask & QCA_SETBAND_2G)) ?
12992 QCA_SETBAND_AUTO :
12993 qca_band_mask & ~QCA_SETBAND_6G;
12994
12995 wpa_printf(MSG_DEBUG,
12996 "nl80211: QCA_BAND_MASK = 0x%x, QCA_BAND_VALUE = %d",
12997 qca_band_mask, qca_band_value);
Ravi Joshie6ccb162015-07-16 17:45:41 -070012998
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000012999 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Ravi Joshie6ccb162015-07-16 17:45:41 -070013000 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13001 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13002 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
13003 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
Hai Shalom60840252021-02-19 19:02:11 -080013004 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE,
13005 qca_band_value) ||
13006 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_MASK,
13007 qca_band_mask)) {
Ravi Joshie6ccb162015-07-16 17:45:41 -070013008 nlmsg_free(msg);
13009 return -ENOBUFS;
13010 }
13011 nla_nest_end(msg, data);
13012
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013013 ret = send_and_recv_cmd(drv, msg);
Ravi Joshie6ccb162015-07-16 17:45:41 -070013014 if (ret) {
13015 wpa_printf(MSG_DEBUG,
13016 "nl80211: Driver setband function failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070013017 strerror(-ret));
Ravi Joshie6ccb162015-07-16 17:45:41 -070013018 }
13019 return ret;
13020}
13021
13022
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013023struct nl80211_pcl {
13024 unsigned int num;
Sunil8cd6f4d2022-06-28 18:40:46 +000013025 struct weighted_pcl *freq_list;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013026};
13027
Sunil8cd6f4d2022-06-28 18:40:46 +000013028static void get_pcl_attr_values(struct weighted_pcl *wpcl, struct nlattr *nl[])
13029{
13030 if (nl[QCA_WLAN_VENDOR_ATTR_PCL_FREQ])
13031 wpcl->freq = nla_get_u32(nl[QCA_WLAN_VENDOR_ATTR_PCL_FREQ]);
13032 if (nl[QCA_WLAN_VENDOR_ATTR_PCL_WEIGHT])
13033 wpcl->weight = nla_get_u8(nl[QCA_WLAN_VENDOR_ATTR_PCL_WEIGHT]);
13034 if (nl[QCA_WLAN_VENDOR_ATTR_PCL_FLAG]) {
13035 u32 flags = nla_get_u32(nl[QCA_WLAN_VENDOR_ATTR_PCL_FLAG]);
13036
13037 wpcl->flag = 0;
13038 if (flags & BIT(0))
13039 wpcl->flag |= WEIGHTED_PCL_GO;
13040 if (flags & BIT(1))
13041 wpcl->flag |= WEIGHTED_PCL_CLI;
13042 if (flags & BIT(2))
13043 wpcl->flag |= WEIGHTED_PCL_MUST_CONSIDER;
13044 if (flags & BIT(3))
13045 wpcl->flag |= WEIGHTED_PCL_EXCLUDE;
13046 } else {
13047 wpcl->flag = WEIGHTED_PCL_GO | WEIGHTED_PCL_CLI;
13048 }
13049}
13050
13051
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013052static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
13053{
13054 struct nlattr *tb[NL80211_ATTR_MAX + 1];
13055 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
13056 struct nl80211_pcl *param = arg;
13057 struct nlattr *nl_vend, *attr;
13058 enum qca_iface_type iface_type;
13059 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
Sunil8cd6f4d2022-06-28 18:40:46 +000013060 struct nlattr *nl_pcl[QCA_WLAN_VENDOR_ATTR_PCL_MAX + 1];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013061 unsigned int num, max_num;
13062 u32 *freqs;
13063
13064 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
13065 genlmsg_attrlen(gnlh, 0), NULL);
13066
13067 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
13068 if (!nl_vend)
13069 return NL_SKIP;
13070
13071 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
13072 nla_data(nl_vend), nla_len(nl_vend), NULL);
13073
13074 attr = tb_vendor[
13075 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
13076 if (!attr) {
13077 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
13078 param->num = 0;
13079 return NL_SKIP;
13080 }
13081
13082 iface_type = (enum qca_iface_type) nla_get_u32(attr);
13083 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
13084 iface_type);
13085
Sunil8cd6f4d2022-06-28 18:40:46 +000013086 attr = tb_vendor[
13087 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_WEIGHED_PCL];
13088 if (attr) {
13089 int rem;
13090 struct nlattr *wpcl = attr;
13091 unsigned int i;
13092
13093 num = 0;
13094 nla_for_each_nested(attr, wpcl, rem) {
13095 if (num == param->num)
13096 break; /* not enough room for all entries */
13097 if (nla_parse(nl_pcl, QCA_WLAN_VENDOR_ATTR_PCL_MAX,
13098 nla_data(attr), nla_len(attr), NULL)) {
13099 wpa_printf(MSG_ERROR,
13100 "nl80211: Failed to parse PCL info");
13101 param->num = 0;
13102 return NL_SKIP;
13103 }
13104 get_pcl_attr_values(&param->freq_list[num], nl_pcl);
13105 num++;
13106 }
13107 param->num = num;
13108
13109 /* Sort frequencies based on their weight */
13110 for (i = 0; i < num; i++) {
13111 unsigned int j;
13112
13113 for (j = i + 1; j < num; j++) {
13114 if (param->freq_list[i].weight <
13115 param->freq_list[j].weight) {
13116 struct weighted_pcl tmp;
13117
13118 tmp = param->freq_list[i];
13119 param->freq_list[i] =
13120 param->freq_list[j];
13121 param->freq_list[j] = tmp;
13122 }
13123 }
13124 }
13125 } else if (tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST]) {
13126 wpa_printf(MSG_DEBUG,
13127 "nl80211: Driver does not provide weighted PCL; use the non-weighted variant");
13128 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
13129 /*
13130 * param->num has the maximum number of entries for which there
13131 * is room in the freq_list provided by the caller.
13132 */
13133 freqs = nla_data(attr);
13134 max_num = nla_len(attr) / sizeof(u32);
13135 if (max_num > param->num)
13136 max_num = param->num;
13137 for (num = 0; num < max_num; num++) {
13138 param->freq_list[num].freq = freqs[num];
13139 param->freq_list[num].flag =
13140 WEIGHTED_PCL_GO | WEIGHTED_PCL_CLI;
13141 }
13142 param->num = num;
13143 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013144 wpa_printf(MSG_ERROR,
13145 "nl80211: preferred_freq_list couldn't be found");
13146 param->num = 0;
13147 return NL_SKIP;
13148 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013149 return NL_SKIP;
13150}
13151
13152
13153static int nl80211_get_pref_freq_list(void *priv,
13154 enum wpa_driver_if_type if_type,
13155 unsigned int *num,
Sunil8cd6f4d2022-06-28 18:40:46 +000013156 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013157{
13158 struct i802_bss *bss = priv;
13159 struct wpa_driver_nl80211_data *drv = bss->drv;
13160 struct nl_msg *msg;
13161 int ret;
13162 unsigned int i;
13163 struct nlattr *params;
13164 struct nl80211_pcl param;
13165 enum qca_iface_type iface_type;
13166
13167 if (!drv->get_pref_freq_list)
13168 return -1;
13169
13170 switch (if_type) {
13171 case WPA_IF_STATION:
13172 iface_type = QCA_IFACE_TYPE_STA;
13173 break;
13174 case WPA_IF_AP_BSS:
13175 iface_type = QCA_IFACE_TYPE_AP;
13176 break;
13177 case WPA_IF_P2P_GO:
13178 iface_type = QCA_IFACE_TYPE_P2P_GO;
13179 break;
13180 case WPA_IF_P2P_CLIENT:
13181 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
13182 break;
13183 case WPA_IF_IBSS:
13184 iface_type = QCA_IFACE_TYPE_IBSS;
13185 break;
13186 case WPA_IF_TDLS:
13187 iface_type = QCA_IFACE_TYPE_TDLS;
13188 break;
13189 default:
13190 return -1;
13191 }
13192
13193 param.num = *num;
13194 param.freq_list = freq_list;
13195
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013196 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013197 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
13198 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13199 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13200 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
13201 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
13202 nla_put_u32(msg,
13203 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
13204 iface_type)) {
13205 wpa_printf(MSG_ERROR,
13206 "%s: err in adding vendor_cmd and vendor_data",
13207 __func__);
13208 nlmsg_free(msg);
13209 return -1;
13210 }
13211 nla_nest_end(msg, params);
13212
Sunil8cd6f4d2022-06-28 18:40:46 +000013213 if (freq_list)
13214 os_memset(freq_list, 0, *num * sizeof(struct weighted_pcl));
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013215 ret = send_and_recv_resp(drv, msg, preferred_freq_info_handler, &param);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013216 if (ret) {
13217 wpa_printf(MSG_ERROR,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013218 "%s: err in send_and_recv_resp", __func__);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013219 return ret;
13220 }
13221
13222 *num = param.num;
13223
13224 for (i = 0; i < *num; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +000013225 wpa_printf(MSG_DEBUG,
13226 "nl80211: preferred_channel_list[%d]=%d[%d]:0x%x",
13227 i, freq_list[i].freq, freq_list[i].weight,
13228 freq_list[i].flag);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013229 }
13230
13231 return 0;
13232}
13233
13234
13235static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
13236{
13237 struct i802_bss *bss = priv;
13238 struct wpa_driver_nl80211_data *drv = bss->drv;
13239 struct nl_msg *msg;
13240 int ret;
13241 struct nlattr *params;
13242
13243 if (!drv->set_prob_oper_freq)
13244 return -1;
13245
13246 wpa_printf(MSG_DEBUG,
13247 "nl80211: Set P2P probable operating freq %u for ifindex %d",
13248 freq, bss->ifindex);
13249
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013250 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013251 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13252 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13253 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
13254 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
13255 nla_put_u32(msg,
13256 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
13257 QCA_IFACE_TYPE_P2P_CLIENT) ||
13258 nla_put_u32(msg,
13259 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
13260 freq)) {
13261 wpa_printf(MSG_ERROR,
13262 "%s: err in adding vendor_cmd and vendor_data",
13263 __func__);
13264 nlmsg_free(msg);
13265 return -1;
13266 }
13267 nla_nest_end(msg, params);
13268
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013269 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013270 msg = NULL;
13271 if (ret) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013272 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_cmd",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013273 __func__);
13274 return ret;
13275 }
13276 nlmsg_free(msg);
13277 return 0;
13278}
13279
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070013280
13281static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
13282 unsigned int period, unsigned int interval,
13283 unsigned int count, const u8 *device_types,
13284 size_t dev_types_len,
13285 const u8 *ies, size_t ies_len)
13286{
13287 struct i802_bss *bss = priv;
13288 struct wpa_driver_nl80211_data *drv = bss->drv;
13289 struct nl_msg *msg;
13290 struct nlattr *container;
13291 int ret;
13292
13293 wpa_printf(MSG_DEBUG,
13294 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
13295 freq, period, interval, count);
13296
13297 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
13298 return -1;
13299
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013300 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070013301 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13302 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13303 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
13304 goto fail;
13305
13306 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13307 if (!container)
13308 goto fail;
13309
13310 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
13311 freq) ||
13312 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
13313 period) ||
13314 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
13315 interval) ||
13316 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
13317 count) ||
13318 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
13319 dev_types_len, device_types) ||
13320 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
13321 ies_len, ies))
13322 goto fail;
13323
13324 nla_nest_end(msg, container);
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013325 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070013326 msg = NULL;
13327 if (ret) {
13328 wpa_printf(MSG_DEBUG,
13329 "nl80211: Failed to send P2P Listen offload vendor command");
13330 goto fail;
13331 }
13332
13333 return 0;
13334
13335fail:
13336 nlmsg_free(msg);
13337 return -1;
13338}
13339
13340
13341static int nl80211_p2p_lo_stop(void *priv)
13342{
13343 struct i802_bss *bss = priv;
13344 struct wpa_driver_nl80211_data *drv = bss->drv;
13345 struct nl_msg *msg;
13346
13347 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
13348
13349 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
13350 return -1;
13351
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013352 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070013353 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13354 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13355 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
13356 nlmsg_free(msg);
13357 return -1;
13358 }
13359
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013360 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070013361}
13362
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013363
13364static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
13365{
13366 struct i802_bss *bss = priv;
13367 struct wpa_driver_nl80211_data *drv = bss->drv;
13368 struct nl_msg *msg;
13369 struct nlattr *params;
13370 int ret;
13371 u32 tdls_mode;
13372
13373 wpa_printf(MSG_DEBUG,
13374 "nl80211: Set TDKS mode: tdls_external_control=%d",
13375 tdls_external_control);
13376
13377 if (tdls_external_control == 1)
13378 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
13379 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
13380 else
13381 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
13382
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013383 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013384 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13385 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13386 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
13387 goto fail;
13388
13389 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13390 if (!params)
13391 goto fail;
13392
13393 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
13394 tdls_mode))
13395 goto fail;
13396
13397 nla_nest_end(msg, params);
13398
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013399 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013400 msg = NULL;
13401 if (ret) {
13402 wpa_printf(MSG_ERROR,
13403 "nl80211: Set TDLS mode failed: ret=%d (%s)",
13404 ret, strerror(-ret));
13405 goto fail;
13406 }
13407 return 0;
13408fail:
13409 nlmsg_free(msg);
13410 return -1;
13411}
13412
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013413
13414#ifdef CONFIG_MBO
13415
13416static enum mbo_transition_reject_reason
13417nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
13418{
13419 switch (status) {
13420 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
13421 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
13422 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
13423 return MBO_TRANSITION_REJECT_REASON_DELAY;
13424 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
13425 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
13426 case QCA_STATUS_REJECT_LOW_RSSI:
13427 return MBO_TRANSITION_REJECT_REASON_RSSI;
13428 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
13429 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
13430 case QCA_STATUS_REJECT_UNKNOWN:
13431 default:
13432 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
13433 }
13434}
13435
13436
13437static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
13438 struct nlattr *tb[], int num)
13439{
13440 enum qca_wlan_btm_candidate_status status;
13441 char buf[50];
13442
13443 os_memcpy(candidate->bssid,
13444 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
13445 ETH_ALEN);
13446
13447 status = nla_get_u32(
13448 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
13449 candidate->is_accept = status == QCA_STATUS_ACCEPT;
13450 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
13451
13452 if (candidate->is_accept)
13453 os_snprintf(buf, sizeof(buf), "Accepted");
13454 else
13455 os_snprintf(buf, sizeof(buf),
13456 "Rejected, Reject_reason: %d",
13457 candidate->reject_reason);
13458 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
13459 num, MAC2STR(candidate->bssid), buf);
13460}
13461
13462
13463static int
13464nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
13465{
13466 struct wpa_bss_candidate_info *info = arg;
13467 struct candidate_list *candidate = info->candidates;
13468 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
13469 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
13470 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
13471 static struct nla_policy policy[
13472 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
13473 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
13474 .minlen = ETH_ALEN
13475 },
13476 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
13477 .type = NLA_U32,
13478 },
13479 };
13480 struct nlattr *attr;
13481 int rem;
13482 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
13483 u8 num;
13484
13485 num = info->num; /* number of candidates sent to driver */
13486 info->num = 0;
13487 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
13488 genlmsg_attrlen(gnlh, 0), NULL);
13489
13490 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
13491 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
13492 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
13493 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
13494 return NL_SKIP;
13495
13496 wpa_printf(MSG_DEBUG,
13497 "nl80211: WNM Candidate list received from driver");
13498 nla_for_each_nested(attr,
13499 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
13500 rem) {
13501 if (info->num >= num ||
13502 nla_parse_nested(
13503 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
13504 attr, policy) ||
13505 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
13506 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
13507 break;
13508
13509 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
13510
13511 candidate++;
13512 info->num++;
13513 }
13514
13515 return NL_SKIP;
13516}
13517
13518
13519static struct wpa_bss_candidate_info *
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013520qca_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013521{
13522 struct i802_bss *bss = priv;
13523 struct wpa_driver_nl80211_data *drv = bss->drv;
13524 struct nl_msg *msg;
13525 struct nlattr *attr, *attr1, *attr2;
13526 struct wpa_bss_candidate_info *info;
13527 u8 i;
13528 int ret;
13529 u8 *pos;
13530
13531 if (!drv->fetch_bss_trans_status)
13532 return NULL;
13533
13534 info = os_zalloc(sizeof(*info));
13535 if (!info)
13536 return NULL;
13537 /* Allocate memory for number of candidates sent to driver */
13538 info->candidates = os_calloc(params->n_candidates,
13539 sizeof(*info->candidates));
13540 if (!info->candidates) {
13541 os_free(info);
13542 return NULL;
13543 }
13544
13545 /* Copy the number of candidates being sent to driver. This is used in
13546 * nl80211_get_bss_transition_status_handler() to limit the number of
13547 * candidates that can be populated in info->candidates and will be
13548 * later overwritten with the actual number of candidates received from
13549 * the driver.
13550 */
13551 info->num = params->n_candidates;
13552
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013553 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013554 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13555 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13556 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
13557 goto fail;
13558
13559 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13560 if (!attr)
13561 goto fail;
13562
13563 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
13564 params->mbo_transition_reason))
13565 goto fail;
13566
13567 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
13568 if (!attr1)
13569 goto fail;
13570
13571 wpa_printf(MSG_DEBUG,
13572 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
13573 params->mbo_transition_reason, params->n_candidates);
13574 pos = params->bssid;
13575 for (i = 0; i < params->n_candidates; i++) {
13576 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
13577 MAC2STR(pos));
13578 attr2 = nla_nest_start(msg, i);
13579 if (!attr2 ||
13580 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
13581 ETH_ALEN, pos))
13582 goto fail;
13583 pos += ETH_ALEN;
13584 nla_nest_end(msg, attr2);
13585 }
13586
13587 nla_nest_end(msg, attr1);
13588 nla_nest_end(msg, attr);
13589
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013590 ret = send_and_recv_resp(drv, msg,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013591 nl80211_get_bss_transition_status_handler,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013592 info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013593 msg = NULL;
13594 if (ret) {
13595 wpa_printf(MSG_ERROR,
13596 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
13597 ret, strerror(-ret));
13598 goto fail;
13599 }
13600 return info;
13601
13602fail:
13603 nlmsg_free(msg);
13604 os_free(info->candidates);
13605 os_free(info);
13606 return NULL;
13607}
13608
13609
13610/**
13611 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
13612 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
13613 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
13614 * Returns: 0 on success, -1 on failure
13615 */
13616static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
13617{
13618 struct i802_bss *bss = priv;
13619 struct wpa_driver_nl80211_data *drv = bss->drv;
13620 struct nl_msg *msg;
13621 struct nlattr *attr;
13622 int ret = -1;
13623
13624 if (!drv->set_wifi_conf_vendor_cmd_avail)
13625 return -1;
13626
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013627 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013628 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13629 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13630 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
13631 goto fail;
13632
13633 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13634 if (!attr)
13635 goto fail;
13636
13637 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
13638 ignore_disallow);
13639 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
13640 ignore_disallow))
13641 goto fail;
13642
13643 nla_nest_end(msg, attr);
13644
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013645 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013646 msg = NULL;
13647 if (ret) {
13648 wpa_printf(MSG_ERROR,
13649 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
13650 ret, strerror(-ret));
13651 goto fail;
13652 }
13653
13654fail:
13655 nlmsg_free(msg);
13656 return ret;
13657}
13658
13659#endif /* CONFIG_MBO */
13660
Sunil Ravi89eba102022-09-13 21:04:37 -070013661
13662#ifdef CONFIG_PASN
13663
13664static int nl80211_send_pasn_resp(void *priv, struct pasn_auth *params)
13665{
13666 unsigned int i;
13667 struct i802_bss *bss = priv;
13668 struct nl_msg *msg = NULL;
13669 struct nlattr *nlpeers, *attr, *attr1;
13670 struct wpa_driver_nl80211_data *drv = bss->drv;
13671
13672 wpa_dbg(drv->ctx, MSG_DEBUG,
13673 "nl80211: PASN authentication response for %d entries",
13674 params->num_peers);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013675 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR);
Sunil Ravi89eba102022-09-13 21:04:37 -070013676 if (!msg ||
13677 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13678 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13679 QCA_NL80211_VENDOR_SUBCMD_PASN))
13680 goto fail;
13681
13682 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13683 if (!attr)
13684 goto fail;
13685
13686 nlpeers = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_PASN_PEERS);
13687 if (!nlpeers)
13688 goto fail;
13689
13690 for (i = 0; i < params->num_peers; i++) {
13691 attr1 = nla_nest_start(msg, i);
13692 if (!attr1 ||
13693 nla_put(msg, QCA_WLAN_VENDOR_ATTR_PASN_PEER_SRC_ADDR,
13694 ETH_ALEN, params->peer[i].own_addr) ||
13695 nla_put(msg, QCA_WLAN_VENDOR_ATTR_PASN_PEER_MAC_ADDR,
13696 ETH_ALEN, params->peer[i].peer_addr))
13697 goto fail;
13698
Sunil Ravi77d572f2023-01-17 23:58:31 +000013699 if (params->peer[i].status == 0 &&
13700 nla_put_flag(msg,
13701 QCA_WLAN_VENDOR_ATTR_PASN_PEER_STATUS_SUCCESS))
13702 goto fail;
Sunil Ravi89eba102022-09-13 21:04:37 -070013703
13704 wpa_printf(MSG_DEBUG,
13705 "nl80211: Own address[%u]: " MACSTR
13706 " Peer address[%u]: " MACSTR " Status: %s",
13707 i, MAC2STR(params->peer[i].own_addr), i,
13708 MAC2STR(params->peer[i].peer_addr),
13709 params->peer[i].status ? "Fail" : "Success");
13710 nla_nest_end(msg, attr1);
13711 }
13712
13713 nla_nest_end(msg, nlpeers);
13714 nla_nest_end(msg, attr);
13715
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013716 return send_and_recv_cmd(drv, msg);
Sunil Ravi89eba102022-09-13 21:04:37 -070013717
13718fail:
13719 nlmsg_free(msg);
13720 return -1;
13721}
13722
13723
13724static u32 wpa_ltf_keyseed_len_to_sha_type(size_t len)
13725{
13726 if (len == SHA384_MAC_LEN)
13727 return QCA_WLAN_VENDOR_SHA_384;
13728 if (len == SHA256_MAC_LEN)
13729 return QCA_WLAN_VENDOR_SHA_256;
13730
13731 wpa_printf(MSG_ERROR, "nl80211: Unexpected LTF keyseed len %zu", len);
13732 return (u32) -1;
13733}
13734
13735
13736static int nl80211_set_secure_ranging_ctx(void *priv,
13737 struct secure_ranging_params *params)
13738{
13739 int ret;
13740 u32 suite;
13741 struct nlattr *attr;
13742 struct nl_msg *msg = NULL;
13743 struct i802_bss *bss = priv;
13744 struct wpa_driver_nl80211_data *drv = bss->drv;
13745
13746 /* Configure secure ranging context only to the drivers that support it.
13747 */
13748 if (!drv->secure_ranging_ctx_vendor_cmd_avail)
13749 return 0;
13750
13751 if (!params->peer_addr || !params->own_addr)
13752 return -1;
13753
13754 wpa_dbg(drv->ctx, MSG_DEBUG,
13755 "nl80211: Secure ranging context for " MACSTR,
13756 MAC2STR(params->peer_addr));
13757
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013758 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR);
Sunil Ravi89eba102022-09-13 21:04:37 -070013759 if (!msg ||
13760 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13761 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13762 QCA_NL80211_VENDOR_SUBCMD_SECURE_RANGING_CONTEXT))
13763 goto fail;
13764
13765 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13766 if (!attr)
13767 goto fail;
13768
13769 if (nla_put(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_PEER_MAC_ADDR,
13770 ETH_ALEN, params->peer_addr) ||
13771 nla_put(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_SRC_ADDR,
13772 ETH_ALEN, params->own_addr) ||
13773 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_ACTION,
13774 params->action))
13775 goto fail;
13776
13777 if (params->cipher) {
13778 suite = wpa_cipher_to_cipher_suite(params->cipher);
13779 if (!suite ||
13780 nla_put_u32(msg,
13781 QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_CIPHER,
13782 suite))
13783 goto fail;
13784 }
13785
13786 if (params->tk_len && params->tk) {
13787 if (nla_put(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_TK,
13788 params->tk_len, params->tk))
13789 goto fail;
13790 wpa_hexdump_key(MSG_DEBUG, "nl80211: TK",
13791 params->tk, params->tk_len);
13792 }
13793
13794 if (params->ltf_keyseed_len && params->ltf_keyseed) {
13795 u32 sha_type = wpa_ltf_keyseed_len_to_sha_type(
13796 params->ltf_keyseed_len);
13797
13798 if (sha_type == (u32) -1 ||
13799 nla_put_u32(
13800 msg,
13801 QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_SHA_TYPE,
13802 sha_type) ||
13803 nla_put(msg,
13804 QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_LTF_KEYSEED,
13805 params->ltf_keyseed_len, params->ltf_keyseed))
13806 goto fail;
13807 wpa_hexdump_key(MSG_DEBUG, "nl80211: LTF keyseed",
13808 params->ltf_keyseed, params->ltf_keyseed_len);
13809 }
13810 nla_nest_end(msg, attr);
13811
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013812 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi89eba102022-09-13 21:04:37 -070013813 if (ret)
13814 wpa_printf(MSG_DEBUG,
13815 "nl80211: Set secure ranging context failed: ret=%d (%s)",
13816 ret, strerror(-ret));
13817 return ret;
13818fail:
13819 nlmsg_free(msg);
13820 return -1;
13821}
13822
13823#endif /* CONFIG_PASN */
13824
Sunil Ravic0f5d412024-09-11 22:12:49 +000013825#ifdef CONFIG_NAN_USD
13826
13827static int nl80211_nan_flush(void *priv)
13828{
13829 struct i802_bss *bss = priv;
13830 struct wpa_driver_nl80211_data *drv = bss->drv;
13831 struct nl_msg *msg;
13832 struct nlattr *container;
13833 int ret;
13834
13835 wpa_printf(MSG_DEBUG, "nl80211: NAN USD flush");
13836
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013837 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR);
Sunil Ravic0f5d412024-09-11 22:12:49 +000013838 if (!msg ||
13839 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13840 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13841 QCA_NL80211_VENDOR_SUBCMD_USD))
13842 goto fail;
13843
13844 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13845 if (!container ||
13846 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_OP_TYPE,
13847 QCA_WLAN_VENDOR_USD_OP_TYPE_FLUSH))
13848 goto fail;
13849
13850 nla_nest_end(msg, container);
13851
13852 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013853 if (ret)
Sunil Ravic0f5d412024-09-11 22:12:49 +000013854 wpa_printf(MSG_ERROR,
13855 "nl80211: Failed to send NAN USD flush");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013856 return ret;
Sunil Ravic0f5d412024-09-11 22:12:49 +000013857
13858fail:
13859 nlmsg_free(msg);
13860 return -1;
13861}
13862
13863
13864static int nl80211_nan_publish(void *priv, const u8 *src, int publish_id,
13865 const char *service_name, const u8 *service_id,
13866 enum nan_service_protocol_type srv_proto_type,
13867 const struct wpabuf *ssi,
13868 const struct wpabuf *elems,
13869 struct nan_publish_params *params)
13870{
13871 struct i802_bss *bss = priv;
13872 struct wpa_driver_nl80211_data *drv = bss->drv;
13873 struct nl_msg *msg;
13874 struct nlattr *container, *attr;
13875 int ret;
13876
13877 wpa_printf(MSG_DEBUG,
13878 "nl80211: Start NAN USD publish: default freq=%u, ttl=%u",
13879 params->freq, params->ttl);
13880 wpa_hexdump_buf(MSG_MSGDUMP, "nl80211: USD elements", elems);
13881
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013882 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR);
Sunil Ravic0f5d412024-09-11 22:12:49 +000013883 if (!msg ||
13884 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13885 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13886 QCA_NL80211_VENDOR_SUBCMD_USD))
13887 goto fail;
13888
13889 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13890 if (!container)
13891 goto fail;
13892
13893 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_OP_TYPE,
13894 QCA_WLAN_VENDOR_USD_OP_TYPE_PUBLISH) ||
13895 nla_put(msg, QCA_WLAN_VENDOR_ATTR_USD_SRC_ADDR, ETH_ALEN, src) ||
13896 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_INSTANCE_ID, publish_id) ||
13897 nla_put(msg, QCA_WLAN_VENDOR_ATTR_USD_SERVICE_ID,
13898 NAN_SERVICE_ID_LEN, service_id) ||
13899 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_SERVICE_PROTOCOL_TYPE,
13900 srv_proto_type) ||
13901 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_USD_TTL, params->ttl) ||
13902 nla_put(msg, QCA_WLAN_VENDOR_ATTR_USD_ELEMENT_CONTAINER,
13903 wpabuf_len(elems), wpabuf_head(elems)) ||
13904 (ssi && nla_put(msg, QCA_WLAN_VENDOR_ATTR_USD_SSI,
13905 wpabuf_len(ssi), wpabuf_head(ssi))))
13906 goto fail;
13907
13908 attr = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_USD_CHAN_CONFIG);
13909 if (!attr)
13910 goto fail;
13911 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_USD_CHAN_CONFIG_DEFAULT_FREQ,
13912 params->freq) ||
13913 add_freq_list(msg, QCA_WLAN_VENDOR_ATTR_USD_CHAN_CONFIG_FREQ_LIST,
13914 params->freq_list))
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013915 goto fail;
Sunil Ravic0f5d412024-09-11 22:12:49 +000013916 nla_nest_end(msg, attr);
13917
13918 nla_nest_end(msg, container);
13919 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013920 if (ret)
Sunil Ravic0f5d412024-09-11 22:12:49 +000013921 wpa_printf(MSG_ERROR,
13922 "nl80211: Failed to send NAN USD publish");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013923 return ret;
Sunil Ravic0f5d412024-09-11 22:12:49 +000013924
13925fail:
13926 nlmsg_free(msg);
13927 return -1;
13928}
13929
13930
13931static int nl80211_nan_cancel_publish(void *priv, int publish_id)
13932{
13933 struct i802_bss *bss = priv;
13934 struct wpa_driver_nl80211_data *drv = bss->drv;
13935 struct nl_msg *msg;
13936 struct nlattr *container;
13937 int ret;
13938
13939 wpa_printf(MSG_DEBUG, "nl80211: NAN USD cancel publish");
13940
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013941 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR);
Sunil Ravic0f5d412024-09-11 22:12:49 +000013942 if (!msg ||
13943 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13944 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13945 QCA_NL80211_VENDOR_SUBCMD_USD))
13946 goto fail;
13947
13948 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13949 if (!container)
13950 goto fail;
13951
13952 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_OP_TYPE,
13953 QCA_WLAN_VENDOR_USD_OP_TYPE_CANCEL_PUBLISH) ||
13954 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_INSTANCE_ID,
13955 publish_id))
13956 goto fail;
13957
13958 nla_nest_end(msg, container);
13959
13960 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013961 if (ret)
Sunil Ravic0f5d412024-09-11 22:12:49 +000013962 wpa_printf(MSG_ERROR,
13963 "nl80211: Failed to send NAN USD cancel publish");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013964 return ret;
Sunil Ravic0f5d412024-09-11 22:12:49 +000013965
13966fail:
13967 nlmsg_free(msg);
13968 return -1;
13969}
13970
13971
13972static int nl80211_nan_update_publish(void *priv, int publish_id,
13973 const struct wpabuf *ssi)
13974{
13975 struct i802_bss *bss = priv;
13976 struct wpa_driver_nl80211_data *drv = bss->drv;
13977 struct nl_msg *msg;
13978 struct nlattr *container;
13979 int ret;
13980
13981 wpa_printf(MSG_DEBUG, "nl80211: NAN USD update publish: id=%d",
13982 publish_id);
13983
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000013984 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR);
Sunil Ravic0f5d412024-09-11 22:12:49 +000013985 if (!msg ||
13986 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13987 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13988 QCA_NL80211_VENDOR_SUBCMD_USD))
13989 goto fail;
13990
13991 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13992 if (!container)
13993 goto fail;
13994
13995 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_OP_TYPE,
13996 QCA_WLAN_VENDOR_USD_OP_TYPE_UPDATE_PUBLISH) ||
13997 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_INSTANCE_ID,
13998 publish_id) ||
13999 (ssi && nla_put(msg, QCA_WLAN_VENDOR_ATTR_USD_SSI,
14000 wpabuf_len(ssi), wpabuf_head(ssi))))
14001 goto fail;
14002
14003 nla_nest_end(msg, container);
14004 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014005 if (ret)
Sunil Ravic0f5d412024-09-11 22:12:49 +000014006 wpa_printf(MSG_ERROR,
14007 "nl80211: Failed to send NAN USD update publish");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014008 return ret;
Sunil Ravic0f5d412024-09-11 22:12:49 +000014009
14010fail:
14011 nlmsg_free(msg);
14012 return -1;
14013}
14014
14015
14016static int nl80211_nan_subscribe(void *priv, const u8 *src, int subscribe_id,
14017 const char *service_name, const u8 *service_id,
14018 enum nan_service_protocol_type srv_proto_type,
14019 const struct wpabuf *ssi,
14020 const struct wpabuf *elems,
14021 struct nan_subscribe_params *params)
14022{
14023 struct i802_bss *bss = priv;
14024 struct wpa_driver_nl80211_data *drv = bss->drv;
14025 struct nl_msg *msg;
14026 struct nlattr *container, *attr;
14027 int ret;
14028
14029 wpa_printf(MSG_DEBUG,
14030 "nl80211: Start NAN USD subscribe: freq=%u, ttl=%u",
14031 params->freq, params->ttl);
14032 wpa_hexdump_buf(MSG_MSGDUMP, "nl80211: USD elements", elems);
14033
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014034 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR);
Sunil Ravic0f5d412024-09-11 22:12:49 +000014035 if (!msg ||
14036 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
14037 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
14038 QCA_NL80211_VENDOR_SUBCMD_USD))
14039 goto fail;
14040
14041 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
14042 if (!container)
14043 goto fail;
14044
14045 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_OP_TYPE,
14046 QCA_WLAN_VENDOR_USD_OP_TYPE_SUBSCRIBE) ||
14047 nla_put(msg, QCA_WLAN_VENDOR_ATTR_USD_SRC_ADDR, ETH_ALEN, src) ||
14048 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_INSTANCE_ID,
14049 subscribe_id) ||
14050 nla_put(msg, QCA_WLAN_VENDOR_ATTR_USD_SERVICE_ID,
14051 NAN_SERVICE_ID_LEN, service_id) ||
14052 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_SERVICE_PROTOCOL_TYPE,
14053 srv_proto_type) ||
14054 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_USD_TTL, params->ttl) ||
14055 nla_put(msg, QCA_WLAN_VENDOR_ATTR_USD_ELEMENT_CONTAINER,
14056 wpabuf_len(elems), wpabuf_head(elems)) ||
14057 (ssi && nla_put(msg, QCA_WLAN_VENDOR_ATTR_USD_SSI,
14058 wpabuf_len(ssi), wpabuf_head(ssi))))
14059 goto fail;
14060
14061 attr = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_USD_CHAN_CONFIG);
14062 if (!attr ||
14063 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_USD_CHAN_CONFIG_DEFAULT_FREQ,
14064 params->freq) ||
14065 add_freq_list(msg, QCA_WLAN_VENDOR_ATTR_USD_CHAN_CONFIG_FREQ_LIST,
14066 params->freq_list))
14067 goto fail;
14068 nla_nest_end(msg, attr);
14069
14070 nla_nest_end(msg, container);
14071 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014072 if (ret)
Sunil Ravic0f5d412024-09-11 22:12:49 +000014073 wpa_printf(MSG_ERROR,
14074 "nl80211: Failed to send NAN USD subscribe");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014075 return ret;
Sunil Ravic0f5d412024-09-11 22:12:49 +000014076
14077fail:
14078 nlmsg_free(msg);
14079 return -1;
14080}
14081
14082
14083static int nl80211_nan_cancel_subscribe(void *priv, int subscribe_id)
14084{
14085 struct i802_bss *bss = priv;
14086 struct wpa_driver_nl80211_data *drv = bss->drv;
14087 struct nl_msg *msg;
14088 struct nlattr *container;
14089 int ret;
14090
14091 wpa_printf(MSG_DEBUG, "nl80211: NAN USD cancel subscribe");
14092
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014093 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_VENDOR);
Sunil Ravic0f5d412024-09-11 22:12:49 +000014094 if (!msg ||
14095 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
14096 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
14097 QCA_NL80211_VENDOR_SUBCMD_USD))
14098 goto fail;
14099
14100 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
14101 if (!container ||
14102 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_OP_TYPE,
14103 QCA_WLAN_VENDOR_USD_OP_TYPE_CANCEL_SUBSCRIBE) ||
14104 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_USD_INSTANCE_ID,
14105 subscribe_id))
14106 goto fail;
14107
14108 nla_nest_end(msg, container);
14109
14110 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014111 if (ret)
Sunil Ravic0f5d412024-09-11 22:12:49 +000014112 wpa_printf(MSG_ERROR,
14113 "nl80211: Failed to send NAN USD cancel subscribe");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014114 return ret;
Sunil Ravic0f5d412024-09-11 22:12:49 +000014115
14116fail:
14117 nlmsg_free(msg);
14118 return -1;
14119}
14120
14121#endif /* CONFIG_NAN_USD */
14122
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080014123#endif /* CONFIG_DRIVER_NL80211_QCA */
14124
Sunil Ravib0ac25f2024-07-12 01:42:03 +000014125
14126#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
14127static int wpa_driver_do_broadcom_acs(struct wpa_driver_nl80211_data *drv,
14128 struct drv_acs_params *params)
14129{
14130 struct nl_msg *msg;
14131 struct nlattr *data;
14132 int freq_list_len;
14133 int ret = -1;
14134
14135 freq_list_len = int_array_len(params->freq_list);
14136 wpa_printf(MSG_DEBUG, "%s: freq_list_len=%d",
14137 __func__, freq_list_len);
14138
14139 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR);
14140 if (!msg ||
14141 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
14142 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
14143 BRCM_VENDOR_SCMD_ACS) ||
14144 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
14145 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_HW_MODE, params->hw_mode) ||
14146 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_HT_ENABLED,
14147 params->ht_enabled) ||
14148 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_HT40_ENABLED,
14149 params->ht40_enabled) ||
14150 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_VHT_ENABLED,
14151 params->vht_enabled) ||
14152 nla_put_u16(msg, BRCM_VENDOR_ATTR_ACS_CHWIDTH, params->ch_width) ||
14153 (freq_list_len > 0 &&
14154 nla_put(msg, BRCM_VENDOR_ATTR_ACS_FREQ_LIST,
14155 sizeof(int) * freq_list_len, params->freq_list)))
14156 goto fail;
14157 nla_nest_end(msg, data);
14158
14159 wpa_printf(MSG_DEBUG,
14160 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d",
14161 params->hw_mode, params->ht_enabled, params->ht40_enabled,
14162 params->vht_enabled, params->ch_width);
14163
14164 ret = send_and_recv_cmd(drv, msg);
14165 if (ret) {
14166 wpa_printf(MSG_ERROR,
14167 "nl80211: BRCM Failed to invoke driver ACS function: %s",
14168 strerror(errno));
14169 }
14170
14171 msg = NULL;
14172fail:
14173 nlmsg_free(msg);
14174 return ret;
14175}
14176#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
14177
14178
Hai Shalomc1a21442022-02-04 13:43:00 -080014179static int nl80211_do_acs(void *priv, struct drv_acs_params *params)
14180{
Andy Kuoaba17c12022-04-14 16:05:31 +080014181#if defined(CONFIG_DRIVER_NL80211_QCA) || defined(CONFIG_DRIVER_NL80211_BRCM) \
14182 || defined(CONFIG_DRIVER_NL80211_SYNA)
Hai Shalomc1a21442022-02-04 13:43:00 -080014183 struct i802_bss *bss = priv;
14184 struct wpa_driver_nl80211_data *drv = bss->drv;
Andy Kuoaba17c12022-04-14 16:05:31 +080014185#endif /* CONFIG_DRIVER_NL80211_QCA || CONFIG_DRIVER_NL80211_BRCM \
14186 || defined(CONFIG_DRIVER_NL80211_SYNA) */
Hai Shalomc1a21442022-02-04 13:43:00 -080014187
14188#ifdef CONFIG_DRIVER_NL80211_QCA
14189 if (drv->qca_do_acs)
14190 return nl80211_qca_do_acs(drv, params);
14191#endif /* CONFIG_DRIVER_NL80211_QCA */
14192
Andy Kuoaba17c12022-04-14 16:05:31 +080014193#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Hai Shalomc1a21442022-02-04 13:43:00 -080014194 if (drv->brcm_do_acs)
14195 return wpa_driver_do_broadcom_acs(drv, params);
Andy Kuoaba17c12022-04-14 16:05:31 +080014196#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Hai Shalomc1a21442022-02-04 13:43:00 -080014197
14198 return -1;
14199}
14200
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080014201
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014202#ifdef CONFIG_MBO
14203static struct wpa_bss_candidate_info *
14204nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
14205{
14206#if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS)
14207 /* This only exists for testing purposes, disable unless requested */
14208 if (TEST_FAIL_TAG("simulate")) {
14209 struct wpa_bss_candidate_info *info;
14210 int i;
14211
14212 info = os_zalloc(sizeof(*info));
14213 if (!info)
14214 return NULL;
14215
14216 info->candidates = os_calloc(params->n_candidates,
14217 sizeof(*info->candidates));
14218 if (!info->candidates) {
14219 os_free(info);
14220 return NULL;
14221 }
14222
14223 info->num = params->n_candidates;
14224 for (i = 0; i < params->n_candidates; i++) {
14225 char bssid_str[ETH_ALEN * 3];
14226
14227 os_memcpy(info->candidates[i].bssid,
14228 &params->bssid[i * ETH_ALEN], ETH_ALEN);
14229
14230 os_snprintf(bssid_str, sizeof(bssid_str), MACSTR,
14231 MAC2STR(info->candidates[i].bssid));
14232
14233 if (TEST_FAIL_TAG(bssid_str)) {
14234 info->candidates[i].is_accept = 0;
14235 info->candidates[i].reject_reason =
14236 MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
14237 } else {
14238 info->candidates[i].is_accept = 1;
14239 info->candidates[i].reject_reason = 0;
14240 }
14241 }
14242
14243 return info;
14244 }
14245#endif /* defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) */
14246
14247#ifdef CONFIG_DRIVER_NL80211_QCA
14248 return qca_get_bss_transition_status(priv, params);
14249#else /* CONFIG_DRIVER_NL80211_QCA */
14250 return NULL;
14251#endif /* CONFIG_DRIVER_NL80211_QCA */
14252}
14253#endif /* CONFIG_MBO */
14254
14255
Dmitry Shmidt849734c2016-05-27 09:59:01 -070014256static int nl80211_write_to_file(const char *name, unsigned int val)
14257{
14258 int fd, len;
14259 char tmp[128];
Hai Shalomc3565922019-10-28 11:58:20 -070014260 int ret = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070014261
14262 fd = open(name, O_RDWR);
14263 if (fd < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -070014264 int level;
14265 /*
14266 * Flags may not exist on older kernels, or while we're tearing
14267 * down a disappearing device.
14268 */
14269 if (errno == ENOENT) {
14270 ret = 0;
14271 level = MSG_DEBUG;
14272 } else {
14273 ret = -1;
14274 level = MSG_ERROR;
14275 }
14276 wpa_printf(level, "nl80211: Failed to open %s: %s",
Dmitry Shmidt849734c2016-05-27 09:59:01 -070014277 name, strerror(errno));
Hai Shalomc3565922019-10-28 11:58:20 -070014278 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070014279 }
14280
14281 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
14282 len = write(fd, tmp, len);
Hai Shalomc3565922019-10-28 11:58:20 -070014283 if (len < 0) {
14284 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070014285 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
14286 name, strerror(errno));
Hai Shalomc3565922019-10-28 11:58:20 -070014287 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -070014288 close(fd);
14289
Hai Shalomc3565922019-10-28 11:58:20 -070014290 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070014291}
14292
14293
14294static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
14295{
14296 struct i802_bss *bss = priv;
14297 char path[128];
14298 int ret;
14299
Hai Shalom60840252021-02-19 19:02:11 -080014300 /* P2P-Device has no netdev that can (or should) be configured here */
14301 if (nl80211_get_ifmode(bss) == NL80211_IFTYPE_P2P_DEVICE)
14302 return 0;
14303
Dmitry Shmidt849734c2016-05-27 09:59:01 -070014304 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
14305 filter_flags);
14306
14307 /* Configure filtering of unicast frame encrypted using GTK */
14308 ret = os_snprintf(path, sizeof(path),
14309 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
14310 bss->ifname);
14311 if (os_snprintf_error(sizeof(path), ret))
14312 return -1;
14313
14314 ret = nl80211_write_to_file(path,
14315 !!(filter_flags &
14316 WPA_DATA_FRAME_FILTER_FLAG_GTK));
14317 if (ret) {
14318 wpa_printf(MSG_ERROR,
14319 "nl80211: Failed to set IPv4 unicast in multicast filter");
14320 return ret;
14321 }
14322
14323 os_snprintf(path, sizeof(path),
14324 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
14325 bss->ifname);
14326 ret = nl80211_write_to_file(path,
14327 !!(filter_flags &
14328 WPA_DATA_FRAME_FILTER_FLAG_GTK));
14329
14330 if (ret) {
14331 wpa_printf(MSG_ERROR,
14332 "nl80211: Failed to set IPv6 unicast in multicast filter");
14333 return ret;
14334 }
14335
14336 /* Configure filtering of unicast frame encrypted using GTK */
14337 os_snprintf(path, sizeof(path),
14338 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
14339 bss->ifname);
14340 ret = nl80211_write_to_file(path,
14341 !!(filter_flags &
14342 WPA_DATA_FRAME_FILTER_FLAG_ARP));
14343 if (ret) {
14344 wpa_printf(MSG_ERROR,
14345 "nl80211: Failed set gratuitous ARP filter");
14346 return ret;
14347 }
14348
14349 /* Configure filtering of IPv6 NA frames */
14350 os_snprintf(path, sizeof(path),
14351 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
14352 bss->ifname);
14353 ret = nl80211_write_to_file(path,
14354 !!(filter_flags &
14355 WPA_DATA_FRAME_FILTER_FLAG_NA));
14356 if (ret) {
14357 wpa_printf(MSG_ERROR,
14358 "nl80211: Failed to set unsolicited NA filter");
14359 return ret;
14360 }
14361
14362 return 0;
14363}
14364
14365
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070014366static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
14367 const u8 **ext_capa, const u8 **ext_capa_mask,
14368 unsigned int *ext_capa_len)
14369{
14370 struct i802_bss *bss = priv;
14371 struct wpa_driver_nl80211_data *drv = bss->drv;
14372 enum nl80211_iftype nlmode;
14373 unsigned int i;
14374
14375 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
14376 return -1;
14377
14378 nlmode = wpa_driver_nl80211_if_type(type);
14379
14380 /* By default, use the per-radio values */
14381 *ext_capa = drv->extended_capa;
14382 *ext_capa_mask = drv->extended_capa_mask;
14383 *ext_capa_len = drv->extended_capa_len;
14384
14385 /* Replace the default value if a per-interface type value exists */
Sunil Ravi2a14cf12023-11-21 00:54:38 +000014386 for (i = 0; i < drv->num_iface_capa; i++) {
14387 if (nlmode == drv->iface_capa[i].iftype) {
14388 *ext_capa = drv->iface_capa[i].ext_capa;
14389 *ext_capa_mask = drv->iface_capa[i].ext_capa_mask;
14390 *ext_capa_len = drv->iface_capa[i].ext_capa_len;
14391 break;
14392 }
14393 }
14394
14395 return 0;
14396}
14397
14398
14399static int nl80211_get_mld_capab(void *priv, enum wpa_driver_if_type type,
14400 u16 *eml_capa, u16 *mld_capa_and_ops)
14401{
14402 struct i802_bss *bss = priv;
14403 struct wpa_driver_nl80211_data *drv = bss->drv;
14404 enum nl80211_iftype nlmode;
14405 unsigned int i;
14406
14407 if (!eml_capa || !mld_capa_and_ops)
14408 return -1;
14409
14410 nlmode = wpa_driver_nl80211_if_type(type);
14411
14412 /* By default, set to zero */
14413 *eml_capa = 0;
14414 *mld_capa_and_ops = 0;
14415
14416 /* Replace the default value if a per-interface type value exists */
14417 for (i = 0; i < drv->num_iface_capa; i++) {
14418 if (nlmode == drv->iface_capa[i].iftype) {
14419 *eml_capa = drv->iface_capa[i].eml_capa;
14420 *mld_capa_and_ops =
14421 drv->iface_capa[i].mld_capa_and_ops;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070014422 break;
14423 }
14424 }
14425
14426 return 0;
14427}
14428
14429
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070014430static int nl80211_update_connection_params(
14431 void *priv, struct wpa_driver_associate_params *params,
14432 enum wpa_drv_update_connect_params_mask mask)
14433{
14434 struct i802_bss *bss = priv;
14435 struct wpa_driver_nl80211_data *drv = bss->drv;
14436 struct nl_msg *msg;
14437 int ret = -1;
14438 enum nl80211_auth_type type;
14439
Hai Shalomc3565922019-10-28 11:58:20 -070014440 /* Update Connection Params is intended for drivers that implement
14441 * internal SME and expect these updated connection params from
14442 * wpa_supplicant. Do not send this request for the drivers using
14443 * SME from wpa_supplicant.
14444 */
14445 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME)
14446 return 0;
14447
Vinayak Yadawade62409f2022-01-20 12:32:07 +053014448 /* Handle any connection param update here which might receive kernel handling in future */
Winnie Chen4138eec2022-11-10 16:32:53 +080014449#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawade62409f2022-01-20 12:32:07 +053014450 if (mask & WPA_DRV_UPDATE_TD_POLICY) {
14451 ret = nl80211_set_td_policy(priv, params->td_policy);
14452 if (ret) {
14453 wpa_dbg(drv->ctx, MSG_DEBUG,
14454 "nl80211: Update connect params command failed: ret=%d (%s)",
14455 ret, strerror(-ret));
14456 }
14457 return ret;
14458 }
Winnie Chen4138eec2022-11-10 16:32:53 +080014459#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Vinayak Yadawade62409f2022-01-20 12:32:07 +053014460
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014461 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070014462 if (!msg)
14463 goto fail;
14464
14465 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
14466 drv->ifindex);
14467
14468 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
14469 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
14470 params->wpa_ie))
14471 goto fail;
14472 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
14473 params->wpa_ie_len);
14474 }
14475
14476 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
14477 type = get_nl_auth_type(params->auth_alg);
14478 if (type == NL80211_AUTHTYPE_MAX ||
14479 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
14480 goto fail;
14481 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
14482 }
14483
14484 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
14485 nl80211_put_fils_connect_params(drv, params, msg))
14486 goto fail;
14487
Sunil Ravib0ac25f2024-07-12 01:42:03 +000014488 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070014489 msg = NULL;
14490 if (ret)
14491 wpa_dbg(drv->ctx, MSG_DEBUG,
14492 "nl80211: Update connect params command failed: ret=%d (%s)",
14493 ret, strerror(-ret));
14494
14495fail:
14496 nlmsg_free(msg);
14497 return ret;
14498}
14499
14500
Roshan Pius3a1667e2018-07-03 15:17:14 -070014501static int nl80211_send_external_auth_status(void *priv,
14502 struct external_auth *params)
14503{
14504 struct i802_bss *bss = priv;
14505 struct wpa_driver_nl80211_data *drv = bss->drv;
14506 struct nl_msg *msg = NULL;
14507 int ret = -1;
14508
Hai Shalom5f92bc92019-04-18 11:54:11 -070014509 /* External auth command/status is intended for drivers that implement
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080014510 * internal SME but want to offload authentication processing (e.g.,
14511 * SAE) to hostapd/wpa_supplicant. Do not send the status to drivers
Hai Shalom5f92bc92019-04-18 11:54:11 -070014512 * which do not support AP SME or use wpa_supplicant/hostapd SME.
14513 */
Hai Shalom81f62d82019-07-22 12:10:00 -070014514 if ((is_ap_interface(drv->nlmode) && !bss->drv->device_ap_sme) ||
Hai Shalom5f92bc92019-04-18 11:54:11 -070014515 (drv->capa.flags & WPA_DRIVER_FLAGS_SME))
14516 return -1;
14517
Roshan Pius3a1667e2018-07-03 15:17:14 -070014518 wpa_dbg(drv->ctx, MSG_DEBUG,
14519 "nl80211: External auth status: %u", params->status);
14520
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014521 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_EXTERNAL_AUTH);
Roshan Pius3a1667e2018-07-03 15:17:14 -070014522 if (!msg ||
14523 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
Hai Shalom5f92bc92019-04-18 11:54:11 -070014524 (params->ssid && params->ssid_len &&
14525 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
14526 (params->pmkid &&
14527 nla_put(msg, NL80211_ATTR_PMKID, PMKID_LEN, params->pmkid)) ||
14528 (params->bssid &&
14529 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid)))
Roshan Pius3a1667e2018-07-03 15:17:14 -070014530 goto fail;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000014531 ret = send_and_recv_cmd(drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -070014532 msg = NULL;
14533 if (ret) {
14534 wpa_printf(MSG_DEBUG,
14535 "nl80211: External Auth status update failed: ret=%d (%s)",
14536 ret, strerror(-ret));
14537 goto fail;
14538 }
14539fail:
14540 nlmsg_free(msg);
14541 return ret;
14542}
14543
14544
Hai Shalom74f70d42019-02-11 14:42:39 -080014545static int nl80211_set_4addr_mode(void *priv, const char *bridge_ifname,
14546 int val)
14547{
14548 struct i802_bss *bss = priv;
14549 struct wpa_driver_nl80211_data *drv = bss->drv;
14550 struct nl_msg *msg;
14551 int ret = -ENOBUFS;
14552
14553 wpa_printf(MSG_DEBUG, "nl80211: %s 4addr mode (bridge_ifname: %s)",
14554 val ? "Enable" : "Disable", bridge_ifname);
14555
14556 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
14557 if (!msg || nla_put_u8(msg, NL80211_ATTR_4ADDR, val))
14558 goto fail;
14559
14560 if (bridge_ifname[0] && bss->added_if_into_bridge && !val) {
14561 if (linux_br_del_if(drv->global->ioctl_sock,
14562 bridge_ifname, bss->ifname)) {
14563 wpa_printf(MSG_ERROR,
14564 "nl80211: Failed to remove interface %s from bridge %s",
14565 bss->ifname, bridge_ifname);
14566 return -1;
14567 }
14568 bss->added_if_into_bridge = 0;
14569 }
14570
Sunil Ravib0ac25f2024-07-12 01:42:03 +000014571 ret = send_and_recv_cmd(drv, msg);
Hai Shalom74f70d42019-02-11 14:42:39 -080014572 msg = NULL;
Hai Shalom60840252021-02-19 19:02:11 -080014573 if (ret && val && nl80211_get_4addr(bss) == 1) {
14574 wpa_printf(MSG_DEBUG,
14575 "nl80211: 4addr mode was already enabled");
14576 ret = 0;
14577 }
Hai Shalom74f70d42019-02-11 14:42:39 -080014578 if (!ret) {
14579 if (bridge_ifname[0] && val &&
14580 i802_check_bridge(drv, bss, bridge_ifname, bss->ifname) < 0)
14581 return -1;
14582 return 0;
14583 }
14584
14585fail:
14586 nlmsg_free(msg);
14587 wpa_printf(MSG_ERROR, "nl80211: Failed to enable/disable 4addr");
14588
14589 return ret;
14590}
14591
14592
Hai Shalome21d4e82020-04-29 16:34:06 -070014593#ifdef CONFIG_DPP
14594static int nl80211_dpp_listen(void *priv, bool enable)
14595{
14596 struct i802_bss *bss = priv;
14597 struct wpa_driver_nl80211_data *drv = bss->drv;
14598 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
14599 struct nl_sock *handle;
14600
14601 if (!drv->multicast_registrations || !bss->nl_mgmt)
14602 return 0; /* cannot do more than hope broadcast RX works */
14603
14604 wpa_printf(MSG_DEBUG,
14605 "nl80211: Update DPP Public Action frame registration (%s multicast RX)",
14606 enable ? "enable" : "disable");
14607 handle = (void *) (((intptr_t) bss->nl_mgmt) ^ ELOOP_SOCKET_INVALID);
14608 return nl80211_register_frame(bss, handle, type,
14609 (u8 *) "\x04\x09\x50\x6f\x9a\x1a", 6,
14610 enable);
14611}
14612#endif /* CONFIG_DPP */
14613
Winnie Chen4138eec2022-11-10 16:32:53 +080014614#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawade62409f2022-01-20 12:32:07 +053014615static int nl80211_set_td_policy(void *priv, u32 td_policy)
14616{
14617 struct i802_bss *bss = priv;
14618 struct wpa_driver_nl80211_data *drv = bss->drv;
14619 struct nl_msg *msg;
14620 int ret;
14621 struct nlattr *params;
14622
14623 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
14624 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
14625 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, BRCM_VENDOR_SCMD_SET_TD_POLICY) ||
14626 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
14627 (nla_put_u32(msg, BRCM_ATTR_DRIVER_TD_POLICY, td_policy))) {
14628 nl80211_nlmsg_clear(msg);
14629 nlmsg_free(msg);
14630 return -ENOBUFS;
14631 }
14632 nla_nest_end(msg, params);
14633 wpa_printf(MSG_DEBUG, "nl80211: Transition Disable Policy %d\n", td_policy);
14634
Sunil Ravib0ac25f2024-07-12 01:42:03 +000014635 ret = send_and_recv_cmd(drv, msg);
Vinayak Yadawade62409f2022-01-20 12:32:07 +053014636 if (ret) {
14637 wpa_printf(MSG_DEBUG, "nl80211: Transition Disable setting failed: ret=%d (%s)",
14638 ret, strerror(-ret));
14639 }
14640
14641 return ret;
14642}
Winnie Chen4138eec2022-11-10 16:32:53 +080014643#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Hai Shalome21d4e82020-04-29 16:34:06 -070014644
Sunil Ravi99c035e2024-07-12 01:42:03 +000014645static int nl80211_link_add(void *priv, u8 link_id, const u8 *addr,
14646 void *bss_ctx)
Sunil Ravi036cec52023-03-29 11:35:17 -070014647{
14648 struct i802_bss *bss = priv;
14649 struct wpa_driver_nl80211_data *drv = bss->drv;
14650 struct nl_msg *msg;
Sunil Ravi036cec52023-03-29 11:35:17 -070014651 int ret;
14652
14653 wpa_printf(MSG_DEBUG, "nl80211: MLD: add link_id=%u, addr=" MACSTR,
14654 link_id, MAC2STR(addr));
14655
14656 if (drv->nlmode != NL80211_IFTYPE_AP) {
14657 wpa_printf(MSG_DEBUG,
14658 "nl80211: MLD: cannot add link to iftype=%u",
14659 drv->nlmode);
14660 return -EINVAL;
14661 }
14662
Sunil Ravi99c035e2024-07-12 01:42:03 +000014663 if (link_id >= MAX_NUM_MLD_LINKS) {
14664 wpa_printf(MSG_DEBUG,
14665 "nl80211: invalid link_id=%u", link_id);
Sunil Ravi036cec52023-03-29 11:35:17 -070014666 return -EINVAL;
14667 }
14668
Sunil Ravi99c035e2024-07-12 01:42:03 +000014669 if (bss->valid_links & BIT(link_id)) {
14670 wpa_printf(MSG_DEBUG,
14671 "nl80211: MLD: Link %u already set", link_id);
14672 return -EINVAL;
Sunil Ravi036cec52023-03-29 11:35:17 -070014673 }
14674
Sunil Ravi99c035e2024-07-12 01:42:03 +000014675 if (!bss->valid_links) {
14676 /* Becoming MLD, verify we were not beaconing */
Sunil Ravi036cec52023-03-29 11:35:17 -070014677 if (bss->flink->beacon_set) {
14678 wpa_printf(MSG_DEBUG, "nl80211: BSS already beaconing");
14679 return -EINVAL;
14680 }
Sunil Ravi036cec52023-03-29 11:35:17 -070014681 }
14682
Sunil Ravi7f769292024-07-23 22:21:32 +000014683 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_ADD_LINK);
Sunil Ravi036cec52023-03-29 11:35:17 -070014684 if (!msg ||
14685 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id) ||
14686 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
14687 nlmsg_free(msg);
14688 return -ENOBUFS;
14689 }
14690
Sunil Ravib0ac25f2024-07-12 01:42:03 +000014691 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi036cec52023-03-29 11:35:17 -070014692 if (ret) {
14693 wpa_printf(MSG_DEBUG, "nl80211: add link failed. ret=%d (%s)",
14694 ret, strerror(-ret));
14695 return ret;
14696 }
14697
Sunil Ravi99c035e2024-07-12 01:42:03 +000014698 os_memcpy(bss->links[link_id].addr, addr, ETH_ALEN);
Sunil Ravi036cec52023-03-29 11:35:17 -070014699
Sunil Ravi99c035e2024-07-12 01:42:03 +000014700 /* The new link is the first one, make it the default */
14701 if (!bss->valid_links)
14702 bss->flink = &bss->links[link_id];
Sunil Ravi036cec52023-03-29 11:35:17 -070014703
Sunil Ravi99c035e2024-07-12 01:42:03 +000014704 bss->valid_links |= BIT(link_id);
14705 bss->links[link_id].ctx = bss_ctx;
14706
Sunil Ravi7f769292024-07-23 22:21:32 +000014707 wpa_printf(MSG_DEBUG, "nl80211: MLD: valid_links=0x%04x on %s",
14708 bss->valid_links, bss->ifname);
Sunil Ravic0f5d412024-09-11 22:12:49 +000014709
14710 if (drv->rtnl_sk)
14711 rtnl_neigh_add_fdb_entry(bss, addr, true);
14712
Sunil Ravi036cec52023-03-29 11:35:17 -070014713 return 0;
14714}
14715
14716
Sunil Ravi99c035e2024-07-12 01:42:03 +000014717#ifdef CONFIG_IEEE80211BE
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014718
Sunil Ravi99c035e2024-07-12 01:42:03 +000014719static int wpa_driver_nl80211_link_sta_remove(void *priv, u8 link_id,
14720 const u8 *addr)
14721{
14722 struct i802_bss *bss = priv;
14723 struct wpa_driver_nl80211_data *drv = bss->drv;
14724 struct nl_msg *msg;
14725 int ret;
14726
14727 if (!(bss->valid_links & BIT(link_id)))
14728 return -ENOLINK;
14729
14730 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_REMOVE_LINK_STA)) ||
14731 nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN, addr) ||
14732 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) {
14733 nlmsg_free(msg);
14734 return -ENOBUFS;
14735 }
14736
14737 ret = send_and_recv_cmd(drv, msg);
14738 wpa_printf(MSG_DEBUG,
14739 "nl80211: link_sta_remove -> REMOVE_LINK_STA on link_id %u from MLD STA "
14740 MACSTR ", from %s --> %d (%s)",
14741 link_id, MAC2STR(addr), bss->ifname, ret, strerror(-ret));
14742
14743 return ret;
14744}
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014745
14746
14747static int wpa_driver_get_wiphy_name_handler(struct nl_msg *msg, void *arg)
14748{
14749 struct nlattr *tb[NL80211_ATTR_MAX + 1];
14750 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
14751 struct wpa_driver_nl80211_data *drv = arg;
14752
14753 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
14754 genlmsg_attrlen(gnlh, 0), NULL);
14755
14756 if (!tb[NL80211_ATTR_WIPHY_NAME])
14757 return NL_SKIP;
14758
14759 os_strlcpy(drv->phyname, nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
14760 sizeof(drv->phyname));
14761
14762 return NL_SKIP;
14763}
14764
14765
14766static int wpa_driver_get_phyname(struct wpa_driver_nl80211_data *drv)
14767{
14768 struct nl_msg *msg;
14769 u32 feat, nl_flags;
14770
14771 feat = get_nl80211_protocol_features(drv);
14772 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
14773 nl_flags = NLM_F_DUMP;
14774
14775 if (!(msg = nl80211_cmd_msg(drv->first_bss, nl_flags,
14776 NL80211_CMD_GET_WIPHY)) ||
14777 nla_put_flag(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP)) {
14778 nlmsg_free(msg);
14779 return -1;
14780 }
14781
14782 if (send_and_recv_resp(drv, msg, wpa_driver_get_wiphy_name_handler,
14783 drv))
14784 return -1;
14785
14786 return 0;
14787}
14788
14789
14790static bool
14791wpa_driver_nl80211_name_match(struct wpa_driver_nl80211_data *drv,
14792 struct wpa_driver_nl80211_data **match_drv)
14793{
14794 struct wpa_driver_nl80211_data *drv2;
14795
14796 dl_list_for_each(drv2, &drv->global->interfaces,
14797 struct wpa_driver_nl80211_data, list) {
14798 if (os_strcmp(drv2->phyname, drv->phyname) == 0) {
14799 if (match_drv)
14800 *match_drv = drv2;
14801 return true;
14802 }
14803 }
14804
14805 return false;
14806}
14807
14808
14809static bool wpa_driver_nl80211_can_share_drv(void *ctx,
14810 struct wpa_init_params *params,
14811 void **hapd)
14812{
14813 struct wpa_driver_nl80211_data *drv, *match_drv = NULL;
14814 struct i802_bss *bss;
14815 bool ret = false;
14816
14817 if (!params->global_priv)
14818 return false;
14819
14820 /* Create a temporary drv to read the phyname */
14821 drv = os_zalloc(sizeof(*drv));
14822 if (!drv)
14823 return false;
14824 drv->global = params->global_priv;
14825 drv->ctx = ctx;
14826
14827 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
14828 if (!drv->first_bss) {
14829 os_free(drv);
14830 return false;
14831 }
14832
14833 bss = drv->first_bss;
14834 bss->drv = drv;
14835 bss->ctx = ctx;
14836
14837 os_strlcpy(bss->ifname, params->ifname, sizeof(bss->ifname));
14838
14839 if (nl80211_init_bss(bss))
14840 goto free_all;
14841
14842 drv->ifindex = if_nametoindex(bss->ifname);
14843 bss->ifindex = drv->ifindex;
14844
14845 if (wpa_driver_get_phyname(drv) ||
14846 !wpa_driver_nl80211_name_match(drv, &match_drv) ||
14847 !match_drv)
14848 goto free_all;
14849
14850 wpa_printf(MSG_DEBUG, "nl80211: Driver for phy %s already exist",
14851 match_drv->phyname);
14852
14853 *hapd = match_drv->first_bss->ctx;
14854 ret = true;
14855
14856free_all:
14857 nl80211_destroy_bss(bss);
14858 os_free(bss);
14859 os_free(drv);
14860 return ret;
14861}
14862
Sunil Ravi99c035e2024-07-12 01:42:03 +000014863#endif /* CONFIG_IEEE80211BE */
14864
14865
Hai Shalomc1a21442022-02-04 13:43:00 -080014866#ifdef CONFIG_TESTING_OPTIONS
14867
14868static int testing_nl80211_register_frame(void *priv, u16 type,
14869 const u8 *match, size_t match_len,
14870 bool multicast)
14871{
14872 struct i802_bss *bss = priv;
14873 struct nl_sock *handle;
14874
14875 if (!bss->nl_mgmt)
14876 return -1;
14877 handle = (void *) (((intptr_t) bss->nl_mgmt) ^ ELOOP_SOCKET_INVALID);
14878 return nl80211_register_frame(bss, handle, type, match, match_len,
14879 multicast);
14880}
14881
14882
14883static int testing_nl80211_radio_disable(void *priv, int disabled)
14884{
14885 struct i802_bss *bss = priv;
14886 struct wpa_driver_nl80211_data *drv = bss->drv;
14887
14888 /* For now, this is supported only partially in station mode with
14889 * SME-in-wpa_supplicant case where the NL80211_ATTR_LOCAL_STATE_CHANGE
14890 * attribute can be used to avoid sending out the Deauthentication frame
14891 * to the currently associated AP. */
14892
14893 if (!disabled)
14894 return 0;
14895
14896 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
14897 return -1;
14898
14899 if (!drv->associated)
14900 return 0;
14901
14902 return wpa_driver_nl80211_mlme(drv, drv->bssid,
14903 NL80211_CMD_DEAUTHENTICATE,
14904 WLAN_REASON_PREV_AUTH_NOT_VALID, 1,
14905 drv->first_bss);
14906}
14907
14908#endif /* CONFIG_TESTING_OPTIONS */
14909
14910
Sunil Ravic0f5d412024-09-11 22:12:49 +000014911static struct hostapd_multi_hw_info *
14912wpa_driver_get_multi_hw_info(void *priv, unsigned int *num_multi_hws)
14913{
14914 struct i802_bss *bss = priv;
14915
14916 return nl80211_get_multi_hw_info(bss, num_multi_hws);
14917}
14918
14919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014920const struct wpa_driver_ops wpa_driver_nl80211_ops = {
14921 .name = "nl80211",
14922 .desc = "Linux nl80211/cfg80211",
14923 .get_bssid = wpa_driver_nl80211_get_bssid,
14924 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014925 .set_key = driver_nl80211_set_key,
14926 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014927 .sched_scan = wpa_driver_nl80211_sched_scan,
14928 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Sunil Ravi99c035e2024-07-12 01:42:03 +000014929 .get_scan_results = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080014930 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014931 .deauthenticate = driver_nl80211_deauthenticate,
14932 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014933 .associate = wpa_driver_nl80211_associate,
14934 .global_init = nl80211_global_init,
14935 .global_deinit = nl80211_global_deinit,
14936 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014937 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014938 .get_capa = wpa_driver_nl80211_get_capa,
14939 .set_operstate = wpa_driver_nl80211_set_operstate,
14940 .set_supp_port = wpa_driver_nl80211_set_supp_port,
14941 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080014942 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014943 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070014944 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014945 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014946 .if_remove = driver_nl80211_if_remove,
14947 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080014948 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014949 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014950 .sta_remove = driver_nl80211_sta_remove,
Hai Shalomfdcde762020-04-02 11:19:20 -070014951 .tx_control_port = nl80211_tx_control_port,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014952 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
14953 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Hai Shalom81f62d82019-07-22 12:10:00 -070014954 .sta_set_airtime_weight = driver_nl80211_sta_set_airtime_weight,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014955 .hapd_init = i802_init,
14956 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070014957 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014958 .get_seqnum = i802_get_seqnum,
14959 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014960 .get_inact_sec = i802_get_inact_sec,
14961 .sta_clear_stats = i802_sta_clear_stats,
14962 .set_rts = i802_set_rts,
14963 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014964 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014965 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014966 .sta_deauth = i802_sta_deauth,
14967 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014968 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014969 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014970 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014971 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
14972 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
14973 .cancel_remain_on_channel =
14974 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014975 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014976 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070014977 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014978 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014979 .signal_monitor = nl80211_signal_monitor,
14980 .signal_poll = nl80211_signal_poll,
Sunil Ravi89eba102022-09-13 21:04:37 -070014981 .mlo_signal_poll = nl80211_mlo_signal_poll,
Hai Shalom74f70d42019-02-11 14:42:39 -080014982 .channel_info = nl80211_channel_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014983 .set_param = nl80211_set_param,
14984 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070014985 .add_pmkid = nl80211_add_pmkid,
14986 .remove_pmkid = nl80211_remove_pmkid,
14987 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014988 .set_rekey_info = nl80211_set_rekey_info,
14989 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014990 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070014991 .start_dfs_cac = nl80211_start_radar_detection,
14992 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014993#ifdef CONFIG_TDLS
14994 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
14995 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080014996 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
14997 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014998#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070014999 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Hai Shalom81f62d82019-07-22 12:10:00 -070015000 .update_dh_ie = nl80211_update_dh_ie,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070015001 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070015002 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070015003 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080015004 .switch_channel = nl80211_switch_channel,
Sunil Ravia04bd252022-05-02 22:54:18 -070015005#ifdef CONFIG_IEEE80211AX
15006 .switch_color = nl80211_switch_color,
15007#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080015008#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070015009 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080015010 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070015011 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080015012#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070015013#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -070015014#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070015015 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -070015016#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080015017#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080015018 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080015019 .set_qos_map = nl80211_set_qos_map,
Hai Shalomfdcde762020-04-02 11:19:20 -070015020 .get_wowlan = nl80211_get_wowlan,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070015021 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070015022 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080015023#ifdef CONFIG_MESH
15024 .init_mesh = wpa_driver_nl80211_init_mesh,
15025 .join_mesh = wpa_driver_nl80211_join_mesh,
15026 .leave_mesh = wpa_driver_nl80211_leave_mesh,
Hai Shalom81f62d82019-07-22 12:10:00 -070015027 .probe_mesh_link = nl80211_probe_mesh_link,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080015028#endif /* CONFIG_MESH */
15029 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
15030 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
15031 .br_port_set_attr = wpa_driver_br_port_set_attr,
15032 .br_set_net_param = wpa_driver_br_set_net_param,
15033 .add_tx_ts = nl80211_add_ts,
15034 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -070015035 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080015036#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070015037 .roaming = nl80211_roaming,
Roshan Pius3a1667e2018-07-03 15:17:14 -070015038 .disable_fils = nl80211_disable_fils,
Ravi Joshie6ccb162015-07-16 17:45:41 -070015039 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080015040 .get_pref_freq_list = nl80211_get_pref_freq_list,
15041 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070015042 .p2p_lo_start = nl80211_p2p_lo_start,
15043 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070015044 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080015045 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070015046#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070015047 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
15048#endif /* CONFIG_MBO */
Hai Shalom899fcc72020-10-19 14:38:18 -070015049 .set_bssid_tmp_disallow = nl80211_set_bssid_tmp_disallow,
Hai Shalomc3565922019-10-28 11:58:20 -070015050 .add_sta_node = nl80211_add_sta_node,
Sunil Ravi89eba102022-09-13 21:04:37 -070015051#ifdef CONFIG_PASN
15052 .send_pasn_resp = nl80211_send_pasn_resp,
15053 .set_secure_ranging_ctx = nl80211_set_secure_ranging_ctx,
15054#endif /* CONFIG_PASN */
Sunil Ravic0f5d412024-09-11 22:12:49 +000015055#ifdef CONFIG_NAN_USD
15056 .nan_flush = nl80211_nan_flush,
15057 .nan_publish = nl80211_nan_publish,
15058 .nan_cancel_publish = nl80211_nan_cancel_publish,
15059 .nan_update_publish = nl80211_nan_update_publish,
15060 .nan_subscribe = nl80211_nan_subscribe,
15061 .nan_cancel_subscribe = nl80211_nan_cancel_subscribe,
15062#endif /* CONFIG_NAN_USD */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080015063#endif /* CONFIG_DRIVER_NL80211_QCA */
Hai Shalomc1a21442022-02-04 13:43:00 -080015064 .do_acs = nl80211_do_acs,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000015065#ifdef CONFIG_MBO
15066 .get_bss_transition_status = nl80211_get_bss_transition_status,
15067#endif /* CONFIG_MBO */
Dmitry Shmidt849734c2016-05-27 09:59:01 -070015068 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070015069 .get_ext_capab = nl80211_get_ext_capab,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000015070 .get_mld_capab = nl80211_get_mld_capab,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070015071 .update_connect_params = nl80211_update_connection_params,
Roshan Pius3a1667e2018-07-03 15:17:14 -070015072 .send_external_auth_status = nl80211_send_external_auth_status,
Hai Shalom74f70d42019-02-11 14:42:39 -080015073 .set_4addr_mode = nl80211_set_4addr_mode,
Hai Shalome21d4e82020-04-29 16:34:06 -070015074#ifdef CONFIG_DPP
15075 .dpp_listen = nl80211_dpp_listen,
15076#endif /* CONFIG_DPP */
Sunil Ravi89eba102022-09-13 21:04:37 -070015077 .get_sta_mlo_info = nl80211_get_sta_mlo_info,
Sunil Ravi036cec52023-03-29 11:35:17 -070015078 .link_add = nl80211_link_add,
Sunil Ravi99c035e2024-07-12 01:42:03 +000015079#ifdef CONFIG_IEEE80211BE
15080 .link_remove = driver_nl80211_link_remove,
15081 .is_drv_shared = nl80211_is_drv_shared,
15082 .link_sta_remove = wpa_driver_nl80211_link_sta_remove,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000015083 .can_share_drv = wpa_driver_nl80211_can_share_drv,
Sunil Ravi99c035e2024-07-12 01:42:03 +000015084#endif /* CONFIG_IEEE80211BE */
Hai Shalomc1a21442022-02-04 13:43:00 -080015085#ifdef CONFIG_TESTING_OPTIONS
15086 .register_frame = testing_nl80211_register_frame,
15087 .radio_disable = testing_nl80211_radio_disable,
15088#endif /* CONFIG_TESTING_OPTIONS */
Sunil Ravic0f5d412024-09-11 22:12:49 +000015089 .get_multi_hw_info = wpa_driver_get_multi_hw_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015090};