blob: b2b909e1ee17b057e18a579245af1a86ce3788bd [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 Ravi89eba102022-09-13 21:04:37 -070033#include "crypto/sha256.h"
34#include "crypto/sha384.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "netlink.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080036#include "linux_defines.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "linux_ioctl.h"
38#include "radiotap.h"
39#include "radiotap_iter.h"
40#include "rfkill.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080041#include "driver_nl80211.h"
Andy Kuoaba17c12022-04-14 16:05:31 +080042#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Ajay Davanagerib921bb82020-09-16 12:49:08 +053043#include "common/brcm_vendor.h"
Andy Kuoaba17c12022-04-14 16:05:31 +080044#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080045
Hai Shalom74f70d42019-02-11 14:42:39 -080046#ifndef NETLINK_CAP_ACK
47#define NETLINK_CAP_ACK 10
48#endif /* NETLINK_CAP_ACK */
Hai Shalom39ba6fc2019-01-22 12:40:38 -080049/* support for extack if compilation headers are too old */
50#ifndef NETLINK_EXT_ACK
51#define NETLINK_EXT_ACK 11
52enum nlmsgerr_attrs {
53 NLMSGERR_ATTR_UNUSED,
54 NLMSGERR_ATTR_MSG,
55 NLMSGERR_ATTR_OFFS,
56 NLMSGERR_ATTR_COOKIE,
57
58 __NLMSGERR_ATTR_MAX,
59 NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1
60};
61#endif
62#ifndef NLM_F_CAPPED
63#define NLM_F_CAPPED 0x100
64#endif
65#ifndef NLM_F_ACK_TLVS
66#define NLM_F_ACK_TLVS 0x200
67#endif
68#ifndef SOL_NETLINK
69#define SOL_NETLINK 270
70#endif
71
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072
Dmitry Shmidt54605472013-11-08 11:10:19 -080073#ifdef ANDROID
74/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
Dmitry Shmidt54605472013-11-08 11:10:19 -080075#undef nl_socket_set_nonblocking
76#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080077
Dmitry Shmidt54605472013-11-08 11:10:19 -080078#endif /* ANDROID */
79
80
Hai Shalomfdcde762020-04-02 11:19:20 -070081static struct nl_sock * nl_create_handle(struct nl_cb *cb, const char *dbg)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080082{
Hai Shalomfdcde762020-04-02 11:19:20 -070083 struct nl_sock *handle;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080084
Hai Shalomfdcde762020-04-02 11:19:20 -070085 handle = nl_socket_alloc_cb(cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080086 if (handle == NULL) {
87 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
88 "callbacks (%s)", dbg);
89 return NULL;
90 }
91
92 if (genl_connect(handle)) {
93 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
94 "netlink (%s)", dbg);
Hai Shalomfdcde762020-04-02 11:19:20 -070095 nl_socket_free(handle);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080096 return NULL;
97 }
98
99 return handle;
100}
101
102
Hai Shalomfdcde762020-04-02 11:19:20 -0700103static void nl_destroy_handles(struct nl_sock **handle)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800104{
105 if (*handle == NULL)
106 return;
Hai Shalomfdcde762020-04-02 11:19:20 -0700107 nl_socket_free(*handle);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800108 *handle = NULL;
109}
110
111
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700112#if __WORDSIZE == 64
113#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
114#else
115#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
116#endif
117
Hai Shalomfdcde762020-04-02 11:19:20 -0700118static void nl80211_register_eloop_read(struct nl_sock **handle,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700119 eloop_sock_handler handler,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700120 void *eloop_data, int persist)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700121{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800122 /*
123 * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
124 * by default. It is possible to hit that limit in some cases where
125 * operations are blocked, e.g., with a burst of Deauthentication frames
126 * to hostapd and STA entry deletion. Try to increase the buffer to make
127 * this less likely to occur.
128 */
Hai Shalomfdcde762020-04-02 11:19:20 -0700129 int err;
130
131 err = nl_socket_set_buffer_size(*handle, 262144, 0);
132 if (err < 0) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800133 wpa_printf(MSG_DEBUG,
134 "nl80211: Could not set nl_socket RX buffer size: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -0700135 nl_geterror(err));
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800136 /* continue anyway with the default (smaller) buffer */
137 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800138
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700139 nl_socket_set_nonblocking(*handle);
140 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
141 eloop_data, *handle);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700142 if (!persist)
143 *handle = (void *) (((intptr_t) *handle) ^
144 ELOOP_SOCKET_INVALID);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700145}
146
147
Hai Shalomfdcde762020-04-02 11:19:20 -0700148static void nl80211_destroy_eloop_handle(struct nl_sock **handle, int persist)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700149{
Roshan Pius3a1667e2018-07-03 15:17:14 -0700150 if (!persist)
151 *handle = (void *) (((intptr_t) *handle) ^
152 ELOOP_SOCKET_INVALID);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700153 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
154 nl_destroy_handles(handle);
155}
156
157
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800158static void nl80211_global_deinit(void *priv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800159static void nl80211_check_global(struct nl80211_global *global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800160
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800161static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700162static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
163 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800166wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800167 const u8 *set_addr, int first,
168 const char *driver_params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800169static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170 unsigned int freq, unsigned int wait,
Hai Shalomfdcde762020-04-02 11:19:20 -0700171 const u8 *buf, size_t buf_len,
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000172 int save_cookie, int no_cck, int no_ack,
173 int offchanok, const u16 *csa_offs,
174 size_t csa_offs_len, int link_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800175static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
176 int report);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800178#define IFIDX_ANY -1
179
180static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
181 int ifidx_reason);
182static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
183 int ifidx_reason);
184static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
185 int ifidx_reason);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700186
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700187static int nl80211_set_channel(struct i802_bss *bss,
188 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
190 int ifindex, int disabled);
191
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800192static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
193 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800194
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700195static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800196static int nl80211_set_param(void *priv, const char *param);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000197static void nl80211_remove_links(struct i802_bss *bss);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -0700198#ifdef CONFIG_MESH
199static int nl80211_put_mesh_config(struct nl_msg *msg,
200 struct wpa_driver_mesh_bss_params *params);
201#endif /* CONFIG_MESH */
Dmitry Shmidt29333592017-01-09 12:27:11 -0800202static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -0700203 u16 reason);
Winnie Chen4138eec2022-11-10 16:32:53 +0800204#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawade62409f2022-01-20 12:32:07 +0530205static int nl80211_set_td_policy(void *priv, u32 td_policy);
Winnie Chen4138eec2022-11-10 16:32:53 +0800206#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700207
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800208/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800209enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800210{
211 switch (width) {
212 case NL80211_CHAN_WIDTH_20_NOHT:
213 return CHAN_WIDTH_20_NOHT;
214 case NL80211_CHAN_WIDTH_20:
215 return CHAN_WIDTH_20;
216 case NL80211_CHAN_WIDTH_40:
217 return CHAN_WIDTH_40;
218 case NL80211_CHAN_WIDTH_80:
219 return CHAN_WIDTH_80;
220 case NL80211_CHAN_WIDTH_80P80:
221 return CHAN_WIDTH_80P80;
222 case NL80211_CHAN_WIDTH_160:
223 return CHAN_WIDTH_160;
Sunil8cd6f4d2022-06-28 18:40:46 +0000224 case NL80211_CHAN_WIDTH_320:
225 return CHAN_WIDTH_320;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000226 default:
227 return CHAN_WIDTH_UNKNOWN;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800228 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800229}
230
231
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800232int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700234 return nlmode == NL80211_IFTYPE_AP ||
235 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800236}
237
238
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800239int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700241 return nlmode == NL80211_IFTYPE_STATION ||
242 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800243}
244
245
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700246static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700248 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
249 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800250}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251
252
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800253struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
254 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700255{
256 struct i802_bss *bss;
257
258 for (bss = drv->first_bss; bss; bss = bss->next) {
259 if (bss->ifindex == ifindex)
260 return bss;
261 }
262
263 return NULL;
264}
265
266
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800267static int is_mesh_interface(enum nl80211_iftype nlmode)
268{
269 return nlmode == NL80211_IFTYPE_MESH_POINT;
270}
271
272
273void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700274{
275 if (drv->associated)
276 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
277 drv->associated = 0;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000278 os_memset(&drv->sta_mlo_info, 0, sizeof(drv->sta_mlo_info));
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700279 os_memset(drv->bssid, 0, ETH_ALEN);
Sunil Ravi036cec52023-03-29 11:35:17 -0700280 drv->first_bss->flink->freq = 0;
Sunil Ravi89eba102022-09-13 21:04:37 -0700281#ifdef CONFIG_DRIVER_NL80211_QCA
282 os_free(drv->pending_roam_data);
283 drv->pending_roam_data = NULL;
Sunil Ravi640215c2023-06-28 23:08:09 +0000284 os_free(drv->pending_t2lm_data);
285 drv->pending_t2lm_data = NULL;
286 os_free(drv->pending_link_reconfig_data);
287 drv->pending_link_reconfig_data = NULL;
Sunil Ravi89eba102022-09-13 21:04:37 -0700288#endif /* CONFIG_DRIVER_NL80211_QCA */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000289
290 drv->auth_mld = false;
291 drv->auth_mld_link_id = -1;
292 os_memset(drv->auth_ap_mld_addr, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700293}
294
295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296/* nl80211 code */
297static int ack_handler(struct nl_msg *msg, void *arg)
298{
299 int *err = arg;
300 *err = 0;
301 return NL_STOP;
302}
303
Hai Shalom899fcc72020-10-19 14:38:18 -0700304
305struct nl80211_ack_ext_arg {
306 int *err;
307 void *ext_data;
308};
309
310
311static int ack_handler_cookie(struct nl_msg *msg, void *arg)
312{
313 struct nl80211_ack_ext_arg *ext_arg = arg;
314 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
315 u64 *cookie = ext_arg->ext_data;
316 struct nlattr *attrs;
317 size_t ack_len, attr_len;
318
319 *ext_arg->err = 0;
320 ack_len = sizeof(struct nlmsghdr) + sizeof(int) +
321 sizeof(struct nlmsghdr);
322 attrs = (struct nlattr *)
323 ((u8 *) nlmsg_data(nlmsg_hdr(msg)) + sizeof(struct nlmsghdr) +
324 sizeof(int));
325 if (nlmsg_hdr(msg)->nlmsg_len <= ack_len)
326 return NL_STOP;
327
328 attr_len = nlmsg_hdr(msg)->nlmsg_len - ack_len;
329
330 if(!(nlmsg_hdr(msg)->nlmsg_flags & NLM_F_ACK_TLVS))
331 return NL_STOP;
332
333 nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, attr_len, NULL);
334 if (tb[NLMSGERR_ATTR_COOKIE])
335 *cookie = nla_get_u64(tb[NLMSGERR_ATTR_COOKIE]);
336
337 return NL_STOP;
338}
339
340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341static int finish_handler(struct nl_msg *msg, void *arg)
342{
343 int *ret = arg;
344 *ret = 0;
345 return NL_SKIP;
346}
347
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000348struct nl80211_ack_err_args {
349 int err;
350 struct nl_msg *orig_msg;
351 struct nl80211_err_info *err_info;
352};
353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
355 void *arg)
356{
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000357 struct nl80211_ack_err_args *err_args = arg;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800358 struct nlmsghdr *nlh = (struct nlmsghdr *) err - 1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000359 struct nlmsghdr *orig_nlh = nlmsg_hdr(err_args->orig_msg);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800360 int len = nlh->nlmsg_len;
361 struct nlattr *attrs;
362 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800363 int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000364 struct nlattr *mlo_links, *link_attr;
365 u32 offset;
366 int rem;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800367
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000368 err_args->err = err->error;
369 if (err_args->err_info)
370 err_args->err_info->link_id = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800371
372 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
373 return NL_SKIP;
374
375 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
376 ack_len += err->msg.nlmsg_len - sizeof(*nlh);
377
378 if (len <= ack_len)
379 return NL_STOP;
380
381 attrs = (void *) ((unsigned char *) nlh + ack_len);
382 len -= ack_len;
383
384 nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, len, NULL);
385 if (tb[NLMSGERR_ATTR_MSG]) {
386 len = strnlen((char *) nla_data(tb[NLMSGERR_ATTR_MSG]),
387 nla_len(tb[NLMSGERR_ATTR_MSG]));
388 wpa_printf(MSG_ERROR, "nl80211: kernel reports: %*s",
389 len, (char *) nla_data(tb[NLMSGERR_ATTR_MSG]));
390 }
391
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000392 if (!err_args->err_info)
393 return NL_SKIP;
394
395 /* Check if it was a per-link error report */
396
397 if (!tb[NLMSGERR_ATTR_OFFS] ||
398 os_memcmp(orig_nlh, &err->msg, sizeof(err->msg)) != 0)
399 return NL_SKIP;
400
401 offset = nla_get_u32(tb[NLMSGERR_ATTR_OFFS]);
402
403 mlo_links = nlmsg_find_attr(orig_nlh, GENL_HDRLEN,
404 NL80211_ATTR_MLO_LINKS);
405 if (!mlo_links)
406 return NL_SKIP;
407
408 nla_for_each_nested(link_attr, mlo_links, rem) {
409 struct nlattr *link_id;
410 size_t link_offset = (u8 *) link_attr - (u8 *) orig_nlh;
411
412 if (offset < link_offset ||
413 offset >= link_offset + link_attr->nla_len)
414 continue;
415
416 link_id = nla_find(nla_data(link_attr), nla_len(link_attr),
417 NL80211_ATTR_MLO_LINK_ID);
418 if (link_id) {
419 err_args->err_info->link_id = nla_get_u8(link_id);
420 wpa_printf(MSG_DEBUG,
421 "nl80211: kernel reports error for link: %d",
422 err_args->err_info->link_id);
423 break;
424 }
425 }
426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 return NL_SKIP;
428}
429
430
431static int no_seq_check(struct nl_msg *msg, void *arg)
432{
433 return NL_OK;
434}
435
436
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800437static void nl80211_nlmsg_clear(struct nl_msg *msg)
438{
439 /*
440 * Clear nlmsg data, e.g., to make sure key material is not left in
441 * heap memory for unnecessarily long time.
442 */
443 if (msg) {
444 struct nlmsghdr *hdr = nlmsg_hdr(msg);
445 void *data = nlmsg_data(hdr);
446 /*
447 * This would use nlmsg_datalen() or the older nlmsg_len() if
448 * only libnl were to maintain a stable API.. Neither will work
449 * with all released versions, so just calculate the length
450 * here.
451 */
452 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
453
454 os_memset(data, 0, len);
455 }
456}
457
458
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000459static int send_event_marker(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460{
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000461 struct nl_sock *handle;
462 struct nl_msg *msg;
463 struct nlmsghdr *hdr;
464 int res = 0;
465 int err = -NLE_NOMEM;
466
467 msg = nlmsg_alloc();
468 if (!msg)
469 goto out;
470
471 /* We only care about the returned sequence number for matching. */
472 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES))
473 goto out;
474
475 handle = (void *) (((intptr_t) drv->global->nl_event) ^
476 ELOOP_SOCKET_INVALID);
477
478 err = nl_send_auto_complete(handle, msg);
479 if (err < 0)
480 goto out;
481
482 hdr = nlmsg_hdr(msg);
483 res = hdr->nlmsg_seq;
484
485out:
486 nlmsg_free(msg);
487 if (err)
488 wpa_printf(MSG_INFO, "nl80211: %s failed: %s",
489 __func__, nl_geterror(err));
490 return res;
491}
492
493
494int send_and_recv(struct nl80211_global *global,
495 struct nl_sock *nl_handle, struct nl_msg *msg,
496 int (*valid_handler)(struct nl_msg *, void *),
497 void *valid_data,
498 int (*ack_handler_custom)(struct nl_msg *, void *),
499 void *ack_data,
500 struct nl80211_err_info *err_info)
501{
502 struct nl_cb *cb, *s_nl_cb;
503 struct nl80211_ack_err_args err;
504 int opt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800506 if (!msg)
507 return -ENOMEM;
508
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000509 err.err = -ENOMEM;
510
511 s_nl_cb = nl_socket_get_cb(nl_handle);
512 cb = nl_cb_clone(s_nl_cb);
513 nl_cb_put(s_nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514 if (!cb)
515 goto out;
516
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800517 /* try to set NETLINK_EXT_ACK to 1, ignoring errors */
518 opt = 1;
519 setsockopt(nl_socket_get_fd(nl_handle), SOL_NETLINK,
520 NETLINK_EXT_ACK, &opt, sizeof(opt));
521
Hai Shalom74f70d42019-02-11 14:42:39 -0800522 /* try to set NETLINK_CAP_ACK to 1, ignoring errors */
523 opt = 1;
524 setsockopt(nl_socket_get_fd(nl_handle), SOL_NETLINK,
525 NETLINK_CAP_ACK, &opt, sizeof(opt));
526
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000527 err.err = nl_send_auto_complete(nl_handle, msg);
528 if (err.err < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700529 wpa_printf(MSG_INFO,
530 "nl80211: nl_send_auto_complete() failed: %s",
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000531 nl_geterror(err.err));
Hai Shalomfdcde762020-04-02 11:19:20 -0700532 /* Need to convert libnl error code to an errno value. For now,
533 * just hardcode this to EBADF; the real error reason is shown
534 * in that error print above. */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000535 err.err = -EBADF;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536 goto out;
Hai Shalomfdcde762020-04-02 11:19:20 -0700537 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000539 err.err = 1;
540 err.orig_msg = msg;
541 err.err_info = err_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542
543 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000544 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err.err);
Hai Shalom899fcc72020-10-19 14:38:18 -0700545 if (ack_handler_custom) {
546 struct nl80211_ack_ext_arg *ext_arg = ack_data;
547
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000548 ext_arg->err = &err.err;
Hai Shalom899fcc72020-10-19 14:38:18 -0700549 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM,
550 ack_handler_custom, ack_data);
551 } else {
552 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
553 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554
555 if (valid_handler)
556 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
557 valid_handler, valid_data);
558
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000559 while (err.err > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700560 int res = nl_recvmsgs(nl_handle, cb);
Hai Shalomfdcde762020-04-02 11:19:20 -0700561
562 if (res == -NLE_DUMP_INTR) {
563 /* Most likely one of the nl80211 dump routines hit a
564 * case where internal results changed while the dump
565 * was being sent. The most common known case for this
566 * is scan results fetching while associated were every
567 * received Beacon frame from the AP may end up
568 * incrementing bss_generation. This
569 * NL80211_CMD_GET_SCAN case tries again in the caller;
570 * other cases (of which there are no known common ones)
571 * will stop and return an error. */
572 wpa_printf(MSG_DEBUG, "nl80211: %s; convert to -EAGAIN",
573 nl_geterror(res));
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000574 err.err = -EAGAIN;
Hai Shalomfdcde762020-04-02 11:19:20 -0700575 } else if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700576 wpa_printf(MSG_INFO,
Hai Shalomfdcde762020-04-02 11:19:20 -0700577 "nl80211: %s->nl_recvmsgs failed: %d (%s)",
578 __func__, res, nl_geterror(res));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700579 }
580 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581 out:
582 nl_cb_put(cb);
Hai Shalom60840252021-02-19 19:02:11 -0800583 /* Always clear the message as it can potentially contain keys */
584 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 nlmsg_free(msg);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000586 return err.err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587}
588
589
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000590static int nl80211_put_control_port(struct wpa_driver_nl80211_data *drv,
591 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592{
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000593 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT) ||
594 nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
595 ((drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX) &&
596 (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_OVER_NL80211) ||
597 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_PREAUTH))))
Hai Shalomb755a2a2020-04-23 21:49:02 -0700598 return -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000599 return 0;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700600}
601
602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603struct family_data {
604 const char *group;
605 int id;
606};
607
608
609static int family_handler(struct nl_msg *msg, void *arg)
610{
611 struct family_data *res = arg;
612 struct nlattr *tb[CTRL_ATTR_MAX + 1];
613 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
614 struct nlattr *mcgrp;
615 int i;
616
617 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
618 genlmsg_attrlen(gnlh, 0), NULL);
619 if (!tb[CTRL_ATTR_MCAST_GROUPS])
620 return NL_SKIP;
621
622 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
623 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
624 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
625 nla_len(mcgrp), NULL);
626 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
627 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
628 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
629 res->group,
630 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
631 continue;
632 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
633 break;
634 };
635
636 return NL_SKIP;
637}
638
639
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800640static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 const char *family, const char *group)
642{
643 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800644 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 struct family_data res = { group, -ENOENT };
646
647 msg = nlmsg_alloc();
648 if (!msg)
649 return -ENOMEM;
Hai Shalomc1a21442022-02-04 13:43:00 -0800650 if (!genlmsg_put(msg, 0, 0, global->nlctrl_id,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800651 0, 0, CTRL_CMD_GETFAMILY, 0) ||
652 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
653 nlmsg_free(msg);
654 return -1;
655 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656
Hai Shalom899fcc72020-10-19 14:38:18 -0700657 ret = send_and_recv(global, global->nl, msg, family_handler, &res,
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000658 NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700659 if (ret == 0)
660 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661 return ret;
662}
663
664
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800665void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
666 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800667{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700668 if (TEST_FAIL())
669 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800670 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
671 0, flags, cmd, 0);
672}
673
674
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800675static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
676{
677 if (bss->wdev_id_set)
678 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
679 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
680}
681
682
683struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
684{
685 struct nl_msg *msg;
686
687 msg = nlmsg_alloc();
688 if (!msg)
689 return NULL;
690
691 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
692 nl80211_set_iface_id(msg, bss) < 0) {
693 nlmsg_free(msg);
694 return NULL;
695 }
696
697 return msg;
698}
699
700
701static struct nl_msg *
Hai Shalomc1a21442022-02-04 13:43:00 -0800702nl80211_ifindex_msg_build(struct wpa_driver_nl80211_data *drv,
703 struct nl_msg *msg, int ifindex, int flags,
704 uint8_t cmd)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800705{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800706 if (!msg)
707 return NULL;
708
709 if (!nl80211_cmd(drv, msg, flags, cmd) ||
710 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
711 nlmsg_free(msg);
712 return NULL;
713 }
714
715 return msg;
716}
717
718
Hai Shalomc1a21442022-02-04 13:43:00 -0800719static struct nl_msg *
720nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
721 int flags, uint8_t cmd)
722{
723 return nl80211_ifindex_msg_build(drv, nlmsg_alloc(), ifindex, flags,
724 cmd);
725}
726
727
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800728struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
729 uint8_t cmd)
730{
731 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
732}
733
734
735struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
736{
737 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
738}
739
740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800741struct wiphy_idx_data {
742 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700743 enum nl80211_iftype nlmode;
744 u8 *macaddr;
Hai Shalom60840252021-02-19 19:02:11 -0800745 u8 use_4addr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800746};
747
748
749static int netdev_info_handler(struct nl_msg *msg, void *arg)
750{
751 struct nlattr *tb[NL80211_ATTR_MAX + 1];
752 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
753 struct wiphy_idx_data *info = arg;
754
755 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
756 genlmsg_attrlen(gnlh, 0), NULL);
757
758 if (tb[NL80211_ATTR_WIPHY])
759 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
760
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700761 if (tb[NL80211_ATTR_IFTYPE])
762 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
763
764 if (tb[NL80211_ATTR_MAC] && info->macaddr)
765 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
766 ETH_ALEN);
767
Hai Shalom60840252021-02-19 19:02:11 -0800768 if (tb[NL80211_ATTR_4ADDR])
769 info->use_4addr = nla_get_u8(tb[NL80211_ATTR_4ADDR]);
770
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800771 return NL_SKIP;
772}
773
774
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800775int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800776{
777 struct nl_msg *msg;
778 struct wiphy_idx_data data = {
779 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700780 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800781 };
782
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800783 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
784 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800785
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000786 if (send_and_recv_resp(bss->drv, msg, netdev_info_handler, &data) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800787 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800788 return -1;
789}
790
791
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700792static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
793{
794 struct nl_msg *msg;
795 struct wiphy_idx_data data = {
796 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
797 .macaddr = NULL,
798 };
799
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800800 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
801 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700802
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000803 if (send_and_recv_resp(bss->drv, msg, netdev_info_handler, &data) == 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700804 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700805 return NL80211_IFTYPE_UNSPECIFIED;
806}
807
808
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700809static int nl80211_get_macaddr(struct i802_bss *bss)
810{
811 struct nl_msg *msg;
812 struct wiphy_idx_data data = {
813 .macaddr = bss->addr,
814 };
815
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800816 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
817 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700818
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000819 return send_and_recv_resp(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700820}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700821
822
Hai Shalom60840252021-02-19 19:02:11 -0800823static int nl80211_get_4addr(struct i802_bss *bss)
824{
825 struct nl_msg *msg;
826 struct wiphy_idx_data data = {
827 .use_4addr = 0,
828 };
829
830 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000831 send_and_recv_resp(bss->drv, msg, netdev_info_handler, &data))
Hai Shalom60840252021-02-19 19:02:11 -0800832 return -1;
833 return data.use_4addr;
834}
835
836
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800837static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
838 struct nl80211_wiphy_data *w)
839{
840 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800841 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800842
843 msg = nlmsg_alloc();
844 if (!msg)
845 return -1;
846
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800847 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
848 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
849 nlmsg_free(msg);
850 return -1;
851 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800852
Hai Shalom899fcc72020-10-19 14:38:18 -0700853 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000854 NULL, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800855 if (ret) {
856 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
857 "failed: ret=%d (%s)",
858 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800859 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800860 return ret;
861}
862
863
864static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
865{
866 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700867 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800868
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800869 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800870
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700871 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700872 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700873 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
874 __func__, res);
875 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800876}
877
878
879static int process_beacon_event(struct nl_msg *msg, void *arg)
880{
881 struct nl80211_wiphy_data *w = arg;
882 struct wpa_driver_nl80211_data *drv;
883 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
884 struct nlattr *tb[NL80211_ATTR_MAX + 1];
885 union wpa_event_data event;
886
887 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
888 genlmsg_attrlen(gnlh, 0), NULL);
889
890 if (gnlh->cmd != NL80211_CMD_FRAME) {
891 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
892 gnlh->cmd);
893 return NL_SKIP;
894 }
895
896 if (!tb[NL80211_ATTR_FRAME])
897 return NL_SKIP;
898
899 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
900 wiphy_list) {
901 os_memset(&event, 0, sizeof(event));
902 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
903 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
904 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
905 }
906
907 return NL_SKIP;
908}
909
910
911static struct nl80211_wiphy_data *
912nl80211_get_wiphy_data_ap(struct i802_bss *bss)
913{
914 static DEFINE_DL_LIST(nl80211_wiphys);
915 struct nl80211_wiphy_data *w;
916 int wiphy_idx, found = 0;
917 struct i802_bss *tmp_bss;
Paul Stewart092955c2017-02-06 09:13:09 -0800918 u8 channel;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800919
920 if (bss->wiphy_data != NULL)
921 return bss->wiphy_data;
922
923 wiphy_idx = nl80211_get_wiphy_index(bss);
924
925 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
926 if (w->wiphy_idx == wiphy_idx)
927 goto add;
928 }
929
930 /* alloc new one */
931 w = os_zalloc(sizeof(*w));
932 if (w == NULL)
933 return NULL;
934 w->wiphy_idx = wiphy_idx;
935 dl_list_init(&w->bsss);
936 dl_list_init(&w->drvs);
937
Paul Stewart092955c2017-02-06 09:13:09 -0800938 /* Beacon frames not supported in IEEE 802.11ad */
Sunil Ravi036cec52023-03-29 11:35:17 -0700939 if (ieee80211_freq_to_chan(bss->flink->freq, &channel) !=
Paul Stewart092955c2017-02-06 09:13:09 -0800940 HOSTAPD_MODE_IEEE80211AD) {
941 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
942 if (!w->nl_cb) {
943 os_free(w);
944 return NULL;
945 }
946 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
947 no_seq_check, NULL);
948 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
949 process_beacon_event, w);
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000950
Paul Stewart092955c2017-02-06 09:13:09 -0800951 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
952 "wiphy beacons");
953 if (w->nl_beacons == NULL) {
954 os_free(w);
955 return NULL;
956 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000957
Paul Stewart092955c2017-02-06 09:13:09 -0800958 if (nl80211_register_beacons(bss->drv, w)) {
959 nl_destroy_handles(&w->nl_beacons);
960 os_free(w);
961 return NULL;
962 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000963
Paul Stewart092955c2017-02-06 09:13:09 -0800964 nl80211_register_eloop_read(&w->nl_beacons,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700965 nl80211_recv_beacons, w, 0);
Paul Stewart092955c2017-02-06 09:13:09 -0800966 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800967
968 dl_list_add(&nl80211_wiphys, &w->list);
969
970add:
971 /* drv entry for this bss already there? */
972 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
973 if (tmp_bss->drv == bss->drv) {
974 found = 1;
975 break;
976 }
977 }
978 /* if not add it */
979 if (!found)
980 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
981
982 dl_list_add(&w->bsss, &bss->wiphy_list);
983 bss->wiphy_data = w;
984 return w;
985}
986
987
988static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
989{
990 struct nl80211_wiphy_data *w = bss->wiphy_data;
991 struct i802_bss *tmp_bss;
992 int found = 0;
993
994 if (w == NULL)
995 return;
996 bss->wiphy_data = NULL;
997 dl_list_del(&bss->wiphy_list);
998
999 /* still any for this drv present? */
1000 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1001 if (tmp_bss->drv == bss->drv) {
1002 found = 1;
1003 break;
1004 }
1005 }
1006 /* if not remove it */
1007 if (!found)
1008 dl_list_del(&bss->drv->wiphy_list);
1009
1010 if (!dl_list_empty(&w->bsss))
1011 return;
1012
Paul Stewart092955c2017-02-06 09:13:09 -08001013 if (w->nl_beacons)
Roshan Pius3a1667e2018-07-03 15:17:14 -07001014 nl80211_destroy_eloop_handle(&w->nl_beacons, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001015
1016 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001017 dl_list_del(&w->list);
1018 os_free(w);
1019}
1020
1021
Dmitry Shmidte4663042016-04-04 10:07:49 -07001022static unsigned int nl80211_get_ifindex(void *priv)
1023{
1024 struct i802_bss *bss = priv;
1025 struct wpa_driver_nl80211_data *drv = bss->drv;
1026
1027 return drv->ifindex;
1028}
1029
1030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1032{
1033 struct i802_bss *bss = priv;
1034 struct wpa_driver_nl80211_data *drv = bss->drv;
1035 if (!drv->associated)
1036 return -1;
1037 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1038 return 0;
1039}
1040
1041
1042static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1043{
1044 struct i802_bss *bss = priv;
1045 struct wpa_driver_nl80211_data *drv = bss->drv;
1046 if (!drv->associated)
1047 return -1;
1048 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1049 return drv->ssid_len;
1050}
1051
1052
Sunil Ravi77d572f2023-01-17 23:58:31 +00001053static int get_mlo_info(struct nl_msg *msg, void *arg)
1054{
1055 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1056 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1057 struct nlattr *link_attr, *link_data[NL80211_ATTR_MAX + 1];
1058 static struct nla_policy link_policy[NL80211_ATTR_MAX + 1] = {
1059 [NL80211_ATTR_MLO_LINK_ID] = { .type = NLA_U8 },
1060 [NL80211_ATTR_MAC] = { .minlen = ETH_ALEN, .maxlen = ETH_ALEN },
1061 };
1062 struct driver_sta_mlo_info *info = arg;
1063 int rem;
1064
1065 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1066 genlmsg_attrlen(gnlh, 0), NULL);
1067
1068 if (!tb[NL80211_ATTR_MLO_LINKS])
1069 return NL_SKIP;
1070
1071 info->valid_links = 0;
1072 nla_for_each_nested(link_attr, tb[NL80211_ATTR_MLO_LINKS], rem) {
1073 u8 link_id;
1074
1075 if (nla_parse_nested(link_data, NL80211_ATTR_MAX,
1076 link_attr, link_policy) != 0)
1077 continue;
1078
1079 if (!link_data[NL80211_ATTR_MLO_LINK_ID] ||
1080 !link_data[NL80211_ATTR_MAC])
1081 continue;
1082
1083 link_id = nla_get_u8(link_data[NL80211_ATTR_MLO_LINK_ID]);
1084 if (link_id >= MAX_NUM_MLD_LINKS)
1085 continue;
1086 info->valid_links |= BIT(link_id);
1087 os_memcpy(info->links[link_id].addr,
1088 nla_data(link_data[NL80211_ATTR_MAC]), ETH_ALEN);
1089 if (link_data[NL80211_ATTR_WIPHY_FREQ])
1090 info->links[link_id].freq =
1091 nla_get_u32(link_data[NL80211_ATTR_WIPHY_FREQ]);
1092 }
1093
1094 return NL_SKIP;
1095}
1096
1097
Sunil Ravi89eba102022-09-13 21:04:37 -07001098static int nl80211_get_sta_mlo_info(void *priv,
1099 struct driver_sta_mlo_info *mlo_info)
1100{
1101 struct i802_bss *bss = priv;
1102 struct wpa_driver_nl80211_data *drv = bss->drv;
1103
1104 if (!drv->associated)
1105 return -1;
1106
Sunil Ravi77d572f2023-01-17 23:58:31 +00001107 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1108 struct nl_msg *msg;
1109
1110 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001111 if (send_and_recv_resp(drv, msg, get_mlo_info,
1112 &drv->sta_mlo_info))
Sunil Ravi77d572f2023-01-17 23:58:31 +00001113 return -1;
1114 }
1115
Sunil Ravi89eba102022-09-13 21:04:37 -07001116 os_memcpy(mlo_info, &drv->sta_mlo_info, sizeof(*mlo_info));
1117 return 0;
1118}
1119
1120
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001121static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidte4663042016-04-04 10:07:49 -07001122 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
1123 int ifindex, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124{
1125 union wpa_event_data event;
1126
Dmitry Shmidte4663042016-04-04 10:07:49 -07001127 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001128 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1129 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1130 drv->first_bss->ifname);
1131 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001132 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001133 if (!drv->if_removed)
1134 return;
1135 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1136 drv->first_bss->ifname);
1137 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 }
1139
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001140 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -07001141 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001142 os_strlcpy(event.interface_status.ifname, ifname,
1143 sizeof(event.interface_status.ifname));
1144 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001145 if (drv)
1146 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1147 else
1148 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
1149 &event);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001150}
1151
1152
1153static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidte4663042016-04-04 10:07:49 -07001154 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
1155 int ifindex, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001156{
1157 union wpa_event_data event;
1158
Dmitry Shmidte4663042016-04-04 10:07:49 -07001159 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001160 if (drv->if_removed) {
1161 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1162 ifname);
1163 return;
1164 }
1165 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1166 ifname);
1167 drv->if_removed = 1;
1168 } else {
1169 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1170 ifname);
1171 }
1172
1173 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -07001174 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001175 os_strlcpy(event.interface_status.ifname, ifname,
1176 sizeof(event.interface_status.ifname));
1177 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001178 if (drv)
1179 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1180 else
1181 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
1182 &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183}
1184
1185
1186static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1187 u8 *buf, size_t len)
1188{
1189 int attrlen, rta_len;
1190 struct rtattr *attr;
1191
1192 attrlen = len;
1193 attr = (struct rtattr *) buf;
1194
1195 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1196 while (RTA_OK(attr, attrlen)) {
1197 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001198 if (os_strcmp(((char *) attr) + rta_len,
1199 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 return 1;
1201 else
1202 break;
1203 }
1204 attr = RTA_NEXT(attr, attrlen);
1205 }
1206
1207 return 0;
1208}
1209
1210
1211static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1212 int ifindex, u8 *buf, size_t len)
1213{
1214 if (drv->ifindex == ifindex)
1215 return 1;
1216
1217 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001218 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1220 "interface");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001221 if (wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL) < 0)
1222 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 return 1;
1224 }
1225
1226 return 0;
1227}
1228
1229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001230static struct wpa_driver_nl80211_data *
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001231nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len,
1232 int *init_failed)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001233{
1234 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001235 int res;
1236
1237 if (init_failed)
1238 *init_failed = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001239 dl_list_for_each(drv, &global->interfaces,
1240 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001241 res = wpa_driver_nl80211_own_ifindex(drv, idx, buf, len);
1242 if (res < 0) {
1243 wpa_printf(MSG_DEBUG,
1244 "nl80211: Found matching own interface, but failed to complete reinitialization");
1245 if (init_failed)
1246 *init_failed = 1;
1247 return drv;
1248 }
1249 if (res > 0 || have_ifidx(drv, idx, IFIDX_ANY))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001250 return drv;
1251 }
1252 return NULL;
1253}
1254
1255
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001256static void nl80211_refresh_mac(struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001257 int ifindex, int notify)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001258{
1259 struct i802_bss *bss;
1260 u8 addr[ETH_ALEN];
1261
1262 bss = get_bss_ifindex(drv, ifindex);
1263 if (bss &&
1264 linux_get_ifhwaddr(drv->global->ioctl_sock,
1265 bss->ifname, addr) < 0) {
1266 wpa_printf(MSG_DEBUG,
1267 "nl80211: %s: failed to re-read MAC address",
1268 bss->ifname);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001269 } else if (bss && !ether_addr_equal(addr, bss->addr)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001270 wpa_printf(MSG_DEBUG,
1271 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1272 MACSTR " to " MACSTR,
1273 ifindex, bss->ifname,
1274 MAC2STR(bss->addr), MAC2STR(addr));
Sunil Ravi77d572f2023-01-17 23:58:31 +00001275 os_memcpy(bss->prev_addr, bss->addr, ETH_ALEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001276 os_memcpy(bss->addr, addr, ETH_ALEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001277 if (notify)
1278 wpa_supplicant_event(drv->ctx,
1279 EVENT_INTERFACE_MAC_CHANGED, NULL);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001280 }
1281}
1282
1283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1285 struct ifinfomsg *ifi,
1286 u8 *buf, size_t len)
1287{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001288 struct nl80211_global *global = ctx;
1289 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001290 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 struct rtattr *attr;
1292 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001293 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001294 char ifname[IFNAMSIZ + 1];
1295 char extra[100], *pos, *end;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001296 int init_failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001298 extra[0] = '\0';
1299 pos = extra;
1300 end = pos + sizeof(extra);
1301 ifname[0] = '\0';
1302
1303 attrlen = len;
1304 attr = (struct rtattr *) buf;
1305 while (RTA_OK(attr, attrlen)) {
1306 switch (attr->rta_type) {
1307 case IFLA_IFNAME:
Hai Shalomfdcde762020-04-02 11:19:20 -07001308 if (RTA_PAYLOAD(attr) > IFNAMSIZ)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001309 break;
1310 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1311 ifname[RTA_PAYLOAD(attr)] = '\0';
1312 break;
1313 case IFLA_MASTER:
1314 brid = nla_get_u32((struct nlattr *) attr);
1315 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1316 break;
1317 case IFLA_WIRELESS:
1318 pos += os_snprintf(pos, end - pos, " wext");
1319 break;
1320 case IFLA_OPERSTATE:
1321 pos += os_snprintf(pos, end - pos, " operstate=%u",
1322 nla_get_u32((struct nlattr *) attr));
1323 break;
1324 case IFLA_LINKMODE:
1325 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1326 nla_get_u32((struct nlattr *) attr));
1327 break;
1328 }
1329 attr = RTA_NEXT(attr, attrlen);
1330 }
1331 extra[sizeof(extra) - 1] = '\0';
1332
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001333 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1334 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1335 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1337 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1338 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1339 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1340
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001341 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, &init_failed);
Dmitry Shmidte4663042016-04-04 10:07:49 -07001342 if (!drv)
1343 goto event_newlink;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001344 if (init_failed)
1345 return; /* do not update interface state */
Dmitry Shmidte4663042016-04-04 10:07:49 -07001346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001348 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001349 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001350 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001351 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1352 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001353 drv->ignore_if_down_event = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001354 /* Re-read MAC address as it may have changed */
1355 nl80211_refresh_mac(drv, ifi->ifi_index, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001356 return;
1357 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001358 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
1359 namebuf, ifname);
1360 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1361 wpa_printf(MSG_DEBUG,
1362 "nl80211: Not the main interface (%s) - do not indicate interface down",
1363 drv->first_bss->ifname);
1364 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001365 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1366 "event generated by mode change");
1367 drv->ignore_if_down_event = 0;
1368 } else {
1369 drv->if_disabled = 1;
1370 wpa_supplicant_event(drv->ctx,
1371 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001372
1373 /*
1374 * Try to get drv again, since it may be removed as
1375 * part of the EVENT_INTERFACE_DISABLED handling for
1376 * dynamic interfaces
1377 */
1378 drv = nl80211_find_drv(global, ifi->ifi_index,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001379 buf, len, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001380 if (!drv)
1381 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383 }
1384
1385 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07001386 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001387 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001388 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001389 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1390 "event since interface %s is down",
1391 namebuf);
Hai Shalomc9e41a12018-07-31 14:41:42 -07001392 return;
1393 }
1394 wpa_printf(MSG_DEBUG, "nl80211: Interface up (%s/%s)",
1395 namebuf, ifname);
1396 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1397 wpa_printf(MSG_DEBUG,
1398 "nl80211: Not the main interface (%s) - do not indicate interface up",
1399 drv->first_bss->ifname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001400 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001401 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1402 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001403 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001404 } else if (drv->if_removed) {
1405 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1406 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001407 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001408 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001409 /* Re-read MAC address as it may have changed */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001410 nl80211_refresh_mac(drv, ifi->ifi_index, 0);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001411
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001412 drv->if_disabled = 0;
1413 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1414 NULL);
1415 }
Sunil Ravi90775442020-09-24 11:53:19 -07001416 } else if (ifi->ifi_flags & IFF_UP) {
1417 /* Re-read MAC address as it may have changed */
1418 nl80211_refresh_mac(drv, ifi->ifi_index, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419 }
1420
1421 /*
1422 * Some drivers send the association event before the operup event--in
1423 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1424 * fails. This will hit us when wpa_supplicant does not need to do
1425 * IEEE 802.1X authentication
1426 */
1427 if (drv->operstate == 1 &&
1428 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001429 !(ifi->ifi_flags & IFF_RUNNING)) {
1430 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001431 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 }
1434
Dmitry Shmidte4663042016-04-04 10:07:49 -07001435event_newlink:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001436 if (ifname[0])
Dmitry Shmidte4663042016-04-04 10:07:49 -07001437 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1438 ifname);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001439
Dmitry Shmidte4663042016-04-04 10:07:49 -07001440 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001441 struct i802_bss *bss;
1442
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001444 if (!if_indextoname(brid, namebuf)) {
1445 wpa_printf(MSG_DEBUG,
1446 "nl80211: Could not find bridge ifname for ifindex %u",
1447 brid);
1448 return;
1449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1451 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001452 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001453
1454 for (bss = drv->first_bss; bss; bss = bss->next) {
1455 if (os_strcmp(ifname, bss->ifname) == 0) {
1456 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1457 break;
1458 }
1459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461}
1462
1463
1464static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1465 struct ifinfomsg *ifi,
1466 u8 *buf, size_t len)
1467{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001468 struct nl80211_global *global = ctx;
1469 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001470 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 struct rtattr *attr;
1472 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001473 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001474 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001476 extra[0] = '\0';
1477 pos = extra;
1478 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001479 ifname[0] = '\0';
1480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001481 attrlen = len;
1482 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001484 switch (attr->rta_type) {
1485 case IFLA_IFNAME:
Hai Shalomfdcde762020-04-02 11:19:20 -07001486 if (RTA_PAYLOAD(attr) > IFNAMSIZ)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001487 break;
1488 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1489 ifname[RTA_PAYLOAD(attr)] = '\0';
1490 break;
1491 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001493 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1494 break;
1495 case IFLA_OPERSTATE:
1496 pos += os_snprintf(pos, end - pos, " operstate=%u",
1497 nla_get_u32((struct nlattr *) attr));
1498 break;
1499 case IFLA_LINKMODE:
1500 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1501 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001502 break;
1503 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 attr = RTA_NEXT(attr, attrlen);
1505 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001506 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001508 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1509 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1510 ifi->ifi_flags,
1511 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1512 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1513 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1514 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1515
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001516 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001517
Dmitry Shmidte4663042016-04-04 10:07:49 -07001518 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519 /* device has been removed from bridge */
1520 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001521
1522 if (!if_indextoname(brid, namebuf)) {
1523 wpa_printf(MSG_DEBUG,
1524 "nl80211: Could not find bridge ifname for ifindex %u",
1525 brid);
1526 } else {
1527 wpa_printf(MSG_DEBUG,
1528 "nl80211: Remove ifindex %u for bridge %s",
1529 brid, namebuf);
1530 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001531 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001532 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001533
1534 if (ifi->ifi_family != AF_BRIDGE || !brid)
1535 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1536 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537}
1538
1539
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001540struct nl80211_get_assoc_freq_arg {
1541 struct wpa_driver_nl80211_data *drv;
1542 unsigned int assoc_freq;
1543 unsigned int ibss_freq;
1544 u8 assoc_bssid[ETH_ALEN];
1545 u8 assoc_ssid[SSID_MAX_LEN];
1546 u8 assoc_ssid_len;
Sunil Ravi89eba102022-09-13 21:04:37 -07001547 u8 bssid[MAX_NUM_MLD_LINKS][ETH_ALEN];
1548 unsigned int freq[MAX_NUM_MLD_LINKS];
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001549};
1550
1551static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1552{
1553 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1554 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1555 struct nlattr *bss[NL80211_BSS_MAX + 1];
1556 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1557 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1558 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1559 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1560 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
Sunil Ravi89eba102022-09-13 21:04:37 -07001561 [NL80211_BSS_MLO_LINK_ID] = { .type = NLA_U8 },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001562 };
1563 struct nl80211_get_assoc_freq_arg *ctx = arg;
1564 enum nl80211_bss_status status;
Sunil Ravi89eba102022-09-13 21:04:37 -07001565 struct wpa_driver_nl80211_data *drv = ctx->drv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001566
1567 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1568 genlmsg_attrlen(gnlh, 0), NULL);
1569 if (!tb[NL80211_ATTR_BSS] ||
1570 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1571 bss_policy) ||
1572 !bss[NL80211_BSS_STATUS])
1573 return NL_SKIP;
1574
1575 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1576 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1577 bss[NL80211_BSS_FREQUENCY]) {
Sunil Ravi89eba102022-09-13 21:04:37 -07001578 int link_id = -1;
1579 u32 freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1580
1581 if (bss[NL80211_BSS_MLO_LINK_ID])
1582 link_id = nla_get_u8(bss[NL80211_BSS_MLO_LINK_ID]);
1583
1584 if (link_id >= 0 && link_id < MAX_NUM_MLD_LINKS) {
1585 ctx->freq[link_id] = freq;
1586 wpa_printf(MSG_DEBUG,
1587 "nl80211: MLO link %d associated on %u MHz",
1588 link_id, ctx->freq[link_id]);
1589 }
1590
1591 if (!drv->sta_mlo_info.valid_links ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00001592 drv->sta_mlo_info.assoc_link_id == link_id) {
Sunil Ravi89eba102022-09-13 21:04:37 -07001593 ctx->assoc_freq = freq;
1594 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1595 ctx->assoc_freq);
1596 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001597 }
1598 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1599 bss[NL80211_BSS_FREQUENCY]) {
1600 ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1601 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1602 ctx->ibss_freq);
1603 }
1604 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1605 bss[NL80211_BSS_BSSID]) {
Sunil Ravi89eba102022-09-13 21:04:37 -07001606 int link_id = -1;
1607 const u8 *bssid = nla_data(bss[NL80211_BSS_BSSID]);
1608
1609 if (bss[NL80211_BSS_MLO_LINK_ID])
1610 link_id = nla_get_u8(bss[NL80211_BSS_MLO_LINK_ID]);
1611
1612 if (link_id >= 0 && link_id < MAX_NUM_MLD_LINKS) {
1613 os_memcpy(ctx->bssid[link_id], bssid, ETH_ALEN);
1614 wpa_printf(MSG_DEBUG,
1615 "nl80211: MLO link %d associated with "
1616 MACSTR, link_id, MAC2STR(bssid));
1617 }
1618
1619 if (!drv->sta_mlo_info.valid_links ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00001620 drv->sta_mlo_info.assoc_link_id == link_id) {
Sunil Ravi89eba102022-09-13 21:04:37 -07001621 os_memcpy(ctx->assoc_bssid, bssid, ETH_ALEN);
1622 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1623 MACSTR, MAC2STR(bssid));
1624 }
1625
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001626 }
1627
1628 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1629 bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1630 const u8 *ie, *ssid;
1631 size_t ie_len;
1632
1633 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1634 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1635 ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1636 if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1637 ctx->assoc_ssid_len = ssid[1];
1638 os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1639 }
1640 }
1641
1642 return NL_SKIP;
1643}
1644
1645
1646int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001647{
1648 struct nl_msg *msg;
1649 int ret;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001650 struct nl80211_get_assoc_freq_arg arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07001651 int count = 0;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001652
Hai Shalomfdcde762020-04-02 11:19:20 -07001653try_again:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001654 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001655 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001656 arg.drv = drv;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001657 ret = send_and_recv_resp(drv, msg, nl80211_get_assoc_freq_handler,
1658 &arg);
Hai Shalomfdcde762020-04-02 11:19:20 -07001659 if (ret == -EAGAIN) {
1660 count++;
1661 if (count >= 10) {
1662 wpa_printf(MSG_INFO,
1663 "nl80211: Failed to receive consistent scan result dump for get_assoc_ssid");
1664 } else {
1665 wpa_printf(MSG_DEBUG,
1666 "nl80211: Failed to receive consistent scan result dump for get_assoc_ssid - try again");
1667 goto try_again;
1668 }
1669 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001670 if (ret == 0) {
1671 os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1672 return arg.assoc_ssid_len;
1673 }
1674 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1675 ret, strerror(-ret));
1676 return ret;
1677}
1678
1679
1680unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1681{
1682 struct nl_msg *msg;
1683 int ret;
1684 struct nl80211_get_assoc_freq_arg arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07001685 int count = 0;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001686
Hai Shalomfdcde762020-04-02 11:19:20 -07001687try_again:
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001688 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1689 os_memset(&arg, 0, sizeof(arg));
1690 arg.drv = drv;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001691 ret = send_and_recv_resp(drv, msg, nl80211_get_assoc_freq_handler,
1692 &arg);
Hai Shalomfdcde762020-04-02 11:19:20 -07001693 if (ret == -EAGAIN) {
1694 count++;
1695 if (count >= 10) {
1696 wpa_printf(MSG_INFO,
1697 "nl80211: Failed to receive consistent scan result dump for get_assoc_freq");
1698 } else {
1699 wpa_printf(MSG_DEBUG,
1700 "nl80211: Failed to receive consistent scan result dump for get_assoc_freq - try again");
1701 goto try_again;
1702 }
1703 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03001704 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001705 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1706 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001707 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001708 "associated BSS from scan results: %u MHz", freq);
1709 if (freq)
1710 drv->assoc_freq = freq;
Sunil Ravi89eba102022-09-13 21:04:37 -07001711
1712 if (drv->sta_mlo_info.valid_links) {
1713 int i;
1714
1715 for (i = 0; i < MAX_NUM_MLD_LINKS; i++)
1716 drv->sta_mlo_info.links[i].freq = arg.freq[i];
1717 }
1718
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001719 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001720 }
1721 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1722 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001723 return drv->assoc_freq;
1724}
1725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726static int get_link_noise(struct nl_msg *msg, void *arg)
1727{
1728 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1729 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1730 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1731 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1732 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1733 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1734 };
1735 struct wpa_signal_info *sig_change = arg;
1736
1737 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1738 genlmsg_attrlen(gnlh, 0), NULL);
1739
1740 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1741 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1742 return NL_SKIP;
1743 }
1744
1745 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1746 tb[NL80211_ATTR_SURVEY_INFO],
1747 survey_policy)) {
1748 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1749 "attributes!");
1750 return NL_SKIP;
1751 }
1752
1753 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1754 return NL_SKIP;
1755
1756 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1757 sig_change->frequency)
1758 return NL_SKIP;
1759
1760 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1761 return NL_SKIP;
1762
1763 sig_change->current_noise =
1764 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1765
1766 return NL_SKIP;
1767}
1768
1769
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001770int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1771 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772{
1773 struct nl_msg *msg;
1774
Hai Shalom74f70d42019-02-11 14:42:39 -08001775 sig_change->current_noise = WPA_INVALID_NOISE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776 sig_change->frequency = drv->assoc_freq;
1777
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001778 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001779 return send_and_recv_resp(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780}
1781
1782
Hai Shalom74f70d42019-02-11 14:42:39 -08001783static int get_channel_info(struct nl_msg *msg, void *arg)
1784{
1785 struct nlattr *tb[NL80211_ATTR_MAX + 1] = { 0 };
1786 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1787 struct wpa_channel_info *chan_info = arg;
1788
1789 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1790 genlmsg_attrlen(gnlh, 0), NULL);
1791
1792 os_memset(chan_info, 0, sizeof(struct wpa_channel_info));
1793 chan_info->chanwidth = CHAN_WIDTH_UNKNOWN;
1794
1795 if (tb[NL80211_ATTR_WIPHY_FREQ])
1796 chan_info->frequency =
1797 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1798 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
1799 chan_info->chanwidth = convert2width(
1800 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
1801 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1802 enum nl80211_channel_type ct =
1803 nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1804
1805 switch (ct) {
1806 case NL80211_CHAN_HT40MINUS:
1807 chan_info->sec_channel = -1;
1808 break;
1809 case NL80211_CHAN_HT40PLUS:
1810 chan_info->sec_channel = 1;
1811 break;
1812 default:
1813 chan_info->sec_channel = 0;
1814 break;
1815 }
1816 }
1817 if (tb[NL80211_ATTR_CENTER_FREQ1])
1818 chan_info->center_frq1 =
1819 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
1820 if (tb[NL80211_ATTR_CENTER_FREQ2])
1821 chan_info->center_frq2 =
1822 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
1823
1824 if (chan_info->center_frq2) {
1825 u8 seg1_idx = 0;
1826
1827 if (ieee80211_freq_to_chan(chan_info->center_frq2, &seg1_idx) !=
1828 NUM_HOSTAPD_MODES)
1829 chan_info->seg1_idx = seg1_idx;
1830 }
1831
1832 return NL_SKIP;
1833}
1834
1835
1836static int nl80211_channel_info(void *priv, struct wpa_channel_info *ci)
1837{
1838 struct i802_bss *bss = priv;
1839 struct wpa_driver_nl80211_data *drv = bss->drv;
1840 struct nl_msg *msg;
1841
1842 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001843 return send_and_recv_resp(drv, msg, get_channel_info, ci);
Hai Shalom74f70d42019-02-11 14:42:39 -08001844}
1845
1846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1848 void *handle)
1849{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001851 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001853 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001855 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001856 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001857 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1858 __func__, res);
1859 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860}
1861
1862
1863/**
1864 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1865 * @priv: driver_nl80211 private data
1866 * @alpha2_arg: country to which to switch to
1867 * Returns: 0 on success, -1 on failure
1868 *
1869 * This asks nl80211 to set the regulatory domain for given
1870 * country ISO / IEC alpha2.
1871 */
1872static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1873{
1874 struct i802_bss *bss = priv;
1875 struct wpa_driver_nl80211_data *drv = bss->drv;
1876 char alpha2[3];
1877 struct nl_msg *msg;
1878
1879 msg = nlmsg_alloc();
1880 if (!msg)
1881 return -ENOMEM;
1882
1883 alpha2[0] = alpha2_arg[0];
1884 alpha2[1] = alpha2_arg[1];
1885 alpha2[2] = '\0';
1886
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001887 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1888 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1889 nlmsg_free(msg);
1890 return -EINVAL;
1891 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001892 if (send_and_recv_cmd(drv, msg))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 return -EINVAL;
1894 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895}
1896
1897
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001898static int nl80211_get_country(struct nl_msg *msg, void *arg)
1899{
1900 char *alpha2 = arg;
1901 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1902 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1903
1904 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1905 genlmsg_attrlen(gnlh, 0), NULL);
1906 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1907 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1908 return NL_SKIP;
1909 }
1910 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1911 return NL_SKIP;
1912}
1913
1914
1915static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1916{
1917 struct i802_bss *bss = priv;
1918 struct wpa_driver_nl80211_data *drv = bss->drv;
1919 struct nl_msg *msg;
1920 int ret;
1921
1922 msg = nlmsg_alloc();
1923 if (!msg)
1924 return -ENOMEM;
1925
1926 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Sunil Ravi036cec52023-03-29 11:35:17 -07001927
1928 if (drv->capa.flags & WPA_DRIVER_FLAGS_SELF_MANAGED_REGULATORY) {
1929 /* put wiphy idx to get the interface specific country code
1930 * instead of the global one. */
1931 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, drv->wiphy_idx)) {
1932 nlmsg_free(msg);
1933 return -1;
1934 }
1935 }
1936
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001937 alpha2[0] = '\0';
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001938 ret = send_and_recv_resp(drv, msg, nl80211_get_country, alpha2);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001939 if (!alpha2[0])
1940 ret = -1;
1941
1942 return ret;
1943}
1944
1945
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001946static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001948 struct nl_cache *cache = NULL;
1949 struct genl_family *family = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001950 int ret;
1951
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001952 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1953 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1955 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001956 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 }
1958
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001959 global->nl = nl_create_handle(global->nl_cb, "nl");
1960 if (global->nl == NULL)
1961 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001963 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1964 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1966 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001967 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001968 }
1969
Hai Shalomc1a21442022-02-04 13:43:00 -08001970 global->nlctrl_id = genl_ctrl_resolve(global->nl, "nlctrl");
1971 if (global->nlctrl_id < 0) {
1972 wpa_printf(MSG_ERROR,
1973 "nl80211: 'nlctrl' generic netlink not found");
1974 goto err;
1975 }
1976
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001977 global->nl_event = nl_create_handle(global->nl_cb, "event");
1978 if (global->nl_event == NULL)
1979 goto err;
1980
1981 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001983 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984 if (ret < 0) {
1985 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1986 "membership for scan events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001987 ret, nl_geterror(ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001988 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 }
1990
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001991 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001993 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994 if (ret < 0) {
1995 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1996 "membership for mlme events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001997 ret, nl_geterror(ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001998 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 }
2000
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002001 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002003 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002004 if (ret < 0) {
2005 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2006 "membership for regulatory events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07002007 ret, nl_geterror(ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008 /* Continue without regulatory events */
2009 }
2010
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002011 ret = nl_get_multicast_id(global, "nl80211", "vendor");
2012 if (ret >= 0)
2013 ret = nl_socket_add_membership(global->nl_event, ret);
2014 if (ret < 0) {
2015 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2016 "membership for vendor events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07002017 ret, nl_geterror(ret));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002018 /* Continue without vendor events */
2019 }
2020
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002021 /* Resolve maxattr for kernel support checks */
2022 ret = genl_ctrl_alloc_cache(global->nl, &cache);
2023 if (ret < 0) {
2024 wpa_printf(MSG_DEBUG,
2025 "nl80211: Could not allocate genl cache: %d (%s)",
2026 ret, nl_geterror(ret));
2027 goto err;
2028 }
2029
2030 family = genl_ctrl_search(cache, global->nl80211_id);
2031 if (!family) {
2032 wpa_printf(MSG_DEBUG,
2033 "nl80211: Could not get nl80211 family from cache: %d (%s)",
2034 ret, nl_geterror(ret));
2035 goto err;
2036 }
2037
2038 global->nl80211_maxattr = genl_family_get_maxattr(family);
2039 wpa_printf(MSG_DEBUG, "nl80211: Maximum supported attribute ID: %u",
2040 global->nl80211_maxattr);
2041 genl_family_put(family);
2042 nl_cache_free(cache);
2043
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002044 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2045 no_seq_check, NULL);
2046 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2047 process_global_event, global);
2048
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002049 nl80211_register_eloop_read(&global->nl_event,
2050 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002051 global->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052
2053 return 0;
2054
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002055err:
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002056 genl_family_put(family);
2057 nl_cache_free(cache);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002058 nl_destroy_handles(&global->nl_event);
2059 nl_destroy_handles(&global->nl);
2060 nl_cb_put(global->nl_cb);
2061 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 return -1;
2063}
2064
2065
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002066static void nl80211_check_global(struct nl80211_global *global)
2067{
Hai Shalomfdcde762020-04-02 11:19:20 -07002068 struct nl_sock *handle;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002069 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
2070 int ret;
2071 unsigned int i;
2072
2073 /*
2074 * Try to re-add memberships to handle case of cfg80211 getting reloaded
2075 * and all registration having been cleared.
2076 */
2077 handle = (void *) (((intptr_t) global->nl_event) ^
2078 ELOOP_SOCKET_INVALID);
2079
2080 for (i = 0; groups[i]; i++) {
2081 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
2082 if (ret >= 0)
2083 ret = nl_socket_add_membership(handle, ret);
2084 if (ret < 0) {
2085 wpa_printf(MSG_INFO,
2086 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07002087 groups[i], ret, nl_geterror(ret));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002088 }
2089 }
2090}
2091
2092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002093static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
2094{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002095 struct wpa_driver_nl80211_data *drv = ctx;
2096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002100 * rtnetlink ifdown handler will report interfaces other than the P2P
2101 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002103 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2104 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105}
2106
2107
2108static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
2109{
2110 struct wpa_driver_nl80211_data *drv = ctx;
2111 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002112 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
2114 "after rfkill unblock");
2115 return;
2116 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002117
2118 if (is_p2p_net_interface(drv->nlmode))
2119 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
2120
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002121 /*
2122 * rtnetlink ifup handler will report interfaces other than the P2P
2123 * Device interface as enabled.
2124 */
2125 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2126 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127}
2128
2129
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002130static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
2131 void *eloop_ctx,
2132 void *handle)
2133{
2134 struct wpa_driver_nl80211_data *drv = eloop_ctx;
2135 u8 data[2048];
2136 struct msghdr msg;
2137 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002138 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002139 struct cmsghdr *cmsg;
2140 int res, found_ee = 0, found_wifi = 0, acked = 0;
2141 union wpa_event_data event;
2142
2143 memset(&msg, 0, sizeof(msg));
2144 msg.msg_iov = &entry;
2145 msg.msg_iovlen = 1;
2146 entry.iov_base = data;
2147 entry.iov_len = sizeof(data);
2148 msg.msg_control = &control;
2149 msg.msg_controllen = sizeof(control);
2150
2151 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
2152 /* if error or not fitting 802.3 header, return */
2153 if (res < 14)
2154 return;
2155
2156 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
2157 {
2158 if (cmsg->cmsg_level == SOL_SOCKET &&
2159 cmsg->cmsg_type == SCM_WIFI_STATUS) {
2160 int *ack;
2161
2162 found_wifi = 1;
2163 ack = (void *)CMSG_DATA(cmsg);
2164 acked = *ack;
2165 }
2166
2167 if (cmsg->cmsg_level == SOL_PACKET &&
2168 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
2169 struct sock_extended_err *err =
2170 (struct sock_extended_err *)CMSG_DATA(cmsg);
2171
2172 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
2173 found_ee = 1;
2174 }
2175 }
2176
2177 if (!found_ee || !found_wifi)
2178 return;
2179
2180 memset(&event, 0, sizeof(event));
2181 event.eapol_tx_status.dst = data;
2182 event.eapol_tx_status.data = data + 14;
2183 event.eapol_tx_status.data_len = res - 14;
2184 event.eapol_tx_status.ack = acked;
2185 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
2186}
2187
2188
Hai Shalomb755a2a2020-04-23 21:49:02 -07002189static int nl80211_init_connect_handle(struct i802_bss *bss)
2190{
2191 if (bss->nl_connect) {
2192 wpa_printf(MSG_DEBUG,
2193 "nl80211: Connect handle already created (nl_connect=%p)",
2194 bss->nl_connect);
2195 return -1;
2196 }
2197
2198 bss->nl_connect = nl_create_handle(bss->nl_cb, "connect");
2199 if (!bss->nl_connect)
2200 return -1;
2201 nl80211_register_eloop_read(&bss->nl_connect,
2202 wpa_driver_nl80211_event_receive,
2203 bss->nl_cb, 1);
2204 return 0;
2205}
2206
2207
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002208static int nl80211_init_bss(struct i802_bss *bss)
2209{
2210 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2211 if (!bss->nl_cb)
2212 return -1;
2213
2214 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2215 no_seq_check, NULL);
2216 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2217 process_bss_event, bss);
2218
Hai Shalomb755a2a2020-04-23 21:49:02 -07002219 nl80211_init_connect_handle(bss);
2220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002221 return 0;
2222}
2223
2224
2225static void nl80211_destroy_bss(struct i802_bss *bss)
2226{
2227 nl_cb_put(bss->nl_cb);
2228 bss->nl_cb = NULL;
Hai Shalomb755a2a2020-04-23 21:49:02 -07002229
2230 if (bss->nl_connect)
2231 nl80211_destroy_eloop_handle(&bss->nl_connect, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002232}
2233
2234
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002235static void
2236wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
2237{
2238 struct rfkill_config *rcfg;
2239
2240 if (drv->rfkill)
2241 return;
2242
2243 rcfg = os_zalloc(sizeof(*rcfg));
2244 if (!rcfg)
2245 return;
2246
2247 rcfg->ctx = drv;
2248
2249 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
2250 * not associated with a netdev, so use the name of some other interface
2251 * sharing the same wiphy as the P2P Device interface.
2252 *
2253 * Note: This is valid, as a P2P Device interface is always dynamically
2254 * created and is created only once another wpa_s interface was added.
2255 */
2256 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
2257 struct nl80211_global *global = drv->global;
2258 struct wpa_driver_nl80211_data *tmp1;
2259
2260 dl_list_for_each(tmp1, &global->interfaces,
2261 struct wpa_driver_nl80211_data, list) {
2262 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
2263 !tmp1->rfkill)
2264 continue;
2265
2266 wpa_printf(MSG_DEBUG,
2267 "nl80211: Use (%s) to initialize P2P Device rfkill",
2268 tmp1->first_bss->ifname);
2269 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
2270 sizeof(rcfg->ifname));
2271 break;
2272 }
2273 } else {
2274 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
2275 sizeof(rcfg->ifname));
2276 }
2277
2278 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
2279 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
2280 drv->rfkill = rfkill_init(rcfg);
2281 if (!drv->rfkill) {
2282 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
2283 os_free(rcfg);
2284 }
2285}
2286
2287
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002288static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
2289 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002290 const u8 *set_addr,
2291 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292{
2293 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 struct i802_bss *bss;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002295 char path[128], buf[200], *pos;
2296 ssize_t len;
2297 int ret;
2298
2299 ret = os_snprintf(path, sizeof(path), "/sys/class/net/%s/device/driver",
2300 ifname);
2301 if (!os_snprintf_error(sizeof(path), ret)) {
2302 len = readlink(path, buf, sizeof(buf));
2303 if (len > 0 && (size_t) len < sizeof(buf)) {
2304 buf[len] = '\0';
2305 pos = strrchr(buf, '/');
2306 if (pos)
2307 pos++;
2308 else
2309 pos = buf;
2310 wpa_printf(MSG_DEBUG,
2311 "nl80211: Initialize interface %s (driver: %s)",
2312 ifname, pos);
2313 }
2314 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002316 if (global_priv == NULL)
2317 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 drv = os_zalloc(sizeof(*drv));
2319 if (drv == NULL)
2320 return NULL;
2321 drv->global = global_priv;
2322 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002323 drv->hostapd = !!hostapd;
2324 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002325
2326 /*
2327 * There is no driver capability flag for this, so assume it is
2328 * supported and disable this on first attempt to use if the driver
2329 * rejects the command due to missing support.
2330 */
2331 drv->set_rekey_offload = 1;
2332
Hai Shalom81f62d82019-07-22 12:10:00 -07002333 drv->num_if_indices = ARRAY_SIZE(drv->default_if_indices);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002334 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002335
2336 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
2337 if (!drv->first_bss) {
2338 os_free(drv);
2339 return NULL;
2340 }
2341 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002343 bss->ctx = ctx;
2344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
2346 drv->monitor_ifidx = -1;
2347 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002348 drv->eapol_tx_sock = -1;
2349 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002351 if (nl80211_init_bss(bss))
2352 goto failed;
2353
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002354 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355 goto failed;
2356
Hai Shalom899fcc72020-10-19 14:38:18 -07002357 if (drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_TX_STATUS) {
2358 drv->control_port_ap = 1;
2359 goto skip_wifi_status;
2360 }
2361
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002362 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
2363 if (drv->eapol_tx_sock < 0)
2364 goto failed;
2365
2366 if (drv->data_tx_status) {
2367 int enabled = 1;
2368
2369 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
2370 &enabled, sizeof(enabled)) < 0) {
2371 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07002372 "nl80211: wifi status sockopt failed: %s",
2373 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002374 drv->data_tx_status = 0;
2375 if (!drv->use_monitor)
2376 drv->capa.flags &=
2377 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2378 } else {
Hai Shalom899fcc72020-10-19 14:38:18 -07002379 eloop_register_read_sock(
2380 drv->eapol_tx_sock,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002381 wpa_driver_nl80211_handle_eapol_tx_status,
2382 drv, NULL);
2383 }
2384 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002385skip_wifi_status:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386
2387 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002388 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002390 drv->in_interface_list = 1;
2391 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392
Sunil Ravi036cec52023-03-29 11:35:17 -07002393 /*
Sunil Ravi99c035e2024-07-12 01:42:03 +00002394 * Use link ID 0 for the single "link" of a non-MLD.
Sunil Ravi036cec52023-03-29 11:35:17 -07002395 */
Sunil Ravi99c035e2024-07-12 01:42:03 +00002396 bss->valid_links = 0;
Sunil Ravi036cec52023-03-29 11:35:17 -07002397 bss->flink = &bss->links[0];
Sunil Ravi036cec52023-03-29 11:35:17 -07002398 os_memcpy(bss->flink->addr, bss->addr, ETH_ALEN);
2399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400 return bss;
2401
2402failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002403 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404 return NULL;
2405}
2406
2407
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002408/**
2409 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2410 * @ctx: context to be used when calling wpa_supplicant functions,
2411 * e.g., wpa_supplicant_event()
2412 * @ifname: interface name, e.g., wlan0
2413 * @global_priv: private driver global data from global_init()
2414 * Returns: Pointer to private data, %NULL on failure
2415 */
2416static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2417 void *global_priv)
2418{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002419 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
2420 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002421}
2422
2423
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002424static int nl80211_register_frame(struct i802_bss *bss,
Hai Shalomfdcde762020-04-02 11:19:20 -07002425 struct nl_sock *nl_handle,
Hai Shalome21d4e82020-04-29 16:34:06 -07002426 u16 type, const u8 *match, size_t match_len,
2427 bool multicast)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002429 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002430 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002431 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002432 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002434 buf[0] = '\0';
2435 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Hai Shalome21d4e82020-04-29 16:34:06 -07002436 wpa_printf(MSG_DEBUG,
2437 "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s multicast=%d",
2438 type, fc2str(type), nl_handle, buf, multicast);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439
Hai Shalomfdcde762020-04-02 11:19:20 -07002440 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_FRAME)) ||
Hai Shalome21d4e82020-04-29 16:34:06 -07002441 (multicast && nla_put_flag(msg, NL80211_ATTR_RECEIVE_MULTICAST)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002442 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
2443 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
2444 nlmsg_free(msg);
2445 return -1;
2446 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447
Hai Shalom899fcc72020-10-19 14:38:18 -07002448 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002449 NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450 if (ret) {
2451 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
2452 "failed (type=%u): ret=%d (%s)",
2453 type, ret, strerror(-ret));
2454 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2455 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457 return ret;
2458}
2459
2460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002461static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
2462{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002463 if (bss->nl_mgmt) {
2464 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
2465 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
2466 return -1;
2467 }
2468
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002469 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002470 if (bss->nl_mgmt == NULL)
2471 return -1;
2472
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002473 return 0;
2474}
2475
2476
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002477static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
2478{
2479 nl80211_register_eloop_read(&bss->nl_mgmt,
2480 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002481 bss->nl_cb, 0);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002482}
2483
2484
Sunil Ravi7f769292024-07-23 22:21:32 +00002485static int nl80211_register_action_frame2(struct i802_bss *bss,
2486 const u8 *match, size_t match_len,
2487 bool multicast)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488{
2489 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002490 return nl80211_register_frame(bss, bss->nl_mgmt,
Sunil Ravi7f769292024-07-23 22:21:32 +00002491 type, match, match_len, multicast);
2492}
2493
2494
2495static int nl80211_register_action_frame(struct i802_bss *bss,
2496 const u8 *match, size_t match_len)
2497{
2498 return nl80211_register_action_frame2(bss, match, match_len, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499}
2500
2501
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002502static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002504 struct wpa_driver_nl80211_data *drv = bss->drv;
Hai Shalomfdcde762020-04-02 11:19:20 -07002505 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002506 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002507
2508 if (nl80211_alloc_mgmt_handle(bss))
2509 return -1;
2510 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
2511 "handle %p", bss->nl_mgmt);
2512
Hai Shalomfdcde762020-04-02 11:19:20 -07002513 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002514 /* register for any AUTH message */
Hai Shalome21d4e82020-04-29 16:34:06 -07002515 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0, false);
Hai Shalomfdcde762020-04-02 11:19:20 -07002516 } else if ((drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
2517 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
2518 /* register for SAE Authentication frames */
2519 nl80211_register_frame(bss, bss->nl_mgmt, type,
Hai Shalome21d4e82020-04-29 16:34:06 -07002520 (u8 *) "\x03\x00", 2, false);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002521 }
2522
Hai Shalom60840252021-02-19 19:02:11 -08002523#ifdef CONFIG_PASN
2524 /* register for PASN Authentication frames */
Sunil Ravi89eba102022-09-13 21:04:37 -07002525 if (nl80211_register_frame(bss, bss->nl_mgmt, type,
Hai Shalom60840252021-02-19 19:02:11 -08002526 (u8 *) "\x07\x00", 2, false))
2527 ret = -1;
2528#endif /* CONFIG_PASN */
2529
Dmitry Shmidt051af732013-10-22 13:52:46 -07002530#ifdef CONFIG_INTERWORKING
2531 /* QoS Map Configure */
2532 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002533 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002534#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002535#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002537 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002538 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002540 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002541 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002543 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002544 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002546 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002547 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002548 /* Protected GAS Initial Request */
2549 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2550 ret = -1;
2551 /* Protected GAS Initial Response */
2552 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2553 ret = -1;
2554 /* Protected GAS Comeback Request */
2555 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2556 ret = -1;
2557 /* Protected GAS Comeback Response */
2558 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2559 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002560#endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002561#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002563 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2565 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002566 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002567 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002568 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569 (u8 *) "\x7f\x50\x6f\x9a\x09",
2570 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002571 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572#endif /* CONFIG_P2P */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002573#ifdef CONFIG_NAN_USD
Sunil Ravi7f769292024-07-23 22:21:32 +00002574#define NAN_PUB_ACTION ((u8 *) "\x04\x09\x50\x6f\x9a\x13")
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002575 /* NAN SDF Public Action */
Sunil Ravi7f769292024-07-23 22:21:32 +00002576 if (nl80211_register_action_frame2(bss, NAN_PUB_ACTION, 6, true) < 0) {
2577 /* fallback to non-multicast */
2578 if (nl80211_register_action_frame2(bss, NAN_PUB_ACTION, 6,
2579 false) < 0)
2580 ret = -1;
2581 }
2582#undef NAN_PUB_ACTION
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002583#endif /* CONFIG_NAN_USD */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002584#ifdef CONFIG_DPP
2585 /* DPP Public Action */
2586 if (nl80211_register_action_frame(bss,
2587 (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
2588 6) < 0)
2589 ret = -1;
2590#endif /* CONFIG_DPP */
Hai Shalom74f70d42019-02-11 14:42:39 -08002591#ifdef CONFIG_OCV
2592 /* SA Query Request */
2593 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x00", 2) < 0)
2594 ret = -1;
2595#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002597 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002598 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002599#ifdef CONFIG_TDLS
2600 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2601 /* TDLS Discovery Response */
2602 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2603 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002604 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002605 }
2606#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002607#ifdef CONFIG_FST
2608 /* FST Action frames */
2609 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2610 ret = -1;
2611#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612
2613 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002614 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002615 ret = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002616 else if (!drv->has_driver_key_mgmt) {
2617 int i;
2618
2619 /* Update supported AKMs only if the driver doesn't advertize
2620 * any AKM capabilities. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2622 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2623
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002624 /* Update per interface supported AKMs */
2625 for (i = 0; i < WPA_IF_MAX; i++)
2626 drv->capa.key_mgmt_iftype[i] = drv->capa.key_mgmt;
2627 }
2628
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002629 /* WNM - BSS Transition Management Request */
2630 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002631 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002632 /* WNM-Sleep Mode Response */
2633 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002634 ret = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002635#ifdef CONFIG_WNM
2636 /* WNM - Collocated Interference Request */
2637 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x0b", 2) < 0)
2638 ret = -1;
2639#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002640
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002641#ifdef CONFIG_HS20
2642 /* WNM-Notification */
2643 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002644 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002645#endif /* CONFIG_HS20 */
2646
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002647 /* WMM-AC ADDTS Response */
2648 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2649 ret = -1;
2650
2651 /* WMM-AC DELTS */
2652 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2653 ret = -1;
2654
2655 /* Radio Measurement - Neighbor Report Response */
2656 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2657 ret = -1;
2658
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002659 /* Radio Measurement - Radio Measurement Request */
Hai Shalom899fcc72020-10-19 14:38:18 -07002660 if (!drv->no_rrm &&
2661 nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002662 ret = -1;
2663
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002664 /* Radio Measurement - Link Measurement Request */
2665 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2666 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2667 ret = -1;
2668
Hai Shalomc1a21442022-02-04 13:43:00 -08002669 /* Robust AV SCS Response */
2670 if (nl80211_register_action_frame(bss, (u8 *) "\x13\x01", 2) < 0)
2671 ret = -1;
2672
Hai Shalom899fcc72020-10-19 14:38:18 -07002673 /* Robust AV MSCS Response */
2674 if (nl80211_register_action_frame(bss, (u8 *) "\x13\x05", 2) < 0)
2675 ret = -1;
2676
Hai Shalomc1a21442022-02-04 13:43:00 -08002677 /* Protected QoS Management Action frame */
2678 if (nl80211_register_action_frame(bss, (u8 *) "\x7e\x50\x6f\x9a\x1a",
2679 5) < 0)
2680 ret = -1;
2681
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002682 nl80211_mgmt_handle_register_eloop(bss);
2683
2684 return ret;
2685}
2686
2687
2688static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2689{
2690 int ret = 0;
2691
2692 if (nl80211_alloc_mgmt_handle(bss))
2693 return -1;
2694
2695 wpa_printf(MSG_DEBUG,
2696 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2697 bss->nl_mgmt);
2698
2699 /* Auth frames for mesh SAE */
2700 if (nl80211_register_frame(bss, bss->nl_mgmt,
2701 (WLAN_FC_TYPE_MGMT << 2) |
2702 (WLAN_FC_STYPE_AUTH << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07002703 NULL, 0, false) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002704 ret = -1;
2705
2706 /* Mesh peering open */
2707 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2708 ret = -1;
2709 /* Mesh peering confirm */
2710 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2711 ret = -1;
2712 /* Mesh peering close */
2713 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2714 ret = -1;
2715
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002716 nl80211_mgmt_handle_register_eloop(bss);
2717
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002718 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719}
2720
2721
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002722static int nl80211_register_spurious_class3(struct i802_bss *bss)
2723{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002724 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002725 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002726
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002727 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
Hai Shalom899fcc72020-10-19 14:38:18 -07002728 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002729 NULL, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002730 if (ret) {
2731 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2732 "failed: ret=%d (%s)",
2733 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002734 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002735 return ret;
2736}
2737
2738
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002739static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2740{
2741 int ret = 0;
2742
2743 /* Public Action frames */
2744 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2745 ret = -1;
2746 /* RRM Measurement Report */
2747 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2748 ret = -1;
Paul Stewart092955c2017-02-06 09:13:09 -08002749 /* RRM Link Measurement Report */
2750 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2751 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002752 /* RRM Neighbor Report Request */
2753 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2754 ret = -1;
2755 /* FT Action frames */
2756 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2757 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002758 /* SA Query */
2759 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2760 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002761 /* Protected Dual of Public Action */
2762 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2763 ret = -1;
2764 /* WNM */
2765 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2766 ret = -1;
2767 /* WMM */
2768 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2769 ret = -1;
2770#ifdef CONFIG_FST
2771 /* FST Action frames */
2772 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2773 ret = -1;
2774#endif /* CONFIG_FST */
2775 /* Vendor-specific */
2776 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2777 ret = -1;
2778
2779 return ret;
2780}
2781
2782
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002783static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2784{
2785 static const int stypes[] = {
2786 WLAN_FC_STYPE_AUTH,
2787 WLAN_FC_STYPE_ASSOC_REQ,
2788 WLAN_FC_STYPE_REASSOC_REQ,
2789 WLAN_FC_STYPE_DISASSOC,
2790 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002791 WLAN_FC_STYPE_PROBE_REQ,
2792/* Beacon doesn't work as mac80211 doesn't currently allow
2793 * it, but it wouldn't really be the right thing anyway as
2794 * it isn't per interface ... maybe just dump the scan
2795 * results periodically for OLBC?
2796 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002797 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002798 };
2799 unsigned int i;
2800
2801 if (nl80211_alloc_mgmt_handle(bss))
2802 return -1;
2803 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2804 "handle %p", bss->nl_mgmt);
2805
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002806 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002807 if (nl80211_register_frame(bss, bss->nl_mgmt,
2808 (WLAN_FC_TYPE_MGMT << 2) |
2809 (stypes[i] << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07002810 NULL, 0, false) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002811 goto out_err;
2812 }
2813 }
2814
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002815 if (nl80211_action_subscribe_ap(bss))
2816 goto out_err;
2817
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002818 if (nl80211_register_spurious_class3(bss))
2819 goto out_err;
2820
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002821 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002822 return 0;
2823
2824out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002825 nl_destroy_handles(&bss->nl_mgmt);
2826 return -1;
2827}
2828
2829
2830static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2831{
2832 if (nl80211_alloc_mgmt_handle(bss))
2833 return -1;
2834 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2835 "handle %p (device SME)", bss->nl_mgmt);
2836
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002837 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002838 goto out_err;
2839
Hai Shalom5f92bc92019-04-18 11:54:11 -07002840 if (bss->drv->device_ap_sme) {
2841 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2842
2843 /* Register for all Authentication frames */
Hai Shalome21d4e82020-04-29 16:34:06 -07002844 if (nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0,
2845 false) < 0)
Hai Shalom5f92bc92019-04-18 11:54:11 -07002846 wpa_printf(MSG_DEBUG,
2847 "nl80211: Failed to subscribe to handle Authentication frames - SAE offload may not work");
2848 }
2849
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002850 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002851 return 0;
2852
2853out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002854 nl_destroy_handles(&bss->nl_mgmt);
2855 return -1;
2856}
2857
2858
2859static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2860{
2861 if (bss->nl_mgmt == NULL)
2862 return;
2863 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2864 "(%s)", bss->nl_mgmt, reason);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002865 nl80211_destroy_eloop_handle(&bss->nl_mgmt, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002866
2867 nl80211_put_wiphy_data_ap(bss);
2868}
2869
2870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2872{
2873 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2874}
2875
2876
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002877static void nl80211_del_p2pdev(struct i802_bss *bss)
2878{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002879 struct nl_msg *msg;
2880 int ret;
2881
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002882 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002883 ret = send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002884
2885 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2886 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002887 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002888}
2889
2890
2891static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2892{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002893 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002894 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002895
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002896 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2897 NL80211_CMD_STOP_P2P_DEVICE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002898 ret = send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002899
2900 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2901 start ? "Start" : "Stop",
2902 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002903 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002904 return ret;
2905}
2906
2907
2908static int i802_set_iface_flags(struct i802_bss *bss, int up)
2909{
2910 enum nl80211_iftype nlmode;
2911
2912 nlmode = nl80211_get_ifmode(bss);
2913 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2914 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2915 bss->ifname, up);
2916 }
2917
2918 /* P2P Device has start/stop which is equivalent */
2919 return nl80211_set_p2pdev(bss, up);
2920}
2921
2922
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002923#ifdef CONFIG_TESTING_OPTIONS
2924static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2925{
2926 /* struct wpa_driver_nl80211_data *drv = arg; */
2927 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2928 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2929
2930
2931 wpa_printf(MSG_DEBUG,
2932 "nl80211: QCA vendor test command response received");
2933
2934 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2935 genlmsg_attrlen(gnlh, 0), NULL);
2936 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2937 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2938 return NL_SKIP;
2939 }
2940
2941 wpa_hexdump(MSG_DEBUG,
2942 "nl80211: Received QCA vendor test command response",
2943 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2944 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2945
2946 return NL_SKIP;
2947}
2948#endif /* CONFIG_TESTING_OPTIONS */
2949
2950
2951static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2952{
2953#ifdef CONFIG_TESTING_OPTIONS
2954 struct nl_msg *msg;
2955 struct nlattr *params;
2956 int ret;
2957
2958 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2959 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2960 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2961 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2962 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2963 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2964 nlmsg_free(msg);
2965 return;
2966 }
2967 nla_nest_end(msg, params);
2968
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002969 ret = send_and_recv_resp(drv, msg, qca_vendor_test_cmd_handler, drv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002970 wpa_printf(MSG_DEBUG,
2971 "nl80211: QCA vendor test command returned %d (%s)",
2972 ret, strerror(-ret));
2973#endif /* CONFIG_TESTING_OPTIONS */
2974}
2975
2976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002978wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002979 const u8 *set_addr, int first,
2980 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002981{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002982 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002983 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002984 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002985
2986 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002987 bss->ifindex = drv->ifindex;
2988 bss->wdev_id = drv->global->if_add_wdevid;
2989 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2990
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002991 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2992 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002993 drv->global->if_add_wdevid_set = 0;
2994
Dmitry Shmidt03658832014-08-13 11:03:49 -07002995 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2996 bss->static_ap = 1;
2997
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002998 if (first &&
2999 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
3000 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
3001 drv->start_iface_up = 1;
3002
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003003 if (wpa_driver_nl80211_capa(drv))
3004 return -1;
3005
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003006 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
3007 return -1;
3008
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003009 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
3010 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003012 if (set_addr &&
3013 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
3014 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3015 set_addr)))
3016 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003017
Hai Shalomc1a21442022-02-04 13:43:00 -08003018 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_STATION)
3019 drv->start_mode_sta = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003020
Dmitry Shmidt03658832014-08-13 11:03:49 -07003021 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003022 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07003023 else if (bss->if_dynamic ||
3024 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003025 nlmode = nl80211_get_ifmode(bss);
3026 else
3027 nlmode = NL80211_IFTYPE_STATION;
3028
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003029 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003030 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003031 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003033
Dmitry Shmidt98660862014-03-11 17:26:21 -07003034 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003035 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003037 wpa_driver_nl80211_drv_init_rfkill(drv);
3038
Dmitry Shmidt98660862014-03-11 17:26:21 -07003039 if (!rfkill_is_blocked(drv->rfkill)) {
3040 int ret = i802_set_iface_flags(bss, 1);
3041 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3043 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003044 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003046
3047 if (is_p2p_net_interface(nlmode))
3048 nl80211_disable_11b_rates(bss->drv,
3049 bss->drv->ifindex, 1);
3050
Dmitry Shmidt98660862014-03-11 17:26:21 -07003051 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
3052 return ret;
3053 } else {
3054 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3055 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003056 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
3057 drv->if_disabled = 1;
3058
Dmitry Shmidt98660862014-03-11 17:26:21 -07003059 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060 }
3061
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003062 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003063 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
3064 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003065
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003066 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
3067 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3068 bss->addr))
3069 return -1;
3070 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
3071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073 if (send_rfkill_event) {
3074 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3075 drv, drv->ctx);
3076 }
3077
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003078 if (drv->vendor_cmd_test_avail)
3079 qca_vendor_test(drv);
3080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081 return 0;
3082}
3083
3084
Sunil Ravi036cec52023-03-29 11:35:17 -07003085static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss,
Sunil Ravi99c035e2024-07-12 01:42:03 +00003086 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003087{
3088 struct nl_msg *msg;
Paul Stewart092955c2017-02-06 09:13:09 -08003089 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003090 struct i802_link *link = nl80211_get_link(bss, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091
Sunil Ravi036cec52023-03-29 11:35:17 -07003092 if (!link->beacon_set)
3093 return 0;
3094
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003095 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
Sunil Ravi99c035e2024-07-12 01:42:03 +00003096 bss->ifindex);
Sunil Ravi036cec52023-03-29 11:35:17 -07003097 link->beacon_set = 0;
3098 link->freq = 0;
3099
Paul Stewart092955c2017-02-06 09:13:09 -08003100 nl80211_put_wiphy_data_ap(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003101 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_BEACON);
Sunil Ravi036cec52023-03-29 11:35:17 -07003102 if (!msg)
3103 return -ENOBUFS;
3104
Sunil Ravi99c035e2024-07-12 01:42:03 +00003105 if (link_id != NL80211_DRV_LINK_ID_NA) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003106 wpa_printf(MSG_DEBUG,
3107 "nl80211: MLD: stop beaconing on link=%u",
Sunil Ravi99c035e2024-07-12 01:42:03 +00003108 link_id);
Sunil Ravi036cec52023-03-29 11:35:17 -07003109
Sunil Ravi99c035e2024-07-12 01:42:03 +00003110 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003111 nlmsg_free(msg);
3112 return -ENOBUFS;
3113 }
3114 }
3115
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003116 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117}
3118
3119
Sunil Ravi036cec52023-03-29 11:35:17 -07003120static void wpa_driver_nl80211_del_beacon_all(struct i802_bss *bss)
3121{
Sunil Ravi99c035e2024-07-12 01:42:03 +00003122 int link_id;
Sunil Ravi036cec52023-03-29 11:35:17 -07003123
Sunil Ravi99c035e2024-07-12 01:42:03 +00003124 for_each_link_default(bss->valid_links, link_id, NL80211_DRV_LINK_ID_NA)
3125 wpa_driver_nl80211_del_beacon(bss, link_id);
Sunil Ravi036cec52023-03-29 11:35:17 -07003126}
3127
3128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003129/**
3130 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003131 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 *
3133 * Shut down driver interface and processing of driver events. Free
3134 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3135 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003136static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003138 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003139 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003141 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
3142 bss->ifname, drv->disabled_11b_rates);
3143
Dmitry Shmidt04949592012-07-19 12:16:46 -07003144 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003145 if (drv->data_tx_status)
3146 eloop_unregister_read_sock(drv->eapol_tx_sock);
3147 if (drv->eapol_tx_sock >= 0)
3148 close(drv->eapol_tx_sock);
3149
3150 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151 wpa_driver_nl80211_probe_req_report(bss, 0);
3152 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003153 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3154 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3156 "interface %s from bridge %s: %s",
3157 bss->ifname, bss->brname, strerror(errno));
3158 }
Hai Shalomc9e41a12018-07-31 14:41:42 -07003159
3160 if (drv->rtnl_sk)
Hai Shalomfdcde762020-04-02 11:19:20 -07003161 nl_socket_free(drv->rtnl_sk);
Hai Shalomc9e41a12018-07-31 14:41:42 -07003162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003163 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003164 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
3165 0) < 0)
3166 wpa_printf(MSG_INFO,
3167 "nl80211: Could not set bridge %s down",
3168 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003169 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003170 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3171 "bridge %s: %s",
3172 bss->brname, strerror(errno));
3173 }
3174
3175 nl80211_remove_monitor_interface(drv);
3176
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003177 if (is_ap_interface(drv->nlmode)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003178 wpa_driver_nl80211_del_beacon_all(bss);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003179 nl80211_remove_links(bss);
3180 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182 if (drv->eapol_sock >= 0) {
3183 eloop_unregister_read_sock(drv->eapol_sock);
3184 close(drv->eapol_sock);
3185 }
3186
3187 if (drv->if_indices != drv->default_if_indices)
3188 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003189
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003190 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3192
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003193 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3194 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003195 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003196 rfkill_deinit(drv->rfkill);
3197
3198 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3199
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003200 if (!drv->start_iface_up)
3201 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003202
3203 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003204 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
3205 0) < 0) {
3206 wpa_printf(MSG_DEBUG,
3207 "nl80211: Could not set interface down to restore permanent MAC address");
3208 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003209 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3210 drv->perm_addr) < 0) {
3211 wpa_printf(MSG_DEBUG,
3212 "nl80211: Could not restore permanent MAC address");
3213 }
3214 }
3215
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003216 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003217 if (drv->start_mode_sta)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003218 wpa_driver_nl80211_set_mode(bss,
3219 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003220 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003221 } else {
3222 nl80211_mgmt_unsubscribe(bss, "deinit");
3223 nl80211_del_p2pdev(bss);
3224 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003226 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228 os_free(drv->filter_ssids);
3229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003230 os_free(drv->auth_ie);
Hai Shalom60840252021-02-19 19:02:11 -08003231 os_free(drv->auth_data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003232
3233 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 dl_list_del(&drv->list);
3235
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003236 os_free(drv->extended_capa);
3237 os_free(drv->extended_capa_mask);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003238 for (i = 0; i < drv->num_iface_capa; i++) {
3239 os_free(drv->iface_capa[i].ext_capa);
3240 os_free(drv->iface_capa[i].ext_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003241 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003242 os_free(drv->first_bss);
Hai Shalom60840252021-02-19 19:02:11 -08003243#ifdef CONFIG_DRIVER_NL80211_QCA
3244 os_free(drv->pending_roam_data);
3245#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003246 os_free(drv);
3247}
3248
3249
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003250static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
3251{
3252 switch (alg) {
3253 case WPA_ALG_WEP:
3254 if (key_len == 5)
Paul Stewart092955c2017-02-06 09:13:09 -08003255 return RSN_CIPHER_SUITE_WEP40;
3256 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003257 case WPA_ALG_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08003258 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003259 case WPA_ALG_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003260 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003261 case WPA_ALG_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003262 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003263 case WPA_ALG_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003264 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003265 case WPA_ALG_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003266 return RSN_CIPHER_SUITE_GCMP_256;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003267 case WPA_ALG_BIP_CMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08003268 return RSN_CIPHER_SUITE_AES_128_CMAC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003269 case WPA_ALG_BIP_GMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08003270 return RSN_CIPHER_SUITE_BIP_GMAC_128;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003271 case WPA_ALG_BIP_GMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003272 return RSN_CIPHER_SUITE_BIP_GMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003273 case WPA_ALG_BIP_CMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003274 return RSN_CIPHER_SUITE_BIP_CMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003275 case WPA_ALG_SMS4:
Paul Stewart092955c2017-02-06 09:13:09 -08003276 return RSN_CIPHER_SUITE_SMS4;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003277 case WPA_ALG_KRK:
Paul Stewart092955c2017-02-06 09:13:09 -08003278 return RSN_CIPHER_SUITE_KRK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003279 case WPA_ALG_NONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003280 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
3281 alg);
3282 return 0;
3283 }
3284
3285 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
3286 alg);
3287 return 0;
3288}
3289
3290
3291static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
3292{
3293 switch (cipher) {
3294 case WPA_CIPHER_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003295 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003296 case WPA_CIPHER_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003297 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003298 case WPA_CIPHER_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003299 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003300 case WPA_CIPHER_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003301 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003302 case WPA_CIPHER_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08003303 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003304 case WPA_CIPHER_WEP104:
Paul Stewart092955c2017-02-06 09:13:09 -08003305 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003306 case WPA_CIPHER_WEP40:
Paul Stewart092955c2017-02-06 09:13:09 -08003307 return RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003308 case WPA_CIPHER_GTK_NOT_USED:
Paul Stewart092955c2017-02-06 09:13:09 -08003309 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003310 default:
3311 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003312 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003313}
3314
3315
3316static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
3317 int max_suites)
3318{
3319 int num_suites = 0;
3320
3321 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08003322 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003323 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08003324 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003325 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08003326 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003327 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08003328 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003329 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
Paul Stewart092955c2017-02-06 09:13:09 -08003330 suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003331 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
Paul Stewart092955c2017-02-06 09:13:09 -08003332 suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003333 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
Paul Stewart092955c2017-02-06 09:13:09 -08003334 suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003335
3336 return num_suites;
3337}
3338
3339
Hai Shalomfdcde762020-04-02 11:19:20 -07003340static int wpa_key_mgmt_to_suites(unsigned int key_mgmt_suites, u32 suites[],
3341 int max_suites)
3342{
3343 int num_suites = 0;
3344
3345#define __AKM(a, b) \
3346 if (num_suites < max_suites && \
3347 (key_mgmt_suites & (WPA_KEY_MGMT_ ## a))) \
3348 suites[num_suites++] = (RSN_AUTH_KEY_MGMT_ ## b)
3349 __AKM(IEEE8021X, UNSPEC_802_1X);
3350 __AKM(PSK, PSK_OVER_802_1X);
3351 __AKM(FT_IEEE8021X, FT_802_1X);
3352 __AKM(FT_PSK, FT_PSK);
3353 __AKM(IEEE8021X_SHA256, 802_1X_SHA256);
3354 __AKM(PSK_SHA256, PSK_SHA256);
3355 __AKM(SAE, SAE);
Sunil Ravi89eba102022-09-13 21:04:37 -07003356 __AKM(SAE_EXT_KEY, SAE_EXT_KEY);
Hai Shalomfdcde762020-04-02 11:19:20 -07003357 __AKM(FT_SAE, FT_SAE);
Sunil Ravi89eba102022-09-13 21:04:37 -07003358 __AKM(FT_SAE_EXT_KEY, FT_SAE_EXT_KEY);
Hai Shalomfdcde762020-04-02 11:19:20 -07003359 __AKM(CCKM, CCKM);
3360 __AKM(OSEN, OSEN);
3361 __AKM(IEEE8021X_SUITE_B, 802_1X_SUITE_B);
3362 __AKM(IEEE8021X_SUITE_B_192, 802_1X_SUITE_B_192);
3363 __AKM(FILS_SHA256, FILS_SHA256);
3364 __AKM(FILS_SHA384, FILS_SHA384);
3365 __AKM(FT_FILS_SHA256, FT_FILS_SHA256);
3366 __AKM(FT_FILS_SHA384, FT_FILS_SHA384);
3367 __AKM(OWE, OWE);
3368 __AKM(DPP, DPP);
3369 __AKM(FT_IEEE8021X_SHA384, FT_802_1X_SHA384);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003370 __AKM(IEEE8021X_SHA384, 802_1X_SHA384);
Hai Shalomfdcde762020-04-02 11:19:20 -07003371#undef __AKM
3372
3373 return num_suites;
3374}
3375
3376
Isaac Chiou6ce580d2024-04-24 17:07:24 +08003377#if (defined(CONFIG_DRIVER_NL80211_BRCM) && !defined(WIFI_BRCM_OPEN_SOURCE_MULTI_AKM)) || \
3378 defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawad14709082022-03-17 14:25:11 +05303379static int wpa_cross_akm_key_mgmt_to_suites(unsigned int key_mgmt_suites, u32 suites[],
3380 int max_suites)
3381{
3382 int num_suites = 0;
3383
3384#define __AKM_TO_SUITES_ARRAY(a, b) \
3385 if (num_suites < max_suites && \
3386 (key_mgmt_suites & (WPA_KEY_MGMT_ ## a))) \
3387 suites[num_suites++] = (RSN_AUTH_KEY_MGMT_ ## b)
3388 __AKM_TO_SUITES_ARRAY(PSK, PSK_OVER_802_1X);
3389 __AKM_TO_SUITES_ARRAY(SAE, SAE);
3390#undef __AKM_TO_SUITES_ARRAY
3391
3392 return num_suites;
3393}
Isaac Chiou6ce580d2024-04-24 17:07:24 +08003394#endif /* (CONFIG_DRIVER_NL80211_BRCM && !WIFI_BRCM_OPEN_SOURCE_MULTI_AKM) ||
3395 * CONFIG_DRIVER_NL80211_SYNA */
Vinayak Yadawad14709082022-03-17 14:25:11 +05303396
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003397
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003398#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003399static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
3400 const u8 *key, size_t key_len)
3401{
3402 struct nl_msg *msg;
3403 int ret;
3404
3405 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
3406 return 0;
3407
3408 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
3409 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
3410 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
3411 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
3412 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
3413 nl80211_nlmsg_clear(msg);
3414 nlmsg_free(msg);
3415 return -1;
3416 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003417 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003418 if (ret) {
3419 wpa_printf(MSG_DEBUG,
3420 "nl80211: Key management set key failed: ret=%d (%s)",
3421 ret, strerror(-ret));
3422 }
3423
3424 return ret;
3425}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003426#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003427
3428
Andy Kuoaba17c12022-04-14 16:05:31 +08003429#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05303430static int key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
3431 const u8 *key, size_t key_len)
3432{
3433 struct nl_msg *msg;
3434 int ret;
3435 struct nlattr *params;
3436
3437 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
3438 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
3439 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
Hai Shalomc1a21442022-02-04 13:43:00 -08003440 BRCM_VENDOR_SCMD_SET_PMK) ||
Mir Ali677e7482020-11-12 19:49:02 +05303441 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
3442 nla_put(msg, BRCM_ATTR_DRIVER_KEY_PMK, key_len, key)) {
3443 nl80211_nlmsg_clear(msg);
3444 nlmsg_free(msg);
3445 return -ENOBUFS;
3446 }
3447 nla_nest_end(msg, params);
3448
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003449 ret = send_and_recv_cmd(drv, msg);
Mir Ali677e7482020-11-12 19:49:02 +05303450 if (ret) {
3451 wpa_printf(MSG_DEBUG, "nl80211: Key mgmt set key failed: ret=%d (%s)",
3452 ret, strerror(-ret));
3453 }
3454
3455 return ret;
3456}
Andy Kuoaba17c12022-04-14 16:05:31 +08003457#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3458
Mir Ali677e7482020-11-12 19:49:02 +05303459
Roshan Pius3a1667e2018-07-03 15:17:14 -07003460static int nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
3461 const u8 *key, size_t key_len,
3462 const u8 *addr)
3463{
3464 struct nl_msg *msg = NULL;
3465 int ret;
3466
3467 /*
3468 * If the authenticator address is not set, assume it is
3469 * the current BSSID.
3470 */
3471 if (!addr && drv->associated)
3472 addr = drv->bssid;
3473 else if (!addr)
3474 return -1;
3475
3476 wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
3477 MAC2STR(addr));
3478 wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
3479 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
3480 if (!msg ||
3481 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3482 nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
3483 nl80211_nlmsg_clear(msg);
3484 nlmsg_free(msg);
3485 return -ENOBUFS;
3486 }
3487
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003488 ret = send_and_recv_cmd(drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003489 if (ret) {
3490 wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
3491 ret, strerror(-ret));
3492 }
3493
3494 return ret;
3495}
3496
3497
Hai Shalomfdcde762020-04-02 11:19:20 -07003498static int wpa_driver_nl80211_set_key(struct i802_bss *bss,
3499 struct wpa_driver_set_key_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003500{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003502 int ifindex;
Hai Shalomc3565922019-10-28 11:58:20 -07003503 struct nl_msg *msg;
3504 struct nl_msg *key_msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003505 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07003506 int skip_set_key = 1;
3507 const char *ifname = params->ifname;
3508 enum wpa_alg alg = params->alg;
3509 const u8 *addr = params->addr;
3510 int key_idx = params->key_idx;
3511 int set_tx = params->set_tx;
3512 const u8 *seq = params->seq;
3513 size_t seq_len = params->seq_len;
3514 const u8 *key = params->key;
3515 size_t key_len = params->key_len;
3516 int vlan_id = params->vlan_id;
3517 enum key_flag key_flag = params->key_flag;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003518 int link_id = params->link_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003519
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003520 /* Ignore for P2P Device */
3521 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
3522 return 0;
3523
3524 ifindex = if_nametoindex(ifname);
3525 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Sunil Ravi77d572f2023-01-17 23:58:31 +00003526 "set_tx=%d seq_len=%lu key_len=%lu key_flag=0x%x link_id=%d",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003527 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003528 (unsigned long) seq_len, (unsigned long) key_len, key_flag,
3529 link_id);
Hai Shalomfdcde762020-04-02 11:19:20 -07003530
3531 if (check_key_flag(key_flag)) {
3532 wpa_printf(MSG_DEBUG, "%s: invalid key_flag", __func__);
3533 return -EINVAL;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07003534 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003535
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003536#ifdef CONFIG_DRIVER_NL80211_QCA
Hai Shalomfdcde762020-04-02 11:19:20 -07003537 if ((key_flag & KEY_FLAG_PMK) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003538 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
3539 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
3540 __func__);
3541 ret = issue_key_mgmt_set_key(drv, key, key_len);
3542 return ret;
3543 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003544#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003545
Hai Shalomfdcde762020-04-02 11:19:20 -07003546 if (key_flag & KEY_FLAG_PMK) {
3547 if (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)
3548 return nl80211_set_pmk(drv, key, key_len, addr);
Andy Kuoaba17c12022-04-14 16:05:31 +08003549#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05303550 if (drv->vendor_set_pmk) {
Jay Patel731adae2021-02-17 14:55:58 -08003551 wpa_printf(MSG_INFO, "nl80211: key_mgmt_set_key with key_len %lu", (unsigned long) key_len);
Mir Ali677e7482020-11-12 19:49:02 +05303552 return key_mgmt_set_key(drv, key, key_len);
3553 }
Andy Kuoaba17c12022-04-14 16:05:31 +08003554#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3555
Hai Shalomfdcde762020-04-02 11:19:20 -07003556 /* The driver does not have any offload mechanism for PMK, so
3557 * there is no need to configure this key. */
3558 return 0;
3559 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003560
Hai Shalomfdcde762020-04-02 11:19:20 -07003561 ret = -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07003562 key_msg = nlmsg_alloc();
3563 if (!key_msg)
Hai Shalomfdcde762020-04-02 11:19:20 -07003564 return ret;
Hai Shalomc3565922019-10-28 11:58:20 -07003565
Hai Shalomfdcde762020-04-02 11:19:20 -07003566 if ((key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3567 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) {
3568 wpa_printf(MSG_DEBUG,
3569 "nl80211: SET_KEY (pairwise RX/TX modify)");
3570 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
3571 if (!msg)
3572 goto fail2;
3573 } else if (alg == WPA_ALG_NONE && (key_flag & KEY_FLAG_RX_TX)) {
3574 wpa_printf(MSG_DEBUG, "%s: invalid key_flag to delete key",
3575 __func__);
3576 ret = -EINVAL;
3577 goto fail2;
3578 } else if (alg == WPA_ALG_NONE) {
3579 wpa_printf(MSG_DEBUG, "nl80211: DEL_KEY");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003580 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
3581 if (!msg)
Hai Shalomc3565922019-10-28 11:58:20 -07003582 goto fail2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003584 u32 suite;
3585
3586 suite = wpa_alg_to_cipher_suite(alg, key_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07003587 if (!suite) {
3588 ret = -EINVAL;
Hai Shalomc3565922019-10-28 11:58:20 -07003589 goto fail2;
Hai Shalomfdcde762020-04-02 11:19:20 -07003590 }
3591 wpa_printf(MSG_DEBUG, "nl80211: NEW_KEY");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003592 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
Hai Shalomc3565922019-10-28 11:58:20 -07003593 if (!msg)
3594 goto fail2;
3595 if (nla_put(key_msg, NL80211_KEY_DATA, key_len, key) ||
3596 nla_put_u32(key_msg, NL80211_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003597 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003598 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599
Hai Shalomfdcde762020-04-02 11:19:20 -07003600 if (seq && seq_len) {
3601 if (nla_put(key_msg, NL80211_KEY_SEQ, seq_len, seq))
3602 goto fail;
3603 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ",
3604 seq, seq_len);
3605 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003606 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607
3608 if (addr && !is_broadcast_ether_addr(addr)) {
3609 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003610 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
3611 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612
Hai Shalomfdcde762020-04-02 11:19:20 -07003613 if ((key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3614 KEY_FLAG_PAIRWISE_RX ||
3615 (key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3616 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) {
3617 if (nla_put_u8(key_msg, NL80211_KEY_MODE,
3618 key_flag == KEY_FLAG_PAIRWISE_RX ?
3619 NL80211_KEY_NO_TX : NL80211_KEY_SET_TX))
3620 goto fail;
3621 } else if ((key_flag & KEY_FLAG_GROUP_MASK) ==
3622 KEY_FLAG_GROUP_RX) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Hai Shalomc3565922019-10-28 11:58:20 -07003624 if (nla_put_u32(key_msg, NL80211_KEY_TYPE,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003625 NL80211_KEYTYPE_GROUP))
3626 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07003627 } else if (!(key_flag & KEY_FLAG_PAIRWISE)) {
3628 wpa_printf(MSG_DEBUG,
3629 " key_flag missing PAIRWISE when setting a pairwise key");
3630 ret = -EINVAL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003631 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07003632 } else if (alg == WPA_ALG_WEP &&
3633 (key_flag & KEY_FLAG_RX_TX) == KEY_FLAG_RX_TX) {
3634 wpa_printf(MSG_DEBUG, " unicast WEP key");
3635 skip_set_key = 0;
3636 } else {
3637 wpa_printf(MSG_DEBUG, " pairwise key");
3638 }
3639 } else if ((key_flag & KEY_FLAG_PAIRWISE) ||
3640 !(key_flag & KEY_FLAG_GROUP)) {
3641 wpa_printf(MSG_DEBUG,
3642 " invalid key_flag for a broadcast key");
3643 ret = -EINVAL;
3644 goto fail;
3645 } else {
3646 wpa_printf(MSG_DEBUG, " broadcast key");
3647 if (key_flag & KEY_FLAG_DEFAULT)
3648 skip_set_key = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649 }
Hai Shalomc3565922019-10-28 11:58:20 -07003650 if (nla_put_u8(key_msg, NL80211_KEY_IDX, key_idx) ||
3651 nla_put_nested(msg, NL80211_ATTR_KEY, key_msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003652 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003653 nl80211_nlmsg_clear(key_msg);
3654 nlmsg_free(key_msg);
3655 key_msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656
Hai Shalomfdcde762020-04-02 11:19:20 -07003657 if (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
3658 wpa_printf(MSG_DEBUG, "nl80211: VLAN ID %d", vlan_id);
3659 if (nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id))
3660 goto fail;
3661 }
3662
Sunil Ravi77d572f2023-01-17 23:58:31 +00003663 if (link_id != -1) {
3664 wpa_printf(MSG_DEBUG, "nl80211: Link ID %d", link_id);
3665 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3666 goto fail;
3667 }
3668
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003669 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
3671 ret = 0;
3672 if (ret)
Hai Shalomfdcde762020-04-02 11:19:20 -07003673 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003674 ret, strerror(-ret));
3675
3676 /*
Hai Shalomfdcde762020-04-02 11:19:20 -07003677 * If we failed or don't need to set the key as default (below),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678 * we're done here.
3679 */
Hai Shalomfdcde762020-04-02 11:19:20 -07003680 if (ret || skip_set_key)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003681 return ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07003682 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_SET_KEY - default key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003683
Hai Shalomfdcde762020-04-02 11:19:20 -07003684 ret = -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07003685 key_msg = nlmsg_alloc();
3686 if (!key_msg)
Hai Shalomfdcde762020-04-02 11:19:20 -07003687 return ret;
Hai Shalomc3565922019-10-28 11:58:20 -07003688
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003689 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
Hai Shalomc3565922019-10-28 11:58:20 -07003690 if (!msg)
3691 goto fail2;
3692 if (!key_msg ||
3693 nla_put_u8(key_msg, NL80211_KEY_IDX, key_idx) ||
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003694 nla_put_flag(key_msg, wpa_alg_bip(alg) ?
Hai Shalomfdcde762020-04-02 11:19:20 -07003695 (key_idx == 6 || key_idx == 7 ?
3696 NL80211_KEY_DEFAULT_BEACON :
3697 NL80211_KEY_DEFAULT_MGMT) :
3698 NL80211_KEY_DEFAULT))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003699 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003701 struct nlattr *types;
3702
Hai Shalomc3565922019-10-28 11:58:20 -07003703 types = nla_nest_start(key_msg, NL80211_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003704 if (!types ||
Hai Shalomc3565922019-10-28 11:58:20 -07003705 nla_put_flag(key_msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003706 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003707 nla_nest_end(key_msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003708 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003709 struct nlattr *types;
3710
Hai Shalomc3565922019-10-28 11:58:20 -07003711 types = nla_nest_start(key_msg, NL80211_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003712 if (!types ||
Hai Shalomc3565922019-10-28 11:58:20 -07003713 nla_put_flag(key_msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003714 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003715 nla_nest_end(key_msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003716 }
3717
Hai Shalomc3565922019-10-28 11:58:20 -07003718 if (nla_put_nested(msg, NL80211_ATTR_KEY, key_msg))
3719 goto fail;
3720 nl80211_nlmsg_clear(key_msg);
3721 nlmsg_free(key_msg);
3722 key_msg = NULL;
3723
Hai Shalomfdcde762020-04-02 11:19:20 -07003724 if (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
3725 wpa_printf(MSG_DEBUG, "nl80211: set_key default - VLAN ID %d",
3726 vlan_id);
3727 if (nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id))
3728 goto fail;
3729 }
3730
Sunil Ravi77d572f2023-01-17 23:58:31 +00003731 if (link_id != -1) {
3732 wpa_printf(MSG_DEBUG, "nl80211: set_key default - Link ID %d",
3733 link_id);
3734 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3735 goto fail;
3736 }
3737
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003738 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003739 if (ret)
Hai Shalomfdcde762020-04-02 11:19:20 -07003740 wpa_printf(MSG_DEBUG,
3741 "nl80211: set_key default failed; err=%d %s",
3742 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743 return ret;
3744
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003745fail:
3746 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003747 nlmsg_free(msg);
Hai Shalomc3565922019-10-28 11:58:20 -07003748fail2:
3749 nl80211_nlmsg_clear(key_msg);
3750 nlmsg_free(key_msg);
Hai Shalomfdcde762020-04-02 11:19:20 -07003751 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752}
3753
3754
3755static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3756 int key_idx, int defkey,
3757 const u8 *seq, size_t seq_len,
3758 const u8 *key, size_t key_len)
3759{
3760 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003761 u32 suite;
3762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763 if (!key_attr)
3764 return -1;
3765
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003766 suite = wpa_alg_to_cipher_suite(alg, key_len);
3767 if (!suite)
3768 return -1;
3769
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003770 if (defkey && wpa_alg_bip(alg)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003771 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3772 return -1;
3773 } else if (defkey) {
3774 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3775 return -1;
3776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003778 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003779 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003780 (seq && seq_len &&
3781 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3782 nla_put(msg, NL80211_KEY_DATA, key_len, key))
3783 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003784
3785 nla_nest_end(msg, key_attr);
3786
3787 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788}
3789
3790
3791static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3792 struct nl_msg *msg)
3793{
3794 int i, privacy = 0;
3795 struct nlattr *nl_keys, *nl_key;
3796
3797 for (i = 0; i < 4; i++) {
3798 if (!params->wep_key[i])
3799 continue;
3800 privacy = 1;
3801 break;
3802 }
3803 if (params->wps == WPS_MODE_PRIVACY)
3804 privacy = 1;
3805 if (params->pairwise_suite &&
3806 params->pairwise_suite != WPA_CIPHER_NONE)
3807 privacy = 1;
3808
3809 if (!privacy)
3810 return 0;
3811
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003812 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3813 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003814
3815 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3816 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003817 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003818
3819 for (i = 0; i < 4; i++) {
3820 if (!params->wep_key[i])
3821 continue;
3822
3823 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003824 if (!nl_key ||
3825 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3826 params->wep_key[i]) ||
3827 nla_put_u32(msg, NL80211_KEY_CIPHER,
3828 params->wep_key_len[i] == 5 ?
Paul Stewart092955c2017-02-06 09:13:09 -08003829 RSN_CIPHER_SUITE_WEP40 :
3830 RSN_CIPHER_SUITE_WEP104) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003831 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3832 (i == params->wep_tx_keyidx &&
3833 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3834 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835
3836 nla_nest_end(msg, nl_key);
3837 }
3838 nla_nest_end(msg, nl_keys);
3839
3840 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003841}
3842
3843
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003844int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3845 const u8 *addr, int cmd, u16 reason_code,
Hai Shalom74f70d42019-02-11 14:42:39 -08003846 int local_state_change,
Hai Shalomc1a21442022-02-04 13:43:00 -08003847 struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003849 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003850 struct nl_msg *msg;
3851
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003852 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
3853 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3854 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3855 (local_state_change &&
3856 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3857 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003858 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003859 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003860
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003861 ret = send_and_recv(drv->global, bss->nl_connect, msg,
3862 NULL, NULL, NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003864 wpa_dbg(drv->ctx, MSG_DEBUG,
3865 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3866 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868 return ret;
3869}
3870
3871
3872static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Hai Shalom81f62d82019-07-22 12:10:00 -07003873 u16 reason_code,
Hai Shalomc1a21442022-02-04 13:43:00 -08003874 struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003875{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003876 int ret;
3877
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003878 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003879 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003880 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003881 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
Hai Shalomc1a21442022-02-04 13:43:00 -08003882 reason_code, 0, bss);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003883 /*
3884 * For locally generated disconnect, supplicant already generates a
3885 * DEAUTH event, so ignore the event from NL80211.
3886 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003887 if (ret == 0)
3888 drv->ignore_next_local_disconnect = send_event_marker(drv);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003889
3890 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003891}
3892
3893
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003894static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
Hai Shalom81f62d82019-07-22 12:10:00 -07003895 const u8 *addr, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003896{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003898 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003899
3900 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3901 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003902 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003903 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003904 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003905 return wpa_driver_nl80211_disconnect(drv, reason_code, bss);
Hai Shalom74f70d42019-02-11 14:42:39 -08003906 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3908 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003909 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003910 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
Hai Shalomc1a21442022-02-04 13:43:00 -08003911 reason_code, 0, bss);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003912 /*
3913 * For locally generated deauthenticate, supplicant already generates a
3914 * DEAUTH event, so ignore the event from NL80211.
3915 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003916 if (ret == 0)
3917 drv->ignore_next_local_deauth = send_event_marker(drv);
Hai Shalomce48b4a2018-09-05 11:41:35 -07003918
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003919 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003920}
3921
3922
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003923static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3924 struct wpa_driver_auth_params *params)
3925{
3926 int i;
3927
3928 drv->auth_freq = params->freq;
3929 drv->auth_alg = params->auth_alg;
3930 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3931 drv->auth_local_state_change = params->local_state_change;
3932 drv->auth_p2p = params->p2p;
3933
3934 if (params->bssid)
3935 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3936 else
3937 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3938
3939 if (params->ssid) {
3940 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3941 drv->auth_ssid_len = params->ssid_len;
3942 } else
3943 drv->auth_ssid_len = 0;
3944
3945
3946 os_free(drv->auth_ie);
3947 drv->auth_ie = NULL;
3948 drv->auth_ie_len = 0;
3949 if (params->ie) {
3950 drv->auth_ie = os_malloc(params->ie_len);
3951 if (drv->auth_ie) {
3952 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3953 drv->auth_ie_len = params->ie_len;
3954 }
3955 }
3956
Sunil Ravi77d572f2023-01-17 23:58:31 +00003957 if (params->mld && params->ap_mld_addr) {
3958 drv->auth_mld = params->mld;
3959 drv->auth_mld_link_id = params->mld_link_id;
3960 os_memcpy(drv->auth_ap_mld_addr, params->ap_mld_addr, ETH_ALEN);
3961 } else {
3962 drv->auth_mld = false;
3963 drv->auth_mld_link_id = -1;
3964 }
3965
Hai Shalom60840252021-02-19 19:02:11 -08003966 os_free(drv->auth_data);
3967 drv->auth_data = NULL;
3968 drv->auth_data_len = 0;
3969 if (params->auth_data) {
3970 drv->auth_data = os_memdup(params->auth_data,
3971 params->auth_data_len);
3972 if (drv->auth_data)
3973 drv->auth_data_len = params->auth_data_len;
3974 }
3975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003976 for (i = 0; i < 4; i++) {
3977 if (params->wep_key[i] && params->wep_key_len[i] &&
3978 params->wep_key_len[i] <= 16) {
3979 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3980 params->wep_key_len[i]);
3981 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3982 } else
3983 drv->auth_wep_key_len[i] = 0;
3984 }
3985}
3986
3987
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003988static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3989{
3990 struct wpa_driver_nl80211_data *drv = bss->drv;
3991
3992 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3993 return;
3994
3995 /*
3996 * Looks like we failed to unmask 11b rates previously. This could
3997 * happen, e.g., if the interface was down at the point in time when a
3998 * P2P group was terminated.
3999 */
4000 wpa_printf(MSG_DEBUG,
4001 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
4002 bss->ifname);
4003 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4004}
4005
4006
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004007static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
4008{
4009 if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
4010 return NL80211_AUTHTYPE_OPEN_SYSTEM;
4011 if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
4012 return NL80211_AUTHTYPE_SHARED_KEY;
4013 if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
4014 return NL80211_AUTHTYPE_NETWORK_EAP;
4015 if (wpa_auth_alg & WPA_AUTH_ALG_FT)
4016 return NL80211_AUTHTYPE_FT;
4017 if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
4018 return NL80211_AUTHTYPE_SAE;
4019 if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
4020 return NL80211_AUTHTYPE_FILS_SK;
4021 if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
4022 return NL80211_AUTHTYPE_FILS_SK_PFS;
4023
4024 return NL80211_AUTHTYPE_MAX;
4025}
4026
4027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004029 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031 struct wpa_driver_nl80211_data *drv = bss->drv;
4032 int ret = -1, i;
4033 struct nl_msg *msg;
4034 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004035 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004036 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004037 int is_retry;
Hai Shalomfdcde762020-04-02 11:19:20 -07004038 struct wpa_driver_set_key_params p;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004039
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004040 nl80211_unmask_11b_rates(bss);
4041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004042 is_retry = drv->retry_auth;
4043 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07004044 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004045
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004046 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004047 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004048 if (params->bssid)
4049 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4050 else
4051 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004053 nlmode = params->p2p ?
4054 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4055 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004056 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004057 return -1;
4058
4059retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4061 drv->ifindex);
4062
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004063 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
4064 if (!msg)
4065 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004066
Hai Shalomfdcde762020-04-02 11:19:20 -07004067 os_memset(&p, 0, sizeof(p));
4068 p.ifname = bss->ifname;
4069 p.alg = WPA_ALG_WEP;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004070 p.link_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071 for (i = 0; i < 4; i++) {
4072 if (!params->wep_key[i])
4073 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07004074 p.key_idx = i;
4075 p.set_tx = i == params->wep_tx_keyidx;
4076 p.key = params->wep_key[i];
4077 p.key_len = params->wep_key_len[i];
4078 p.key_flag = i == params->wep_tx_keyidx ?
4079 KEY_FLAG_GROUP_RX_TX_DEFAULT :
4080 KEY_FLAG_GROUP_RX_TX;
4081 wpa_driver_nl80211_set_key(bss, &p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 if (params->wep_tx_keyidx != i)
4083 continue;
4084 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004085 params->wep_key[i], params->wep_key_len[i]))
4086 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 }
4088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089 if (params->bssid) {
4090 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4091 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004092 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4093 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 }
4095 if (params->freq) {
4096 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004097 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
4098 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004099 }
4100 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004101 wpa_printf(MSG_DEBUG, " * SSID=%s",
4102 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004103 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4104 params->ssid))
4105 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106 }
4107 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004108 if (params->ie &&
4109 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
4110 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004111 if (params->auth_data) {
4112 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
4113 params->auth_data_len);
4114 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
4115 params->auth_data))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004116 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004117 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004118 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004120 if (type == NL80211_AUTHTYPE_MAX ||
4121 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004122 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123 if (params->local_state_change) {
4124 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004125 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
4126 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127 }
4128
Sunil Ravi77d572f2023-01-17 23:58:31 +00004129 if (params->mld && params->ap_mld_addr) {
4130 wpa_printf(MSG_DEBUG, " * MLD: link_id=%u, MLD addr=" MACSTR,
4131 params->mld_link_id, MAC2STR(params->ap_mld_addr));
4132
4133 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
4134 params->mld_link_id) ||
4135 nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
4136 params->ap_mld_addr))
4137 goto fail;
4138 }
4139
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004140 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141 msg = NULL;
4142 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004143 wpa_dbg(drv->ctx, MSG_DEBUG,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004144 "nl80211: MLME command failed (auth): count=%d ret=%d (%s)",
4145 count, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146 count++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004147 if ((ret == -EALREADY || ret == -EEXIST) && count == 1 &&
4148 params->bssid && !params->local_state_change) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149 /*
4150 * mac80211 does not currently accept new
4151 * authentication if we are already authenticated. As a
4152 * workaround, force deauthentication and try again.
4153 */
4154 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4155 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07004156 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157 wpa_driver_nl80211_deauthenticate(
4158 bss, params->bssid,
4159 WLAN_REASON_PREV_AUTH_NOT_VALID);
4160 nlmsg_free(msg);
4161 goto retry;
4162 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004163
4164 if (ret == -ENOENT && params->freq && !is_retry) {
4165 /*
4166 * cfg80211 has likely expired the BSS entry even
4167 * though it was previously available in our internal
4168 * BSS table. To recover quickly, start a single
4169 * channel scan on the specified channel.
4170 */
4171 struct wpa_driver_scan_params scan;
4172 int freqs[2];
4173
4174 os_memset(&scan, 0, sizeof(scan));
4175 scan.num_ssids = 1;
4176 if (params->ssid) {
4177 scan.ssids[0].ssid = params->ssid;
4178 scan.ssids[0].ssid_len = params->ssid_len;
4179 }
4180 freqs[0] = params->freq;
4181 freqs[1] = 0;
4182 scan.freqs = freqs;
4183 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
4184 "channel scan to refresh cfg80211 BSS "
4185 "entry");
4186 ret = wpa_driver_nl80211_scan(bss, &scan);
4187 if (ret == 0) {
4188 nl80211_copy_auth_params(drv, params);
4189 drv->scan_for_auth = 1;
4190 }
4191 } else if (is_retry) {
4192 /*
4193 * Need to indicate this with an event since the return
4194 * value from the retry is not delivered to core code.
4195 */
4196 union wpa_event_data event;
4197 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
4198 "failed");
4199 os_memset(&event, 0, sizeof(event));
4200 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
4201 ETH_ALEN);
4202 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
4203 &event);
4204 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004205 } else {
4206 wpa_printf(MSG_DEBUG,
4207 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004210fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004211 nlmsg_free(msg);
4212 return ret;
4213}
4214
4215
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004216int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004217{
4218 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004219 struct i802_bss *bss = drv->first_bss;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004220 u8 ap_mld_addr[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004221 int i;
4222
4223 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
4224
4225 os_memset(&params, 0, sizeof(params));
4226 params.freq = drv->auth_freq;
4227 params.auth_alg = drv->auth_alg;
4228 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
4229 params.local_state_change = drv->auth_local_state_change;
4230 params.p2p = drv->auth_p2p;
4231
4232 if (!is_zero_ether_addr(drv->auth_bssid_))
4233 params.bssid = drv->auth_bssid_;
4234
4235 if (drv->auth_ssid_len) {
4236 params.ssid = drv->auth_ssid;
4237 params.ssid_len = drv->auth_ssid_len;
4238 }
4239
4240 params.ie = drv->auth_ie;
4241 params.ie_len = drv->auth_ie_len;
Hai Shalom60840252021-02-19 19:02:11 -08004242 params.auth_data = drv->auth_data;
4243 params.auth_data_len = drv->auth_data_len;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004244 params.mld = drv->auth_mld;
4245 params.mld_link_id = drv->auth_mld_link_id;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004246 if (drv->auth_mld) {
4247 os_memcpy(ap_mld_addr, drv->auth_ap_mld_addr, ETH_ALEN);
4248 params.ap_mld_addr = ap_mld_addr;
4249 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004250
4251 for (i = 0; i < 4; i++) {
4252 if (drv->auth_wep_key_len[i]) {
4253 params.wep_key[i] = drv->auth_wep_key[i];
4254 params.wep_key_len[i] = drv->auth_wep_key_len[i];
4255 }
4256 }
4257
4258 drv->retry_auth = 1;
4259 return wpa_driver_nl80211_authenticate(bss, &params);
4260}
4261
4262
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004263struct i802_link * nl80211_get_link(struct i802_bss *bss, s8 link_id)
4264{
Sunil Ravi99c035e2024-07-12 01:42:03 +00004265 if (link_id < 0 || link_id >= MAX_NUM_MLD_LINKS)
4266 return bss->flink;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004267
Sunil Ravi99c035e2024-07-12 01:42:03 +00004268 if (BIT(link_id) & bss->valid_links)
4269 return &bss->links[link_id];
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004270
4271 return bss->flink;
4272}
4273
4274
4275static void nl80211_link_set_freq(struct i802_bss *bss, s8 link_id, int freq)
4276{
4277 struct i802_link *link = nl80211_get_link(bss, link_id);
4278
4279 link->freq = freq;
4280}
4281
4282
4283static int nl80211_get_link_freq(struct i802_bss *bss, const u8 *addr,
4284 bool bss_freq_debug)
4285{
Sunil Ravi99c035e2024-07-12 01:42:03 +00004286 u8 i;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004287
Sunil Ravi99c035e2024-07-12 01:42:03 +00004288 for_each_link(bss->valid_links, i) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004289 if (ether_addr_equal(bss->links[i].addr, addr)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004290 wpa_printf(MSG_DEBUG,
4291 "nl80211: Use link freq=%d for address "
4292 MACSTR,
4293 bss->links[i].freq, MAC2STR(addr));
4294 return bss->links[i].freq;
4295 }
4296 }
4297
4298 if (bss_freq_debug)
4299 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
4300 bss->flink->freq);
4301
4302 return bss->flink->freq;
4303}
4304
4305
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004306static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
4307 size_t data_len, int noack,
4308 unsigned int freq, int no_cck,
4309 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004310 unsigned int wait_time,
4311 const u16 *csa_offs,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004312 size_t csa_offs_len, int no_encrypt,
4313 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004314{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004315 struct wpa_driver_nl80211_data *drv = bss->drv;
4316 struct ieee80211_mgmt *mgmt;
Hai Shalomfdcde762020-04-02 11:19:20 -07004317 int encrypt = !no_encrypt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 u16 fc;
Hai Shalomfdcde762020-04-02 11:19:20 -07004319 int use_cookie = 1;
4320 int res;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004321 struct i802_link *link = nl80211_get_link(bss, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004322
4323 mgmt = (struct ieee80211_mgmt *) data;
4324 fc = le_to_host16(mgmt->frame_control);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004325 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da=" MACSTR " sa=" MACSTR
4326 " bssid=" MACSTR
Hai Shalomfdcde762020-04-02 11:19:20 -07004327 " 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 +00004328 MAC2STR(mgmt->da), MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid),
4329 noack, freq, no_cck, offchanok, wait_time,
Hai Shalomfdcde762020-04-02 11:19:20 -07004330 no_encrypt, fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004331
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004332 if ((is_sta_interface(drv->nlmode) ||
4333 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4335 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
4336 /*
4337 * The use of last_mgmt_freq is a bit of a hack,
4338 * but it works due to the single-threaded nature
4339 * of wpa_supplicant.
4340 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07004341 if (freq == 0) {
4342 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
4343 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004344 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07004345 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004346 wait_time = 0;
4347 use_cookie = 0;
4348 no_cck = 1;
4349 offchanok = 1;
4350 goto send_frame_cmd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004351 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004352
4353 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004354 unsigned int link_freq = nl80211_get_link_freq(bss, mgmt->sa,
4355 !freq);
4356
4357 if (!freq)
4358 freq = link_freq;
4359
4360 if (freq == link_freq)
Hai Shalomfdcde762020-04-02 11:19:20 -07004361 wait_time = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004362
Hai Shalomfdcde762020-04-02 11:19:20 -07004363 goto send_frame_cmd;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07004364 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07004365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4367 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
4368 /*
4369 * Only one of the authentication frame types is encrypted.
4370 * In order for static WEP encryption to work properly (i.e.,
4371 * to not encrypt the frame), we need to tell mac80211 about
4372 * the frames that must not be encrypted.
4373 */
4374 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
4375 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
4376 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
4377 encrypt = 0;
4378 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004379
Hai Shalom60840252021-02-19 19:02:11 -08004380 if (is_sta_interface(drv->nlmode) &&
Hai Shalomfdcde762020-04-02 11:19:20 -07004381 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4382 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
Hai Shalom60840252021-02-19 19:02:11 -08004383 if (freq == 0 &&
4384 (drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
4385 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
4386 freq = nl80211_get_assoc_freq(drv);
4387 wpa_printf(MSG_DEBUG,
4388 "nl80211: send_mlme - Use assoc_freq=%u for external auth",
4389 freq);
4390 }
4391
4392 /* Allow off channel for PASN authentication */
4393 if (data_len >= IEEE80211_HDRLEN + 2 &&
4394 WPA_GET_LE16(data + IEEE80211_HDRLEN) == WLAN_AUTH_PASN &&
4395 !offchanok) {
4396 wpa_printf(MSG_DEBUG,
4397 "nl80211: send_mlme: allow off channel for PASN");
4398 offchanok = 1;
4399 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004400 }
4401
Hai Shalomc1a21442022-02-04 13:43:00 -08004402#ifdef CONFIG_PASN
4403 if (is_sta_interface(drv->nlmode) &&
4404 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4405 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_DEAUTH) {
4406 wpa_printf(MSG_DEBUG,
4407 "nl80211: send_mlme: allow Deauthentication frame for PASN");
4408
4409 use_cookie = 0;
4410 offchanok = 1;
4411 goto send_frame_cmd;
4412 }
4413#endif /* CONFIG_PASN */
4414
Hai Shalomfdcde762020-04-02 11:19:20 -07004415 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
4416 freq = nl80211_get_assoc_freq(drv);
4417 wpa_printf(MSG_DEBUG,
4418 "nl80211: send_mlme - Use assoc_freq=%u for IBSS",
4419 freq);
4420 }
4421 if (freq == 0) {
4422 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - Use bss->freq=%u",
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004423 link->freq);
4424 freq = link->freq;
Hai Shalomfdcde762020-04-02 11:19:20 -07004425 }
4426
Hai Shalomc1a21442022-02-04 13:43:00 -08004427 if (drv->use_monitor && is_ap_interface(drv->nlmode)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004428 wpa_printf(MSG_DEBUG,
4429 "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004430 freq, link->freq);
Hai Shalomfdcde762020-04-02 11:19:20 -07004431 return nl80211_send_monitor(drv, data, data_len, encrypt,
4432 noack);
4433 }
4434
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004435 if ((noack || WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
4436 WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION) &&
4437 link_id == NL80211_DRV_LINK_ID_NA)
Hai Shalomfdcde762020-04-02 11:19:20 -07004438 use_cookie = 0;
4439send_frame_cmd:
4440#ifdef CONFIG_TESTING_OPTIONS
4441 if (no_encrypt && !encrypt && !drv->use_monitor) {
4442 wpa_printf(MSG_DEBUG,
4443 "nl80211: Request to send an unencrypted frame - use a monitor interface for this");
4444 if (nl80211_create_monitor_interface(drv) < 0)
4445 return -1;
4446 res = nl80211_send_monitor(drv, data, data_len, encrypt,
4447 noack);
4448 nl80211_remove_monitor_interface(drv);
4449 return res;
4450 }
4451#endif /* CONFIG_TESTING_OPTIONS */
4452
4453 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame_cmd");
4454 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, data_len,
4455 use_cookie, no_cck, noack, offchanok,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004456 csa_offs, csa_offs_len, link_id);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004457 if (!res)
4458 drv->send_frame_link_id = link_id;
Hai Shalomfdcde762020-04-02 11:19:20 -07004459
4460 return res;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004461}
4462
4463
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004464static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
4465{
4466 u8 rates[NL80211_MAX_SUPP_RATES];
4467 u8 rates_len = 0;
4468 int i;
4469
4470 if (!basic_rates)
4471 return 0;
4472
4473 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
4474 rates[rates_len++] = basic_rates[i] / 5;
4475
4476 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
4477}
4478
4479
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004480static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
4481 int slot, int ht_opmode, int ap_isolate,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004482 const int *basic_rates, int link_id)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004483{
4484 struct wpa_driver_nl80211_data *drv = bss->drv;
4485 struct nl_msg *msg;
4486
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004487 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
4488 (cts >= 0 &&
4489 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
4490 (preamble >= 0 &&
4491 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
4492 (slot >= 0 &&
4493 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
4494 (ht_opmode >= 0 &&
4495 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
4496 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004497 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004498 nl80211_put_basic_rates(msg, basic_rates) ||
4499 (link_id != NL80211_DRV_LINK_ID_NA &&
4500 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004501 nlmsg_free(msg);
4502 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004503 }
4504
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004505 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004506}
4507
4508
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004509static int wpa_driver_nl80211_set_acl(void *priv,
4510 struct hostapd_acl_params *params)
4511{
4512 struct i802_bss *bss = priv;
4513 struct wpa_driver_nl80211_data *drv = bss->drv;
4514 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004515 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004516 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004517 int ret;
Hai Shalomc1a21442022-02-04 13:43:00 -08004518 size_t acl_nla_sz, acl_nlmsg_sz, nla_sz, nlmsg_sz;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004519
4520 if (!(drv->capa.max_acl_mac_addrs))
4521 return -ENOTSUP;
4522
4523 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
4524 return -ENOTSUP;
4525
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004526 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
4527 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
4528
Hai Shalomc1a21442022-02-04 13:43:00 -08004529 acl_nla_sz = nla_total_size(ETH_ALEN) * params->num_mac_acl;
4530 acl_nlmsg_sz = nlmsg_total_size(acl_nla_sz);
4531 acl = nlmsg_alloc_size(acl_nlmsg_sz);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004532 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004533 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004534 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004535 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
4536 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004537 return -ENOMEM;
4538 }
4539 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004540
Hai Shalomc1a21442022-02-04 13:43:00 -08004541 /*
4542 * genetlink message header (Length of user header is 0) +
4543 * u32 attr: NL80211_ATTR_IFINDEX +
4544 * u32 attr: NL80211_ATTR_ACL_POLICY +
4545 * nested acl attr
4546 */
4547 nla_sz = GENL_HDRLEN +
4548 nla_total_size(4) * 2 +
4549 nla_total_size(acl_nla_sz);
4550 nlmsg_sz = nlmsg_total_size(nla_sz);
4551 if (!(msg = nl80211_ifindex_msg_build(drv, nlmsg_alloc_size(nlmsg_sz),
4552 drv->ifindex, 0,
4553 NL80211_CMD_SET_MAC_ACL)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004554 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
4555 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
4556 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
4557 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
4558 nlmsg_free(msg);
4559 nlmsg_free(acl);
4560 return -ENOMEM;
4561 }
4562 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004563
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004564 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004565 if (ret) {
4566 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
4567 ret, strerror(-ret));
4568 }
4569
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004570 return ret;
4571}
4572
4573
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004574static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
4575{
4576 if (beacon_int > 0) {
4577 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
4578 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
4579 beacon_int);
4580 }
4581
4582 return 0;
4583}
4584
4585
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004586static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
4587{
4588 if (dtim_period > 0) {
4589 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
4590 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
4591 }
4592
4593 return 0;
4594}
4595
4596
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004597#ifdef CONFIG_MESH
4598static int nl80211_set_mesh_config(void *priv,
4599 struct wpa_driver_mesh_bss_params *params)
4600{
4601 struct i802_bss *bss = priv;
4602 struct wpa_driver_nl80211_data *drv = bss->drv;
4603 struct nl_msg *msg;
4604 int ret;
4605
4606 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
4607 if (!msg)
4608 return -1;
4609
4610 ret = nl80211_put_mesh_config(msg, params);
4611 if (ret < 0) {
4612 nlmsg_free(msg);
4613 return ret;
4614 }
4615
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004616 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004617 if (ret) {
4618 wpa_printf(MSG_ERROR,
4619 "nl80211: Mesh config set failed: %d (%s)",
4620 ret, strerror(-ret));
4621 return ret;
4622 }
4623 return 0;
4624}
4625#endif /* CONFIG_MESH */
4626
4627
Hai Shalom60840252021-02-19 19:02:11 -08004628static int nl80211_put_beacon_rate(struct nl_msg *msg, u64 flags, u64 flags2,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004629 struct wpa_driver_ap_params *params)
4630{
4631 struct nlattr *bands, *band;
4632 struct nl80211_txrate_vht vht_rate;
Hai Shalom60840252021-02-19 19:02:11 -08004633 struct nl80211_txrate_he he_rate;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004634
4635 if (!params->freq ||
4636 (params->beacon_rate == 0 &&
4637 params->rate_type == BEACON_RATE_LEGACY))
4638 return 0;
4639
4640 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
4641 if (!bands)
4642 return -1;
4643
4644 switch (params->freq->mode) {
4645 case HOSTAPD_MODE_IEEE80211B:
4646 case HOSTAPD_MODE_IEEE80211G:
4647 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
4648 break;
4649 case HOSTAPD_MODE_IEEE80211A:
Hai Shalom60840252021-02-19 19:02:11 -08004650 if (is_6ghz_freq(params->freq->freq))
4651 band = nla_nest_start(msg, NL80211_BAND_6GHZ);
4652 else
4653 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004654 break;
4655 case HOSTAPD_MODE_IEEE80211AD:
4656 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
4657 break;
4658 default:
4659 return 0;
4660 }
4661
4662 if (!band)
4663 return -1;
4664
4665 os_memset(&vht_rate, 0, sizeof(vht_rate));
Hai Shalom60840252021-02-19 19:02:11 -08004666 os_memset(&he_rate, 0, sizeof(he_rate));
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004667
4668 switch (params->rate_type) {
4669 case BEACON_RATE_LEGACY:
4670 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
4671 wpa_printf(MSG_INFO,
4672 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
4673 return -1;
4674 }
4675
4676 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004677 (u8) (params->beacon_rate / 5)) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004678 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
4679 (params->freq->vht_enabled &&
4680 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4681 &vht_rate)))
4682 return -1;
4683
4684 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
4685 params->beacon_rate);
4686 break;
4687 case BEACON_RATE_HT:
4688 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
4689 wpa_printf(MSG_INFO,
4690 "nl80211: Driver does not support setting Beacon frame rate (HT)");
4691 return -1;
4692 }
4693 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
4694 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
4695 (params->freq->vht_enabled &&
4696 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4697 &vht_rate)))
4698 return -1;
4699 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
4700 params->beacon_rate);
4701 break;
4702 case BEACON_RATE_VHT:
4703 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
4704 wpa_printf(MSG_INFO,
4705 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
4706 return -1;
4707 }
4708 vht_rate.mcs[0] = BIT(params->beacon_rate);
4709 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
4710 return -1;
4711 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
4712 return -1;
4713 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4714 &vht_rate))
4715 return -1;
4716 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
4717 params->beacon_rate);
4718 break;
Hai Shalom60840252021-02-19 19:02:11 -08004719 case BEACON_RATE_HE:
4720 if (!(flags2 & WPA_DRIVER_FLAGS2_BEACON_RATE_HE)) {
4721 wpa_printf(MSG_INFO,
4722 "nl80211: Driver does not support setting Beacon frame rate (HE)");
4723 return -1;
4724 }
4725 he_rate.mcs[0] = BIT(params->beacon_rate);
4726 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
4727 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
4728 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4729 &vht_rate) ||
4730 nla_put(msg, NL80211_TXRATE_HE, sizeof(he_rate), &he_rate))
4731 return -1;
4732 wpa_printf(MSG_DEBUG, " * beacon_rate = HE-MCS %u",
4733 params->beacon_rate);
4734 break;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004735 }
4736
4737 nla_nest_end(msg, band);
4738 nla_nest_end(msg, bands);
4739
4740 return 0;
4741}
4742
4743
4744static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
4745 int multicast_to_unicast)
4746{
4747 struct wpa_driver_nl80211_data *drv = bss->drv;
4748 struct nl_msg *msg;
4749 int ret;
4750
4751 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
4752 if (!msg ||
4753 (multicast_to_unicast &&
4754 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
4755 wpa_printf(MSG_ERROR,
4756 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
4757 bss->ifname);
4758 nlmsg_free(msg);
4759 return -ENOBUFS;
4760 }
4761
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004762 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004763
4764 switch (ret) {
4765 case 0:
4766 wpa_printf(MSG_DEBUG,
4767 "nl80211: multicast to unicast %s on interface %s",
4768 multicast_to_unicast ? "enabled" : "disabled",
4769 bss->ifname);
4770 break;
4771 case -EOPNOTSUPP:
4772 if (!multicast_to_unicast)
4773 break;
4774 wpa_printf(MSG_INFO,
4775 "nl80211: multicast to unicast not supported on interface %s",
4776 bss->ifname);
4777 break;
4778 default:
4779 wpa_printf(MSG_ERROR,
4780 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
4781 multicast_to_unicast ? "enabling" : "disabling",
4782 ret, strerror(-ret), bss->ifname);
4783 break;
4784 }
4785
4786 return ret;
4787}
4788
4789
Hai Shalom60840252021-02-19 19:02:11 -08004790#ifdef CONFIG_SAE
Sunil Ravi77d572f2023-01-17 23:58:31 +00004791static int nl80211_put_sae_pwe(struct nl_msg *msg, enum sae_pwe pwe)
Hai Shalom60840252021-02-19 19:02:11 -08004792{
4793 u8 sae_pwe;
4794
4795 wpa_printf(MSG_DEBUG, "nl802111: sae_pwe=%d", pwe);
Sunil Ravi77d572f2023-01-17 23:58:31 +00004796 if (pwe == SAE_PWE_HUNT_AND_PECK)
Hai Shalom60840252021-02-19 19:02:11 -08004797 sae_pwe = NL80211_SAE_PWE_HUNT_AND_PECK;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004798 else if (pwe == SAE_PWE_HASH_TO_ELEMENT)
Hai Shalom60840252021-02-19 19:02:11 -08004799 sae_pwe = NL80211_SAE_PWE_HASH_TO_ELEMENT;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004800 else if (pwe == SAE_PWE_BOTH)
Hai Shalom60840252021-02-19 19:02:11 -08004801 sae_pwe = NL80211_SAE_PWE_BOTH;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004802 else if (pwe == SAE_PWE_FORCE_HUNT_AND_PECK)
Hai Shalom60840252021-02-19 19:02:11 -08004803 return 0; /* special test mode */
4804 else
4805 return -1;
4806 if (nla_put_u8(msg, NL80211_ATTR_SAE_PWE, sae_pwe))
4807 return -1;
4808
4809 return 0;
4810}
4811#endif /* CONFIG_SAE */
4812
4813
4814#ifdef CONFIG_FILS
4815static int nl80211_fils_discovery(struct i802_bss *bss, struct nl_msg *msg,
4816 struct wpa_driver_ap_params *params)
4817{
4818 struct nlattr *attr;
4819
4820 if (!bss->drv->fils_discovery) {
4821 wpa_printf(MSG_ERROR,
4822 "nl80211: Driver does not support FILS Discovery frame transmission for %s",
4823 bss->ifname);
4824 return -1;
4825 }
4826
4827 attr = nla_nest_start(msg, NL80211_ATTR_FILS_DISCOVERY);
4828 if (!attr ||
4829 nla_put_u32(msg, NL80211_FILS_DISCOVERY_ATTR_INT_MIN,
4830 params->fd_min_int) ||
4831 nla_put_u32(msg, NL80211_FILS_DISCOVERY_ATTR_INT_MAX,
4832 params->fd_max_int) ||
4833 (params->fd_frame_tmpl &&
4834 nla_put(msg, NL80211_FILS_DISCOVERY_ATTR_TMPL,
4835 params->fd_frame_tmpl_len, params->fd_frame_tmpl)))
4836 return -1;
4837
4838 nla_nest_end(msg, attr);
4839 return 0;
4840}
4841#endif /* CONFIG_FILS */
4842
4843
4844#ifdef CONFIG_IEEE80211AX
Sunil Ravi77d572f2023-01-17 23:58:31 +00004845
Hai Shalom60840252021-02-19 19:02:11 -08004846static int nl80211_unsol_bcast_probe_resp(struct i802_bss *bss,
4847 struct nl_msg *msg,
Sunil Ravi7f769292024-07-23 22:21:32 +00004848 struct unsol_bcast_probe_resp *ubpr)
Hai Shalom60840252021-02-19 19:02:11 -08004849{
4850 struct nlattr *attr;
4851
4852 if (!bss->drv->unsol_bcast_probe_resp) {
4853 wpa_printf(MSG_ERROR,
4854 "nl80211: Driver does not support unsolicited broadcast Probe Response frame transmission for %s",
4855 bss->ifname);
4856 return -1;
4857 }
4858
4859 wpa_printf(MSG_DEBUG,
4860 "nl80211: Unsolicited broadcast Probe Response frame interval: %u",
Sunil Ravi7f769292024-07-23 22:21:32 +00004861 ubpr->unsol_bcast_probe_resp_interval);
Hai Shalom60840252021-02-19 19:02:11 -08004862 attr = nla_nest_start(msg, NL80211_ATTR_UNSOL_BCAST_PROBE_RESP);
4863 if (!attr ||
4864 nla_put_u32(msg, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT,
Sunil Ravi7f769292024-07-23 22:21:32 +00004865 ubpr->unsol_bcast_probe_resp_interval) ||
4866 (ubpr->unsol_bcast_probe_resp_tmpl &&
Hai Shalom60840252021-02-19 19:02:11 -08004867 nla_put(msg, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL,
Sunil Ravi7f769292024-07-23 22:21:32 +00004868 ubpr->unsol_bcast_probe_resp_tmpl_len,
4869 ubpr->unsol_bcast_probe_resp_tmpl)))
Hai Shalom60840252021-02-19 19:02:11 -08004870 return -1;
4871
4872 nla_nest_end(msg, attr);
4873 return 0;
4874}
Sunil Ravi77d572f2023-01-17 23:58:31 +00004875
4876
4877static int nl80211_mbssid(struct nl_msg *msg,
4878 struct wpa_driver_ap_params *params)
4879{
4880 struct nlattr *config, *elems;
4881 int ifidx;
4882
4883 if (!params->mbssid_tx_iface)
4884 return 0;
4885
4886 config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
4887 if (!config ||
4888 nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_INDEX,
4889 params->mbssid_index))
4890 return -1;
4891
4892 if (params->mbssid_tx_iface) {
4893 ifidx = if_nametoindex(params->mbssid_tx_iface);
4894 if (ifidx <= 0 ||
4895 nla_put_u32(msg, NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX,
4896 ifidx))
4897 return -1;
4898 }
4899
4900 if (params->ema && nla_put_flag(msg, NL80211_MBSSID_CONFIG_ATTR_EMA))
4901 return -1;
4902
4903 nla_nest_end(msg, config);
4904
4905 if (params->mbssid_elem_count && params->mbssid_elem_len &&
4906 params->mbssid_elem_offset && *params->mbssid_elem_offset) {
4907 u8 i, **offs = params->mbssid_elem_offset;
4908
4909 elems = nla_nest_start(msg, NL80211_ATTR_MBSSID_ELEMS);
4910 if (!elems)
4911 return -1;
4912
4913 for (i = 0; i < params->mbssid_elem_count - 1; i++) {
4914 if (nla_put(msg, i + 1, offs[i + 1] - offs[i], offs[i]))
4915 return -1;
4916 }
4917
4918 if (nla_put(msg, i + 1,
4919 *offs + params->mbssid_elem_len - offs[i],
4920 offs[i]))
4921 return -1;
4922
4923 nla_nest_end(msg, elems);
4924 }
4925
Sunil Ravi640215c2023-06-28 23:08:09 +00004926 if (!params->ema)
4927 return 0;
4928
4929 if (params->rnr_elem_count && params->rnr_elem_len &&
4930 params->rnr_elem_offset && *params->rnr_elem_offset) {
4931 u8 i, **offs = params->rnr_elem_offset;
4932
4933 elems = nla_nest_start(msg, NL80211_ATTR_EMA_RNR_ELEMS);
4934 if (!elems)
4935 return -1;
4936
4937 for (i = 0; i < params->rnr_elem_count - 1; i++) {
4938 if (nla_put(msg, i + 1, offs[i + 1] - offs[i], offs[i]))
4939 return -1;
4940 }
4941
4942 if (nla_put(msg, i + 1, *offs + params->rnr_elem_len - offs[i],
4943 offs[i]))
4944 return -1;
4945 nla_nest_end(msg, elems);
4946 }
4947
Sunil Ravi77d572f2023-01-17 23:58:31 +00004948 return 0;
4949}
4950
Hai Shalom60840252021-02-19 19:02:11 -08004951#endif /* CONFIG_IEEE80211AX */
4952
4953
Sunil Ravi640215c2023-06-28 23:08:09 +00004954#ifdef CONFIG_DRIVER_NL80211_QCA
4955static void qca_set_allowed_ap_freqs(struct wpa_driver_nl80211_data *drv,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004956 const int *freqs, int num_freqs,
4957 int link_id)
Sunil Ravi640215c2023-06-28 23:08:09 +00004958{
4959 struct nl_msg *msg;
4960 struct nlattr *params, *freqs_list;
4961 int i, ret;
4962
4963 if (!drv->set_wifi_conf_vendor_cmd_avail || !drv->qca_ap_allowed_freqs)
4964 return;
4965
4966 wpa_printf(MSG_DEBUG, "nl80211: Set AP allowed frequency list");
4967
4968 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
4969 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
4970 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
4971 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
4972 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)))
4973 goto err;
4974
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004975 if (link_id != NL80211_DRV_LINK_ID_NA &&
4976 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_MLO_LINK_ID, link_id))
4977 goto err;
4978
Sunil Ravi640215c2023-06-28 23:08:09 +00004979 freqs_list = nla_nest_start(
4980 msg, QCA_WLAN_VENDOR_ATTR_CONFIG_AP_ALLOWED_FREQ_LIST);
4981 if (!freqs_list)
4982 goto err;
4983
4984 for (i = 0; i < num_freqs; i++) {
4985 if (nla_put_u32(msg, i, freqs[i]))
4986 goto err;
4987 }
4988
4989 nla_nest_end(msg, freqs_list);
4990 nla_nest_end(msg, params);
4991
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004992 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi640215c2023-06-28 23:08:09 +00004993 if (ret)
4994 wpa_printf(MSG_ERROR,
4995 "nl80211: Failed set AP alllowed frequency list: %d (%s)",
4996 ret, strerror(-ret));
4997
4998 return;
4999err:
5000 nlmsg_free(msg);
5001}
5002#endif /* CONFIG_DRIVER_NL80211_QCA */
5003
5004
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005005static int nl80211_put_freq_params(struct nl_msg *msg,
5006 const struct hostapd_freq_params *freq)
5007{
5008 enum hostapd_hw_mode hw_mode;
5009 int is_24ghz;
5010 u8 channel;
5011
5012 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
5013 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
5014 return -ENOBUFS;
5015
5016 wpa_printf(MSG_DEBUG, " * eht_enabled=%d", freq->eht_enabled);
5017 wpa_printf(MSG_DEBUG, " * he_enabled=%d", freq->he_enabled);
5018 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
5019 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
5020 wpa_printf(MSG_DEBUG, " * radar_background=%d",
5021 freq->radar_background);
5022
5023 hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
5024 is_24ghz = hw_mode == HOSTAPD_MODE_IEEE80211G ||
5025 hw_mode == HOSTAPD_MODE_IEEE80211B;
5026
5027 if (freq->vht_enabled ||
5028 ((freq->he_enabled || freq->eht_enabled) && !is_24ghz)) {
5029 enum nl80211_chan_width cw;
5030
5031 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
5032 switch (freq->bandwidth) {
5033 case 20:
5034 cw = NL80211_CHAN_WIDTH_20;
5035 break;
5036 case 40:
5037 cw = NL80211_CHAN_WIDTH_40;
5038 break;
5039 case 80:
5040 if (freq->center_freq2)
5041 cw = NL80211_CHAN_WIDTH_80P80;
5042 else
5043 cw = NL80211_CHAN_WIDTH_80;
5044 break;
5045 case 160:
5046 cw = NL80211_CHAN_WIDTH_160;
5047 break;
5048 case 320:
5049 cw = NL80211_CHAN_WIDTH_320;
5050 break;
5051 default:
5052 return -EINVAL;
5053 }
5054
5055 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
5056 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
5057 freq->center_freq1);
5058 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
5059 freq->center_freq2);
5060 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
5061 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
5062 freq->center_freq1) ||
5063 (freq->center_freq2 &&
5064 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
5065 freq->center_freq2)))
5066 return -ENOBUFS;
5067 } else if (freq->ht_enabled) {
5068 enum nl80211_channel_type ct;
5069
5070 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
5071 freq->sec_channel_offset);
5072 switch (freq->sec_channel_offset) {
5073 case -1:
5074 ct = NL80211_CHAN_HT40MINUS;
5075 break;
5076 case 1:
5077 ct = NL80211_CHAN_HT40PLUS;
5078 break;
5079 default:
5080 ct = NL80211_CHAN_HT20;
5081 break;
5082 }
5083
5084 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
5085 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
5086 return -ENOBUFS;
5087 } else if (freq->edmg.channels && freq->edmg.bw_config) {
5088 wpa_printf(MSG_DEBUG,
5089 " * EDMG configuration: channels=0x%x bw_config=%d",
5090 freq->edmg.channels, freq->edmg.bw_config);
5091 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_CHANNELS,
5092 freq->edmg.channels) ||
5093 nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_BW_CONFIG,
5094 freq->edmg.bw_config))
5095 return -1;
5096 } else {
5097 wpa_printf(MSG_DEBUG, " * channel_type=%d",
5098 NL80211_CHAN_NO_HT);
5099 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5100 NL80211_CHAN_NO_HT))
5101 return -ENOBUFS;
5102 }
5103 if (freq->radar_background &&
5104 nla_put_flag(msg, NL80211_ATTR_RADAR_BACKGROUND))
5105 return -ENOBUFS;
5106
5107 return 0;
5108}
5109
5110
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005111static int wpa_driver_nl80211_set_ap(void *priv,
5112 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005113{
5114 struct i802_bss *bss = priv;
5115 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005116 struct i802_link *link = bss->flink;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005117 struct nl_msg *msg;
5118 u8 cmd = NL80211_CMD_NEW_BEACON;
Hai Shalom74f70d42019-02-11 14:42:39 -08005119 int ret = -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005121 int num_suites;
Hai Shalomfdcde762020-04-02 11:19:20 -07005122 u32 suites[20], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005123 u32 ver;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005124#ifdef CONFIG_MESH
5125 struct wpa_driver_mesh_bss_params mesh_params;
5126#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005127
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005128 if (params->mld_ap) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00005129 if (!nl80211_link_valid(bss->valid_links,
5130 params->mld_link_id)) {
5131 wpa_printf(MSG_DEBUG,
5132 "nl80211: Link ID=%u invalid (valid: 0x%04x)",
5133 params->mld_link_id, bss->valid_links);
Sunil Ravi88611412024-06-28 17:34:56 +00005134 return -EINVAL;
5135 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00005136
5137 link = nl80211_get_link(bss, params->mld_link_id);
5138 } else if (bss->valid_links) {
5139 wpa_printf(MSG_DEBUG, "nl80211: MLD configuration expected");
5140 return -EINVAL;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005141 }
5142
5143 beacon_set = params->reenable ? 0 : link->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
5146 beacon_set);
5147 if (beacon_set)
5148 cmd = NL80211_CMD_SET_BEACON;
Paul Stewart092955c2017-02-06 09:13:09 -08005149 else if (!drv->device_ap_sme && !drv->use_monitor &&
5150 !nl80211_get_wiphy_data_ap(bss))
5151 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005152
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005153 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
5154 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005155 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
5156 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005157 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005158 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08005159 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
5160 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005161 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Hai Shalom74f70d42019-02-11 14:42:39 -08005162 wpa_printf(MSG_DEBUG, "nl80211: ssid=%s",
5163 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005164 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
5165 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
5166 params->head) ||
5167 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
5168 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005169 nl80211_put_beacon_int(msg, params->beacon_int) ||
Hai Shalom60840252021-02-19 19:02:11 -08005170 nl80211_put_beacon_rate(msg, drv->capa.flags, drv->capa.flags2,
5171 params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005172 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005173 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5174 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005175
5176 if (params->mld_ap) {
5177 wpa_printf(MSG_DEBUG, "nl80211: link_id=%u",
5178 params->mld_link_id);
5179
5180 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
Sunil Ravi99c035e2024-07-12 01:42:03 +00005181 params->mld_link_id))
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005182 goto fail;
5183
Sunil Ravi99c035e2024-07-12 01:42:03 +00005184 if (params->freq)
5185 nl80211_link_set_freq(bss, params->mld_link_id,
5186 params->freq->freq);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005187 }
5188
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005189 if (params->proberesp && params->proberesp_len) {
5190 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
5191 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005192 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5193 params->proberesp))
5194 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005195 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005196 switch (params->hide_ssid) {
5197 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005198 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005199 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
5200 NL80211_HIDDEN_SSID_NOT_IN_USE))
5201 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005202 break;
5203 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005204 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005205 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
5206 NL80211_HIDDEN_SSID_ZERO_LEN))
5207 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005208 break;
5209 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005210 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005211 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
5212 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
5213 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005214 break;
5215 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005216 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005217 if (params->privacy &&
5218 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
5219 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005220 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005221 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5222 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5223 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005224 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
5225 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
5226 NL80211_AUTHTYPE_SHARED_KEY))
5227 goto fail;
5228 } else {
5229 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
5230 NL80211_AUTHTYPE_OPEN_SYSTEM))
5231 goto fail;
5232 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005233
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005234 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005235 ver = 0;
5236 if (params->wpa_version & WPA_PROTO_WPA)
5237 ver |= NL80211_WPA_VERSION_1;
5238 if (params->wpa_version & WPA_PROTO_RSN)
5239 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005240 if (ver &&
5241 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5242 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005243
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005244 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
5245 params->key_mgmt_suites);
Hai Shalomfdcde762020-04-02 11:19:20 -07005246 num_suites = wpa_key_mgmt_to_suites(params->key_mgmt_suites,
5247 suites, ARRAY_SIZE(suites));
Sunil Ravi7f769292024-07-23 22:21:32 +00005248 if ((unsigned int) num_suites > drv->capa.max_num_akms)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07005249 wpa_printf(MSG_DEBUG,
Sunil Ravi7f769292024-07-23 22:21:32 +00005250 "nl80211: Not enough room for all AKM suites (num_suites=%d > %d)",
5251 num_suites, drv->capa.max_num_akms);
Hai Shalomfdcde762020-04-02 11:19:20 -07005252 else if (num_suites &&
5253 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
5254 suites))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005255 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005256
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005257 if (wpa_key_mgmt_wpa_psk_no_sae(params->key_mgmt_suites) &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005258 (drv->capa.flags2 & WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005259 params->psk_len &&
5260 nla_put(msg, NL80211_ATTR_PMK, params->psk_len, params->psk))
5261 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005262
5263 if (wpa_key_mgmt_sae(params->key_mgmt_suites) &&
5264 (drv->capa.flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_AP) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005265 params->sae_password &&
5266 nla_put(msg, NL80211_ATTR_SAE_PASSWORD,
5267 os_strlen(params->sae_password), params->sae_password))
5268 goto fail;
5269
5270 if (nl80211_put_control_port(drv, msg) < 0)
5271 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005272
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005273 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005274 (!params->pairwise_ciphers ||
5275 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005276 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005277 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005278
Sunil Ravia04bd252022-05-02 22:54:18 -07005279 if (drv->device_ap_sme) {
5280 u32 flags = 0;
5281
Sunil Ravi89eba102022-09-13 21:04:37 -07005282 if (params->key_mgmt_suites & (WPA_KEY_MGMT_SAE |
5283 WPA_KEY_MGMT_SAE_EXT_KEY)) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005284 /* Add the previously used flag attribute to support
5285 * older kernel versions and the newer flag bit for
5286 * newer kernels. */
5287 if (nla_put_flag(msg,
5288 NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5289 goto fail;
5290 flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
5291 }
5292
5293 flags |= NL80211_AP_SETTINGS_SA_QUERY_OFFLOAD_SUPPORT;
5294
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005295 if (nl80211_attr_supported(drv,
5296 NL80211_ATTR_AP_SETTINGS_FLAGS) &&
5297 nla_put_u32(msg, NL80211_ATTR_AP_SETTINGS_FLAGS, flags))
Sunil Ravia04bd252022-05-02 22:54:18 -07005298 goto fail;
5299 }
Hai Shalom5f92bc92019-04-18 11:54:11 -07005300
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005301 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
5302 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005303 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
5304 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005305 if (num_suites &&
5306 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5307 num_suites * sizeof(u32), suites))
5308 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005309
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005310 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
5311 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005312 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005313 if (suite &&
5314 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
5315 goto fail;
5316
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005317 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005318 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
5319 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005320 if (nla_put(msg, NL80211_ATTR_IE,
5321 wpabuf_len(params->beacon_ies),
5322 wpabuf_head(params->beacon_ies)))
5323 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005324 }
5325 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005326 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
5327 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005328 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
5329 wpabuf_len(params->proberesp_ies),
5330 wpabuf_head(params->proberesp_ies)))
5331 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005332 }
5333 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005334 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
5335 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005336 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
5337 wpabuf_len(params->assocresp_ies),
5338 wpabuf_head(params->assocresp_ies)))
5339 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005340 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005341
Dmitry Shmidt04949592012-07-19 12:16:46 -07005342 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005343 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
5344 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005345 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
5346 params->ap_max_inactivity))
5347 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005348 }
5349
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005350#ifdef CONFIG_P2P
5351 if (params->p2p_go_ctwindow > 0) {
5352 if (drv->p2p_go_ctwindow_supported) {
5353 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
5354 params->p2p_go_ctwindow);
5355 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
5356 params->p2p_go_ctwindow))
5357 goto fail;
5358 } else {
5359 wpa_printf(MSG_INFO,
5360 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
5361 }
5362 }
5363#endif /* CONFIG_P2P */
5364
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005365 if (params->pbss) {
5366 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
5367 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5368 goto fail;
5369 }
5370
Hai Shalom74f70d42019-02-11 14:42:39 -08005371 if (params->ftm_responder) {
5372 struct nlattr *ftm;
5373
5374 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_FTM_RESPONDER)) {
5375 ret = -ENOTSUP;
5376 goto fail;
5377 }
5378
5379 ftm = nla_nest_start(msg, NL80211_ATTR_FTM_RESPONDER);
5380 if (!ftm ||
5381 nla_put_flag(msg, NL80211_FTM_RESP_ATTR_ENABLED) ||
5382 (params->lci &&
5383 nla_put(msg, NL80211_FTM_RESP_ATTR_LCI,
5384 wpabuf_len(params->lci),
5385 wpabuf_head(params->lci))) ||
5386 (params->civic &&
5387 nla_put(msg, NL80211_FTM_RESP_ATTR_CIVICLOC,
5388 wpabuf_len(params->civic),
5389 wpabuf_head(params->civic))))
5390 goto fail;
5391 nla_nest_end(msg, ftm);
5392 }
5393
Sunil Ravi99c035e2024-07-12 01:42:03 +00005394 if (params->freq && nl80211_put_freq_params(msg, params->freq) < 0)
5395 goto fail;
5396
Hai Shalomc3565922019-10-28 11:58:20 -07005397#ifdef CONFIG_IEEE80211AX
Hai Shalom60840252021-02-19 19:02:11 -08005398 if (params->he_spr_ctrl) {
Hai Shalomc3565922019-10-28 11:58:20 -07005399 struct nlattr *spr;
5400
5401 spr = nla_nest_start(msg, NL80211_ATTR_HE_OBSS_PD);
Hai Shalom60840252021-02-19 19:02:11 -08005402 wpa_printf(MSG_DEBUG, "nl80211: he_spr_ctrl=0x%x",
5403 params->he_spr_ctrl);
Hai Shalomc3565922019-10-28 11:58:20 -07005404
Hai Shalomfdcde762020-04-02 11:19:20 -07005405 if (!spr ||
Hai Shalom60840252021-02-19 19:02:11 -08005406 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_SR_CTRL,
5407 params->he_spr_ctrl) ||
5408 ((params->he_spr_ctrl &
5409 SPATIAL_REUSE_NON_SRG_OFFSET_PRESENT) &&
5410 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET,
5411 params->he_spr_non_srg_obss_pd_max_offset)))
5412 goto fail;
5413
5414 if ((params->he_spr_ctrl &
5415 SPATIAL_REUSE_SRG_INFORMATION_PRESENT) &&
5416 (nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET,
5417 params->he_spr_srg_obss_pd_min_offset) ||
5418 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET,
5419 params->he_spr_srg_obss_pd_max_offset) ||
5420 nla_put(msg, NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP,
5421 sizeof(params->he_spr_bss_color_bitmap),
5422 params->he_spr_bss_color_bitmap) ||
5423 nla_put(msg, NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP,
5424 sizeof(params->he_spr_partial_bssid_bitmap),
5425 params->he_spr_partial_bssid_bitmap)))
Hai Shalomc3565922019-10-28 11:58:20 -07005426 goto fail;
5427
5428 nla_nest_end(msg, spr);
5429 }
Hai Shalomfdcde762020-04-02 11:19:20 -07005430
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005431 if (params->freq && params->freq->he_enabled &&
5432 nl80211_attr_supported(drv, NL80211_ATTR_HE_BSS_COLOR)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005433 struct nlattr *bss_color;
5434
5435 bss_color = nla_nest_start(msg, NL80211_ATTR_HE_BSS_COLOR);
5436 if (!bss_color ||
5437 (params->he_bss_color_disabled &&
5438 nla_put_flag(msg, NL80211_HE_BSS_COLOR_ATTR_DISABLED)) ||
5439 (params->he_bss_color_partial &&
5440 nla_put_flag(msg, NL80211_HE_BSS_COLOR_ATTR_PARTIAL)) ||
5441 nla_put_u8(msg, NL80211_HE_BSS_COLOR_ATTR_COLOR,
5442 params->he_bss_color))
5443 goto fail;
5444 nla_nest_end(msg, bss_color);
5445 }
5446
5447 if (params->twt_responder) {
5448 wpa_printf(MSG_DEBUG, "nl80211: twt_responder=%d",
5449 params->twt_responder);
5450 if (nla_put_flag(msg, NL80211_ATTR_TWT_RESPONDER))
5451 goto fail;
5452 }
Hai Shalom60840252021-02-19 19:02:11 -08005453
Sunil Ravi7f769292024-07-23 22:21:32 +00005454 if (params->ubpr.unsol_bcast_probe_resp_interval &&
5455 nl80211_unsol_bcast_probe_resp(bss, msg, &params->ubpr) < 0)
Hai Shalom60840252021-02-19 19:02:11 -08005456 goto fail;
Sunil Ravi77d572f2023-01-17 23:58:31 +00005457
5458 if (nl80211_mbssid(msg, params) < 0)
5459 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07005460#endif /* CONFIG_IEEE80211AX */
5461
Hai Shalom60840252021-02-19 19:02:11 -08005462#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -07005463 if (wpa_key_mgmt_sae(params->key_mgmt_suites) &&
Hai Shalom60840252021-02-19 19:02:11 -08005464 nl80211_put_sae_pwe(msg, params->sae_pwe) < 0)
5465 goto fail;
5466#endif /* CONFIG_SAE */
5467
5468#ifdef CONFIG_FILS
5469 if (params->fd_max_int && nl80211_fils_discovery(bss, msg, params) < 0)
5470 goto fail;
5471#endif /* CONFIG_FILS */
5472
Sunil Ravi036cec52023-03-29 11:35:17 -07005473 if (params->punct_bitmap) {
5474 wpa_printf(MSG_DEBUG, "nl80211: Puncturing bitmap=0x%04x",
5475 params->punct_bitmap);
5476 if (nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP,
5477 params->punct_bitmap))
5478 goto fail;
5479 }
5480
Sunil Ravi640215c2023-06-28 23:08:09 +00005481#ifdef CONFIG_DRIVER_NL80211_QCA
5482 if (cmd == NL80211_CMD_NEW_BEACON && params->allowed_freqs)
5483 qca_set_allowed_ap_freqs(drv, params->allowed_freqs,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005484 int_array_len(params->allowed_freqs),
5485 params->mld_ap ? params->mld_link_id :
5486 NL80211_DRV_LINK_ID_NA);
Sunil Ravi640215c2023-06-28 23:08:09 +00005487#endif /* CONFIG_DRIVER_NL80211_QCA */
5488
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005489 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
5490 goto fail;
5491 ret = send_and_recv(drv->global, bss->nl_connect, msg, NULL, NULL, NULL,
5492 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493 if (ret) {
5494 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5495 ret, strerror(-ret));
5496 } else {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005497 link->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005498 nl80211_set_bss(bss, params->cts_protect, params->preamble,
5499 params->short_slot_time, params->ht_opmode,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005500 params->isolate, params->basic_rates,
5501 params->mld_ap ? params->mld_link_id :
5502 NL80211_DRV_LINK_ID_NA);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08005503 nl80211_set_multicast_to_unicast(bss,
5504 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005505 if (beacon_set && params->freq &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005506 params->freq->bandwidth != link->bandwidth) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005507 wpa_printf(MSG_DEBUG,
5508 "nl80211: Update BSS %s bandwidth: %d -> %d",
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005509 bss->ifname, link->bandwidth,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005510 params->freq->bandwidth);
5511 ret = nl80211_set_channel(bss, params->freq, 1);
5512 if (ret) {
5513 wpa_printf(MSG_DEBUG,
5514 "nl80211: Frequency set failed: %d (%s)",
5515 ret, strerror(-ret));
5516 } else {
5517 wpa_printf(MSG_DEBUG,
5518 "nl80211: Frequency set succeeded for ht2040 coex");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005519 link->bandwidth = params->freq->bandwidth;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005520 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005521 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005522 /*
5523 * cfg80211 updates the driver on frequence change in AP
5524 * mode only at the point when beaconing is started, so
5525 * set the initial value here.
5526 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005527 link->bandwidth = params->freq->bandwidth;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005528 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005529 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005530
5531#ifdef CONFIG_MESH
5532 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
5533 os_memset(&mesh_params, 0, sizeof(mesh_params));
5534 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
5535 mesh_params.ht_opmode = params->ht_opmode;
5536 ret = nl80211_set_mesh_config(priv, &mesh_params);
5537 if (ret < 0)
5538 return ret;
5539 }
5540#endif /* CONFIG_MESH */
5541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005542 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005543fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005544 nlmsg_free(msg);
Hai Shalom74f70d42019-02-11 14:42:39 -08005545 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005546}
5547
5548
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005549static int nl80211_set_channel(struct i802_bss *bss,
5550 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005551{
5552 struct wpa_driver_nl80211_data *drv = bss->drv;
5553 struct nl_msg *msg;
5554 int ret;
5555
5556 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07005557 "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)",
5558 freq->freq, freq->ht_enabled, freq->vht_enabled,
5559 freq->he_enabled, freq->eht_enabled, freq->bandwidth,
5560 freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005561
5562 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
5563 NL80211_CMD_SET_WIPHY);
5564 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
5565 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005566 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005567 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005568
Sunil Ravi99c035e2024-07-12 01:42:03 +00005569 if (nl80211_link_valid(bss->valid_links, freq->link_id)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005570 wpa_printf(MSG_DEBUG, "nl80211: Set link_id=%u for freq",
5571 freq->link_id);
5572
5573 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, freq->link_id)) {
5574 nlmsg_free(msg);
5575 return -ENOBUFS;
5576 }
5577 }
5578
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005579 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005580 if (ret == 0) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005581 nl80211_link_set_freq(bss, freq->link_id, freq->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005582 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005583 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005584 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005585 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005586 return -1;
5587}
5588
5589
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005590static u32 sta_flags_nl80211(int flags)
5591{
5592 u32 f = 0;
5593
5594 if (flags & WPA_STA_AUTHORIZED)
5595 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
5596 if (flags & WPA_STA_WMM)
5597 f |= BIT(NL80211_STA_FLAG_WME);
5598 if (flags & WPA_STA_SHORT_PREAMBLE)
5599 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
5600 if (flags & WPA_STA_MFP)
5601 f |= BIT(NL80211_STA_FLAG_MFP);
5602 if (flags & WPA_STA_TDLS_PEER)
5603 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005604 if (flags & WPA_STA_AUTHENTICATED)
5605 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005606 if (flags & WPA_STA_ASSOCIATED)
5607 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005608
5609 return f;
5610}
5611
5612
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005613#ifdef CONFIG_MESH
5614static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
5615{
5616 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005617 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005618 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005619 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005620 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005621 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005622 return NL80211_PLINK_OPN_RCVD;
5623 case PLINK_CNF_RCVD:
5624 return NL80211_PLINK_CNF_RCVD;
5625 case PLINK_ESTAB:
5626 return NL80211_PLINK_ESTAB;
5627 case PLINK_HOLDING:
5628 return NL80211_PLINK_HOLDING;
5629 case PLINK_BLOCKED:
5630 return NL80211_PLINK_BLOCKED;
5631 default:
5632 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
5633 state);
5634 }
5635 return -1;
5636}
5637#endif /* CONFIG_MESH */
5638
5639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005640static int wpa_driver_nl80211_sta_add(void *priv,
5641 struct hostapd_sta_add_params *params)
5642{
5643 struct i802_bss *bss = priv;
5644 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005645 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005646 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005647 int ret = -ENOBUFS;
Sunil Ravi036cec52023-03-29 11:35:17 -07005648 u8 cmd;
5649 const char *cmd_string;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005651 if ((params->flags & WPA_STA_TDLS_PEER) &&
5652 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
5653 return -EOPNOTSUPP;
5654
Sunil Ravi036cec52023-03-29 11:35:17 -07005655 if (params->mld_link_sta) {
5656 cmd = params->set ? NL80211_CMD_MODIFY_LINK_STA :
5657 NL80211_CMD_ADD_LINK_STA;
5658 cmd_string = params->set ? "NL80211_CMD_MODIFY_LINK_STA" :
5659 "NL80211_CMD_ADD_LINK_STA";
5660 } else {
5661 cmd = params->set ? NL80211_CMD_SET_STATION :
5662 NL80211_CMD_NEW_STATION;
5663 cmd_string = params->set ? "NL80211_CMD_SET_STATION" :
5664 "NL80211_CMD_NEW_STATION";
5665 }
5666
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005667 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
Sunil Ravi036cec52023-03-29 11:35:17 -07005668 cmd_string, MAC2STR(params->addr));
5669 msg = nl80211_bss_msg(bss, 0, cmd);
5670 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005671 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005673 /*
5674 * Set the below properties only in one of the following cases:
5675 * 1. New station is added, already associated.
5676 * 2. Set WPA_STA_TDLS_PEER station.
5677 * 3. Set an already added unassociated station, if driver supports
5678 * full AP client state. (Set these properties after station became
5679 * associated will be rejected by the driver).
5680 */
5681 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
5682 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
5683 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005684 wpa_hexdump(MSG_DEBUG, " * supported rates",
5685 params->supp_rates, params->supp_rates_len);
5686 wpa_printf(MSG_DEBUG, " * capability=0x%x",
5687 params->capability);
5688 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
5689 params->supp_rates_len, params->supp_rates) ||
5690 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
5691 params->capability))
5692 goto fail;
5693
5694 if (params->ht_capabilities) {
5695 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
5696 (u8 *) params->ht_capabilities,
5697 sizeof(*params->ht_capabilities));
5698 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
5699 sizeof(*params->ht_capabilities),
5700 params->ht_capabilities))
5701 goto fail;
5702 }
5703
5704 if (params->vht_capabilities) {
5705 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
5706 (u8 *) params->vht_capabilities,
5707 sizeof(*params->vht_capabilities));
5708 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
5709 sizeof(*params->vht_capabilities),
5710 params->vht_capabilities))
5711 goto fail;
5712 }
5713
Hai Shalom81f62d82019-07-22 12:10:00 -07005714 if (params->he_capab) {
5715 wpa_hexdump(MSG_DEBUG, " * he_capab",
5716 params->he_capab, params->he_capab_len);
5717 if (nla_put(msg, NL80211_ATTR_HE_CAPABILITY,
5718 params->he_capab_len, params->he_capab))
5719 goto fail;
5720 }
5721
Hai Shalom60840252021-02-19 19:02:11 -08005722 if (params->he_6ghz_capab) {
5723 wpa_hexdump(MSG_DEBUG, " * he_6ghz_capab",
5724 params->he_6ghz_capab,
5725 sizeof(*params->he_6ghz_capab));
5726 if (nla_put(msg, NL80211_ATTR_HE_6GHZ_CAPABILITY,
5727 sizeof(*params->he_6ghz_capab),
5728 params->he_6ghz_capab))
5729 goto fail;
5730 }
5731
Sunil Ravia04bd252022-05-02 22:54:18 -07005732 if (params->eht_capab) {
5733 wpa_hexdump(MSG_DEBUG, " * eht_capab",
5734 params->eht_capab, params->eht_capab_len);
5735 if (nla_put(msg, NL80211_ATTR_EHT_CAPABILITY,
5736 params->eht_capab_len, params->eht_capab))
5737 goto fail;
5738 }
5739
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005740 if (params->ext_capab) {
5741 wpa_hexdump(MSG_DEBUG, " * ext_capab",
5742 params->ext_capab, params->ext_capab_len);
5743 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
5744 params->ext_capab_len, params->ext_capab))
5745 goto fail;
5746 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005747
5748 if (is_ap_interface(drv->nlmode) &&
5749 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
5750 params->support_p2p_ps ?
5751 NL80211_P2P_PS_SUPPORTED :
5752 NL80211_P2P_PS_UNSUPPORTED))
5753 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005754 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005755 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005756 if (params->aid) {
5757 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005758 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
5759 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005760 } else {
5761 /*
5762 * cfg80211 validates that AID is non-zero, so we have
5763 * to make this a non-zero value for the TDLS case where
Hai Shalomc1a21442022-02-04 13:43:00 -08005764 * a stub STA entry is used for now and for a station
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005765 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005766 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005767 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
5768 (params->flags & WPA_STA_TDLS_PEER) ?
5769 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005770 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
5771 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005772 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005773 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
5774 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005775 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5776 params->listen_interval))
5777 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005778 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
5779 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005780 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
5781 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005782 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
5783 (params->flags & WPA_STA_ASSOCIATED)) {
5784 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
5785 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
5786 params->listen_interval);
5787 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
5788 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5789 params->listen_interval))
5790 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005791 }
5792
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005793 if (params->vht_opmode_enabled) {
5794 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005795 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
5796 params->vht_opmode))
5797 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005798 }
5799
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005800 if (params->supp_channels) {
5801 wpa_hexdump(MSG_DEBUG, " * supported channels",
5802 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005803 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
5804 params->supp_channels_len, params->supp_channels))
5805 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005806 }
5807
5808 if (params->supp_oper_classes) {
5809 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
5810 params->supp_oper_classes,
5811 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005812 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
5813 params->supp_oper_classes_len,
5814 params->supp_oper_classes))
5815 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005816 }
5817
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005818 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005819 upd.set = sta_flags_nl80211(params->flags);
5820 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005821
5822 /*
5823 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
5824 * flags, as nl80211 driver moves a new station, by default, into
5825 * associated state.
5826 *
5827 * On the other hand, if the driver supports that feature and the
5828 * station is added in unauthenticated state, set the
5829 * authenticated/associated bits in the mask to prevent moving this
5830 * station to associated state before it is actually associated.
5831 *
5832 * This is irrelevant for mesh mode where the station is added to the
5833 * driver as authenticated already, and ASSOCIATED isn't part of the
5834 * nl80211 API.
5835 */
5836 if (!is_mesh_interface(drv->nlmode)) {
5837 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
5838 wpa_printf(MSG_DEBUG,
5839 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
5840 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
5841 BIT(NL80211_STA_FLAG_AUTHENTICATED));
5842 } else if (!params->set &&
5843 !(params->flags & WPA_STA_TDLS_PEER)) {
5844 if (!(params->flags & WPA_STA_AUTHENTICATED))
5845 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
5846 if (!(params->flags & WPA_STA_ASSOCIATED))
5847 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
5848 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005849#ifdef CONFIG_MESH
5850 } else {
5851 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
5852 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
5853 params->peer_aid);
5854 if (ret)
5855 goto fail;
5856 }
5857#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005858 }
5859
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005860 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
5861 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005862 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
5863 goto fail;
5864
5865#ifdef CONFIG_MESH
5866 if (params->plink_state &&
5867 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
5868 sta_plink_state_nl80211(params->plink_state)))
5869 goto fail;
5870#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005871
Hai Shalomc3565922019-10-28 11:58:20 -07005872 if ((!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
5873 FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) &&
5874 (params->flags & WPA_STA_WMM)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005875 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
5876
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005877 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005878 if (!wme ||
5879 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
5880 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
5881 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
5882 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
5883 WMM_QOSINFO_STA_SP_MASK))
5884 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005885 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005886 }
5887
Sunil Ravi036cec52023-03-29 11:35:17 -07005888 /* In case we are an AP MLD need to always specify the link ID */
5889 if (params->mld_link_id >= 0) {
5890 wpa_printf(MSG_DEBUG, " * mld_link_id=%d",
5891 params->mld_link_id);
5892 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
5893 params->mld_link_id))
5894 goto fail;
5895
5896 /*
5897 * If the link address is specified the station is a non-AP MLD
5898 * and thus need to provide the MLD address as the station
5899 * address, and the non-AP MLD link address as the link address.
5900 */
5901 if (params->mld_link_addr) {
5902 wpa_printf(MSG_DEBUG, " * mld_link_addr=" MACSTR,
5903 MAC2STR(params->mld_link_addr));
5904
5905 if (nla_put(msg, NL80211_ATTR_MLD_ADDR,
5906 ETH_ALEN, params->addr) ||
5907 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
5908 params->mld_link_addr))
5909 goto fail;
5910 } else {
5911 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
5912 params->addr))
5913 goto fail;
5914 }
5915 } else {
5916 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
5917 goto fail;
5918 }
5919
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005920 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005921 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005922 if (ret)
Sunil Ravi036cec52023-03-29 11:35:17 -07005923 wpa_printf(MSG_DEBUG, "nl80211: %s result: %d (%s)",
5924 cmd_string, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005925 if (ret == -EEXIST)
5926 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005927fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005928 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005929 return ret;
5930}
5931
5932
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005933static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
5934{
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005935 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +00005936 struct ndmsg nhdr = {
5937 .ndm_state = NUD_PERMANENT,
5938 .ndm_ifindex = bss->ifindex,
5939 .ndm_family = AF_BRIDGE,
5940 };
5941 struct nl_msg *msg;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005942 int err;
5943
Sunil Ravi99c035e2024-07-12 01:42:03 +00005944 msg = nlmsg_alloc_simple(RTM_DELNEIGH, NLM_F_CREATE);
5945 if (!msg)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005946 return;
5947
Sunil Ravi99c035e2024-07-12 01:42:03 +00005948 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0 ||
5949 nla_put(msg, NDA_LLADDR, ETH_ALEN, (void *) addr) ||
5950 nl_send_auto_complete(drv->rtnl_sk, msg) < 0)
5951 goto errout;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005952
Sunil Ravi99c035e2024-07-12 01:42:03 +00005953 err = nl_wait_for_ack(drv->rtnl_sk);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005954 if (err < 0) {
5955 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
5956 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
5957 bss->ifindex, nl_geterror(err));
5958 } else {
5959 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
5960 MACSTR, MAC2STR(addr));
5961 }
5962
Sunil Ravi99c035e2024-07-12 01:42:03 +00005963errout:
5964 nlmsg_free(msg);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005965}
5966
5967
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005968static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
5969 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005970{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005971 struct wpa_driver_nl80211_data *drv = bss->drv;
5972 struct nl_msg *msg;
5973 int ret;
5974
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005975 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
5976 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5977 (deauth == 0 &&
5978 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
5979 WLAN_FC_STYPE_DISASSOC)) ||
5980 (deauth == 1 &&
5981 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
5982 WLAN_FC_STYPE_DEAUTH)) ||
5983 (reason_code &&
5984 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
5985 nlmsg_free(msg);
5986 return -ENOBUFS;
5987 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005988
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005989 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005990 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
5991 " --> %d (%s)",
5992 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005993
5994 if (drv->rtnl_sk)
5995 rtnl_neigh_delete_fdb_entry(bss, addr);
5996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005997 if (ret == -ENOENT)
5998 return 0;
5999 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000}
6001
6002
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006003void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006004{
6005 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006006 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006007
6008 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
6009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006010 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006011 dl_list_for_each(drv2, &drv->global->interfaces,
6012 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006013 {
6014 del_ifidx(drv2, ifidx, IFIDX_ANY);
6015 /* Remove all bridges learned for this iface */
6016 del_ifidx(drv2, IFIDX_ANY, ifidx);
6017 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006018
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006019 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006020 if (send_and_recv_cmd(drv, msg) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006021 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
6023}
6024
6025
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07006026const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006027{
6028 switch (mode) {
6029 case NL80211_IFTYPE_ADHOC:
6030 return "ADHOC";
6031 case NL80211_IFTYPE_STATION:
6032 return "STATION";
6033 case NL80211_IFTYPE_AP:
6034 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07006035 case NL80211_IFTYPE_AP_VLAN:
6036 return "AP_VLAN";
6037 case NL80211_IFTYPE_WDS:
6038 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006039 case NL80211_IFTYPE_MONITOR:
6040 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07006041 case NL80211_IFTYPE_MESH_POINT:
6042 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006043 case NL80211_IFTYPE_P2P_CLIENT:
6044 return "P2P_CLIENT";
6045 case NL80211_IFTYPE_P2P_GO:
6046 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006047 case NL80211_IFTYPE_P2P_DEVICE:
6048 return "P2P_DEVICE";
Hai Shalom4fbc08f2020-05-18 12:37:00 -07006049 case NL80211_IFTYPE_OCB:
6050 return "OCB";
6051 case NL80211_IFTYPE_NAN:
6052 return "NAN";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006053 default:
6054 return "unknown";
6055 }
6056}
6057
6058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006059static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6060 const char *ifname,
6061 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006062 const u8 *addr, int wds,
6063 int (*handler)(struct nl_msg *, void *),
6064 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006066 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006067 int ifidx;
6068 int ret = -ENOBUFS;
6069
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006070 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6071 iftype, nl80211_iftype_str(iftype));
6072
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006073 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
6074 if (!msg ||
6075 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
6076 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
6077 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006078
6079 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006080 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006081
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006082 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006083 if (!flags ||
6084 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
6085 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006086
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006087 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006088 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006089 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
6090 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006091 }
6092
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006093 /*
6094 * Tell cfg80211 that the interface belongs to the socket that created
6095 * it, and the interface should be deleted when the socket is closed.
6096 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006097 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
6098 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006099
Hai Shalom60840252021-02-19 19:02:11 -08006100 if ((addr && iftype == NL80211_IFTYPE_P2P_DEVICE) &&
6101 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
6102 goto fail;
6103
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006104 ret = send_and_recv_resp(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006105 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006106 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006107 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006108 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006109 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6110 ifname, ret, strerror(-ret));
6111 return ret;
6112 }
6113
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006114 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
6115 return 0;
6116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117 ifidx = if_nametoindex(ifname);
6118 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6119 ifname, ifidx);
6120
6121 if (ifidx <= 0)
6122 return -1;
6123
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006124 /*
6125 * Some virtual interfaces need to process EAPOL packets and events on
6126 * the parent interface. This is used mainly with hostapd.
6127 */
6128 if (drv->hostapd ||
6129 iftype == NL80211_IFTYPE_AP_VLAN ||
6130 iftype == NL80211_IFTYPE_WDS ||
6131 iftype == NL80211_IFTYPE_MONITOR) {
6132 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006133 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006135
6136 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006137 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006138 nl80211_remove_iface(drv, ifidx);
6139 return -1;
6140 }
6141
6142 return ifidx;
6143}
6144
6145
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006146int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6147 const char *ifname, enum nl80211_iftype iftype,
6148 const u8 *addr, int wds,
6149 int (*handler)(struct nl_msg *, void *),
6150 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006151{
6152 int ret;
6153
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006154 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
6155 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006156
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006157 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006159 if (use_existing) {
6160 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
6161 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006162 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
6163 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
6164 addr) < 0 &&
6165 (linux_set_iface_flags(drv->global->ioctl_sock,
6166 ifname, 0) < 0 ||
6167 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
6168 addr) < 0 ||
6169 linux_set_iface_flags(drv->global->ioctl_sock,
6170 ifname, 1) < 0))
6171 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006172 return -ENFILE;
6173 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006174 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6175
6176 /* Try to remove the interface that was already there. */
6177 nl80211_remove_iface(drv, if_nametoindex(ifname));
6178
6179 /* Try to create the interface again */
6180 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006181 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006182 }
6183
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006184 if (ret >= 0 && is_p2p_net_interface(iftype)) {
6185 wpa_printf(MSG_DEBUG,
6186 "nl80211: Interface %s created for P2P - disable 11b rates",
6187 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006188 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006190
6191 return ret;
6192}
6193
6194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006195static int nl80211_setup_ap(struct i802_bss *bss)
6196{
6197 struct wpa_driver_nl80211_data *drv = bss->drv;
6198
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006199 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
6200 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006201
6202 /*
6203 * Disable Probe Request reporting unless we need it in this way for
6204 * devices that include the AP SME, in the other case (unless using
6205 * monitor iface) we'll get it through the nl_mgmt socket instead.
6206 */
6207 if (!drv->device_ap_sme)
6208 wpa_driver_nl80211_probe_req_report(bss, 0);
6209
6210 if (!drv->device_ap_sme && !drv->use_monitor)
6211 if (nl80211_mgmt_subscribe_ap(bss))
6212 return -1;
6213
6214 if (drv->device_ap_sme && !drv->use_monitor)
6215 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006216 wpa_printf(MSG_DEBUG,
6217 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006218
6219 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07006220 nl80211_create_monitor_interface(drv) &&
6221 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006222 return -1;
6223
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006224 if (drv->device_ap_sme &&
6225 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6226 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6227 "Probe Request frame reporting in AP mode");
6228 /* Try to survive without this */
6229 }
6230
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006231 return 0;
6232}
6233
6234
6235static void nl80211_teardown_ap(struct i802_bss *bss)
6236{
6237 struct wpa_driver_nl80211_data *drv = bss->drv;
6238
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006239 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
6240 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006241 if (drv->device_ap_sme) {
6242 wpa_driver_nl80211_probe_req_report(bss, 0);
6243 if (!drv->use_monitor)
6244 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6245 } else if (drv->use_monitor)
6246 nl80211_remove_monitor_interface(drv);
6247 else
6248 nl80211_mgmt_unsubscribe(bss, "AP teardown");
6249
Paul Stewart092955c2017-02-06 09:13:09 -08006250 nl80211_put_wiphy_data_ap(bss);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006251 if (bss->flink)
6252 bss->flink->beacon_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006253}
6254
6255
Hai Shalomfdcde762020-04-02 11:19:20 -07006256static int nl80211_tx_control_port(void *priv, const u8 *dest,
6257 u16 proto, const u8 *buf, size_t len,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006258 int no_encrypt, int link_id)
Hai Shalomfdcde762020-04-02 11:19:20 -07006259{
Hai Shalom899fcc72020-10-19 14:38:18 -07006260 struct nl80211_ack_ext_arg ext_arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07006261 struct i802_bss *bss = priv;
6262 struct nl_msg *msg;
Hai Shalom899fcc72020-10-19 14:38:18 -07006263 u64 cookie = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07006264 int ret;
6265
6266 wpa_printf(MSG_DEBUG,
6267 "nl80211: Send over control port dest=" MACSTR
6268 " proto=0x%04x len=%u no_encrypt=%d",
6269 MAC2STR(dest), proto, (unsigned int) len, no_encrypt);
6270
6271 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CONTROL_PORT_FRAME);
6272 if (!msg ||
6273 nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
6274 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dest) ||
6275 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
6276 (no_encrypt &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006277 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)) ||
6278 (link_id != NL80211_DRV_LINK_ID_NA &&
6279 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))) {
Hai Shalomfdcde762020-04-02 11:19:20 -07006280 nlmsg_free(msg);
6281 return -ENOBUFS;
6282 }
6283
Hai Shalom899fcc72020-10-19 14:38:18 -07006284 os_memset(&ext_arg, 0, sizeof(struct nl80211_ack_ext_arg));
6285 ext_arg.ext_data = &cookie;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006286 ret = send_and_recv(bss->drv->global, bss->drv->global->nl, msg,
6287 NULL, NULL, ack_handler_cookie, &ext_arg, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07006288 if (ret) {
Hai Shalomfdcde762020-04-02 11:19:20 -07006289 wpa_printf(MSG_DEBUG,
6290 "nl80211: tx_control_port failed: ret=%d (%s)",
6291 ret, strerror(-ret));
Hai Shalom899fcc72020-10-19 14:38:18 -07006292 } else {
6293 struct wpa_driver_nl80211_data *drv = bss->drv;
6294
6295 wpa_printf(MSG_DEBUG,
6296 "nl80211: tx_control_port cookie=0x%llx",
6297 (long long unsigned int) cookie);
6298 drv->eapol_tx_cookie = cookie;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006299 drv->eapol_tx_link_id = link_id;
Hai Shalom899fcc72020-10-19 14:38:18 -07006300 }
Hai Shalomfdcde762020-04-02 11:19:20 -07006301
6302 return ret;
6303}
6304
6305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006306static int nl80211_send_eapol_data(struct i802_bss *bss,
6307 const u8 *addr, const u8 *data,
6308 size_t data_len)
6309{
6310 struct sockaddr_ll ll;
6311 int ret;
6312
6313 if (bss->drv->eapol_tx_sock < 0) {
6314 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
6315 return -1;
6316 }
6317
6318 os_memset(&ll, 0, sizeof(ll));
6319 ll.sll_family = AF_PACKET;
6320 ll.sll_ifindex = bss->ifindex;
6321 ll.sll_protocol = htons(ETH_P_PAE);
6322 ll.sll_halen = ETH_ALEN;
6323 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6324 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6325 (struct sockaddr *) &ll, sizeof(ll));
6326 if (ret < 0)
6327 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6328 strerror(errno));
6329
6330 return ret;
6331}
6332
6333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006334static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6335
6336static int wpa_driver_nl80211_hapd_send_eapol(
6337 void *priv, const u8 *addr, const u8 *data,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006338 size_t data_len, int encrypt, const u8 *own_addr, u32 flags,
6339 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006340{
6341 struct i802_bss *bss = priv;
6342 struct wpa_driver_nl80211_data *drv = bss->drv;
6343 struct ieee80211_hdr *hdr;
6344 size_t len;
6345 u8 *pos;
6346 int res;
6347 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08006348
Hai Shalomb755a2a2020-04-23 21:49:02 -07006349 /* For now, disable EAPOL TX over control port in AP mode by default
6350 * since it does not provide TX status notifications. */
6351 if (drv->control_port_ap &&
6352 (drv->capa.flags & WPA_DRIVER_FLAGS_CONTROL_PORT))
Hai Shalomfdcde762020-04-02 11:19:20 -07006353 return nl80211_tx_control_port(bss, addr, ETH_P_EAPOL,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006354 data, data_len, !encrypt,
6355 link_id);
Hai Shalomfdcde762020-04-02 11:19:20 -07006356
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006357 if (drv->device_ap_sme || !drv->use_monitor)
6358 return nl80211_send_eapol_data(bss, addr, data, data_len);
6359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006360 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6361 data_len;
6362 hdr = os_zalloc(len);
6363 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006364 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
6365 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006366 return -1;
6367 }
6368
6369 hdr->frame_control =
6370 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6371 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6372 if (encrypt)
6373 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
6374 if (qos) {
6375 hdr->frame_control |=
6376 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6377 }
6378
6379 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6380 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6381 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6382 pos = (u8 *) (hdr + 1);
6383
6384 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006385 /* Set highest priority in QoS header */
6386 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006387 pos[1] = 0;
6388 pos += 2;
6389 }
6390
6391 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6392 pos += sizeof(rfc1042_header);
6393 WPA_PUT_BE16(pos, ETH_P_PAE);
6394 pos += 2;
6395 memcpy(pos, data, data_len);
6396
Hai Shalomfdcde762020-04-02 11:19:20 -07006397 res = nl80211_send_monitor(drv, hdr, len, encrypt, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006398 if (res < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -07006399 wpa_printf(MSG_ERROR,
6400 "hapd_send_eapol - packet len: %lu - failed",
6401 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006402 }
6403 os_free(hdr);
6404
6405 return res;
6406}
6407
6408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006409static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006410 unsigned int total_flags,
6411 unsigned int flags_or,
6412 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006413{
6414 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006415 struct nl_msg *msg;
6416 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417 struct nl80211_sta_flag_update upd;
6418
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006419 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
6420 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
6421 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
6422 !!(total_flags & WPA_STA_AUTHORIZED));
6423
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006424 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6425 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
6426 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006427
6428 /*
6429 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6430 * can be removed eventually.
6431 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006432 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006433 if (!flags ||
6434 ((total_flags & WPA_STA_AUTHORIZED) &&
6435 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
6436 ((total_flags & WPA_STA_WMM) &&
6437 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
6438 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
6439 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
6440 ((total_flags & WPA_STA_MFP) &&
6441 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
6442 ((total_flags & WPA_STA_TDLS_PEER) &&
6443 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
6444 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006445
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006446 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006447
6448 os_memset(&upd, 0, sizeof(upd));
6449 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6450 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006451 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
6452 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006453
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006454 return send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006455fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006456 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006457 return -ENOBUFS;
6458}
6459
6460
Hai Shalom81f62d82019-07-22 12:10:00 -07006461static int driver_nl80211_sta_set_airtime_weight(void *priv, const u8 *addr,
6462 unsigned int weight)
6463{
6464 struct i802_bss *bss = priv;
6465 struct nl_msg *msg;
Hai Shalomc1a21442022-02-04 13:43:00 -08006466 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07006467
6468 wpa_printf(MSG_DEBUG,
6469 "nl80211: Set STA airtime weight - ifname=%s addr=" MACSTR
6470 " weight=%u", bss->ifname, MAC2STR(addr), weight);
6471
6472 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6473 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6474 nla_put_u16(msg, NL80211_ATTR_AIRTIME_WEIGHT, weight))
6475 goto fail;
6476
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006477 ret = send_and_recv_cmd(bss->drv, msg);
Hai Shalomc1a21442022-02-04 13:43:00 -08006478 if (ret) {
6479 wpa_printf(MSG_DEBUG,
6480 "nl80211: SET_STATION[AIRTIME_WEIGHT] failed: ret=%d (%s)",
6481 ret, strerror(-ret));
6482 }
6483 return ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07006484fail:
6485 nlmsg_free(msg);
6486 return -ENOBUFS;
6487}
6488
6489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006490static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6491 struct wpa_driver_associate_params *params)
6492{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006493 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006494
6495 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006496 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6497 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006498 nlmode = NL80211_IFTYPE_P2P_GO;
6499 } else
6500 nlmode = NL80211_IFTYPE_AP;
6501
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006502 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006503 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006504 nl80211_remove_monitor_interface(drv);
6505 return -1;
6506 }
6507
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006508 if (params->freq.freq &&
6509 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006510 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006511 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006512 nl80211_remove_monitor_interface(drv);
6513 return -1;
6514 }
6515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006516 return 0;
6517}
6518
6519
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006520static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
6521 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006522{
6523 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006524 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006525
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006526 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006527 ret = send_and_recv(drv->global, drv->first_bss->nl_connect, msg, NULL,
6528 NULL, NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006529 if (ret) {
6530 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
6531 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006532 } else {
6533 wpa_printf(MSG_DEBUG,
6534 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006535 }
6536
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006537 if (reset_mode &&
6538 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07006539 NL80211_IFTYPE_STATION)) {
6540 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6541 "station mode");
6542 }
6543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006544 return ret;
6545}
6546
6547
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006548static int nl80211_ht_vht_overrides(struct nl_msg *msg,
6549 struct wpa_driver_associate_params *params)
6550{
6551 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
6552 return -1;
6553
6554 if (params->htcaps && params->htcaps_mask) {
6555 int sz = sizeof(struct ieee80211_ht_capabilities);
6556 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
6557 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
6558 params->htcaps_mask, sz);
6559 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
6560 params->htcaps) ||
6561 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6562 params->htcaps_mask))
6563 return -1;
6564 }
6565
6566#ifdef CONFIG_VHT_OVERRIDES
6567 if (params->disable_vht) {
6568 wpa_printf(MSG_DEBUG, " * VHT disabled");
6569 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
6570 return -1;
6571 }
6572
6573 if (params->vhtcaps && params->vhtcaps_mask) {
6574 int sz = sizeof(struct ieee80211_vht_capabilities);
6575 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
6576 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
6577 params->vhtcaps_mask, sz);
6578 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
6579 params->vhtcaps) ||
6580 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
6581 params->vhtcaps_mask))
6582 return -1;
6583 }
6584#endif /* CONFIG_VHT_OVERRIDES */
6585
Hai Shalomc1a21442022-02-04 13:43:00 -08006586#ifdef CONFIG_HE_OVERRIDES
6587 if (params->disable_he) {
6588 wpa_printf(MSG_DEBUG, " * HE disabled");
6589 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_HE))
6590 return -1;
6591 }
6592#endif /* CONFIG_HE_OVERRIDES */
6593
Sunil Ravi77d572f2023-01-17 23:58:31 +00006594 if (params->disable_eht) {
6595 wpa_printf(MSG_DEBUG, " * EHT disabled");
6596 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_EHT))
6597 return -1;
6598 }
6599
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006600 return 0;
6601}
6602
6603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006604static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
6605 struct wpa_driver_associate_params *params)
6606{
6607 struct nl_msg *msg;
6608 int ret = -1;
6609 int count = 0;
6610
6611 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
6612
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006613 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006614 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6615 "IBSS mode");
6616 return -1;
6617 }
6618
6619retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006620 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
6621 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
6622 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006623
Hai Shalom74f70d42019-02-11 14:42:39 -08006624 wpa_printf(MSG_DEBUG, " * SSID=%s",
6625 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006626 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
6627 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006628 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6629 drv->ssid_len = params->ssid_len;
6630
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006631 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
6632 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006633 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006634
6635 ret = nl80211_set_conn_keys(params, msg);
6636 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006637 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006638
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006639 if (params->bssid && params->fixed_bssid) {
6640 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
6641 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006642 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
6643 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006644 }
6645
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006646 if (params->fixed_freq) {
6647 wpa_printf(MSG_DEBUG, " * fixed_freq");
6648 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
6649 goto fail;
6650 }
6651
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006652 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
6653 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
6654 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006655 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
6656 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA384) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006657 wpa_printf(MSG_DEBUG, " * control port");
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006658 if (nl80211_put_control_port(drv, msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006659 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006660 }
6661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006662 if (params->wpa_ie) {
6663 wpa_hexdump(MSG_DEBUG,
6664 " * Extra IEs for Beacon/Probe Response frames",
6665 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006666 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6667 params->wpa_ie))
6668 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006669 }
6670
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08006671 ret = nl80211_ht_vht_overrides(msg, params);
6672 if (ret < 0)
6673 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006674
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006675 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
6676 goto fail;
6677 ret = send_and_recv(drv->global, drv->first_bss->nl_connect, msg, NULL,
6678 NULL, NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006679 msg = NULL;
6680 if (ret) {
6681 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
6682 ret, strerror(-ret));
6683 count++;
6684 if (ret == -EALREADY && count == 1) {
6685 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
6686 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006687 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006688 nlmsg_free(msg);
6689 goto retry;
6690 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006691 } else {
6692 wpa_printf(MSG_DEBUG,
6693 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006695
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006696fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006697 nlmsg_free(msg);
6698 return ret;
6699}
6700
6701
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006702static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
6703 struct wpa_driver_associate_params *params,
6704 struct nl_msg *msg)
6705{
6706 if (params->fils_erp_username_len) {
6707 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
6708 params->fils_erp_username,
6709 params->fils_erp_username_len);
6710 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
6711 params->fils_erp_username_len,
6712 params->fils_erp_username))
6713 return -1;
6714 }
6715
6716 if (params->fils_erp_realm_len) {
6717 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
6718 params->fils_erp_realm,
6719 params->fils_erp_realm_len);
6720 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
6721 params->fils_erp_realm_len, params->fils_erp_realm))
6722 return -1;
6723 }
6724
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006725 if (params->fils_erp_rrk_len) {
Vinita S. Maloo3a5b4412020-05-19 17:43:22 +05306726 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
6727 params->fils_erp_next_seq_num);
6728 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
6729 params->fils_erp_next_seq_num))
6730 return -1;
6731
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006732 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
6733 (unsigned long) params->fils_erp_rrk_len);
6734 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
6735 params->fils_erp_rrk_len, params->fils_erp_rrk))
6736 return -1;
6737 }
6738
6739 return 0;
6740}
6741
6742
Sunil Ravi89eba102022-09-13 21:04:37 -07006743static unsigned int num_bits_set(u32 val)
6744{
6745 unsigned int c;
6746
6747 for (c = 0; val; c++)
6748 val &= val - 1;
6749
6750 return c;
6751}
6752
6753
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006754static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
6755 struct wpa_driver_associate_params *params,
6756 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006757{
Sunil Ravi77d572f2023-01-17 23:58:31 +00006758 if (params->mld_params.mld_addr && params->mld_params.valid_links > 0) {
6759 struct wpa_driver_mld_params *mld_params = &params->mld_params;
6760 struct nlattr *links, *attr;
Sunil Ravi77d572f2023-01-17 23:58:31 +00006761 u8 link_id;
6762
6763 wpa_printf(MSG_DEBUG, " * MLD: MLD addr=" MACSTR,
6764 MAC2STR(mld_params->mld_addr));
6765
6766 if (nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
6767 mld_params->mld_addr) ||
6768 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6769 mld_params->assoc_link_id))
6770 return -1;
6771
6772 links = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
6773 if (!links)
6774 return -1;
6775
Sunil Ravi99c035e2024-07-12 01:42:03 +00006776 for_each_link(mld_params->valid_links, link_id) {
6777 attr = nla_nest_start(msg, 0);
Sunil Ravi77d572f2023-01-17 23:58:31 +00006778 if (!attr)
6779 return -1;
6780
6781 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6782 link_id) ||
6783 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
6784 mld_params->mld_links[link_id].bssid) ||
6785 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
6786 mld_params->mld_links[link_id].freq) ||
Sunil Ravi99c035e2024-07-12 01:42:03 +00006787 (mld_params->mld_links[link_id].disabled &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006788 nla_put_flag(msg,
6789 NL80211_ATTR_MLO_LINK_DISABLED)) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00006790 (mld_params->mld_links[link_id].ies &&
Sunil Ravi99c035e2024-07-12 01:42:03 +00006791 mld_params->mld_links[link_id].ies_len &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00006792 nla_put(msg, NL80211_ATTR_IE,
6793 mld_params->mld_links[link_id].ies_len,
6794 mld_params->mld_links[link_id].ies)))
6795 return -1;
6796
6797 os_memcpy(drv->sta_mlo_info.links[link_id].bssid,
6798 mld_params->mld_links[link_id].bssid,
6799 ETH_ALEN);
6800 nla_nest_end(msg, attr);
Sunil Ravi77d572f2023-01-17 23:58:31 +00006801 }
6802
6803 nla_nest_end(msg, links);
6804
6805 os_memcpy(drv->sta_mlo_info.ap_mld_addr,
6806 params->mld_params.mld_addr, ETH_ALEN);
6807 drv->sta_mlo_info.assoc_link_id = mld_params->assoc_link_id;
6808 drv->sta_mlo_info.req_links = mld_params->valid_links;
6809 }
6810
Paul Stewart092955c2017-02-06 09:13:09 -08006811 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
6812 return -1;
6813
Sunil Ravi77d572f2023-01-17 23:58:31 +00006814 if (params->bssid && !params->mld_params.mld_addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006815 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6816 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006817 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
6818 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006819 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006820
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006821 if (params->bssid_hint) {
6822 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
6823 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006824 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
6825 params->bssid_hint))
6826 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006827 }
6828
Sunil Ravi77d572f2023-01-17 23:58:31 +00006829 if (params->freq.freq && !params->mld_params.mld_addr) {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006830 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006831 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
6832 params->freq.freq))
6833 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006834 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006835 } else
6836 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006837
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006838 if (params->freq_hint) {
6839 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006840 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
6841 params->freq_hint))
6842 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006843 }
6844
Hai Shalomc3565922019-10-28 11:58:20 -07006845 if (params->freq.edmg.channels && params->freq.edmg.bw_config) {
6846 wpa_printf(MSG_DEBUG,
6847 " * EDMG configuration: channels=0x%x bw_config=%d",
6848 params->freq.edmg.channels,
6849 params->freq.edmg.bw_config);
6850 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_CHANNELS,
6851 params->freq.edmg.channels) ||
6852 nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_BW_CONFIG,
6853 params->freq.edmg.bw_config))
6854 return -1;
6855 }
6856
Dmitry Shmidt04949592012-07-19 12:16:46 -07006857 if (params->bg_scan_period >= 0) {
6858 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
6859 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006860 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
6861 params->bg_scan_period))
6862 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006863 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006865 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08006866 wpa_printf(MSG_DEBUG, " * SSID=%s",
6867 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006868 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
6869 params->ssid))
6870 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006871 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006872 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006873 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6874 drv->ssid_len = params->ssid_len;
6875 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006877 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006878 if (params->wpa_ie &&
6879 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
6880 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006881
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006882 if (params->wpa_proto) {
6883 enum nl80211_wpa_versions ver = 0;
6884
6885 if (params->wpa_proto & WPA_PROTO_WPA)
6886 ver |= NL80211_WPA_VERSION_1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006887 if (params->wpa_proto & WPA_PROTO_RSN) {
6888#if !defined(CONFIG_DRIVER_NL80211_BRCM) && !defined(CONFIG_DRIVER_NL80211_SYNA)
6889 /*
6890 * NL80211_ATTR_SAE_PASSWORD is related and was added
6891 * at the same time as NL80211_WPA_VERSION_3.
6892 */
6893 if (nl80211_attr_supported(drv,
6894 NL80211_ATTR_SAE_PASSWORD) &&
6895 wpa_key_mgmt_sae(params->key_mgmt_suite))
6896 ver |= NL80211_WPA_VERSION_3;
6897 else
6898#endif
6899 ver |= NL80211_WPA_VERSION_2;
6900 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006901
6902 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006903 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
6904 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006905 }
6906
6907 if (params->pairwise_suite != WPA_CIPHER_NONE) {
6908 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
6909 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006910 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
6911 cipher))
6912 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006913 }
6914
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006915 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
6916 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
6917 /*
6918 * This is likely to work even though many drivers do not
6919 * advertise support for operations without GTK.
6920 */
6921 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
6922 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006923 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
6924 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006925 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
6926 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006927 }
6928
6929 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
6930 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
6931 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
6932 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07006933 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07006934 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
6935 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006936 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07006937 params->key_mgmt_suite == WPA_KEY_MGMT_SAE ||
Sunil Ravi89eba102022-09-13 21:04:37 -07006938 params->key_mgmt_suite == WPA_KEY_MGMT_SAE_EXT_KEY ||
Hai Shalom021b0b52019-04-10 11:17:58 -07006939 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE ||
Sunil Ravi89eba102022-09-13 21:04:37 -07006940 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE_EXT_KEY ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08006941 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006942 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07006943 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X_SHA384 ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006944 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
6945 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
6946 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07006947 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
6948 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006949 params->key_mgmt_suite == WPA_KEY_MGMT_DPP ||
6950 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA384) {
Sunil Ravi89eba102022-09-13 21:04:37 -07006951 u32 *mgmt;
6952 unsigned int akm_count = 1, i;
6953
6954 /*
6955 * Make sure the driver has capability to handle default AKM in
6956 * key_mgmt_suite plus allowed AKMs in allowed_key_mgmts.
6957 */
6958 if (drv->capa.max_num_akms <=
6959 num_bits_set(params->allowed_key_mgmts)) {
6960 wpa_printf(MSG_INFO,
6961 "nl80211: Not enough support for the allowed AKMs (max_num_akms=%u <= num_bits_set=%u)",
6962 drv->capa.max_num_akms,
6963 num_bits_set(params->allowed_key_mgmts));
6964 return -1;
6965 }
6966
6967 mgmt = os_malloc(sizeof(u32) * drv->capa.max_num_akms);
6968 if (!mgmt)
6969 return -1;
6970
6971 mgmt[0] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006972
6973 switch (params->key_mgmt_suite) {
6974 case WPA_KEY_MGMT_CCKM:
Sunil Ravi89eba102022-09-13 21:04:37 -07006975 mgmt[0] = RSN_AUTH_KEY_MGMT_CCKM;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006976 break;
6977 case WPA_KEY_MGMT_IEEE8021X:
Sunil Ravi89eba102022-09-13 21:04:37 -07006978 mgmt[0] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006979 break;
6980 case WPA_KEY_MGMT_FT_IEEE8021X:
Sunil Ravi89eba102022-09-13 21:04:37 -07006981 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006982 break;
6983 case WPA_KEY_MGMT_FT_PSK:
Sunil Ravi89eba102022-09-13 21:04:37 -07006984 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_PSK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006985 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07006986 case WPA_KEY_MGMT_IEEE8021X_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07006987 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07006988 break;
6989 case WPA_KEY_MGMT_PSK_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07006990 mgmt[0] = RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07006991 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006992 case WPA_KEY_MGMT_OSEN:
Sunil Ravi89eba102022-09-13 21:04:37 -07006993 mgmt[0] = RSN_AUTH_KEY_MGMT_OSEN;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006994 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07006995 case WPA_KEY_MGMT_SAE:
Sunil Ravi89eba102022-09-13 21:04:37 -07006996 mgmt[0] = RSN_AUTH_KEY_MGMT_SAE;
6997 break;
6998 case WPA_KEY_MGMT_SAE_EXT_KEY:
6999 mgmt[0] = RSN_AUTH_KEY_MGMT_SAE_EXT_KEY;
Hai Shalom021b0b52019-04-10 11:17:58 -07007000 break;
7001 case WPA_KEY_MGMT_FT_SAE:
Sunil Ravi89eba102022-09-13 21:04:37 -07007002 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_SAE;
7003 break;
7004 case WPA_KEY_MGMT_FT_SAE_EXT_KEY:
7005 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY;
Hai Shalom021b0b52019-04-10 11:17:58 -07007006 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007007 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
Sunil Ravi89eba102022-09-13 21:04:37 -07007008 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007009 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007010 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
Sunil Ravi89eba102022-09-13 21:04:37 -07007011 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007012 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07007013 case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
Sunil Ravi89eba102022-09-13 21:04:37 -07007014 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384;
Hai Shalom021b0b52019-04-10 11:17:58 -07007015 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007016 case WPA_KEY_MGMT_FILS_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07007017 mgmt[0] = RSN_AUTH_KEY_MGMT_FILS_SHA256;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007018 break;
7019 case WPA_KEY_MGMT_FILS_SHA384:
Sunil Ravi89eba102022-09-13 21:04:37 -07007020 mgmt[0] = RSN_AUTH_KEY_MGMT_FILS_SHA384;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007021 break;
7022 case WPA_KEY_MGMT_FT_FILS_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07007023 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007024 break;
7025 case WPA_KEY_MGMT_FT_FILS_SHA384:
Sunil Ravi89eba102022-09-13 21:04:37 -07007026 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007027 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007028 case WPA_KEY_MGMT_OWE:
Sunil Ravi89eba102022-09-13 21:04:37 -07007029 mgmt[0] = RSN_AUTH_KEY_MGMT_OWE;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007030 break;
7031 case WPA_KEY_MGMT_DPP:
Sunil Ravi89eba102022-09-13 21:04:37 -07007032 mgmt[0] = RSN_AUTH_KEY_MGMT_DPP;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007033 break;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007034 case WPA_KEY_MGMT_IEEE8021X_SHA384:
7035 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SHA384;
7036 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007037 case WPA_KEY_MGMT_PSK:
7038 default:
Sunil Ravi89eba102022-09-13 21:04:37 -07007039 mgmt[0] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007040 break;
7041 }
Sunil Ravi89eba102022-09-13 21:04:37 -07007042
7043 if (drv->capa.max_num_akms > 1) {
7044 akm_count += wpa_key_mgmt_to_suites(
7045 params->allowed_key_mgmts, &mgmt[1],
7046 drv->capa.max_num_akms - 1);
7047 }
7048
7049 for (i = 0; i < akm_count; i++)
7050 wpa_printf(MSG_DEBUG, " * akm[%d]=0x%x", i, mgmt[i]);
7051
7052 if (nla_put(msg, NL80211_ATTR_AKM_SUITES,
7053 akm_count * sizeof(u32), mgmt)) {
7054 os_free(mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007055 return -1;
Sunil Ravi89eba102022-09-13 21:04:37 -07007056 }
7057
7058 os_free(mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007059 }
7060
Isaac Chiou6ce580d2024-04-24 17:07:24 +08007061#if (defined(CONFIG_DRIVER_NL80211_BRCM) && !defined(WIFI_BRCM_OPEN_SOURCE_MULTI_AKM)) || \
7062 defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawad14709082022-03-17 14:25:11 +05307063 if (IS_CROSS_AKM_ROAM_KEY_MGMT(params->key_mgmt_suite)) {
7064 int num_suites;
7065 u32 suites[NL80211_MAX_NR_AKM_SUITES];
7066
7067 wpa_printf(MSG_INFO, "nl80211: key_mgmt_suites=0x%x",
7068 params->key_mgmt_suite);
7069 num_suites = wpa_cross_akm_key_mgmt_to_suites(params->key_mgmt_suite,
7070 suites, ARRAY_SIZE(suites));
7071 if (num_suites &&
7072 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32), suites)) {
7073 wpa_printf(MSG_ERROR, "Updating multi akm_suite failed");
7074 return -1;
7075 }
7076 }
Isaac Chiou6ce580d2024-04-24 17:07:24 +08007077#endif /* (CONFIG_DRIVER_NL80211_BRCM && !WIFI_BRCM_OPEN_SOURCE_MULTI_AKM) ||
7078 * CONFIG_DRIVER_NL80211_SYNA */
Hai Shalomc3565922019-10-28 11:58:20 -07007079 if (params->req_handshake_offload &&
Hai Shalom74f70d42019-02-11 14:42:39 -08007080 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)) {
7081 wpa_printf(MSG_DEBUG, " * WANT_1X_4WAY_HS");
7082 if (nla_put_flag(msg, NL80211_ATTR_WANT_1X_4WAY_HS))
7083 return -1;
7084 }
7085
Roshan Pius3a1667e2018-07-03 15:17:14 -07007086 /* Add PSK in case of 4-way handshake offload */
7087 if (params->psk &&
Hai Shalom74f70d42019-02-11 14:42:39 -08007088 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07007089 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
7090 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
7091 return -1;
7092 }
7093
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007094 if (nl80211_put_control_port(drv, msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007095 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007096
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07007097 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
7098 (params->pairwise_suite == WPA_CIPHER_NONE ||
7099 params->pairwise_suite == WPA_CIPHER_WEP104 ||
7100 params->pairwise_suite == WPA_CIPHER_WEP40) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007101 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07007102 return -1;
7103
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007104 if (params->rrm_used) {
7105 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007106 if ((!((drv_rrm_flags &
7107 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
7108 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
7109 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007110 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
7111 return -1;
7112 }
7113
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007114 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007115 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007116
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007117 if (params->p2p)
7118 wpa_printf(MSG_DEBUG, " * P2P group");
7119
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007120 if (params->pbss) {
7121 wpa_printf(MSG_DEBUG, " * PBSS");
7122 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
7123 return -1;
7124 }
7125
Dmitry Shmidte4663042016-04-04 10:07:49 -07007126 drv->connect_reassoc = 0;
7127 if (params->prev_bssid) {
7128 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
7129 MAC2STR(params->prev_bssid));
7130 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
7131 params->prev_bssid))
7132 return -1;
7133 drv->connect_reassoc = 1;
7134 }
7135
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007136 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
7137 nl80211_put_fils_connect_params(drv, params, msg) != 0)
7138 return -1;
7139
Sunil Ravi89eba102022-09-13 21:04:37 -07007140 if ((wpa_key_mgmt_sae(params->key_mgmt_suite) ||
7141 wpa_key_mgmt_sae(params->allowed_key_mgmts)) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07007142 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
7143 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
7144 return -1;
7145
Sunil Ravi036cec52023-03-29 11:35:17 -07007146 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
7147 nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT))
7148 return -1;
7149
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007150 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007151}
7152
7153
7154static int wpa_driver_nl80211_try_connect(
7155 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007156 struct wpa_driver_associate_params *params,
Hai Shalomc1a21442022-02-04 13:43:00 -08007157 struct i802_bss *bss)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007158{
7159 struct nl_msg *msg;
7160 enum nl80211_auth_type type;
7161 int ret;
7162 int algs;
7163
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007164#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007165 if (params->req_key_mgmt_offload && params->psk &&
Sunil Ravi89eba102022-09-13 21:04:37 -07007166 (wpa_key_mgmt_wpa_psk_no_sae(params->key_mgmt_suite) ||
7167 wpa_key_mgmt_wpa_psk_no_sae(params->allowed_key_mgmts))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007168 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
7169 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
7170 if (ret)
7171 return ret;
7172 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007173#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007174
7175 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
7176 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007177 if (!msg)
7178 return -1;
7179
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007180 ret = nl80211_connect_common(drv, params, msg);
7181 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007182 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007183
Roshan Pius3a1667e2018-07-03 15:17:14 -07007184 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
7185 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
7186 goto fail;
7187
7188 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
7189 (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
7190 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
7191 goto fail;
7192
Hai Shalom60840252021-02-19 19:02:11 -08007193#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -07007194 if ((wpa_key_mgmt_sae(params->key_mgmt_suite) ||
7195 wpa_key_mgmt_sae(params->allowed_key_mgmts)) &&
Hai Shalom60840252021-02-19 19:02:11 -08007196 nl80211_put_sae_pwe(msg, params->sae_pwe) < 0)
7197 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007198
7199 /* Add SAE password in case of SAE authentication offload */
7200 if ((params->sae_password || params->passphrase) &&
7201 (drv->capa.flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_STA)) {
7202 const char *password;
7203 size_t pwd_len;
7204
7205 if (params->sae_password && params->sae_password_id) {
7206 wpa_printf(MSG_INFO,
7207 "nl80211: Use of SAE password identifiers not supported with driver-based SAE");
7208 goto fail;
7209 }
7210
7211 password = params->sae_password;
7212 if (!password)
7213 password = params->passphrase;
7214 pwd_len = os_strlen(password);
7215 wpa_printf(MSG_DEBUG, " * SAE password");
7216 if (nla_put(msg, NL80211_ATTR_SAE_PASSWORD, pwd_len, password))
7217 goto fail;
7218 }
Hai Shalom60840252021-02-19 19:02:11 -08007219#endif /* CONFIG_SAE */
7220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007221 algs = 0;
7222 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7223 algs++;
7224 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7225 algs++;
7226 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7227 algs++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007228 if (params->auth_alg & WPA_AUTH_ALG_FILS)
7229 algs++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007230 if (params->auth_alg & WPA_AUTH_ALG_FT)
7231 algs++;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007232 if (params->auth_alg & WPA_AUTH_ALG_SAE)
7233 algs++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007234 if (algs > 1) {
7235 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
7236 "selection");
7237 goto skip_auth_type;
7238 }
7239
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007240 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007241 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007242 if (type == NL80211_AUTHTYPE_MAX ||
7243 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007244 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007245
7246skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007247 ret = nl80211_set_conn_keys(params, msg);
7248 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007249 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007250
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007251 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
7252 goto fail;
7253 ret = send_and_recv(drv->global, bss->nl_connect, msg, NULL, NULL, NULL,
7254 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007255 msg = NULL;
7256 if (ret) {
7257 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7258 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007259 } else {
Hai Shalom60840252021-02-19 19:02:11 -08007260#ifdef CONFIG_DRIVER_NL80211_QCA
7261 drv->roam_indication_done = false;
7262#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007263 wpa_printf(MSG_DEBUG,
7264 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007265 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007266
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007267fail:
Hai Shalom74f70d42019-02-11 14:42:39 -08007268 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007269 nlmsg_free(msg);
7270 return ret;
7271
7272}
7273
7274
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007275static int wpa_driver_nl80211_connect(
7276 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007277 struct wpa_driver_associate_params *params,
Hai Shalomc1a21442022-02-04 13:43:00 -08007278 struct i802_bss *bss)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007279{
Jithu Jancea7c60b42014-12-03 18:54:40 +05307280 int ret;
7281
7282 /* Store the connection attempted bssid for future use */
7283 if (params->bssid)
7284 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
7285 else
7286 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
7287
Hai Shalomc1a21442022-02-04 13:43:00 -08007288 ret = wpa_driver_nl80211_try_connect(drv, params, bss);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007289 if (ret == -EALREADY) {
7290 /*
7291 * cfg80211 does not currently accept new connections if
7292 * we are already connected. As a workaround, force
7293 * disconnection and try again.
7294 */
7295 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7296 "disconnecting before reassociation "
7297 "attempt");
7298 if (wpa_driver_nl80211_disconnect(
Hai Shalomc1a21442022-02-04 13:43:00 -08007299 drv, WLAN_REASON_PREV_AUTH_NOT_VALID, bss))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007300 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007301 ret = wpa_driver_nl80211_try_connect(drv, params, bss);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007302 }
7303 return ret;
7304}
7305
7306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007307static int wpa_driver_nl80211_associate(
7308 void *priv, struct wpa_driver_associate_params *params)
7309{
7310 struct i802_bss *bss = priv;
7311 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007312 struct nl80211_err_info err_info;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007313 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007314 struct nl_msg *msg;
7315
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007316 nl80211_unmask_11b_rates(bss);
7317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007318 if (params->mode == IEEE80211_MODE_AP)
7319 return wpa_driver_nl80211_ap(drv, params);
7320
7321 if (params->mode == IEEE80211_MODE_IBSS)
7322 return wpa_driver_nl80211_ibss(drv, params);
7323
7324 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007325 enum nl80211_iftype nlmode = params->p2p ?
7326 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
7327
7328 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007329 return -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08007330
Hai Shalomc1a21442022-02-04 13:43:00 -08007331 return wpa_driver_nl80211_connect(drv, params, bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007332 }
7333
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007334 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007335
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007336 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
7337 drv->ifindex);
7338 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007339 if (!msg)
7340 return -1;
7341
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007342 ret = nl80211_connect_common(drv, params, msg);
7343 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007344 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007345
Roshan Pius3a1667e2018-07-03 15:17:14 -07007346 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
7347 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
7348 goto fail;
7349
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08007350 if (params->fils_kek) {
7351 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
7352 (unsigned int) params->fils_kek_len);
7353 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
7354 params->fils_kek))
7355 goto fail;
7356 }
7357 if (params->fils_nonces) {
7358 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
7359 params->fils_nonces,
7360 params->fils_nonces_len);
7361 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
7362 params->fils_nonces_len, params->fils_nonces))
7363 goto fail;
7364 }
7365
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007366 if (!TEST_FAIL_TAG("assoc")) {
7367 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
7368 goto fail;
7369 ret = send_and_recv(drv->global, drv->first_bss->nl_connect,
7370 msg, NULL, NULL, NULL, NULL, &err_info);
7371 msg = NULL;
7372 } else {
7373 int i;
7374
7375 /* Error and force TEST_FAIL checking for each link */
7376 ret = -EINVAL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00007377 for_each_link(params->mld_params.valid_links, i) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007378 if (TEST_FAIL_TAG("link"))
7379 err_info.link_id = i;
7380 }
7381 }
7382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007383 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007384 wpa_dbg(drv->ctx, MSG_DEBUG,
7385 "nl80211: MLME command failed (assoc): ret=%d (%s)",
7386 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007387 nl80211_dump_scan(drv);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007388
7389 /* Mark failed link within params */
7390 if (err_info.link_id >= 0) {
7391 if (err_info.link_id >= MAX_NUM_MLD_LINKS ||
7392 !(params->mld_params.valid_links &
7393 BIT(err_info.link_id))) {
7394 wpa_printf(MSG_DEBUG,
7395 "nl80211: Invalid errorred link_id %d",
7396 err_info.link_id);
7397 goto fail;
7398 }
7399 params->mld_params.mld_links[err_info.link_id].error =
7400 ret;
7401 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007402 } else {
7403 wpa_printf(MSG_DEBUG,
7404 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007405 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007406
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007407fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007408 nlmsg_free(msg);
7409 return ret;
7410}
7411
7412
7413static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007414 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007415{
7416 struct nl_msg *msg;
7417 int ret = -ENOBUFS;
7418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007419 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
7420 ifindex, mode, nl80211_iftype_str(mode));
7421
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007422 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
7423 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
7424 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007425
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007426 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007427 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007428 if (!ret)
7429 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007430fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007431 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007432 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7433 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7434 return ret;
7435}
7436
7437
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007438static int wpa_driver_nl80211_set_mode_impl(
7439 struct i802_bss *bss,
7440 enum nl80211_iftype nlmode,
7441 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007442{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007443 struct wpa_driver_nl80211_data *drv = bss->drv;
7444 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007445 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007446 int was_ap = is_ap_interface(drv->nlmode);
7447 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007448 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007449
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007450 if (TEST_FAIL())
7451 return -1;
7452
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007453 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7454 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
7455 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007456
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007457 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007458 drv->nlmode = nlmode;
7459 ret = 0;
7460 goto done;
7461 }
7462
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007463 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007464 return -1;
7465
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007466 if (nlmode == drv->nlmode) {
7467 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7468 "requested mode - ignore error");
7469 ret = 0;
7470 goto done; /* Already in the requested mode */
7471 }
7472
7473 /* mac80211 doesn't allow mode changes while the device is up, so
7474 * take the device down, try to set the mode again, and bring the
7475 * device back up.
7476 */
7477 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7478 "interface down");
7479 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007480 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007481 if (res == -EACCES || res == -ENODEV)
7482 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007483 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007484 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7485 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007486 os_sleep(0, 100000);
7487 continue;
7488 }
7489
7490 /*
7491 * Setting the mode will fail for some drivers if the phy is
7492 * on a frequency that the mode is disallowed in.
7493 */
7494 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007495 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007496 if (res) {
7497 wpa_printf(MSG_DEBUG,
7498 "nl80211: Failed to set frequency on interface");
7499 }
7500 }
7501
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007502 if (i == 0 && was_ap && !is_ap_interface(nlmode) &&
7503 bss->brname[0] &&
7504 (bss->added_if_into_bridge || bss->already_in_bridge)) {
7505 wpa_printf(MSG_DEBUG,
7506 "nl80211: Remove AP interface %s temporarily from the bridge %s to allow its mode to be set to STATION",
7507 bss->ifname, bss->brname);
7508 if (linux_br_del_if(drv->global->ioctl_sock,
7509 bss->brname, bss->ifname) < 0)
7510 wpa_printf(MSG_INFO,
7511 "nl80211: Failed to remove interface %s from bridge %s: %s",
7512 bss->ifname, bss->brname,
7513 strerror(errno));
7514 }
7515
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007516 /* Try to set the mode again while the interface is down */
7517 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7518 if (mode_switch_res == -EBUSY) {
7519 wpa_printf(MSG_DEBUG,
7520 "nl80211: Delaying mode set while interface going down");
7521 os_sleep(0, 100000);
7522 continue;
7523 }
7524 ret = mode_switch_res;
7525 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007526 }
7527
7528 if (!ret) {
7529 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7530 "interface is down");
7531 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007532 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007533 }
7534
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007535 /* Bring the interface back up */
7536 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
7537 if (res != 0) {
7538 wpa_printf(MSG_DEBUG,
7539 "nl80211: Failed to set interface up after switching mode");
7540 ret = -1;
7541 }
7542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007543done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007544 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007545 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7546 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007547 return ret;
7548 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007549
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007550 if (is_p2p_net_interface(nlmode)) {
7551 wpa_printf(MSG_DEBUG,
7552 "nl80211: Interface %s mode change to P2P - disable 11b rates",
7553 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007554 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007555 } else if (drv->disabled_11b_rates) {
7556 wpa_printf(MSG_DEBUG,
7557 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
7558 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007559 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007560 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007561
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007562 if (is_ap_interface(nlmode)) {
7563 nl80211_mgmt_unsubscribe(bss, "start AP");
7564 /* Setup additional AP mode functionality if needed */
7565 if (nl80211_setup_ap(bss))
7566 return -1;
7567 } else if (was_ap) {
7568 /* Remove additional AP mode functionality */
7569 nl80211_teardown_ap(bss);
7570 } else {
7571 nl80211_mgmt_unsubscribe(bss, "mode change");
7572 }
7573
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007574 if (is_mesh_interface(nlmode) &&
7575 nl80211_mgmt_subscribe_mesh(bss))
7576 return -1;
7577
Dmitry Shmidt04949592012-07-19 12:16:46 -07007578 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007579 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007580 nl80211_mgmt_subscribe_non_ap(bss) < 0)
7581 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7582 "frame processing - ignore for now");
7583
7584 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007585}
7586
7587
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007588void nl80211_restore_ap_mode(struct i802_bss *bss)
7589{
7590 struct wpa_driver_nl80211_data *drv = bss->drv;
7591 int was_ap = is_ap_interface(drv->nlmode);
leslc3979c32021-03-29 22:34:02 +08007592 int br_ifindex;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007593
7594 wpa_driver_nl80211_set_mode(bss, drv->ap_scan_as_station);
7595 if (!was_ap && is_ap_interface(drv->ap_scan_as_station) &&
7596 bss->brname[0] &&
7597 (bss->added_if_into_bridge || bss->already_in_bridge)) {
7598 wpa_printf(MSG_DEBUG,
7599 "nl80211: Add AP interface %s back into the bridge %s",
7600 bss->ifname, bss->brname);
7601 if (linux_br_add_if(drv->global->ioctl_sock, bss->brname,
7602 bss->ifname) < 0) {
7603 wpa_printf(MSG_WARNING,
7604 "nl80211: Failed to add interface %s into bridge %s: %s",
7605 bss->ifname, bss->brname, strerror(errno));
7606 }
leslc3979c32021-03-29 22:34:02 +08007607 br_ifindex = if_nametoindex(bss->brname);
7608 add_ifidx(drv, br_ifindex, drv->ifindex);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007609 }
7610 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
7611}
7612
7613
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007614int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7615 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007616{
7617 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
7618}
7619
7620
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007621static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
7622 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007623{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007624 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007625 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007626}
7627
7628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007629static int wpa_driver_nl80211_get_capa(void *priv,
7630 struct wpa_driver_capa *capa)
7631{
7632 struct i802_bss *bss = priv;
7633 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07007634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635 if (!drv->has_capability)
7636 return -1;
7637 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007638 if (drv->extended_capa && drv->extended_capa_mask) {
7639 capa->extended_capa = drv->extended_capa;
7640 capa->extended_capa_mask = drv->extended_capa_mask;
7641 capa->extended_capa_len = drv->extended_capa_len;
7642 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007643
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007644 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007645}
7646
7647
7648static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7649{
7650 struct i802_bss *bss = priv;
7651 struct wpa_driver_nl80211_data *drv = bss->drv;
7652
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007653 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
7654 bss->ifname, drv->operstate, state,
7655 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007656 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007657 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007658 state ? IF_OPER_UP : IF_OPER_DORMANT);
7659}
7660
7661
7662static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7663{
7664 struct i802_bss *bss = priv;
7665 struct wpa_driver_nl80211_data *drv = bss->drv;
7666 struct nl_msg *msg;
7667 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007668 int ret;
Sunil Ravi89eba102022-09-13 21:04:37 -07007669 const u8 *connected_addr = drv->sta_mlo_info.valid_links ?
7670 drv->sta_mlo_info.ap_mld_addr : drv->bssid;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007671
Sunil Ravi89eba102022-09-13 21:04:37 -07007672 if (!drv->associated && is_zero_ether_addr(connected_addr) &&
7673 !authorized) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007674 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
7675 return 0;
7676 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007677
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007678 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
Sunil Ravi89eba102022-09-13 21:04:37 -07007679 MACSTR, authorized ? "" : "un", MAC2STR(connected_addr));
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007681 os_memset(&upd, 0, sizeof(upd));
7682 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7683 if (authorized)
7684 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007685
7686 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
Sunil Ravi89eba102022-09-13 21:04:37 -07007687 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007688 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
7689 nlmsg_free(msg);
7690 return -ENOBUFS;
7691 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007692
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007693 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007694 if (!ret)
7695 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007696 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
7697 ret, strerror(-ret));
7698 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007699}
7700
7701
Jouni Malinen75ecf522011-06-27 15:19:46 -07007702/* Set kernel driver on given frequency (MHz) */
7703static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007704{
Jouni Malinen75ecf522011-06-27 15:19:46 -07007705 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007706 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007707}
7708
7709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007710static inline int min_int(int a, int b)
7711{
7712 if (a < b)
7713 return a;
7714 return b;
7715}
7716
7717
7718static int get_key_handler(struct nl_msg *msg, void *arg)
7719{
7720 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7721 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7722
7723 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7724 genlmsg_attrlen(gnlh, 0), NULL);
7725
7726 /*
7727 * TODO: validate the key index and mac address!
7728 * Otherwise, there's a race condition as soon as
7729 * the kernel starts sending key notifications.
7730 */
7731
7732 if (tb[NL80211_ATTR_KEY_SEQ])
7733 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7734 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
Hai Shalom021b0b52019-04-10 11:17:58 -07007735 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007736 return NL_SKIP;
7737}
7738
7739
7740static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007741 int idx, int link_id, u8 *seq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007742{
7743 struct i802_bss *bss = priv;
7744 struct wpa_driver_nl80211_data *drv = bss->drv;
7745 struct nl_msg *msg;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007746 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007747
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007748 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
7749 NL80211_CMD_GET_KEY);
7750 if (!msg ||
7751 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007752 (link_id != NL80211_DRV_LINK_ID_NA &&
7753 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007754 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
7755 nlmsg_free(msg);
7756 return -ENOBUFS;
7757 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007758
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007759 os_memset(seq, 0, 6);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007760 res = send_and_recv_resp(drv, msg, get_key_handler, seq);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007761 if (res) {
7762 wpa_printf(MSG_DEBUG,
7763 "nl80211: Failed to get current TX sequence for a key (link_id=%d idx=%d): %d (%s)",
7764 link_id, idx, res, strerror(-res));
7765 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007766
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007767 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007768}
7769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007770
7771static int i802_set_rts(void *priv, int rts)
7772{
7773 struct i802_bss *bss = priv;
7774 struct wpa_driver_nl80211_data *drv = bss->drv;
7775 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007776 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007777 u32 val;
7778
Hai Shalom021b0b52019-04-10 11:17:58 -07007779 if (rts >= 2347 || rts == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007780 val = (u32) -1;
7781 else
7782 val = rts;
7783
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007784 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
7785 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
7786 nlmsg_free(msg);
7787 return -ENOBUFS;
7788 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007789
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007790 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007791 if (!ret)
7792 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007793 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7794 "%d (%s)", rts, ret, strerror(-ret));
7795 return ret;
7796}
7797
7798
7799static int i802_set_frag(void *priv, int frag)
7800{
7801 struct i802_bss *bss = priv;
7802 struct wpa_driver_nl80211_data *drv = bss->drv;
7803 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007804 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007805 u32 val;
7806
Hai Shalom021b0b52019-04-10 11:17:58 -07007807 if (frag >= 2346 || frag == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007808 val = (u32) -1;
7809 else
7810 val = frag;
7811
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007812 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
7813 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
7814 nlmsg_free(msg);
7815 return -ENOBUFS;
7816 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007817
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007818 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007819 if (!ret)
7820 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007821 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7822 "%d: %d (%s)", frag, ret, strerror(-ret));
7823 return ret;
7824}
7825
7826
Sunil Ravi7f769292024-07-23 22:21:32 +00007827static int i802_flush(void *priv, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007828{
7829 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007830 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007831 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007832
Sunil Ravi7f769292024-07-23 22:21:32 +00007833 if (link_id == NL80211_DRV_LINK_ID_NA)
7834 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
7835 bss->ifname);
7836 else
7837 wpa_printf(MSG_DEBUG,
7838 "nl80211: flush -> DEL_STATION %s (with link %d)",
7839 bss->ifname, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007840
7841 /*
7842 * XXX: FIX! this needs to flush all VLANs too
7843 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007844 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
Sunil Ravi7f769292024-07-23 22:21:32 +00007845 if (link_id >= 0 && (bss->valid_links & BIT(link_id)) &&
7846 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
7847 goto fail;
7848
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007849 res = send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007850 if (res) {
7851 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7852 "(%s)", res, strerror(-res));
7853 }
7854 return res;
Sunil Ravi7f769292024-07-23 22:21:32 +00007855fail:
7856 nlmsg_free(msg);
7857 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007858}
7859
7860
Hai Shalom81f62d82019-07-22 12:10:00 -07007861static void get_sta_tid_stats(struct hostap_sta_driver_data *data,
7862 struct nlattr *attr)
7863{
7864 struct nlattr *tid_stats[NL80211_TID_STATS_MAX + 1], *tidattr;
7865 struct nlattr *txq_stats[NL80211_TXQ_STATS_MAX + 1];
7866 static struct nla_policy txq_stats_policy[NL80211_TXQ_STATS_MAX + 1] = {
7867 [NL80211_TXQ_STATS_BACKLOG_BYTES] = { .type = NLA_U32 },
7868 [NL80211_TXQ_STATS_BACKLOG_PACKETS] = { .type = NLA_U32 },
7869 };
7870 int rem;
7871
7872 nla_for_each_nested(tidattr, attr, rem) {
7873 if (nla_parse_nested(tid_stats, NL80211_TID_STATS_MAX,
7874 tidattr, NULL) != 0 ||
7875 !tid_stats[NL80211_TID_STATS_TXQ_STATS] ||
7876 nla_parse_nested(txq_stats, NL80211_TXQ_STATS_MAX,
7877 tid_stats[NL80211_TID_STATS_TXQ_STATS],
7878 txq_stats_policy) != 0)
7879 continue;
7880 /* sum the backlogs over all TIDs for station */
7881 if (txq_stats[NL80211_TXQ_STATS_BACKLOG_BYTES])
7882 data->backlog_bytes += nla_get_u32(
7883 txq_stats[NL80211_TXQ_STATS_BACKLOG_BYTES]);
7884 if (txq_stats[NL80211_TXQ_STATS_BACKLOG_PACKETS])
7885 data->backlog_bytes += nla_get_u32(
7886 txq_stats[NL80211_TXQ_STATS_BACKLOG_PACKETS]);
7887 }
7888}
7889
7890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007891static int get_sta_handler(struct nl_msg *msg, void *arg)
7892{
7893 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7894 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7895 struct hostap_sta_driver_data *data = arg;
7896 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7897 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7898 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7899 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7900 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007901 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007902 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7903 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007904 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007905 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007906 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
7907 [NL80211_STA_INFO_CONNECTED_TIME] = { .type = NLA_U32 },
7908 [NL80211_STA_INFO_BEACON_LOSS] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007909 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
7910 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007911 [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 },
7912 [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64 },
7913 [NL80211_STA_INFO_BEACON_RX] = { .type = NLA_U64 },
7914 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8},
Hai Shalom81f62d82019-07-22 12:10:00 -07007915 [NL80211_STA_INFO_RX_DURATION] = { .type = NLA_U64 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007916 [NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
7917 [NL80211_STA_INFO_ACK_SIGNAL_AVG] = { .type = NLA_S8 },
7918 [NL80211_STA_INFO_RX_MPDUS] = { .type = NLA_U32 },
7919 [NL80211_STA_INFO_FCS_ERROR_COUNT] = { .type = NLA_U32 },
Hai Shalom81f62d82019-07-22 12:10:00 -07007920 [NL80211_STA_INFO_TX_DURATION] = { .type = NLA_U64 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007921 };
7922 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
7923 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
7924 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
7925 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
7926 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
7927 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
7928 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
7929 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007930 [NL80211_RATE_INFO_HE_MCS] = { .type = NLA_U8 },
7931 [NL80211_RATE_INFO_HE_NSS] = { .type = NLA_U8 },
Sunil Ravi036cec52023-03-29 11:35:17 -07007932 [NL80211_RATE_INFO_HE_GI] = { .type = NLA_U8 },
7933 [NL80211_RATE_INFO_HE_DCM] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007934 };
7935
7936 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7937 genlmsg_attrlen(gnlh, 0), NULL);
7938
7939 /*
7940 * TODO: validate the interface and mac address!
7941 * Otherwise, there's a race condition as soon as
7942 * the kernel starts sending station notifications.
7943 */
7944
7945 if (!tb[NL80211_ATTR_STA_INFO]) {
7946 wpa_printf(MSG_DEBUG, "sta stats missing!");
7947 return NL_SKIP;
7948 }
7949 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7950 tb[NL80211_ATTR_STA_INFO],
7951 stats_policy)) {
7952 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7953 return NL_SKIP;
7954 }
7955
7956 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7957 data->inactive_msec =
7958 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007959 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007960 if (stats[NL80211_STA_INFO_RX_BYTES])
7961 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7962 if (stats[NL80211_STA_INFO_TX_BYTES])
7963 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007964 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
7965 stats[NL80211_STA_INFO_TX_BYTES64]) {
7966 /*
7967 * The driver supports 64-bit counters, so use them to override
7968 * the 32-bit values.
7969 */
7970 data->rx_bytes =
7971 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
7972 data->tx_bytes =
7973 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
7974 data->bytes_64bit = 1;
7975 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00007976 if (stats[NL80211_STA_INFO_SIGNAL])
7977 data->signal = (s8) nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007978 if (stats[NL80211_STA_INFO_RX_PACKETS])
7979 data->rx_packets =
7980 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7981 if (stats[NL80211_STA_INFO_TX_PACKETS])
7982 data->tx_packets =
7983 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007984 if (stats[NL80211_STA_INFO_TX_RETRIES])
7985 data->tx_retry_count =
7986 nla_get_u32(stats[NL80211_STA_INFO_TX_RETRIES]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007987 if (stats[NL80211_STA_INFO_TX_FAILED])
7988 data->tx_retry_failed =
7989 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007990 if (stats[NL80211_STA_INFO_SIGNAL_AVG])
7991 data->avg_signal =
7992 (s8) nla_get_u8(stats[NL80211_STA_INFO_SIGNAL_AVG]);
Hai Shalomc1a21442022-02-04 13:43:00 -08007993 if (stats[NL80211_STA_INFO_CONNECTED_TIME]) {
7994 data->connected_sec =
7995 nla_get_u32(stats[NL80211_STA_INFO_CONNECTED_TIME]);
7996 data->flags |= STA_DRV_DATA_CONN_TIME;
7997 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00007998 if (stats[NL80211_STA_INFO_BEACON_LOSS])
7999 data->beacon_loss_count =
8000 nla_get_u32(stats[NL80211_STA_INFO_BEACON_LOSS]);
8001 if (stats[NL80211_STA_INFO_EXPECTED_THROUGHPUT])
8002 data->expected_throughput =
8003 nla_get_u32(stats[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
8004 if (stats[NL80211_STA_INFO_RX_DROP_MISC])
8005 data->rx_drop_misc =
8006 nla_get_u64(stats[NL80211_STA_INFO_RX_DROP_MISC]);
8007 if (stats[NL80211_STA_INFO_BEACON_RX])
8008 data->beacons_count =
8009 nla_get_u64(stats[NL80211_STA_INFO_BEACON_RX]);
8010 if (stats[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
8011 data->avg_beacon_signal =
8012 (s8) nla_get_u8(stats[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
8013 if (stats[NL80211_STA_INFO_RX_DURATION])
8014 data->rx_airtime =
8015 nla_get_u64(stats[NL80211_STA_INFO_RX_DURATION]);
8016 if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
8017 data->last_ack_rssi =
8018 nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
8019 data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
8020 }
8021 if (stats[NL80211_STA_INFO_ACK_SIGNAL_AVG])
8022 data->avg_ack_signal =
8023 nla_get_s8(stats[NL80211_STA_INFO_ACK_SIGNAL_AVG]);
8024 if (stats[NL80211_STA_INFO_RX_MPDUS])
8025 data->rx_mpdus = nla_get_u32(stats[NL80211_STA_INFO_RX_MPDUS]);
8026 if (stats[NL80211_STA_INFO_FCS_ERROR_COUNT])
8027 data->fcs_error_count =
8028 nla_get_u32(stats[NL80211_STA_INFO_FCS_ERROR_COUNT]);
8029 if (stats[NL80211_STA_INFO_TX_DURATION])
8030 data->tx_airtime =
8031 nla_get_u64(stats[NL80211_STA_INFO_TX_DURATION]);
Hai Shalomc1a21442022-02-04 13:43:00 -08008032
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008033 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
8034 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
8035 stats[NL80211_STA_INFO_TX_BITRATE],
8036 rate_policy) == 0) {
8037 if (rate[NL80211_RATE_INFO_BITRATE32])
8038 data->current_tx_rate =
8039 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
8040 else if (rate[NL80211_RATE_INFO_BITRATE])
8041 data->current_tx_rate =
8042 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
8043
Sunil Ravi77d572f2023-01-17 23:58:31 +00008044 /* Convert from 100 kbps to kbps; it's a more convenient unit.
8045 * It's also safe up until ~1Tbps. */
8046 data->current_tx_rate = data->current_tx_rate * 100;
8047
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008048 if (rate[NL80211_RATE_INFO_MCS]) {
8049 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
8050 data->flags |= STA_DRV_DATA_TX_MCS;
8051 }
8052 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
8053 data->tx_vhtmcs =
8054 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
8055 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
8056 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008057 if (rate[NL80211_RATE_INFO_SHORT_GI]) {
8058 data->tx_guard_interval = GUARD_INTERVAL_0_4;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008059 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
Sunil Ravi036cec52023-03-29 11:35:17 -07008060 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008061 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
8062 data->tx_vht_nss =
8063 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
8064 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
8065 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00008066 if (rate[NL80211_RATE_INFO_HE_MCS]) {
8067 data->tx_hemcs =
8068 nla_get_u8(rate[NL80211_RATE_INFO_HE_MCS]);
8069 data->flags |= STA_DRV_DATA_TX_HE_MCS;
8070 }
8071 if (rate[NL80211_RATE_INFO_HE_NSS]) {
8072 data->tx_he_nss =
8073 nla_get_u8(rate[NL80211_RATE_INFO_HE_NSS]);
8074 data->flags |= STA_DRV_DATA_TX_HE_NSS;
8075 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008076 if (rate[NL80211_RATE_INFO_HE_GI]) {
8077 switch (nla_get_u8(rate[NL80211_RATE_INFO_HE_GI])) {
8078 case NL80211_RATE_INFO_HE_GI_0_8:
8079 data->tx_guard_interval = GUARD_INTERVAL_0_8;
8080 break;
8081 case NL80211_RATE_INFO_HE_GI_1_6:
8082 data->tx_guard_interval = GUARD_INTERVAL_1_6;
8083 break;
8084 case NL80211_RATE_INFO_HE_GI_3_2:
8085 data->tx_guard_interval = GUARD_INTERVAL_3_2;
8086 break;
8087 }
8088 data->flags |= STA_DRV_DATA_TX_HE_GI;
8089 }
8090 if (rate[NL80211_RATE_INFO_HE_DCM]) {
8091 data->tx_dcm =
8092 nla_get_u8(rate[NL80211_RATE_INFO_HE_DCM]);
8093 data->flags |= STA_DRV_DATA_TX_HE_DCM;
8094 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008095 }
8096
8097 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
8098 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
8099 stats[NL80211_STA_INFO_RX_BITRATE],
8100 rate_policy) == 0) {
8101 if (rate[NL80211_RATE_INFO_BITRATE32])
8102 data->current_rx_rate =
8103 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
8104 else if (rate[NL80211_RATE_INFO_BITRATE])
8105 data->current_rx_rate =
8106 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
8107
Sunil Ravi77d572f2023-01-17 23:58:31 +00008108 /* Convert from 100 kbps to kbps; it's a more convenient unit.
8109 * It's also safe up until ~1Tbps. */
8110 data->current_rx_rate = data->current_rx_rate * 100;
8111
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008112 if (rate[NL80211_RATE_INFO_MCS]) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00008113 data->rx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008114 data->flags |= STA_DRV_DATA_RX_MCS;
8115 }
8116 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
8117 data->rx_vhtmcs =
8118 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
8119 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
8120 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008121 if (rate[NL80211_RATE_INFO_SHORT_GI]) {
8122 data->rx_guard_interval = GUARD_INTERVAL_0_4;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008123 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
Sunil Ravi036cec52023-03-29 11:35:17 -07008124 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008125 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
8126 data->rx_vht_nss =
8127 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
8128 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
8129 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00008130 if (rate[NL80211_RATE_INFO_HE_MCS]) {
8131 data->rx_hemcs =
8132 nla_get_u8(rate[NL80211_RATE_INFO_HE_MCS]);
8133 data->flags |= STA_DRV_DATA_RX_HE_MCS;
8134 }
8135 if (rate[NL80211_RATE_INFO_HE_NSS]) {
8136 data->rx_he_nss =
8137 nla_get_u8(rate[NL80211_RATE_INFO_HE_NSS]);
8138 data->flags |= STA_DRV_DATA_RX_HE_NSS;
8139 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008140 if (rate[NL80211_RATE_INFO_HE_GI]) {
8141 switch (nla_get_u8(rate[NL80211_RATE_INFO_HE_GI])) {
8142 case NL80211_RATE_INFO_HE_GI_0_8:
8143 data->rx_guard_interval = GUARD_INTERVAL_0_8;
8144 break;
8145 case NL80211_RATE_INFO_HE_GI_1_6:
8146 data->rx_guard_interval = GUARD_INTERVAL_1_6;
8147 break;
8148 case NL80211_RATE_INFO_HE_GI_3_2:
8149 data->rx_guard_interval = GUARD_INTERVAL_3_2;
8150 break;
8151 }
8152 data->flags |= STA_DRV_DATA_RX_HE_GI;
8153 }
8154 if (rate[NL80211_RATE_INFO_HE_DCM]) {
8155 data->rx_dcm =
8156 nla_get_u8(rate[NL80211_RATE_INFO_HE_DCM]);
8157 data->flags |= STA_DRV_DATA_RX_HE_DCM;
8158 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008159 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008160
Hai Shalom81f62d82019-07-22 12:10:00 -07008161 if (stats[NL80211_STA_INFO_TID_STATS])
8162 get_sta_tid_stats(data, stats[NL80211_STA_INFO_TID_STATS]);
8163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008164 return NL_SKIP;
8165}
8166
Sunil Ravi77d572f2023-01-17 23:58:31 +00008167
8168int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
8169 const u8 *bssid,
8170 struct hostap_sta_driver_data *data)
8171{
8172 struct nl_msg *msg;
8173
8174 data->signal = -WPA_INVALID_NOISE;
8175 data->current_tx_rate = 0;
8176
8177 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
8178 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) {
8179 nlmsg_free(msg);
8180 return -ENOBUFS;
8181 }
8182
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008183 return send_and_recv_resp(drv, msg, get_sta_handler, data);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008184}
8185
8186
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008187static int i802_read_sta_data(struct i802_bss *bss,
8188 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008189 const u8 *addr)
8190{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008191 struct nl_msg *msg;
8192
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008193 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
8194 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8195 nlmsg_free(msg);
8196 return -ENOBUFS;
8197 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008198
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008199 return send_and_recv_resp(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008200}
8201
8202
8203static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008204 int cw_min, int cw_max, int burst_time,
8205 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008206{
8207 struct i802_bss *bss = priv;
8208 struct wpa_driver_nl80211_data *drv = bss->drv;
8209 struct nl_msg *msg;
8210 struct nlattr *txq, *params;
Hai Shalom74f70d42019-02-11 14:42:39 -08008211 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008212
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008213 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008214 if (!msg)
8215 return -1;
8216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008217 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
8218 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008219 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008220
8221 /* We are only sending parameters for a single TXQ at a time */
8222 params = nla_nest_start(msg, 1);
8223 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008224 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008225
8226 switch (queue) {
8227 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008228 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
8229 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008230 break;
8231 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008232 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
8233 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008234 break;
8235 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008236 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
8237 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008238 break;
8239 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008240 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
8241 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008242 break;
8243 }
8244 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
8245 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008246 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
8247 (burst_time * 100 + 16) / 32) ||
8248 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
8249 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
8250 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
8251 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008252
8253 nla_nest_end(msg, params);
8254
8255 nla_nest_end(msg, txq);
8256
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008257 if (link_id != NL80211_DRV_LINK_ID_NA &&
8258 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
8259 goto fail;
8260
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008261 res = send_and_recv_cmd(drv, msg);
Hai Shalom74f70d42019-02-11 14:42:39 -08008262 wpa_printf(MSG_DEBUG,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008263 "nl80211: link=%d: TX queue param set: queue=%d aifs=%d cw_min=%d cw_max=%d burst_time=%d --> res=%d",
8264 link_id, queue, aifs, cw_min, cw_max, burst_time, res);
Hai Shalom74f70d42019-02-11 14:42:39 -08008265 if (res == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008266 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008267 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008268fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008269 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008270 return -1;
8271}
8272
8273
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008274static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008275 const char *ifname, int vlan_id, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008276{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008277 struct wpa_driver_nl80211_data *drv = bss->drv;
8278 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008279 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008280
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008281 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
8282 ", ifname=%s[%d], vlan_id=%d)",
8283 bss->ifname, if_nametoindex(bss->ifname),
8284 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008285 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
8286 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07008287 (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD) &&
Hai Shalomfdcde762020-04-02 11:19:20 -07008288 nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id)) ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008289 (link_id != NL80211_DRV_LINK_ID_NA &&
8290 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008291 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
8292 nlmsg_free(msg);
8293 return -ENOBUFS;
8294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008295
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008296 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008297 if (ret < 0) {
8298 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8299 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8300 MAC2STR(addr), ifname, vlan_id, ret,
8301 strerror(-ret));
8302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008303 return ret;
8304}
8305
8306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008307static int i802_get_inact_sec(void *priv, const u8 *addr)
8308{
8309 struct hostap_sta_driver_data data;
8310 int ret;
8311
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08008312 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008313 data.inactive_msec = (unsigned long) -1;
8314 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008315 if (ret == -ENOENT)
8316 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008317 if (ret || data.inactive_msec == (unsigned long) -1)
8318 return -1;
8319 return data.inactive_msec / 1000;
8320}
8321
8322
8323static int i802_sta_clear_stats(void *priv, const u8 *addr)
8324{
8325#if 0
8326 /* TODO */
8327#endif
8328 return 0;
8329}
8330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008331
8332static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008333 u16 reason, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008334{
8335 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008336 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008337 struct ieee80211_mgmt mgmt;
Dmitry Shmidt29333592017-01-09 12:27:11 -08008338 u8 channel;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008339 struct i802_link *link = nl80211_get_link(bss, link_id);
Dmitry Shmidt29333592017-01-09 12:27:11 -08008340
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008341 if (ieee80211_freq_to_chan(link->freq, &channel) ==
Dmitry Shmidt29333592017-01-09 12:27:11 -08008342 HOSTAPD_MODE_IEEE80211AD) {
8343 /* Deauthentication is not used in DMG/IEEE 802.11ad;
8344 * disassociate the STA instead. */
8345 return i802_sta_disassoc(priv, own_addr, addr, reason);
8346 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008347
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008348 if (is_mesh_interface(drv->nlmode))
8349 return -1;
8350
Dmitry Shmidt04949592012-07-19 12:16:46 -07008351 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008352 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008354 memset(&mgmt, 0, sizeof(mgmt));
8355 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8356 WLAN_FC_STYPE_DEAUTH);
8357 memcpy(mgmt.da, addr, ETH_ALEN);
8358 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8359 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8360 mgmt.u.deauth.reason_code = host_to_le16(reason);
8361 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8362 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008363 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Sunil Ravi7f769292024-07-23 22:21:32 +00008364 0, NULL, 0, 0, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008365}
8366
8367
8368static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -07008369 u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008370{
8371 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008372 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008373 struct ieee80211_mgmt mgmt;
8374
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008375 if (is_mesh_interface(drv->nlmode))
8376 return -1;
8377
Dmitry Shmidt04949592012-07-19 12:16:46 -07008378 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008379 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008381 memset(&mgmt, 0, sizeof(mgmt));
8382 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8383 WLAN_FC_STYPE_DISASSOC);
8384 memcpy(mgmt.da, addr, ETH_ALEN);
8385 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8386 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8387 mgmt.u.disassoc.reason_code = host_to_le16(reason);
8388 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8389 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008390 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008391 0, NULL, 0, 0, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008392}
8393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008394
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008395static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
8396{
8397 char buf[200], *pos, *end;
8398 int i, res;
8399
8400 pos = buf;
8401 end = pos + sizeof(buf);
8402
8403 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008404 if (!drv->if_indices[i].ifindex)
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008405 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008406 res = os_snprintf(pos, end - pos, " %d(%d)",
Hai Shalom81f62d82019-07-22 12:10:00 -07008407 drv->if_indices[i].ifindex,
8408 drv->if_indices[i].reason);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008409 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008410 break;
8411 pos += res;
8412 }
8413 *pos = '\0';
8414
8415 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
8416 drv->num_if_indices, buf);
8417}
8418
8419
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008420static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
8421 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008422{
8423 int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07008424 struct drv_nl80211_if_info *old;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008425
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008426 wpa_printf(MSG_DEBUG,
8427 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
8428 ifidx, ifidx_reason);
8429 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008430 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
8431 ifidx);
8432 return;
8433 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07008434 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008435 if (drv->if_indices[i].ifindex == 0) {
8436 drv->if_indices[i].ifindex = ifidx;
8437 drv->if_indices[i].reason = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008438 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008439 return;
8440 }
8441 }
8442
8443 if (drv->if_indices != drv->default_if_indices)
8444 old = drv->if_indices;
8445 else
8446 old = NULL;
8447
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008448 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
Hai Shalom81f62d82019-07-22 12:10:00 -07008449 sizeof(*old));
Jouni Malinen75ecf522011-06-27 15:19:46 -07008450 if (!drv->if_indices) {
8451 if (!old)
8452 drv->if_indices = drv->default_if_indices;
8453 else
8454 drv->if_indices = old;
8455 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8456 "interfaces");
8457 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
8458 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008459 }
8460 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008461 os_memcpy(drv->if_indices, drv->default_if_indices,
8462 sizeof(drv->default_if_indices));
Hai Shalom81f62d82019-07-22 12:10:00 -07008463 drv->if_indices[drv->num_if_indices].ifindex = ifidx;
8464 drv->if_indices[drv->num_if_indices].reason = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008465 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008466 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008467}
8468
8469
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008470static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
8471 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008472{
8473 int i;
8474
8475 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008476 if ((drv->if_indices[i].ifindex == ifidx ||
8477 ifidx == IFIDX_ANY) &&
8478 (drv->if_indices[i].reason == ifidx_reason ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008479 ifidx_reason == IFIDX_ANY)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008480 drv->if_indices[i].ifindex = 0;
8481 drv->if_indices[i].reason = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008482 break;
8483 }
8484 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008485 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008486}
8487
8488
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008489static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
8490 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008491{
8492 int i;
8493
8494 for (i = 0; i < drv->num_if_indices; i++)
Hai Shalom81f62d82019-07-22 12:10:00 -07008495 if (drv->if_indices[i].ifindex == ifidx &&
8496 (drv->if_indices[i].reason == ifidx_reason ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008497 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07008498 return 1;
8499
8500 return 0;
8501}
8502
8503
8504static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008505 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008506{
8507 struct i802_bss *bss = priv;
8508 struct wpa_driver_nl80211_data *drv = bss->drv;
8509 char name[IFNAMSIZ + 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07008510 union wpa_event_data event;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08008511 int ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008512
Hai Shalom39ba6fc2019-01-22 12:40:38 -08008513 ret = os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
8514 if (ret >= (int) sizeof(name))
8515 wpa_printf(MSG_WARNING,
8516 "nl80211: WDS interface name was truncated");
8517 else if (ret < 0)
8518 return ret;
8519
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008520 if (ifname_wds)
8521 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
8522
Jouni Malinen75ecf522011-06-27 15:19:46 -07008523 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
8524 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
8525 if (val) {
8526 if (!if_nametoindex(name)) {
8527 if (nl80211_create_iface(drv, name,
8528 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008529 bss->addr, 1, NULL, NULL, 0) <
8530 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008531 return -1;
8532 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008533 linux_br_add_if(drv->global->ioctl_sock,
8534 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008535 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008536
8537 os_memset(&event, 0, sizeof(event));
8538 event.wds_sta_interface.sta_addr = addr;
8539 event.wds_sta_interface.ifname = name;
8540 event.wds_sta_interface.istatus = INTERFACE_ADDED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07008541 wpa_supplicant_event(bss->ctx,
Roshan Pius3a1667e2018-07-03 15:17:14 -07008542 EVENT_WDS_STA_INTERFACE_STATUS,
8543 &event);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008544 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008545 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
8546 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
8547 "interface %s up", name);
8548 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008549 return i802_set_sta_vlan(priv, addr, name, 0,
8550 NL80211_DRV_LINK_ID_NA);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008551 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08008552 if (bridge_ifname &&
8553 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
8554 name) < 0)
8555 wpa_printf(MSG_INFO,
8556 "nl80211: Failed to remove interface %s from bridge %s: %s",
8557 name, bridge_ifname, strerror(errno));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008558
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008559 i802_set_sta_vlan(priv, addr, bss->ifname, 0,
8560 NL80211_DRV_LINK_ID_NA);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008561 nl80211_remove_iface(drv, if_nametoindex(name));
Roshan Pius3a1667e2018-07-03 15:17:14 -07008562 os_memset(&event, 0, sizeof(event));
8563 event.wds_sta_interface.sta_addr = addr;
8564 event.wds_sta_interface.ifname = name;
8565 event.wds_sta_interface.istatus = INTERFACE_REMOVED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07008566 wpa_supplicant_event(bss->ctx, EVENT_WDS_STA_INTERFACE_STATUS,
Roshan Pius3a1667e2018-07-03 15:17:14 -07008567 &event);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008568 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008569 }
8570}
8571
8572
8573static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
8574{
8575 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8576 struct sockaddr_ll lladdr;
8577 unsigned char buf[3000];
8578 int len;
8579 socklen_t fromlen = sizeof(lladdr);
8580
8581 len = recvfrom(sock, buf, sizeof(buf), 0,
8582 (struct sockaddr *)&lladdr, &fromlen);
8583 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008584 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
8585 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07008586 return;
8587 }
8588
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008589 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07008590 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
8591}
8592
8593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008594static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
8595 struct i802_bss *bss,
8596 const char *brname, const char *ifname)
8597{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008598 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008599 char in_br[IFNAMSIZ];
8600
8601 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008602 br_ifindex = if_nametoindex(brname);
8603 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008604 /*
8605 * Bridge was configured, but the bridge device does
8606 * not exist. Try to add it now.
8607 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008608 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008609 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8610 "bridge interface %s: %s",
8611 brname, strerror(errno));
8612 return -1;
8613 }
8614 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008615 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008616 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008617 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008618 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008619
8620 if (linux_br_get(in_br, ifname) == 0) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07008621 if (os_strcmp(in_br, brname) == 0) {
8622 bss->already_in_bridge = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008623 return 0; /* already in the bridge */
Hai Shalomc9e41a12018-07-31 14:41:42 -07008624 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008625
8626 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8627 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008628 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8629 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008630 wpa_printf(MSG_ERROR, "nl80211: Failed to "
8631 "remove interface %s from bridge "
8632 "%s: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008633 ifname, in_br, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008634 return -1;
8635 }
8636 }
8637
8638 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8639 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008640 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008641 wpa_printf(MSG_WARNING,
8642 "nl80211: Failed to add interface %s into bridge %s: %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008643 ifname, brname, strerror(errno));
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008644 /* Try to continue without the interface being in a bridge. This
8645 * may be needed for some cases, e.g., with Open vSwitch, where
8646 * an external component will need to handle bridge
8647 * configuration. */
8648 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008649 }
8650 bss->added_if_into_bridge = 1;
8651
8652 return 0;
8653}
8654
8655
8656static void *i802_init(struct hostapd_data *hapd,
8657 struct wpa_init_params *params)
8658{
8659 struct wpa_driver_nl80211_data *drv;
8660 struct i802_bss *bss;
8661 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008662 char master_ifname[IFNAMSIZ];
8663 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008664 int br_added = 0;
8665
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008666 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
8667 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008668 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008669 if (bss == NULL)
8670 return NULL;
8671
8672 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008673
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008674 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008675 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008676 params->ifname, master_ifname);
8677 br_ifindex = if_nametoindex(master_ifname);
8678 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
8679 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
8680 linux_master_get(master_ifname, params->ifname) == 0) {
8681 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
8682 params->ifname, master_ifname);
8683 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008684 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08008685
8686 /* check if master itself is under bridge */
8687 if (linux_br_get(master_ifname, master_ifname) == 0) {
8688 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
8689 master_ifname);
8690 br_ifindex = if_nametoindex(master_ifname);
8691 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
8692 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008693 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008694 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008695 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008696
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008697 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008699 for (i = 0; i < params->num_bridge; i++) {
8700 if (params->bridge[i]) {
8701 ifindex = if_nametoindex(params->bridge[i]);
8702 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008703 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008704 if (ifindex == br_ifindex)
8705 br_added = 1;
8706 }
8707 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008708
8709 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008710 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008711
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008712 if (params->num_bridge && params->bridge[0]) {
8713 if (i802_check_bridge(drv, bss, params->bridge[0],
8714 params->ifname) < 0)
8715 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008716 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008717 br_added = 1;
8718 }
8719
8720 if (!br_added && br_ifindex &&
8721 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008722 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008723
Hai Shalomc9e41a12018-07-31 14:41:42 -07008724 if (bss->added_if_into_bridge || bss->already_in_bridge) {
Hai Shalomfdcde762020-04-02 11:19:20 -07008725 int err;
8726
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008727 drv->rtnl_sk = nl_socket_alloc();
8728 if (drv->rtnl_sk == NULL) {
8729 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
8730 goto failed;
8731 }
8732
Hai Shalomfdcde762020-04-02 11:19:20 -07008733 err = nl_connect(drv->rtnl_sk, NETLINK_ROUTE);
8734 if (err) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008735 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -07008736 nl_geterror(err));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008737 goto failed;
8738 }
8739 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008740
Hai Shalomb755a2a2020-04-23 21:49:02 -07008741 if (drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX) {
8742 wpa_printf(MSG_DEBUG,
8743 "nl80211: Do not open EAPOL RX socket - using control port for RX");
8744 goto skip_eapol_sock;
8745 }
8746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008747 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
8748 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008749 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
8750 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008751 goto failed;
8752 }
8753
8754 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
8755 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008756 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008757 goto failed;
8758 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07008759skip_eapol_sock:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008760
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008761 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8762 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008763 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008764 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008765
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008766 memcpy(bss->addr, params->own_addr, ETH_ALEN);
8767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008768 return bss;
8769
8770failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008771 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008772 return NULL;
8773}
8774
8775
8776static void i802_deinit(void *priv)
8777{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008778 struct i802_bss *bss = priv;
8779 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008780}
8781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008782
8783static enum nl80211_iftype wpa_driver_nl80211_if_type(
8784 enum wpa_driver_if_type type)
8785{
8786 switch (type) {
8787 case WPA_IF_STATION:
8788 return NL80211_IFTYPE_STATION;
8789 case WPA_IF_P2P_CLIENT:
8790 case WPA_IF_P2P_GROUP:
8791 return NL80211_IFTYPE_P2P_CLIENT;
8792 case WPA_IF_AP_VLAN:
8793 return NL80211_IFTYPE_AP_VLAN;
8794 case WPA_IF_AP_BSS:
8795 return NL80211_IFTYPE_AP;
8796 case WPA_IF_P2P_GO:
8797 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008798 case WPA_IF_P2P_DEVICE:
8799 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008800 case WPA_IF_MESH:
8801 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008802 default:
8803 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008804 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008805}
8806
8807
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008808static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
8809{
8810 struct wpa_driver_nl80211_data *drv;
8811 dl_list_for_each(drv, &global->interfaces,
8812 struct wpa_driver_nl80211_data, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008813 if (ether_addr_equal(addr, drv->first_bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008814 return 1;
8815 }
8816 return 0;
8817}
8818
8819
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008820static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008821{
8822 unsigned int idx;
8823
8824 if (!drv->global)
8825 return -1;
8826
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008827 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008828 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008829 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008830 new_addr[0] ^= idx << 2;
8831 if (!nl80211_addr_in_use(drv->global, new_addr))
8832 break;
8833 }
8834 if (idx == 64)
8835 return -1;
8836
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008837 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008838 MACSTR, MAC2STR(new_addr));
8839
8840 return 0;
8841}
8842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008843
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008844struct wdev_info {
8845 u64 wdev_id;
8846 int wdev_id_set;
8847 u8 macaddr[ETH_ALEN];
8848};
8849
8850static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
8851{
8852 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8853 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8854 struct wdev_info *wi = arg;
8855
8856 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8857 genlmsg_attrlen(gnlh, 0), NULL);
8858 if (tb[NL80211_ATTR_WDEV]) {
8859 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
8860 wi->wdev_id_set = 1;
8861 }
8862
8863 if (tb[NL80211_ATTR_MAC])
8864 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
8865 ETH_ALEN);
8866
8867 return NL_SKIP;
8868}
8869
8870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008871static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8872 const char *ifname, const u8 *addr,
8873 void *bss_ctx, void **drv_priv,
8874 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008875 const char *bridge, int use_existing,
8876 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008877{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008878 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008879 struct i802_bss *bss = priv;
8880 struct wpa_driver_nl80211_data *drv = bss->drv;
8881 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008882 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008883
8884 if (addr)
8885 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008886 nlmode = wpa_driver_nl80211_if_type(type);
8887 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
8888 struct wdev_info p2pdev_info;
8889
8890 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
8891 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
8892 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008893 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008894 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
8895 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
8896 ifname);
8897 return -1;
8898 }
8899
8900 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
8901 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
Mir Ali8a8f1002020-10-06 22:41:40 +05308902 if (!is_zero_ether_addr(p2pdev_info.macaddr)) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008903 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
Mir Ali8a8f1002020-10-06 22:41:40 +05308904 os_memcpy(drv->global->p2p_perm_addr, p2pdev_info.macaddr, ETH_ALEN);
8905 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008906 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
8907 ifname,
8908 (long long unsigned int) p2pdev_info.wdev_id);
8909 } else {
8910 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008911 0, NULL, NULL, use_existing);
8912 if (use_existing && ifidx == -ENFILE) {
8913 added = 0;
8914 ifidx = if_nametoindex(ifname);
8915 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008916 return -1;
8917 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008918 }
8919
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008920 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07008921 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008922 os_memcpy(if_addr, bss->addr, ETH_ALEN);
8923 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07008924 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008925 if (added)
8926 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008927 return -1;
8928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008929 }
8930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008931 if (!addr &&
8932 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07008933 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00008934 type == WPA_IF_STATION || type == WPA_IF_AP_BSS)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07008935 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008936 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008937
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008938 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008939 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07008940 if (added)
8941 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008942 return -1;
8943 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008944 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008945 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07008946 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008947 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07008948 if (added)
8949 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008950 return -1;
8951 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008952 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008953 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07008954 if (added)
8955 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008956 return -1;
8957 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008958 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008959 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008960 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008961
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008962 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008963 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
Sunil Ravi036cec52023-03-29 11:35:17 -07008964
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008965 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008966 if (added)
8967 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008968 return -1;
8969 }
8970
8971 if (bridge &&
8972 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
8973 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
8974 "interface %s to a bridge %s",
8975 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008976 if (added)
8977 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008978 os_free(new_bss);
8979 return -1;
8980 }
8981
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008982 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
8983 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07008984 if (added)
8985 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008986 os_free(new_bss);
8987 return -1;
8988 }
8989 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008990 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008991 new_bss->ifindex = ifidx;
8992 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008993 new_bss->next = drv->first_bss->next;
Sunil Ravi036cec52023-03-29 11:35:17 -07008994 new_bss->flink = &new_bss->links[0];
Sunil Ravi99c035e2024-07-12 01:42:03 +00008995 new_bss->valid_links = 0;
Sunil Ravi036cec52023-03-29 11:35:17 -07008996 os_memcpy(new_bss->flink->addr, new_bss->addr, ETH_ALEN);
8997
8998 new_bss->flink->freq = drv->first_bss->flink->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008999 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009000 new_bss->added_if = added;
9001 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009002 if (drv_priv)
9003 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009004 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009005
9006 /* Subscribe management frames for this WPA_IF_AP_BSS */
9007 if (nl80211_setup_ap(new_bss))
9008 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009009 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009010
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009011 if (drv->global)
9012 drv->global->if_add_ifindex = ifidx;
9013
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009014 /*
9015 * Some virtual interfaces need to process EAPOL packets and events on
9016 * the parent interface. This is used mainly with hostapd.
9017 */
9018 if (ifidx > 0 &&
9019 (drv->hostapd ||
9020 nlmode == NL80211_IFTYPE_AP_VLAN ||
9021 nlmode == NL80211_IFTYPE_WDS ||
9022 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009023 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009025 return 0;
9026}
9027
9028
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009029static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009030 enum wpa_driver_if_type type,
9031 const char *ifname)
9032{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009033 struct wpa_driver_nl80211_data *drv = bss->drv;
9034 int ifindex = if_nametoindex(ifname);
9035
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009036 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
9037 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08009038 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07009039 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07009040 else if (ifindex > 0 && !bss->added_if) {
9041 struct wpa_driver_nl80211_data *drv2;
9042 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009043 struct wpa_driver_nl80211_data, list) {
9044 del_ifidx(drv2, ifindex, IFIDX_ANY);
9045 del_ifidx(drv2, IFIDX_ANY, ifindex);
9046 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07009047 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009048
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009049 if (type != WPA_IF_AP_BSS)
9050 return 0;
9051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009052 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009053 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9054 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009055 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9056 "interface %s from bridge %s: %s",
9057 bss->ifname, bss->brname, strerror(errno));
9058 }
9059 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009060 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009061 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9062 "bridge %s: %s",
9063 bss->brname, strerror(errno));
9064 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009065
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009066 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009067 struct i802_bss *tbss;
9068
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009069 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
9070 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009071 if (tbss->next == bss) {
9072 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009073 /* Unsubscribe management frames */
9074 nl80211_teardown_ap(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00009075 nl80211_remove_links(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009076 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009077 if (!bss->added_if)
9078 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009079 os_free(bss);
9080 bss = NULL;
9081 break;
9082 }
9083 }
9084 if (bss)
9085 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9086 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009087 } else {
9088 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
9089 nl80211_teardown_ap(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00009090 nl80211_remove_links(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009091 if (!bss->added_if && !drv->first_bss->next)
Sunil Ravi036cec52023-03-29 11:35:17 -07009092 wpa_driver_nl80211_del_beacon_all(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009093 nl80211_destroy_bss(bss);
9094 if (!bss->added_if)
9095 i802_set_iface_flags(bss, 0);
9096 if (drv->first_bss->next) {
9097 drv->first_bss = drv->first_bss->next;
9098 drv->ctx = drv->first_bss->ctx;
Sunil Ravi99c035e2024-07-12 01:42:03 +00009099 drv->ifindex = drv->first_bss->ifindex;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009100 os_free(bss);
9101 } else {
9102 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
9103 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009105
9106 return 0;
9107}
9108
9109
9110static int cookie_handler(struct nl_msg *msg, void *arg)
9111{
9112 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9113 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9114 u64 *cookie = arg;
9115 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9116 genlmsg_attrlen(gnlh, 0), NULL);
9117 if (tb[NL80211_ATTR_COOKIE])
9118 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9119 return NL_SKIP;
9120}
9121
9122
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009123static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009124 unsigned int freq, unsigned int wait,
9125 const u8 *buf, size_t buf_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07009126 int save_cookie, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009127 int offchanok, const u16 *csa_offs,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009128 size_t csa_offs_len, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009129{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009130 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009131 struct nl_msg *msg;
9132 u64 cookie;
9133 int ret = -1;
9134
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009135 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07009136 "no_ack=%d offchanok=%d",
9137 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009138 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009139
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009140 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009141 ((link_id != NL80211_DRV_LINK_ID_NA) &&
9142 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009143 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9144 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
9145 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9146 drv->test_use_roc_tx) &&
9147 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
9148 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
9149 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009150 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
9151 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009152 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
9153 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009154
9155 cookie = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009156 ret = send_and_recv_resp(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009157 msg = NULL;
9158 if (ret) {
9159 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009160 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9161 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009162 } else {
9163 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
9164 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9165 (long long unsigned int) cookie);
9166
Hai Shalomfdcde762020-04-02 11:19:20 -07009167 if (save_cookie)
9168 drv->send_frame_cookie = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009169
Sunil Ravia04bd252022-05-02 22:54:18 -07009170 if (!wait) {
9171 /* There is no need to store this cookie since there
9172 * is no wait that could be canceled later. */
9173 goto fail;
9174 }
Hai Shalomfdcde762020-04-02 11:19:20 -07009175 if (drv->num_send_frame_cookies == MAX_SEND_FRAME_COOKIES) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009176 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -07009177 "nl80211: Drop oldest pending send frame cookie 0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009178 (long long unsigned int)
Hai Shalomfdcde762020-04-02 11:19:20 -07009179 drv->send_frame_cookies[0]);
9180 os_memmove(&drv->send_frame_cookies[0],
9181 &drv->send_frame_cookies[1],
9182 (MAX_SEND_FRAME_COOKIES - 1) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009183 sizeof(u64));
Hai Shalomfdcde762020-04-02 11:19:20 -07009184 drv->num_send_frame_cookies--;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009185 }
Hai Shalomfdcde762020-04-02 11:19:20 -07009186 drv->send_frame_cookies[drv->num_send_frame_cookies] = cookie;
9187 drv->num_send_frame_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009188 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009189
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009190fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009191 nlmsg_free(msg);
9192 return ret;
9193}
9194
9195
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009196static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
9197 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009198 unsigned int wait_time,
9199 const u8 *dst, const u8 *src,
9200 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009201 const u8 *data, size_t data_len,
9202 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009203{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009204 struct wpa_driver_nl80211_data *drv = bss->drv;
9205 int ret = -1;
9206 u8 *buf;
9207 struct ieee80211_hdr *hdr;
Hai Shalomfdcde762020-04-02 11:19:20 -07009208 int offchanok = 1;
9209
Sunil Ravi036cec52023-03-29 11:35:17 -07009210 if (is_ap_interface(drv->nlmode) && (int) freq == bss->flink->freq &&
9211 bss->flink->beacon_set)
Hai Shalomfdcde762020-04-02 11:19:20 -07009212 offchanok = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009213
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009214 if (!freq && is_sta_interface(drv->nlmode))
9215 offchanok = 0;
9216
9217 wpa_printf(MSG_DEBUG,
9218 "nl80211: Send Action frame (ifindex=%d, freq=%u MHz wait=%d ms no_cck=%d offchanok=%d dst="
9219 MACSTR " src=" MACSTR " bssid=" MACSTR ")",
9220 drv->ifindex, freq, wait_time, no_cck, offchanok,
9221 MAC2STR(dst), MAC2STR(src), MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009222
9223 buf = os_zalloc(24 + data_len);
9224 if (buf == NULL)
9225 return ret;
9226 os_memcpy(buf + 24, data, data_len);
9227 hdr = (struct ieee80211_hdr *) buf;
9228 hdr->frame_control =
9229 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
9230 os_memcpy(hdr->addr1, dst, ETH_ALEN);
9231 os_memcpy(hdr->addr2, src, ETH_ALEN);
9232 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
9233
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009234 if (!ether_addr_equal(bss->addr, src)) {
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08009235 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
9236 MAC2STR(src));
9237 os_memcpy(bss->rand_addr, src, ETH_ALEN);
9238 } else {
9239 os_memset(bss->rand_addr, 0, ETH_ALEN);
9240 }
9241
Hai Shalom60840252021-02-19 19:02:11 -08009242#ifdef CONFIG_MESH
9243 if (is_mesh_interface(drv->nlmode)) {
9244 struct hostapd_hw_modes *modes;
9245 u16 num_modes, flags;
9246 u8 dfs_domain;
9247 int i;
9248
9249 modes = nl80211_get_hw_feature_data(bss, &num_modes,
9250 &flags, &dfs_domain);
9251 if (dfs_domain != HOSTAPD_DFS_REGION_ETSI &&
Sunil Ravi036cec52023-03-29 11:35:17 -07009252 ieee80211_is_dfs(bss->flink->freq, modes, num_modes))
Hai Shalom60840252021-02-19 19:02:11 -08009253 offchanok = 0;
9254 if (modes) {
9255 for (i = 0; i < num_modes; i++) {
9256 os_free(modes[i].channels);
9257 os_free(modes[i].rates);
9258 }
9259 os_free(modes);
9260 }
9261 }
9262#endif /* CONFIG_MESH */
9263
Dmitry Shmidt56052862013-10-04 10:23:25 -07009264 if (is_ap_interface(drv->nlmode) &&
9265 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
Sunil Ravi036cec52023-03-29 11:35:17 -07009266 (int) freq == bss->flink->freq || drv->device_ap_sme ||
Dmitry Shmidt56052862013-10-04 10:23:25 -07009267 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009268 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07009269 0, freq, no_cck, offchanok,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00009270 wait_time, NULL, 0, 0, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009271 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009272 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009273 24 + data_len, 1, no_cck, 0,
9274 offchanok, NULL, 0,
9275 NL80211_DRV_LINK_ID_NA);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009276
9277 os_free(buf);
9278 return ret;
9279}
9280
9281
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009282static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009283{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009284 struct wpa_driver_nl80211_data *drv = bss->drv;
9285 struct nl_msg *msg;
9286 int ret;
9287
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08009288 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009289 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009290 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009291 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009292 nlmsg_free(msg);
9293 return;
9294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009295
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009296 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009297 if (ret)
9298 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
9299 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009300}
9301
9302
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009303static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
9304{
9305 struct i802_bss *bss = priv;
9306 struct wpa_driver_nl80211_data *drv = bss->drv;
9307 unsigned int i;
9308 u64 cookie;
9309
9310 /* Cancel the last pending TX cookie */
Sunil Ravia04bd252022-05-02 22:54:18 -07009311 if (drv->send_frame_cookie != (u64) -1)
9312 nl80211_frame_wait_cancel(bss, drv->send_frame_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009313
9314 /*
9315 * Cancel the other pending TX cookies, if any. This is needed since
9316 * the driver may keep a list of all pending offchannel TX operations
9317 * and free up the radio only once they have expired or cancelled.
9318 */
Hai Shalomfdcde762020-04-02 11:19:20 -07009319 for (i = drv->num_send_frame_cookies; i > 0; i--) {
9320 cookie = drv->send_frame_cookies[i - 1];
9321 if (cookie != drv->send_frame_cookie)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009322 nl80211_frame_wait_cancel(bss, cookie);
9323 }
Hai Shalomfdcde762020-04-02 11:19:20 -07009324 drv->num_send_frame_cookies = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009325}
9326
9327
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009328static int nl80211_put_any_link_id(struct nl_msg *msg,
9329 struct driver_sta_mlo_info *mlo,
9330 int freq)
9331{
9332 int i;
9333 int link_id = -1;
9334 int any_valid_link_id = -1;
9335
9336 if (!mlo->valid_links)
9337 return 0;
9338
9339 /* First try to pick a link that uses the same band */
Sunil Ravi99c035e2024-07-12 01:42:03 +00009340 for_each_link(mlo->valid_links, i) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009341 if (any_valid_link_id == -1)
9342 any_valid_link_id = i;
9343
9344 if (is_same_band(freq, mlo->links[i].freq)) {
9345 link_id = i;
9346 break;
9347 }
9348 }
9349
9350 /* Use any valid link ID if no band match was found */
9351 if (link_id == -1)
9352 link_id = any_valid_link_id;
9353
9354 if (link_id == -1) {
9355 wpa_printf(MSG_INFO,
9356 "nl80211: No valid Link ID found for freq %u", freq);
9357 return 0;
9358 }
9359
9360 wpa_printf(MSG_DEBUG, "nl80211: Add Link ID %d", link_id);
9361 return nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id);
9362}
9363
9364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009365static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
9366 unsigned int duration)
9367{
9368 struct i802_bss *bss = priv;
9369 struct wpa_driver_nl80211_data *drv = bss->drv;
9370 struct nl_msg *msg;
9371 int ret;
9372 u64 cookie;
9373
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009374 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
9375 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009376 nla_put_u32(msg, NL80211_ATTR_DURATION, duration) ||
9377 nl80211_put_any_link_id(msg, &drv->sta_mlo_info, freq)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009378 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009379 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009380 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009381
9382 cookie = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009383 ret = send_and_recv_resp(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009384 if (ret == 0) {
9385 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
9386 "0x%llx for freq=%u MHz duration=%u",
9387 (long long unsigned int) cookie, freq, duration);
9388 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009389 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009390 return 0;
9391 }
9392 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
9393 "(freq=%d duration=%u): %d (%s)",
9394 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009395 return -1;
9396}
9397
9398
9399static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
9400{
9401 struct i802_bss *bss = priv;
9402 struct wpa_driver_nl80211_data *drv = bss->drv;
9403 struct nl_msg *msg;
9404 int ret;
9405
9406 if (!drv->pending_remain_on_chan) {
9407 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
9408 "to cancel");
9409 return -1;
9410 }
9411
9412 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
9413 "0x%llx",
9414 (long long unsigned int) drv->remain_on_chan_cookie);
9415
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009416 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
9417 if (!msg ||
9418 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
9419 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009420 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009421 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009422
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009423 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009424 if (ret == 0)
9425 return 0;
9426 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
9427 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009428 return -1;
9429}
9430
9431
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009432static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009433{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009434 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009436 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009437 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07009438 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
9439 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009440 /*
9441 * Do not disable Probe Request reporting that was
9442 * enabled in nl80211_setup_ap().
9443 */
9444 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
9445 "Probe Request reporting nl_preq=%p while "
9446 "in AP mode", bss->nl_preq);
9447 } else if (bss->nl_preq) {
9448 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
9449 "reporting nl_preq=%p", bss->nl_preq);
Roshan Pius3a1667e2018-07-03 15:17:14 -07009450 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009451 }
9452 return 0;
9453 }
9454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009455 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009456 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009457 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009458 return 0;
9459 }
9460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009461 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
9462 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009463 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009464 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
9465 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009466
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009467 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009468 (WLAN_FC_TYPE_MGMT << 2) |
9469 (WLAN_FC_STYPE_PROBE_REQ << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07009470 NULL, 0, false) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009471 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07009472
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009473 nl80211_register_eloop_read(&bss->nl_preq,
9474 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07009475 bss->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009476
9477 return 0;
9478
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009479 out_err:
9480 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009481 return -1;
9482}
9483
9484
9485static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
9486 int ifindex, int disabled)
9487{
9488 struct nl_msg *msg;
9489 struct nlattr *bands, *band;
9490 int ret;
9491
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009492 wpa_printf(MSG_DEBUG,
9493 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
9494 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
9495 "no NL80211_TXRATE_LEGACY constraint");
9496
9497 msg = nl80211_ifindex_msg(drv, ifindex, 0,
9498 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009499 if (!msg)
9500 return -1;
9501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009502 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
9503 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009504 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009505
9506 /*
9507 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
9508 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
9509 * rates. All 5 GHz rates are left enabled.
9510 */
9511 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009512 if (!band ||
9513 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
9514 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
9515 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009516 nla_nest_end(msg, band);
9517
9518 nla_nest_end(msg, bands);
9519
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009520 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009521 if (ret) {
9522 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
9523 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009524 } else
9525 drv->disabled_11b_rates = disabled;
9526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009527 return ret;
9528
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009529fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009530 nlmsg_free(msg);
9531 return -1;
9532}
9533
9534
Sunil Ravi99c035e2024-07-12 01:42:03 +00009535static int nl80211_remove_link(struct i802_bss *bss, int link_id)
Sunil Raviaf399a82024-05-05 20:56:55 +00009536{
Sunil Ravi88611412024-06-28 17:34:56 +00009537 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +00009538 struct i802_link *link;
Sunil Ravi88611412024-06-28 17:34:56 +00009539 struct nl_msg *msg;
Sunil Ravi99c035e2024-07-12 01:42:03 +00009540 size_t i;
9541 int ret;
9542
9543 wpa_printf(MSG_DEBUG, "nl80211: Remove link (ifindex=%d link_id=%u)",
9544 bss->ifindex, link_id);
9545
9546 if (!(bss->valid_links & BIT(link_id))) {
9547 wpa_printf(MSG_DEBUG,
9548 "nl80211: MLD: remove link: Link not found");
9549 return -1;
9550 }
9551
9552 link = &bss->links[link_id];
9553
9554 wpa_driver_nl80211_del_beacon(bss, link_id);
9555
9556 /* First remove the link locally */
9557 bss->valid_links &= ~BIT(link_id);
9558 os_memset(link->addr, 0, ETH_ALEN);
9559
9560 /* Choose new deflink if we are removing that link */
9561 if (bss->flink == link) {
9562 for_each_link_default(bss->valid_links, i, 0) {
9563 bss->flink = &bss->links[i];
9564 break;
9565 }
9566 }
9567
9568 /* If this was the last link, reset default link */
9569 if (!bss->valid_links) {
9570 /* TODO: Does keeping freq/bandwidth make sense? */
9571 if (bss->flink != link)
9572 os_memcpy(bss->flink, link, sizeof(*link));
9573
9574 os_memcpy(bss->flink->addr, bss->addr, ETH_ALEN);
9575 }
9576
9577 /* Remove the link from the kernel */
9578 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_REMOVE_LINK);
9579 if (!msg ||
9580 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) {
9581 nlmsg_free(msg);
9582 wpa_printf(MSG_ERROR,
9583 "nl80211: remove link (%d) failed", link_id);
9584 return -1;
9585 }
9586
9587 ret = send_and_recv_cmd(drv, msg);
9588 if (ret)
9589 wpa_printf(MSG_ERROR,
9590 "nl80211: remove link (%d) failed. ret=%d (%s)",
9591 link_id, ret, strerror(-ret));
9592
9593 return ret;
9594}
9595
9596
9597static void nl80211_remove_links(struct i802_bss *bss)
9598{
Sunil Ravi036cec52023-03-29 11:35:17 -07009599 int ret;
9600 u8 link_id;
9601
Sunil Ravi99c035e2024-07-12 01:42:03 +00009602 for_each_link(bss->valid_links, link_id) {
9603 ret = nl80211_remove_link(bss, link_id);
9604 if (ret)
9605 break;
Sunil Ravi88611412024-06-28 17:34:56 +00009606 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00009607
9608 if (bss->flink)
9609 os_memcpy(bss->flink->addr, bss->addr, ETH_ALEN);
Sunil Ravi036cec52023-03-29 11:35:17 -07009610}
9611
9612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009613static int wpa_driver_nl80211_deinit_ap(void *priv)
9614{
9615 struct i802_bss *bss = priv;
9616 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi036cec52023-03-29 11:35:17 -07009617
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009618 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009619 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07009620
9621 /* Stop beaconing */
Sunil Ravi99c035e2024-07-12 01:42:03 +00009622 wpa_driver_nl80211_del_beacon(bss, NL80211_DRV_LINK_ID_NA);
Sunil Ravi036cec52023-03-29 11:35:17 -07009623
9624 nl80211_remove_links(bss);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009625
9626 /*
9627 * If the P2P GO interface was dynamically added, then it is
9628 * possible that the interface change to station is not possible.
9629 */
9630 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
9631 return 0;
9632
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009633 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009634}
9635
9636
Sunil Ravi99c035e2024-07-12 01:42:03 +00009637static int wpa_driver_nl80211_stop_ap(void *priv, int link_id)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009638{
9639 struct i802_bss *bss = priv;
9640 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi036cec52023-03-29 11:35:17 -07009641
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009642 if (!is_ap_interface(drv->nlmode))
9643 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07009644
Sunil Ravi99c035e2024-07-12 01:42:03 +00009645 if (link_id == -1) {
9646 wpa_driver_nl80211_del_beacon_all(bss);
9647 return 0;
9648 }
Sunil Ravi036cec52023-03-29 11:35:17 -07009649
Sunil Ravi99c035e2024-07-12 01:42:03 +00009650 if (nl80211_link_valid(bss->valid_links, link_id)) {
9651 wpa_driver_nl80211_del_beacon(bss, link_id);
9652 return 0;
9653 }
9654
9655 return -1;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009656}
9657
9658
Dmitry Shmidt04949592012-07-19 12:16:46 -07009659static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
9660{
9661 struct i802_bss *bss = priv;
9662 struct wpa_driver_nl80211_data *drv = bss->drv;
9663 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
9664 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009665
9666 /*
9667 * If the P2P Client interface was dynamically added, then it is
9668 * possible that the interface change to station is not possible.
9669 */
9670 if (bss->if_dynamic)
9671 return 0;
9672
Dmitry Shmidt04949592012-07-19 12:16:46 -07009673 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9674}
9675
9676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009677static void wpa_driver_nl80211_resume(void *priv)
9678{
9679 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009680 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009681
9682 if (i802_set_iface_flags(bss, 1))
9683 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009684
9685 if (is_p2p_net_interface(nlmode))
9686 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009687}
9688
9689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009690static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9691{
9692 struct i802_bss *bss = priv;
9693 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009694 struct nl_msg *msg;
9695 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009696
9697 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9698 "hysteresis=%d", threshold, hysteresis);
9699
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009700 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
9701 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
9702 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
9703 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
9704 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009705 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009706 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009707 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009708
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009709 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009710}
9711
9712
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009713static int get_channel_width(struct nl_msg *msg, void *arg)
9714{
9715 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9716 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9717 struct wpa_signal_info *sig_change = arg;
9718
9719 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9720 genlmsg_attrlen(gnlh, 0), NULL);
9721
9722 sig_change->center_frq1 = -1;
9723 sig_change->center_frq2 = -1;
9724 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
9725
9726 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
9727 sig_change->chanwidth = convert2width(
9728 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
9729 if (tb[NL80211_ATTR_CENTER_FREQ1])
9730 sig_change->center_frq1 =
9731 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
9732 if (tb[NL80211_ATTR_CENTER_FREQ2])
9733 sig_change->center_frq2 =
9734 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
9735 }
9736
9737 return NL_SKIP;
9738}
9739
9740
9741static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
9742 struct wpa_signal_info *sig)
9743{
9744 struct nl_msg *msg;
9745
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009746 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009747 return send_and_recv_resp(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009748}
9749
9750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009751static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
9752{
9753 struct i802_bss *bss = priv;
9754 struct wpa_driver_nl80211_data *drv = bss->drv;
9755 int res;
9756
9757 os_memset(si, 0, sizeof(*si));
Sunil Ravi77d572f2023-01-17 23:58:31 +00009758 res = nl80211_get_link_signal(drv, drv->bssid, &si->data);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009759 if (res) {
9760 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
9761 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
9762 return res;
Sunil Ravi77d572f2023-01-17 23:58:31 +00009763 si->data.signal = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009764 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009765
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009766 res = nl80211_get_channel_width(drv, si);
9767 if (res != 0)
9768 return res;
9769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009770 return nl80211_get_link_noise(drv, si);
9771}
9772
9773
Sunil Ravi89eba102022-09-13 21:04:37 -07009774static int get_links_noise(struct nl_msg *msg, void *arg)
9775{
9776 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9777 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9778 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
9779 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
9780 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
9781 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
9782 };
9783 struct wpa_mlo_signal_info *mlo_sig = arg;
9784 int i;
9785
9786 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9787 genlmsg_attrlen(gnlh, 0), NULL);
9788
9789 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
9790 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
9791 return NL_SKIP;
9792 }
9793
9794 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
9795 tb[NL80211_ATTR_SURVEY_INFO],
9796 survey_policy)) {
9797 wpa_printf(MSG_DEBUG,
9798 "nl80211: Failed to parse nested attributes");
9799 return NL_SKIP;
9800 }
9801
9802 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
9803 return NL_SKIP;
9804
9805 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
9806 return NL_SKIP;
9807
Sunil Ravi99c035e2024-07-12 01:42:03 +00009808 for_each_link(mlo_sig->valid_links, i) {
Sunil Ravi89eba102022-09-13 21:04:37 -07009809 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
9810 mlo_sig->links[i].frequency)
9811 continue;
9812
9813 mlo_sig->links[i].current_noise =
9814 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
9815 break;
9816 }
9817
9818 return NL_SKIP;
9819}
9820
9821
9822static int nl80211_get_links_noise(struct wpa_driver_nl80211_data *drv,
9823 struct wpa_mlo_signal_info *mlo_sig)
9824{
9825 struct nl_msg *msg;
9826
9827 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009828 return send_and_recv_resp(drv, msg, get_links_noise, mlo_sig);
Sunil Ravi89eba102022-09-13 21:04:37 -07009829}
9830
9831
9832static int get_links_channel_width(struct nl_msg *msg, void *arg)
9833{
9834 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9835 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9836 struct wpa_mlo_signal_info *mlo_sig = arg;
9837 struct nlattr *link;
9838 int rem_links;
9839
9840 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9841 genlmsg_attrlen(gnlh, 0), NULL);
9842
9843 if (!tb[NL80211_ATTR_MLO_LINKS])
9844 return NL_SKIP;
9845
9846 nla_for_each_nested(link, tb[NL80211_ATTR_MLO_LINKS], rem_links) {
9847 struct nlattr *tb2[NL80211_ATTR_MAX + 1];
9848 int link_id;
9849
9850 nla_parse(tb2, NL80211_ATTR_MAX, nla_data(link), nla_len(link),
9851 NULL);
9852
9853 if (!tb2[NL80211_ATTR_MLO_LINK_ID])
9854 continue;
9855
9856 link_id = nla_get_u8(tb2[NL80211_ATTR_MLO_LINK_ID]);
9857 if (link_id >= MAX_NUM_MLD_LINKS)
9858 continue;
9859
9860 if (!tb2[NL80211_ATTR_CHANNEL_WIDTH])
9861 continue;
9862 mlo_sig->links[link_id].chanwidth = convert2width(
9863 nla_get_u32(tb2[NL80211_ATTR_CHANNEL_WIDTH]));
9864 if (tb2[NL80211_ATTR_CENTER_FREQ1])
9865 mlo_sig->links[link_id].center_frq1 =
9866 nla_get_u32(tb2[NL80211_ATTR_CENTER_FREQ1]);
9867 if (tb2[NL80211_ATTR_CENTER_FREQ2])
9868 mlo_sig->links[link_id].center_frq2 =
9869 nla_get_u32(tb2[NL80211_ATTR_CENTER_FREQ2]);
9870 }
9871
9872 return NL_SKIP;
9873}
9874
9875
9876static int nl80211_get_links_channel_width(struct wpa_driver_nl80211_data *drv,
9877 struct wpa_mlo_signal_info *mlo_sig)
9878{
9879 struct nl_msg *msg;
9880
9881 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009882 return send_and_recv_resp(drv, msg, get_links_channel_width, mlo_sig);
Sunil Ravi89eba102022-09-13 21:04:37 -07009883}
9884
9885
9886static int nl80211_mlo_signal_poll(void *priv,
9887 struct wpa_mlo_signal_info *mlo_si)
9888{
9889 struct i802_bss *bss = priv;
9890 struct wpa_driver_nl80211_data *drv = bss->drv;
9891 int res;
9892 int i;
9893
9894 if (drv->nlmode != NL80211_IFTYPE_STATION ||
9895 !drv->sta_mlo_info.valid_links)
9896 return -1;
9897
9898 os_memset(mlo_si, 0, sizeof(*mlo_si));
9899 mlo_si->valid_links = drv->sta_mlo_info.valid_links;
9900
Sunil Ravi99c035e2024-07-12 01:42:03 +00009901 for_each_link(mlo_si->valid_links, i) {
Sunil Ravi89eba102022-09-13 21:04:37 -07009902 res = nl80211_get_link_signal(drv,
9903 drv->sta_mlo_info.links[i].bssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +00009904 &mlo_si->links[i].data);
Sunil Ravi89eba102022-09-13 21:04:37 -07009905 if (res != 0)
9906 return res;
9907
9908 mlo_si->links[i].center_frq1 = -1;
9909 mlo_si->links[i].center_frq2 = -1;
9910 mlo_si->links[i].chanwidth = CHAN_WIDTH_UNKNOWN;
9911 mlo_si->links[i].current_noise = WPA_INVALID_NOISE;
9912 mlo_si->links[i].frequency = drv->sta_mlo_info.links[i].freq;
9913 }
9914
9915 res = nl80211_get_links_channel_width(drv, mlo_si);
9916 if (res != 0)
9917 return res;
9918
9919 return nl80211_get_links_noise(drv, mlo_si);
9920}
9921
9922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009923static int nl80211_set_param(void *priv, const char *param)
9924{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07009925 struct i802_bss *bss = priv;
9926 struct wpa_driver_nl80211_data *drv = bss->drv;
9927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009928 if (param == NULL)
9929 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009930 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009931
9932#ifdef CONFIG_P2P
9933 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009934 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
9935 "interface");
9936 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
9937 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
9938 }
9939#endif /* CONFIG_P2P */
9940
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07009941 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009942 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009943
9944 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009945 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009946 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009947 }
9948
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07009949 if (os_strstr(param, "force_bss_selection=1"))
9950 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
9951
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08009952 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08009953 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
9954 drv->test_use_roc_tx = 1;
9955 }
9956
Hai Shalomb755a2a2020-04-23 21:49:02 -07009957 if (os_strstr(param, "control_port=0")) {
Hai Shalomfdcde762020-04-02 11:19:20 -07009958 drv->capa.flags &= ~WPA_DRIVER_FLAGS_CONTROL_PORT;
Hai Shalom899fcc72020-10-19 14:38:18 -07009959 drv->capa.flags2 &= ~(WPA_DRIVER_FLAGS2_CONTROL_PORT_RX |
9960 WPA_DRIVER_FLAGS2_CONTROL_PORT_TX_STATUS);
9961 drv->control_port_ap = 0;
Hai Shalomb755a2a2020-04-23 21:49:02 -07009962 }
9963
9964 if (os_strstr(param, "control_port_ap=1"))
9965 drv->control_port_ap = 1;
Hai Shalomfdcde762020-04-02 11:19:20 -07009966
Hai Shalom899fcc72020-10-19 14:38:18 -07009967 if (os_strstr(param, "control_port_ap=0")) {
9968 drv->capa.flags2 &= ~WPA_DRIVER_FLAGS2_CONTROL_PORT_TX_STATUS;
9969 drv->control_port_ap = 0;
9970 }
9971
Hai Shalomfdcde762020-04-02 11:19:20 -07009972 if (os_strstr(param, "full_ap_client_state=0"))
9973 drv->capa.flags &= ~WPA_DRIVER_FLAGS_FULL_AP_CLIENT_STATE;
9974
Hai Shalom899fcc72020-10-19 14:38:18 -07009975 if (os_strstr(param, "no_rrm=1")) {
9976 drv->no_rrm = 1;
9977
9978 if (!bss->in_deinit && !is_ap_interface(drv->nlmode) &&
9979 !is_mesh_interface(drv->nlmode)) {
9980 nl80211_mgmt_unsubscribe(bss, "no_rrm=1");
9981 if (nl80211_mgmt_subscribe_non_ap(bss) < 0)
9982 wpa_printf(MSG_DEBUG,
9983 "nl80211: Failed to re-register Action frame processing - ignore for now");
9984 }
9985 }
9986
Sunil Ravi640215c2023-06-28 23:08:09 +00009987 if (os_strstr(param, "secure_ltf=1")) {
9988 drv->capa.flags2 |= WPA_DRIVER_FLAGS2_SEC_LTF_STA |
9989 WPA_DRIVER_FLAGS2_SEC_LTF_AP;
9990 }
9991
Sunil Ravi7f769292024-07-23 22:21:32 +00009992 if (os_strstr(param, "rsn_override_in_driver=1"))
9993 drv->capa.flags2 |= WPA_DRIVER_FLAGS2_RSN_OVERRIDE_STA;
9994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009995 return 0;
9996}
9997
9998
Dmitry Shmidte4663042016-04-04 10:07:49 -07009999static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010000{
10001 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010002 struct netlink_config *cfg;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010003 struct utsname name;
10004
10005 if (uname(&name) == 0) {
10006 wpa_printf(MSG_DEBUG, "nl80211: Kernel version: %s %s (%s; %s)",
10007 name.sysname, name.release,
10008 name.version, name.machine);
10009 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010011 global = os_zalloc(sizeof(*global));
10012 if (global == NULL)
10013 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -070010014 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010015 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010016 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010017 global->if_add_ifindex = -1;
10018
10019 cfg = os_zalloc(sizeof(*cfg));
10020 if (cfg == NULL)
10021 goto err;
10022
10023 cfg->ctx = global;
10024 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10025 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10026 global->netlink = netlink_init(cfg);
10027 if (global->netlink == NULL) {
10028 os_free(cfg);
10029 goto err;
10030 }
10031
10032 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10033 goto err;
10034
10035 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10036 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010037 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10038 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010039 goto err;
10040 }
10041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010042 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010043
10044err:
10045 nl80211_global_deinit(global);
10046 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010047}
10048
10049
10050static void nl80211_global_deinit(void *priv)
10051{
10052 struct nl80211_global *global = priv;
10053 if (global == NULL)
10054 return;
10055 if (!dl_list_empty(&global->interfaces)) {
10056 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10057 "nl80211_global_deinit",
10058 dl_list_len(&global->interfaces));
10059 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010060
10061 if (global->netlink)
10062 netlink_deinit(global->netlink);
10063
10064 nl_destroy_handles(&global->nl);
10065
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010066 if (global->nl_event)
Roshan Pius3a1667e2018-07-03 15:17:14 -070010067 nl80211_destroy_eloop_handle(&global->nl_event, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010068
10069 nl_cb_put(global->nl_cb);
10070
10071 if (global->ioctl_sock >= 0)
10072 close(global->ioctl_sock);
10073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010074 os_free(global);
10075}
10076
10077
10078static const char * nl80211_get_radio_name(void *priv)
10079{
10080 struct i802_bss *bss = priv;
10081 struct wpa_driver_nl80211_data *drv = bss->drv;
10082 return drv->phyname;
10083}
10084
10085
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010086static int nl80211_pmkid(struct i802_bss *bss, int cmd,
Sunil Ravi77d572f2023-01-17 23:58:31 +000010087 struct wpa_pmkid_params *params, bool skip_pmk)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010088{
10089 struct nl_msg *msg;
Sunil Ravi77d572f2023-01-17 23:58:31 +000010090
10091 if (cmd == NL80211_CMD_SET_PMKSA)
10092 wpa_printf(MSG_DEBUG,
10093 "nl80211: NL80211_CMD_SET_PMKSA with skip_pmk=%s pmk_len=%zu",
10094 skip_pmk ? "true" : "false", params->pmk_len);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010095
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010096 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010097 (params->pmkid &&
10098 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
10099 (params->bssid &&
10100 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
10101 (params->ssid_len &&
10102 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
10103 (params->fils_cache_id &&
10104 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
10105 params->fils_cache_id)) ||
Hai Shalomfdcde762020-04-02 11:19:20 -070010106 (params->pmk_lifetime &&
10107 nla_put_u32(msg, NL80211_ATTR_PMK_LIFETIME,
10108 params->pmk_lifetime)) ||
10109 (params->pmk_reauth_threshold &&
10110 nla_put_u8(msg, NL80211_ATTR_PMK_REAUTH_THRESHOLD,
10111 params->pmk_reauth_threshold)) ||
Hai Shalom021b0b52019-04-10 11:17:58 -070010112 (cmd != NL80211_CMD_DEL_PMKSA &&
Sunil Ravi77d572f2023-01-17 23:58:31 +000010113 params->pmk_len && !skip_pmk &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010114 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
Hai Shalom74f70d42019-02-11 14:42:39 -080010115 nl80211_nlmsg_clear(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010116 nlmsg_free(msg);
10117 return -ENOBUFS;
10118 }
Jouni Malinen75ecf522011-06-27 15:19:46 -070010119
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010120 return send_and_recv_cmd(bss->drv, msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010121}
10122
10123
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010124static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010125{
10126 struct i802_bss *bss = priv;
Sunil Ravi77d572f2023-01-17 23:58:31 +000010127 const size_t PMK_MAX_LEN = 64; /* current cfg80211 limit */
10128 const size_t LEGACY_PMK_MAX_LEN = 48; /* old cfg80211 limit */
10129 bool skip_pmk = params->pmk_len > PMK_MAX_LEN;
Roshan Pius3a1667e2018-07-03 15:17:14 -070010130 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010131
10132 if (params->bssid)
10133 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
10134 MAC2STR(params->bssid));
10135 else if (params->fils_cache_id && params->ssid_len) {
10136 wpa_printf(MSG_DEBUG,
10137 "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
10138 params->fils_cache_id[0], params->fils_cache_id[1],
10139 wpa_ssid_txt(params->ssid, params->ssid_len));
10140 }
10141
Sunil Ravi77d572f2023-01-17 23:58:31 +000010142 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params, skip_pmk);
10143 /*
10144 * Try again by skipping PMK if the first attempt failed with ERANGE
10145 * error, PMK was not skipped, and PMK length is greater than the
10146 * legacy kernel maximum allowed limit.
10147 */
10148 if (ret == -ERANGE && !skip_pmk &&
10149 params->pmk_len > LEGACY_PMK_MAX_LEN)
10150 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params, true);
Roshan Pius3a1667e2018-07-03 15:17:14 -070010151 if (ret < 0) {
10152 wpa_printf(MSG_DEBUG,
10153 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
10154 ret, strerror(-ret));
10155 }
10156
10157 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -070010158}
10159
10160
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010161static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010162{
10163 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -070010164 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010165
10166 if (params->bssid)
10167 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10168 MAC2STR(params->bssid));
10169 else if (params->fils_cache_id && params->ssid_len) {
10170 wpa_printf(MSG_DEBUG,
10171 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
10172 params->fils_cache_id[0], params->fils_cache_id[1],
10173 wpa_ssid_txt(params->ssid, params->ssid_len));
10174 }
10175
Sunil Ravi77d572f2023-01-17 23:58:31 +000010176 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params, true);
Roshan Pius3a1667e2018-07-03 15:17:14 -070010177 if (ret < 0) {
10178 wpa_printf(MSG_DEBUG,
10179 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
10180 ret, strerror(-ret));
10181 }
10182
10183 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -070010184}
10185
10186
10187static int nl80211_flush_pmkid(void *priv)
10188{
10189 struct i802_bss *bss = priv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010190 struct nl_msg *msg;
10191
Jouni Malinen75ecf522011-06-27 15:19:46 -070010192 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010193 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
10194 if (!msg)
10195 return -ENOBUFS;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010196 return send_and_recv_cmd(bss->drv, msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010197}
10198
10199
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010200static void clean_survey_results(struct survey_results *survey_results)
10201{
10202 struct freq_survey *survey, *tmp;
10203
10204 if (dl_list_empty(&survey_results->survey_list))
10205 return;
10206
10207 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10208 struct freq_survey, list) {
10209 dl_list_del(&survey->list);
10210 os_free(survey);
10211 }
10212}
10213
10214
10215static void add_survey(struct nlattr **sinfo, u32 ifidx,
10216 struct dl_list *survey_list)
10217{
10218 struct freq_survey *survey;
10219
10220 survey = os_zalloc(sizeof(struct freq_survey));
10221 if (!survey)
10222 return;
10223
10224 survey->ifidx = ifidx;
10225 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10226 survey->filled = 0;
10227
10228 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10229 survey->nf = (int8_t)
10230 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10231 survey->filled |= SURVEY_HAS_NF;
10232 }
10233
10234 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10235 survey->channel_time =
10236 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10237 survey->filled |= SURVEY_HAS_CHAN_TIME;
10238 }
10239
10240 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10241 survey->channel_time_busy =
10242 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10243 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10244 }
10245
10246 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10247 survey->channel_time_rx =
10248 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10249 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10250 }
10251
10252 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10253 survey->channel_time_tx =
10254 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10255 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10256 }
10257
10258 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)",
10259 survey->freq,
10260 survey->nf,
10261 (unsigned long int) survey->channel_time,
10262 (unsigned long int) survey->channel_time_busy,
10263 (unsigned long int) survey->channel_time_tx,
10264 (unsigned long int) survey->channel_time_rx,
10265 survey->filled);
10266
10267 dl_list_add_tail(survey_list, &survey->list);
10268}
10269
10270
10271static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10272 unsigned int freq_filter)
10273{
10274 if (!freq_filter)
10275 return 1;
10276
10277 return freq_filter == surveyed_freq;
10278}
10279
10280
10281static int survey_handler(struct nl_msg *msg, void *arg)
10282{
10283 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10284 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10285 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10286 struct survey_results *survey_results;
10287 u32 surveyed_freq = 0;
10288 u32 ifidx;
10289
10290 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10291 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10292 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10293 };
10294
10295 survey_results = (struct survey_results *) arg;
10296
10297 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10298 genlmsg_attrlen(gnlh, 0), NULL);
10299
Dmitry Shmidt97672262014-02-03 13:02:54 -080010300 if (!tb[NL80211_ATTR_IFINDEX])
10301 return NL_SKIP;
10302
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010303 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10304
10305 if (!tb[NL80211_ATTR_SURVEY_INFO])
10306 return NL_SKIP;
10307
10308 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10309 tb[NL80211_ATTR_SURVEY_INFO],
10310 survey_policy))
10311 return NL_SKIP;
10312
10313 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10314 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10315 return NL_SKIP;
10316 }
10317
10318 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10319
10320 if (!check_survey_ok(sinfo, surveyed_freq,
10321 survey_results->freq_filter))
10322 return NL_SKIP;
10323
10324 if (survey_results->freq_filter &&
10325 survey_results->freq_filter != surveyed_freq) {
10326 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10327 surveyed_freq);
10328 return NL_SKIP;
10329 }
10330
10331 add_survey(sinfo, ifidx, &survey_results->survey_list);
10332
10333 return NL_SKIP;
10334}
10335
10336
10337static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10338{
10339 struct i802_bss *bss = priv;
10340 struct wpa_driver_nl80211_data *drv = bss->drv;
10341 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010342 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010343 union wpa_event_data data;
10344 struct survey_results *survey_results;
10345
10346 os_memset(&data, 0, sizeof(data));
10347 survey_results = &data.survey_results;
10348
10349 dl_list_init(&survey_results->survey_list);
10350
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010351 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010352 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010353 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010354
10355 if (freq)
10356 data.survey_results.freq_filter = freq;
10357
10358 do {
10359 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010360 err = send_and_recv_resp(drv, msg, survey_handler,
10361 survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010362 } while (err > 0);
10363
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010364 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010365 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010366 else
10367 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010368
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010369 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010370 return err;
10371}
10372
10373
Dmitry Shmidt807291d2015-01-27 13:40:23 -080010374static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
10375 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010376 const u8 *replay_ctr)
10377{
10378 struct i802_bss *bss = priv;
10379 struct wpa_driver_nl80211_data *drv = bss->drv;
10380 struct nlattr *replay_nested;
10381 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010382 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010383
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010384 if (!drv->set_rekey_offload)
10385 return;
10386
10387 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010388 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
10389 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -080010390 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010391 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010392 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
10393 replay_ctr)) {
10394 nl80211_nlmsg_clear(msg);
10395 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010396 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010397 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010398
10399 nla_nest_end(msg, replay_nested);
10400
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010401 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010402 if (ret == -EOPNOTSUPP) {
10403 wpa_printf(MSG_DEBUG,
10404 "nl80211: Driver does not support rekey offload");
10405 drv->set_rekey_offload = 0;
10406 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010407}
10408
10409
10410static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
10411 const u8 *addr, int qos)
10412{
10413 /* send data frame to poll STA and check whether
10414 * this frame is ACKed */
10415 struct {
10416 struct ieee80211_hdr hdr;
10417 u16 qos_ctl;
10418 } STRUCT_PACKED nulldata;
10419 size_t size;
10420
10421 /* Send data frame to poll STA and check whether this frame is ACKed */
10422
10423 os_memset(&nulldata, 0, sizeof(nulldata));
10424
10425 if (qos) {
10426 nulldata.hdr.frame_control =
10427 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10428 WLAN_FC_STYPE_QOS_NULL);
10429 size = sizeof(nulldata);
10430 } else {
10431 nulldata.hdr.frame_control =
10432 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10433 WLAN_FC_STYPE_NULLFUNC);
10434 size = sizeof(struct ieee80211_hdr);
10435 }
10436
10437 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
10438 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
10439 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
10440 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
10441
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010442 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010443 0, 0, NULL, 0, 0, -1) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010444 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
10445 "send poll frame");
10446}
10447
10448static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
10449 int qos)
10450{
10451 struct i802_bss *bss = priv;
10452 struct wpa_driver_nl80211_data *drv = bss->drv;
10453 struct nl_msg *msg;
Hai Shalom5f92bc92019-04-18 11:54:11 -070010454 u64 cookie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010455 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010456
10457 if (!drv->poll_command_supported) {
10458 nl80211_send_null_frame(bss, own_addr, addr, qos);
10459 return;
10460 }
10461
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010462 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
10463 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
10464 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010465 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010466 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010467
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010468 ret = send_and_recv_resp(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010469 if (ret < 0) {
10470 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
10471 MACSTR " failed: ret=%d (%s)",
10472 MAC2STR(addr), ret, strerror(-ret));
Hai Shalom5f92bc92019-04-18 11:54:11 -070010473 } else {
10474 wpa_printf(MSG_DEBUG,
10475 "nl80211: Client probe request addr=" MACSTR
10476 " cookie=%llu", MAC2STR(addr),
10477 (long long unsigned int) cookie);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010478 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010479}
10480
10481
10482static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
10483{
10484 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -070010485 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010486
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010487 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
10488 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
10489 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
10490 nlmsg_free(msg);
10491 return -ENOBUFS;
10492 }
Roshan Pius3a1667e2018-07-03 15:17:14 -070010493
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010494 ret = send_and_recv_cmd(bss->drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -070010495 if (ret < 0) {
10496 wpa_printf(MSG_DEBUG,
10497 "nl80211: Setting PS state %s failed: %d (%s)",
10498 enabled ? "enabled" : "disabled",
10499 ret, strerror(-ret));
10500 }
10501 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010502}
10503
10504
10505static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
10506 int ctwindow)
10507{
10508 struct i802_bss *bss = priv;
10509
10510 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
10511 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
10512
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010513 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010514#ifdef ANDROID_P2P
10515 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010516#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010517 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010518#endif /* ANDROID_P2P */
10519 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010520
10521 if (legacy_ps == -1)
10522 return 0;
10523 if (legacy_ps != 0 && legacy_ps != 1)
10524 return -1; /* Not yet supported */
10525
10526 return nl80211_set_power_save(bss, legacy_ps);
10527}
10528
10529
Dmitry Shmidt051af732013-10-22 13:52:46 -070010530static int nl80211_start_radar_detection(void *priv,
10531 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010532{
10533 struct i802_bss *bss = priv;
10534 struct wpa_driver_nl80211_data *drv = bss->drv;
10535 struct nl_msg *msg;
10536 int ret;
10537
Hai Shalom81f62d82019-07-22 12:10:00 -070010538 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)",
10539 freq->freq, freq->ht_enabled, freq->vht_enabled, freq->he_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -070010540 freq->bandwidth, freq->center_freq1, freq->center_freq2);
10541
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010542 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
10543 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
10544 "detection");
10545 return -1;
10546 }
10547
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010548 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
10549 nl80211_put_freq_params(msg, freq) < 0) {
10550 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010551 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010552 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010553
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010554 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010555 if (ret == 0)
10556 return 0;
10557 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
10558 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010559 return -1;
10560}
10561
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010562#ifdef CONFIG_TDLS
10563
Hai Shalomc1a21442022-02-04 13:43:00 -080010564static int nl80211_add_peer_capab(struct nl_msg *msg,
10565 enum tdls_peer_capability capa)
10566{
10567 u32 peer_capab = 0;
10568
10569 if (!capa)
10570 return 0;
10571
10572 if (capa & TDLS_PEER_HT)
10573 peer_capab |= NL80211_TDLS_PEER_HT;
10574 if (capa & TDLS_PEER_VHT)
10575 peer_capab |= NL80211_TDLS_PEER_VHT;
10576 if (capa & TDLS_PEER_WMM)
10577 peer_capab |= NL80211_TDLS_PEER_WMM;
10578 if (capa & TDLS_PEER_HE)
10579 peer_capab |= NL80211_TDLS_PEER_HE;
10580
10581 return nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
10582 peer_capab);
10583}
10584
10585
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010586static int
10587nl80211_tdls_set_discovery_resp_link(struct wpa_driver_nl80211_data *drv,
10588 int link_id)
10589{
10590#ifdef CONFIG_DRIVER_NL80211_QCA
10591 struct nl_msg *msg;
10592 struct nlattr *params;
10593
10594 wpa_printf(MSG_DEBUG, "nl80211: TDLS Discovery Response Tx link ID %u",
10595 link_id);
10596
10597 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10598 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
10599 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10600 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10601 QCA_NL80211_VENDOR_SUBCMD_TDLS_DISC_RSP_EXT) ||
10602 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10603 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_TDLS_DISC_RSP_EXT_TX_LINK,
10604 link_id)) {
10605 wpa_printf(MSG_ERROR,
10606 "%s: err in adding vendor_cmd and vendor_data",
10607 __func__);
10608 nlmsg_free(msg);
10609 return -1;
10610 }
10611 nla_nest_end(msg, params);
10612
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010613 return send_and_recv_cmd(drv, msg);
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010614#else /* CONFIG_DRIVER_NL80211_QCA */
10615 wpa_printf(MSG_ERROR,
10616 "nl80211: Setting TX link for TDLS Discovery Response not supported");
10617 return -1;
10618#endif /* CONFIG_DRIVER_NL80211_QCA */
10619}
10620
10621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010622static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
10623 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070010624 u32 peer_capab, int initiator, const u8 *buf,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010625 size_t len, int link_id)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010626{
10627 struct i802_bss *bss = priv;
10628 struct wpa_driver_nl80211_data *drv = bss->drv;
10629 struct nl_msg *msg;
10630
10631 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10632 return -EOPNOTSUPP;
10633
10634 if (!dst)
10635 return -EINVAL;
10636
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010637 if (link_id >= 0 &&
10638 nl80211_tdls_set_discovery_resp_link(drv, link_id) < 0)
10639 return -EOPNOTSUPP;
10640
Sunil Ravi7f769292024-07-23 22:21:32 +000010641 if (link_id < 0 && drv->sta_mlo_info.valid_links)
10642 link_id = drv->sta_mlo_info.assoc_link_id;
10643
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010644 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
10645 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
10646 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
10647 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
Hai Shalomc1a21442022-02-04 13:43:00 -080010648 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code) ||
Sunil Ravi7f769292024-07-23 22:21:32 +000010649 (link_id >= 0 &&
10650 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Hai Shalomc1a21442022-02-04 13:43:00 -080010651 nl80211_add_peer_capab(msg, peer_capab) ||
10652 (initiator && nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010653 nla_put(msg, NL80211_ATTR_IE, len, buf))
10654 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010655
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010656 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010657
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010658fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010659 nlmsg_free(msg);
10660 return -ENOBUFS;
10661}
10662
10663
10664static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
10665{
10666 struct i802_bss *bss = priv;
10667 struct wpa_driver_nl80211_data *drv = bss->drv;
10668 struct nl_msg *msg;
10669 enum nl80211_tdls_operation nl80211_oper;
Paul Stewart092955c2017-02-06 09:13:09 -080010670 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010671
10672 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10673 return -EOPNOTSUPP;
10674
10675 switch (oper) {
10676 case TDLS_DISCOVERY_REQ:
10677 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
10678 break;
10679 case TDLS_SETUP:
10680 nl80211_oper = NL80211_TDLS_SETUP;
10681 break;
10682 case TDLS_TEARDOWN:
10683 nl80211_oper = NL80211_TDLS_TEARDOWN;
10684 break;
10685 case TDLS_ENABLE_LINK:
10686 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
10687 break;
10688 case TDLS_DISABLE_LINK:
10689 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
10690 break;
10691 case TDLS_ENABLE:
10692 return 0;
10693 case TDLS_DISABLE:
10694 return 0;
10695 default:
10696 return -EINVAL;
10697 }
10698
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010699 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
10700 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
10701 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
10702 nlmsg_free(msg);
10703 return -ENOBUFS;
10704 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010705
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010706 res = send_and_recv_cmd(drv, msg);
Paul Stewart092955c2017-02-06 09:13:09 -080010707 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
10708 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
10709 strerror(-res));
10710 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010711}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010712
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010713
10714static int
10715nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
10716 const struct hostapd_freq_params *params)
10717{
10718 struct i802_bss *bss = priv;
10719 struct wpa_driver_nl80211_data *drv = bss->drv;
10720 struct nl_msg *msg;
10721 int ret = -ENOBUFS;
10722
10723 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
10724 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
10725 return -EOPNOTSUPP;
10726
10727 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
10728 " oper_class=%u freq=%u",
10729 MAC2STR(addr), oper_class, params->freq);
10730 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
10731 if (!msg ||
10732 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10733 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
10734 (ret = nl80211_put_freq_params(msg, params))) {
10735 nlmsg_free(msg);
10736 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
10737 return ret;
10738 }
10739
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010740 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010741}
10742
10743
10744static int
10745nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
10746{
10747 struct i802_bss *bss = priv;
10748 struct wpa_driver_nl80211_data *drv = bss->drv;
10749 struct nl_msg *msg;
10750
10751 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
10752 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
10753 return -EOPNOTSUPP;
10754
10755 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
10756 MAC2STR(addr));
10757 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
10758 if (!msg ||
10759 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
10760 nlmsg_free(msg);
10761 wpa_printf(MSG_DEBUG,
10762 "nl80211: Could not build TDLS cancel chan switch");
10763 return -ENOBUFS;
10764 }
10765
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010766 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010767}
10768
10769#endif /* CONFIG TDLS */
10770
10771
Hai Shalomfdcde762020-04-02 11:19:20 -070010772static int driver_nl80211_set_key(void *priv,
10773 struct wpa_driver_set_key_params *params)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010774{
10775 struct i802_bss *bss = priv;
Hai Shalomfdcde762020-04-02 11:19:20 -070010776
10777 return wpa_driver_nl80211_set_key(bss, params);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010778}
10779
10780
10781static int driver_nl80211_scan2(void *priv,
10782 struct wpa_driver_scan_params *params)
10783{
10784 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010785#ifdef CONFIG_DRIVER_NL80211_QCA
10786 struct wpa_driver_nl80211_data *drv = bss->drv;
10787
10788 /*
10789 * Do a vendor specific scan if possible. If only_new_results is
10790 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
10791 * cannot be achieved through a vendor scan. The below condition may
10792 * need to be modified if new scan flags are added in the future whose
10793 * functionality can only be achieved through a normal scan.
10794 */
10795 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
10796 return wpa_driver_nl80211_vendor_scan(bss, params);
10797#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010798 return wpa_driver_nl80211_scan(bss, params);
10799}
10800
10801
10802static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -070010803 u16 reason_code)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010804{
10805 struct i802_bss *bss = priv;
10806 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
10807}
10808
10809
10810static int driver_nl80211_authenticate(void *priv,
10811 struct wpa_driver_auth_params *params)
10812{
10813 struct i802_bss *bss = priv;
10814 return wpa_driver_nl80211_authenticate(bss, params);
10815}
10816
10817
10818static void driver_nl80211_deinit(void *priv)
10819{
10820 struct i802_bss *bss = priv;
10821 wpa_driver_nl80211_deinit(bss);
10822}
10823
10824
10825static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
10826 const char *ifname)
10827{
10828 struct i802_bss *bss = priv;
10829 return wpa_driver_nl80211_if_remove(bss, type, ifname);
10830}
10831
10832
Sunil Ravi99c035e2024-07-12 01:42:03 +000010833#ifdef CONFIG_IEEE80211BE
10834
10835static int driver_nl80211_link_remove(void *priv, enum wpa_driver_if_type type,
10836 const char *ifname, u8 link_id)
10837{
10838 struct i802_bss *bss = priv;
10839 struct wpa_driver_nl80211_data *drv = bss->drv;
10840
10841 if (type != WPA_IF_AP_BSS ||
10842 !nl80211_link_valid(bss->valid_links, link_id))
10843 return -1;
10844
10845 wpa_printf(MSG_DEBUG,
10846 "nl80211: Teardown AP(%s) link %d (type=%d ifname=%s links=0x%x)",
10847 bss->ifname, link_id, type, ifname, bss->valid_links);
10848
10849 nl80211_remove_link(bss, link_id);
10850
10851 bss->ctx = bss->flink->ctx;
10852
Sunil Ravi7f769292024-07-23 22:21:32 +000010853 if (drv->first_bss == bss && bss->valid_links)
Sunil Ravi99c035e2024-07-12 01:42:03 +000010854 drv->ctx = bss->ctx;
10855
10856 if (!bss->valid_links) {
10857 wpa_printf(MSG_DEBUG,
10858 "nl80211: No more links remaining, so remove interface");
10859 return wpa_driver_nl80211_if_remove(bss, type, ifname);
10860 }
10861
10862 return 0;
10863}
10864
10865
10866static bool nl80211_is_drv_shared(void *priv, void *bss_ctx)
10867{
10868 struct i802_bss *bss = priv;
10869 struct wpa_driver_nl80211_data *drv = bss->drv;
10870 unsigned int num_bss = 0;
10871
10872 /* If any other BSS exist, someone else is using this since at this
10873 * time, we would have removed all BSSs created by this driver and only
10874 * this BSS should be remaining if the driver is not shared by anyone.
10875 */
10876 for (bss = drv->first_bss; bss; bss = bss->next) {
10877 num_bss++;
10878 if (num_bss > 1)
10879 return true;
10880 }
10881
10882 /* This is the only BSS present */
10883 bss = priv;
10884
10885 /* If only one/no link is there no one is sharing */
10886 if (bss->valid_links <= 1)
10887 return false;
10888
10889 /* More than one link means someone is still using. To check if
10890 * only 1 bit is set, power of 2 condition can be checked. */
10891 if (!(bss->valid_links & (bss->valid_links - 1)))
10892 return false;
10893
10894 return true;
10895}
10896
10897#endif /* CONFIG_IEEE80211BE */
10898
10899
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010900static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -070010901 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010902 unsigned int freq,
Hai Shalomfdcde762020-04-02 11:19:20 -070010903 const u16 *csa_offs, size_t csa_offs_len,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010904 int no_encrypt, unsigned int wait,
10905 int link_id)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010906{
10907 struct i802_bss *bss = priv;
10908 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Hai Shalomfdcde762020-04-02 11:19:20 -070010909 freq, 0, 0, wait, csa_offs,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010910 csa_offs_len, no_encrypt, link_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010911}
10912
10913
10914static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
10915{
10916 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010917 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010918}
10919
10920
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010921static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010922 const char *ifname, int vlan_id,
10923 int link_id)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010924{
10925 struct i802_bss *bss = priv;
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010926 return i802_set_sta_vlan(bss, addr, ifname, vlan_id, link_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010927}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010928
10929
10930static int driver_nl80211_read_sta_data(void *priv,
10931 struct hostap_sta_driver_data *data,
10932 const u8 *addr)
10933{
10934 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -080010935
10936 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010937 return i802_read_sta_data(bss, data, addr);
10938}
10939
10940
10941static int driver_nl80211_send_action(void *priv, unsigned int freq,
10942 unsigned int wait_time,
10943 const u8 *dst, const u8 *src,
10944 const u8 *bssid,
10945 const u8 *data, size_t data_len,
10946 int no_cck)
10947{
10948 struct i802_bss *bss = priv;
10949 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
10950 bssid, data, data_len, no_cck);
10951}
10952
10953
10954static int driver_nl80211_probe_req_report(void *priv, int report)
10955{
10956 struct i802_bss *bss = priv;
10957 return wpa_driver_nl80211_probe_req_report(bss, report);
10958}
10959
10960
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010961static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
10962 const u8 *ies, size_t ies_len)
10963{
10964 int ret;
10965 struct nl_msg *msg;
10966 struct i802_bss *bss = priv;
10967 struct wpa_driver_nl80211_data *drv = bss->drv;
10968 u16 mdid = WPA_GET_LE16(md);
10969
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010970 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010971 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
10972 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
10973 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
10974 nlmsg_free(msg);
10975 return -ENOBUFS;
10976 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010977
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010978 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010979 if (ret) {
10980 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
10981 "err=%d (%s)", ret, strerror(-ret));
10982 }
10983
10984 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010985}
10986
10987
Hai Shalom81f62d82019-07-22 12:10:00 -070010988static int nl80211_update_dh_ie(void *priv, const u8 *peer_mac,
10989 u16 reason_code, const u8 *ie, size_t ie_len)
10990{
10991 int ret;
10992 struct nl_msg *msg;
10993 struct i802_bss *bss = priv;
10994 struct wpa_driver_nl80211_data *drv = bss->drv;
10995
10996 wpa_printf(MSG_DEBUG, "nl80211: Updating DH IE peer: " MACSTR
10997 " reason %u", MAC2STR(peer_mac), reason_code);
10998 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UPDATE_OWE_INFO)) ||
10999 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer_mac) ||
11000 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, reason_code) ||
11001 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie))) {
11002 nlmsg_free(msg);
11003 return -ENOBUFS;
11004 }
11005
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011006 ret = send_and_recv_cmd(drv, msg);
Hai Shalom81f62d82019-07-22 12:10:00 -070011007 if (ret) {
11008 wpa_printf(MSG_DEBUG,
11009 "nl80211: update_dh_ie failed err=%d (%s)",
11010 ret, strerror(-ret));
11011 }
11012
11013 return ret;
11014}
11015
11016
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070011017static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011018{
11019 struct i802_bss *bss = priv;
11020 struct wpa_driver_nl80211_data *drv = bss->drv;
11021
11022 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11023 return NULL;
11024
11025 return bss->addr;
11026}
11027
11028
Dmitry Shmidt56052862013-10-04 10:23:25 -070011029static const char * scan_state_str(enum scan_states scan_state)
11030{
11031 switch (scan_state) {
11032 case NO_SCAN:
11033 return "NO_SCAN";
11034 case SCAN_REQUESTED:
11035 return "SCAN_REQUESTED";
11036 case SCAN_STARTED:
11037 return "SCAN_STARTED";
11038 case SCAN_COMPLETED:
11039 return "SCAN_COMPLETED";
11040 case SCAN_ABORTED:
11041 return "SCAN_ABORTED";
11042 case SCHED_SCAN_STARTED:
11043 return "SCHED_SCAN_STARTED";
11044 case SCHED_SCAN_STOPPED:
11045 return "SCHED_SCAN_STOPPED";
11046 case SCHED_SCAN_RESULTS:
11047 return "SCHED_SCAN_RESULTS";
11048 }
11049
11050 return "??";
11051}
11052
11053
11054static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11055{
11056 struct i802_bss *bss = priv;
11057 struct wpa_driver_nl80211_data *drv = bss->drv;
11058 int res;
11059 char *pos, *end;
Hai Shalom74f70d42019-02-11 14:42:39 -080011060 struct nl_msg *msg;
11061 char alpha2[3] = { 0, 0, 0 };
Dmitry Shmidt56052862013-10-04 10:23:25 -070011062
11063 pos = buf;
11064 end = buf + buflen;
11065
11066 res = os_snprintf(pos, end - pos,
11067 "ifindex=%d\n"
11068 "ifname=%s\n"
11069 "brname=%s\n"
11070 "addr=" MACSTR "\n"
11071 "freq=%d\n"
Hai Shalomc9e41a12018-07-31 14:41:42 -070011072 "%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011073 bss->ifindex,
11074 bss->ifname,
11075 bss->brname,
11076 MAC2STR(bss->addr),
Sunil Ravi036cec52023-03-29 11:35:17 -070011077 bss->flink->freq,
11078 bss->flink->beacon_set ? "beacon_set=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011079 bss->added_if_into_bridge ?
11080 "added_if_into_bridge=1\n" : "",
Hai Shalomc9e41a12018-07-31 14:41:42 -070011081 bss->already_in_bridge ? "already_in_bridge=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011082 bss->added_bridge ? "added_bridge=1\n" : "",
11083 bss->in_deinit ? "in_deinit=1\n" : "",
11084 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011085 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011086 return pos - buf;
11087 pos += res;
11088
11089 if (bss->wdev_id_set) {
11090 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11091 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011092 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011093 return pos - buf;
11094 pos += res;
11095 }
11096
11097 res = os_snprintf(pos, end - pos,
11098 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011099 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -070011100 "drv_ifindex=%d\n"
11101 "operstate=%d\n"
11102 "scan_state=%s\n"
11103 "auth_bssid=" MACSTR "\n"
11104 "auth_attempt_bssid=" MACSTR "\n"
11105 "bssid=" MACSTR "\n"
11106 "prev_bssid=" MACSTR "\n"
11107 "associated=%d\n"
11108 "assoc_freq=%u\n"
11109 "monitor_sock=%d\n"
11110 "monitor_ifidx=%d\n"
11111 "monitor_refcount=%d\n"
11112 "last_mgmt_freq=%u\n"
11113 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011114 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011115 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011116 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -070011117 drv->ifindex,
11118 drv->operstate,
11119 scan_state_str(drv->scan_state),
11120 MAC2STR(drv->auth_bssid),
11121 MAC2STR(drv->auth_attempt_bssid),
11122 MAC2STR(drv->bssid),
11123 MAC2STR(drv->prev_bssid),
11124 drv->associated,
11125 drv->assoc_freq,
11126 drv->monitor_sock,
11127 drv->monitor_ifidx,
11128 drv->monitor_refcount,
11129 drv->last_mgmt_freq,
11130 drv->eapol_tx_sock,
11131 drv->ignore_if_down_event ?
11132 "ignore_if_down_event=1\n" : "",
11133 drv->scan_complete_events ?
11134 "scan_complete_events=1\n" : "",
11135 drv->disabled_11b_rates ?
11136 "disabled_11b_rates=1\n" : "",
11137 drv->pending_remain_on_chan ?
11138 "pending_remain_on_chan=1\n" : "",
11139 drv->in_interface_list ? "in_interface_list=1\n" : "",
11140 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11141 drv->poll_command_supported ?
11142 "poll_command_supported=1\n" : "",
11143 drv->data_tx_status ? "data_tx_status=1\n" : "",
11144 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11145 drv->retry_auth ? "retry_auth=1\n" : "",
11146 drv->use_monitor ? "use_monitor=1\n" : "",
11147 drv->ignore_next_local_disconnect ?
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011148 "ignore_next_local_disconnect\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011149 drv->ignore_next_local_deauth ?
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011150 "ignore_next_local_deauth\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011151 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011152 return pos - buf;
11153 pos += res;
11154
Sunil Ravi89eba102022-09-13 21:04:37 -070011155 if (drv->sta_mlo_info.valid_links) {
11156 int i;
11157 struct driver_sta_mlo_info *mlo = &drv->sta_mlo_info;
11158
11159 res = os_snprintf(pos, end - pos,
Sunil Ravi640215c2023-06-28 23:08:09 +000011160 "ap_mld_addr=" MACSTR "\n"
11161 "default_map=%d\n",
11162 MAC2STR(mlo->ap_mld_addr),
11163 mlo->default_map);
Sunil Ravi89eba102022-09-13 21:04:37 -070011164 if (os_snprintf_error(end - pos, res))
11165 return pos - buf;
11166 pos += res;
11167
Sunil Ravi99c035e2024-07-12 01:42:03 +000011168 for_each_link(mlo->valid_links, i) {
Sunil Ravi89eba102022-09-13 21:04:37 -070011169 res = os_snprintf(pos, end - pos,
11170 "link_addr[%u]=" MACSTR "\n"
11171 "link_bssid[%u]=" MACSTR "\n"
11172 "link_freq[%u]=%u\n",
11173 i, MAC2STR(mlo->links[i].addr),
11174 i, MAC2STR(mlo->links[i].bssid),
11175 i, mlo->links[i].freq);
11176 if (os_snprintf_error(end - pos, res))
11177 return pos - buf;
11178 pos += res;
Sunil Ravi640215c2023-06-28 23:08:09 +000011179
11180 if (!mlo->default_map) {
11181 res = os_snprintf(
11182 pos, end - pos,
11183 "uplink_map[%u]=%x\n"
11184 "downlink_map[%u]=%x\n",
11185 i, mlo->links[i].t2lmap.uplink,
11186 i, mlo->links[i].t2lmap.downlink);
11187 if (os_snprintf_error(end - pos, res))
11188 return pos - buf;
11189 pos += res;
11190 }
Sunil Ravi89eba102022-09-13 21:04:37 -070011191 }
11192 }
11193
Dmitry Shmidt56052862013-10-04 10:23:25 -070011194 if (drv->has_capability) {
11195 res = os_snprintf(pos, end - pos,
11196 "capa.key_mgmt=0x%x\n"
11197 "capa.enc=0x%x\n"
11198 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011199 "capa.flags=0x%llx\n"
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011200 "capa.flags2=0x%llx\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011201 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -070011202 "capa.max_scan_ssids=%d\n"
11203 "capa.max_sched_scan_ssids=%d\n"
11204 "capa.sched_scan_supported=%d\n"
11205 "capa.max_match_sets=%d\n"
11206 "capa.max_remain_on_chan=%u\n"
11207 "capa.max_stations=%u\n"
11208 "capa.probe_resp_offloads=0x%x\n"
11209 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011210 "capa.num_multichan_concurrent=%u\n"
11211 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011212 "capa.mac_addr_rand_scan_supported=%d\n"
11213 "capa.conc_capab=%u\n"
11214 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080011215 "capa.max_conc_chan_5_0=%u\n"
11216 "capa.max_sched_scan_plans=%u\n"
11217 "capa.max_sched_scan_plan_interval=%u\n"
Sunil Ravi77d572f2023-01-17 23:58:31 +000011218 "capa.max_sched_scan_plan_iterations=%u\n"
11219 "capa.mbssid_max_interfaces=%u\n"
11220 "capa.ema_max_periodicity=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011221 drv->capa.key_mgmt,
11222 drv->capa.enc,
11223 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011224 (unsigned long long) drv->capa.flags,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011225 (unsigned long long) drv->capa.flags2,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011226 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -070011227 drv->capa.max_scan_ssids,
11228 drv->capa.max_sched_scan_ssids,
11229 drv->capa.sched_scan_supported,
11230 drv->capa.max_match_sets,
11231 drv->capa.max_remain_on_chan,
11232 drv->capa.max_stations,
11233 drv->capa.probe_resp_offloads,
11234 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011235 drv->capa.num_multichan_concurrent,
11236 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011237 drv->capa.mac_addr_rand_scan_supported,
11238 drv->capa.conc_capab,
11239 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080011240 drv->capa.max_conc_chan_5_0,
11241 drv->capa.max_sched_scan_plans,
11242 drv->capa.max_sched_scan_plan_interval,
Sunil Ravi77d572f2023-01-17 23:58:31 +000011243 drv->capa.max_sched_scan_plan_iterations,
11244 drv->capa.mbssid_max_interfaces,
11245 drv->capa.ema_max_periodicity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011246 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011247 return pos - buf;
11248 pos += res;
11249 }
11250
Hai Shalom74f70d42019-02-11 14:42:39 -080011251 msg = nlmsg_alloc();
11252 if (msg &&
11253 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG) &&
11254 nla_put_u32(msg, NL80211_ATTR_WIPHY, drv->wiphy_idx) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011255 if (send_and_recv_resp(drv, msg, nl80211_get_country,
11256 alpha2) == 0 &&
Hai Shalom74f70d42019-02-11 14:42:39 -080011257 alpha2[0]) {
11258 res = os_snprintf(pos, end - pos, "country=%s\n",
11259 alpha2);
11260 if (os_snprintf_error(end - pos, res))
11261 return pos - buf;
11262 pos += res;
11263 }
11264 } else {
11265 nlmsg_free(msg);
11266 }
11267
Dmitry Shmidt56052862013-10-04 10:23:25 -070011268 return pos - buf;
11269}
11270
11271
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011272static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11273{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011274 if ((settings->head &&
11275 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
11276 settings->head_len, settings->head)) ||
11277 (settings->tail &&
11278 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
11279 settings->tail_len, settings->tail)) ||
11280 (settings->beacon_ies &&
11281 nla_put(msg, NL80211_ATTR_IE,
11282 settings->beacon_ies_len, settings->beacon_ies)) ||
11283 (settings->proberesp_ies &&
11284 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
11285 settings->proberesp_ies_len, settings->proberesp_ies)) ||
11286 (settings->assocresp_ies &&
11287 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
11288 settings->assocresp_ies_len, settings->assocresp_ies)) ||
11289 (settings->probe_resp &&
11290 nla_put(msg, NL80211_ATTR_PROBE_RESP,
11291 settings->probe_resp_len, settings->probe_resp)))
11292 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011293
11294 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011295}
11296
11297
11298static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11299{
11300 struct nl_msg *msg;
11301 struct i802_bss *bss = priv;
11302 struct wpa_driver_nl80211_data *drv = bss->drv;
11303 struct nlattr *beacon_csa;
11304 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011305 int csa_off_len = 0;
11306 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011307
Hai Shalomc1a21442022-02-04 13:43:00 -080011308 wpa_printf(MSG_DEBUG,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011309 "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 -080011310 settings->cs_count, settings->block_tx,
Hai Shalomc1a21442022-02-04 13:43:00 -080011311 settings->freq_params.freq,
11312 settings->freq_params.channel,
11313 settings->freq_params.sec_channel_offset,
11314 settings->freq_params.bandwidth,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011315 settings->freq_params.center_freq1,
Hai Shalomc1a21442022-02-04 13:43:00 -080011316 settings->freq_params.center_freq2,
Sunil Ravi036cec52023-03-29 11:35:17 -070011317 settings->punct_bitmap,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011318 settings->link_id,
Hai Shalomc1a21442022-02-04 13:43:00 -080011319 settings->freq_params.ht_enabled ? " ht" : "",
11320 settings->freq_params.vht_enabled ? " vht" : "",
11321 settings->freq_params.he_enabled ? " he" : "");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011322
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011323 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011324 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11325 return -EOPNOTSUPP;
11326 }
11327
Roshan Pius3a1667e2018-07-03 15:17:14 -070011328 if (drv->nlmode != NL80211_IFTYPE_AP &&
11329 drv->nlmode != NL80211_IFTYPE_P2P_GO &&
11330 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011331 return -EOPNOTSUPP;
11332
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011333 /*
11334 * Remove empty counters, assuming Probe Response and Beacon frame
11335 * counters match. This implementation assumes that there are only two
11336 * counters.
11337 */
11338 if (settings->counter_offset_beacon[0] &&
11339 !settings->counter_offset_beacon[1]) {
11340 csa_off_len = 1;
11341 } else if (settings->counter_offset_beacon[1] &&
11342 !settings->counter_offset_beacon[0]) {
11343 csa_off_len = 1;
11344 settings->counter_offset_beacon[0] =
11345 settings->counter_offset_beacon[1];
11346 settings->counter_offset_presp[0] =
11347 settings->counter_offset_presp[1];
11348 } else if (settings->counter_offset_beacon[1] &&
11349 settings->counter_offset_beacon[0]) {
11350 csa_off_len = 2;
11351 } else {
11352 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
11353 return -EINVAL;
11354 }
11355
11356 /* Check CSA counters validity */
11357 if (drv->capa.max_csa_counters &&
11358 csa_off_len > drv->capa.max_csa_counters) {
11359 wpa_printf(MSG_ERROR,
11360 "nl80211: Too many CSA counters provided");
11361 return -EINVAL;
11362 }
11363
11364 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011365 return -EINVAL;
11366
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011367 for (i = 0; i < csa_off_len; i++) {
11368 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
11369 u16 csa_c_off_presp = settings->counter_offset_presp[i];
11370
11371 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
11372 (settings->beacon_csa.tail[csa_c_off_bcn] !=
11373 settings->cs_count))
11374 return -EINVAL;
11375
11376 if (settings->beacon_csa.probe_resp &&
11377 ((settings->beacon_csa.probe_resp_len <=
11378 csa_c_off_presp) ||
11379 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
11380 settings->cs_count)))
11381 return -EINVAL;
11382 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011383
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011384 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
11385 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
11386 settings->cs_count) ||
11387 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
11388 (settings->block_tx &&
Sunil Ravi036cec52023-03-29 11:35:17 -070011389 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)) ||
11390 (settings->punct_bitmap &&
11391 nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011392 settings->punct_bitmap)) ||
11393 (settings->link_id != NL80211_DRV_LINK_ID_NA &&
11394 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, settings->link_id)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011395 goto error;
11396
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011397 /* beacon_after params */
11398 ret = set_beacon_data(msg, &settings->beacon_after);
11399 if (ret)
11400 goto error;
11401
11402 /* beacon_csa params */
11403 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
11404 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011405 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011406
11407 ret = set_beacon_data(msg, &settings->beacon_csa);
11408 if (ret)
11409 goto error;
11410
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011411 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
11412 csa_off_len * sizeof(u16),
11413 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011414 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011415 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
11416 csa_off_len * sizeof(u16),
11417 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011418 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011419
11420 nla_nest_end(msg, beacon_csa);
Sunil Ravi7f769292024-07-23 22:21:32 +000011421
11422#ifdef CONFIG_IEEE80211AX
11423 if (settings->ubpr.unsol_bcast_probe_resp_interval &&
11424 nl80211_unsol_bcast_probe_resp(bss, msg, &settings->ubpr) < 0)
11425 goto fail;
11426#endif /* CONFIG_IEEE80211AX */
11427
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011428 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011429 if (ret) {
11430 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
11431 ret, strerror(-ret));
11432 }
11433 return ret;
11434
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011435fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011436 ret = -ENOBUFS;
11437error:
11438 nlmsg_free(msg);
11439 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
11440 return ret;
11441}
11442
11443
Sunil Ravia04bd252022-05-02 22:54:18 -070011444#ifdef CONFIG_IEEE80211AX
11445static int nl80211_switch_color(void *priv, struct cca_settings *settings)
11446{
11447 struct i802_bss *bss = priv;
11448 struct wpa_driver_nl80211_data *drv = bss->drv;
11449 struct nlattr *beacon_cca;
11450 struct nl_msg *msg;
11451 int ret = -ENOBUFS;
11452
11453 wpa_printf(MSG_DEBUG,
11454 "nl80211: Color change request (cca_count=%u color=%d)",
11455 settings->cca_count, settings->cca_color);
11456
11457 if (drv->nlmode != NL80211_IFTYPE_AP)
11458 return -EOPNOTSUPP;
11459
11460 if (!settings->beacon_cca.tail)
11461 return -EINVAL;
11462
11463 if (settings->beacon_cca.tail_len <= settings->counter_offset_beacon ||
11464 settings->beacon_cca.tail[settings->counter_offset_beacon] !=
11465 settings->cca_count)
11466 return -EINVAL;
11467
11468 if (settings->beacon_cca.probe_resp &&
11469 (settings->beacon_cca.probe_resp_len <=
11470 settings->counter_offset_presp ||
11471 settings->beacon_cca.probe_resp[settings->counter_offset_presp] !=
11472 settings->cca_count))
11473 return -EINVAL;
11474
11475 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_COLOR_CHANGE_REQUEST);
11476 if (!msg ||
11477 nla_put_u8(msg, NL80211_ATTR_COLOR_CHANGE_COUNT,
11478 settings->cca_count) ||
11479 nla_put_u8(msg, NL80211_ATTR_COLOR_CHANGE_COLOR,
11480 settings->cca_color))
11481 goto error;
11482
11483 /* beacon_after params */
11484 ret = set_beacon_data(msg, &settings->beacon_after);
11485 if (ret)
11486 goto error;
11487
11488 /* beacon_csa params */
11489 beacon_cca = nla_nest_start(msg, NL80211_ATTR_COLOR_CHANGE_ELEMS);
11490 if (!beacon_cca) {
11491 ret = -ENOBUFS;
11492 goto error;
11493 }
11494
11495 ret = set_beacon_data(msg, &settings->beacon_cca);
11496 if (ret)
11497 goto error;
11498
11499 if (nla_put_u16(msg, NL80211_ATTR_CNTDWN_OFFS_BEACON,
11500 settings->counter_offset_beacon) ||
11501 (settings->beacon_cca.probe_resp &&
11502 nla_put_u16(msg, NL80211_ATTR_CNTDWN_OFFS_PRESP,
11503 settings->counter_offset_presp))) {
11504 ret = -ENOBUFS;
11505 goto error;
11506 }
11507
11508 nla_nest_end(msg, beacon_cca);
Sunil Ravi7f769292024-07-23 22:21:32 +000011509
11510 if (settings->ubpr.unsol_bcast_probe_resp_interval &&
11511 nl80211_unsol_bcast_probe_resp(bss, msg, &settings->ubpr) < 0) {
11512 ret = -ENOBUFS;
11513 goto error;
11514 }
11515
11516#ifdef CONFIG_IEEE80211BE
11517 if (nl80211_link_valid(bss->valid_links, settings->link_id)) {
11518 wpa_printf(MSG_DEBUG,
11519 "nl80211: Color change request on link_id=%d",
11520 settings->link_id);
11521
11522 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
11523 settings->link_id)) {
11524 nlmsg_free(msg);
11525 return -1;
11526 }
11527 }
11528#endif /* CONFIG_IEEE80211BE */
11529
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011530 ret = send_and_recv_cmd(drv, msg);
Sunil Ravia04bd252022-05-02 22:54:18 -070011531 if (ret) {
11532 wpa_printf(MSG_DEBUG,
11533 "nl80211: switch_color failed err=%d (%s)",
11534 ret, strerror(-ret));
11535 }
11536 return ret;
11537
11538error:
11539 nlmsg_free(msg);
11540 wpa_printf(MSG_DEBUG, "nl80211: Could not build color switch request");
11541 return ret;
11542}
11543#endif /* CONFIG_IEEE80211AX */
11544
11545
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011546static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
11547 u8 user_priority, u16 admitted_time)
11548{
11549 struct i802_bss *bss = priv;
11550 struct wpa_driver_nl80211_data *drv = bss->drv;
11551 struct nl_msg *msg;
11552 int ret;
11553
11554 wpa_printf(MSG_DEBUG,
11555 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
11556 tsid, admitted_time, user_priority);
11557
11558 if (!is_sta_interface(drv->nlmode))
11559 return -ENOTSUP;
11560
11561 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
11562 if (!msg ||
11563 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
11564 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11565 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
11566 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
11567 nlmsg_free(msg);
11568 return -ENOBUFS;
11569 }
11570
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011571 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011572 if (ret)
11573 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
11574 ret, strerror(-ret));
11575 return ret;
11576}
11577
11578
11579static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
11580{
11581 struct i802_bss *bss = priv;
11582 struct wpa_driver_nl80211_data *drv = bss->drv;
11583 struct nl_msg *msg;
11584 int ret;
11585
11586 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
11587
11588 if (!is_sta_interface(drv->nlmode))
11589 return -ENOTSUP;
11590
11591 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
11592 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
11593 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
11594 nlmsg_free(msg);
11595 return -ENOBUFS;
11596 }
11597
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011598 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011599 if (ret)
11600 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
11601 ret, strerror(-ret));
11602 return ret;
11603}
11604
11605
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011606#ifdef CONFIG_TESTING_OPTIONS
11607static int cmd_reply_handler(struct nl_msg *msg, void *arg)
11608{
11609 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11610 struct wpabuf *buf = arg;
11611
11612 if (!buf)
11613 return NL_SKIP;
11614
11615 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
11616 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
11617 return NL_SKIP;
11618 }
11619
11620 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
11621 genlmsg_attrlen(gnlh, 0));
11622
11623 return NL_SKIP;
11624}
11625#endif /* CONFIG_TESTING_OPTIONS */
11626
11627
11628static int vendor_reply_handler(struct nl_msg *msg, void *arg)
11629{
11630 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11631 struct nlattr *nl_vendor_reply, *nl;
11632 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11633 struct wpabuf *buf = arg;
11634 int rem;
11635
11636 if (!buf)
11637 return NL_SKIP;
11638
11639 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11640 genlmsg_attrlen(gnlh, 0), NULL);
11641 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
11642
11643 if (!nl_vendor_reply)
11644 return NL_SKIP;
11645
11646 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
11647 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
11648 return NL_SKIP;
11649 }
11650
11651 nla_for_each_nested(nl, nl_vendor_reply, rem) {
11652 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
11653 }
11654
11655 return NL_SKIP;
11656}
11657
11658
Hai Shalom60840252021-02-19 19:02:11 -080011659static bool is_cmd_with_nested_attrs(unsigned int vendor_id,
11660 unsigned int subcmd)
11661{
11662 if (vendor_id != OUI_QCA)
11663 return true;
11664
11665 switch (subcmd) {
11666 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
11667 case QCA_NL80211_VENDOR_SUBCMD_STATS_EXT:
11668 case QCA_NL80211_VENDOR_SUBCMD_SCANNING_MAC_OUI:
11669 case QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY:
11670 case QCA_NL80211_VENDOR_SUBCMD_SPECTRAL_SCAN_GET_STATUS:
11671 case QCA_NL80211_VENDOR_SUBCMD_NAN:
11672 return false;
11673 default:
11674 return true;
11675 }
11676}
11677
11678
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011679static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
11680 unsigned int subcmd, const u8 *data,
Hai Shalom60840252021-02-19 19:02:11 -080011681 size_t data_len, enum nested_attr nested_attr,
11682 struct wpabuf *buf)
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011683{
11684 struct i802_bss *bss = priv;
11685 struct wpa_driver_nl80211_data *drv = bss->drv;
11686 struct nl_msg *msg;
Hai Shalom60840252021-02-19 19:02:11 -080011687 int ret, nla_flag;
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011688
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011689#ifdef CONFIG_TESTING_OPTIONS
11690 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011691 msg = nlmsg_alloc();
11692 if (!msg)
11693 return -ENOMEM;
11694
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011695 nl80211_cmd(drv, msg, 0, subcmd);
11696 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
11697 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011698 goto fail;
Hai Shalomb755a2a2020-04-23 21:49:02 -070011699 /* This test vendor_cmd can be used with nl80211 commands that
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011700 * need the connect nl_sock, so use the variant that takes in
11701 * bss->nl_connect as the handle. */
11702 ret = send_and_recv(drv->global, bss->nl_connect, msg,
11703 cmd_reply_handler, buf, NULL, NULL, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011704 if (ret)
11705 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
11706 ret);
11707 return ret;
11708 }
11709#endif /* CONFIG_TESTING_OPTIONS */
11710
Hai Shalom60840252021-02-19 19:02:11 -080011711 if (nested_attr == NESTED_ATTR_USED)
11712 nla_flag = NLA_F_NESTED;
11713 else if (nested_attr == NESTED_ATTR_UNSPECIFIED &&
11714 is_cmd_with_nested_attrs(vendor_id, subcmd))
11715 nla_flag = NLA_F_NESTED;
11716 else
11717 nla_flag = 0;
11718
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011719 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
11720 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
11721 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
11722 (data &&
Hai Shalom60840252021-02-19 19:02:11 -080011723 nla_put(msg, nla_flag | NL80211_ATTR_VENDOR_DATA,
11724 data_len, data)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011725 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011726
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011727 ret = send_and_recv_resp(drv, msg, vendor_reply_handler, buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011728 if (ret)
11729 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
11730 ret);
11731 return ret;
11732
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011733fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011734 nlmsg_free(msg);
11735 return -ENOBUFS;
11736}
11737
11738
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011739static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
11740 u8 qos_map_set_len)
11741{
11742 struct i802_bss *bss = priv;
11743 struct wpa_driver_nl80211_data *drv = bss->drv;
11744 struct nl_msg *msg;
11745 int ret;
11746
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011747 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
11748 qos_map_set, qos_map_set_len);
11749
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011750 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_QOS_MAP)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011751 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
11752 nlmsg_free(msg);
11753 return -ENOBUFS;
11754 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011755
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011756 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011757 if (ret)
11758 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
11759
11760 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011761}
11762
11763
Hai Shalomfdcde762020-04-02 11:19:20 -070011764static int get_wowlan_handler(struct nl_msg *msg, void *arg)
11765{
11766 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11767 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11768 int *wowlan_enabled = arg;
11769
11770 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11771 genlmsg_attrlen(gnlh, 0), NULL);
11772
11773 *wowlan_enabled = !!tb[NL80211_ATTR_WOWLAN_TRIGGERS];
11774
11775 return NL_SKIP;
11776}
11777
11778
11779static int nl80211_get_wowlan(void *priv)
11780{
11781 struct i802_bss *bss = priv;
11782 struct wpa_driver_nl80211_data *drv = bss->drv;
11783 struct nl_msg *msg;
11784 int wowlan_enabled;
11785 int ret;
11786
11787 wpa_printf(MSG_DEBUG, "nl80211: Getting wowlan status");
11788
11789 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_WOWLAN);
11790
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011791 ret = send_and_recv_resp(drv, msg, get_wowlan_handler, &wowlan_enabled);
Hai Shalomfdcde762020-04-02 11:19:20 -070011792 if (ret) {
11793 wpa_printf(MSG_DEBUG, "nl80211: Getting wowlan status failed");
11794 return 0;
11795 }
11796
11797 wpa_printf(MSG_DEBUG, "nl80211: wowlan is %s",
11798 wowlan_enabled ? "enabled" : "disabled");
11799
11800 return wowlan_enabled;
11801}
11802
11803
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011804static int nl80211_set_wowlan(void *priv,
11805 const struct wowlan_triggers *triggers)
11806{
11807 struct i802_bss *bss = priv;
11808 struct wpa_driver_nl80211_data *drv = bss->drv;
11809 struct nl_msg *msg;
11810 struct nlattr *wowlan_triggers;
11811 int ret;
11812
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011813 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
11814
Dmitry Shmidta3dc3092015-06-23 11:21:28 -070011815 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011816 !(wowlan_triggers = nla_nest_start(msg,
11817 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
11818 (triggers->any &&
11819 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
11820 (triggers->disconnect &&
11821 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
11822 (triggers->magic_pkt &&
11823 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
11824 (triggers->gtk_rekey_failure &&
11825 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
11826 (triggers->eap_identity_req &&
11827 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
11828 (triggers->four_way_handshake &&
11829 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
11830 (triggers->rfkill_release &&
11831 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
11832 nlmsg_free(msg);
11833 return -ENOBUFS;
11834 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011835
11836 nla_nest_end(msg, wowlan_triggers);
11837
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011838 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011839 if (ret)
11840 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
11841
11842 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011843}
11844
11845
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011846#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011847static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
11848{
11849 struct i802_bss *bss = priv;
11850 struct wpa_driver_nl80211_data *drv = bss->drv;
11851 struct nl_msg *msg;
11852 struct nlattr *params;
11853
11854 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
11855
11856 if (!drv->roaming_vendor_cmd_avail) {
11857 wpa_printf(MSG_DEBUG,
11858 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
11859 return -1;
11860 }
11861
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011862 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11863 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11864 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11865 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
11866 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
11867 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
11868 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
11869 QCA_ROAMING_NOT_ALLOWED) ||
11870 (bssid &&
11871 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
11872 nlmsg_free(msg);
11873 return -1;
11874 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011875 nla_nest_end(msg, params);
11876
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011877 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011878}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011879
11880
Roshan Pius3a1667e2018-07-03 15:17:14 -070011881static int nl80211_disable_fils(void *priv, int disable)
11882{
11883 struct i802_bss *bss = priv;
11884 struct wpa_driver_nl80211_data *drv = bss->drv;
11885 struct nl_msg *msg;
11886 struct nlattr *params;
11887
11888 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
11889
11890 if (!drv->set_wifi_conf_vendor_cmd_avail)
11891 return -1;
11892
11893 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11894 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11895 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11896 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
11897 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
11898 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
11899 disable)) {
11900 nlmsg_free(msg);
11901 return -1;
11902 }
11903 nla_nest_end(msg, params);
11904
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011905 return send_and_recv_cmd(drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -070011906}
11907
11908
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011909/* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
11910#define WPA_SUPPLICANT_CLIENT_ID 1
11911
Hai Shalom899fcc72020-10-19 14:38:18 -070011912static int nl80211_set_bssid_tmp_disallow(void *priv, unsigned int num_bssid,
11913 const u8 *bssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011914{
11915 struct i802_bss *bss = priv;
11916 struct wpa_driver_nl80211_data *drv = bss->drv;
11917 struct nl_msg *msg;
11918 struct nlattr *params, *nlbssids, *attr;
11919 unsigned int i;
11920
Hai Shalom899fcc72020-10-19 14:38:18 -070011921 wpa_printf(MSG_DEBUG,
11922 "nl80211: Set temporarily disallowed BSSIDs (num=%u)",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011923 num_bssid);
11924
11925 if (!drv->roam_vendor_cmd_avail)
11926 return -1;
11927
11928 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11929 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11930 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11931 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
11932 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
11933 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
Hai Shalomc3565922019-10-28 11:58:20 -070011934 QCA_WLAN_VENDOR_ROAMING_SUBCMD_SET_BLACKLIST_BSSID) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011935 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
11936 WPA_SUPPLICANT_CLIENT_ID) ||
11937 nla_put_u32(msg,
11938 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
11939 num_bssid))
11940 goto fail;
11941
11942 nlbssids = nla_nest_start(
11943 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
11944 if (!nlbssids)
11945 goto fail;
11946
11947 for (i = 0; i < num_bssid; i++) {
11948 attr = nla_nest_start(msg, i);
11949 if (!attr)
11950 goto fail;
11951 if (nla_put(msg,
11952 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
11953 ETH_ALEN, &bssid[i * ETH_ALEN]))
11954 goto fail;
11955 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
11956 MAC2STR(&bssid[i * ETH_ALEN]));
11957 nla_nest_end(msg, attr);
11958 }
11959 nla_nest_end(msg, nlbssids);
11960 nla_nest_end(msg, params);
11961
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011962 return send_and_recv_cmd(drv, msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011963
11964fail:
11965 nlmsg_free(msg);
11966 return -1;
11967}
11968
Hai Shalomc3565922019-10-28 11:58:20 -070011969
11970static int nl80211_add_sta_node(void *priv, const u8 *addr, u16 auth_alg)
11971{
11972 struct i802_bss *bss = priv;
11973 struct wpa_driver_nl80211_data *drv = bss->drv;
11974 struct nl_msg *msg;
11975 struct nlattr *params;
11976
11977 if (!drv->add_sta_node_vendor_cmd_avail)
11978 return -EOPNOTSUPP;
11979
11980 wpa_printf(MSG_DEBUG, "nl80211: Add STA node");
11981
11982 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11983 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11984 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11985 QCA_NL80211_VENDOR_SUBCMD_ADD_STA_NODE) ||
11986 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
11987 (addr &&
11988 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ADD_STA_NODE_MAC_ADDR, ETH_ALEN,
11989 addr)) ||
11990 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ADD_STA_NODE_AUTH_ALGO,
11991 auth_alg)) {
11992 nlmsg_free(msg);
11993 wpa_printf(MSG_ERROR,
11994 "%s: err in adding vendor_cmd and vendor_data",
11995 __func__);
11996 return -1;
11997 }
11998 nla_nest_end(msg, params);
11999
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012000 return send_and_recv_cmd(drv, msg);
Hai Shalomc3565922019-10-28 11:58:20 -070012001}
12002
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012003#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012004
12005
12006static int nl80211_set_mac_addr(void *priv, const u8 *addr)
12007{
12008 struct i802_bss *bss = priv;
12009 struct wpa_driver_nl80211_data *drv = bss->drv;
12010 int new_addr = addr != NULL;
Andy Kuoaba17c12022-04-14 16:05:31 +080012011#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali8a8f1002020-10-06 22:41:40 +053012012 struct nl_msg *msg;
12013 struct nlattr *params;
12014 int ret;
Andy Kuoaba17c12022-04-14 16:05:31 +080012015#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Mir Ali8a8f1002020-10-06 22:41:40 +053012016 wpa_printf(MSG_DEBUG, "Enter: %s", __FUNCTION__);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012017
Dmitry Shmidt849734c2016-05-27 09:59:01 -070012018 if (TEST_FAIL())
12019 return -1;
Mir Ali8a8f1002020-10-06 22:41:40 +053012020 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
Andy Kuoaba17c12022-04-14 16:05:31 +080012021#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali8a8f1002020-10-06 22:41:40 +053012022 if (!addr ) {
12023 addr = drv->global->p2p_perm_addr;
12024 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -070012025
Mir Ali8a8f1002020-10-06 22:41:40 +053012026 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
12027 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
12028 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
Hai Shalomc1a21442022-02-04 13:43:00 -080012029 BRCM_VENDOR_SCMD_SET_MAC) ||
Mir Ali8a8f1002020-10-06 22:41:40 +053012030 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12031 nla_put(msg, BRCM_ATTR_DRIVER_MAC_ADDR, ETH_ALEN, addr)) {
12032 wpa_printf(MSG_ERROR, "failed to put p2p randmac");
12033 nl80211_nlmsg_clear(msg);
12034 nlmsg_free(msg);
12035 return -ENOBUFS;
12036 }
12037 nla_nest_end(msg, params);
12038
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012039 ret = send_and_recv_cmd(drv, msg);
Mir Ali8a8f1002020-10-06 22:41:40 +053012040 if (ret) {
12041 wpa_printf(MSG_ERROR, "nl80211: p2p set macaddr failed: ret=%d (%s)",
12042 ret, strerror(-ret));
12043 }
12044 memcpy(bss->addr, addr, ETH_ALEN);
12045 return ret;
12046#else
12047 return -ENOTSUP;
Andy Kuoaba17c12022-04-14 16:05:31 +080012048#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Mir Ali8a8f1002020-10-06 22:41:40 +053012049 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012050 if (!addr)
12051 addr = drv->perm_addr;
12052
12053 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
12054 return -1;
12055
12056 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
12057 {
12058 wpa_printf(MSG_DEBUG,
Mir Ali8a8f1002020-10-06 22:41:40 +053012059 "nl80211: failed to set_mac_addr for %s to " MACSTR,
12060 bss->ifname, MAC2STR(addr));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012061 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
Mir Ali8a8f1002020-10-06 22:41:40 +053012062 1) < 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012063 wpa_printf(MSG_DEBUG,
Mir Ali8a8f1002020-10-06 22:41:40 +053012064 "nl80211: Could not restore interface UP after failed set_mac_addr");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012065 }
12066 return -1;
12067 }
12068
12069 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000012070 bss->ifname, MAC2STR(addr));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012071 drv->addr_changed = new_addr;
Sunil Ravi77d572f2023-01-17 23:58:31 +000012072 os_memcpy(bss->prev_addr, bss->addr, ETH_ALEN);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012073 os_memcpy(bss->addr, addr, ETH_ALEN);
12074
Hsiu-Chang Chen289286d2024-01-24 18:00:43 +080012075 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
12076 {
12077 wpa_printf(MSG_DEBUG,
12078 "nl80211: Could not restore interface UP after set_mac_addr");
12079 }
12080
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012081 return 0;
12082}
12083
12084
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012085#ifdef CONFIG_MESH
12086
12087static int wpa_driver_nl80211_init_mesh(void *priv)
12088{
12089 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
12090 wpa_printf(MSG_INFO,
12091 "nl80211: Failed to set interface into mesh mode");
12092 return -1;
12093 }
12094 return 0;
12095}
12096
12097
Dmitry Shmidtff787d52015-01-12 13:01:47 -080012098static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
12099 size_t mesh_id_len)
12100{
12101 if (mesh_id) {
Hai Shalom74f70d42019-02-11 14:42:39 -080012102 wpa_printf(MSG_DEBUG, " * Mesh ID (SSID)=%s",
12103 wpa_ssid_txt(mesh_id, mesh_id_len));
Dmitry Shmidtff787d52015-01-12 13:01:47 -080012104 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
12105 }
12106
12107 return 0;
12108}
12109
12110
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012111static int nl80211_put_mesh_config(struct nl_msg *msg,
12112 struct wpa_driver_mesh_bss_params *params)
12113{
12114 struct nlattr *container;
12115
12116 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
12117 if (!container)
12118 return -1;
12119
12120 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070012121 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
12122 params->auto_plinks)) ||
Hai Shalomc1a21442022-02-04 13:43:00 -080012123 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_FORWARDING) &&
12124 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
12125 params->forwarding)) ||
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012126 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
12127 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070012128 params->max_peer_links)) ||
12129 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
12130 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
12131 params->rssi_threshold)))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012132 return -1;
12133
12134 /*
12135 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
12136 * the timer could disconnect stations even in that case.
12137 */
12138 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
12139 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
12140 params->peer_link_timeout)) {
12141 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
12142 return -1;
12143 }
12144
12145 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
12146 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
12147 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
12148 return -1;
12149 }
12150
12151 nla_nest_end(msg, container);
12152
12153 return 0;
12154}
12155
12156
Dmitry Shmidt7f656022015-02-25 14:36:37 -080012157static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012158 struct wpa_driver_mesh_join_params *params)
12159{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012160 struct wpa_driver_nl80211_data *drv = bss->drv;
12161 struct nl_msg *msg;
12162 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080012163 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012164
12165 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
12166 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -080012167 if (!msg ||
12168 nl80211_put_freq_params(msg, &params->freq) ||
12169 nl80211_put_basic_rates(msg, params->basic_rates) ||
12170 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070012171 nl80211_put_beacon_int(msg, params->beacon_int) ||
12172 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012173 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012174
12175 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
12176
Hai Shalom60840252021-02-19 19:02:11 -080012177 if (params->handle_dfs && nla_put_flag(msg, NL80211_ATTR_HANDLE_DFS))
12178 goto fail;
12179
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012180 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
12181 if (!container)
12182 goto fail;
12183
12184 if (params->ies) {
12185 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
12186 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
12187 params->ies))
12188 goto fail;
12189 }
12190 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
12191 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
12192 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
12193 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
12194 goto fail;
12195 }
12196 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
12197 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
12198 goto fail;
12199 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
12200 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
12201 goto fail;
12202 nla_nest_end(msg, container);
12203
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012204 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
12205 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
12206 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
12207 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012208 goto fail;
12209
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012210 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
12211 return -1;
12212 ret = send_and_recv(drv->global, bss->nl_connect, msg, NULL, NULL, NULL,
12213 NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012214 msg = NULL;
12215 if (ret) {
12216 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
12217 ret, strerror(-ret));
12218 goto fail;
12219 }
12220 ret = 0;
Sunil Ravi036cec52023-03-29 11:35:17 -070012221 drv->assoc_freq = bss->flink->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012222 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
12223
12224fail:
12225 nlmsg_free(msg);
12226 return ret;
12227}
12228
12229
Dmitry Shmidt7f656022015-02-25 14:36:37 -080012230static int
12231wpa_driver_nl80211_join_mesh(void *priv,
12232 struct wpa_driver_mesh_join_params *params)
12233{
12234 struct i802_bss *bss = priv;
12235 int ret, timeout;
12236
12237 timeout = params->conf.peer_link_timeout;
12238
12239 /* Disable kernel inactivity timer */
12240 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
12241 params->conf.peer_link_timeout = 0;
12242
12243 ret = nl80211_join_mesh(bss, params);
12244 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
12245 wpa_printf(MSG_DEBUG,
12246 "nl80211: Mesh join retry for peer_link_timeout");
12247 /*
12248 * Old kernel does not support setting
12249 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
12250 * into future from peer_link_timeout.
12251 */
12252 params->conf.peer_link_timeout = timeout + 60;
12253 ret = nl80211_join_mesh(priv, params);
12254 }
12255
12256 params->conf.peer_link_timeout = timeout;
12257 return ret;
12258}
12259
12260
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012261static int wpa_driver_nl80211_leave_mesh(void *priv)
12262{
12263 struct i802_bss *bss = priv;
12264 struct wpa_driver_nl80211_data *drv = bss->drv;
12265 struct nl_msg *msg;
12266 int ret;
12267
12268 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
12269 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012270 ret = send_and_recv(drv->global, bss->nl_connect, msg, NULL, NULL, NULL,
12271 NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012272 if (ret) {
12273 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
12274 ret, strerror(-ret));
12275 } else {
12276 wpa_printf(MSG_DEBUG,
12277 "nl80211: mesh leave request send successfully");
Sunil Ravi036cec52023-03-29 11:35:17 -070012278 drv->first_bss->flink->freq = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012279 }
12280
Hai Shalomc1a21442022-02-04 13:43:00 -080012281 if (drv->start_mode_sta &&
12282 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012283 NL80211_IFTYPE_STATION)) {
12284 wpa_printf(MSG_INFO,
12285 "nl80211: Failed to set interface into station mode");
12286 }
12287 return ret;
12288}
12289
Hai Shalom81f62d82019-07-22 12:10:00 -070012290
12291static int nl80211_probe_mesh_link(void *priv, const u8 *addr, const u8 *eth,
12292 size_t len)
12293{
12294 struct i802_bss *bss = priv;
12295 struct wpa_driver_nl80211_data *drv = bss->drv;
12296 struct nl_msg *msg;
12297 int ret;
12298
12299 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_PROBE_MESH_LINK);
12300 if (!msg ||
12301 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
12302 nla_put(msg, NL80211_ATTR_FRAME, len, eth)) {
12303 nlmsg_free(msg);
12304 return -ENOBUFS;
12305 }
12306
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012307 ret = send_and_recv_cmd(drv, msg);
Hai Shalom81f62d82019-07-22 12:10:00 -070012308 if (ret) {
12309 wpa_printf(MSG_DEBUG, "nl80211: mesh link probe to " MACSTR
12310 " failed: ret=%d (%s)",
12311 MAC2STR(addr), ret, strerror(-ret));
12312 } else {
12313 wpa_printf(MSG_DEBUG, "nl80211: Mesh link to " MACSTR
12314 " probed successfully", MAC2STR(addr));
12315 }
12316
12317 return ret;
12318}
12319
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012320#endif /* CONFIG_MESH */
12321
12322
12323static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
12324 const u8 *ipaddr, int prefixlen,
12325 const u8 *addr)
12326{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012327 struct i802_bss *bss = priv;
12328 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012329 struct ndmsg nhdr = {
12330 .ndm_state = NUD_PERMANENT,
12331 .ndm_ifindex = bss->br_ifindex,
12332 };
12333 struct nl_msg *msg;
12334 int addrsize;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012335 int res;
12336
12337 if (!ipaddr || prefixlen == 0 || !addr)
12338 return -EINVAL;
12339
12340 if (bss->br_ifindex == 0) {
12341 wpa_printf(MSG_DEBUG,
12342 "nl80211: bridge must be set before adding an ip neigh to it");
12343 return -1;
12344 }
12345
12346 if (!drv->rtnl_sk) {
12347 wpa_printf(MSG_DEBUG,
12348 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
12349 return -1;
12350 }
12351
12352 if (version == 4) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012353 nhdr.ndm_family = AF_INET;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012354 addrsize = 4;
12355 } else if (version == 6) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012356 nhdr.ndm_family = AF_INET6;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012357 addrsize = 16;
12358 } else {
12359 return -EINVAL;
12360 }
12361
Sunil Ravi99c035e2024-07-12 01:42:03 +000012362 msg = nlmsg_alloc_simple(RTM_NEWNEIGH, NLM_F_CREATE);
12363 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012364 return -ENOMEM;
12365
Sunil Ravi99c035e2024-07-12 01:42:03 +000012366 res = -ENOMEM;
12367 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0 ||
12368 nla_put(msg, NDA_DST, addrsize, (void *) ipaddr) ||
12369 nla_put(msg, NDA_LLADDR, ETH_ALEN, (void *) addr))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012370 goto errout;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012371
12372 res = nl_send_auto_complete(drv->rtnl_sk, msg);
12373 if (res < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012374 goto errout;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012375
Sunil Ravi99c035e2024-07-12 01:42:03 +000012376 res = nl_wait_for_ack(drv->rtnl_sk);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012377 if (res) {
12378 wpa_printf(MSG_DEBUG,
12379 "nl80211: Adding bridge ip neigh failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012380 nl_geterror(res));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012381 }
12382errout:
Sunil Ravi99c035e2024-07-12 01:42:03 +000012383 nlmsg_free(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012384 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012385}
12386
12387
12388static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
12389 const u8 *ipaddr)
12390{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012391 struct i802_bss *bss = priv;
12392 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012393 struct ndmsg nhdr = {
12394 .ndm_state = NUD_PERMANENT,
12395 .ndm_ifindex = bss->br_ifindex,
12396 };
12397 struct nl_msg *msg;
12398 int addrsize;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012399 int res;
12400
12401 if (!ipaddr)
12402 return -EINVAL;
12403
12404 if (version == 4) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012405 nhdr.ndm_family = AF_INET;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012406 addrsize = 4;
12407 } else if (version == 6) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012408 nhdr.ndm_family = AF_INET6;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012409 addrsize = 16;
12410 } else {
12411 return -EINVAL;
12412 }
12413
12414 if (bss->br_ifindex == 0) {
12415 wpa_printf(MSG_DEBUG,
12416 "nl80211: bridge must be set to delete an ip neigh");
12417 return -1;
12418 }
12419
12420 if (!drv->rtnl_sk) {
12421 wpa_printf(MSG_DEBUG,
12422 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
12423 return -1;
12424 }
12425
Sunil Ravi99c035e2024-07-12 01:42:03 +000012426 msg = nlmsg_alloc_simple(RTM_DELNEIGH, NLM_F_CREATE);
12427 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012428 return -ENOMEM;
12429
Sunil Ravi99c035e2024-07-12 01:42:03 +000012430 res = -ENOMEM;
12431 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0 ||
12432 nla_put(msg, NDA_DST, addrsize, (void *) ipaddr))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012433 goto errout;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012434
12435 res = nl_send_auto_complete(drv->rtnl_sk, msg);
12436 if (res < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012437 goto errout;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012438
Sunil Ravi99c035e2024-07-12 01:42:03 +000012439 res = nl_wait_for_ack(drv->rtnl_sk);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012440 if (res) {
12441 wpa_printf(MSG_DEBUG,
12442 "nl80211: Deleting bridge ip neigh failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012443 nl_geterror(res));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012444 }
12445errout:
Sunil Ravi99c035e2024-07-12 01:42:03 +000012446 nlmsg_free(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012447 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012448}
12449
12450
12451static int linux_write_system_file(const char *path, unsigned int val)
12452{
12453 char buf[50];
12454 int fd, len;
12455
12456 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
12457 if (os_snprintf_error(sizeof(buf), len))
12458 return -1;
12459
12460 fd = open(path, O_WRONLY);
12461 if (fd < 0)
12462 return -1;
12463
12464 if (write(fd, buf, len) < 0) {
12465 wpa_printf(MSG_DEBUG,
12466 "nl80211: Failed to write Linux system file: %s with the value of %d",
12467 path, val);
12468 close(fd);
12469 return -1;
12470 }
12471 close(fd);
12472
12473 return 0;
12474}
12475
12476
12477static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
12478{
12479 switch (attr) {
12480 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -070012481 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012482 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
12483 return "hairpin_mode";
Sunil Ravi036cec52023-03-29 11:35:17 -070012484 case DRV_BR_PORT_ATTR_MCAST2UCAST:
12485 return "multicast_to_unicast";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012486 }
12487
12488 return NULL;
12489}
12490
12491
12492static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
12493 unsigned int val)
12494{
12495 struct i802_bss *bss = priv;
12496 char path[128];
12497 const char *attr_txt;
12498
12499 attr_txt = drv_br_port_attr_str(attr);
12500 if (attr_txt == NULL)
12501 return -EINVAL;
12502
12503 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
12504 bss->ifname, attr_txt);
12505
12506 if (linux_write_system_file(path, val))
12507 return -1;
12508
12509 return 0;
12510}
12511
12512
12513static const char * drv_br_net_param_str(enum drv_br_net_param param)
12514{
12515 switch (param) {
12516 case DRV_BR_NET_PARAM_GARP_ACCEPT:
12517 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -070012518 default:
12519 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012520 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012521}
12522
12523
12524static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
12525 unsigned int val)
12526{
12527 struct i802_bss *bss = priv;
12528 char path[128];
12529 const char *param_txt;
12530 int ip_version = 4;
12531
Dmitry Shmidt83474442015-04-15 13:47:09 -070012532 if (param == DRV_BR_MULTICAST_SNOOPING) {
12533 os_snprintf(path, sizeof(path),
12534 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
12535 bss->brname);
12536 goto set_val;
12537 }
12538
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012539 param_txt = drv_br_net_param_str(param);
12540 if (param_txt == NULL)
12541 return -EINVAL;
12542
12543 switch (param) {
12544 case DRV_BR_NET_PARAM_GARP_ACCEPT:
12545 ip_version = 4;
12546 break;
12547 default:
12548 return -EINVAL;
12549 }
12550
12551 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
12552 ip_version, bss->brname, param_txt);
12553
Dmitry Shmidt83474442015-04-15 13:47:09 -070012554set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012555 if (linux_write_system_file(path, val))
12556 return -1;
12557
12558 return 0;
12559}
12560
12561
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012562#ifdef CONFIG_DRIVER_NL80211_QCA
12563
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012564static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
12565{
12566 switch (hw_mode) {
12567 case HOSTAPD_MODE_IEEE80211B:
12568 return QCA_ACS_MODE_IEEE80211B;
12569 case HOSTAPD_MODE_IEEE80211G:
12570 return QCA_ACS_MODE_IEEE80211G;
12571 case HOSTAPD_MODE_IEEE80211A:
12572 return QCA_ACS_MODE_IEEE80211A;
12573 case HOSTAPD_MODE_IEEE80211AD:
12574 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -070012575 case HOSTAPD_MODE_IEEE80211ANY:
12576 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012577 default:
12578 return -1;
12579 }
12580}
12581
12582
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080012583static int add_acs_ch_list(struct nl_msg *msg, const int *freq_list)
12584{
12585 int num_channels = 0, num_freqs;
12586 u8 *ch_list;
12587 enum hostapd_hw_mode hw_mode;
12588 int ret = 0;
12589 int i;
12590
12591 if (!freq_list)
12592 return 0;
12593
12594 num_freqs = int_array_len(freq_list);
12595 ch_list = os_malloc(sizeof(u8) * num_freqs);
12596 if (!ch_list)
12597 return -1;
12598
12599 for (i = 0; i < num_freqs; i++) {
12600 const int freq = freq_list[i];
12601
12602 if (freq == 0)
12603 break;
12604 /* Send 2.4 GHz and 5 GHz channels with
12605 * QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST to maintain backwards
12606 * compatibility.
12607 */
12608 if (!(freq >= 2412 && freq <= 2484) &&
Hai Shalomc1a21442022-02-04 13:43:00 -080012609 !(freq >= 5180 && freq <= 5900) &&
12610 !(freq >= 5945 && freq <= 7115))
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080012611 continue;
12612 hw_mode = ieee80211_freq_to_chan(freq, &ch_list[num_channels]);
12613 if (hw_mode != NUM_HOSTAPD_MODES)
12614 num_channels++;
12615 }
12616
12617 if (num_channels)
12618 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST,
12619 num_channels, ch_list);
12620
12621 os_free(ch_list);
12622 return ret;
12623}
12624
12625
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012626static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
12627{
12628 int i, len, ret;
12629 u32 *freqs;
12630
12631 if (!freq_list)
12632 return 0;
12633 len = int_array_len(freq_list);
12634 freqs = os_malloc(sizeof(u32) * len);
12635 if (!freqs)
12636 return -1;
12637 for (i = 0; i < len; i++)
12638 freqs[i] = freq_list[i];
12639 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
12640 sizeof(u32) * len, freqs);
12641 os_free(freqs);
12642 return ret;
12643}
12644
12645
Hai Shalomc1a21442022-02-04 13:43:00 -080012646static int nl80211_qca_do_acs(struct wpa_driver_nl80211_data *drv,
12647 struct drv_acs_params *params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012648{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012649 struct nl_msg *msg;
12650 struct nlattr *data;
12651 int ret;
12652 int mode;
12653
12654 mode = hw_mode_to_qca_acs(params->hw_mode);
12655 if (mode < 0)
12656 return -1;
12657
12658 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
12659 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12660 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12661 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
12662 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12663 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
12664 (params->ht_enabled &&
12665 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
12666 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012667 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
12668 (params->vht_enabled &&
12669 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
Sunil Ravia04bd252022-05-02 22:54:18 -070012670 (params->eht_enabled &&
12671 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_EHT_ENABLED)) ||
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012672 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
12673 params->ch_width) ||
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080012674 add_acs_ch_list(msg, params->freq_list) ||
Hai Shalomfdcde762020-04-02 11:19:20 -070012675 add_acs_freq_list(msg, params->freq_list) ||
12676 (params->edmg_enabled &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012677 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_EDMG_ENABLED)) ||
12678 (params->link_id != NL80211_DRV_LINK_ID_NA &&
12679 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_LINK_ID,
12680 params->link_id))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012681 nlmsg_free(msg);
12682 return -ENOBUFS;
12683 }
12684 nla_nest_end(msg, data);
12685
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012686 wpa_printf(MSG_DEBUG,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012687 "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 -070012688 params->hw_mode, params->ht_enabled, params->ht40_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -070012689 params->vht_enabled, params->eht_enabled, params->ch_width,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012690 params->edmg_enabled, params->link_id);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012691
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012692 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012693 if (ret) {
12694 wpa_printf(MSG_DEBUG,
12695 "nl80211: Failed to invoke driver ACS function: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012696 strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012697 }
12698 return ret;
12699}
12700
12701
Hai Shalom60840252021-02-19 19:02:11 -080012702static int nl80211_set_band(void *priv, u32 band_mask)
Ravi Joshie6ccb162015-07-16 17:45:41 -070012703{
12704 struct i802_bss *bss = priv;
12705 struct wpa_driver_nl80211_data *drv = bss->drv;
12706 struct nl_msg *msg;
12707 struct nlattr *data;
12708 int ret;
Hai Shalom60840252021-02-19 19:02:11 -080012709 enum qca_set_band qca_band_value;
12710 u32 qca_band_mask = QCA_SETBAND_AUTO;
Ravi Joshie6ccb162015-07-16 17:45:41 -070012711
Hai Shalom60840252021-02-19 19:02:11 -080012712 if (!drv->setband_vendor_cmd_avail ||
12713 (band_mask > (WPA_SETBAND_2G | WPA_SETBAND_5G | WPA_SETBAND_6G)))
Ravi Joshie6ccb162015-07-16 17:45:41 -070012714 return -1;
12715
Hai Shalom60840252021-02-19 19:02:11 -080012716 if (band_mask & WPA_SETBAND_5G)
12717 qca_band_mask |= QCA_SETBAND_5G;
12718 if (band_mask & WPA_SETBAND_2G)
12719 qca_band_mask |= QCA_SETBAND_2G;
12720 if (band_mask & WPA_SETBAND_6G)
12721 qca_band_mask |= QCA_SETBAND_6G;
12722
12723 /*
12724 * QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE is a legacy interface hence make
12725 * it suite to its values (AUTO/5G/2G) for backwards compatibility.
12726 */
12727 qca_band_value = ((qca_band_mask & QCA_SETBAND_5G) &&
12728 (qca_band_mask & QCA_SETBAND_2G)) ?
12729 QCA_SETBAND_AUTO :
12730 qca_band_mask & ~QCA_SETBAND_6G;
12731
12732 wpa_printf(MSG_DEBUG,
12733 "nl80211: QCA_BAND_MASK = 0x%x, QCA_BAND_VALUE = %d",
12734 qca_band_mask, qca_band_value);
Ravi Joshie6ccb162015-07-16 17:45:41 -070012735
12736 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
12737 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12738 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12739 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
12740 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
Hai Shalom60840252021-02-19 19:02:11 -080012741 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE,
12742 qca_band_value) ||
12743 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_MASK,
12744 qca_band_mask)) {
Ravi Joshie6ccb162015-07-16 17:45:41 -070012745 nlmsg_free(msg);
12746 return -ENOBUFS;
12747 }
12748 nla_nest_end(msg, data);
12749
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012750 ret = send_and_recv_cmd(drv, msg);
Ravi Joshie6ccb162015-07-16 17:45:41 -070012751 if (ret) {
12752 wpa_printf(MSG_DEBUG,
12753 "nl80211: Driver setband function failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012754 strerror(-ret));
Ravi Joshie6ccb162015-07-16 17:45:41 -070012755 }
12756 return ret;
12757}
12758
12759
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012760struct nl80211_pcl {
12761 unsigned int num;
Sunil8cd6f4d2022-06-28 18:40:46 +000012762 struct weighted_pcl *freq_list;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012763};
12764
Sunil8cd6f4d2022-06-28 18:40:46 +000012765static void get_pcl_attr_values(struct weighted_pcl *wpcl, struct nlattr *nl[])
12766{
12767 if (nl[QCA_WLAN_VENDOR_ATTR_PCL_FREQ])
12768 wpcl->freq = nla_get_u32(nl[QCA_WLAN_VENDOR_ATTR_PCL_FREQ]);
12769 if (nl[QCA_WLAN_VENDOR_ATTR_PCL_WEIGHT])
12770 wpcl->weight = nla_get_u8(nl[QCA_WLAN_VENDOR_ATTR_PCL_WEIGHT]);
12771 if (nl[QCA_WLAN_VENDOR_ATTR_PCL_FLAG]) {
12772 u32 flags = nla_get_u32(nl[QCA_WLAN_VENDOR_ATTR_PCL_FLAG]);
12773
12774 wpcl->flag = 0;
12775 if (flags & BIT(0))
12776 wpcl->flag |= WEIGHTED_PCL_GO;
12777 if (flags & BIT(1))
12778 wpcl->flag |= WEIGHTED_PCL_CLI;
12779 if (flags & BIT(2))
12780 wpcl->flag |= WEIGHTED_PCL_MUST_CONSIDER;
12781 if (flags & BIT(3))
12782 wpcl->flag |= WEIGHTED_PCL_EXCLUDE;
12783 } else {
12784 wpcl->flag = WEIGHTED_PCL_GO | WEIGHTED_PCL_CLI;
12785 }
12786}
12787
12788
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012789static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
12790{
12791 struct nlattr *tb[NL80211_ATTR_MAX + 1];
12792 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12793 struct nl80211_pcl *param = arg;
12794 struct nlattr *nl_vend, *attr;
12795 enum qca_iface_type iface_type;
12796 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
Sunil8cd6f4d2022-06-28 18:40:46 +000012797 struct nlattr *nl_pcl[QCA_WLAN_VENDOR_ATTR_PCL_MAX + 1];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012798 unsigned int num, max_num;
12799 u32 *freqs;
12800
12801 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
12802 genlmsg_attrlen(gnlh, 0), NULL);
12803
12804 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
12805 if (!nl_vend)
12806 return NL_SKIP;
12807
12808 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
12809 nla_data(nl_vend), nla_len(nl_vend), NULL);
12810
12811 attr = tb_vendor[
12812 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
12813 if (!attr) {
12814 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
12815 param->num = 0;
12816 return NL_SKIP;
12817 }
12818
12819 iface_type = (enum qca_iface_type) nla_get_u32(attr);
12820 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
12821 iface_type);
12822
Sunil8cd6f4d2022-06-28 18:40:46 +000012823 attr = tb_vendor[
12824 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_WEIGHED_PCL];
12825 if (attr) {
12826 int rem;
12827 struct nlattr *wpcl = attr;
12828 unsigned int i;
12829
12830 num = 0;
12831 nla_for_each_nested(attr, wpcl, rem) {
12832 if (num == param->num)
12833 break; /* not enough room for all entries */
12834 if (nla_parse(nl_pcl, QCA_WLAN_VENDOR_ATTR_PCL_MAX,
12835 nla_data(attr), nla_len(attr), NULL)) {
12836 wpa_printf(MSG_ERROR,
12837 "nl80211: Failed to parse PCL info");
12838 param->num = 0;
12839 return NL_SKIP;
12840 }
12841 get_pcl_attr_values(&param->freq_list[num], nl_pcl);
12842 num++;
12843 }
12844 param->num = num;
12845
12846 /* Sort frequencies based on their weight */
12847 for (i = 0; i < num; i++) {
12848 unsigned int j;
12849
12850 for (j = i + 1; j < num; j++) {
12851 if (param->freq_list[i].weight <
12852 param->freq_list[j].weight) {
12853 struct weighted_pcl tmp;
12854
12855 tmp = param->freq_list[i];
12856 param->freq_list[i] =
12857 param->freq_list[j];
12858 param->freq_list[j] = tmp;
12859 }
12860 }
12861 }
12862 } else if (tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST]) {
12863 wpa_printf(MSG_DEBUG,
12864 "nl80211: Driver does not provide weighted PCL; use the non-weighted variant");
12865 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
12866 /*
12867 * param->num has the maximum number of entries for which there
12868 * is room in the freq_list provided by the caller.
12869 */
12870 freqs = nla_data(attr);
12871 max_num = nla_len(attr) / sizeof(u32);
12872 if (max_num > param->num)
12873 max_num = param->num;
12874 for (num = 0; num < max_num; num++) {
12875 param->freq_list[num].freq = freqs[num];
12876 param->freq_list[num].flag =
12877 WEIGHTED_PCL_GO | WEIGHTED_PCL_CLI;
12878 }
12879 param->num = num;
12880 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012881 wpa_printf(MSG_ERROR,
12882 "nl80211: preferred_freq_list couldn't be found");
12883 param->num = 0;
12884 return NL_SKIP;
12885 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012886 return NL_SKIP;
12887}
12888
12889
12890static int nl80211_get_pref_freq_list(void *priv,
12891 enum wpa_driver_if_type if_type,
12892 unsigned int *num,
Sunil8cd6f4d2022-06-28 18:40:46 +000012893 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012894{
12895 struct i802_bss *bss = priv;
12896 struct wpa_driver_nl80211_data *drv = bss->drv;
12897 struct nl_msg *msg;
12898 int ret;
12899 unsigned int i;
12900 struct nlattr *params;
12901 struct nl80211_pcl param;
12902 enum qca_iface_type iface_type;
12903
12904 if (!drv->get_pref_freq_list)
12905 return -1;
12906
12907 switch (if_type) {
12908 case WPA_IF_STATION:
12909 iface_type = QCA_IFACE_TYPE_STA;
12910 break;
12911 case WPA_IF_AP_BSS:
12912 iface_type = QCA_IFACE_TYPE_AP;
12913 break;
12914 case WPA_IF_P2P_GO:
12915 iface_type = QCA_IFACE_TYPE_P2P_GO;
12916 break;
12917 case WPA_IF_P2P_CLIENT:
12918 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
12919 break;
12920 case WPA_IF_IBSS:
12921 iface_type = QCA_IFACE_TYPE_IBSS;
12922 break;
12923 case WPA_IF_TDLS:
12924 iface_type = QCA_IFACE_TYPE_TDLS;
12925 break;
12926 default:
12927 return -1;
12928 }
12929
12930 param.num = *num;
12931 param.freq_list = freq_list;
12932
12933 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
12934 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
12935 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12936 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12937 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
12938 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12939 nla_put_u32(msg,
12940 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
12941 iface_type)) {
12942 wpa_printf(MSG_ERROR,
12943 "%s: err in adding vendor_cmd and vendor_data",
12944 __func__);
12945 nlmsg_free(msg);
12946 return -1;
12947 }
12948 nla_nest_end(msg, params);
12949
Sunil8cd6f4d2022-06-28 18:40:46 +000012950 if (freq_list)
12951 os_memset(freq_list, 0, *num * sizeof(struct weighted_pcl));
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012952 ret = send_and_recv_resp(drv, msg, preferred_freq_info_handler, &param);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012953 if (ret) {
12954 wpa_printf(MSG_ERROR,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012955 "%s: err in send_and_recv_resp", __func__);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012956 return ret;
12957 }
12958
12959 *num = param.num;
12960
12961 for (i = 0; i < *num; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +000012962 wpa_printf(MSG_DEBUG,
12963 "nl80211: preferred_channel_list[%d]=%d[%d]:0x%x",
12964 i, freq_list[i].freq, freq_list[i].weight,
12965 freq_list[i].flag);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012966 }
12967
12968 return 0;
12969}
12970
12971
12972static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
12973{
12974 struct i802_bss *bss = priv;
12975 struct wpa_driver_nl80211_data *drv = bss->drv;
12976 struct nl_msg *msg;
12977 int ret;
12978 struct nlattr *params;
12979
12980 if (!drv->set_prob_oper_freq)
12981 return -1;
12982
12983 wpa_printf(MSG_DEBUG,
12984 "nl80211: Set P2P probable operating freq %u for ifindex %d",
12985 freq, bss->ifindex);
12986
12987 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
12988 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12989 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12990 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
12991 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12992 nla_put_u32(msg,
12993 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
12994 QCA_IFACE_TYPE_P2P_CLIENT) ||
12995 nla_put_u32(msg,
12996 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
12997 freq)) {
12998 wpa_printf(MSG_ERROR,
12999 "%s: err in adding vendor_cmd and vendor_data",
13000 __func__);
13001 nlmsg_free(msg);
13002 return -1;
13003 }
13004 nla_nest_end(msg, params);
13005
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013006 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013007 msg = NULL;
13008 if (ret) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013009 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_cmd",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013010 __func__);
13011 return ret;
13012 }
13013 nlmsg_free(msg);
13014 return 0;
13015}
13016
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070013017
13018static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
13019 unsigned int period, unsigned int interval,
13020 unsigned int count, const u8 *device_types,
13021 size_t dev_types_len,
13022 const u8 *ies, size_t ies_len)
13023{
13024 struct i802_bss *bss = priv;
13025 struct wpa_driver_nl80211_data *drv = bss->drv;
13026 struct nl_msg *msg;
13027 struct nlattr *container;
13028 int ret;
13029
13030 wpa_printf(MSG_DEBUG,
13031 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
13032 freq, period, interval, count);
13033
13034 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
13035 return -1;
13036
13037 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
13038 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13039 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13040 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
13041 goto fail;
13042
13043 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13044 if (!container)
13045 goto fail;
13046
13047 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
13048 freq) ||
13049 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
13050 period) ||
13051 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
13052 interval) ||
13053 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
13054 count) ||
13055 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
13056 dev_types_len, device_types) ||
13057 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
13058 ies_len, ies))
13059 goto fail;
13060
13061 nla_nest_end(msg, container);
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013062 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070013063 msg = NULL;
13064 if (ret) {
13065 wpa_printf(MSG_DEBUG,
13066 "nl80211: Failed to send P2P Listen offload vendor command");
13067 goto fail;
13068 }
13069
13070 return 0;
13071
13072fail:
13073 nlmsg_free(msg);
13074 return -1;
13075}
13076
13077
13078static int nl80211_p2p_lo_stop(void *priv)
13079{
13080 struct i802_bss *bss = priv;
13081 struct wpa_driver_nl80211_data *drv = bss->drv;
13082 struct nl_msg *msg;
13083
13084 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
13085
13086 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
13087 return -1;
13088
13089 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
13090 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13091 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13092 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
13093 nlmsg_free(msg);
13094 return -1;
13095 }
13096
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013097 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070013098}
13099
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013100
13101static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
13102{
13103 struct i802_bss *bss = priv;
13104 struct wpa_driver_nl80211_data *drv = bss->drv;
13105 struct nl_msg *msg;
13106 struct nlattr *params;
13107 int ret;
13108 u32 tdls_mode;
13109
13110 wpa_printf(MSG_DEBUG,
13111 "nl80211: Set TDKS mode: tdls_external_control=%d",
13112 tdls_external_control);
13113
13114 if (tdls_external_control == 1)
13115 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
13116 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
13117 else
13118 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
13119
13120 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
13121 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13122 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13123 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
13124 goto fail;
13125
13126 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13127 if (!params)
13128 goto fail;
13129
13130 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
13131 tdls_mode))
13132 goto fail;
13133
13134 nla_nest_end(msg, params);
13135
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013136 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013137 msg = NULL;
13138 if (ret) {
13139 wpa_printf(MSG_ERROR,
13140 "nl80211: Set TDLS mode failed: ret=%d (%s)",
13141 ret, strerror(-ret));
13142 goto fail;
13143 }
13144 return 0;
13145fail:
13146 nlmsg_free(msg);
13147 return -1;
13148}
13149
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013150
13151#ifdef CONFIG_MBO
13152
13153static enum mbo_transition_reject_reason
13154nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
13155{
13156 switch (status) {
13157 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
13158 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
13159 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
13160 return MBO_TRANSITION_REJECT_REASON_DELAY;
13161 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
13162 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
13163 case QCA_STATUS_REJECT_LOW_RSSI:
13164 return MBO_TRANSITION_REJECT_REASON_RSSI;
13165 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
13166 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
13167 case QCA_STATUS_REJECT_UNKNOWN:
13168 default:
13169 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
13170 }
13171}
13172
13173
13174static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
13175 struct nlattr *tb[], int num)
13176{
13177 enum qca_wlan_btm_candidate_status status;
13178 char buf[50];
13179
13180 os_memcpy(candidate->bssid,
13181 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
13182 ETH_ALEN);
13183
13184 status = nla_get_u32(
13185 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
13186 candidate->is_accept = status == QCA_STATUS_ACCEPT;
13187 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
13188
13189 if (candidate->is_accept)
13190 os_snprintf(buf, sizeof(buf), "Accepted");
13191 else
13192 os_snprintf(buf, sizeof(buf),
13193 "Rejected, Reject_reason: %d",
13194 candidate->reject_reason);
13195 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
13196 num, MAC2STR(candidate->bssid), buf);
13197}
13198
13199
13200static int
13201nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
13202{
13203 struct wpa_bss_candidate_info *info = arg;
13204 struct candidate_list *candidate = info->candidates;
13205 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
13206 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
13207 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
13208 static struct nla_policy policy[
13209 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
13210 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
13211 .minlen = ETH_ALEN
13212 },
13213 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
13214 .type = NLA_U32,
13215 },
13216 };
13217 struct nlattr *attr;
13218 int rem;
13219 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
13220 u8 num;
13221
13222 num = info->num; /* number of candidates sent to driver */
13223 info->num = 0;
13224 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
13225 genlmsg_attrlen(gnlh, 0), NULL);
13226
13227 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
13228 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
13229 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
13230 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
13231 return NL_SKIP;
13232
13233 wpa_printf(MSG_DEBUG,
13234 "nl80211: WNM Candidate list received from driver");
13235 nla_for_each_nested(attr,
13236 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
13237 rem) {
13238 if (info->num >= num ||
13239 nla_parse_nested(
13240 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
13241 attr, policy) ||
13242 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
13243 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
13244 break;
13245
13246 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
13247
13248 candidate++;
13249 info->num++;
13250 }
13251
13252 return NL_SKIP;
13253}
13254
13255
13256static struct wpa_bss_candidate_info *
13257nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
13258{
13259 struct i802_bss *bss = priv;
13260 struct wpa_driver_nl80211_data *drv = bss->drv;
13261 struct nl_msg *msg;
13262 struct nlattr *attr, *attr1, *attr2;
13263 struct wpa_bss_candidate_info *info;
13264 u8 i;
13265 int ret;
13266 u8 *pos;
13267
13268 if (!drv->fetch_bss_trans_status)
13269 return NULL;
13270
13271 info = os_zalloc(sizeof(*info));
13272 if (!info)
13273 return NULL;
13274 /* Allocate memory for number of candidates sent to driver */
13275 info->candidates = os_calloc(params->n_candidates,
13276 sizeof(*info->candidates));
13277 if (!info->candidates) {
13278 os_free(info);
13279 return NULL;
13280 }
13281
13282 /* Copy the number of candidates being sent to driver. This is used in
13283 * nl80211_get_bss_transition_status_handler() to limit the number of
13284 * candidates that can be populated in info->candidates and will be
13285 * later overwritten with the actual number of candidates received from
13286 * the driver.
13287 */
13288 info->num = params->n_candidates;
13289
13290 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
13291 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13292 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13293 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
13294 goto fail;
13295
13296 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13297 if (!attr)
13298 goto fail;
13299
13300 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
13301 params->mbo_transition_reason))
13302 goto fail;
13303
13304 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
13305 if (!attr1)
13306 goto fail;
13307
13308 wpa_printf(MSG_DEBUG,
13309 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
13310 params->mbo_transition_reason, params->n_candidates);
13311 pos = params->bssid;
13312 for (i = 0; i < params->n_candidates; i++) {
13313 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
13314 MAC2STR(pos));
13315 attr2 = nla_nest_start(msg, i);
13316 if (!attr2 ||
13317 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
13318 ETH_ALEN, pos))
13319 goto fail;
13320 pos += ETH_ALEN;
13321 nla_nest_end(msg, attr2);
13322 }
13323
13324 nla_nest_end(msg, attr1);
13325 nla_nest_end(msg, attr);
13326
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013327 ret = send_and_recv_resp(drv, msg,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013328 nl80211_get_bss_transition_status_handler,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013329 info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013330 msg = NULL;
13331 if (ret) {
13332 wpa_printf(MSG_ERROR,
13333 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
13334 ret, strerror(-ret));
13335 goto fail;
13336 }
13337 return info;
13338
13339fail:
13340 nlmsg_free(msg);
13341 os_free(info->candidates);
13342 os_free(info);
13343 return NULL;
13344}
13345
13346
13347/**
13348 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
13349 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
13350 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
13351 * Returns: 0 on success, -1 on failure
13352 */
13353static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
13354{
13355 struct i802_bss *bss = priv;
13356 struct wpa_driver_nl80211_data *drv = bss->drv;
13357 struct nl_msg *msg;
13358 struct nlattr *attr;
13359 int ret = -1;
13360
13361 if (!drv->set_wifi_conf_vendor_cmd_avail)
13362 return -1;
13363
13364 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
13365 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13366 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13367 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
13368 goto fail;
13369
13370 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13371 if (!attr)
13372 goto fail;
13373
13374 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
13375 ignore_disallow);
13376 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
13377 ignore_disallow))
13378 goto fail;
13379
13380 nla_nest_end(msg, attr);
13381
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013382 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013383 msg = NULL;
13384 if (ret) {
13385 wpa_printf(MSG_ERROR,
13386 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
13387 ret, strerror(-ret));
13388 goto fail;
13389 }
13390
13391fail:
13392 nlmsg_free(msg);
13393 return ret;
13394}
13395
13396#endif /* CONFIG_MBO */
13397
Sunil Ravi89eba102022-09-13 21:04:37 -070013398
13399#ifdef CONFIG_PASN
13400
13401static int nl80211_send_pasn_resp(void *priv, struct pasn_auth *params)
13402{
13403 unsigned int i;
13404 struct i802_bss *bss = priv;
13405 struct nl_msg *msg = NULL;
13406 struct nlattr *nlpeers, *attr, *attr1;
13407 struct wpa_driver_nl80211_data *drv = bss->drv;
13408
13409 wpa_dbg(drv->ctx, MSG_DEBUG,
13410 "nl80211: PASN authentication response for %d entries",
13411 params->num_peers);
13412 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR);
13413 if (!msg ||
13414 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13415 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13416 QCA_NL80211_VENDOR_SUBCMD_PASN))
13417 goto fail;
13418
13419 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13420 if (!attr)
13421 goto fail;
13422
13423 nlpeers = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_PASN_PEERS);
13424 if (!nlpeers)
13425 goto fail;
13426
13427 for (i = 0; i < params->num_peers; i++) {
13428 attr1 = nla_nest_start(msg, i);
13429 if (!attr1 ||
13430 nla_put(msg, QCA_WLAN_VENDOR_ATTR_PASN_PEER_SRC_ADDR,
13431 ETH_ALEN, params->peer[i].own_addr) ||
13432 nla_put(msg, QCA_WLAN_VENDOR_ATTR_PASN_PEER_MAC_ADDR,
13433 ETH_ALEN, params->peer[i].peer_addr))
13434 goto fail;
13435
Sunil Ravi77d572f2023-01-17 23:58:31 +000013436 if (params->peer[i].status == 0 &&
13437 nla_put_flag(msg,
13438 QCA_WLAN_VENDOR_ATTR_PASN_PEER_STATUS_SUCCESS))
13439 goto fail;
Sunil Ravi89eba102022-09-13 21:04:37 -070013440
13441 wpa_printf(MSG_DEBUG,
13442 "nl80211: Own address[%u]: " MACSTR
13443 " Peer address[%u]: " MACSTR " Status: %s",
13444 i, MAC2STR(params->peer[i].own_addr), i,
13445 MAC2STR(params->peer[i].peer_addr),
13446 params->peer[i].status ? "Fail" : "Success");
13447 nla_nest_end(msg, attr1);
13448 }
13449
13450 nla_nest_end(msg, nlpeers);
13451 nla_nest_end(msg, attr);
13452
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013453 return send_and_recv_cmd(drv, msg);
Sunil Ravi89eba102022-09-13 21:04:37 -070013454
13455fail:
13456 nlmsg_free(msg);
13457 return -1;
13458}
13459
13460
13461static u32 wpa_ltf_keyseed_len_to_sha_type(size_t len)
13462{
13463 if (len == SHA384_MAC_LEN)
13464 return QCA_WLAN_VENDOR_SHA_384;
13465 if (len == SHA256_MAC_LEN)
13466 return QCA_WLAN_VENDOR_SHA_256;
13467
13468 wpa_printf(MSG_ERROR, "nl80211: Unexpected LTF keyseed len %zu", len);
13469 return (u32) -1;
13470}
13471
13472
13473static int nl80211_set_secure_ranging_ctx(void *priv,
13474 struct secure_ranging_params *params)
13475{
13476 int ret;
13477 u32 suite;
13478 struct nlattr *attr;
13479 struct nl_msg *msg = NULL;
13480 struct i802_bss *bss = priv;
13481 struct wpa_driver_nl80211_data *drv = bss->drv;
13482
13483 /* Configure secure ranging context only to the drivers that support it.
13484 */
13485 if (!drv->secure_ranging_ctx_vendor_cmd_avail)
13486 return 0;
13487
13488 if (!params->peer_addr || !params->own_addr)
13489 return -1;
13490
13491 wpa_dbg(drv->ctx, MSG_DEBUG,
13492 "nl80211: Secure ranging context for " MACSTR,
13493 MAC2STR(params->peer_addr));
13494
13495 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR);
13496 if (!msg ||
13497 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13498 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13499 QCA_NL80211_VENDOR_SUBCMD_SECURE_RANGING_CONTEXT))
13500 goto fail;
13501
13502 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13503 if (!attr)
13504 goto fail;
13505
13506 if (nla_put(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_PEER_MAC_ADDR,
13507 ETH_ALEN, params->peer_addr) ||
13508 nla_put(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_SRC_ADDR,
13509 ETH_ALEN, params->own_addr) ||
13510 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_ACTION,
13511 params->action))
13512 goto fail;
13513
13514 if (params->cipher) {
13515 suite = wpa_cipher_to_cipher_suite(params->cipher);
13516 if (!suite ||
13517 nla_put_u32(msg,
13518 QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_CIPHER,
13519 suite))
13520 goto fail;
13521 }
13522
13523 if (params->tk_len && params->tk) {
13524 if (nla_put(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_TK,
13525 params->tk_len, params->tk))
13526 goto fail;
13527 wpa_hexdump_key(MSG_DEBUG, "nl80211: TK",
13528 params->tk, params->tk_len);
13529 }
13530
13531 if (params->ltf_keyseed_len && params->ltf_keyseed) {
13532 u32 sha_type = wpa_ltf_keyseed_len_to_sha_type(
13533 params->ltf_keyseed_len);
13534
13535 if (sha_type == (u32) -1 ||
13536 nla_put_u32(
13537 msg,
13538 QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_SHA_TYPE,
13539 sha_type) ||
13540 nla_put(msg,
13541 QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_LTF_KEYSEED,
13542 params->ltf_keyseed_len, params->ltf_keyseed))
13543 goto fail;
13544 wpa_hexdump_key(MSG_DEBUG, "nl80211: LTF keyseed",
13545 params->ltf_keyseed, params->ltf_keyseed_len);
13546 }
13547 nla_nest_end(msg, attr);
13548
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013549 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi89eba102022-09-13 21:04:37 -070013550 if (ret)
13551 wpa_printf(MSG_DEBUG,
13552 "nl80211: Set secure ranging context failed: ret=%d (%s)",
13553 ret, strerror(-ret));
13554 return ret;
13555fail:
13556 nlmsg_free(msg);
13557 return -1;
13558}
13559
13560#endif /* CONFIG_PASN */
13561
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013562#endif /* CONFIG_DRIVER_NL80211_QCA */
13563
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013564
13565#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
13566static int wpa_driver_do_broadcom_acs(struct wpa_driver_nl80211_data *drv,
13567 struct drv_acs_params *params)
13568{
13569 struct nl_msg *msg;
13570 struct nlattr *data;
13571 int freq_list_len;
13572 int ret = -1;
13573
13574 freq_list_len = int_array_len(params->freq_list);
13575 wpa_printf(MSG_DEBUG, "%s: freq_list_len=%d",
13576 __func__, freq_list_len);
13577
13578 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR);
13579 if (!msg ||
13580 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
13581 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13582 BRCM_VENDOR_SCMD_ACS) ||
13583 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
13584 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_HW_MODE, params->hw_mode) ||
13585 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_HT_ENABLED,
13586 params->ht_enabled) ||
13587 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_HT40_ENABLED,
13588 params->ht40_enabled) ||
13589 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_VHT_ENABLED,
13590 params->vht_enabled) ||
13591 nla_put_u16(msg, BRCM_VENDOR_ATTR_ACS_CHWIDTH, params->ch_width) ||
13592 (freq_list_len > 0 &&
13593 nla_put(msg, BRCM_VENDOR_ATTR_ACS_FREQ_LIST,
13594 sizeof(int) * freq_list_len, params->freq_list)))
13595 goto fail;
13596 nla_nest_end(msg, data);
13597
13598 wpa_printf(MSG_DEBUG,
13599 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d",
13600 params->hw_mode, params->ht_enabled, params->ht40_enabled,
13601 params->vht_enabled, params->ch_width);
13602
13603 ret = send_and_recv_cmd(drv, msg);
13604 if (ret) {
13605 wpa_printf(MSG_ERROR,
13606 "nl80211: BRCM Failed to invoke driver ACS function: %s",
13607 strerror(errno));
13608 }
13609
13610 msg = NULL;
13611fail:
13612 nlmsg_free(msg);
13613 return ret;
13614}
13615#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
13616
13617
Hai Shalomc1a21442022-02-04 13:43:00 -080013618static int nl80211_do_acs(void *priv, struct drv_acs_params *params)
13619{
Andy Kuoaba17c12022-04-14 16:05:31 +080013620#if defined(CONFIG_DRIVER_NL80211_QCA) || defined(CONFIG_DRIVER_NL80211_BRCM) \
13621 || defined(CONFIG_DRIVER_NL80211_SYNA)
Hai Shalomc1a21442022-02-04 13:43:00 -080013622 struct i802_bss *bss = priv;
13623 struct wpa_driver_nl80211_data *drv = bss->drv;
Andy Kuoaba17c12022-04-14 16:05:31 +080013624#endif /* CONFIG_DRIVER_NL80211_QCA || CONFIG_DRIVER_NL80211_BRCM \
13625 || defined(CONFIG_DRIVER_NL80211_SYNA) */
Hai Shalomc1a21442022-02-04 13:43:00 -080013626
13627#ifdef CONFIG_DRIVER_NL80211_QCA
13628 if (drv->qca_do_acs)
13629 return nl80211_qca_do_acs(drv, params);
13630#endif /* CONFIG_DRIVER_NL80211_QCA */
13631
Andy Kuoaba17c12022-04-14 16:05:31 +080013632#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Hai Shalomc1a21442022-02-04 13:43:00 -080013633 if (drv->brcm_do_acs)
13634 return wpa_driver_do_broadcom_acs(drv, params);
Andy Kuoaba17c12022-04-14 16:05:31 +080013635#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Hai Shalomc1a21442022-02-04 13:43:00 -080013636
13637 return -1;
13638}
13639
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013640
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013641static int nl80211_write_to_file(const char *name, unsigned int val)
13642{
13643 int fd, len;
13644 char tmp[128];
Hai Shalomc3565922019-10-28 11:58:20 -070013645 int ret = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013646
13647 fd = open(name, O_RDWR);
13648 if (fd < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -070013649 int level;
13650 /*
13651 * Flags may not exist on older kernels, or while we're tearing
13652 * down a disappearing device.
13653 */
13654 if (errno == ENOENT) {
13655 ret = 0;
13656 level = MSG_DEBUG;
13657 } else {
13658 ret = -1;
13659 level = MSG_ERROR;
13660 }
13661 wpa_printf(level, "nl80211: Failed to open %s: %s",
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013662 name, strerror(errno));
Hai Shalomc3565922019-10-28 11:58:20 -070013663 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013664 }
13665
13666 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
13667 len = write(fd, tmp, len);
Hai Shalomc3565922019-10-28 11:58:20 -070013668 if (len < 0) {
13669 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013670 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
13671 name, strerror(errno));
Hai Shalomc3565922019-10-28 11:58:20 -070013672 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013673 close(fd);
13674
Hai Shalomc3565922019-10-28 11:58:20 -070013675 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013676}
13677
13678
13679static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
13680{
13681 struct i802_bss *bss = priv;
13682 char path[128];
13683 int ret;
13684
Hai Shalom60840252021-02-19 19:02:11 -080013685 /* P2P-Device has no netdev that can (or should) be configured here */
13686 if (nl80211_get_ifmode(bss) == NL80211_IFTYPE_P2P_DEVICE)
13687 return 0;
13688
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013689 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
13690 filter_flags);
13691
13692 /* Configure filtering of unicast frame encrypted using GTK */
13693 ret = os_snprintf(path, sizeof(path),
13694 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
13695 bss->ifname);
13696 if (os_snprintf_error(sizeof(path), ret))
13697 return -1;
13698
13699 ret = nl80211_write_to_file(path,
13700 !!(filter_flags &
13701 WPA_DATA_FRAME_FILTER_FLAG_GTK));
13702 if (ret) {
13703 wpa_printf(MSG_ERROR,
13704 "nl80211: Failed to set IPv4 unicast in multicast filter");
13705 return ret;
13706 }
13707
13708 os_snprintf(path, sizeof(path),
13709 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
13710 bss->ifname);
13711 ret = nl80211_write_to_file(path,
13712 !!(filter_flags &
13713 WPA_DATA_FRAME_FILTER_FLAG_GTK));
13714
13715 if (ret) {
13716 wpa_printf(MSG_ERROR,
13717 "nl80211: Failed to set IPv6 unicast in multicast filter");
13718 return ret;
13719 }
13720
13721 /* Configure filtering of unicast frame encrypted using GTK */
13722 os_snprintf(path, sizeof(path),
13723 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
13724 bss->ifname);
13725 ret = nl80211_write_to_file(path,
13726 !!(filter_flags &
13727 WPA_DATA_FRAME_FILTER_FLAG_ARP));
13728 if (ret) {
13729 wpa_printf(MSG_ERROR,
13730 "nl80211: Failed set gratuitous ARP filter");
13731 return ret;
13732 }
13733
13734 /* Configure filtering of IPv6 NA frames */
13735 os_snprintf(path, sizeof(path),
13736 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
13737 bss->ifname);
13738 ret = nl80211_write_to_file(path,
13739 !!(filter_flags &
13740 WPA_DATA_FRAME_FILTER_FLAG_NA));
13741 if (ret) {
13742 wpa_printf(MSG_ERROR,
13743 "nl80211: Failed to set unsolicited NA filter");
13744 return ret;
13745 }
13746
13747 return 0;
13748}
13749
13750
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070013751static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
13752 const u8 **ext_capa, const u8 **ext_capa_mask,
13753 unsigned int *ext_capa_len)
13754{
13755 struct i802_bss *bss = priv;
13756 struct wpa_driver_nl80211_data *drv = bss->drv;
13757 enum nl80211_iftype nlmode;
13758 unsigned int i;
13759
13760 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
13761 return -1;
13762
13763 nlmode = wpa_driver_nl80211_if_type(type);
13764
13765 /* By default, use the per-radio values */
13766 *ext_capa = drv->extended_capa;
13767 *ext_capa_mask = drv->extended_capa_mask;
13768 *ext_capa_len = drv->extended_capa_len;
13769
13770 /* Replace the default value if a per-interface type value exists */
Sunil Ravi2a14cf12023-11-21 00:54:38 +000013771 for (i = 0; i < drv->num_iface_capa; i++) {
13772 if (nlmode == drv->iface_capa[i].iftype) {
13773 *ext_capa = drv->iface_capa[i].ext_capa;
13774 *ext_capa_mask = drv->iface_capa[i].ext_capa_mask;
13775 *ext_capa_len = drv->iface_capa[i].ext_capa_len;
13776 break;
13777 }
13778 }
13779
13780 return 0;
13781}
13782
13783
13784static int nl80211_get_mld_capab(void *priv, enum wpa_driver_if_type type,
13785 u16 *eml_capa, u16 *mld_capa_and_ops)
13786{
13787 struct i802_bss *bss = priv;
13788 struct wpa_driver_nl80211_data *drv = bss->drv;
13789 enum nl80211_iftype nlmode;
13790 unsigned int i;
13791
13792 if (!eml_capa || !mld_capa_and_ops)
13793 return -1;
13794
13795 nlmode = wpa_driver_nl80211_if_type(type);
13796
13797 /* By default, set to zero */
13798 *eml_capa = 0;
13799 *mld_capa_and_ops = 0;
13800
13801 /* Replace the default value if a per-interface type value exists */
13802 for (i = 0; i < drv->num_iface_capa; i++) {
13803 if (nlmode == drv->iface_capa[i].iftype) {
13804 *eml_capa = drv->iface_capa[i].eml_capa;
13805 *mld_capa_and_ops =
13806 drv->iface_capa[i].mld_capa_and_ops;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070013807 break;
13808 }
13809 }
13810
13811 return 0;
13812}
13813
13814
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013815static int nl80211_update_connection_params(
13816 void *priv, struct wpa_driver_associate_params *params,
13817 enum wpa_drv_update_connect_params_mask mask)
13818{
13819 struct i802_bss *bss = priv;
13820 struct wpa_driver_nl80211_data *drv = bss->drv;
13821 struct nl_msg *msg;
13822 int ret = -1;
13823 enum nl80211_auth_type type;
13824
Hai Shalomc3565922019-10-28 11:58:20 -070013825 /* Update Connection Params is intended for drivers that implement
13826 * internal SME and expect these updated connection params from
13827 * wpa_supplicant. Do not send this request for the drivers using
13828 * SME from wpa_supplicant.
13829 */
13830 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME)
13831 return 0;
13832
Vinayak Yadawade62409f2022-01-20 12:32:07 +053013833 /* Handle any connection param update here which might receive kernel handling in future */
Winnie Chen4138eec2022-11-10 16:32:53 +080013834#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawade62409f2022-01-20 12:32:07 +053013835 if (mask & WPA_DRV_UPDATE_TD_POLICY) {
13836 ret = nl80211_set_td_policy(priv, params->td_policy);
13837 if (ret) {
13838 wpa_dbg(drv->ctx, MSG_DEBUG,
13839 "nl80211: Update connect params command failed: ret=%d (%s)",
13840 ret, strerror(-ret));
13841 }
13842 return ret;
13843 }
Winnie Chen4138eec2022-11-10 16:32:53 +080013844#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Vinayak Yadawade62409f2022-01-20 12:32:07 +053013845
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013846 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
13847 if (!msg)
13848 goto fail;
13849
13850 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
13851 drv->ifindex);
13852
13853 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
13854 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
13855 params->wpa_ie))
13856 goto fail;
13857 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
13858 params->wpa_ie_len);
13859 }
13860
13861 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
13862 type = get_nl_auth_type(params->auth_alg);
13863 if (type == NL80211_AUTHTYPE_MAX ||
13864 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
13865 goto fail;
13866 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
13867 }
13868
13869 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
13870 nl80211_put_fils_connect_params(drv, params, msg))
13871 goto fail;
13872
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013873 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013874 msg = NULL;
13875 if (ret)
13876 wpa_dbg(drv->ctx, MSG_DEBUG,
13877 "nl80211: Update connect params command failed: ret=%d (%s)",
13878 ret, strerror(-ret));
13879
13880fail:
13881 nlmsg_free(msg);
13882 return ret;
13883}
13884
13885
Roshan Pius3a1667e2018-07-03 15:17:14 -070013886static int nl80211_send_external_auth_status(void *priv,
13887 struct external_auth *params)
13888{
13889 struct i802_bss *bss = priv;
13890 struct wpa_driver_nl80211_data *drv = bss->drv;
13891 struct nl_msg *msg = NULL;
13892 int ret = -1;
13893
Hai Shalom5f92bc92019-04-18 11:54:11 -070013894 /* External auth command/status is intended for drivers that implement
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080013895 * internal SME but want to offload authentication processing (e.g.,
13896 * SAE) to hostapd/wpa_supplicant. Do not send the status to drivers
Hai Shalom5f92bc92019-04-18 11:54:11 -070013897 * which do not support AP SME or use wpa_supplicant/hostapd SME.
13898 */
Hai Shalom81f62d82019-07-22 12:10:00 -070013899 if ((is_ap_interface(drv->nlmode) && !bss->drv->device_ap_sme) ||
Hai Shalom5f92bc92019-04-18 11:54:11 -070013900 (drv->capa.flags & WPA_DRIVER_FLAGS_SME))
13901 return -1;
13902
Roshan Pius3a1667e2018-07-03 15:17:14 -070013903 wpa_dbg(drv->ctx, MSG_DEBUG,
13904 "nl80211: External auth status: %u", params->status);
13905
13906 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
13907 if (!msg ||
13908 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
Hai Shalom5f92bc92019-04-18 11:54:11 -070013909 (params->ssid && params->ssid_len &&
13910 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
13911 (params->pmkid &&
13912 nla_put(msg, NL80211_ATTR_PMKID, PMKID_LEN, params->pmkid)) ||
13913 (params->bssid &&
13914 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid)))
Roshan Pius3a1667e2018-07-03 15:17:14 -070013915 goto fail;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013916 ret = send_and_recv_cmd(drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -070013917 msg = NULL;
13918 if (ret) {
13919 wpa_printf(MSG_DEBUG,
13920 "nl80211: External Auth status update failed: ret=%d (%s)",
13921 ret, strerror(-ret));
13922 goto fail;
13923 }
13924fail:
13925 nlmsg_free(msg);
13926 return ret;
13927}
13928
13929
Hai Shalom74f70d42019-02-11 14:42:39 -080013930static int nl80211_set_4addr_mode(void *priv, const char *bridge_ifname,
13931 int val)
13932{
13933 struct i802_bss *bss = priv;
13934 struct wpa_driver_nl80211_data *drv = bss->drv;
13935 struct nl_msg *msg;
13936 int ret = -ENOBUFS;
13937
13938 wpa_printf(MSG_DEBUG, "nl80211: %s 4addr mode (bridge_ifname: %s)",
13939 val ? "Enable" : "Disable", bridge_ifname);
13940
13941 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
13942 if (!msg || nla_put_u8(msg, NL80211_ATTR_4ADDR, val))
13943 goto fail;
13944
13945 if (bridge_ifname[0] && bss->added_if_into_bridge && !val) {
13946 if (linux_br_del_if(drv->global->ioctl_sock,
13947 bridge_ifname, bss->ifname)) {
13948 wpa_printf(MSG_ERROR,
13949 "nl80211: Failed to remove interface %s from bridge %s",
13950 bss->ifname, bridge_ifname);
13951 return -1;
13952 }
13953 bss->added_if_into_bridge = 0;
13954 }
13955
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013956 ret = send_and_recv_cmd(drv, msg);
Hai Shalom74f70d42019-02-11 14:42:39 -080013957 msg = NULL;
Hai Shalom60840252021-02-19 19:02:11 -080013958 if (ret && val && nl80211_get_4addr(bss) == 1) {
13959 wpa_printf(MSG_DEBUG,
13960 "nl80211: 4addr mode was already enabled");
13961 ret = 0;
13962 }
Hai Shalom74f70d42019-02-11 14:42:39 -080013963 if (!ret) {
13964 if (bridge_ifname[0] && val &&
13965 i802_check_bridge(drv, bss, bridge_ifname, bss->ifname) < 0)
13966 return -1;
13967 return 0;
13968 }
13969
13970fail:
13971 nlmsg_free(msg);
13972 wpa_printf(MSG_ERROR, "nl80211: Failed to enable/disable 4addr");
13973
13974 return ret;
13975}
13976
13977
Hai Shalome21d4e82020-04-29 16:34:06 -070013978#ifdef CONFIG_DPP
13979static int nl80211_dpp_listen(void *priv, bool enable)
13980{
13981 struct i802_bss *bss = priv;
13982 struct wpa_driver_nl80211_data *drv = bss->drv;
13983 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
13984 struct nl_sock *handle;
13985
13986 if (!drv->multicast_registrations || !bss->nl_mgmt)
13987 return 0; /* cannot do more than hope broadcast RX works */
13988
13989 wpa_printf(MSG_DEBUG,
13990 "nl80211: Update DPP Public Action frame registration (%s multicast RX)",
13991 enable ? "enable" : "disable");
13992 handle = (void *) (((intptr_t) bss->nl_mgmt) ^ ELOOP_SOCKET_INVALID);
13993 return nl80211_register_frame(bss, handle, type,
13994 (u8 *) "\x04\x09\x50\x6f\x9a\x1a", 6,
13995 enable);
13996}
13997#endif /* CONFIG_DPP */
13998
Winnie Chen4138eec2022-11-10 16:32:53 +080013999#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawade62409f2022-01-20 12:32:07 +053014000static int nl80211_set_td_policy(void *priv, u32 td_policy)
14001{
14002 struct i802_bss *bss = priv;
14003 struct wpa_driver_nl80211_data *drv = bss->drv;
14004 struct nl_msg *msg;
14005 int ret;
14006 struct nlattr *params;
14007
14008 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
14009 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
14010 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, BRCM_VENDOR_SCMD_SET_TD_POLICY) ||
14011 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
14012 (nla_put_u32(msg, BRCM_ATTR_DRIVER_TD_POLICY, td_policy))) {
14013 nl80211_nlmsg_clear(msg);
14014 nlmsg_free(msg);
14015 return -ENOBUFS;
14016 }
14017 nla_nest_end(msg, params);
14018 wpa_printf(MSG_DEBUG, "nl80211: Transition Disable Policy %d\n", td_policy);
14019
Sunil Ravib0ac25f2024-07-12 01:42:03 +000014020 ret = send_and_recv_cmd(drv, msg);
Vinayak Yadawade62409f2022-01-20 12:32:07 +053014021 if (ret) {
14022 wpa_printf(MSG_DEBUG, "nl80211: Transition Disable setting failed: ret=%d (%s)",
14023 ret, strerror(-ret));
14024 }
14025
14026 return ret;
14027}
Winnie Chen4138eec2022-11-10 16:32:53 +080014028#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Hai Shalome21d4e82020-04-29 16:34:06 -070014029
Sunil Ravi99c035e2024-07-12 01:42:03 +000014030static int nl80211_link_add(void *priv, u8 link_id, const u8 *addr,
14031 void *bss_ctx)
Sunil Ravi036cec52023-03-29 11:35:17 -070014032{
14033 struct i802_bss *bss = priv;
14034 struct wpa_driver_nl80211_data *drv = bss->drv;
14035 struct nl_msg *msg;
Sunil Ravi036cec52023-03-29 11:35:17 -070014036 int ret;
14037
14038 wpa_printf(MSG_DEBUG, "nl80211: MLD: add link_id=%u, addr=" MACSTR,
14039 link_id, MAC2STR(addr));
14040
14041 if (drv->nlmode != NL80211_IFTYPE_AP) {
14042 wpa_printf(MSG_DEBUG,
14043 "nl80211: MLD: cannot add link to iftype=%u",
14044 drv->nlmode);
14045 return -EINVAL;
14046 }
14047
Sunil Ravi99c035e2024-07-12 01:42:03 +000014048 if (link_id >= MAX_NUM_MLD_LINKS) {
14049 wpa_printf(MSG_DEBUG,
14050 "nl80211: invalid link_id=%u", link_id);
Sunil Ravi036cec52023-03-29 11:35:17 -070014051 return -EINVAL;
14052 }
14053
Sunil Ravi99c035e2024-07-12 01:42:03 +000014054 if (bss->valid_links & BIT(link_id)) {
14055 wpa_printf(MSG_DEBUG,
14056 "nl80211: MLD: Link %u already set", link_id);
14057 return -EINVAL;
Sunil Ravi036cec52023-03-29 11:35:17 -070014058 }
14059
Sunil Ravi99c035e2024-07-12 01:42:03 +000014060 if (!bss->valid_links) {
14061 /* Becoming MLD, verify we were not beaconing */
Sunil Ravi036cec52023-03-29 11:35:17 -070014062 if (bss->flink->beacon_set) {
14063 wpa_printf(MSG_DEBUG, "nl80211: BSS already beaconing");
14064 return -EINVAL;
14065 }
Sunil Ravi036cec52023-03-29 11:35:17 -070014066 }
14067
Sunil Ravi7f769292024-07-23 22:21:32 +000014068 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_ADD_LINK);
Sunil Ravi036cec52023-03-29 11:35:17 -070014069 if (!msg ||
14070 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id) ||
14071 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
14072 nlmsg_free(msg);
14073 return -ENOBUFS;
14074 }
14075
Sunil Ravib0ac25f2024-07-12 01:42:03 +000014076 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi036cec52023-03-29 11:35:17 -070014077 if (ret) {
14078 wpa_printf(MSG_DEBUG, "nl80211: add link failed. ret=%d (%s)",
14079 ret, strerror(-ret));
14080 return ret;
14081 }
14082
Sunil Ravi99c035e2024-07-12 01:42:03 +000014083 os_memcpy(bss->links[link_id].addr, addr, ETH_ALEN);
Sunil Ravi036cec52023-03-29 11:35:17 -070014084
Sunil Ravi99c035e2024-07-12 01:42:03 +000014085 /* The new link is the first one, make it the default */
14086 if (!bss->valid_links)
14087 bss->flink = &bss->links[link_id];
Sunil Ravi036cec52023-03-29 11:35:17 -070014088
Sunil Ravi99c035e2024-07-12 01:42:03 +000014089 bss->valid_links |= BIT(link_id);
14090 bss->links[link_id].ctx = bss_ctx;
14091
Sunil Ravi7f769292024-07-23 22:21:32 +000014092 wpa_printf(MSG_DEBUG, "nl80211: MLD: valid_links=0x%04x on %s",
14093 bss->valid_links, bss->ifname);
Sunil Ravi036cec52023-03-29 11:35:17 -070014094 return 0;
14095}
14096
14097
Sunil Ravi99c035e2024-07-12 01:42:03 +000014098#ifdef CONFIG_IEEE80211BE
14099static int wpa_driver_nl80211_link_sta_remove(void *priv, u8 link_id,
14100 const u8 *addr)
14101{
14102 struct i802_bss *bss = priv;
14103 struct wpa_driver_nl80211_data *drv = bss->drv;
14104 struct nl_msg *msg;
14105 int ret;
14106
14107 if (!(bss->valid_links & BIT(link_id)))
14108 return -ENOLINK;
14109
14110 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_REMOVE_LINK_STA)) ||
14111 nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN, addr) ||
14112 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) {
14113 nlmsg_free(msg);
14114 return -ENOBUFS;
14115 }
14116
14117 ret = send_and_recv_cmd(drv, msg);
14118 wpa_printf(MSG_DEBUG,
14119 "nl80211: link_sta_remove -> REMOVE_LINK_STA on link_id %u from MLD STA "
14120 MACSTR ", from %s --> %d (%s)",
14121 link_id, MAC2STR(addr), bss->ifname, ret, strerror(-ret));
14122
14123 return ret;
14124}
14125#endif /* CONFIG_IEEE80211BE */
14126
14127
Hai Shalomc1a21442022-02-04 13:43:00 -080014128#ifdef CONFIG_TESTING_OPTIONS
14129
14130static int testing_nl80211_register_frame(void *priv, u16 type,
14131 const u8 *match, size_t match_len,
14132 bool multicast)
14133{
14134 struct i802_bss *bss = priv;
14135 struct nl_sock *handle;
14136
14137 if (!bss->nl_mgmt)
14138 return -1;
14139 handle = (void *) (((intptr_t) bss->nl_mgmt) ^ ELOOP_SOCKET_INVALID);
14140 return nl80211_register_frame(bss, handle, type, match, match_len,
14141 multicast);
14142}
14143
14144
14145static int testing_nl80211_radio_disable(void *priv, int disabled)
14146{
14147 struct i802_bss *bss = priv;
14148 struct wpa_driver_nl80211_data *drv = bss->drv;
14149
14150 /* For now, this is supported only partially in station mode with
14151 * SME-in-wpa_supplicant case where the NL80211_ATTR_LOCAL_STATE_CHANGE
14152 * attribute can be used to avoid sending out the Deauthentication frame
14153 * to the currently associated AP. */
14154
14155 if (!disabled)
14156 return 0;
14157
14158 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
14159 return -1;
14160
14161 if (!drv->associated)
14162 return 0;
14163
14164 return wpa_driver_nl80211_mlme(drv, drv->bssid,
14165 NL80211_CMD_DEAUTHENTICATE,
14166 WLAN_REASON_PREV_AUTH_NOT_VALID, 1,
14167 drv->first_bss);
14168}
14169
14170#endif /* CONFIG_TESTING_OPTIONS */
14171
14172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014173const struct wpa_driver_ops wpa_driver_nl80211_ops = {
14174 .name = "nl80211",
14175 .desc = "Linux nl80211/cfg80211",
14176 .get_bssid = wpa_driver_nl80211_get_bssid,
14177 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014178 .set_key = driver_nl80211_set_key,
14179 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014180 .sched_scan = wpa_driver_nl80211_sched_scan,
14181 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Sunil Ravi99c035e2024-07-12 01:42:03 +000014182 .get_scan_results = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080014183 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014184 .deauthenticate = driver_nl80211_deauthenticate,
14185 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014186 .associate = wpa_driver_nl80211_associate,
14187 .global_init = nl80211_global_init,
14188 .global_deinit = nl80211_global_deinit,
14189 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014190 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014191 .get_capa = wpa_driver_nl80211_get_capa,
14192 .set_operstate = wpa_driver_nl80211_set_operstate,
14193 .set_supp_port = wpa_driver_nl80211_set_supp_port,
14194 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080014195 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014196 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070014197 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014198 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014199 .if_remove = driver_nl80211_if_remove,
14200 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080014201 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014202 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014203 .sta_remove = driver_nl80211_sta_remove,
Hai Shalomfdcde762020-04-02 11:19:20 -070014204 .tx_control_port = nl80211_tx_control_port,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014205 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
14206 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Hai Shalom81f62d82019-07-22 12:10:00 -070014207 .sta_set_airtime_weight = driver_nl80211_sta_set_airtime_weight,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014208 .hapd_init = i802_init,
14209 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070014210 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014211 .get_seqnum = i802_get_seqnum,
14212 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014213 .get_inact_sec = i802_get_inact_sec,
14214 .sta_clear_stats = i802_sta_clear_stats,
14215 .set_rts = i802_set_rts,
14216 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014217 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014218 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014219 .sta_deauth = i802_sta_deauth,
14220 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014221 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014222 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014223 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014224 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
14225 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
14226 .cancel_remain_on_channel =
14227 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014228 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014229 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070014230 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014231 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014232 .signal_monitor = nl80211_signal_monitor,
14233 .signal_poll = nl80211_signal_poll,
Sunil Ravi89eba102022-09-13 21:04:37 -070014234 .mlo_signal_poll = nl80211_mlo_signal_poll,
Hai Shalom74f70d42019-02-11 14:42:39 -080014235 .channel_info = nl80211_channel_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014236 .set_param = nl80211_set_param,
14237 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070014238 .add_pmkid = nl80211_add_pmkid,
14239 .remove_pmkid = nl80211_remove_pmkid,
14240 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014241 .set_rekey_info = nl80211_set_rekey_info,
14242 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014243 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070014244 .start_dfs_cac = nl80211_start_radar_detection,
14245 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014246#ifdef CONFIG_TDLS
14247 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
14248 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080014249 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
14250 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014251#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070014252 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Hai Shalom81f62d82019-07-22 12:10:00 -070014253 .update_dh_ie = nl80211_update_dh_ie,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070014254 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070014255 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070014256 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080014257 .switch_channel = nl80211_switch_channel,
Sunil Ravia04bd252022-05-02 22:54:18 -070014258#ifdef CONFIG_IEEE80211AX
14259 .switch_color = nl80211_switch_color,
14260#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014261#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070014262 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014263 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070014264 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080014265#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070014266#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -070014267#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070014268 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -070014269#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080014270#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080014271 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080014272 .set_qos_map = nl80211_set_qos_map,
Hai Shalomfdcde762020-04-02 11:19:20 -070014273 .get_wowlan = nl80211_get_wowlan,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070014274 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070014275 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080014276#ifdef CONFIG_MESH
14277 .init_mesh = wpa_driver_nl80211_init_mesh,
14278 .join_mesh = wpa_driver_nl80211_join_mesh,
14279 .leave_mesh = wpa_driver_nl80211_leave_mesh,
Hai Shalom81f62d82019-07-22 12:10:00 -070014280 .probe_mesh_link = nl80211_probe_mesh_link,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080014281#endif /* CONFIG_MESH */
14282 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
14283 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
14284 .br_port_set_attr = wpa_driver_br_port_set_attr,
14285 .br_set_net_param = wpa_driver_br_set_net_param,
14286 .add_tx_ts = nl80211_add_ts,
14287 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -070014288 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080014289#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070014290 .roaming = nl80211_roaming,
Roshan Pius3a1667e2018-07-03 15:17:14 -070014291 .disable_fils = nl80211_disable_fils,
Ravi Joshie6ccb162015-07-16 17:45:41 -070014292 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080014293 .get_pref_freq_list = nl80211_get_pref_freq_list,
14294 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070014295 .p2p_lo_start = nl80211_p2p_lo_start,
14296 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070014297 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080014298 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070014299#ifdef CONFIG_MBO
14300 .get_bss_transition_status = nl80211_get_bss_transition_status,
14301 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
14302#endif /* CONFIG_MBO */
Hai Shalom899fcc72020-10-19 14:38:18 -070014303 .set_bssid_tmp_disallow = nl80211_set_bssid_tmp_disallow,
Hai Shalomc3565922019-10-28 11:58:20 -070014304 .add_sta_node = nl80211_add_sta_node,
Sunil Ravi89eba102022-09-13 21:04:37 -070014305#ifdef CONFIG_PASN
14306 .send_pasn_resp = nl80211_send_pasn_resp,
14307 .set_secure_ranging_ctx = nl80211_set_secure_ranging_ctx,
14308#endif /* CONFIG_PASN */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080014309#endif /* CONFIG_DRIVER_NL80211_QCA */
Hai Shalomc1a21442022-02-04 13:43:00 -080014310 .do_acs = nl80211_do_acs,
Dmitry Shmidt849734c2016-05-27 09:59:01 -070014311 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070014312 .get_ext_capab = nl80211_get_ext_capab,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000014313 .get_mld_capab = nl80211_get_mld_capab,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070014314 .update_connect_params = nl80211_update_connection_params,
Roshan Pius3a1667e2018-07-03 15:17:14 -070014315 .send_external_auth_status = nl80211_send_external_auth_status,
Hai Shalom74f70d42019-02-11 14:42:39 -080014316 .set_4addr_mode = nl80211_set_4addr_mode,
Hai Shalome21d4e82020-04-29 16:34:06 -070014317#ifdef CONFIG_DPP
14318 .dpp_listen = nl80211_dpp_listen,
14319#endif /* CONFIG_DPP */
Sunil Ravi89eba102022-09-13 21:04:37 -070014320 .get_sta_mlo_info = nl80211_get_sta_mlo_info,
Sunil Ravi036cec52023-03-29 11:35:17 -070014321 .link_add = nl80211_link_add,
Sunil Ravi99c035e2024-07-12 01:42:03 +000014322#ifdef CONFIG_IEEE80211BE
14323 .link_remove = driver_nl80211_link_remove,
14324 .is_drv_shared = nl80211_is_drv_shared,
14325 .link_sta_remove = wpa_driver_nl80211_link_sta_remove,
14326#endif /* CONFIG_IEEE80211BE */
Hai Shalomc1a21442022-02-04 13:43:00 -080014327#ifdef CONFIG_TESTING_OPTIONS
14328 .register_frame = testing_nl80211_register_frame,
14329 .radio_disable = testing_nl80211_radio_disable,
14330#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014331};